]> granicus.if.org Git - sysstat/commitdiff
Split rd_stats.c and rd_stats.h files
authorSebastien GODARD <sysstat@orange.fr.fake>
Sun, 19 May 2013 09:18:22 +0000 (11:18 +0200)
committerSebastien GODARD <sysstat@orange.fr.fake>
Sun, 19 May 2013 09:18:22 +0000 (11:18 +0200)
rd_stats.c file was becoming really big. So remove from it functions
used to count the number of items and put them in a separate file
(count.c).
Functions prototypes go to count.h.

12 files changed:
CHANGES
Makefile.in
cifsiostat.c
count.c [new file with mode: 0644]
count.h [new file with mode: 0644]
iostat.c
mpstat.c
nfsiostat.c
pidstat.c
rd_stats.c
rd_stats.h
sa_wrap.c

diff --git a/CHANGES b/CHANGES
index d1742356ababd03e46fcce6973004a571dc23447..f08aac972d04fec9e56d125a6291804cb9c9f7ef 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,8 @@ xxxx/xx/xx: Version 10.1.6 - Sebastien Godard (sysstat <at> orange.fr)
          to enter 'iostat -px 2 5' since no device name is given to
          option -p. This also concerns pidstat option -U.
        * Fixed type for memfree element in XSD document.
+       * Removed functions used to count number of items from rd_stats.c
+         and put them in a separate file (count.c).
        * Typos fixed. README file updated.
        * Yet another cosmetic fix in pidstat manual page. Sar manual
          page updated.
index e7f78a5a6e2184397b51cff1838c04165a42cd95..05cd7e48ae7af1e6de518abebf25f14965c892e4 100644 (file)
@@ -179,6 +179,8 @@ act_sadf.o: activity.c sa.h rndr_stats.h xml_stats.h json_stats.h
 
 rd_stats.o: rd_stats.c common.h rd_stats.h ioconf.h sysconfig.h
 
+count.o: count.c common.h rd_stats.h
+
 rd_sensors.o: rd_sensors.c common.h rd_sensors.h sysconfig.h
 
 pr_stats.o: pr_stats.c sa.h ioconf.h sysconfig.h pr_stats.h
@@ -189,7 +191,7 @@ xml_stats.o: xml_stats.c sa.h sadf.h ioconf.h sysconfig.h xml_stats.h
 
 json_stats.o: json_stats.c sa.h sadf.h ioconf.h sysconfig.h json_stats.h
 
-sa_wrap.o: sa_wrap.c sa.h rd_stats.h rd_sensors.h
+sa_wrap.o: sa_wrap.c sa.h rd_stats.h count.h rd_sensors.h
 
 format.o: format.c sadf.h
 
@@ -200,7 +202,7 @@ sadf_misc.o: sadf_misc.c sadf.h sa.h
 libsyscom.a: common.o ioconf.o
        $(AR) rvs $@ $?
 
-librdstats.a: librdstats.a(rd_stats.o)
+librdstats.a: librdstats.a(rd_stats.o count.o)
 
 librdsensors.a: librdsensors.a(rd_sensors.o)
 
@@ -217,23 +219,23 @@ sadf.o: sadf.c sadf.h version.h sa.h common.h ioconf.h sysconfig.h
 
 sadf: sadf.o act_sadf.o format.o sadf_misc.o rndr_stats.o xml_stats.o json_stats.o sa_common.o libsyscom.a
 
-iostat.o: iostat.c iostat.h version.h common.h ioconf.h sysconfig.h rd_stats.h
+iostat.o: iostat.c iostat.h version.h common.h ioconf.h sysconfig.h rd_stats.h count.h
 
 iostat: iostat.o librdstats.a libsyscom.a
 
-pidstat.o: pidstat.c pidstat.h version.h common.h rd_stats.h
+pidstat.o: pidstat.c pidstat.h version.h common.h rd_stats.h count.h
 
 pidstat: pidstat.o librdstats.a libsyscom.a
 
-mpstat.o: mpstat.c mpstat.h version.h common.h rd_stats.h
+mpstat.o: mpstat.c mpstat.h version.h common.h rd_stats.h count.h
 
 mpstat: mpstat.o librdstats.a libsyscom.a
 
