summaryrefslogtreecommitdiff
path: root/usr.bin/systat/sensors.c
diff options
context:
space:
mode:
authorChris Kuethe <ckuethe@cvs.openbsd.org>2007-03-23 01:10:39 +0000
committerChris Kuethe <ckuethe@cvs.openbsd.org>2007-03-23 01:10:39 +0000
commitd67058749ee0e6d5a7138de075883bf5c1e0fff5 (patch)
tree8ee6cb18574249cfe730a30ce5adca1cc238fa43 /usr.bin/systat/sensors.c
parent6eba61501011365450f62c69b32e7e7724814a76 (diff)
Add a routine to scale timedelta sensors to more meaningful units,
like fmt_scaled(3) does. Ranges from picoseconds to days. Also removes a couple of unneeded curses operations. ok deanna, ok deraadt
Diffstat (limited to 'usr.bin/systat/sensors.c')
-rw-r--r--usr.bin/systat/sensors.c61
1 files changed, 56 insertions, 5 deletions
diff --git a/usr.bin/systat/sensors.c b/usr.bin/systat/sensors.c
index 5d8988f760b..c0c86eb64bf 100644
--- a/usr.bin/systat/sensors.c
+++ b/usr.bin/systat/sensors.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sensors.c,v 1.8 2007/03/04 21:17:37 deanna Exp $ */
+/* $OpenBSD: sensors.c,v 1.9 2007/03/23 01:10:38 ckuethe Exp $ */
/*
* Copyright (c) 2007 Deanna Phillips <deanna@openbsd.org>
@@ -36,6 +36,7 @@ struct sensor sensor;
struct sensordev sensordev;
int row, sensor_cnt;
void printline(void);
+static char * fmttime(double);
WINDOW *
opensensors(void)
@@ -133,8 +134,6 @@ initsensors(void)
void
printline(void)
{
- wmove(wnd, row, 0);
- wclrtoeol(wnd);
mvwprintw(wnd, row, 0, "%s.%s%d", sensordev.xname,
sensor_type_s[sensor.type], sensor.numt);
switch (sensor.type) {
@@ -172,8 +171,7 @@ printline(void)
}
break;
case SENSOR_TIMEDELTA:
- mvwprintw(wnd, row, 24, "%10.6f secs",
- sensor.value / 1000000000.0);
+ mvwprintw(wnd, row, 24, "%15s", fmttime(sensor.value / 1000000000.0));
break;
case SENSOR_WATTHOUR:
mvwprintw(wnd, row, 24, "%12.2f Wh", sensor.value / 1000000.0);
@@ -206,3 +204,56 @@ printline(void)
}
row++;
}
+
+#define SECS_PER_DAY 86400
+#define SECS_PER_HOUR 3600
+#define SECS_PER_MIN 60
+
+static char *
+fmttime(double in)
+{
+ int signbit = 1;
+ int tiny = 0;
+ char *unit;
+#define LEN 32
+ static char outbuf[LEN];
+
+ if (in < 0){
+ signbit = -1;
+ in *= -1;
+ }
+
+ if (in >= SECS_PER_DAY ){
+ unit = "days";
+ in /= SECS_PER_DAY;
+ } else if (in >= SECS_PER_HOUR ){
+ unit = "hr";
+ in /= SECS_PER_HOUR;
+ } else if (in >= SECS_PER_MIN ){
+ unit = "min";
+ in /= SECS_PER_MIN;
+ } else if (in >= 1 ){
+ unit = "sec";
+ /* in *= 1; */ /* no op */
+ } else if (in >= 1e-3 ){
+ unit = "mS";
+ in *= 1e3;
+ } else if (in >= 1e-6 ){
+ unit = "uS";
+ in *= 1e6;
+ } else if (in >= 1e-9 ){
+ unit = "nS";
+ in *= 1e9;
+ } else {
+ unit = "pS";
+ if (in < 1e-13)
+ tiny = 1;
+ in *= 1e12;
+ }
+
+ snprintf(outbuf, LEN,
+ tiny ? "%s%lf %s" : "%s%.3lf %s",
+ signbit == -1 ? "-" : "", in, unit);
+
+ return outbuf;
+}