suspend_time.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * debugfs file to track time spent in suspend
  3. *
  4. * Copyright (c) 2011, Google, Inc.
  5. *
  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.
  10. *
  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
  14. * more details.
  15. */
  16. #include <linux/debugfs.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/syscore_ops.h>
  22. #include <linux/time.h>
  23. static struct timespec suspend_time_before;
  24. static unsigned int time_in_suspend_bins[32];
  25. #ifdef CONFIG_DEBUG_FS
  26. static int suspend_time_debug_show(struct seq_file *s, void *data)
  27. {
  28. int bin;
  29. seq_printf(s, "time (secs) count\n");
  30. seq_printf(s, "------------------\n");
  31. for (bin = 0; bin < 32; bin++) {
  32. if (time_in_suspend_bins[bin] == 0)
  33. continue;
  34. seq_printf(s, "%4d - %4d %4u\n",
  35. bin ? 1 << (bin - 1) : 0, 1 << bin,
  36. time_in_suspend_bins[bin]);
  37. }
  38. return 0;
  39. }
  40. static int suspend_time_debug_open(struct inode *inode, struct file *file)
  41. {
  42. return single_open(file, suspend_time_debug_show, NULL);
  43. }
  44. static const struct file_operations suspend_time_debug_fops = {
  45. .open = suspend_time_debug_open,
  46. .read = seq_read,
  47. .llseek = seq_lseek,
  48. .release = single_release,
  49. };
  50. static int __init suspend_time_debug_init(void)
  51. {
  52. struct dentry *d;
  53. d = debugfs_create_file("suspend_time", 0755, NULL, NULL,
  54. &suspend_time_debug_fops);
  55. if (!d) {
  56. pr_err("Failed to create suspend_time debug file\n");
  57. return -ENOMEM;
  58. }
  59. return 0;
  60. }
  61. late_initcall(suspend_time_debug_init);
  62. #endif
  63. static int suspend_time_syscore_suspend(void)
  64. {
  65. read_persistent_clock(&suspend_time_before);
  66. return 0;
  67. }
  68. static void suspend_time_syscore_resume(void)
  69. {
  70. struct timespec after;
  71. read_persistent_clock(&after);
  72. after = timespec_sub(after, suspend_time_before);
  73. time_in_suspend_bins[fls(after.tv_sec)]++;
  74. pr_info("Suspended for %lu.%03lu seconds\n", after.tv_sec,
  75. after.tv_nsec / NSEC_PER_MSEC);
  76. }
  77. static struct syscore_ops suspend_time_syscore_ops = {
  78. .suspend = suspend_time_syscore_suspend,
  79. .resume = suspend_time_syscore_resume,
  80. };
  81. static int suspend_time_syscore_init(void)
  82. {
  83. register_syscore_ops(&suspend_time_syscore_ops);
  84. return 0;
  85. }
  86. static void suspend_time_syscore_exit(void)
  87. {
  88. unregister_syscore_ops(&suspend_time_syscore_ops);
  89. }
  90. module_init(suspend_time_syscore_init);
  91. module_exit(suspend_time_syscore_exit);