Skip to contentSkip to conversation

usemem: Add option init-time

PATCH New Hui Zhu 1 participant 1 message Dec 16, 2020

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH 1/1 usemem: Add option init-time

From: Hui Zhu <[email protected]>

This commit add a new option init-time to remove the initialization time from the run time and show the initialization time.

---
 usemem.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
usemem.c +27 -2
index 823647e..6d1d575 100644
@@ -96,6 +96,7 @@ int opt_bind_interval = 0;
 unsigned long opt_delay = 0;
 int opt_read_again = 0;
 int opt_punch_holes = 0;
+int opt_init_time = 0;
 int nr_task;
 int nr_thread;
 int nr_cpu;
@@ -155,6 +156,7 @@ void usage(int ok)
 	"    -U|--hugetlb        allocate hugetlbfs page\n"
 	"    -Z|--read-again     read memory again after access the memory\n"
 	"    --punch-holes       free every other page after allocation\n"
+	"    --init-time         remove the initialization time from the run time and show the initialization time\n"
 	"    -h|--help           show this message\n"
 	,		ourname);
 
@@ -193,7 +195,8 @@ static const struct option opts[] = {
 	{ "delay"	, 1, NULL, 'e' },
 	{ "hugetlb"	, 0, NULL, 'U' },
 	{ "read-again"	, 0, NULL, 'Z' },
-	{ "punch-holes" , 0, NULL,   0 },
+	{ "punch-holes"	, 0, NULL,   0 },
+	{ "init-time"	, 0, NULL,   0 },
 	{ "help"	, 0, NULL, 'h' },
 	{ NULL		, 0, NULL, 0 }
 };
@@ -945,6 +948,8 @@ int main(int argc, char *argv[])
 		case 0:
 			if (strcmp(opts[opt_index].name, "punch-holes") == 0) {
 				opt_punch_holes = 1;
+			} else if (strcmp(opts[opt_index].name, "init-time") == 0) { 
+				opt_init_time = 1;
 			} else
 				usage(1);
 			break;
@@ -1128,7 +1133,7 @@ int main(int argc, char *argv[])
 	if (optind != argc - 1)
 		usage(0);
 
-	if (!opt_write_signal_read)
+	if (!opt_write_signal_read || opt_init_time)
 		gettimeofday(&start_time, NULL);
 
 	opt_bytes = memparse(argv[optind], NULL);
@@ -1263,5 +1268,25 @@ int main(int argc, char *argv[])
 	if (!nr_task)
 		nr_task = 1;
 
+	if (opt_init_time) {
+		struct timeval stop;
+		char buf[1024];
+		size_t len;
+		unsigned long delta_us;
+
+		gettimeofday(&stop, NULL);
+		delta_us = (stop.tv_sec - start_time.tv_sec) * 1000000 +
+			(stop.tv_usec - start_time.tv_usec);
+		len = snprintf(buf, sizeof(buf),
+			"the initialization time is %lu secs %lu usecs\n",
+			delta_us / 1000000, delta_us % 1000000);
+		fflush(stdout);
+		if (write(1, buf, len) != len)
+			fprintf(stderr, "WARNING: statistics output may be incomplete.\n");
+
+		if (!opt_write_signal_read)
+			gettimeofday(&start_time, NULL);
+	}
+
 	return do_tasks();
 }
Signature · 1 line
2.17.1