am 61af6530: Merge "Remove /dev/alarm cruft from uptime."
* commit '61af6530605e10ca99f066c851436921c0385479': Remove /dev/alarm cruft from uptime.
This commit is contained in:
commit
a575b8503d
1 changed files with 21 additions and 56 deletions
|
|
@ -29,71 +29,33 @@
|
|||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <linux/ioctl.h>
|
||||
#include <linux/rtc.h>
|
||||
#include <linux/android_alarm.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void format_time(int time, char* buffer) {
|
||||
int seconds, minutes, hours, days;
|
||||
|
||||
seconds = time % 60;
|
||||
int seconds = time % 60;
|
||||
time /= 60;
|
||||
minutes = time % 60;
|
||||
int minutes = time % 60;
|
||||
time /= 60;
|
||||
hours = time % 24;
|
||||
days = time / 24;
|
||||
int hours = time % 24;
|
||||
int days = time / 24;
|
||||
|
||||
if (days > 0)
|
||||
sprintf(buffer, "%d days, %02d:%02d:%02d", days, hours, minutes, seconds);
|
||||
else
|
||||
if (days > 0) {
|
||||
sprintf(buffer, "%d day%s, %02d:%02d:%02d", days, (days == 1) ? "" : "s", hours, minutes, seconds);
|
||||
} else {
|
||||
sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
static int elapsedRealtimeAlarm(struct timespec *ts)
|
||||
{
|
||||
int fd, result;
|
||||
|
||||
fd = open("/dev/alarm", O_RDONLY);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
result = ioctl(fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), ts);
|
||||
close(fd);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int64_t elapsedRealtime()
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
int result = elapsedRealtimeAlarm(&ts);
|
||||
if (result < 0)
|
||||
result = clock_gettime(CLOCK_BOOTTIME, &ts);
|
||||
|
||||
if (result == 0)
|
||||
return ts.tv_sec;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int uptime_main(int argc __attribute__((unused)),
|
||||
char *argv[] __attribute__((unused)))
|
||||
{
|
||||
float up_time, idle_time;
|
||||
char up_string[100], idle_string[100], sleep_string[100];
|
||||
int elapsed;
|
||||
struct timespec up_timespec;
|
||||
|
||||
int uptime_main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) {
|
||||
FILE* file = fopen("/proc/uptime", "r");
|
||||
if (!file) {
|
||||
fprintf(stderr, "Could not open /proc/uptime\n");
|
||||
return -1;
|
||||
}
|
||||
float idle_time;
|
||||
if (fscanf(file, "%*f %f", &idle_time) != 1) {
|
||||
fprintf(stderr, "Could not parse /proc/uptime\n");
|
||||
fclose(file);
|
||||
|
|
@ -101,18 +63,21 @@ int uptime_main(int argc __attribute__((unused)),
|
|||
}
|
||||
fclose(file);
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) < 0) {
|
||||
fprintf(stderr, "Could not get monotonic time\n");
|
||||
struct timespec up_timespec;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) == -1) {
|
||||
fprintf(stderr, "Could not get monotonic time: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9;
|
||||
float up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9;
|
||||
|
||||
elapsed = elapsedRealtime();
|
||||
if (elapsed < 0) {
|
||||
fprintf(stderr, "elapsedRealtime failed\n");
|
||||
struct timespec elapsed_timespec;
|
||||
if (clock_gettime(CLOCK_BOOTTIME, &elapsed_timespec) == -1) {
|
||||
fprintf(stderr, "Could not get boot time: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
int elapsed = elapsed_timespec.tv_sec;
|
||||
|
||||
char up_string[100], idle_string[100], sleep_string[100];
|
||||
format_time(elapsed, up_string);
|
||||
format_time((int)idle_time, idle_string);
|
||||
format_time((int)(elapsed - up_time), sleep_string);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue