2 * debugfs file to track time spent in suspend
4 * Copyright (c) 2011, Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/debugfs.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/seq_file.h>
22 #include <linux/syscore_ops.h>
23 #include <linux/time.h>
25 static struct timespec suspend_time_before
;
26 static unsigned int time_in_suspend_bins
[32];
28 #ifdef CONFIG_DEBUG_FS
29 static int suspend_time_debug_show(struct seq_file
*s
, void *data
)
32 seq_printf(s
, "time (secs) count\n");
33 seq_printf(s
, "------------------\n");
34 for (bin
= 0; bin
< 32; bin
++) {
35 if (time_in_suspend_bins
[bin
] == 0)
37 seq_printf(s
, "%4d - %4d %4u\n",
38 bin
? 1 << (bin
- 1) : 0, 1 << bin
,
39 time_in_suspend_bins
[bin
]);
44 static int suspend_time_debug_open(struct inode
*inode
, struct file
*file
)
46 return single_open(file
, suspend_time_debug_show
, NULL
);
49 static const struct file_operations suspend_time_debug_fops
= {
50 .open
= suspend_time_debug_open
,
53 .release
= single_release
,
56 static int __init
suspend_time_debug_init(void)
60 d
= debugfs_create_file("suspend_time", 0755, NULL
, NULL
,
61 &suspend_time_debug_fops
);
63 pr_err("Failed to create suspend_time debug file\n");
70 late_initcall(suspend_time_debug_init
);
73 static int suspend_time_syscore_suspend(void)
75 read_persistent_clock(&suspend_time_before
);
80 static void suspend_time_syscore_resume(void)
82 struct timespec after
;
84 read_persistent_clock(&after
);
86 after
= timespec_sub(after
, suspend_time_before
);
88 time_in_suspend_bins
[fls(after
.tv_sec
)]++;
90 pr_info("Suspended for %lu.%03lu seconds\n", after
.tv_sec
,
91 after
.tv_nsec
/ NSEC_PER_MSEC
);
94 static struct syscore_ops suspend_time_syscore_ops
= {
95 .suspend
= suspend_time_syscore_suspend
,
96 .resume
= suspend_time_syscore_resume
,
99 static int suspend_time_syscore_init(void)
101 register_syscore_ops(&suspend_time_syscore_ops
);
106 static void suspend_time_syscore_exit(void)
108 unregister_syscore_ops(&suspend_time_syscore_ops
);
110 module_init(suspend_time_syscore_init
);
111 module_exit(suspend_time_syscore_exit
);