1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * debugfs file to track time spent in suspend 4 * 5 * Copyright (c) 2011, Google, Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 */ 17 18 #include <linux/debugfs.h> 19 #include <linux/err.h> 20 #include <linux/init.h> 21 #include <linux/kernel.h> 22 #include <linux/seq_file.h> 23 #include <linux/suspend.h> 24 #include <linux/time.h> 25 26 #include "timekeeping_internal.h" 27 28 #define NUM_BINS 32 29 30 static unsigned int sleep_time_bin[NUM_BINS] = {0}; 31 32 static int tk_debug_show_sleep_time(struct seq_file *s, void *data) 33 { 34 unsigned int bin; 35 seq_puts(s, " time (secs) count\n"); 36 seq_puts(s, "------------------------------\n"); 37 for (bin = 0; bin < 32; bin++) { 38 if (sleep_time_bin[bin] == 0) 39 continue; 40 seq_printf(s, "%10u - %-10u %4u\n", 41 bin ? 1 << (bin - 1) : 0, 1 << bin, 42 sleep_time_bin[bin]); 43 } 44 return 0; 45 } 46 47 static int tk_debug_sleep_time_open(struct inode *inode, struct file *file) 48 { 49 return single_open(file, tk_debug_show_sleep_time, NULL); 50 } 51 52 static const struct file_operations tk_debug_sleep_time_fops = { 53 .open = tk_debug_sleep_time_open, 54 .read = seq_read, 55 .llseek = seq_lseek, 56 .release = single_release, 57 }; 58 59 static int __init tk_debug_sleep_time_init(void) 60 { 61 struct dentry *d; 62 63 d = debugfs_create_file("sleep_time", 0444, NULL, NULL, 64 &tk_debug_sleep_time_fops); 65 if (!d) { 66 pr_err("Failed to create sleep_time debug file\n"); 67 return -ENOMEM; 68 } 69 70 return 0; 71 } 72 late_initcall(tk_debug_sleep_time_init); 73 74 void tk_debug_account_sleep_time(const struct timespec64 *t) 75 { 76 /* Cap bin index so we don't overflow the array */ 77 int bin = min(fls(t->tv_sec), NUM_BINS-1); 78 79 sleep_time_bin[bin]++; 80 pm_deferred_pr_dbg("Timekeeping suspended for %lld.%03lu seconds\n", 81 (s64)t->tv_sec, t->tv_nsec / NSEC_PER_MSEC); 82 } 83 84