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