From 2689f58f2a0afa296a29ab301a4c1665b914caab Mon Sep 17 00:00:00 2001
From: Zachary T Welch <zw@superlucidity.net>
Date: Sat, 7 Nov 2009 23:20:33 -0800
Subject: Overhaul time support API

This patch changes the duration_* API in several ways.  First, it
updates the API to use better names.  Second, string formatting has
been removed from the API (with its associated malloc).  Finally, a
new function added to convert the time into seconds, which can be
used (or formatted) by the caller.  This eliminates hidden calls to
malloc that require associated calls to free().

This patch also removes the useless extern keyword from prototypes,
and it eliminates the duration_t typedef (use 'struct duration').
These API also allows proper error checking, as it is possible for
gettimeofday to fail in certain circumstances.

The consumers have all been chased to use this new API as well, as
there were relatively few cases doing this type of measurement.
In most cases, the code performs additional checks for errors, but
the calling code looks much cleaner in every case.
---
 src/target/target.c | 83 +++++++++++++++++++----------------------------------
 1 file changed, 30 insertions(+), 53 deletions(-)

(limited to 'src/target')

diff --git a/src/target/target.c b/src/target/target.c
index 5eec09c8..3e4c00d0 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -2507,20 +2507,17 @@ static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cm
 	uint32_t min_address = 0;
 	uint32_t max_address = 0xffffffff;
 	int i;
-	int retvaltemp;
-
 	image_t image;
 
-	duration_t duration;
-	char *duration_text;
-
 	int retval = parse_load_image_command_args(cmd_ctx, args, argc,
 			&image, &min_address, &max_address);
 	if (ERROR_OK != retval)
 		return retval;
 
 	target_t *target = get_current_target(cmd_ctx);
-	duration_start_measure(&duration);
+
+	struct duration bench;
+	duration_start(&bench);
 
 	if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
 	{
@@ -2580,20 +2577,13 @@ static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cm
 		free(buffer);
 	}
 
-	if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+	if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
 	{
-		image_close(&image);
-		return retvaltemp;
+		command_print(cmd_ctx, "downloaded %" PRIu32 " bytes "
+				"in %fs (%0.3f kb/s)", image_size,
+				duration_elapsed(&bench), duration_kbps(&bench, image_size));
 	}
 
-	if (retval == ERROR_OK)
-	{
-		command_print(cmd_ctx, "downloaded %u byte in %s",
-					  (unsigned int)image_size,
-					  duration_text);
-	}
-	free(duration_text);
-
 	image_close(&image);
 
 	return retval;
@@ -2607,8 +2597,6 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm
 	uint8_t buffer[560];
 	int retvaltemp;
 
-	duration_t duration;
-	char *duration_text;
 
 	target_t *target = get_current_target(cmd_ctx);
 
@@ -2628,7 +2616,8 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm
 		return ERROR_OK;
 	}
 
-	duration_start_measure(&duration);
+	struct duration bench;
+	duration_start(&bench);
 
 	int retval = ERROR_OK;
 	while (size > 0)
@@ -2654,14 +2643,11 @@ static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cm
 	if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
 		return retvaltemp;
 
-	if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
-		return retvaltemp;
-
-	if (retval == ERROR_OK)
+	if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
 	{
-		command_print(cmd_ctx, "dumped %lld byte in %s",
-				fileio.size, duration_text);
-		free(duration_text);
+		command_print(cmd_ctx,
+				"dumped %lld bytes in %fs (%0.3f kb/s)", fileio.size,
+				duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
 	}
 
 	return retval;
@@ -2673,15 +2659,12 @@ static int handle_verify_image_command_internal(struct command_context_s *cmd_ct
 	uint32_t buf_cnt;
 	uint32_t image_size;
 	int i;
-	int retval, retvaltemp;
+	int retval;
 	uint32_t checksum = 0;
 	uint32_t mem_checksum = 0;
 
 	image_t image;
 
-	duration_t duration;
-	char *duration_text;
-
 	target_t *target = get_current_target(cmd_ctx);
 
 	if (argc < 1)
@@ -2695,7 +2678,8 @@ static int handle_verify_image_command_internal(struct command_context_s *cmd_ct
 		return ERROR_FAIL;
 	}
 
-	duration_start_measure(&duration);
+	struct duration bench;
+	duration_start(&bench);
 
 	if (argc >= 2)
 	{
@@ -2802,20 +2786,12 @@ static int handle_verify_image_command_internal(struct command_context_s *cmd_ct
 		image_size += buf_cnt;
 	}
 done:
-
-	if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
-	{
-		image_close(&image);
-		return retvaltemp;
-	}
-
-	if (retval == ERROR_OK)
+	if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
 	{
-		command_print(cmd_ctx, "verified %u bytes in %s",
-					  (unsigned int)image_size,
-					  duration_text);
+		command_print(cmd_ctx, "verified %" PRIu32 " bytes "
+				"in %fs (%0.3f kb/s)", image_size,
+				duration_elapsed(&bench), duration_kbps(&bench, image_size));
 	}
-	free(duration_text);
 
 	image_close(&image);
 
@@ -4672,15 +4648,13 @@ static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, cha
 
 	image_t image;
 
-	duration_t duration;
-	char *duration_text;
-
 	int retval = parse_load_image_command_args(cmd_ctx, args, argc,
 			&image, &min_address, &max_address);
 	if (ERROR_OK != retval)
 		return retval;
 
-	duration_start_measure(&duration);
+	struct duration bench;
+	duration_start(&bench);
 
 	if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
 	{
@@ -4753,13 +4727,16 @@ static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, cha
 		free(buffer);
 	}
 
-	duration_stop_measure(&duration, &duration_text);
-	if (retval == ERROR_OK)
+	if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
 	{
-		command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
-		command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
+		command_print(cmd_ctx, "Loaded %" PRIu32 " bytes "
+				"in %fs (%0.3f kb/s)", image_size, 
+				duration_elapsed(&bench), duration_kbps(&bench, image_size));
+
+		command_print(cmd_ctx,
+				"WARNING: image has not been loaded to target!"
+				"You can issue a 'fast_load' to finish loading.");
 	}
-	free(duration_text);
 
 	image_close(&image);
 
-- 
cgit v1.2.3