Blame SOURCES/time-1.7-Recompute-CPU-usage-at-microsecond-level.patch

136502
From 0d743a7d946fe176a07baf2586a6af0e867fd89c Mon Sep 17 00:00:00 2001
136502
From: H.J. Lu <hongjiu.lu@intel.com>
136502
Date: Wed, 11 May 2011 16:19:55 +0200
136502
Subject: [PATCH] Recompute CPU usage at microsecond level
136502
136502
If job finishes quickly, miliseconds arithmetic rounds to zero. If
136502
that happens, recalculate CPU usage ratio with microsecond accuracy
136502
to raise chance to get non-zero values.
136502
---
136502
 resuse.h |    2 ++
136502
 time.c   |   15 +++++++++++++++
136502
 2 files changed, 17 insertions(+), 0 deletions(-)
136502
136502
diff --git a/resuse.h b/resuse.h
136502
index 992143f..7a3ee66 100644
136502
--- a/resuse.h
136502
+++ b/resuse.h
136502
@@ -33,9 +33,11 @@ struct timeval
136502
 #if HAVE_SYS_RUSAGE_H
136502
 /* This rusage structure measures nanoseconds instead of microseconds.  */
136502
 # define TV_MSEC tv_nsec / 1000000
136502
+# define TV_USEC tv_nsec / 1000
136502
 # include <sys/rusage.h>
136502
 #else
136502
 # define TV_MSEC tv_usec / 1000
136502
+# define TV_USEC tv_usec
136502
 # if HAVE_WAIT3
136502
 #  include <sys/resource.h>
136502
 # else
136502
diff --git a/time.c b/time.c
136502
index 43aec0b..96cfdde 100644
136502
--- a/time.c
136502
+++ b/time.c
136502
@@ -326,6 +326,8 @@ summarize (fp, fmt, command, resp)
136502
 {
136502
   unsigned long r;		/* Elapsed real milliseconds.  */
136502
   unsigned long v;		/* Elapsed virtual (CPU) milliseconds.  */
136502
+  unsigned long ru;		/* Elapsed real microseconds.  */
136502
+  unsigned long vu;		/* Elapsed virtual (CPU) microseconds.  */
136502
 
136502
   if (verbose)
136502
   {
136502
@@ -350,6 +352,17 @@ summarize (fp, fmt, command, resp)
136502
   v = resp->ru.ru_utime.tv_sec * 1000 + resp->ru.ru_utime.TV_MSEC +
136502
     resp->ru.ru_stime.tv_sec * 1000 + resp->ru.ru_stime.TV_MSEC;
136502
 
136502
+  if (r == 0 && v == 0)
136502
+    {
136502
+      ru = resp->elapsed.tv_usec;
136502
+      vu = resp->ru.ru_utime.TV_USEC + resp->ru.ru_stime.TV_USEC;
136502
+    }
136502
+  else
136502
+    {
136502
+      ru = 0;
136502
+      vu = 0;
136502
+    }
136502
+
136502
   while (*fmt)
136502
     {
136502
       switch (*fmt)
136502
@@ -408,6 +421,8 @@ summarize (fp, fmt, command, resp)
136502
 	      /* % cpu is (total cpu time)/(elapsed time).  */
136502
 	      if (r > 0)
136502
 		fprintf (fp, "%lu%%", (v * 100 / r));
136502
+	      else if (ru > 0)
136502
+		fprintf (fp, "%lu%%", (vu * 100 / ru));
136502
 	      else
136502
 		fprintf (fp, "?%%");
136502
 	      break;
136502
-- 
136502
1.7.4.4
136502