-nfsiostat.o: nfsiostat.c nfsiostat.h version.h common.h
+nfsiostat.o: nfsiostat.c nfsiostat.h count.h version.h common.h
 
 nfsiostat: nfsiostat.o librdstats.a libsyscom.a
 
-cifsiostat.o: cifsiostat.c cifsiostat.h version.h common.h
+cifsiostat.o: cifsiostat.c cifsiostat.h count.h version.h common.h
 
 cifsiostat: cifsiostat.o librdstats.a libsyscom.a
 
index d59670691d2ad77a89d371f50a185476cc24d2c3..9c5054a3cd585bf9b42fad42385660db6daafe34 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "version.h"
 #include "cifsiostat.h"
+#include "count.h"
 #include "common.h"
 
 #ifdef USE_NLS
diff --git a/count.c b/count.c
new file mode 100644 (file)
index 0000000..51dbc7c
--- /dev/null
+++ b/count.c
@@ -0,0 +1,474 @@
+/*
+ * count.c: Read system statistics
+ * (C) 1999-2013 by Sebastien GODARD (sysstat <at> orange.fr)
+ *
+ ***************************************************************************
+ * This program is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU General Public License as published  by  the *
+ * Free Software Foundation; either version 2 of the License, or (at  your *
+ * option) any later version.                                              *
+ *                                                                         *
+ * This program is distributed in the hope that it  will  be  useful,  but *
+ * WITHOUT ANY WARRANTY; without the implied warranty  of  MERCHANTABILITY *
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
+ * for more details.                                                       *
+ *                                                                         *
+ * You should have received a copy of the GNU General Public License along *
+ * with this program; if not, write to the Free Software Foundation, Inc., *
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                   *
+ ***************************************************************************
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <dirent.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "rd_stats.h"
+
+#ifdef USE_NLS
+#include <locale.h>
+#include <libintl.h>
+#define _(string) gettext(string)
+#else
+#define _(string) (string)
+#endif
+
+
+/*
+ ***************************************************************************
+ * Count number of interrupts that are in /proc/stat file.
+ *
+ * RETURNS:
+ * Number of interrupts, including total number of interrupts.
+ ***************************************************************************
+ */
+int get_irq_nr(void)
+{
+       FILE *fp;
+       char line[8192];
+       int in = 0;
+       int pos = 4;
+
+       if ((fp = fopen(STAT, "r")) == NULL)
+               return 0;
+
+       while (fgets(line, 8192, fp) != NULL) {
+
+               if (!strncmp(line, "intr ", 5)) {
+
+                       while (pos < strlen(line)) {
+                               in++;
+                               pos += strcspn(line + pos + 1, " ") + 1;
+                       }
+               }
+       }
+
+       fclose(fp);
+
+       return in;
+}
+
+/*
+ ***************************************************************************
+ * Find number of serial lines that support tx/rx accounting
+ * in /proc/tty/driver/serial file.
+ *
+ * RETURNS:
+ * Number of serial lines supporting tx/rx accouting.
+ ***************************************************************************
+ */
+int get_serial_nr(void)
+{
+       FILE *fp;
+       char line[256];
+       int sl = 0;
+
+       if ((fp = fopen(SERIAL, "r")) == NULL)
+               return 0;       /* No SERIAL file */
+
+       while (fgets(line, 256, fp) != NULL) {
+               /*
+                * tx/rx statistics are always present,
+                * except when serial line is unknown.
+                */
+               if (strstr(line, "tx:") != NULL) {
+                       sl++;
+               }
+       }
+
+       fclose(fp);
+
+       return sl;
+}
+
+/*
+ ***************************************************************************
+ * Find number of interfaces (network devices) that are in /proc/net/dev
+ * file.
+ *
+ * RETURNS:
+ * Number of network interfaces.
+ ***************************************************************************
+ */
+int get_iface_nr(void)
+{
+       FILE *fp;
+       char line[128];
+       int iface = 0;
+
+       if ((fp = fopen(NET_DEV, "r")) == NULL)
+               return 0;       /* No network device file */
+
+       while (fgets(line, 128, fp) != NULL) {
+               if (strchr(line, ':')) {
+                       iface++;
+               }
+       }
+
+       fclose(fp);
+
+       return iface;
+}
+
+/*
+ ***************************************************************************
+ * Find number of devices and partitions available in /proc/diskstats.
+ *
+ * IN:
+ * @count_part         Set to TRUE if devices _and_ partitions are to be
+ *                     counted.
+ * @only_used_dev      When counting devices, set to TRUE if only devices
+ *                     with non zero stats must be counted.
+ *
+ * RETURNS:
+ * Number of devices (and partitions).
+ ***************************************************************************
+ */
+int get_diskstats_dev_nr(int count_part, int only_used_dev)
+{
+       FILE *fp;
+       char line[256];
+       char dev_name[MAX_NAME_LEN];
+       int dev = 0, i;
+       unsigned long rd_ios, wr_ios;
+
+       if ((fp = fopen(DISKSTATS, "r")) == NULL)
+               /* File non-existent */
+               return 0;
+
+       /*
+        * Counting devices and partitions is simply a matter of counting
+        * the number of lines...
+        */
+       while (fgets(line, 256, fp) != NULL) {
+               if (!count_part) {
+                       i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu",
+                                  dev_name, &rd_ios, &wr_ios);
+                       if ((i == 2) || !is_device(dev_name, ACCEPT_VIRTUAL_DEVICES))
+                               /* It was a partition and not a device */
+                               continue;
+                       if (only_used_dev && !rd_ios && !wr_ios)
+                               /* Unused device */
+                               continue;
+               }
+               dev++;
+       }
+
+       fclose(fp);
+
+       return dev;
+}
+
+/*
+ ***************************************************************************
+ * Get number of devices in /proc/diskstats.
+ *
+ * IN:
+ * @f  Non zero (true) if disks *and* partitions should be counted, and
+ *     zero (false) if only disks must be counted.
+ *
+ * RETURNS:
+ * Number of devices.
+ ***************************************************************************
+ */
+int get_disk_nr(unsigned int f)
+{
+       int disk_nr;
+
+       /*
+        * Partitions are taken into account by sar -d only with
+        * kernels 2.6.25 and later.
+        */
+       disk_nr = get_diskstats_dev_nr(f, CNT_USED_DEV);
+
+       return disk_nr;
+}
+
+/*
+ ***************************************************************************
+ * Count number of processors in /sys.
+ *
+ * RETURNS:
+ * Number of processors (online and offline).
+ * A value of 0 means that /sys was not mounted.
+ * A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)).
+ ***************************************************************************
+ */
+int get_sys_cpu_nr(void)
+{
+       DIR *dir;
+       struct dirent *drd;
+       struct stat buf;
+       char line[MAX_PF_NAME];
+       int proc_nr = 0;
+
+       /* Open relevant /sys directory */
+       if ((dir = opendir(SYSFS_DEVCPU)) == NULL)
+               return 0;
+
+       /* Get current file entry */
+       while ((drd = readdir(dir)) != NULL) {
+
+               if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
+                       snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name);
+                       line[MAX_PF_NAME - 1] = '\0';
+                       if (stat(line, &buf) < 0)
+                               continue;
+                       if (S_ISDIR(buf.st_mode)) {
+                               proc_nr++;
+                       }
+               }
+       }
+
+       /* Close directory */
+       closedir(dir);
+
+       return proc_nr;
+}
+
+/*
+ ***************************************************************************
+ * Count number of processors in /proc/stat.
+ *
+ * RETURNS:
+ * Number of processors. The returned value is greater than or equal to the
+ * number of online processors.
+ * A value of 0 means one processor and non SMP kernel.
+ * A value of N (!=0) means N processor(s) (0 .. N-1) with SMP kernel.
+ ***************************************************************************
+ */
+int get_proc_cpu_nr(void)
+{
+       FILE *fp;
+       char line[16];
+       int num_proc, proc_nr = -1;
+
+       if ((fp = fopen(STAT, "r")) == NULL) {
+               fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
+               exit(1);
+       }
+
+       while (fgets(line, 16, fp) != NULL) {
+
+               if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) {
+                       sscanf(line + 3, "%d", &num_proc);
+                       if (num_proc > proc_nr) {
+                               proc_nr = num_proc;
+                       }
+               }
+       }
+
+       fclose(fp);
+
+       return (proc_nr + 1);
+}
+
+/*
+ ***************************************************************************
+ * Count the number of processors on the machine.
+ * Try to use /sys for that, or /proc/stat if /sys doesn't exist.
+ *
+ * IN:
+ * @max_nr_cpus        Maximum number of proc that sysstat can handle.
+ *
+ * RETURNS:
+ * Number of processors.
+ * 0: one proc and non SMP kernel.
+ * 1: one proc and SMP kernel (NB: On SMP machines where all the CPUs but
+ *    one have been disabled, we get the total number of proc since we use
+ *    /sys to count them).
+ * 2: two proc...
+ ***************************************************************************
+ */
+int get_cpu_nr(unsigned int max_nr_cpus)
+{
+       int cpu_nr;
+
+       if ((cpu_nr = get_sys_cpu_nr()) == 0) {
+               /* /sys may be not mounted. Use /proc/stat instead */
+               cpu_nr = get_proc_cpu_nr();
+       }
+
+       if (cpu_nr > max_nr_cpus) {
+               fprintf(stderr, _("Cannot handle so many processors!\n"));
+               exit(1);
+       }
+
+       return cpu_nr;
+}
+
+/*
+ ***************************************************************************
+ * Find number of interrupts available per processor (use
+ * /proc/interrupts file or /proc/softirqs).
+ *
+ * IN:
+ * @file               /proc file to read (interrupts or softirqs).
+ * @max_nr_irqcpu       Maximum number of interrupts per processor that
+ *                      sadc can handle.
+ * @cpu_nr             Number of processors.
+ *
+ * RETURNS:
+ * Number of interrupts per processor + a pre-allocation constant.
+ ***************************************************************************
+ */
+int get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
+{
+       FILE *fp;
+       char *line = NULL;
+       unsigned int irq = 0;
+       int p;
+
+       if ((fp = fopen(file, "r")) == NULL)
+               return 0;       /* No interrupts file */
+
+       SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
+
+       while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr , fp) != NULL) &&
+              (irq < max_nr_irqcpu)) {
+               p = strcspn(line, ":");
+               if ((p > 0) && (p < 16)) {
+                       irq++;
+               }
+       }
+
+       fclose(fp);
+
+       if (line) {
+               free(line);
+       }
+
+       return irq;
+}
+
+/*
+ ***************************************************************************
+ * Count number of possible frequencies for CPU#0.
+ *
+ * RETURNS:
+ * Number of frequencies.
+ ***************************************************************************
+ */
+int get_freq_nr(void)
+{
+       FILE *fp;
+       char filename[MAX_PF_NAME];
+       char line[128];
+       int freq = 0;
+
+       snprintf(filename, MAX_PF_NAME, "%s/cpu0/%s",
+                SYSFS_DEVCPU, SYSFS_TIME_IN_STATE);
+       if ((fp = fopen(filename, "r")) == NULL)
+               return 0;       /* No time_in_state file for CPU#0 */
+
+       while (fgets(line, 128, fp) != NULL) {
+               freq++;
+       }
+
+       fclose(fp);
+
+       return freq;
+}
+
+/*
+ ***************************************************************************
+ * Count number of USB devices in /sys/bus/usb/devices.
+ *
+ * RETURNS:
+ * Number of USB devices plugged into the system.
+ * Don't count USB root hubs.
+ * Return -1 if directory doesn't exist in sysfs.
+ ***************************************************************************
+ */
+int get_usb_nr(void)
+{
+       DIR *dir;
+       struct dirent *drd;
+       int usb = 0;
+
+       /* Open relevant /sys directory */
+       if ((dir = opendir(SYSFS_USBDEV)) == NULL)
+               return -1;
+
+       /* Get current file entry */
+       while ((drd = readdir(dir)) != NULL) {
+
+               if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
+                       usb++;
+               }
+       }
+
+       /* Close directory */
+       closedir(dir);
+
+       return usb;
+}
+
+/*
+ ***************************************************************************
+ * Find number of filesystems in /etc/mtab. Pseudo-filesystems are ignored.
+ *
+ * RETURNS:
+ * Number of filesystems.
+ ***************************************************************************
+ */
+int get_filesystem_nr(void)
+{
+       FILE *fp;
+       char line[256], fs_name[MAX_FS_LEN], mountp[128];
+       int fs = 0;
+       struct statfs buf;
+
+       if ((fp = fopen(MTAB, "r")) == NULL)
+               /* File non-existent */
+               return 0;
+
+       /* Get current filesystem */
+       while (fgets(line, 256, fp) != NULL) {
+               if (line[0] == '/') {
+                       
+                       /* Read filesystem name and mount point */
+                       sscanf(line, "%71s %127s", fs_name, mountp);
+                       
+                       /* Check that total size is not null */
+                       if (statfs(mountp, &buf) < 0)
+                               continue;
+                       
+                       if (buf.f_blocks) {
+                               fs++;
+                       }
+               }
+       }
+
+       fclose(fp);
+
+       return fs;
+}
diff --git a/count.h b/count.h
new file mode 100644 (file)
index 0000000..093e6e7
--- /dev/null
+++ b/count.h
@@ -0,0 +1,38 @@
+/*
+ * count.h: Include file used to read system statistics
+ * (C) 1999-2013 by Sebastien Godard (sysstat <at> orange.fr)
+ */
+
+#ifndef _COUNT_H
+#define _COUNT_H
+
+#include "common.h"
+
+/*
+ ***************************************************************************
+ * Prototypes for functions used to count number of items.
+ ***************************************************************************
+ */
+
+extern int
+       get_irq_nr(void);
+extern int
+       get_serial_nr(void);
+extern int
+       get_iface_nr(void);
+extern int
+       get_diskstats_dev_nr(int, int);
+extern int
+       get_disk_nr(unsigned int);
+extern int
+       get_cpu_nr(unsigned int);
+extern int
+       get_irqcpu_nr(char *, int, int);
+extern int
+       get_freq_nr(void);
+extern int
+       get_usb_nr(void);
+extern int
+       get_filesystem_nr(void);
+
+#endif /* _COUNT_H */
index 93a503ccd266a110d994fda6e6d7d21937f986b8..8b5034e2ac935444a9741d95df6eb2ce8b736084 100644 (file)
--- a/iostat.c
+++ b/iostat.c
@@ -38,6 +38,7 @@
 #include "common.h"
 #include "ioconf.h"
 #include "rd_stats.h"
