2 * sysstat - sa_wrap.c: Functions used in activity.c
3 * (C) 1999-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
19 ***************************************************************************
28 #include "rd_sensors.h"
30 extern unsigned int flags;
31 extern struct record_header record_hdr;
34 ***************************************************************************
35 * Read CPU statistics.
38 * @a Activity structure.
41 * @a Activity structure with statistics.
42 ***************************************************************************
44 __read_funct_t wrap_read_stat_cpu(struct activity *a)
46 struct stats_cpu *st_cpu
47 = (struct stats_cpu *) a->_buf0;
49 /* Read CPU statistics */
50 read_stat_cpu(st_cpu, a->nr, &record_hdr.uptime, &record_hdr.uptime0);
56 ***************************************************************************
57 * Read process (task) creation and context switch statistics.
60 * @a Activity structure.
63 * @a Activity structure with statistics.
64 ***************************************************************************
66 __read_funct_t wrap_read_stat_pcsw(struct activity *a)
68 struct stats_pcsw *st_pcsw
69 = (struct stats_pcsw *) a->_buf0;
71 /* Read process and context switch stats */
72 read_stat_pcsw(st_pcsw);
78 ***************************************************************************
79 * Read interrupt statistics.
82 * @a Activity structure.
85 * @a Activity structure with statistics.
86 ***************************************************************************
88 __read_funct_t wrap_read_stat_irq(struct activity *a)
90 struct stats_irq *st_irq
91 = (struct stats_irq *) a->_buf0;
93 /* Read interrupts stats */
94 read_stat_irq(st_irq, a->nr);
100 ***************************************************************************
101 * Read queue and load statistics.
104 * @a Activity structure.
107 * @a Activity structure with statistics.
108 ***************************************************************************
110 __read_funct_t wrap_read_loadavg(struct activity *a)
112 struct stats_queue *st_queue
113 = (struct stats_queue *) a->_buf0;
115 /* Read queue and load stats */
116 read_loadavg(st_queue);
122 ***************************************************************************
123 * Read memory statistics.
126 * @a Activity structure.
129 * @a Activity structure with statistics.
130 ***************************************************************************
132 __read_funct_t wrap_read_meminfo(struct activity *a)
134 struct stats_memory *st_memory
135 = (struct stats_memory *) a->_buf0;
137 /* Read memory stats */
138 read_meminfo(st_memory);
144 ***************************************************************************
145 * Read swapping statistics.
148 * @a Activity structure.
151 * @a Activity structure with statistics.
152 ***************************************************************************
154 __read_funct_t wrap_read_swap(struct activity *a)
156 struct stats_swap *st_swap
157 = (struct stats_swap *) a->_buf0;
159 /* Read stats from /proc/vmstat */
160 read_vmstat_swap(st_swap);
166 ***************************************************************************
167 * Read paging statistics.
170 * @a Activity structure.
173 * @a Activity structure with statistics.
174 ***************************************************************************
176 __read_funct_t wrap_read_paging(struct activity *a)
178 struct stats_paging *st_paging
179 = (struct stats_paging *) a->_buf0;
181 /* Read stats from /proc/vmstat */
182 read_vmstat_paging(st_paging);
188 ***************************************************************************
189 * Read I/O and transfer rates statistics.
192 * @a Activity structure.
195 * @a Activity structure with statistics.
196 ***************************************************************************
198 __read_funct_t wrap_read_io(struct activity *a)
200 struct stats_io *st_io
201 = (struct stats_io *) a->_buf0;
203 /* Read stats from /proc/diskstats */
204 read_diskstats_io(st_io);
210 ***************************************************************************
211 * Read block devices statistics.
214 * @a Activity structure.
217 * @a Activity structure with statistics.
218 ***************************************************************************
220 __read_funct_t wrap_read_disk(struct activity *a)
222 struct stats_disk *st_disk
223 = (struct stats_disk *) a->_buf0;
225 /* Read stats from /proc/diskstats */
226 read_diskstats_disk(st_disk, a->nr, COLLECT_PARTITIONS(a->opt_flags));
232 ***************************************************************************
233 * Read serial lines statistics.
236 * @a Activity structure.
239 * @a Activity structure with statistics.
240 ***************************************************************************
242 __read_funct_t wrap_read_tty_driver_serial(struct activity *a)
244 struct stats_serial *st_serial
245 = (struct stats_serial *) a->_buf0;
247 /* Read serial lines stats */
248 read_tty_driver_serial(st_serial, a->nr);
254 ***************************************************************************
255 * Read kernel tables statistics.
258 * @a Activity structure.
261 * @a Activity structure with statistics.
262 ***************************************************************************
264 __read_funct_t wrap_read_kernel_tables(struct activity *a)
266 struct stats_ktables *st_ktables
267 = (struct stats_ktables *) a->_buf0;
269 /* Read kernel tables stats */
270 read_kernel_tables(st_ktables);
276 ***************************************************************************
277 * Read network interfaces statistics.
280 * @a Activity structure.
283 * @a Activity structure with statistics.
284 ***************************************************************************
286 __read_funct_t wrap_read_net_dev(struct activity *a)
288 struct stats_net_dev *st_net_dev
289 = (struct stats_net_dev *) a->_buf0;
292 /* Read network interfaces stats */
293 dev = read_net_dev(st_net_dev, a->nr);
295 /* No data read. Exit */
298 /* Read duplex and speed info for each interface */
299 read_if_info(st_net_dev, dev);
305 ***************************************************************************
306 * Read network interfaces errors statistics.
309 * @a Activity structure.
312 * @a Activity structure with statistics.
313 ***************************************************************************
315 __read_funct_t wrap_read_net_edev(struct activity *a)
317 struct stats_net_edev *st_net_edev
318 = (struct stats_net_edev *) a->_buf0;
320 /* Read network interfaces errors stats */
321 read_net_edev(st_net_edev, a->nr);
327 ***************************************************************************
328 * Read NFS client statistics.
331 * @a Activity structure.
334 * @a Activity structure with statistics.
335 ***************************************************************************
337 __read_funct_t wrap_read_net_nfs(struct activity *a)
339 struct stats_net_nfs *st_net_nfs
340 = (struct stats_net_nfs *) a->_buf0;
342 /* Read NFS client stats */
343 read_net_nfs(st_net_nfs);
349 ***************************************************************************
350 * Read NFS server statistics.
353 * @a Activity structure.
356 * @a Activity structure with statistics.
357 ***************************************************************************
359 __read_funct_t wrap_read_net_nfsd(struct activity *a)
361 struct stats_net_nfsd *st_net_nfsd
362 = (struct stats_net_nfsd *) a->_buf0;
364 /* Read NFS server stats */
365 read_net_nfsd(st_net_nfsd);
371 ***************************************************************************
372 * Read network sockets statistics.
375 * @a Activity structure.
378 * @a Activity structure with statistics.
379 ***************************************************************************
381 __read_funct_t wrap_read_net_sock(struct activity *a)
383 struct stats_net_sock *st_net_sock
384 = (struct stats_net_sock *) a->_buf0;
386 /* Read network sockets stats */
387 read_net_sock(st_net_sock);
393 ***************************************************************************
394 * Read IP statistics.
397 * @a Activity structure.
400 * @a Activity structure with statistics.
401 ***************************************************************************
403 __read_funct_t wrap_read_net_ip(struct activity *a)
405 struct stats_net_ip *st_net_ip
406 = (struct stats_net_ip *) a->_buf0;
409 read_net_ip(st_net_ip);
415 ***************************************************************************
416 * Read IP error statistics.
419 * @a Activity structure.
422 * @a Activity structure with statistics.
423 ***************************************************************************
425 __read_funct_t wrap_read_net_eip(struct activity *a)
427 struct stats_net_eip *st_net_eip
428 = (struct stats_net_eip *) a->_buf0;
430 /* Read IP error stats */
431 read_net_eip(st_net_eip);
437 ***************************************************************************
438 * Read ICMP statistics.
441 * @a Activity structure.
444 * @a Activity structure with statistics.
445 ***************************************************************************
447 __read_funct_t wrap_read_net_icmp(struct activity *a)
449 struct stats_net_icmp *st_net_icmp
450 = (struct stats_net_icmp *) a->_buf0;
452 /* Read ICMP stats */
453 read_net_icmp(st_net_icmp);
459 ***************************************************************************
460 * Read ICMP error statistics.
463 * @a Activity structure.
466 * @a Activity structure with statistics.
467 ***************************************************************************
469 __read_funct_t wrap_read_net_eicmp(struct activity *a)
471 struct stats_net_eicmp *st_net_eicmp
472 = (struct stats_net_eicmp *) a->_buf0;
474 /* Read ICMP error stats */
475 read_net_eicmp(st_net_eicmp);
481 ***************************************************************************
482 * Read TCP statistics.
485 * @a Activity structure.
488 * @a Activity structure with statistics.
489 ***************************************************************************
491 __read_funct_t wrap_read_net_tcp(struct activity *a)
493 struct stats_net_tcp *st_net_tcp
494 = (struct stats_net_tcp *) a->_buf0;
497 read_net_tcp(st_net_tcp);
503 ***************************************************************************
504 * Read TCP error statistics.
507 * @a Activity structure.
510 * @a Activity structure with statistics.
511 ***************************************************************************
513 __read_funct_t wrap_read_net_etcp(struct activity *a)
515 struct stats_net_etcp *st_net_etcp
516 = (struct stats_net_etcp *) a->_buf0;
518 /* Read TCP error stats */
519 read_net_etcp(st_net_etcp);
525 ***************************************************************************
526 * Read UDP statistics.
529 * @a Activity structure.
532 * @a Activity structure with statistics.
533 ***************************************************************************
535 __read_funct_t wrap_read_net_udp(struct activity *a)
537 struct stats_net_udp *st_net_udp
538 = (struct stats_net_udp *) a->_buf0;
541 read_net_udp(st_net_udp);
547 ***************************************************************************
548 * Read IPv6 network sockets statistics.
551 * @a Activity structure.
554 * @a Activity structure with statistics.
555 ***************************************************************************
557 __read_funct_t wrap_read_net_sock6(struct activity *a)
559 struct stats_net_sock6 *st_net_sock6
560 = (struct stats_net_sock6 *) a->_buf0;
562 /* Read IPv6 network sockets stats */
563 read_net_sock6(st_net_sock6);
569 ***************************************************************************
570 * Read IPv6 statistics.
573 * @a Activity structure.
576 * @a Activity structure with statistics.
577 ***************************************************************************
579 __read_funct_t wrap_read_net_ip6(struct activity *a)
581 struct stats_net_ip6 *st_net_ip6
582 = (struct stats_net_ip6 *) a->_buf0;
584 /* Read IPv6 stats */
585 read_net_ip6(st_net_ip6);
591 ***************************************************************************
592 * Read IPv6 error statistics.
595 * @a Activity structure.
598 * @a Activity structure with statistics.
599 ***************************************************************************
601 __read_funct_t wrap_read_net_eip6(struct activity *a)
603 struct stats_net_eip6 *st_net_eip6
604 = (struct stats_net_eip6 *) a->_buf0;
606 /* Read IPv6 error stats */
607 read_net_eip6(st_net_eip6);
613 ***************************************************************************
614 * Read ICMPv6 statistics.
617 * @a Activity structure.
620 * @a Activity structure with statistics.
621 ***************************************************************************
623 __read_funct_t wrap_read_net_icmp6(struct activity *a)
625 struct stats_net_icmp6 *st_net_icmp6
626 = (struct stats_net_icmp6 *) a->_buf0;
628 /* Read ICMPv6 stats */
629 read_net_icmp6(st_net_icmp6);
635 ***************************************************************************
636 * Read ICMPv6 error statistics.
639 * @a Activity structure.
642 * @a Activity structure with statistics.
643 ***************************************************************************
645 __read_funct_t wrap_read_net_eicmp6(struct activity *a)
647 struct stats_net_eicmp6 *st_net_eicmp6
648 = (struct stats_net_eicmp6 *) a->_buf0;
650 /* Read ICMPv6 error stats */
651 read_net_eicmp6(st_net_eicmp6);
657 ***************************************************************************
658 * Read UDPv6 statistics.
661 * @a Activity structure.
664 * @a Activity structure with statistics.
665 ***************************************************************************
667 __read_funct_t wrap_read_net_udp6(struct activity *a)
669 struct stats_net_udp6 *st_net_udp6
670 = (struct stats_net_udp6 *) a->_buf0;
672 /* Read UDPv6 stats */
673 read_net_udp6(st_net_udp6);
679 ***************************************************************************
680 * Read CPU frequency statistics.
683 * @a Activity structure.
686 * @a Activity structure with statistics.
687 ***************************************************************************
689 __read_funct_t wrap_read_cpuinfo(struct activity *a)
691 struct stats_pwr_cpufreq *st_pwr_cpufreq
692 = (struct stats_pwr_cpufreq *) a->_buf0;
694 /* Read CPU frequency stats */
695 read_cpuinfo(st_pwr_cpufreq, a->nr);
701 ***************************************************************************
702 * Read fan statistics.
705 * @a Activity structure.
708 * @a Activity structure with statistics.
709 ***************************************************************************
711 __read_funct_t wrap_read_fan(struct activity *a)
713 struct stats_pwr_fan *st_pwr_fan
714 = (struct stats_pwr_fan *) a->_buf0;
717 read_fan(st_pwr_fan, a->nr);
723 ***************************************************************************
724 * Read temperature statistics.
727 * @a Activity structure.
730 * @a Activity structure with statistics.
731 ***************************************************************************
733 __read_funct_t wrap_read_temp(struct activity *a)
735 struct stats_pwr_temp *st_pwr_temp
736 = (struct stats_pwr_temp *) a->_buf0;
738 /* Read temperature stats */
739 read_temp(st_pwr_temp, a->nr);
745 ***************************************************************************
746 * Read voltage input statistics.
749 * @a Activity structure.
752 * @a Activity structure with statistics.
753 ***************************************************************************
755 __read_funct_t wrap_read_in(struct activity *a)
757 struct stats_pwr_in *st_pwr_in
758 = (struct stats_pwr_in *) a->_buf0;
760 /* Read voltage input stats */
761 read_in(st_pwr_in, a->nr);
767 ***************************************************************************
768 * Read hugepages statistics.
771 * @a Activity structure.
774 * @a Activity structure with statistics.
775 ***************************************************************************
777 __read_funct_t wrap_read_meminfo_huge(struct activity *a)
779 struct stats_huge *st_huge
780 = (struct stats_huge *) a->_buf0;
782 /* Read hugepages stats */
783 read_meminfo_huge(st_huge);
789 ***************************************************************************
790 * Read weighted CPU frequency statistics.
793 * @a Activity structure.
796 * @a Activity structure with statistics.
797 ***************************************************************************
799 __read_funct_t wrap_read_time_in_state(struct activity *a)
803 struct stats_pwr_wghfreq *st_pwr_wghfreq
804 = (struct stats_pwr_wghfreq *) a->_buf0;
805 struct stats_pwr_wghfreq *st_pwr_wghfreq_i, *st_pwr_wghfreq_j, *st_pwr_wghfreq_all_j;
807 while (cpu < (a->nr - 1)) {
808 /* Read current CPU time-in-state data */
809 st_pwr_wghfreq_i = st_pwr_wghfreq + (cpu + 1) * a->nr2;
810 read_time_in_state(st_pwr_wghfreq_i, cpu, a->nr2);
812 /* Also save data for CPU 'all' */
813 for (j = 0; j < a->nr2; j++) {
814 st_pwr_wghfreq_j = st_pwr_wghfreq_i + j; /* CPU #cpu, state #j */
815 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
817 /* Assume that possible frequencies are the same for all CPUs */
818 st_pwr_wghfreq_all_j->freq = st_pwr_wghfreq_j->freq;
820 st_pwr_wghfreq_all_j->time_in_state += st_pwr_wghfreq_j->time_in_state;
825 /* Special processing for non SMP kernels: Only CPU 'all' is available */
827 read_time_in_state(st_pwr_wghfreq, 0, a->nr2);
830 for (j = 0; j < a->nr2; j++) {
831 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
832 st_pwr_wghfreq_all_j->time_in_state /= (a->nr - 1);
840 ***************************************************************************
841 * Read USB devices statistics.
844 * @a Activity structure.
847 * @a Activity structure with statistics.
848 ***************************************************************************
850 __read_funct_t wrap_read_bus_usb_dev(struct activity *a)
852 struct stats_pwr_usb *st_pwr_usb
853 = (struct stats_pwr_usb *) a->_buf0;
855 /* Read USB devices stats */
856 read_bus_usb_dev(st_pwr_usb, a->nr);
862 ***************************************************************************
863 * Read filesystem statistics.
866 * @a Activity structure.
869 * @a Activity structure with statistics.
870 ***************************************************************************
872 __read_funct_t wrap_read_filesystem(struct activity *a)
874 struct stats_filesystem *st_filesystem
875 = (struct stats_filesystem *) a->_buf0;
877 /* Read filesystems from /etc/mtab */
878 read_filesystem(st_filesystem, a->nr);
884 ***************************************************************************
885 * Read Fibre Channel HBA statistics.
888 * @a Activity structure.
891 * @a Activity structure with statistics.
892 ***************************************************************************
894 __read_funct_t wrap_read_fchost(struct activity *a)
896 struct stats_fchost *st_fc
897 = (struct stats_fchost *) a->_buf0;
899 read_fchost(st_fc, a->nr);
906 ***************************************************************************
907 * Count number of interrupts that are in /proc/stat file.
908 * Truncate the number of different individual interrupts to NR_IRQS.
911 * @a Activity structure.
914 * Number of interrupts, including total number of interrupts.
915 * Value in [0, NR_IRQS + 1].
916 ***************************************************************************
918 __nr_t wrap_get_irq_nr(struct activity *a)
922 if ((n = get_irq_nr()) > (a->bitmap->b_size + 1)) {
923 n = a->bitmap->b_size + 1;
930 ***************************************************************************
931 * Find number of serial lines that support tx/rx accounting
932 * in /proc/tty/driver/serial file.
935 * @a Activity structure.
938 * Number of serial lines supporting tx/rx accouting + a pre-allocation
939 * constant. Number cannot exceed MAX_NR_SERIAL_LINES.
940 ***************************************************************************
942 __nr_t wrap_get_serial_nr(struct activity *a)
946 if ((n = get_serial_nr()) > 0) {
947 if ((n + NR_SERIAL_PREALLOC) > MAX_NR_SERIAL_LINES)
948 return MAX_NR_SERIAL_LINES;
950 return n + NR_SERIAL_PREALLOC;
957 ***************************************************************************
958 * Find number of interfaces (network devices) that are in /proc/net/dev
962 * @a Activity structure.
965 * Number of network interfaces + a pre-allocation constant. Number cannot
966 * exceed MAX_NR_IFACES.
967 ***************************************************************************
969 __nr_t wrap_get_iface_nr(struct activity *a)
973 if ((n = get_iface_nr()) > 0) {
974 if ((n + NR_IFACE_PREALLOC) > MAX_NR_IFACES)
975 return MAX_NR_IFACES;
977 return n + NR_IFACE_PREALLOC;
984 ***************************************************************************
985 * Compute number of CPU structures to allocate.
988 * @a Activity structure.
991 * Number of structures (value in [1, NR_CPUS + 1]).
992 * 1 means that there is only one proc and non SMP kernel.
993 * 2 means one proc and SMP kernel.
995 ***************************************************************************
997 __nr_t wrap_get_cpu_nr(struct activity *a)
999 return (get_cpu_nr(a->bitmap->b_size, FALSE) + 1);
1003 ***************************************************************************
1004 * Get number of devices in /proc/diskstats.
1005 * Always done, since disk stats must be read at least for sar -b
1006 * if not for sar -d.
1009 * @a Activity structure.
1012 * Number of devices + a pre-allocation constant. Number cannot exceed
1014 ***************************************************************************
1016 __nr_t wrap_get_disk_nr(struct activity *a)
1019 unsigned int f = COLLECT_PARTITIONS(a->opt_flags);
1021 if ((n = get_disk_nr(f)) > 0) {
1022 if ((n + NR_DISK_PREALLOC) > MAX_NR_DISKS)
1023 return MAX_NR_DISKS;
1025 return n + NR_DISK_PREALLOC;
1032 ***************************************************************************
1033 * Get number of fan sensors.
1036 * @a Activity structure.
1039 * Number of fan sensors. Number cannot exceed MAX_NR_FANS.
1040 ***************************************************************************
1042 __nr_t wrap_get_fan_nr(struct activity *a)
1046 if ((n = get_fan_nr()) > MAX_NR_FANS)
1053 ***************************************************************************
1054 * Get number of temp sensors.
1057 * @a Activity structure.
1060 * Number of temp sensors. Number cannot exceed MAX_NR_TEMP_SENSORS.
1061 ***************************************************************************
1063 __nr_t wrap_get_temp_nr(struct activity *a)
1067 if ((n = get_temp_nr()) > MAX_NR_TEMP_SENSORS)
1068 return MAX_NR_TEMP_SENSORS;
1074 ***************************************************************************
1075 * Get number of voltage input sensors.
1078 * @a Activity structure.
1081 * Number of voltage input sensors. Number cannot exceed MAX_NR_IN_SENSORS.
1082 ***************************************************************************
1084 __nr_t wrap_get_in_nr(struct activity *a)
1088 if ((n = get_in_nr()) > MAX_NR_IN_SENSORS)
1089 return MAX_NR_IN_SENSORS;
1095 ***************************************************************************
1096 * Count number of possible frequencies for CPU#0.
1099 * @a Activity structure.
1102 * Number of CPU frequencies + a pre-allocation constant.
1103 ***************************************************************************
1105 __nr_t wrap_get_freq_nr(struct activity *a)
1109 if ((n = get_freq_nr()) > 0)
1110 return n + NR_FREQ_PREALLOC;
1116 ***************************************************************************
1117 * Count number of USB devices plugged into the system.
1120 * @a Activity structure.
1123 * Number of USB devices + a pre-allocation constant. Number cannot exceed
1125 ***************************************************************************
1127 __nr_t wrap_get_usb_nr(struct activity *a)
1131 if ((n = get_usb_nr()) >= 0) {
1132 /* Return a positive number even if no USB devices have been found */
1133 if ((n + NR_USB_PREALLOC) > MAX_NR_USB)
1136 return n + NR_USB_PREALLOC;
1143 ***************************************************************************
1144 * Get number of mounted filesystems from /etc/mtab. Don't take into account
1145 * pseudo-filesystems.
1148 * @a Activity structure.
1151 * Number of filesystems + a pre-allocation constant. Number cannot exceed
1153 ***************************************************************************
1155 __nr_t wrap_get_filesystem_nr(struct activity *a)
1159 if ((n = get_filesystem_nr()) > 0) {
1160 if ((n + NR_FILESYSTEM_PREALLOC) > MAX_NR_FS)
1163 return n + NR_FILESYSTEM_PREALLOC;
1170 ***************************************************************************
1171 * Get number of FC hosts.
1174 * @a Activity structure.
1177 * Number of FC hosts + a pre-allocation constant. Number cannot exceed
1179 ***************************************************************************
1181 __nr_t wrap_get_fchost_nr(struct activity *a)
1185 if ((n = get_fchost_nr()) >= 0) {
1186 /* Return a positive number even if no FC hosts have been found */
1187 if ((n + NR_FCHOST_PREALLOC) > MAX_NR_FCHOSTS)
1188 return MAX_NR_FCHOSTS;
1190 return n + NR_FCHOST_PREALLOC;