[sheepdog] [PATCH 1/2] dog: add elapsed time to measure the program is fast or not

Ruoyu liangry at ucweb.com
Mon Jul 7 14:42:52 CEST 2014


As title.

Signed-off-by: Ruoyu <liangry at ucweb.com>
---
 dog/dog.c      |  7 +++++++
 include/util.h |  4 ++++
 lib/util.c     | 25 +++++++++++++++++++++++++
 3 files changed, 36 insertions(+)

diff --git a/dog/dog.c b/dog/dog.c
index 7d1153d..46992ec 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -453,6 +453,9 @@ int main(int argc, char **argv)
 	const struct sd_option *sd_opts;
 	uint8_t sdhost[16];
 	int sdport;
+	struct timespec start, end;
+
+	start = get_time_tick();
 
 	log_dog_operation(argc, argv);
 
@@ -565,5 +568,9 @@ int main(int argc, char **argv)
 	ret = command_fn(argc, argv);
 	if (ret == EXIT_USAGE)
 		subcommand_usage(argv[1], argv[2], EXIT_USAGE);
+
+	end = get_time_tick();
+	printf("\nElapsed time: %.3lf seconds\n",
+			get_time_interval(&start, &end));
 	return ret;
 }
diff --git a/include/util.h b/include/util.h
index c7a1921..d230f7f 100644
--- a/include/util.h
+++ b/include/util.h
@@ -125,6 +125,10 @@ int atomic_create_and_write(const char *path, const char *buf, size_t len,
 void find_zero_blocks(const void *buf, uint64_t *poffset, uint32_t *plen);
 void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen);
 
+struct timespec get_time_tick(void);
+double get_time_interval(const struct timespec *start,
+						 const struct timespec *end);
+
 /* a type safe version of qsort() */
 #define xqsort(base, nmemb, compar)					\
 ({									\
diff --git a/lib/util.c b/lib/util.c
index 408fc19..9a3adcd 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -806,3 +806,28 @@ void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen)
 	if (orig_offset < *poffset)
 		memmove(p, p + *poffset - orig_offset, *plen);
 }
+
+struct timespec get_time_tick(void)
+{
+	struct timespec ts;
+	int ret;
+
+	ts.tv_sec = 0;
+	ts.tv_nsec = 0;
+
+	ret = clock_gettime(CLOCK_MONOTONIC, &ts);
+	if (ret < 0)
+		sd_err("clock_gettime failure: %m");
+
+	return ts;
+}
+
+double get_time_interval(const struct timespec *start,
+						 const struct timespec *end)
+{
+	assert(start);
+	assert(end);
+
+	return ((end->tv_nsec - start->tv_nsec) * 0.000000001)
+			+ end->tv_sec - start->tv_sec;
+}
-- 
1.8.3.2





More information about the sheepdog mailing list