timekeeping_debug.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include <linux/debugfs.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/suspend.h>
  13. #include <linux/time.h>
  14. #include "timekeeping_internal.h"
  15. #define NUM_BINS 32
  16. static unsigned int sleep_time_bin[NUM_BINS] = {0};
  17. static int tk_debug_sleep_time_show(struct seq_file *s, void *data)
  18. {
  19. unsigned int bin;
  20. seq_puts(s, " time (secs) count\n");
  21. seq_puts(s, "------------------------------\n");
  22. for (bin = 0; bin < 32; bin++) {
  23. if (sleep_time_bin[bin] == 0)
  24. continue;
  25. seq_printf(s, "%10u - %-10u %4u\n",
  26. bin ? 1 << (bin - 1) : 0, 1 << bin,
  27. sleep_time_bin[bin]);
  28. }
  29. return 0;
  30. }
  31. DEFINE_SHOW_ATTRIBUTE(tk_debug_sleep_time);
  32. static int __init tk_debug_sleep_time_init(void)
  33. {
  34. debugfs_create_file("sleep_time", 0444, NULL, NULL,
  35. &tk_debug_sleep_time_fops);
  36. return 0;
  37. }
  38. late_initcall(tk_debug_sleep_time_init);
  39. void tk_debug_account_sleep_time(const struct timespec64 *t)
  40. {
  41. /* Cap bin index so we don't overflow the array */
  42. int bin = min(fls(t->tv_sec), NUM_BINS-1);
  43. sleep_time_bin[bin]++;
  44. pm_deferred_pr_dbg("Timekeeping suspended for %lld.%03lu seconds\n",
  45. (s64)t->tv_sec, t->tv_nsec / NSEC_PER_MSEC);
  46. }