[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    =?us-ascii?Q?=5BPATCH=5D=20iostat=3A=20another=20code=20shrink=20=7E0=2E5?=
From:       <xmaks () email ! cz>
Date:       2011-05-22 6:36:33
Message-ID: 7878.3026.4706-26982-1868393785-1306046193 () email ! cz
[Download RAW message or body]

Hi Denis,

attached is a patch for the iostat applet, please review and consider usage.

function                                             old     new   delta
.rodata                                           148274  148243     -31
is_partition                                          44       -     -44
iostat_main                                         2064    1928    -136
print_stats_dev_struct                               272       -    -272
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 0/2 up/down: 0/-483)           Total: -483 bytes
   text	   data	    bss	    dec	    hex	filename
 928580	   4122	   9552	 942254	  e60ae	busybox_old
 928001	   4122	   9552	 941675	  e5e6b	busybox_unstripped



Max
["=?us-ascii?Q?busybox=2Eiostat=2Epatch?=" (text/x-patch)]

diff --git a/procps/iostat.c b/procps/iostat.c
index cd233c7..4dd42b7 100644
--- a/procps/iostat.c
+++ b/procps/iostat.c
@@ -24,6 +24,7 @@
 #define debug(fmt, ...) ((void)0)
 
 #define MAX_DEVICE_NAME 12
+#define MAX_DEVICE_NAME_STR "12"
 
 #if 1
 typedef unsigned long long cputime_t;
@@ -64,21 +65,27 @@ typedef struct {
 	cputime_t itv;
 } stats_cpu_pair_t;
 
-struct stats_dev {
-	char dname[MAX_DEVICE_NAME];
+typedef struct {
 	unsigned long long rd_sectors;
 	unsigned long long wr_sectors;
 	unsigned long rd_ops;
 	unsigned long wr_ops;
-};
+} stats_dev_data_t;
+
+typedef struct stats_dev {
+	struct stats_dev *next;
+	char dname[MAX_DEVICE_NAME + 1];
+	stats_dev_data_t prev_data;
+	stats_dev_data_t curr_data;
+} stats_dev_t;
 
 /* Globals. Sort by size and access frequency. */
 struct globals {
 	smallint show_all;
 	unsigned total_cpus;            /* Number of CPUs */
 	unsigned clk_tck;               /* Number of clock ticks per second */
-	llist_t *dev_list;              /* List of devices entered on the command line */
-	struct stats_dev *saved_stats_dev;
+	llist_t *dev_name_list;         /* List of devices entered on the command line */
+	stats_dev_t *stats_dev_list;
 	struct tm tmtime;
 	struct {
 		const char *str;
@@ -244,16 +251,16 @@ static void print_stats_cpu_struct(stats_cpu_pair_t *stats)
 	);
 }
 