+#include "count.h"
 
 #ifdef USE_NLS
 #include <locale.h>
index 585e089674eff556d8ad211242eb96e1926f4046..efe7170086b189f102f92f7cc4b15926d22614f5 100644 (file)
--- a/mpstat.c
+++ b/mpstat.c
@@ -32,6 +32,7 @@
 #include "mpstat.h"
 #include "common.h"
 #include "rd_stats.h"
+#include "count.h"
 
 #ifdef USE_NLS
 #include <locale.h>
index edb1614cd5d64e9c8f367b9990418ad7e3097e55..b437a39045e3c10c0830c77a93744bc8f5fe6298 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "version.h"
 #include "nfsiostat.h"
+#include "count.h"
 #include "common.h"
 
 #ifdef USE_NLS
index e2dce186a59edf5295bd3602c2ef8f392e9b24df..ab565a6bd32142ec4ac7f9c5b1f3a36b9ac52c8c 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -35,6 +35,7 @@
 #include "pidstat.h"
 #include "common.h"
 #include "rd_stats.h"
+#include "count.h"
 
 #ifdef USE_NLS
 #include <locale.h>
index b9377f49c469c9f31995a724fe7c46fb8b94dc9b..d8fdf774ed75bbb8775aa6235cadfe5eb42f1f3b 100644 (file)
@@ -1936,434 +1936,3 @@ void read_uptime(unsigned long long *uptime)
        fclose(fp);
 
 }
