tools/power turbostat: allow sub-sec intervals
authorLen Brown <len.brown@intel.com>
Sat, 13 Feb 2016 03:44:48 +0000 (22:44 -0500)
committerLen Brown <len.brown@intel.com>
Sun, 13 Mar 2016 07:55:32 +0000 (03:55 -0400)
turbostat -i interval_sec

will sample and display statistics every interval_sec.
interval_sec used to be a whole number of seconds,
but now we accept a decimal, as small as 0.001 sec (1 ms).

Signed-off-by: Len Brown <len.brown@intel.com>
tools/power/x86/turbostat/turbostat.8
tools/power/x86/turbostat/turbostat.c

index 622db685b4f9930ab4c2f67aadf947969652371f..4d8f198f348f946cb817155e1167fce8df493d52 100644 (file)
@@ -34,7 +34,7 @@ name as necessary to disambiguate it from others is necessary.  Note that option
 \fB--debug\fP displays additional system configuration information.  Invoking this parameter
 more than once may also enable internal turbostat debug information.
 .PP
-\fB--interval seconds\fP overrides the default 5-second measurement interval.
+\fB--interval seconds\fP overrides the default 5.0 second measurement interval.
 .PP
 \fB--help\fP displays usage for the most common parameters.
 .PP
index c600340dfc4e78a6cb8063188d1cc37dab253a6b..e411cc4351374e2fa375702203c9a8fbc9540eff 100644 (file)
 #include <string.h>
 #include <ctype.h>
 #include <sched.h>
+#include <time.h>
 #include <cpuid.h>
 #include <linux/capability.h>
 #include <errno.h>
 
 char *proc_stat = "/proc/stat";
-unsigned int interval_sec = 5;
+struct timespec interval_ts = {5, 0};
 unsigned int debug;
 unsigned int rapl_joules;
 unsigned int summary_only;
@@ -1728,7 +1729,7 @@ restart:
                        re_initialize();
                        goto restart;
                }
-               sleep(interval_sec);
+               nanosleep(&interval_ts, NULL);
                retval = for_all_cpus(get_counters, ODD_COUNTERS);
                if (retval < -1) {
                        exit(retval);
@@ -1742,7 +1743,7 @@ restart:
                compute_average(EVEN_COUNTERS);
                format_all_counters(EVEN_COUNTERS);
                flush_stdout();
-               sleep(interval_sec);
+               nanosleep(&interval_ts, NULL);
                retval = for_all_cpus(get_counters, EVEN_COUNTERS);
                if (retval < -1) {
                        exit(retval);
@@ -3347,7 +3348,18 @@ void cmdline(int argc, char **argv)
                        help();
                        exit(1);
                case 'i':
-                       interval_sec = atoi(optarg);
+                       {
+                               double interval = strtod(optarg, NULL);
+
+                               if (interval < 0.001) {
+                                       fprintf(stderr, "interval %f seconds is too small\n",
+                                               interval);
+                                       exit(2);
+                               }
+
+                               interval_ts.tv_sec = interval;
+                               interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
+                       }
                        break;
                case 'J':
                        rapl_joules++;