[sheepdog] [PATCH v2 2/4] dog: size_to_str -> strnumber

Liu Yuan namei.unix at gmail.com
Mon Aug 26 08:51:54 CEST 2013


And make strnumber() take only one parameter instead of lengthy 3.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/common.c |   43 +++++++++++++++++++++++++------------------
 dog/dog.h    |    3 ++-
 dog/node.c   |   36 +++++++++++++-----------------------
 dog/vdi.c    |   40 ++++++++++++++++------------------------
 4 files changed, 56 insertions(+), 66 deletions(-)

diff --git a/dog/common.c b/dog/common.c
index b185d4e..062eedd 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -13,15 +13,25 @@
 #include "sha1.h"
 #include "sockfd_cache.h"
 
-char *size_to_str(uint64_t _size, char *str, int str_size)
+char *strnumber(uint64_t size)
+{
+	return strnumber_raw(size, raw_output);
+}
+
+char *strnumber_raw(uint64_t _size, bool raw)
 {
 	const char *units[] = {"MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
+	static struct size_str {
+		char str[UINT64_DECIMAL_SIZE];
+	} s[1024]; /* Is this big enough ? */
+	static int j;
 	int i = 0;
 	double size;
+	char *ret;
 
-	if (raw_output) {
-		snprintf(str, str_size, "%" PRIu64, _size);
-		return str;
+	if (raw) {
+		snprintf(s[j].str, UINT64_DECIMAL_SIZE, "%" PRIu64, _size);
+		goto out;
 	}
 
 	size = (double)_size;
@@ -32,11 +42,16 @@ char *size_to_str(uint64_t _size, char *str, int str_size)
 	}
 
 	if (size >= 10)
-		snprintf(str, str_size, "%.0lf %s", size, units[i]);
+		snprintf(s[j].str, UINT64_DECIMAL_SIZE, "%.0lf %s",
+			 size, units[i]);
 	else
-		snprintf(str, str_size, "%.1lf %s", size, units[i]);
-
-	return str;
+		snprintf(s[j].str, UINT64_DECIMAL_SIZE, "%.1lf %s",
+			 size, units[i]);
+out:
+	ret = s[j++].str;
+	if (j == 1024)
+		j = 0;
+	return ret;
 }
 
 int sd_read_object(uint64_t oid, void *data, unsigned int datalen,
@@ -281,7 +296,6 @@ static int get_screen_width(void)
  */
 void show_progress(uint64_t done, uint64_t total, bool raw)
 {
-	char done_str[256], total_str[256];
 	int screen_width = get_screen_width();
 	int bar_length = screen_width - 30;
 	char *buf;
@@ -293,14 +307,6 @@ void show_progress(uint64_t done, uint64_t total, bool raw)
 
 	printf("\r"); /* move to the beginning of the line */
 
-	if (raw) {
-		snprintf(done_str, sizeof(done_str), "%"PRIu64, done);
-		snprintf(total_str, sizeof(total_str), "%"PRIu64, total);
-	} else {
-		size_to_str(done, done_str, sizeof(done_str));
-		size_to_str(total, total_str, sizeof(total_str));
-	}
-
 	buf = xmalloc(screen_width + 1);
 	snprintf(buf, screen_width, "%5.1lf %% [", (double)done / total * 100);
 
@@ -314,7 +320,8 @@ void show_progress(uint64_t done, uint64_t total, bool raw)
 			strcat(buf, " ");
 	}
 	snprintf(buf + strlen(buf), screen_width - strlen(buf),
-		 "] %s / %s", done_str, total_str);
+		 "] %s / %s", strnumber_raw(done, raw),
+		 strnumber_raw(total, raw));
 
 	/* fill the rest of buffer with blank characters */
 	memset(buf + strlen(buf), ' ', screen_width - strlen(buf));
diff --git a/dog/dog.h b/dog/dog.h
index 897cf92..27e10c7 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -61,7 +61,8 @@ extern struct sd_vnode sd_vnodes[SD_MAX_VNODES];
 extern int sd_nodes_nr, sd_vnodes_nr;
 
 bool is_current(const struct sd_inode *i);
-char *size_to_str(uint64_t _size, char *str, int str_size);
+char *strnumber(uint64_t _size);
+char *strnumber_raw(uint64_t _size, bool raw);
 typedef void (*vdi_parser_func_t)(uint32_t vid, const char *name,
 				  const char *tag, uint32_t snapid,
 				  uint32_t flags,
diff --git a/dog/node.c b/dog/node.c
index f6fe28b..cb4725d 100644
--- a/dog/node.c
+++ b/dog/node.c
@@ -47,8 +47,6 @@ static int node_info(int argc, char **argv)
 {
 	int i, ret, success = 0;
 	uint64_t total_size = 0, total_avail = 0, total_vdi_size = 0;
-	char total_str[UINT64_DECIMAL_SIZE], use_str[UINT64_DECIMAL_SIZE],
-	     avail_str[UINT64_DECIMAL_SIZE], vdi_size_str[UINT64_DECIMAL_SIZE];
 
 	if (!raw_output)
 		printf("Id\tSize\tUsed\tAvail\tUse%%\n");
@@ -56,26 +54,23 @@ static int node_info(int argc, char **argv)
 	for (i = 0; i < sd_nodes_nr; i++) {
 		struct sd_req req;
 		struct sd_rsp *rsp = (struct sd_rsp *)&req;
-		char store_str[UINT64_DECIMAL_SIZE],
-		     used_str[UINT64_DECIMAL_SIZE],
-		     free_str[UINT64_DECIMAL_SIZE];
 
 		sd_init_req(&req, SD_OP_STAT_SHEEP);
 
 		ret = send_light_req(&req, sd_nodes[i].nid.addr,
 				     sd_nodes[i].nid.port);
 
-		size_to_str(rsp->node.store_size, store_str, sizeof(store_str));
-		size_to_str(rsp->node.store_free, free_str, sizeof(free_str));
-		size_to_str(rsp->node.store_size - rsp->node.store_free,
-			    used_str, sizeof(used_str));
 		if (!ret) {
 			int ratio = (int)(((double)(rsp->node.store_size -
 						    rsp->node.store_free) /
 					   rsp->node.store_size) * 100);
 			printf(raw_output ? "%d %s %s %s %d%%\n" :
 					"%2d\t%s\t%s\t%s\t%3d%%\n",
-			       i, store_str, used_str, free_str,
+			       i,
+			       strnumber(rsp->node.store_size),
+			       strnumber(rsp->node.store_size -
+					   rsp->node.store_free),
+			       strnumber(rsp->node.store_free),
 			       rsp->node.store_size == 0 ? 0 : ratio);
 			success++;
 		}
@@ -93,16 +88,14 @@ static int node_info(int argc, char **argv)
 			&total_vdi_size) < 0)
 		return EXIT_SYSFAIL;
 
-	size_to_str(total_size, total_str, sizeof(total_str));
-	size_to_str(total_avail, avail_str, sizeof(avail_str));
-	size_to_str(total_size - total_avail, use_str, sizeof(use_str));
-	size_to_str(total_vdi_size, vdi_size_str, sizeof(vdi_size_str));
 	printf(raw_output ? "Total %s %s %s %d%% %s\n"
 			  : "Total\t%s\t%s\t%s\t%3d%%\n\n"
 			  "Total virtual image size\t%s\n",
-	       total_str, use_str, avail_str,
+	       strnumber(total_size),
+	       strnumber(total_size - total_avail),
+	       strnumber(total_avail),
 	       (int)(((double)(total_size - total_avail) / total_size) * 100),
-	       vdi_size_str);
+	       strnumber(total_vdi_size));
 
 	return EXIT_SUCCESS;
 }