-
-/*
- ***************************************************************************
- * Count number of interrupts that are in /proc/stat file.
- *
- * RETURNS:
- * Number of interrupts, including total number of interrupts.
- ***************************************************************************
- */
-int get_irq_nr(void)
-{
-       FILE *fp;
-       char line[8192];
-       int in = 0;
-       int pos = 4;
-
-       if ((fp = fopen(STAT, "r")) == NULL)
-               return 0;
-
-       while (fgets(line, 8192, fp) != NULL) {
-
-               if (!strncmp(line, "intr ", 5)) {
-
-                       while (pos < strlen(line)) {
-                               in++;
-                               pos += strcspn(line + pos + 1, " ") + 1;
-                       }
-               }
-       }
-
-       fclose(fp);
-
-       return in;
-}
-
-/*
- ***************************************************************************
- * Find number of serial lines that support tx/rx accounting
- * in /proc/tty/driver/serial file.
- *
- * RETURNS:
- * Number of serial lines supporting tx/rx accouting.
- ***************************************************************************
- */
-int get_serial_nr(void)
-{
-       FILE *fp;
-       char line[256];
-       int sl = 0;
-
-       if ((fp = fopen(SERIAL, "r")) == NULL)
-               return 0;       /* No SERIAL file */
-
-       while (fgets(line, 256, fp) != NULL) {
-               /*
-                * tx/rx statistics are always present,
-                * except when serial line is unknown.
-                */
-               if (strstr(line, "tx:") != NULL) {
-                       sl++;
-               }
-       }
-
-       fclose(fp);
-
-       return sl;
-}
-
-/*
- ***************************************************************************
- * Find number of interfaces (network devices) that are in /proc/net/dev
- * file.
- *
- * RETURNS:
- * Number of network interfaces.
- ***************************************************************************
- */
-int get_iface_nr(void)
-{
-       FILE *fp;
-       char line[128];
-       int iface = 0;
-
-       if ((fp = fopen(NET_DEV, "r")) == NULL)
-               return 0;       /* No network device file */
-
-       while (fgets(line, 128, fp) != NULL) {
-               if (strchr(line, ':')) {
-                       iface++;
-               }
-       }
-
-       fclose(fp);
-
-       return iface;
-}
-
-/*
- ***************************************************************************
- * Find number of devices and partitions available in /proc/diskstats.
- *
- * IN:
- * @count_part         Set to TRUE if devices _and_ partitions are to be
- *                     counted.
- * @only_used_dev      When counting devices, set to TRUE if only devices
- *                     with non zero stats must be counted.
- *
- * RETURNS:
- * Number of devices (and partitions).
- ***************************************************************************
- */
-int get_diskstats_dev_nr(int count_part, int only_used_dev)
-{
-       FILE *fp;
-       char line[256];
-       char dev_name[MAX_NAME_LEN];
-       int dev = 0, i;
-       unsigned long rd_ios, wr_ios;
-
-       if ((fp = fopen(DISKSTATS, "r")) == NULL)
-               /* File non-existent */
-               return 0;
-
-       /*
-        * Counting devices and partitions is simply a matter of counting
-        * the number of lines...
-        */
-       while (fgets(line, 256, fp) != NULL) {
-               if (!count_part) {
-                       i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu",
-                                  dev_name, &rd_ios, &wr_ios);
-                       if ((i == 2) || !is_device(dev_name, ACCEPT_VIRTUAL_DEVICES))
-                               /* It was a partition and not a device */
-                               continue;
-                       if (only_used_dev && !rd_ios && !wr_ios)
-                               /* Unused device */
-                               continue;
-               }
-               dev++;
-       }
-
-       fclose(fp);
-
-       return dev;
-}
-
-/*
- ***************************************************************************
- * Get number of devices in /proc/diskstats.
- *
- * IN:
- * @f  Non zero (true) if disks *and* partitions should be counted, and
- *     zero (false) if only disks must be counted.
- *
- * RETURNS:
- * Number of devices.
- ***************************************************************************
- */
-int get_disk_nr(unsigned int f)
-{
-       int disk_nr;
-
-       /*
-        * Partitions are taken into account by sar -d only with
-        * kernels 2.6.25 and later.
-        */
-       disk_nr = get_diskstats_dev_nr(f, CNT_USED_DEV);
-
-       return disk_nr;
-}
-
-/*
- ***************************************************************************
- * Count number of processors in /sys.
- *
- * RETURNS:
- * Number of processors (online and offline).
- * A value of 0 means that /sys was not mounted.
- * A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)).
- ***************************************************************************
- */
-int get_sys_cpu_nr(void)
-{
-       DIR *dir;
-       struct dirent *drd;
-       struct stat buf;
-       char line[MAX_PF_NAME];
-       int proc_nr = 0;
-
-       /* Open relevant /sys directory */
-       if ((dir = opendir(SYSFS_DEVCPU)) == NULL)
-               return 0;
-
-       /* Get current file entry */
-       while ((drd = readdir(dir)) != NULL) {
-
-               if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
-                       snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name);
-                       line[MAX_PF_NAME - 1] = '\0';
-                       if (stat(line, &buf) < 0)
-                               continue;
-                       if (S_ISDIR(buf.st_mode)) {
-                               proc_nr++;
-                       }
-               }
-       }
-
-       /* Close directory */
-       closedir(dir);
-
-       return proc_nr;
-}
-
-/*
- ***************************************************************************
- * Count number of processors in /proc/stat.
- *
- * RETURNS:
- * Number of processors. The returned value is greater than or equal to the
- * number of online processors.
- * A value of 0 means one processor and non SMP kernel.
- * A value of N (!=0) means N processor(s) (0 .. N-1) with SMP kernel.
- ***************************************************************************
- */
-int get_proc_cpu_nr(void)
-{
-       FILE *fp;
-       char line[16];
-       int num_proc, proc_nr = -1;
-
-       if ((fp = fopen(STAT, "r")) == NULL) {
-               fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
-               exit(1);
-       }
-
-       while (fgets(line, 16, fp) != NULL) {
-
-               if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) {
-                       sscanf(line + 3, "%d", &num_proc);
-                       if (num_proc > proc_nr) {
-                               proc_nr = num_proc;
-                       }
-               }
-       }
-
-       fclose(fp);
-
-       return (proc_nr + 1);
-}
-
-/*
- ***************************************************************************
- * Count the number of processors on the machine.
- * Try to use /sys for that, or /proc/stat if /sys doesn't exist.
- *
- * IN:
- * @max_nr_cpus        Maximum number of proc that sysstat can handle.
- *
- * RETURNS:
- * Number of processors.
- * 0: one proc and non SMP kernel.
- * 1: one proc and SMP kernel (NB: On SMP machines where all the CPUs but
- *    one have been disabled, we get the total number of proc since we use
- *    /sys to count them).
- * 2: two proc...
- ***************************************************************************
- */
-int get_cpu_nr(unsigned int max_nr_cpus)
-{
-       int cpu_nr;
-
-       if ((cpu_nr = get_sys_cpu_nr()) == 0) {
-               /* /sys may be not mounted. Use /proc/stat instead */
-               cpu_nr = get_proc_cpu_nr();
-       }
-
-       if (cpu_nr > max_nr_cpus) {
-               fprintf(stderr, _("Cannot handle so many processors!\n"));
-               exit(1);
-       }
-
-       return cpu_nr;
-}
-
-/*
- ***************************************************************************
- * Find number of interrupts available per processor (use
- * /proc/interrupts file or /proc/softirqs).
- *
- * IN:
- * @file               /proc file to read (interrupts or softirqs).
- * @max_nr_irqcpu       Maximum number of interrupts per processor that
- *                      sadc can handle.
- * @cpu_nr             Number of processors.
- *
- * RETURNS:
- * Number of interrupts per processor + a pre-allocation constant.
- ***************************************************************************
- */
-int get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
-{
-       FILE *fp;
-       char *line = NULL;
-       unsigned int irq = 0;
-       int p;
-
-       if ((fp = fopen(file, "r")) == NULL)
-               return 0;       /* No interrupts file */
-
-       SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
-
-       while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr , fp) != NULL) &&
-              (irq < max_nr_irqcpu)) {
-               p = strcspn(line, ":");
-               if ((p > 0) && (p < 16)) {
-                       irq++;
-               }
-       }
-
-       fclose(fp);
-
-       if (line) {
-               free(line);
-       }
-
-       return irq;
-}
-
-/*
- ***************************************************************************
- * Count number of possible frequencies for CPU#0.
- *
- * RETURNS:
- * Number of frequencies.
- ***************************************************************************
- */
-int get_freq_nr(void)
-{
-       FILE *fp;
-       char filename[MAX_PF_NAME];
-       char line[128];
-       int freq = 0;
-
-       snprintf(filename, MAX_PF_NAME, "%s/cpu0/%s",
-                SYSFS_DEVCPU, SYSFS_TIME_IN_STATE);
-       if ((fp = fopen(filename, "r")) == NULL)
-               return 0;       /* No time_in_state file for CPU#0 */
-
-       while (fgets(line, 128, fp) != NULL) {
-               freq++;
-       }
-
-       fclose(fp);
-
-       return freq;
-}
-
-/*
- ***************************************************************************
- * Count number of USB devices in /sys/bus/usb/devices.
- *
- * RETURNS:
- * Number of USB devices plugged into the system.
- * Don't count USB root hubs.
- * Return -1 if directory doesn't exist in sysfs.
- ***************************************************************************
- */
-int get_usb_nr(void)
-{
-       DIR *dir;
-       struct dirent *drd;
-       int usb = 0;
-
-       /* Open relevant /sys directory */
-       if ((dir = opendir(SYSFS_USBDEV)) == NULL)
-               return -1;
-
-       /* Get current file entry */
-       while ((drd = readdir(dir)) != NULL) {
-
-               if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
-                       usb++;
-               }
-       }
-
-       /* Close directory */
-       closedir(dir);
-
-       return usb;
-}
-
-/*
- ***************************************************************************
- * Find number of filesystems in /etc/mtab. Pseudo-filesystems are ignored.
- *
- * RETURNS:
- * Number of filesystems.
- ***************************************************************************
- */
-int get_filesystem_nr(void)
-{
-       FILE *fp;
-       char line[256], fs_name[MAX_FS_LEN], mountp[128];
-       int fs = 0;
-       struct statfs buf;
-
-       if ((fp = fopen(MTAB, "r")) == NULL)
-               /* File non-existent */
-               return 0;
-
-       /* Get current filesystem */
-       while (fgets(line, 256, fp) != NULL) {
-               if (line[0] == '/') {
-                       
-                       /* Read filesystem name and mount point */
-                       sscanf(line, "%71s %127s", fs_name, mountp);
-                       
-                       /* Check that total size is not null */
-                       if (statfs(mountp, &buf) < 0)
-                               continue;
-                       
-                       if (buf.f_blocks) {
-                               fs++;
-                       }
-               }
-       }
-
-       fclose(fp);
-
-       return fs;
-}
index d9568191ec7fb70fc1fc9a350031558bc2cbf9c0..cc2984433386ca06c0d3f0bea3e3f6680d6e22ff 100644 (file)
@@ -612,31 +612,4 @@ extern void
 extern void
        read_filesystem(struct stats_filesystem *, int);
 
-/*
- ***************************************************************************
- * Prototypes for functions used to count number of items
- ***************************************************************************
- */
-
-extern int
-       get_irq_nr(void);
-extern int
-       get_serial_nr(void);
-extern int
-       get_iface_nr(void);
-extern int
-       get_diskstats_dev_nr(int, int);
-extern int
-       get_disk_nr(unsigned int);
-extern int
-       get_cpu_nr(unsigned int);
-extern int
-       get_irqcpu_nr(char *, int, int);
-extern int
-       get_freq_nr(void);
-extern int
-       get_usb_nr(void);
-extern int
-       get_filesystem_nr(void);
-
 #endif /* _RD_STATS_H */
index 486ed228fa9e50207882b039512af506b255cb07..ffaccb82e13260e38a93078a5003d772ee377b34 100644 (file)
--- a/sa_wrap.c
+++ b/sa_wrap.c
@@ -21,6 +21,7 @@
 
 #include "sa.h"
 #include "rd_stats.h"
+#include "count.h"
 #include "rd_sensors.h"
 
 extern unsigned int flags;