blob: deb99bc9b7e6b009b8f2210bdf6a0959bfcbe9ed [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +03002#include <linux/fs.h>
Alexey Dobriyan96177602008-10-03 02:38:18 +04003#include <linux/init.h>
4#include <linux/proc_fs.h>
5#include <linux/sched.h>
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +03006#include <linux/seq_file.h>
Alexey Dobriyan96177602008-10-03 02:38:18 +04007#include <linux/time.h>
Dmitry Safonov0efc8bb2019-11-12 01:27:07 +00008#include <linux/time_namespace.h>
Michael Abbott96830a52009-09-24 10:15:19 +02009#include <linux/kernel_stat.h>
Alexey Dobriyan96177602008-10-03 02:38:18 +040010
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +030011static int uptime_proc_show(struct seq_file *m, void *v)
Alexey Dobriyan96177602008-10-03 02:38:18 +040012{
Arnd Bergmannbdf228a2018-08-21 21:54:13 -070013 struct timespec64 uptime;
Deepa Dinamani95582b02018-05-08 19:36:02 -070014 struct timespec64 idle;
Josh Dona130e8f2021-08-27 09:54:38 -070015 u64 idle_nsec;
Martin Schwidefskyc3e0ef92011-12-15 14:56:10 +010016 u32 rem;
Michael Abbott96830a52009-09-24 10:15:19 +020017 int i;
Michael Abbott96830a52009-09-24 10:15:19 +020018
Josh Dona130e8f2021-08-27 09:54:38 -070019 idle_nsec = 0;
20 for_each_possible_cpu(i) {
21 struct kernel_cpustat kcs;
22
23 kcpustat_cpu_fetch(&kcs, i);
24 idle_nsec += get_idle_time(&kcs, i);
25 }
Alexey Dobriyan96177602008-10-03 02:38:18 +040026
Arnd Bergmannbdf228a2018-08-21 21:54:13 -070027 ktime_get_boottime_ts64(&uptime);
Dmitry Safonov0efc8bb2019-11-12 01:27:07 +000028 timens_add_boottime(&uptime);
29
Josh Dona130e8f2021-08-27 09:54:38 -070030 idle.tv_sec = div_u64_rem(idle_nsec, NSEC_PER_SEC, &rem);
Martin Schwidefskyc3e0ef92011-12-15 14:56:10 +010031 idle.tv_nsec = rem;
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +030032 seq_printf(m, "%lu.%02lu %lu.%02lu\n",
Alexey Dobriyan96177602008-10-03 02:38:18 +040033 (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 Dobriyana9caa3d2009-02-20 17:07:22 +030037 return 0;
Alexey Dobriyan96177602008-10-03 02:38:18 +040038}
39
Alexey Dobriyan96177602008-10-03 02:38:18 +040040static int __init proc_uptime_init(void)
41{
Christoph Hellwig3f3942a2018-05-15 15:57:23 +020042 proc_create_single("uptime", 0, NULL, uptime_proc_show);
Alexey Dobriyan96177602008-10-03 02:38:18 +040043 return 0;
44}
Paul Gortmakerabaf3782014-01-23 15:55:45 -080045fs_initcall(proc_uptime_init);