Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/proc_fs.h> |
| 3 | #include <linux/sched.h> |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 4 | #include <linux/time.h> |
| 5 | #include <asm/cputime.h> |
| 6 | |
Alexey Dobriyan | 6c87df3 | 2008-10-27 22:38:27 +0300 | [diff] [blame] | 7 | static int proc_calc_metrics(char *page, char **start, off_t off, |
| 8 | int count, int *eof, int len) |
| 9 | { |
| 10 | if (len <= off + count) |
| 11 | *eof = 1; |
| 12 | *start = page + off; |
| 13 | len -= off; |
| 14 | if (len > count) |
| 15 | len = count; |
| 16 | if (len < 0) |
| 17 | len = 0; |
| 18 | return len; |
| 19 | } |
| 20 | |
| 21 | static int uptime_read_proc(char *page, char **start, off_t off, int count, |
| 22 | int *eof, void *data) |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 23 | { |
| 24 | struct timespec uptime; |
| 25 | struct timespec idle; |
Alexey Dobriyan | 6c87df3 | 2008-10-27 22:38:27 +0300 | [diff] [blame] | 26 | int len; |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 27 | cputime_t idletime = cputime_add(init_task.utime, init_task.stime); |
| 28 | |
| 29 | do_posix_clock_monotonic_gettime(&uptime); |
| 30 | monotonic_to_bootbased(&uptime); |
| 31 | cputime_to_timespec(idletime, &idle); |
Alexey Dobriyan | 6c87df3 | 2008-10-27 22:38:27 +0300 | [diff] [blame] | 32 | len = sprintf(page, "%lu.%02lu %lu.%02lu\n", |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 33 | (unsigned long) uptime.tv_sec, |
| 34 | (uptime.tv_nsec / (NSEC_PER_SEC / 100)), |
| 35 | (unsigned long) idle.tv_sec, |
| 36 | (idle.tv_nsec / (NSEC_PER_SEC / 100))); |
Alexey Dobriyan | 6c87df3 | 2008-10-27 22:38:27 +0300 | [diff] [blame] | 37 | return proc_calc_metrics(page, start, off, count, eof, len); |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 38 | } |
| 39 | |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 40 | static int __init proc_uptime_init(void) |
| 41 | { |
Alexey Dobriyan | 6c87df3 | 2008-10-27 22:38:27 +0300 | [diff] [blame] | 42 | create_proc_read_entry("uptime", 0, NULL, uptime_read_proc, NULL); |
Alexey Dobriyan | 9617760 | 2008-10-03 02:38:18 +0400 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | module_init(proc_uptime_init); |