@@ -272,8 +265,6 @@ static int node_kill(int argc, char **argv)
 static int node_md_info(struct node_id *nid)
 {
 	struct sd_md_info info = {};
-	char size_str[UINT64_DECIMAL_SIZE], used_str[UINT64_DECIMAL_SIZE],
-	     avail_str[UINT64_DECIMAL_SIZE];
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	int ret, i;
@@ -295,12 +286,11 @@ static int node_md_info(struct node_id *nid)
 		uint64_t size = info.disk[i].free + info.disk[i].used;
 		int ratio = (int)(((double)info.disk[i].used / size) * 100);
 
-		size_to_str(size, size_str, sizeof(size_str));
-		size_to_str(info.disk[i].used, used_str, sizeof(used_str));
-		size_to_str(info.disk[i].free, avail_str, sizeof(avail_str));
 		fprintf(stdout, "%2d\t%s\t%s\t%s\t%3d%%\t%s\n",
-			info.disk[i].idx, size_str, used_str, avail_str, ratio,
-			info.disk[i].path);
+			info.disk[i].idx, strnumber(size),
+			strnumber(info.disk[i].used),
+			strnumber(info.disk[i].free),
+			ratio, info.disk[i].path);
 	}
 	return EXIT_SUCCESS;
 }
diff --git a/dog/vdi.c b/dog/vdi.c
index 30f686b..b0c2ea5 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -67,7 +67,6 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 	int idx;
 	bool is_clone = false;
 	uint64_t my_objs, cow_objs;
-	char vdi_size_str[16], my_objs_str[16], cow_objs_str[16];
 	time_t ti;
 	struct tm tm;
 	char dbuf[128];
@@ -96,10 +95,6 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 			cow_objs++;
 	}
 
-	size_to_str(i->vdi_size, vdi_size_str, sizeof(vdi_size_str));
-	size_to_str(my_objs * SD_DATA_OBJ_SIZE, my_objs_str, sizeof(my_objs_str));
-	size_to_str(cow_objs * SD_DATA_OBJ_SIZE, cow_objs_str, sizeof(cow_objs_str));
-
 	if (i->snap_id == 1 && i->parent_vdi_id != 0)
 		is_clone = true;
 
