activity_stats.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* net/activity_stats.c
  2. *
  3. * Copyright (C) 2010 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Author: Mike Chan (mike@android.com)
  15. */
  16. #include <linux/proc_fs.h>
  17. #include <linux/suspend.h>
  18. #include <net/net_namespace.h>
  19. /*
  20. * Track transmission rates in buckets (power of 2).
  21. * 1,2,4,8...512 seconds.
  22. *
  23. * Buckets represent the count of network transmissions at least
  24. * N seconds apart, where N is 1 << bucket index.
  25. */
  26. #define BUCKET_MAX 10
  27. /* Track network activity frequency */
  28. static unsigned long activity_stats[BUCKET_MAX];
  29. static ktime_t last_transmit;
  30. static ktime_t suspend_time;
  31. static DEFINE_SPINLOCK(activity_lock);
  32. void activity_stats_update(void)
  33. {
  34. int i;
  35. unsigned long flags;
  36. ktime_t now;
  37. s64 delta;
  38. spin_lock_irqsave(&activity_lock, flags);
  39. now = ktime_get();
  40. delta = ktime_to_ns(ktime_sub(now, last_transmit));
  41. for (i = BUCKET_MAX - 1; i >= 0; i--) {
  42. /*
  43. * Check if the time delta between network activity is within the
  44. * minimum bucket range.
  45. */
  46. if (delta < (1000000000ULL << i))
  47. continue;
  48. activity_stats[i]++;
  49. last_transmit = now;
  50. break;
  51. }
  52. spin_unlock_irqrestore(&activity_lock, flags);
  53. }
  54. static int activity_stats_read_proc(char *page, char **start, off_t off,
  55. int count, int *eof, void *data)
  56. {
  57. int i;
  58. int len;
  59. char *p = page;
  60. /* Only print if offset is 0, or we have enough buffer space */
  61. if (off || count < (30 * BUCKET_MAX + 22))
  62. return -ENOMEM;
  63. len = snprintf(p, count, "Min Bucket(sec) Count\n");
  64. count -= len;
  65. p += len;
  66. for (i = 0; i < BUCKET_MAX; i++) {
  67. len = snprintf(p, count, "%15d %lu\n", 1 << i, activity_stats[i]);
  68. count -= len;
  69. p += len;
  70. }
  71. *eof = 1;
  72. return p - page;
  73. }
  74. static int activity_stats_notifier(struct notifier_block *nb,
  75. unsigned long event, void *dummy)
  76. {
  77. switch (event) {
  78. case PM_SUSPEND_PREPARE:
  79. suspend_time = ktime_get_real();
  80. break;
  81. case PM_POST_SUSPEND:
  82. suspend_time = ktime_sub(ktime_get_real(), suspend_time);
  83. last_transmit = ktime_sub(last_transmit, suspend_time);
  84. }
  85. return 0;
  86. }
  87. static struct notifier_block activity_stats_notifier_block = {
  88. .notifier_call = activity_stats_notifier,
  89. };
  90. static int __init activity_stats_init(void)
  91. {
  92. create_proc_read_entry("activity", S_IRUGO,
  93. init_net.proc_net_stat, activity_stats_read_proc, NULL);
  94. return register_pm_notifier(&activity_stats_notifier_block);
  95. }
  96. subsys_initcall(activity_stats_init);