Blame SOURCES/0002-Filesystems-statistics-for-sar-part-2-Read-statistic.patch

72abff
From cea93dc1db568312987814b3cae0a6f837cfaa4e Mon Sep 17 00:00:00 2001
72abff
From: seb <sysstat@orange.fr.fake>
72abff
Date: Sun, 5 May 2013 17:42:03 +0200
72abff
Subject: [PATCH] Filesystems statistics for sar (part 2): Read statistics
72abff
72abff
This patch reads statistics for mounted filesystems except for
72abff
pseudo-filesystems which are ignored.
72abff
It also determines the number of filesystems for which stats will be
72abff
read.
72abff
Oh, and it adds another field to the stats_filesystem structure so that
72abff
filesystem name can be saved ;-)
72abff
72abff
(cherry picked from commit 512bf2e7ebfa5e5fec757d96820b0b3f94341797)
72abff
---
72abff
 rd_stats.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72abff
 rd_stats.h | 17 ++++++++----
72abff
 sa_wrap.c  |  5 ++--
72abff
 3 files changed, 102 insertions(+), 7 deletions(-)
72abff
72abff
diff --git a/rd_stats.c b/rd_stats.c
72abff
index d8c337a..829dae5 100644
72abff
--- a/rd_stats.c
72abff
+++ b/rd_stats.c
72abff
@@ -27,6 +27,7 @@
72abff
 #include <ctype.h>
72abff
 #include <sys/types.h>
72abff
 #include <sys/stat.h>
72abff
+#include <sys/vfs.h>
72abff
 #include <unistd.h>
72abff
 
72abff
 #include "common.h"
72abff
@@ -1863,6 +1864,51 @@ void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
72abff
 	closedir(dir);
72abff
 }
72abff
 
72abff
+/*
72abff
+ ***************************************************************************
72abff
+ * Read filesystems statistics.
72abff
+ *
72abff
+ * IN:
72abff
+ * @st_filesystem	Structure where stats will be saved.
72abff
+ * @nbr			Total number of filesystems.
72abff
+ *
72abff
+ * OUT:
72abff
+ * @st_filesystem	Structure with statistics.
72abff
+ ***************************************************************************
72abff
+ */
72abff
+void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
72abff
+{
72abff
+	FILE *fp;
72abff
+	char line[256], fs_name[MAX_FS_LEN], mountp[128];
72abff
+	int fs = 0;
72abff
+	struct stats_filesystem *st_filesystem_i;
72abff
+	struct statfs buf;
72abff
+
72abff
+	if ((fp = fopen(MTAB, "r")) == NULL)
72abff
+		return;
72abff
+
72abff
+	while ((fgets(line, 256, fp) != NULL) && (fs < nbr)) {
72abff
+		if (line[0] == '/') {
72abff
+			
72abff
+			/* Read current filesystem name and mount point */
72abff
+			sscanf(line, "%71s %127s", fs_name, mountp);
72abff
+			
72abff
+			if ((statfs(mountp, &buf) < 0) || (!buf.f_blocks))
72abff
+				continue;
72abff
+			
72abff
+			st_filesystem_i = st_filesystem + fs++;
72abff
+			st_filesystem_i->f_blocks = buf.f_blocks * buf.f_bsize;
72abff
+			st_filesystem_i->f_bfree  = buf.f_bfree * buf.f_bsize;
72abff
+			st_filesystem_i->f_bavail = buf.f_bavail * buf.f_bsize;
72abff
+			st_filesystem_i->f_files  = buf.f_files;
72abff
+			st_filesystem_i->f_ffree  = buf.f_ffree;
72abff
+			strcpy(st_filesystem_i->fs_name, fs_name);
72abff
+		}
72abff
+	}
72abff
+
72abff
+	fclose(fp);
72abff
+}
72abff
+
72abff
 /*
72abff
  ***************************************************************************
72abff
  * Read machine uptime, independently of the number of processors.
72abff
@@ -2282,3 +2328,44 @@ int get_usb_nr(void)
72abff
 
72abff
 	return usb;
72abff
 }
72abff
+
72abff
+/*
72abff
+ ***************************************************************************
72abff
+ * Find number of filesystems in /etc/mtab. Pseudo-filesystems are ignored.
72abff
+ *
72abff
+ * RETURNS:
72abff
+ * Number of filesystems.
72abff
+ ***************************************************************************
72abff
+ */
72abff
+int get_filesystem_nr(void)
72abff
+{
72abff
+	FILE *fp;
72abff
+	char line[256], fs_name[MAX_FS_LEN], mountp[128];
72abff
+	int fs = 0;
72abff
+	struct statfs buf;
72abff
+
72abff
+	if ((fp = fopen(MTAB, "r")) == NULL)
72abff
+		/* File non-existent */
72abff
+		return 0;
72abff
+
72abff
+	/* Get current filesystem */
72abff
+	while (fgets(line, 256, fp) != NULL) {
72abff
+		if (line[0] == '/') {
72abff
+			
72abff
+			/* Read filesystem name and mount point */
72abff
+			sscanf(line, "%71s %127s", fs_name, mountp);
72abff
+			
72abff
+			/* Check that total size is not null */
72abff
+			if (statfs(mountp, &buf) < 0)
72abff
+				continue;
72abff
+			
72abff
+			if (buf.f_blocks) {
72abff
+				fs++;
72abff
+			}
72abff
+		}
72abff
+	}
72abff
+
72abff
+	fclose(fp);
72abff
+
72abff
+	return fs;
72abff
+}
72abff
diff --git a/rd_stats.h b/rd_stats.h
72abff
index f941426..3f1edde 100644
72abff
--- a/rd_stats.h
72abff
+++ b/rd_stats.h
72abff
@@ -27,6 +27,8 @@
72abff
 #define MAX_MANUF_LEN	24
