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

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