uptime.c 1.1 KB

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