72abff
 /* Maximum length of USB product string */
72abff
 #define MAX_PROD_LEN	48
72abff
+/* Maximum length of filesystem name */
72abff
+#define MAX_FS_LEN	72
72abff
 
72abff
 #define CNT_DEV		0
72abff
 #define CNT_PART	1
72abff
@@ -523,11 +525,12 @@ struct stats_pwr_usb {
72abff
 
72abff
 /* Structure for filesystems statistics */
72abff
 struct stats_filesystem {
72abff
-	unsigned long long f_blocks	__attribute__ ((aligned (16)));
72abff
-	unsigned long long f_bfree	__attribute__ ((aligned (16)));
72abff
-	unsigned long long f_bavail	__attribute__ ((aligned (16)));
72abff
-	unsigned long long f_files	__attribute__ ((aligned (16)));
72abff
-	unsigned long long f_ffree	__attribute__ ((aligned (16)));
72abff
+	unsigned long long f_blocks		__attribute__ ((aligned (16)));
72abff
+	unsigned long long f_bfree		__attribute__ ((aligned (16)));
72abff
+	unsigned long long f_bavail		__attribute__ ((aligned (16)));
72abff
+	unsigned long long f_files		__attribute__ ((aligned (16)));
72abff
+	unsigned long long f_ffree		__attribute__ ((aligned (16)));
72abff
+	char 		   fs_name[MAX_FS_LEN]	__attribute__ ((aligned (16)));
72abff
 };
72abff
 
72abff
 #define STATS_FILESYSTEM_SIZE	(sizeof(struct stats_filesystem))
72abff
@@ -607,6 +610,8 @@ extern void
72abff
 	read_time_in_state(struct stats_pwr_wghfreq *, int, int);
72abff
 extern void
72abff
 	read_bus_usb_dev(struct stats_pwr_usb *, int);
72abff
+extern void
72abff
+	read_filesystem(struct stats_filesystem *, int);
72abff
 
72abff
 /*
72abff
  ***************************************************************************
72abff
@@ -632,5 +637,7 @@ extern int
72abff
 	get_freq_nr(void);
72abff
 extern int
72abff
 	get_usb_nr(void);
72abff
+extern int
72abff
+	get_filesystem_nr(void);
72abff
 
72abff
 #endif /* _RD_STATS_H */
72abff
diff --git a/sa_wrap.c b/sa_wrap.c
72abff
index 298f889..6a829d7 100644
72abff
--- a/sa_wrap.c
72abff
+++ b/sa_wrap.c
72abff
@@ -864,7 +864,7 @@ __read_funct_t wrap_read_filesystem(struct activity *a)
72abff
 		= (struct stats_filesystem *) a->_buf0;
72abff
 
72abff
 	/* Read filesystems from /etc/mtab */
72abff
-	/* FIXME */
72abff
+	read_filesystem(st_filesystem, a->nr);
72abff
 
72abff
 	return;
72abff
 }
72abff
@@ -1088,7 +1088,8 @@ __nr_t wrap_get_filesystem_nr(struct activity *a)
72abff
 {
72abff
 	__nr_t n = 0;
72abff
 
72abff
-	/* FIXME */
72abff
+	if ((n = get_filesystem_nr()) > 0)
72abff
+		return n + NR_FILESYSTEM_PREALLOC;
72abff
 
72abff
 	return 0;
72abff
 }
72abff
-- 
72abff
2.14.3
72abff