From 898bba493d2fe3d7d5a352ffaa6526fe7c084d84 Mon Sep 17 00:00:00 2001
From: Colin Ian King <colin.king@canonical.com>
Date: Thu, 21 Jul 2016 11:57:49 +0100
Subject: [PATCH 06/16] Do not print garbage latency if lat_data_get fails to
 get latency
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

It is possible for lat_data_get to not succeed in reading the latency,
in which case the latency contains a garbage value and this misleading
value is printed.  Instead, print "unknown" if we cannot fetch the latency
to indicate that it could not be read rather than emiting an incorrect
garbage value.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 common/win.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/common/win.c b/common/win.c
index cff5813..21f8737 100644
--- a/common/win.c
+++ b/common/win.c
@@ -2357,7 +2357,7 @@ win_lat_data_show(track_proc_t *proc, dyn_lat_t *dyn, boolean_t *note_out)
 	win_reg_t *r;
 	int lat;
 	track_lwp_t *lwp = NULL;
-	char content[WIN_LINECHAR_MAX], intval_buf[16];
+	char content[WIN_LINECHAR_MAX], intval_buf[16], lat_buf[32];
 
 	*note_out = B_FALSE;
 
@@ -2371,20 +2371,25 @@ win_lat_data_show(track_proc_t *proc, dyn_lat_t *dyn, boolean_t *note_out)
 	}
 
 	dump_cache_enable();
-	(void) lat_data_get(proc, lwp, dyn, &lat);
+	if (lat_data_get(proc, lwp, dyn, &lat) < 0) {
+		strcpy(lat_buf, "unknown");
+	} else {
+		snprintf(lat_buf, sizeof(lat_buf), "%"PRIu64"ns", cyc2ns(lat));
+	}
 	dump_cache_disable();
 
+
 	disp_intval(intval_buf, 16);
 	if (lwp == NULL) {
 		(void) snprintf(content, sizeof (content),
 		    "Monitoring memory areas (pid: %d, "
-		    "AVG.LAT: %"PRIu64"ns, interval: %s)",
-		    proc->pid, cyc2ns(lat), intval_buf);
+		    "AVG.LAT: %s, interval: %s)",
+		    proc->pid, lat_buf, intval_buf);
 	} else {
 		(void) snprintf(content, sizeof (content),
 		    "Monitoring memory areas (lwpid: %d, "
-		    "AVG.LAT: %"PRIu64"ns, interval: %s)",
-		    lwp->id, cyc2ns(lat), intval_buf);
+		    "AVG.LAT: %s, interval: %s)",
+		    lwp->id, lat_buf, intval_buf);
 	}
 
 	r = &dyn->msg;
-- 
2.10.2

