blob: b5343d209381aae892d2423602607f1ce05e479e [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 Dobriyanef1d6172022-09-20 20:35:23 +030010#include "internal.h"
Alexey Dobriyan96177602008-10-03 02:38:18 +040011
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +030012static int uptime_proc_show(struct seq_file *m, void *v)
Alexey Dobriyan96177602008-10-03 02:38:18 +040013{
Arnd Bergmannbdf228a2018-08-21 21:54:13 -070014 struct timespec64 uptime;
Deepa Dinamani95582b02018-05-08 19:36:02 -070015 struct timespec64 idle;
Josh Dona130e8f2021-08-27 09:54:38 -070016 u64 idle_nsec;
Martin Schwidefskyc3e0ef92011-12-15 14:56:10 +010017 u32 rem;
Michael Abbott96830a52009-09-24 10:15:19 +020018 int i;
Michael Abbott96830a52009-09-24 10:15:19 +020019
Josh Dona130e8f2021-08-27 09:54:38 -070020 idle_nsec = 0;
21 for_each_possible_cpu(i) {
22 struct kernel_cpustat kcs;
23
24 kcpustat_cpu_fetch(&kcs, i);
25 idle_nsec += get_idle_time(&kcs, i);
26 }
Alexey Dobriyan96177602008-10-03 02:38:18 +040027
Arnd Bergmannbdf228a2018-08-21 21:54:13 -070028 ktime_get_boottime_ts64(&uptime);
Dmitry Safonov0efc8bb2019-11-12 01:27:07 +000029 timens_add_boottime(&uptime);
30
Josh Dona130e8f2021-08-27 09:54:38 -070031 idle.tv_sec = div_u64_rem(idle_nsec, NSEC_PER_SEC, &rem);
Martin Schwidefskyc3e0ef92011-12-15 14:56:10 +010032 idle.tv_nsec = rem;
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +030033 seq_printf(m, "%lu.%02lu %lu.%02lu\n",
Alexey Dobriyan96177602008-10-03 02:38:18 +040034 (unsigned long) uptime.tv_sec,
35 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
36 (unsigned long) idle.tv_sec,
37 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
Alexey Dobriyana9caa3d2009-02-20 17:07:22 +030038 return 0;
Alexey Dobriyan96177602008-10-03 02:38:18 +040039}
40
Alexey Dobriyan96177602008-10-03 02:38:18 +040041static int __init proc_uptime_init(void)
42{
Alexey Dobriyanef1d6172022-09-20 20:35:23 +030043 struct proc_dir_entry *pde;
44
45 pde = proc_create_single("uptime", 0, NULL, uptime_proc_show);
46 pde_make_permanent(pde);
Alexey Dobriyan96177602008-10-03 02:38:18 +040047 return 0;
48}
Paul Gortmakerabaf3782014-01-23 15:55:45 -080049fs_initcall(proc_uptime_init);