From: seb Date: Sun, 5 May 2013 15:42:03 +0000 (+0200) Subject: Filesystems statistics for sar (part 2): Read statistics X-Git-Tag: v10.1.6~17 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=512bf2e7ebfa5e5fec757d96820b0b3f94341797;p=sysstat Filesystems statistics for sar (part 2): Read statistics 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 ;-) --- diff --git a/rd_stats.c b/rd_stats.c index dafd975..b9377f4 100644 --- a/rd_stats.c +++ b/rd_stats.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #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; +} diff --git a/rd_stats.h b/rd_stats.h index 3b9b084..d956819 100644 --- a/rd_stats.h +++ b/rd_stats.h @@ -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 */ diff --git a/sa_wrap.c b/sa_wrap.c index 298f889..6a829d7 100644 --- 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; }