loadavg.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/init.h>
  4. #include <linux/pid_namespace.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/sched.h>
  7. #include <linux/sched/loadavg.h>
  8. #include <linux/sched/stat.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/seqlock.h>
  11. #include <linux/time.h>
  12. #define LOAD_INT(x) ((x) >> FSHIFT)
  13. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  14. static int loadavg_proc_show(struct seq_file *m, void *v)
  15. {
  16. unsigned long avnrun[3];
  17. get_avenrun(avnrun, FIXED_1/200, 0);
  18. seq_printf(m, "%lu.%02lu %lu.%02lu %lu.%02lu %ld/%d %d\n",
  19. LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
  20. LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
  21. LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
  22. nr_running(), nr_threads,
  23. idr_get_cursor(&task_active_pid_ns(current)->idr));
  24. return 0;
  25. }
  26. static int loadavg_proc_open(struct inode *inode, struct file *file)
  27. {
  28. return single_open(file, loadavg_proc_show, NULL);
  29. }
  30. static const struct file_operations loadavg_proc_fops = {
  31. .open = loadavg_proc_open,
  32. .read = seq_read,
  33. .llseek = seq_lseek,
  34. .release = single_release,
  35. };
  36. static int __init proc_loadavg_init(void)
  37. {
  38. proc_create("loadavg", 0, NULL, &loadavg_proc_fops);
  39. return 0;
  40. }
  41. fs_initcall(proc_loadavg_init);