-static void print_stats_dev_struct(const struct stats_dev *p,
-		const struct stats_dev *c, cputime_t itv)
+static void print_stats_dev_struct(stats_dev_t *stats_dev, cputime_t itv)
 {
+	stats_dev_data_t *p = &stats_dev->prev_data;
+	stats_dev_data_t *c = &stats_dev->curr_data;
 	if (option_mask32 & OPT_z)
 		if (p->rd_ops == c->rd_ops && p->wr_ops == c->wr_ops)
 			return;
 
-	printf("%-13s %8.2f %12.2f %12.2f %10llu %10llu \n", c->dname,
-		percent_value(p->rd_ops + p->wr_ops ,
-		/**/		  c->rd_ops + c->wr_ops , itv),
+	printf("%-13s %8.2f %12.2f %12.2f %10llu %10llu \n", stats_dev->dname,
+		percent_value(p->rd_ops + p->wr_ops, c->rd_ops + c->wr_ops, itv),
 		percent_value(p->rd_sectors, c->rd_sectors, itv) / G.unit.div,
 		percent_value(p->wr_sectors, c->wr_sectors, itv) / G.unit.div,
 		(c->rd_sectors - p->rd_sectors) / G.unit.div,
@@ -285,61 +292,81 @@ static int is_partition(const char *dev)
 	return ((dev[0] - 's') | (dev[1] - 'd') | (dev[2] - 'a')) == 0 && isdigit(dev[3]);
 }
 
+static stats_dev_t *stats_dev_find_or_new(const char *dev_name)
+{
+	stats_dev_t **curr = &G.stats_dev_list;
+
+	while (*curr != NULL) {
+		if (strcmp((*curr)->dname, dev_name) == 0)
+			return *curr;
+		curr = &(*curr)->next;
+	}
+
+	*curr = xzalloc(sizeof(stats_dev_t));
+	strncpy((*curr)->dname, dev_name, MAX_DEVICE_NAME);
+	return *curr;
+}
+
+static void stats_dev_free(stats_dev_t *stats_dev)
+{
+	if (stats_dev != NULL) {
+		stats_dev_free(stats_dev->next);
+		free(stats_dev);
+	}
+}
+
 static void do_disk_statistics(cputime_t itv)
 {
+	char buf[128];
+	char dev_name[MAX_DEVICE_NAME + 1];
+	unsigned long long rd_sec_or_dummy;
+	unsigned long long wr_sec_or_dummy;
+	stats_dev_data_t *curr_data;
+	stats_dev_t *stats_dev;
 	FILE *fp;
 	int rc;
-	int i = 0;
-	char buf[128];
-	unsigned major, minor;
-	unsigned long wr_ops, dummy; /* %*lu for suppress the conversion wouldn't work */
-	unsigned long long rd_sec_or_wr_ops;
-	unsigned long long rd_sec_or_dummy, wr_sec_or_dummy, wr_sec;
-	struct stats_dev sd;
 
 	fp = xfopen_for_read("/proc/diskstats");
-
 	/* Read and possibly print stats from /proc/diskstats */
 	while (fgets(buf, sizeof(buf), fp)) {
-		rc = sscanf(buf, "%u %u %s %lu %llu %llu %llu %lu %lu %llu %lu %lu %lu %lu",
-			&major, &minor, sd.dname, &sd.rd_ops,
-			&rd_sec_or_dummy, &rd_sec_or_wr_ops, &wr_sec_or_dummy,
-			&wr_ops, &dummy, &wr_sec, &dummy, &dummy, &dummy, &dummy);
-
-		switch (rc) {
-		case 14:
-			sd.wr_ops = wr_ops;
-			sd.rd_sectors = rd_sec_or_wr_ops;
-			sd.wr_sectors = wr_sec;
-			break;
-		case 7:
-			sd.rd_sectors = rd_sec_or_dummy;
-			sd.wr_ops = (unsigned long)rd_sec_or_wr_ops;
-			sd.wr_sectors = wr_sec_or_dummy;
-			break;
-		default:
-			break;
+		sscanf(buf, "%*s %*s %"MAX_DEVICE_NAME_STR"s", dev_name);
+		if (G.dev_name_list) {
+			/* Is device name in list? */
+			if (!llist_find_str(G.dev_name_list, dev_name))
+				continue;
+		} else if (is_partition(dev_name)) {
+			continue;
 		}
 
-		if (!G.dev_list && !is_partition(sd.dname)) {
-			/* User didn't specify device */
-			if (!G.show_all && !sd.rd_ops && !sd.wr_ops) {
-				/* Don't print unused device */
-				continue;
-			}
-			print_stats_dev_struct(&G.saved_stats_dev[i], &sd, itv);
-			G.saved_stats_dev[i] = sd;
-			i++;
-		} else {
-			/* Is device in device list? */
-			if (llist_find_str(G.dev_list, sd.dname)) {
-				/* Print current statistics */
-				print_stats_dev_struct(&G.saved_stats_dev[i], &sd, itv);
-				G.saved_stats_dev[i] = sd;
-				i++;
-			} else
-				continue;
+		stats_dev = stats_dev_find_or_new(dev_name);
+		curr_data = &stats_dev->curr_data;
+
+		rc = sscanf(buf, "%*s %*s %*s %lu %llu %llu %llu %lu %*s %llu",
+			&curr_data->rd_ops,
+			&rd_sec_or_dummy,
+			&curr_data->rd_sectors,
+			&wr_sec_or_dummy,
+			&curr_data->wr_ops,
+			&curr_data->wr_sectors);
+		if (rc != 6) {
+			curr_data->rd_sectors = rd_sec_or_dummy;
+			curr_data->wr_sectors = wr_sec_or_dummy;
+			//curr_data->rd_ops = ;
+			curr_data->wr_ops = (unsigned long)curr_data->rd_sectors;
 		}
+
+		if (!G.dev_name_list /* User didn't specify device */
+		  && !G.show_all
+		  && !curr_data->rd_ops
+		  && !curr_data->wr_ops
+		) {
+			/* Don't print unused device */
+			continue;
+		}
+
+		/* Print current statistics */
+		print_stats_dev_struct(stats_dev, itv);
+		stats_dev->prev_data = *curr_data;
 	}
 
 	fclose(fp);
@@ -354,35 +381,6 @@ static void dev_report(cputime_t itv)
 	do_disk_statistics(itv);
 }
 
-static unsigned get_number_of_devices(void)
-{
-	FILE *fp;
-	char buf[128];
-	int rv;
-	unsigned n = 0;
-	unsigned long rd_ops, wr_ops;
-	char dname[MAX_DEVICE_NAME];
-
-	fp = xfopen_for_read("/proc/diskstats");
-
-	while (fgets(buf, sizeof(buf), fp)) {
-		rv = sscanf(buf, "%*d %*d %s %lu %*u %*u %*u %lu",
-				dname, &rd_ops, &wr_ops);
-		if (rv == 2 || is_partition(dname))
-			/* A partition */
-			continue;
-		if (!rd_ops && !wr_ops) {
-			/* Unused device */
-			if (!G.show_all)
-				continue;
-		}
-		n++;
-	}
-
-	fclose(fp);
-	return n;
-}
-
 //usage:#define iostat_trivial_usage
 //usage:       "[-c] [-d] [-t] [-z] [-k|-m] [ALL|BLOCKDEV...] [INTERVAL [COUNT]]"
 //usage:#define iostat_full_usage "\n\n"
@@ -398,7 +396,7 @@ static unsigned get_number_of_devices(void)
 int iostat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int iostat_main(int argc UNUSED_PARAM, char **argv)
 {
-	int opt, dev_num;
+	int opt;
 	unsigned interval;
 	int count;
 	stats_cpu_t stats_data[2];
@@ -427,14 +425,12 @@ int iostat_main(int argc UNUSED_PARAM, char **argv)
 	argv += optind;
 
 	/* Store device names into device list */
-	dev_num = 0;
 	while (*argv && !isdigit(*argv[0])) {
 		if (strcmp(*argv, "ALL") != 0) {
 			/* If not ALL, save device name */
 			char *dev_name = skip_dev_pfx(*argv);
-			if (!llist_find_str(G.dev_list, dev_name)) {
-				llist_add_to(&G.dev_list, dev_name);
-				dev_num++;
+			if (!llist_find_str(G.dev_name_list, dev_name)) {
+				llist_add_to(&G.dev_name_list, dev_name);
 			}
 		} else {
 			G.show_all = 1;
@@ -454,13 +450,6 @@ int iostat_main(int argc UNUSED_PARAM, char **argv)
 			count = xatoi_positive(*argv);
 	}
 
-	/* Allocate space for device stats */
-	if (opt & OPT_d) {
-		G.saved_stats_dev = xzalloc(sizeof(G.saved_stats_dev[0]) *
-				(dev_num ? dev_num : get_number_of_devices())
-		);
-	}
-
 	if (opt & OPT_m) {
 		G.unit.str = " MB";
 		G.unit.div = 2048;
@@ -530,8 +519,8 @@ int iostat_main(int argc UNUSED_PARAM, char **argv)
 	}
 
 	if (ENABLE_FEATURE_CLEAN_UP) {
-		llist_free(G.dev_list, NULL);
-		free(G.saved_stats_dev);
+		llist_free(G.dev_name_list, NULL);
+		stats_dev_free(G.stats_dev_list);
 		free(&G);
 	}
 


_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic