2 * sysstat - sa_wrap.c: Functions used in activity.c
3 * (C) 1999-2013 by Sebastien GODARD (sysstat <at> orange.fr)
5 ***************************************************************************
6 * This program is free software; you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation; either version 2 of the License, or (at your *
9 * option) any later version. *
11 * This program is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * You should have received a copy of the GNU General Public License along *
17 * with this program; if not, write to the Free Software Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************
25 #include "rd_sensors.h"
27 extern unsigned int flags;
28 extern struct record_header record_hdr;
31 ***************************************************************************
32 * Read CPU statistics.
35 * @a Activity structure.
38 * @a Activity structure with statistics.
39 ***************************************************************************
41 __read_funct_t wrap_read_stat_cpu(struct activity *a)
43 struct stats_cpu *st_cpu
44 = (struct stats_cpu *) a->_buf0;
46 /* Read CPU statistics */
47 read_stat_cpu(st_cpu, a->nr, &record_hdr.uptime, &record_hdr.uptime0);
53 ***************************************************************************
54 * Read process (task) creation and context switch statistics.
57 * @a Activity structure.
60 * @a Activity structure with statistics.
61 ***************************************************************************
63 __read_funct_t wrap_read_stat_pcsw(struct activity *a)
65 struct stats_pcsw *st_pcsw
66 = (struct stats_pcsw *) a->_buf0;
68 /* Read process and context switch stats */
69 read_stat_pcsw(st_pcsw);
75 ***************************************************************************
76 * Read interrupt statistics.
79 * @a Activity structure.
82 * @a Activity structure with statistics.
83 ***************************************************************************
85 __read_funct_t wrap_read_stat_irq(struct activity *a)
87 struct stats_irq *st_irq
88 = (struct stats_irq *) a->_buf0;
90 /* Read interrupts stats */
91 read_stat_irq(st_irq, a->nr);
97 ***************************************************************************
98 * Read queue and load statistics.
101 * @a Activity structure.
104 * @a Activity structure with statistics.
105 ***************************************************************************
107 __read_funct_t wrap_read_loadavg(struct activity *a)
109 struct stats_queue *st_queue
110 = (struct stats_queue *) a->_buf0;
112 /* Read queue and load stats */
113 read_loadavg(st_queue);
119 ***************************************************************************
120 * Read memory statistics.
123 * @a Activity structure.
126 * @a Activity structure with statistics.
127 ***************************************************************************
129 __read_funct_t wrap_read_meminfo(struct activity *a)
131 struct stats_memory *st_memory
132 = (struct stats_memory *) a->_buf0;
134 /* Read memory stats */
135 read_meminfo(st_memory);
141 ***************************************************************************
142 * Read swapping statistics.
145 * @a Activity structure.
148 * @a Activity structure with statistics.
149 ***************************************************************************
151 __read_funct_t wrap_read_swap(struct activity *a)
153 struct stats_swap *st_swap
154 = (struct stats_swap *) a->_buf0;
156 /* Read stats from /proc/vmstat */
157 read_vmstat_swap(st_swap);
163 ***************************************************************************
164 * Read paging statistics.
167 * @a Activity structure.
170 * @a Activity structure with statistics.
171 ***************************************************************************
173 __read_funct_t wrap_read_paging(struct activity *a)
175 struct stats_paging *st_paging
176 = (struct stats_paging *) a->_buf0;
178 /* Read stats from /proc/vmstat */
179 read_vmstat_paging(st_paging);
185 ***************************************************************************
186 * Read I/O and transfer rates statistics.
189 * @a Activity structure.
192 * @a Activity structure with statistics.
193 ***************************************************************************
195 __read_funct_t wrap_read_io(struct activity *a)
197 struct stats_io *st_io
198 = (struct stats_io *) a->_buf0;
200 /* Read stats from /proc/diskstats */
201 read_diskstats_io(st_io);
207 ***************************************************************************
208 * Read block devices statistics.
211 * @a Activity structure.
214 * @a Activity structure with statistics.
215 ***************************************************************************
217 __read_funct_t wrap_read_disk(struct activity *a)
219 struct stats_disk *st_disk
220 = (struct stats_disk *) a->_buf0;
222 /* Read stats from /proc/diskstats */
223 read_diskstats_disk(st_disk, a->nr, COLLECT_PARTITIONS(a->opt_flags));
229 ***************************************************************************
230 * Read serial lines statistics.
233 * @a Activity structure.
236 * @a Activity structure with statistics.
237 ***************************************************************************
239 __read_funct_t wrap_read_tty_driver_serial(struct activity *a)
241 struct stats_serial *st_serial
242 = (struct stats_serial *) a->_buf0;
244 /* Read serial lines stats */
245 read_tty_driver_serial(st_serial, a->nr);
251 ***************************************************************************
252 * Read kernel tables statistics.
255 * @a Activity structure.
258 * @a Activity structure with statistics.
259 ***************************************************************************
261 __read_funct_t wrap_read_kernel_tables(struct activity *a)
263 struct stats_ktables *st_ktables
264 = (struct stats_ktables *) a->_buf0;
266 /* Read kernel tables stats */
267 read_kernel_tables(st_ktables);
273 ***************************************************************************
274 * Read network interfaces statistics.
277 * @a Activity structure.
280 * @a Activity structure with statistics.
281 ***************************************************************************
283 __read_funct_t wrap_read_net_dev(struct activity *a)
285 struct stats_net_dev *st_net_dev
286 = (struct stats_net_dev *) a->_buf0;
289 /* Read network interfaces stats */
290 dev = read_net_dev(st_net_dev, a->nr);
292 /* No data read. Exit */
295 /* Read duplex and speed info for each interface */
296 read_if_info(st_net_dev, dev);
302 ***************************************************************************
303 * Read network interfaces errors statistics.
306 * @a Activity structure.
309 * @a Activity structure with statistics.
310 ***************************************************************************
312 __read_funct_t wrap_read_net_edev(struct activity *a)
314 struct stats_net_edev *st_net_edev
315 = (struct stats_net_edev *) a->_buf0;
317 /* Read network interfaces errors stats */
318 read_net_edev(st_net_edev, a->nr);
324 ***************************************************************************
325 * Read NFS client statistics.
328 * @a Activity structure.
331 * @a Activity structure with statistics.
332 ***************************************************************************
334 __read_funct_t wrap_read_net_nfs(struct activity *a)
336 struct stats_net_nfs *st_net_nfs
337 = (struct stats_net_nfs *) a->_buf0;
339 /* Read NFS client stats */
340 read_net_nfs(st_net_nfs);
346 ***************************************************************************
347 * Read NFS server statistics.
350 * @a Activity structure.
353 * @a Activity structure with statistics.
354 ***************************************************************************
356 __read_funct_t wrap_read_net_nfsd(struct activity *a)
358 struct stats_net_nfsd *st_net_nfsd
359 = (struct stats_net_nfsd *) a->_buf0;
361 /* Read NFS server stats */
362 read_net_nfsd(st_net_nfsd);
368 ***************************************************************************
369 * Read network sockets statistics.
372 * @a Activity structure.
375 * @a Activity structure with statistics.
376 ***************************************************************************
378 __read_funct_t wrap_read_net_sock(struct activity *a)
380 struct stats_net_sock *st_net_sock
381 = (struct stats_net_sock *) a->_buf0;
383 /* Read network sockets stats */
384 read_net_sock(st_net_sock);
390 ***************************************************************************
391 * Read IP statistics.
394 * @a Activity structure.
397 * @a Activity structure with statistics.
398 ***************************************************************************
400 __read_funct_t wrap_read_net_ip(struct activity *a)
402 struct stats_net_ip *st_net_ip
403 = (struct stats_net_ip *) a->_buf0;
406 read_net_ip(st_net_ip);
412 ***************************************************************************
413 * Read IP error statistics.
416 * @a Activity structure.
419 * @a Activity structure with statistics.
420 ***************************************************************************
422 __read_funct_t wrap_read_net_eip(struct activity *a)
424 struct stats_net_eip *st_net_eip
425 = (struct stats_net_eip *) a->_buf0;
427 /* Read IP error stats */
428 read_net_eip(st_net_eip);
434 ***************************************************************************
435 * Read ICMP statistics.
438 * @a Activity structure.
441 * @a Activity structure with statistics.
442 ***************************************************************************
444 __read_funct_t wrap_read_net_icmp(struct activity *a)
446 struct stats_net_icmp *st_net_icmp
447 = (struct stats_net_icmp *) a->_buf0;
449 /* Read ICMP stats */
450 read_net_icmp(st_net_icmp);
456 ***************************************************************************
457 * Read ICMP error statistics.
460 * @a Activity structure.
463 * @a Activity structure with statistics.
464 ***************************************************************************
466 __read_funct_t wrap_read_net_eicmp(struct activity *a)
468 struct stats_net_eicmp *st_net_eicmp
469 = (struct stats_net_eicmp *) a->_buf0;
471 /* Read ICMP error stats */
472 read_net_eicmp(st_net_eicmp);
478 ***************************************************************************
479 * Read TCP statistics.
482 * @a Activity structure.
485 * @a Activity structure with statistics.
486 ***************************************************************************
488 __read_funct_t wrap_read_net_tcp(struct activity *a)
490 struct stats_net_tcp *st_net_tcp
491 = (struct stats_net_tcp *) a->_buf0;
494 read_net_tcp(st_net_tcp);
500 ***************************************************************************
501 * Read TCP error statistics.
504 * @a Activity structure.
507 * @a Activity structure with statistics.
508 ***************************************************************************
510 __read_funct_t wrap_read_net_etcp(struct activity *a)
512 struct stats_net_etcp *st_net_etcp
513 = (struct stats_net_etcp *) a->_buf0;
515 /* Read TCP error stats */
516 read_net_etcp(st_net_etcp);
522 ***************************************************************************
523 * Read UDP statistics.
526 * @a Activity structure.
529 * @a Activity structure with statistics.
530 ***************************************************************************
532 __read_funct_t wrap_read_net_udp(struct activity *a)
534 struct stats_net_udp *st_net_udp
535 = (struct stats_net_udp *) a->_buf0;
538 read_net_udp(st_net_udp);
544 ***************************************************************************
545 * Read IPv6 network sockets statistics.
548 * @a Activity structure.
551 * @a Activity structure with statistics.
552 ***************************************************************************
554 __read_funct_t wrap_read_net_sock6(struct activity *a)
556 struct stats_net_sock6 *st_net_sock6
557 = (struct stats_net_sock6 *) a->_buf0;
559 /* Read IPv6 network sockets stats */
560 read_net_sock6(st_net_sock6);
566 ***************************************************************************
567 * Read IPv6 statistics.
570 * @a Activity structure.
573 * @a Activity structure with statistics.
574 ***************************************************************************
576 __read_funct_t wrap_read_net_ip6(struct activity *a)
578 struct stats_net_ip6 *st_net_ip6
579 = (struct stats_net_ip6 *) a->_buf0;
581 /* Read IPv6 stats */
582 read_net_ip6(st_net_ip6);
588 ***************************************************************************
589 * Read IPv6 error statistics.
592 * @a Activity structure.
595 * @a Activity structure with statistics.
596 ***************************************************************************
598 __read_funct_t wrap_read_net_eip6(struct activity *a)
600 struct stats_net_eip6 *st_net_eip6
601 = (struct stats_net_eip6 *) a->_buf0;
603 /* Read IPv6 error stats */
604 read_net_eip6(st_net_eip6);
610 ***************************************************************************
611 * Read ICMPv6 statistics.
614 * @a Activity structure.
617 * @a Activity structure with statistics.
618 ***************************************************************************
620 __read_funct_t wrap_read_net_icmp6(struct activity *a)
622 struct stats_net_icmp6 *st_net_icmp6
623 = (struct stats_net_icmp6 *) a->_buf0;
625 /* Read ICMPv6 stats */
626 read_net_icmp6(st_net_icmp6);
632 ***************************************************************************
633 * Read ICMPv6 error statistics.
636 * @a Activity structure.
639 * @a Activity structure with statistics.
640 ***************************************************************************
642 __read_funct_t wrap_read_net_eicmp6(struct activity *a)
644 struct stats_net_eicmp6 *st_net_eicmp6
645 = (struct stats_net_eicmp6 *) a->_buf0;
647 /* Read ICMPv6 error stats */
648 read_net_eicmp6(st_net_eicmp6);
654 ***************************************************************************
655 * Read UDPv6 statistics.
658 * @a Activity structure.
661 * @a Activity structure with statistics.
662 ***************************************************************************
664 __read_funct_t wrap_read_net_udp6(struct activity *a)
666 struct stats_net_udp6 *st_net_udp6
667 = (struct stats_net_udp6 *) a->_buf0;
669 /* Read UDPv6 stats */
670 read_net_udp6(st_net_udp6);
676 ***************************************************************************
677 * Read CPU frequency statistics.
680 * @a Activity structure.
683 * @a Activity structure with statistics.
684 ***************************************************************************
686 __read_funct_t wrap_read_cpuinfo(struct activity *a)
688 struct stats_pwr_cpufreq *st_pwr_cpufreq
689 = (struct stats_pwr_cpufreq *) a->_buf0;
691 /* Read CPU frequency stats */
692 read_cpuinfo(st_pwr_cpufreq, a->nr);
698 ***************************************************************************
699 * Read fan statistics.
702 * @a Activity structure.
705 * @a Activity structure with statistics.
706 ***************************************************************************
708 __read_funct_t wrap_read_fan(struct activity *a)
710 struct stats_pwr_fan *st_pwr_fan
711 = (struct stats_pwr_fan *) a->_buf0;
714 read_fan(st_pwr_fan, a->nr);
720 ***************************************************************************
721 * Read temperature statistics.
724 * @a Activity structure.
727 * @a Activity structure with statistics.
728 ***************************************************************************
730 __read_funct_t wrap_read_temp(struct activity *a)
732 struct stats_pwr_temp *st_pwr_temp
733 = (struct stats_pwr_temp *) a->_buf0;
735 /* Read temperature stats */
736 read_temp(st_pwr_temp, a->nr);
742 ***************************************************************************
743 * Read voltage input statistics.
746 * @a Activity structure.
749 * @a Activity structure with statistics.
750 ***************************************************************************
752 __read_funct_t wrap_read_in(struct activity *a)
754 struct stats_pwr_in *st_pwr_in
755 = (struct stats_pwr_in *) a->_buf0;
757 /* Read voltage input stats */
758 read_in(st_pwr_in, a->nr);
764 ***************************************************************************
765 * Read hugepages statistics.
768 * @a Activity structure.
771 * @a Activity structure with statistics.
772 ***************************************************************************
774 __read_funct_t wrap_read_meminfo_huge(struct activity *a)
776 struct stats_huge *st_huge
777 = (struct stats_huge *) a->_buf0;
779 /* Read hugepages stats */
780 read_meminfo_huge(st_huge);
786 ***************************************************************************
787 * Read weighted CPU frequency statistics.
790 * @a Activity structure.
793 * @a Activity structure with statistics.
794 ***************************************************************************
796 __read_funct_t wrap_read_time_in_state(struct activity *a)
800 struct stats_pwr_wghfreq *st_pwr_wghfreq
801 = (struct stats_pwr_wghfreq *) a->_buf0;
802 struct stats_pwr_wghfreq *st_pwr_wghfreq_i, *st_pwr_wghfreq_j, *st_pwr_wghfreq_all_j;
804 while (cpu < (a->nr - 1)) {
805 /* Read current CPU time-in-state data */
806 st_pwr_wghfreq_i = st_pwr_wghfreq + (cpu + 1) * a->nr2;
807 read_time_in_state(st_pwr_wghfreq_i, cpu, a->nr2);
809 /* Also save data for CPU 'all' */
810 for (j = 0; j < a->nr2; j++) {
811 st_pwr_wghfreq_j = st_pwr_wghfreq_i + j; /* CPU #cpu, state #j */
812 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
814 /* Assume that possible frequencies are the same for all CPUs */
815 st_pwr_wghfreq_all_j->freq = st_pwr_wghfreq_j->freq;
817 st_pwr_wghfreq_all_j->time_in_state += st_pwr_wghfreq_j->time_in_state;
822 /* Special processing for non SMP kernels: Only CPU 'all' is available */
824 read_time_in_state(st_pwr_wghfreq, 0, a->nr2);
827 for (j = 0; j < a->nr2; j++) {
828 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
829 st_pwr_wghfreq_all_j->time_in_state /= (a->nr - 1);
837 ***************************************************************************
838 * Read USB devices statistics.
841 * @a Activity structure.
844 * @a Activity structure with statistics.
845 ***************************************************************************
847 __read_funct_t wrap_read_bus_usb_dev(struct activity *a)
849 struct stats_pwr_usb *st_pwr_usb
850 = (struct stats_pwr_usb *) a->_buf0;
852 /* Read USB devices stats */
853 read_bus_usb_dev(st_pwr_usb, a->nr);
859 ***************************************************************************
860 * Read filesystem statistics.
863 * @a Activity structure.
866 * @a Activity structure with statistics.
867 ***************************************************************************
869 __read_funct_t wrap_read_filesystem(struct activity *a)
871 struct stats_filesystem *st_filesystem
872 = (struct stats_filesystem *) a->_buf0;
874 /* Read filesystems from /etc/mtab */
875 read_filesystem(st_filesystem, a->nr);
881 ***************************************************************************
882 * Count number of interrupts that are in /proc/stat file.
883 * Truncate the number of different individual interrupts to NR_IRQS.
886 * @a Activity structure.
889 * Number of interrupts, including total number of interrupts.
890 * Value in [0, NR_IRQS + 1].
891 ***************************************************************************
893 __nr_t wrap_get_irq_nr(struct activity *a)
897 if ((n = get_irq_nr()) > (a->bitmap->b_size + 1)) {
898 n = a->bitmap->b_size + 1;
905 ***************************************************************************
906 * Find number of serial lines that support tx/rx accounting
907 * in /proc/tty/driver/serial file.
910 * @a Activity structure.
913 * Number of serial lines supporting tx/rx accouting + a pre-allocation
915 ***************************************************************************
917 __nr_t wrap_get_serial_nr(struct activity *a)
921 if ((n = get_serial_nr()) > 0)
922 return n + NR_SERIAL_PREALLOC;
928 ***************************************************************************
929 * Find number of interfaces (network devices) that are in /proc/net/dev
933 * @a Activity structure.
936 * Number of network interfaces + a pre-allocation constant.
937 ***************************************************************************
939 __nr_t wrap_get_iface_nr(struct activity *a)
943 if ((n = get_iface_nr()) > 0)
944 return n + NR_IFACE_PREALLOC;
950 ***************************************************************************
951 * Compute number of CPU structures to allocate.
954 * @a Activity structure.
957 * Number of structures (value in [1, NR_CPUS + 1]).
958 * 1 means that there is only one proc and non SMP kernel.
959 * 2 means one proc and SMP kernel.
961 ***************************************************************************
963 __nr_t wrap_get_cpu_nr(struct activity *a)
965 return (get_cpu_nr(a->bitmap->b_size) + 1);
969 ***************************************************************************
970 * Get number of devices in /proc/diskstats.
971 * Always done, since disk stats must be read at least for sar -b
975 * @a Activity structure.
978 * Number of devices + a pre-allocation constant.
979 ***************************************************************************
981 __nr_t wrap_get_disk_nr(struct activity *a)
984 unsigned int f = COLLECT_PARTITIONS(a->opt_flags);
986 if ((n = get_disk_nr(f)) > 0)
987 return n + NR_DISK_PREALLOC;
993 ***************************************************************************
994 * Get number of fan structures to allocate.
997 * @a Activity structure.
1000 * Number of structures.
1001 ***************************************************************************
1003 __nr_t wrap_get_fan_nr(struct activity *a)
1005 return (get_fan_nr());
1009 ***************************************************************************
1010 * Get number of temp structures to allocate.
1013 * @a Activity structure.
1016 * Number of structures.
1017 ***************************************************************************
1019 __nr_t wrap_get_temp_nr(struct activity *a)
1021 return (get_temp_nr());
1025 ***************************************************************************
1026 * Get number of voltage input structures to allocate.
1029 * @a Activity structure.
1032 * Number of structures.
1033 ***************************************************************************
1035 __nr_t wrap_get_in_nr(struct activity *a)
1037 return (get_in_nr());
1041 ***************************************************************************
1042 * Count number of possible frequencies for CPU#0.
1045 * @a Activity structure.
1048 * Number of CPU frequencies + a pre-allocation constant.
1049 ***************************************************************************
1051 __nr_t wrap_get_freq_nr(struct activity *a)
1055 if ((n = get_freq_nr()) > 0)
1056 return n + NR_FREQ_PREALLOC;
1062 ***************************************************************************
1063 * Count number of USB devices plugged into the system.
1066 * @a Activity structure.
1069 * Number of USB devices + a pre-allocation constant.
1070 ***************************************************************************
1072 __nr_t wrap_get_usb_nr(struct activity *a)
1076 if ((n = get_usb_nr()) >= 0)
1077 /* Return a positive number even if no USB devices have been found */
1078 return n + NR_USB_PREALLOC;
1084 ***************************************************************************
1085 * Get number of mounted filesystems from /etc/mtab. Don't take into account
1086 * pseudo-filesystems.
1089 * @a Activity structure.
1092 * Number of filesystems + a pre-allocation constant.
1093 ***************************************************************************
1095 __nr_t wrap_get_filesystem_nr(struct activity *a)
1099 if ((n = get_filesystem_nr()) > 0)
1100 return n + NR_FILESYSTEM_PREALLOC;