aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2011-01-05 15:19:31 -0800
committerColin Cross <ccross@android.com>2011-01-05 15:22:09 -0800
commit9d6d030f83f682a0ab944260eca8402d85fb008c (patch)
treebf5ce20cc88a422596b9a8a11bde993da4532a4b
parent8bc6fb3433c76428d2fffb83be8902052644fca2 (diff)
downloadsystem_core-9d6d030f83f682a0ab944260eca8402d85fb008c.tar.gz
system_core-9d6d030f83f682a0ab944260eca8402d85fb008c.tar.bz2
system_core-9d6d030f83f682a0ab944260eca8402d85fb008c.zip
uptime: Use clock_gettime to get monotonic time
The first field in /proc/uptime is bootbased time, not monotonic time. If the kernel tracks bootbased time correctly, it counts elapsed run time as well as sleep time, which is the same as the elapsed time in the android alarm driver, and sleep time is always returned as 0. Use clock_gettime(CLOCK_MONOTONIC) instead, which will return elapsed run time not counting sleep time on all platforms. Change-Id: I28a22e8c93d78f62666ee8c877c7c6718a2b640a
-rw-r--r--toolbox/uptime.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/toolbox/uptime.c b/toolbox/uptime.c
index 3d8061cf..1c312b02 100644
--- a/toolbox/uptime.c
+++ b/toolbox/uptime.c
@@ -35,6 +35,7 @@
#include <linux/android_alarm.h>
#include <fcntl.h>
#include <stdio.h>
+#include <time.h>
static void format_time(int time, char* buffer) {
@@ -75,19 +76,26 @@ int uptime_main(int argc, char *argv[])
float up_time, idle_time;
char up_string[100], idle_string[100], sleep_string[100];
int elapsed;
+ struct timespec up_timespec;
FILE* file = fopen("/proc/uptime", "r");
if (!file) {
fprintf(stderr, "Could not open /proc/uptime\n");
return -1;
}
- if (fscanf(file, "%f %f", &up_time, &idle_time) != 2) {
+ if (fscanf(file, "%*f %f", &idle_time) != 1) {
fprintf(stderr, "Could not parse /proc/uptime\n");
fclose(file);
return -1;
}
fclose(file);
+ if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) < 0) {
+ fprintf(stderr, "Could not get monotonic time\n");
+ return -1;
+ }
+ up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9;
+
elapsed = elapsedRealtime();
if (elapsed < 0) {
fprintf(stderr, "elapsedRealtime failed\n");