2 * count.c: Count items for which statistics will be collected.
3 * (C) 1999-2014 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 ***************************************************************************
28 #include <sys/types.h>
39 #define _(string) gettext(string)
41 #define _(string) (string)
46 ***************************************************************************
47 * Count number of processors in /sys.
50 * @highest If set to TRUE, then look for the highest processor number.
51 * This is used when eg. the machine has 4 CPU numbered 0, 1, 4
52 * and 5. In this case, this procedure will return 6.
55 * Number of processors (online and offline).
56 * A value of 0 means that /sys was not mounted.
57 * A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)).
58 ***************************************************************************
60 int get_sys_cpu_nr(int highest)
65 char line[MAX_PF_NAME];
66 int num_proc, proc_nr = -1;
68 /* Open relevant /sys directory */
69 if ((dir = opendir(SYSFS_DEVCPU)) == NULL)
72 /* Get current file entry */
73 while ((drd = readdir(dir)) != NULL) {
75 if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
76 snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name);
77 line[MAX_PF_NAME - 1] = '\0';
78 if (stat(line, &buf) < 0)
80 if (S_ISDIR(buf.st_mode)) {
82 sscanf(drd->d_name + 3, "%d", &num_proc);
83 if (num_proc > proc_nr) {
101 ***************************************************************************
102 * Count number of processors in /proc/stat.
105 * Number of processors. The returned value is greater than or equal to the
106 * number of online processors.
107 * A value of 0 means one processor and non SMP kernel.
108 * A value of N (!=0) means N processor(s) (0 .. N-1) with SMP kernel.
109 ***************************************************************************
111 int get_proc_cpu_nr(void)
115 int num_proc, proc_nr = -1;
117 if ((fp = fopen(STAT, "r")) == NULL) {
118 fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
122 while (fgets(line, sizeof(line), fp) != NULL) {
124 if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) {
125 sscanf(line + 3, "%d", &num_proc);
126 if (num_proc > proc_nr) {
134 return (proc_nr + 1);
138 ***************************************************************************
139 * Count the number of processors on the machine, or look for the
140 * highest processor number.
141 * Try to use /sys for that, or /proc/stat if /sys doesn't exist.
144 * @max_nr_cpus Maximum number of proc that sysstat can handle.
145 * @highest If set to TRUE, then look for the highest processor number.
146 * This is used when eg. the machine has 4 CPU numbered 0, 1, 4
147 * and 5. In this case, this procedure will return 6.
150 * Number of processors.
151 * 0: one proc and non SMP kernel.
152 * 1: one proc and SMP kernel (NB: On SMP machines where all the CPUs but
153 * one have been disabled, we get the total number of proc since we use
154 * /sys to count them).
156 ***************************************************************************
158 int get_cpu_nr(unsigned int max_nr_cpus, int highest)
162 if ((cpu_nr = get_sys_cpu_nr(highest)) == 0) {
163 /* /sys may be not mounted. Use /proc/stat instead */
164 cpu_nr = get_proc_cpu_nr();
167 if (cpu_nr > max_nr_cpus) {
168 fprintf(stderr, _("Cannot handle so many processors!\n"));
176 ***************************************************************************
177 * Find number of interrupts available per processor (use
178 * /proc/interrupts file or /proc/softirqs).
181 * @file /proc file to read (interrupts or softirqs).
182 * @max_nr_irqcpu Maximum number of interrupts per processor that
184 * @cpu_nr Number of processors.
187 * Number of interrupts per processor + a pre-allocation constant.
188 ***************************************************************************
190 int get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
194 unsigned int irq = 0;
197 if ((fp = fopen(file, "r")) == NULL)
198 return 0; /* No interrupts file */
200 SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
202 while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr , fp) != NULL) &&
203 (irq < max_nr_irqcpu)) {
204 p = strcspn(line, ":");
205 if ((p > 0) && (p < 16)) {
218 ***************************************************************************
219 * Find number of devices and partitions available in /proc/diskstats.
222 * @count_part Set to TRUE if devices _and_ partitions are to be
224 * @only_used_dev When counting devices, set to TRUE if only devices
225 * with non zero stats must be counted.
228 * Number of devices (and partitions).
229 ***************************************************************************
231 int get_diskstats_dev_nr(int count_part, int only_used_dev)
235 char dev_name[MAX_NAME_LEN];
237 unsigned long rd_ios, wr_ios;
239 if ((fp = fopen(DISKSTATS, "r")) == NULL)
240 /* File non-existent */
244 * Counting devices and partitions is simply a matter of counting
245 * the number of lines...
247 while (fgets(line, sizeof(line), fp) != NULL) {
249 i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu",
250 dev_name, &rd_ios, &wr_ios);
251 if ((i == 2) || !is_device(dev_name, ACCEPT_VIRTUAL_DEVICES))
252 /* It was a partition and not a device */
254 if (only_used_dev && !rd_ios && !wr_ios)
267 /*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
270 ***************************************************************************
271 * Count number of interrupts that are in /proc/stat file.
274 * Number of interrupts, including total number of interrupts.
275 ***************************************************************************
284 if ((fp = fopen(STAT, "r")) == NULL)
287 while (fgets(line, sizeof(line), fp) != NULL) {
289 if (!strncmp(line, "intr ", 5)) {
291 while (pos < strlen(line)) {
293 pos += strcspn(line + pos + 1, " ") + 1;
304 ***************************************************************************
305 * Find number of serial lines that support tx/rx accounting
306 * in /proc/tty/driver/serial file.
309 * Number of serial lines supporting tx/rx accouting.
310 ***************************************************************************
312 int get_serial_nr(void)
318 if ((fp = fopen(SERIAL, "r")) == NULL)
319 return 0; /* No SERIAL file */
321 while (fgets(line, sizeof(line), fp) != NULL) {
323 * tx/rx statistics are always present,
324 * except when serial line is unknown.
326 if (strstr(line, "tx:") != NULL) {
337 ***************************************************************************
338 * Find number of interfaces (network devices) that are in /proc/net/dev
342 * Number of network interfaces.
343 ***************************************************************************
345 int get_iface_nr(void)
351 if ((fp = fopen(NET_DEV, "r")) == NULL)
352 return 0; /* No network device file */
354 while (fgets(line, sizeof(line), fp) != NULL) {
355 if (strchr(line, ':')) {
366 ***************************************************************************
367 * Get number of devices in /proc/diskstats.
370 * @f Non zero (true) if disks *and* partitions should be counted, and
371 * zero (false) if only disks must be counted.
375 ***************************************************************************
377 int get_disk_nr(unsigned int f)
382 * Partitions are taken into account by sar -d only with
383 * kernels 2.6.25 and later.
385 disk_nr = get_diskstats_dev_nr(f, CNT_USED_DEV);
391 ***************************************************************************
392 * Count number of possible frequencies for CPU#0.
395 * Number of frequencies.
396 ***************************************************************************
398 int get_freq_nr(void)
401 char filename[MAX_PF_NAME];
405 snprintf(filename, MAX_PF_NAME, "%s/cpu0/%s",
406 SYSFS_DEVCPU, SYSFS_TIME_IN_STATE);
407 if ((fp = fopen(filename, "r")) == NULL)
408 return 0; /* No time_in_state file for CPU#0 */
410 while (fgets(line, sizeof(line), fp) != NULL) {
420 ***************************************************************************
421 * Count number of USB devices in /sys/bus/usb/devices.
424 * Number of USB devices plugged into the system.
425 * Don't count USB root hubs.
426 * Return -1 if directory doesn't exist in sysfs.
427 ***************************************************************************
435 /* Open relevant /sys directory */
436 if ((dir = opendir(SYSFS_USBDEV)) == NULL)
439 /* Get current file entry */
440 while ((drd = readdir(dir)) != NULL) {
442 if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
447 /* Close directory */
454 ***************************************************************************
455 * Find number of filesystems in /etc/mtab. Pseudo-filesystems are ignored.
458 * Number of filesystems.
459 ***************************************************************************
461 int get_filesystem_nr(void)
464 char line[256], fs_name[MAX_FS_LEN], mountp[128];
468 if ((fp = fopen(MTAB, "r")) == NULL)
469 /* File non-existent */
472 /* Get current filesystem */
473 while (fgets(line, sizeof(line), fp) != NULL) {
474 if (line[0] == '/') {
476 /* Read filesystem name and mount point */
477 sscanf(line, "%71s %127s", fs_name, mountp);
479 /* Replace octal codes */
482 /* Check that total size is not null */
483 if (statfs(mountp, &buf) < 0)
497 /*------------------ END: FUNCTIONS USED BY SADC ONLY ---------------------*/
498 #endif /* SOURCE_SADC */