@@ -111,13 +106,18 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 			putchar(*name++);
 		}
 		printf(" %d %s %s %s %s %" PRIx32 " %d %s\n", snapid,
-				vdi_size_str, my_objs_str, cow_objs_str, dbuf, vid,
-				i->nr_copies, i->tag);
+		       strnumber(i->vdi_size),
+		       strnumber(my_objs * SD_DATA_OBJ_SIZE),
+		       strnumber(cow_objs * SD_DATA_OBJ_SIZE),
+		       dbuf, vid, i->nr_copies, i->tag);
 	} else {
 		printf("%c %-8s %5d %7s %7s %7s %s  %7" PRIx32 " %5d %13s\n",
-				vdi_is_snapshot(i) ? 's' : (is_clone ? 'c' : ' '),
-				name, snapid, vdi_size_str, my_objs_str, cow_objs_str,
-				dbuf, vid, i->nr_copies, i->tag);
+		       vdi_is_snapshot(i) ? 's' : (is_clone ? 'c' : ' '),
+		       name, snapid,
+		       strnumber(i->vdi_size),
+		       strnumber(my_objs * SD_DATA_OBJ_SIZE),
+		       strnumber(cow_objs * SD_DATA_OBJ_SIZE),
+		       dbuf, vid, i->nr_copies, i->tag);
 	}
 }
 
@@ -148,14 +148,13 @@ static void print_vdi_graph(uint32_t vid, const char *name, const char *tag,
 {
 	time_t ti;
 	struct tm tm;
-	char dbuf[128], tbuf[128], size_str[128];
+	char dbuf[128], tbuf[128];
 
 	ti = i->create_time >> 32;
 	localtime_r(&ti, &tm);
 
 	strftime(dbuf, sizeof(dbuf), "%Y-%m-%d", &tm);
 	strftime(tbuf, sizeof(tbuf), "%H:%M:%S", &tm);
-	size_to_str(i->vdi_size, size_str, sizeof(size_str));
 
 	printf("  \"%x\" -> \"%x\";\n", i->parent_vdi_id, vid);
 	printf("  \"%x\" [\n"
@@ -167,7 +166,7 @@ static void print_vdi_graph(uint32_t vid, const char *name, const char *tag,
 	       "Size: %10s\\n"
 	       "Date: %10s\\n"
 	       "Time: %10s",
-	       name, snapid, size_str, dbuf, tbuf);
+	       name, snapid, strnumber(i->vdi_size), dbuf, tbuf);
 
 	if (vdi_is_snapshot(i))
 		printf("\"\n  ];\n\n");
@@ -1957,7 +1956,6 @@ static int vdi_cache_info(int argc, char **argv)
 	struct object_cache_info info = {};
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-	char size_str[UINT64_DECIMAL_SIZE], used_str[UINT64_DECIMAL_SIZE];
 	int ret, i;
 
 	sd_init_req(&hdr, SD_OP_GET_CACHE_INFO);
@@ -1974,27 +1972,21 @@ static int vdi_cache_info(int argc, char **argv)
 
 	fprintf(stdout, "Name\tTag\tTotal\tDirty\tClean\n");
 	for (i = 0; i < info.count; i++) {
-		char total_str[UINT64_DECIMAL_SIZE],
-		     dirty_str[UINT64_DECIMAL_SIZE],
-		     clean_str[UINT64_DECIMAL_SIZE];
 		uint64_t total = info.caches[i].total * SD_DATA_OBJ_SIZE,
 			 dirty = info.caches[i].dirty * SD_DATA_OBJ_SIZE,
 			 clean = total - dirty;
 		char name[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
 
-		size_to_str(total, total_str, sizeof(total_str));
-		size_to_str(dirty, dirty_str, sizeof(dirty_str));
-		size_to_str(clean, clean_str, sizeof(clean_str));
 		ret = vid_to_name_tag(info.caches[i].vid, name, tag);
 		if (ret != SD_RES_SUCCESS)
 			return EXIT_FAILURE;
 		fprintf(stdout, "%s\t%s\t%s\t%s\t%s\n",
-			name, tag, total_str, dirty_str, clean_str);
+			name, tag, strnumber(total), strnumber(dirty),
+			strnumber(clean));
 	}
 
-	size_to_str(info.size, size_str, sizeof(size_str));
-	size_to_str(info.used, used_str, sizeof(used_str));
-	fprintf(stdout, "\nCache size %s, used %s, %s\n", size_str, used_str,
+	fprintf(stdout, "\nCache size %s, used %s, %s\n",
+		strnumber(info.size), strnumber(info.used),
 		info.directio ? "directio" : "non-directio");
 
 	return EXIT_SUCCESS;
-- 
1.7.9.5




More information about the sheepdog mailing list