xref: /openbmc/linux/kernel/time/timekeeping_debug.c (revision 597473720f4dc69749542bfcfed4a927a43d935e)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0+
25c83545fSColin Cross /*
35c83545fSColin Cross  * debugfs file to track time spent in suspend
45c83545fSColin Cross  *
55c83545fSColin Cross  * Copyright (c) 2011, Google, Inc.
65c83545fSColin Cross  */
75c83545fSColin Cross 
85c83545fSColin Cross #include <linux/debugfs.h>
95c83545fSColin Cross #include <linux/err.h>
105c83545fSColin Cross #include <linux/init.h>
115c83545fSColin Cross #include <linux/kernel.h>
125c83545fSColin Cross #include <linux/seq_file.h>
13cb08e035SRafael J. Wysocki #include <linux/suspend.h>
145c83545fSColin Cross #include <linux/time.h>
155c83545fSColin Cross 
1664e8d20bSRashika Kheria #include "timekeeping_internal.h"
1764e8d20bSRashika Kheria 
18a4f8f666SJohn Stultz #define NUM_BINS 32
19a4f8f666SJohn Stultz 
20a4f8f666SJohn Stultz static unsigned int sleep_time_bin[NUM_BINS] = {0};
215c83545fSColin Cross 
tk_debug_sleep_time_show(struct seq_file * s,void * data)225b20c6fdSYangtao Li static int tk_debug_sleep_time_show(struct seq_file *s, void *data)
235c83545fSColin Cross {
245c83545fSColin Cross 	unsigned int bin;
255c83545fSColin Cross 	seq_puts(s, "      time (secs)        count\n");
265c83545fSColin Cross 	seq_puts(s, "------------------------------\n");
275c83545fSColin Cross 	for (bin = 0; bin < 32; bin++) {
285c83545fSColin Cross 		if (sleep_time_bin[bin] == 0)
295c83545fSColin Cross 			continue;
305c83545fSColin Cross 		seq_printf(s, "%10u - %-10u %4u\n",
315c83545fSColin Cross 			bin ? 1 << (bin - 1) : 0, 1 << bin,
325c83545fSColin Cross 				sleep_time_bin[bin]);
335c83545fSColin Cross 	}
345c83545fSColin Cross 	return 0;
355c83545fSColin Cross }
365b20c6fdSYangtao Li DEFINE_SHOW_ATTRIBUTE(tk_debug_sleep_time);
375c83545fSColin Cross 
tk_debug_sleep_time_init(void)385c83545fSColin Cross static int __init tk_debug_sleep_time_init(void)
395c83545fSColin Cross {
40*ae503ab0SGreg Kroah-Hartman 	debugfs_create_file("sleep_time", 0444, NULL, NULL,
415c83545fSColin Cross 			    &tk_debug_sleep_time_fops);
425c83545fSColin Cross 	return 0;
435c83545fSColin Cross }
445c83545fSColin Cross late_initcall(tk_debug_sleep_time_init);
455c83545fSColin Cross 
tk_debug_account_sleep_time(const struct timespec64 * t)46985e6950SOndrej Mosnacek void tk_debug_account_sleep_time(const struct timespec64 *t)
475c83545fSColin Cross {
48a4f8f666SJohn Stultz 	/* Cap bin index so we don't overflow the array */
49a4f8f666SJohn Stultz 	int bin = min(fls(t->tv_sec), NUM_BINS-1);
50a4f8f666SJohn Stultz 
51a4f8f666SJohn Stultz 	sleep_time_bin[bin]++;
52cb08e035SRafael J. Wysocki 	pm_deferred_pr_dbg("Timekeeping suspended for %lld.%03lu seconds\n",
53f222449cSSergey Senozhatsky 			   (s64)t->tv_sec, t->tv_nsec / NSEC_PER_MSEC);
545c83545fSColin Cross }
555c83545fSColin Cross 
56