2 * sysstat - sa_wrap.c: Functions used in activity.c
3 * (C) 1999-2011 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 ***************************************************************************
24 #include "rd_sensors.h"
26 extern unsigned int flags;
27 extern struct record_header record_hdr;
30 ***************************************************************************
31 * Read CPU statistics.
34 * @a Activity structure.
37 * @a Activity structure with statistics.
38 ***************************************************************************
40 __read_funct_t wrap_read_stat_cpu(struct activity *a)
42 struct stats_cpu *st_cpu
43 = (struct stats_cpu *) a->_buf0;
45 /* Read CPU statistics */
46 read_stat_cpu(st_cpu, a->nr, &record_hdr.uptime, &record_hdr.uptime0);
52 ***************************************************************************
53 * Read process (task) creation and context switch statistics.
56 * @a Activity structure.
59 * @a Activity structure with statistics.
60 ***************************************************************************
62 __read_funct_t wrap_read_stat_pcsw(struct activity *a)
64 struct stats_pcsw *st_pcsw
65 = (struct stats_pcsw *) a->_buf0;
67 /* Read process and context switch stats */
68 read_stat_pcsw(st_pcsw);
74 ***************************************************************************
75 * Read interrupt statistics.
78 * @a Activity structure.
81 * @a Activity structure with statistics.
82 ***************************************************************************
84 __read_funct_t wrap_read_stat_irq(struct activity *a)
86 struct stats_irq *st_irq
87 = (struct stats_irq *) a->_buf0;
89 /* Read interrupts stats */
90 read_stat_irq(st_irq, a->nr);
96 ***************************************************************************
97 * Read queue and load statistics.
100 * @a Activity structure.
103 * @a Activity structure with statistics.
104 ***************************************************************************
106 __read_funct_t wrap_read_loadavg(struct activity *a)
108 struct stats_queue *st_queue
109 = (struct stats_queue *) a->_buf0;
111 /* Read queue and load stats */
112 read_loadavg(st_queue);
118 ***************************************************************************
119 * Read memory statistics.
122 * @a Activity structure.
125 * @a Activity structure with statistics.
126 ***************************************************************************
128 __read_funct_t wrap_read_meminfo(struct activity *a)
130 struct stats_memory *st_memory
131 = (struct stats_memory *) a->_buf0;
133 /* Read memory stats */
134 read_meminfo(st_memory);
140 ***************************************************************************
141 * Read swapping statistics.
144 * @a Activity structure.
147 * @a Activity structure with statistics.
148 ***************************************************************************
150 __read_funct_t wrap_read_swap(struct activity *a)
152 struct stats_swap *st_swap
153 = (struct stats_swap *) a->_buf0;
155 /* Read stats from /proc/vmstat */
156 read_vmstat_swap(st_swap);
162 ***************************************************************************
163 * Read paging statistics.
166 * @a Activity structure.
169 * @a Activity structure with statistics.
170 ***************************************************************************
172 __read_funct_t wrap_read_paging(struct activity *a)
174 struct stats_paging *st_paging
175 = (struct stats_paging *) a->_buf0;
177 /* Read stats from /proc/vmstat */
178 read_vmstat_paging(st_paging);
184 ***************************************************************************
185 * Read I/O and transfer rates statistics.
188 * @a Activity structure.
191 * @a Activity structure with statistics.
192 ***************************************************************************
194 __read_funct_t wrap_read_io(struct activity *a)
196 struct stats_io *st_io
197 = (struct stats_io *) a->_buf0;
199 /* Read stats from /proc/diskstats */
200 read_diskstats_io(st_io);
206 ***************************************************************************
207 * Read block devices statistics.
210 * @a Activity structure.
213 * @a Activity structure with statistics.
214 ***************************************************************************
216 __read_funct_t wrap_read_disk(struct activity *a)
218 struct stats_disk *st_disk
219 = (struct stats_disk *) a->_buf0;
221 /* Read stats from /proc/diskstats */
222 read_diskstats_disk(st_disk, a->nr, COLLECT_PARTITIONS(a->opt_flags));
228 ***************************************************************************
229 * Read serial lines statistics.
232 * @a Activity structure.
235 * @a Activity structure with statistics.
236 ***************************************************************************
238 __read_funct_t wrap_read_tty_driver_serial(struct activity *a)
240 struct stats_serial *st_serial
241 = (struct stats_serial *) a->_buf0;
243 /* Read serial lines stats */
244 read_tty_driver_serial(st_serial, a->nr);
250 ***************************************************************************
251 * Read kernel tables statistics.
254 * @a Activity structure.
257 * @a Activity structure with statistics.
258 ***************************************************************************
260 __read_funct_t wrap_read_kernel_tables(struct activity *a)
262 struct stats_ktables *st_ktables
263 = (struct stats_ktables *) a->_buf0;
265 /* Read kernel tables stats */
266 read_kernel_tables(st_ktables);
272 ***************************************************************************
273 * Read network interfaces statistics.
276 * @a Activity structure.
279 * @a Activity structure with statistics.
280 ***************************************************************************
282 __read_funct_t wrap_read_net_dev(struct activity *a)
284 struct stats_net_dev *st_net_dev
285 = (struct stats_net_dev *) a->_buf0;
287 /* Read network interfaces stats */
288 read_net_dev(st_net_dev, a->nr);
294 ***************************************************************************
295 * Read network interfaces errors statistics.
298 * @a Activity structure.
301 * @a Activity structure with statistics.
302 ***************************************************************************
304 __read_funct_t wrap_read_net_edev(struct activity *a)
306 struct stats_net_edev *st_net_edev
307 = (struct stats_net_edev *) a->_buf0;
309 /* Read network interfaces errors stats */
310 read_net_edev(st_net_edev, a->nr);
316 ***************************************************************************
317 * Read NFS client statistics.
320 * @a Activity structure.
323 * @a Activity structure with statistics.
324 ***************************************************************************
326 __read_funct_t wrap_read_net_nfs(struct activity *a)
328 struct stats_net_nfs *st_net_nfs
329 = (struct stats_net_nfs *) a->_buf0;
331 /* Read NFS client stats */
332 read_net_nfs(st_net_nfs);
338 ***************************************************************************
339 * Read NFS server statistics.
342 * @a Activity structure.
345 * @a Activity structure with statistics.
346 ***************************************************************************
348 __read_funct_t wrap_read_net_nfsd(struct activity *a)
350 struct stats_net_nfsd *st_net_nfsd
351 = (struct stats_net_nfsd *) a->_buf0;
353 /* Read NFS server stats */
354 read_net_nfsd(st_net_nfsd);
360 ***************************************************************************
361 * Read network sockets statistics.
364 * @a Activity structure.
367 * @a Activity structure with statistics.
368 ***************************************************************************
370 __read_funct_t wrap_read_net_sock(struct activity *a)
372 struct stats_net_sock *st_net_sock
373 = (struct stats_net_sock *) a->_buf0;
375 /* Read network sockets stats */
376 read_net_sock(st_net_sock);
382 ***************************************************************************
383 * Read IP statistics.
386 * @a Activity structure.
389 * @a Activity structure with statistics.
390 ***************************************************************************
392 __read_funct_t wrap_read_net_ip(struct activity *a)
394 struct stats_net_ip *st_net_ip
395 = (struct stats_net_ip *) a->_buf0;
398 read_net_ip(st_net_ip);
404 ***************************************************************************
405 * Read IP error statistics.
408 * @a Activity structure.
411 * @a Activity structure with statistics.
412 ***************************************************************************
414 __read_funct_t wrap_read_net_eip(struct activity *a)
416 struct stats_net_eip *st_net_eip
417 = (struct stats_net_eip *) a->_buf0;
419 /* Read IP error stats */
420 read_net_eip(st_net_eip);
426 ***************************************************************************
427 * Read ICMP statistics.
430 * @a Activity structure.
433 * @a Activity structure with statistics.
434 ***************************************************************************
436 __read_funct_t wrap_read_net_icmp(struct activity *a)
438 struct stats_net_icmp *st_net_icmp
439 = (struct stats_net_icmp *) a->_buf0;
441 /* Read ICMP stats */
442 read_net_icmp(st_net_icmp);
448 ***************************************************************************
449 * Read ICMP error statistics.
452 * @a Activity structure.
455 * @a Activity structure with statistics.
456 ***************************************************************************
458 __read_funct_t wrap_read_net_eicmp(struct activity *a)
460 struct stats_net_eicmp *st_net_eicmp
461 = (struct stats_net_eicmp *) a->_buf0;
463 /* Read ICMP error stats */
464 read_net_eicmp(st_net_eicmp);
470 ***************************************************************************
471 * Read TCP statistics.
474 * @a Activity structure.
477 * @a Activity structure with statistics.
478 ***************************************************************************
480 __read_funct_t wrap_read_net_tcp(struct activity *a)
482 struct stats_net_tcp *st_net_tcp
483 = (struct stats_net_tcp *) a->_buf0;
486 read_net_tcp(st_net_tcp);
492 ***************************************************************************
493 * Read TCP error statistics.
496 * @a Activity structure.
499 * @a Activity structure with statistics.
500 ***************************************************************************
502 __read_funct_t wrap_read_net_etcp(struct activity *a)
504 struct stats_net_etcp *st_net_etcp
505 = (struct stats_net_etcp *) a->_buf0;
507 /* Read TCP error stats */
508 read_net_etcp(st_net_etcp);
514 ***************************************************************************
515 * Read UDP statistics.
518 * @a Activity structure.
521 * @a Activity structure with statistics.
522 ***************************************************************************
524 __read_funct_t wrap_read_net_udp(struct activity *a)
526 struct stats_net_udp *st_net_udp
527 = (struct stats_net_udp *) a->_buf0;
530 read_net_udp(st_net_udp);
536 ***************************************************************************
537 * Read IPv6 network sockets statistics.
540 * @a Activity structure.
543 * @a Activity structure with statistics.
544 ***************************************************************************
546 __read_funct_t wrap_read_net_sock6(struct activity *a)
548 struct stats_net_sock6 *st_net_sock6
549 = (struct stats_net_sock6 *) a->_buf0;
551 /* Read IPv6 network sockets stats */
552 read_net_sock6(st_net_sock6);
558 ***************************************************************************
559 * Read IPv6 statistics.
562 * @a Activity structure.
565 * @a Activity structure with statistics.
566 ***************************************************************************
568 __read_funct_t wrap_read_net_ip6(struct activity *a)
570 struct stats_net_ip6 *st_net_ip6
571 = (struct stats_net_ip6 *) a->_buf0;
573 /* Read IPv6 stats */
574 read_net_ip6(st_net_ip6);
580 ***************************************************************************
581 * Read IPv6 error statistics.
584 * @a Activity structure.
587 * @a Activity structure with statistics.
588 ***************************************************************************
590 __read_funct_t wrap_read_net_eip6(struct activity *a)
592 struct stats_net_eip6 *st_net_eip6
593 = (struct stats_net_eip6 *) a->_buf0;
595 /* Read IPv6 error stats */
596 read_net_eip6(st_net_eip6);
602 ***************************************************************************
603 * Read ICMPv6 statistics.
606 * @a Activity structure.
609 * @a Activity structure with statistics.
610 ***************************************************************************
612 __read_funct_t wrap_read_net_icmp6(struct activity *a)
614 struct stats_net_icmp6 *st_net_icmp6
615 = (struct stats_net_icmp6 *) a->_buf0;
617 /* Read ICMPv6 stats */
618 read_net_icmp6(st_net_icmp6);
624 ***************************************************************************
625 * Read ICMPv6 error statistics.
628 * @a Activity structure.
631 * @a Activity structure with statistics.
632 ***************************************************************************
634 __read_funct_t wrap_read_net_eicmp6(struct activity *a)
636 struct stats_net_eicmp6 *st_net_eicmp6
637 = (struct stats_net_eicmp6 *) a->_buf0;
639 /* Read ICMPv6 error stats */
640 read_net_eicmp6(st_net_eicmp6);
646 ***************************************************************************
647 * Read UDPv6 statistics.
650 * @a Activity structure.
653 * @a Activity structure with statistics.
654 ***************************************************************************
656 __read_funct_t wrap_read_net_udp6(struct activity *a)
658 struct stats_net_udp6 *st_net_udp6
659 = (struct stats_net_udp6 *) a->_buf0;
661 /* Read UDPv6 stats */
662 read_net_udp6(st_net_udp6);
668 ***************************************************************************
669 * Read CPU frequency statistics.
672 * @a Activity structure.
675 * @a Activity structure with statistics.
676 ***************************************************************************
678 __read_funct_t wrap_read_cpuinfo(struct activity *a)
680 struct stats_pwr_cpufreq *st_pwr_cpufreq
681 = (struct stats_pwr_cpufreq *) a->_buf0;
683 /* Read CPU frequency stats */
684 read_cpuinfo(st_pwr_cpufreq, a->nr);
690 ***************************************************************************
691 * Read fan statistics.
694 * @a Activity structure.
697 * @a Activity structure with statistics.
698 ***************************************************************************
700 __read_funct_t wrap_read_fan(struct activity *a)
702 struct stats_pwr_fan *st_pwr_fan
703 = (struct stats_pwr_fan *) a->_buf0;
706 read_fan(st_pwr_fan, a->nr);
712 ***************************************************************************
713 * Read temperature statistics.
716 * @a Activity structure.
719 * @a Activity structure with statistics.
720 ***************************************************************************
722 __read_funct_t wrap_read_temp(struct activity *a)
724 struct stats_pwr_temp *st_pwr_temp
725 = (struct stats_pwr_temp *) a->_buf0;
727 /* Read temperature stats */
728 read_temp(st_pwr_temp, a->nr);
734 ***************************************************************************
735 * Read voltage input statistics.
738 * @a Activity structure.
741 * @a Activity structure with statistics.
742 ***************************************************************************
744 __read_funct_t wrap_read_in(struct activity *a)
746 struct stats_pwr_in *st_pwr_in
747 = (struct stats_pwr_in *) a->_buf0;
749 /* Read voltage input stats */
750 read_in(st_pwr_in, a->nr);
756 ***************************************************************************
757 * Read hugepages statistics.
760 * @a Activity structure.
763 * @a Activity structure with statistics.
764 ***************************************************************************
766 __read_funct_t wrap_read_meminfo_huge(struct activity *a)
768 struct stats_huge *st_huge
769 = (struct stats_huge *) a->_buf0;
771 /* Read hugepages stats */
772 read_meminfo_huge(st_huge);
778 ***************************************************************************
779 * Read weighted CPU frequency statistics.
782 * @a Activity structure.
785 * @a Activity structure with statistics.
786 ***************************************************************************
788 __read_funct_t wrap_read_time_in_state(struct activity *a)
792 struct stats_pwr_wghfreq *st_pwr_wghfreq
793 = (struct stats_pwr_wghfreq *) a->_buf0;
794 struct stats_pwr_wghfreq *st_pwr_wghfreq_i, *st_pwr_wghfreq_j, *st_pwr_wghfreq_all_j;
796 while (cpu < (a->nr - 1)) {
797 /* Read current CPU time-in-state data */
798 st_pwr_wghfreq_i = st_pwr_wghfreq + (cpu + 1) * a->nr2;
799 read_time_in_state(st_pwr_wghfreq_i, cpu, a->nr2);
801 /* Also save data for CPU 'all' */
802 for (j = 0; j < a->nr2; j++) {
803 st_pwr_wghfreq_j = st_pwr_wghfreq_i + j; /* CPU #cpu, state #j */
804 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
806 /* Assume that possible frequencies are the same for all CPUs */
807 st_pwr_wghfreq_all_j->freq = st_pwr_wghfreq_j->freq;
809 st_pwr_wghfreq_all_j->time_in_state += st_pwr_wghfreq_j->time_in_state;
814 /* Special processing for non SMP kernels: Only CPU 'all' is available */
816 read_time_in_state(st_pwr_wghfreq, 0, a->nr2);
819 for (j = 0; j < a->nr2; j++) {
820 st_pwr_wghfreq_all_j = st_pwr_wghfreq + j; /* CPU #all, state #j */
821 st_pwr_wghfreq_all_j->time_in_state /= (a->nr - 1);
829 ***************************************************************************
830 * Read USB devices statistics.
833 * @a Activity structure.
836 * @a Activity structure with statistics.
837 ***************************************************************************
839 __read_funct_t wrap_read_bus_usb_dev(struct activity *a)
841 struct stats_pwr_usb *st_pwr_usb
842 = (struct stats_pwr_usb *) a->_buf0;
844 /* Read USB devices stats */
845 read_bus_usb_dev(st_pwr_usb, a->nr);
851 ***************************************************************************
852 * Read filesystem statistics.
855 * @a Activity structure.
858 * @a Activity structure with statistics.
859 ***************************************************************************
861 __read_funct_t wrap_read_filesystem(struct activity *a)
863 struct stats_filesystem *st_filesystem
864 = (struct stats_filesystem *) a->_buf0;
866 /* Read filesystems from /etc/mtab */
867 read_filesystem(st_filesystem, a->nr);
873 ***************************************************************************
874 * Count number of interrupts that are in /proc/stat file.
875 * Truncate the number of different individual interrupts to NR_IRQS.
878 * @a Activity structure.
881 * Number of interrupts, including total number of interrupts.
882 * Value in [0, NR_IRQS + 1].
883 ***************************************************************************
885 __nr_t wrap_get_irq_nr(struct activity *a)
889 if ((n = get_irq_nr()) > (a->bitmap->b_size + 1)) {
890 n = a->bitmap->b_size + 1;
897 ***************************************************************************
898 * Find number of serial lines that support tx/rx accounting
899 * in /proc/tty/driver/serial file.
902 * @a Activity structure.
905 * Number of serial lines supporting tx/rx accouting + a pre-allocation
907 ***************************************************************************
909 __nr_t wrap_get_serial_nr(struct activity *a)
913 if ((n = get_serial_nr()) > 0)
914 return n + NR_SERIAL_PREALLOC;
920 ***************************************************************************
921 * Find number of interfaces (network devices) that are in /proc/net/dev
925 * @a Activity structure.
928 * Number of network interfaces + a pre-allocation constant.
929 ***************************************************************************
931 __nr_t wrap_get_iface_nr(struct activity *a)
935 if ((n = get_iface_nr()) > 0)
936 return n + NR_IFACE_PREALLOC;
942 ***************************************************************************
943 * Compute number of CPU structures to allocate.
946 * @a Activity structure.
949 * Number of structures (value in [1, NR_CPUS + 1]).
950 * 1 means that there is only one proc and non SMP kernel.
951 * 2 means one proc and SMP kernel.
953 ***************************************************************************
955 __nr_t wrap_get_cpu_nr(struct activity *a)
957 return (get_cpu_nr(a->bitmap->b_size) + 1);
961 ***************************************************************************
962 * Get number of devices in /proc/diskstats.
963 * Always done, since disk stats must be read at least for sar -b
967 * @a Activity structure.
970 * Number of devices + a pre-allocation constant.
971 ***************************************************************************
973 __nr_t wrap_get_disk_nr(struct activity *a)
976 unsigned int f = COLLECT_PARTITIONS(a->opt_flags);
978 if ((n = get_disk_nr(f)) > 0)
979 return n + NR_DISK_PREALLOC;
985 ***************************************************************************
986 * Get number of fan structures to allocate.
989 * @a Activity structure.
992 * Number of structures.
993 ***************************************************************************
995 __nr_t wrap_get_fan_nr(struct activity *a)
997 return (get_fan_nr());
1001 ***************************************************************************
1002 * Get number of temp structures to allocate.
1005 * @a Activity structure.
1008 * Number of structures.
1009 ***************************************************************************
1011 __nr_t wrap_get_temp_nr(struct activity *a)
1013 return (get_temp_nr());
1017 ***************************************************************************
1018 * Get number of voltage input structures to allocate.
1021 * @a Activity structure.
1024 * Number of structures.
1025 ***************************************************************************
1027 __nr_t wrap_get_in_nr(struct activity *a)
1029 return (get_in_nr());
1033 ***************************************************************************
1034 * Count number of possible frequencies for CPU#0.
1037 * @a Activity structure.
1040 * Number of CPU frequencies + a pre-allocation constant.
1041 ***************************************************************************
1043 __nr_t wrap_get_freq_nr(struct activity *a)
1047 if ((n = get_freq_nr()) > 0)
1048 return n + NR_FREQ_PREALLOC;
1054 ***************************************************************************
1055 * Count number of USB devices plugged into the system.
1058 * @a Activity structure.
1061 * Number of USB devices + a pre-allocation constant.
1062 ***************************************************************************
1064 __nr_t wrap_get_usb_nr(struct activity *a)
1068 if ((n = get_usb_nr()) >= 0)
1069 /* Return a positive number even if no USB devices have been found */
1070 return n + NR_USB_PREALLOC;
1076 ***************************************************************************
1077 * Get number of mounted filesystems from /etc/mtab. Don't take into account
1078 * pseudo-filesystems.
1081 * @a Activity structure.
1084 * Number of filesystems + a pre-allocation constant.
1085 ***************************************************************************
1087 __nr_t wrap_get_filesystem_nr(struct activity *a)
1091 if ((n = get_filesystem_nr()) > 0)
1092 return n + NR_FILESYSTEM_PREALLOC;