]> granicus.if.org Git - sysstat/blob - count.c
0adc25e14722868da4158262edf5caecb3acd657
[sysstat] / count.c
1 /*
2  * count.c: Count items for which statistics will be collected.
3  * (C) 1999-2021 by Sebastien GODARD (sysstat <at> orange.fr)
4  *
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.                                              *
10  *                                                                         *
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 *
14  * for more details.                                                       *
15  *                                                                         *
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  ***************************************************************************
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <dirent.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/statvfs.h>
31 #include <unistd.h>
32
33 #include "common.h"
34 #include "rd_stats.h"
35
36 #ifdef USE_NLS
37 #include <locale.h>
38 #include <libintl.h>
39 #define _(string) gettext(string)
40 #else
41 #define _(string) (string)
42 #endif
43
44
45 /*
46  ***************************************************************************
47  * Count number of processors in /sys.
48  *
49  * IN:
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.
53  *
54  * RETURNS:
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  ***************************************************************************
59  */
60 int get_sys_cpu_nr(int highest)
61 {
62         DIR *dir;
63         struct dirent *drd;
64         struct stat buf;
65         char line[MAX_PF_NAME];
66         int num_proc, proc_nr = -1;
67
68         /* Open relevant /sys directory */
69         if ((dir = opendir(SYSFS_DEVCPU)) == NULL)
70                 return 0;
71
72         /* Get current file entry */
73         while ((drd = readdir(dir)) != NULL) {
74
75                 if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
76                         snprintf(line, sizeof(line), "%s/%s", SYSFS_DEVCPU, drd->d_name);
77                         line[sizeof(line) - 1] = '\0';
78                         if (stat(line, &buf) < 0)
79                                 continue;
80                         if (S_ISDIR(buf.st_mode)) {
81                                 if (highest) {
82                                         sscanf(drd->d_name + 3, "%d", &num_proc);
83                                         if (num_proc > proc_nr) {
84                                                 proc_nr = num_proc;
85                                         }
86                                 }
87                                 else {
88                                         proc_nr++;
89                                 }
90                         }
91                 }
92         }
93
94         /* Close directory */
95         closedir(dir);
96
97         return (proc_nr + 1);
98 }
99
100 /*
101  ***************************************************************************
102  * Count number of processors in /proc/stat.
103  *
104  * RETURNS:
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  ***************************************************************************
110  */
111 int get_proc_cpu_nr(void)
112 {
113         FILE *fp;
114         char line[16];
115         int num_proc, proc_nr = -1;
116
117         if ((fp = fopen(STAT, "r")) == NULL) {
118                 fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
119                 exit(1);
120         }
121
122         while (fgets(line, sizeof(line), fp) != NULL) {
123
124                 if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) {
125                         sscanf(line + 3, "%d", &num_proc);
126                         if (num_proc > proc_nr) {
127                                 proc_nr = num_proc;
128                         }
129                 }
130         }
131
132         fclose(fp);
133
134         proc_nr++;
135         return proc_nr;
136 }
137
138 /*
139  ***************************************************************************
140  * Count the number of processors on the machine, or look for the
141  * highest processor number.
142  * Try to use /sys for that, or /proc/stat if /sys doesn't exist.
143  *
144  * IN:
145  * @max_nr_cpus Maximum number of proc that sysstat can handle.
146  * @highest     If set to TRUE, then look for the highest processor number.
147  *              This is used when eg. the machine has 4 CPU numbered 0, 1, 4
148  *              and 5. In this case, this procedure will return 6.
149  *
150  * RETURNS:
151  * Number of processors.
152  * 0: one proc and non SMP kernel.
153  * 1: one proc and SMP kernel (NB: On SMP machines where all the CPUs but
154  *    one have been disabled, we get the total number of proc since we use
155  *    /sys to count them).
156  * 2: two proc...
157  ***************************************************************************
158  */
159 __nr_t get_cpu_nr(unsigned int max_nr_cpus, int highest)
160 {
161         __nr_t cpu_nr;
162
163         if ((cpu_nr = get_sys_cpu_nr(highest)) == 0) {
164                 /* /sys may be not mounted. Use /proc/stat instead */
165                 cpu_nr = get_proc_cpu_nr();
166         }
167
168         if (cpu_nr > max_nr_cpus) {
169                 fprintf(stderr, _("Cannot handle so many processors!\n"));
170                 exit(1);
171         }
172
173         return cpu_nr;
174 }
175
176 /*
177  ***************************************************************************
178  * Find number of interrupts available per processor (use
179  * /proc/interrupts file or /proc/softirqs).
180  *
181  * IN:
182  * @file                /proc file to read (interrupts or softirqs).
183  * @max_nr_irqcpu       Maximum number of interrupts per processor that
184  *                      sadc can handle.
185  * @cpu_nr              Number of processors.
186  *
187  * RETURNS:
188  * Number of interrupts per processor.
189  ***************************************************************************
190  */
191 __nr_t get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
192 {
193         FILE *fp;
194         char *line = NULL;
195         __nr_t irq = 0;
196         int p;
197
198         if ((fp = fopen(file, "r")) == NULL)
199                 return 0;       /* No interrupts file */
200
201         SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
202
203         while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr , fp) != NULL) &&
204                (irq < max_nr_irqcpu)) {
205                 p = strcspn(line, ":");
206                 if ((p > 0) && (p < 16)) {
207                         irq++;
208                 }
209         }
210
211         fclose(fp);
212
213         free(line);
214
215         return irq;
216 }
217
218 /*
219  ***************************************************************************
220  * Find number of devices and partitions available in /proc/diskstats.
221  *
222  * IN:
223  * @count_part          Set to TRUE if devices _and_ partitions are to be
224  *                      counted.
225  * @only_used_dev       When counting devices, set to TRUE if only devices
226  *                      with non zero stats must be counted.
227  *
228  * RETURNS:
229  * Number of devices (and partitions).
230  ***************************************************************************
231  */
232 __nr_t get_diskstats_dev_nr(int count_part, int only_used_dev)
233 {
234         FILE *fp;
235         char line[256];
236         char dev_name[MAX_NAME_LEN];
237         __nr_t dev = 0;
238         int i;
239         unsigned long rd_ios, wr_ios;
240
241         if ((fp = fopen(DISKSTATS, "r")) == NULL)
242                 /* File non-existent */
243                 return 0;
244
245         /*
246          * Counting devices and partitions is simply a matter of counting
247          * the number of lines...
248          */
249         while (fgets(line, sizeof(line), fp) != NULL) {
250                 if (!count_part) {
251                         i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu",
252                                    dev_name, &rd_ios, &wr_ios);
253                         if ((i == 2) || !is_device(SLASH_SYS, dev_name, ACCEPT_VIRTUAL_DEVICES))
254                                 /* It was a partition and not a device */
255                                 continue;
256                         if (only_used_dev && !rd_ios && !wr_ios)
257                                 /* Unused device */
258                                 continue;
259                 }
260                 dev++;
261         }
262
263         fclose(fp);
264
265         return dev;
266 }
267
268 #ifdef SOURCE_SADC
269 /*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
270
271 /*
272  ***************************************************************************
273  * Find number of serial lines that support tx/rx accounting
274  * in /proc/tty/driver/serial file.
275  *
276  * RETURNS:
277  * Number of serial lines supporting tx/rx accouting.
278  ***************************************************************************
279  */
280 __nr_t get_serial_nr(void)
281 {
282         FILE *fp;
283         char line[256];
284         __nr_t sl = 0;
285
286         if ((fp = fopen(SERIAL, "r")) == NULL)
287                 return 0;       /* No SERIAL file */
288
289         while (fgets(line, sizeof(line), fp) != NULL) {
290                 /*
291                  * tx/rx statistics are always present,
292                  * except when serial line is unknown.
293                  */
294                 if (strstr(line, "tx:") != NULL) {
295                         sl++;
296                 }
297         }
298
299         fclose(fp);
300
301         return sl;
302 }
303
304 /*
305  ***************************************************************************
306  * Find number of interfaces (network devices) that are in /proc/net/dev
307  * file.
308  *
309  * RETURNS:
310  * Number of network interfaces.
311  ***************************************************************************
312  */
313 __nr_t get_iface_nr(void)
314 {
315         FILE *fp;
316         char line[128];
317         __nr_t iface = 0;
318
319         if ((fp = fopen(NET_DEV, "r")) == NULL)
320                 return 0;       /* No network device file */
321
322         while (fgets(line, sizeof(line), fp) != NULL) {
323                 if (strchr(line, ':')) {
324                         iface++;
325                 }
326         }
327
328         fclose(fp);
329
330         return iface;
331 }
332
333 /*
334  ***************************************************************************
335  * Get number of devices in /proc/diskstats.
336  *
337  * IN:
338  * @f   Non zero (true) if disks *and* partitions should be counted, and
339  *      zero (false) if only disks must be counted.
340  *
341  * RETURNS:
342  * Number of devices.
343  ***************************************************************************
344  */
345 __nr_t get_disk_nr(unsigned int f)
346 {
347         __nr_t disk_nr;
348
349         /*
350          * Partitions are taken into account by sar -d only with
351          * kernels 2.6.25 and later.
352          */
353         disk_nr = get_diskstats_dev_nr(f, CNT_USED_DEV);
354
355         return disk_nr;
356 }
357
358 /*
359  ***************************************************************************
360  * Count number of possible frequencies for CPU#0.
361  *
362  * RETURNS:
363  * Number of frequencies.
364  ***************************************************************************
365  */
366 __nr_t get_freq_nr(void)
367 {
368         FILE *fp;
369         char filename[MAX_PF_NAME];
370         char line[128];
371         __nr_t freq = 0;
372
373         snprintf(filename, MAX_PF_NAME, "%s/cpu0/%s",
374                  SYSFS_DEVCPU, SYSFS_TIME_IN_STATE);
375         if ((fp = fopen(filename, "r")) == NULL)
376                 return 0;       /* No time_in_state file for CPU#0 */
377
378         while (fgets(line, sizeof(line), fp) != NULL) {
379                 freq++;
380         }
381
382         fclose(fp);
383
384         return freq;
385 }
386
387 /*
388  ***************************************************************************
389  * Count number of USB devices in /sys/bus/usb/devices.
390  *
391  * RETURNS:
392  * Number of USB devices plugged into the system.
393  * Don't count USB root hubs.
394  * Return -1 if directory doesn't exist in sysfs.
395  ***************************************************************************
396  */
397 __nr_t get_usb_nr(void)
398 {
399         DIR *dir;
400         struct dirent *drd;
401         __nr_t usb = 0;
402
403         /* Open relevant /sys directory */
404         if ((dir = opendir(SYSFS_USBDEV)) == NULL)
405                 return -1;
406
407         /* Get current file entry */
408         while ((drd = readdir(dir)) != NULL) {
409
410                 if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
411                         usb++;
412                 }
413         }
414
415         /* Close directory */
416         closedir(dir);
417
418         return usb;
419 }
420
421 /*
422  ***************************************************************************
423  * Find number of filesystems in /etc/mtab. Pseudo-filesystems are ignored.
424  *
425  * RETURNS:
426  * Number of filesystems.
427  ***************************************************************************
428  */
429 __nr_t get_filesystem_nr(void)
430 {
431         FILE *fp;
432         char line[512], fs_name[MAX_FS_LEN], mountp[256], type[128];
433         char *pos = 0, *pos2 = 0;
434         __nr_t fs = 0;
435         int skip = 0, skip_next = 0;
436         struct statvfs buf;
437
438         if ((fp = fopen(MTAB, "r")) == NULL)
439                 /* File non-existent */
440                 return 0;
441
442         /* Get current filesystem */
443         while (fgets(line, sizeof(line), fp) != NULL) {
444                 /*
445                  * Ignore line if the preceding line did not contain '\n'.
446                  * (Some very long lines may be found for instance when
447                  * overlay2 filesystem with docker is used).
448                  */
449                 skip = skip_next;
450                 skip_next = (strchr(line, '\n') == NULL);
451                 if (skip)
452                         continue;
453
454                 if (line[0] == '/') {
455                         /* Find field separator position */
456                         pos = strchr(line, ' ');
457                         if (pos == NULL)
458                                 continue;
459
460                         /*
461                          * Find second field separator position,
462                          * read filesystem type,
463                          * if filesystem type is autofs, skip it
464                         */
465                         pos2 = strchr(pos + 1, ' ');
466                         if (pos2 == NULL)
467                                 continue;
468
469                         sscanf(pos2 + 1, "%127s", type);
470                         if(strcmp(type, "autofs") == 0)
471                                 continue;
472
473                         /* Read filesystem name and mount point */
474                         sscanf(line, "%127s", fs_name);
475                         sscanf(pos + 1, "%255s", mountp);
476
477                         /* Replace octal codes */
478                         oct2chr(mountp);
479
480                         /* Check that total size is not zero */
481                         if (__statvfs(mountp, &buf) < 0)
482                                 continue;
483
484                         if (buf.f_blocks) {
485                                 fs++;
486                         }
487                 }
488         }
489
490         fclose(fp);
491
492         return fs;
493 }
494
495 /*
496  ***************************************************************************
497  * Find number of fibre channel hosts in /sys/class/fc_host/.
498  *
499  * RETURNS:
500  * Number of FC hosts.
501  * Return -1 if directory doesn't exist in sysfs.
502  ***************************************************************************
503  */
504 __nr_t get_fchost_nr(void)
505 {
506         DIR *dir;
507         struct dirent *drd;
508         __nr_t fc = 0;
509
510         if ((dir = opendir(SYSFS_FCHOST)) == NULL) {
511                 /* Directory non-existent */
512                 return -1;
513         }
514
515         while ((drd = readdir(dir)) != NULL) {
516
517                 if (!strncmp(drd->d_name, "host", 4)) {
518                         fc++;
519                 }
520         }
521
522         /* Close directory */
523         closedir(dir);
524
525         return fc;
526 }
527
528 /*------------------ END: FUNCTIONS USED BY SADC ONLY ---------------------*/
529 #endif /* SOURCE_SADC */