^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/time_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static int uptime_proc_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct timespec64 uptime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct timespec64 idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u64 idle_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u32 rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) idle_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct kernel_cpustat kcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) kcpustat_cpu_fetch(&kcs, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) idle_nsec += get_idle_time(&kcs, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ktime_get_boottime_ts64(&uptime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) timens_add_boottime(&uptime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) idle.tv_sec = div_u64_rem(idle_nsec, NSEC_PER_SEC, &rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) idle.tv_nsec = rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) seq_printf(m, "%lu.%02lu %lu.%02lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (unsigned long) uptime.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) (unsigned long) idle.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (idle.tv_nsec / (NSEC_PER_SEC / 100)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int __init proc_uptime_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) proc_create_single("uptime", 0, NULL, uptime_proc_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fs_initcall(proc_uptime_init);