xref: /linux/fs/proc/uptime.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a9caa3deSAlexey Dobriyan #include <linux/fs.h>
396177602SAlexey Dobriyan #include <linux/init.h>
496177602SAlexey Dobriyan #include <linux/proc_fs.h>
596177602SAlexey Dobriyan #include <linux/sched.h>
6a9caa3deSAlexey Dobriyan #include <linux/seq_file.h>
796177602SAlexey Dobriyan #include <linux/time.h>
80efc8bb0SDmitry Safonov #include <linux/time_namespace.h>
996830a57SMichael Abbott #include <linux/kernel_stat.h>
10*ef1d6178SAlexey Dobriyan #include "internal.h"
1196177602SAlexey Dobriyan 
uptime_proc_show(struct seq_file * m,void * v)12a9caa3deSAlexey Dobriyan static int uptime_proc_show(struct seq_file *m, void *v)
1396177602SAlexey Dobriyan {
14bdf228a2SArnd Bergmann 	struct timespec64 uptime;
1595582b00SDeepa Dinamani 	struct timespec64 idle;
16a130e8fbSJosh Don 	u64 idle_nsec;
17c3e0ef9aSMartin Schwidefsky 	u32 rem;
1896830a57SMichael Abbott 	int i;
1996830a57SMichael Abbott 
20a130e8fbSJosh Don 	idle_nsec = 0;
21a130e8fbSJosh Don 	for_each_possible_cpu(i) {
22a130e8fbSJosh Don 		struct kernel_cpustat kcs;
23a130e8fbSJosh Don 
24a130e8fbSJosh Don 		kcpustat_cpu_fetch(&kcs, i);
25a130e8fbSJosh Don 		idle_nsec += get_idle_time(&kcs, i);
26a130e8fbSJosh Don 	}
2796177602SAlexey Dobriyan 
28bdf228a2SArnd Bergmann 	ktime_get_boottime_ts64(&uptime);
290efc8bb0SDmitry Safonov 	timens_add_boottime(&uptime);
300efc8bb0SDmitry Safonov 
31a130e8fbSJosh Don 	idle.tv_sec = div_u64_rem(idle_nsec, NSEC_PER_SEC, &rem);
32c3e0ef9aSMartin Schwidefsky 	idle.tv_nsec = rem;
33a9caa3deSAlexey Dobriyan 	seq_printf(m, "%lu.%02lu %lu.%02lu\n",
3496177602SAlexey Dobriyan 			(unsigned long) uptime.tv_sec,
3596177602SAlexey Dobriyan 			(uptime.tv_nsec / (NSEC_PER_SEC / 100)),
3696177602SAlexey Dobriyan 			(unsigned long) idle.tv_sec,
3796177602SAlexey Dobriyan 			(idle.tv_nsec / (NSEC_PER_SEC / 100)));
38a9caa3deSAlexey Dobriyan 	return 0;
3996177602SAlexey Dobriyan }
4096177602SAlexey Dobriyan 
proc_uptime_init(void)4196177602SAlexey Dobriyan static int __init proc_uptime_init(void)
4296177602SAlexey Dobriyan {
43*ef1d6178SAlexey Dobriyan 	struct proc_dir_entry *pde;
44*ef1d6178SAlexey Dobriyan 
45*ef1d6178SAlexey Dobriyan 	pde = proc_create_single("uptime", 0, NULL, uptime_proc_show);
46*ef1d6178SAlexey Dobriyan 	pde_make_permanent(pde);
4796177602SAlexey Dobriyan 	return 0;
4896177602SAlexey Dobriyan }
49abaf3787SPaul Gortmaker fs_initcall(proc_uptime_init);
50