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 
20*2a153857SJeff Layton /* Incremented every time mg_floor is updated */
21*2a153857SJeff Layton DEFINE_PER_CPU(unsigned long, timekeeping_mg_floor_swaps);
22*2a153857SJeff Layton 
23a4f8f666SJohn Stultz static unsigned int sleep_time_bin[NUM_BINS] = {0};
245c83545fSColin Cross 
tk_debug_sleep_time_show(struct seq_file * s,void * data)255b20c6fdSYangtao Li static int tk_debug_sleep_time_show(struct seq_file *s, void *data)
265c83545fSColin Cross {
275c83545fSColin Cross 	unsigned int bin;
285c83545fSColin Cross 	seq_puts(s, "      time (secs)        count\n");
295c83545fSColin Cross 	seq_puts(s, "------------------------------\n");
305c83545fSColin Cross 	for (bin = 0; bin < 32; bin++) {
315c83545fSColin Cross 		if (sleep_time_bin[bin] == 0)
325c83545fSColin Cross 			continue;
335c83545fSColin Cross 		seq_printf(s, "%10u - %-10u %4u\n",
345c83545fSColin Cross 			bin ? 1 << (bin - 1) : 0, 1 << bin,
355c83545fSColin Cross 				sleep_time_bin[bin]);
365c83545fSColin Cross 	}
375c83545fSColin Cross 	return 0;
385c83545fSColin Cross }
395b20c6fdSYangtao Li DEFINE_SHOW_ATTRIBUTE(tk_debug_sleep_time);
405c83545fSColin Cross 
tk_debug_sleep_time_init(void)415c83545fSColin Cross static int __init tk_debug_sleep_time_init(void)
425c83545fSColin Cross {
43ae503ab0SGreg Kroah-Hartman 	debugfs_create_file("sleep_time", 0444, NULL, NULL,
445c83545fSColin Cross 			    &tk_debug_sleep_time_fops);
455c83545fSColin Cross 	return 0;
465c83545fSColin Cross }
475c83545fSColin Cross late_initcall(tk_debug_sleep_time_init);
485c83545fSColin Cross 
tk_debug_account_sleep_time(const struct timespec64 * t)49985e6950SOndrej Mosnacek void tk_debug_account_sleep_time(const struct timespec64 *t)
505c83545fSColin Cross {
51a4f8f666SJohn Stultz 	/* Cap bin index so we don't overflow the array */
52a4f8f666SJohn Stultz 	int bin = min(fls(t->tv_sec), NUM_BINS-1);
53a4f8f666SJohn Stultz 
54a4f8f666SJohn Stultz 	sleep_time_bin[bin]++;
55cb08e035SRafael J. Wysocki 	pm_deferred_pr_dbg("Timekeeping suspended for %lld.%03lu seconds\n",
56f222449cSSergey Senozhatsky 			   (s64)t->tv_sec, t->tv_nsec / NSEC_PER_MSEC);
575c83545fSColin Cross }
585c83545fSColin Cross 
timekeeping_get_mg_floor_swaps(void)59*2a153857SJeff Layton unsigned long timekeeping_get_mg_floor_swaps(void)
60*2a153857SJeff Layton {
61*2a153857SJeff Layton 	unsigned long sum = 0;
62*2a153857SJeff Layton 	int cpu;
63*2a153857SJeff Layton 
64*2a153857SJeff Layton 	for_each_possible_cpu(cpu)
65*2a153857SJeff Layton 		sum += data_race(per_cpu(timekeeping_mg_floor_swaps, cpu));
66*2a153857SJeff Layton 
67*2a153857SJeff Layton 	return sum;
68*2a153857SJeff Layton }
69