Thomas Gleixner | 35728b8 | 2018-10-31 19:21:09 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * debugfs file to track time spent in suspend |
| 4 | * |
| 5 | * Copyright (c) 2011, Google, Inc. |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/seq_file.h> |
Rafael J. Wysocki | cb08e03 | 2017-07-23 00:03:43 +0200 | [diff] [blame] | 13 | #include <linux/suspend.h> |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 14 | #include <linux/time.h> |
| 15 | |
Rashika Kheria | 64e8d20 | 2014-02-27 17:25:54 +0530 | [diff] [blame] | 16 | #include "timekeeping_internal.h" |
| 17 | |
John Stultz | a4f8f66 | 2016-08-23 16:08:22 -0700 | [diff] [blame] | 18 | #define NUM_BINS 32 |
| 19 | |
| 20 | static unsigned int sleep_time_bin[NUM_BINS] = {0}; |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 21 | |
| 22 | static int tk_debug_show_sleep_time(struct seq_file *s, void *data) |
| 23 | { |
| 24 | unsigned int bin; |
| 25 | seq_puts(s, " time (secs) count\n"); |
| 26 | seq_puts(s, "------------------------------\n"); |
| 27 | for (bin = 0; bin < 32; bin++) { |
| 28 | if (sleep_time_bin[bin] == 0) |
| 29 | continue; |
| 30 | seq_printf(s, "%10u - %-10u %4u\n", |
| 31 | bin ? 1 << (bin - 1) : 0, 1 << bin, |
| 32 | sleep_time_bin[bin]); |
| 33 | } |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int tk_debug_sleep_time_open(struct inode *inode, struct file *file) |
| 38 | { |
| 39 | return single_open(file, tk_debug_show_sleep_time, NULL); |
| 40 | } |
| 41 | |
| 42 | static const struct file_operations tk_debug_sleep_time_fops = { |
| 43 | .open = tk_debug_sleep_time_open, |
| 44 | .read = seq_read, |
| 45 | .llseek = seq_lseek, |
| 46 | .release = single_release, |
| 47 | }; |
| 48 | |
| 49 | static int __init tk_debug_sleep_time_init(void) |
| 50 | { |
| 51 | struct dentry *d; |
| 52 | |
| 53 | d = debugfs_create_file("sleep_time", 0444, NULL, NULL, |
| 54 | &tk_debug_sleep_time_fops); |
| 55 | if (!d) { |
| 56 | pr_err("Failed to create sleep_time debug file\n"); |
| 57 | return -ENOMEM; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | late_initcall(tk_debug_sleep_time_init); |
| 63 | |
Ondrej Mosnacek | 985e695 | 2018-07-13 14:06:42 +0200 | [diff] [blame] | 64 | void tk_debug_account_sleep_time(const struct timespec64 *t) |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 65 | { |
John Stultz | a4f8f66 | 2016-08-23 16:08:22 -0700 | [diff] [blame] | 66 | /* Cap bin index so we don't overflow the array */ |
| 67 | int bin = min(fls(t->tv_sec), NUM_BINS-1); |
| 68 | |
| 69 | sleep_time_bin[bin]++; |
Rafael J. Wysocki | cb08e03 | 2017-07-23 00:03:43 +0200 | [diff] [blame] | 70 | pm_deferred_pr_dbg("Timekeeping suspended for %lld.%03lu seconds\n", |
| 71 | (s64)t->tv_sec, t->tv_nsec / NSEC_PER_MSEC); |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 72 | } |
| 73 | |