]> granicus.if.org Git - sysstat/commitdiff
Filesystems statistics for sar (part 2): Read statistics
authorseb <sysstat@orange.fr.fake>
Sun, 5 May 2013 15:42:03 +0000 (17:42 +0200)
committerseb <sysstat@orange.fr.fake>
Sun, 5 May 2013 15:42:03 +0000 (17:42 +0200)
This patch reads statistics for mounted filesystems except for
pseudo-filesystems which are ignored.
It also determines the number of filesystems for which stats will be
read.
Oh, and it adds another field to the stats_filesystem structure so that
filesystem name can be saved ;-)

rd_stats.c
rd_stats.h
sa_wrap.c

index dafd9757b0e96d3be81bc4caff0a4f782cff3fd4..b9377f49c469c9f31995a724fe7c46fb8b94dc9b 100644 (file)
@@ -27,6 +27,7 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <unistd.h>
 
 #include "common.h"
@@ -1861,6 +1862,51 @@ void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
        closedir(dir);
 }
 
+/*
+ ***************************************************************************
+ * Read filesystems statistics.
+ *
+ * IN:
+ * @st_filesystem      Structure where stats will be saved.
+ * @nbr                        Total number of filesystems.
+ *
+ * OUT:
+ * @st_filesystem      Structure with statistics.
+ ***************************************************************************
+ */
+void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
+{
+       FILE *fp;
+       char line[256], fs_name[MAX_FS_LEN], mountp[128];
+       int fs = 0;
+       struct stats_filesystem *st_filesystem_i;
+       struct statfs buf;
+
+       if ((fp = fopen(MTAB, "r")) == NULL)
+               return;
+
+       while ((fgets(line, 256, fp) != NULL) && (fs < nbr)) {
+               if (line[0] == '/') {
+                       
+                       /* Read current filesystem name and mount point */
+                       sscanf(line, "%71s %127s", fs_name, mountp);
+                       
+                       if ((statfs(mountp, &buf) < 0) || (!buf.f_blocks))
+                               continue;
+                       
+                       st_filesystem_i = st_filesystem + fs++;
+                       st_filesystem_i->f_blocks = buf.f_blocks * buf.f_bsize;
+                       st_filesystem_i->f_bfree  = buf.f_bfree * buf.f_bsize;
+                       st_filesystem_i->f_bavail = buf.f_bavail * buf.f_bsize;
+                       st_filesystem_i->f_files  = buf.f_files;
+                       st_filesystem_i->f_ffree  = buf.f_ffree;
+                       strcpy(st_filesystem_i->fs_name, fs_name);
+               }
+       }
+
+       fclose(fp);
+}
+
 /*
  ***************************************************************************
  * Read machine uptime, independently of the number of processors.
@@ -2280,3 +2326,44 @@ int get_usb_nr(void)
 
        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 3b9b084f6d5cc9e2bd047896505a241d4c9f84e2..d9568191ec7fb70fc1fc9a350031558bc2cbf9c0 100644 (file)
@@ -27,6 +27,8 @@
 #define MAX_MANUF_LEN  24
 /* Maximum length of USB product string */
 #define MAX_PROD_LEN   48
+/* Maximum length of filesystem name */
+#define MAX_FS_LEN     72
 
 #define CNT_PART       1
 #define CNT_ALL_DEV    0
@@ -522,11 +524,12 @@ struct stats_pwr_usb {
 
 /* Structure for filesystems statistics */
 struct stats_filesystem {
-       unsigned long long f_blocks     __attribute__ ((aligned (16)));
-       unsigned long long f_bfree      __attribute__ ((aligned (16)));
-       unsigned long long f_bavail     __attribute__ ((aligned (16)));
-       unsigned long long f_files      __attribute__ ((aligned (16)));
-       unsigned long long f_ffree      __attribute__ ((aligned (16)));
+       unsigned long long f_blocks             __attribute__ ((aligned (16)));
+       unsigned long long f_bfree              __attribute__ ((aligned (16)));
+       unsigned long long f_bavail             __attribute__ ((aligned (16)));
+       unsigned long long f_files              __attribute__ ((aligned (16)));
+       unsigned long long f_ffree              __attribute__ ((aligned (16)));
+       char               fs_name[MAX_FS_LEN]  __attribute__ ((aligned (16)));
 };
 
 #define STATS_FILESYSTEM_SIZE  (sizeof(struct stats_filesystem))
@@ -606,6 +609,8 @@ extern void
        read_time_in_state(struct stats_pwr_wghfreq *, int, int);
 extern void
        read_bus_usb_dev(struct stats_pwr_usb *, int);
+extern void
+       read_filesystem(struct stats_filesystem *, int);
 
 /*
  ***************************************************************************
@@ -631,5 +636,7 @@ extern int
        get_freq_nr(void);
 extern int
        get_usb_nr(void);
+extern int
+       get_filesystem_nr(void);
 
 #endif /* _RD_STATS_H */
index 298f889ce32371892619bb07516033b45fcac47b..6a829d7e98b384ce557e5b43d39746583c605ad8 100644 (file)
--- a/sa_wrap.c
+++ b/sa_wrap.c
@@ -864,7 +864,7 @@ __read_funct_t wrap_read_filesystem(struct activity *a)
                = (struct stats_filesystem *) a->_buf0;
 
        /* Read filesystems from /etc/mtab */
-       /* FIXME */
+       read_filesystem(st_filesystem, a->nr);
 
        return;
 }
@@ -1088,7 +1088,8 @@ __nr_t wrap_get_filesystem_nr(struct activity *a)
 {
        __nr_t n = 0;
 
-       /* FIXME */
+       if ((n = get_filesystem_nr()) > 0)
+               return n + NR_FILESYSTEM_PREALLOC;
 
        return 0;
 }