activity_stats.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/seq_file.h>
  18. #include <linux/suspend.h>
  19. #include <net/net_namespace.h>
  20. /*
  21. * Track transmission rates in buckets (power of 2).
  22. * 1,2,4,8...512 seconds.
  23. *
  24. * Buckets represent the count of network transmissions at least
  25. * N seconds apart, where N is 1 << bucket index.
  26. */
  27. #define BUCKET_MAX 10
  28. /* Track network activity frequency */
  29. static unsigned long activity_stats[BUCKET_MAX];
  30. static ktime_t last_transmit;
  31. static ktime_t suspend_time;
  32. static DEFINE_SPINLOCK(activity_lock);
  33. void activity_stats_update(void)
  34. {
  35. int i;
  36. unsigned long flags;
  37. ktime_t now;
  38. s64 delta;
  39. spin_lock_irqsave(&activity_lock, flags);
  40. now = ktime_get();
  41. delta = ktime_to_ns(ktime_sub(now, last_transmit));
  42. for (i = BUCKET_MAX - 1; i >= 0; i--) {
  43. /*
  44. * Check if the time delta between network activity is within the
  45. * minimum bucket range.
  46. */
  47. if (delta < (1000000000ULL << i))
  48. continue;
  49. activity_stats[i]++;
  50. last_transmit = now;
  51. break;
  52. }
  53. spin_unlock_irqrestore(&activity_lock, flags);
  54. }
  55. static int activity_stats_show(struct seq_file *m, void *v)
  56. {
  57. int i;
  58. int ret;
  59. seq_printf(m, "Min Bucket(sec) Count\n");
  60. for (i = 0; i < BUCKET_MAX; i++) {
  61. ret = seq_printf(m, "%15d %lu\n", 1 << i, activity_stats[i]);
  62. if (ret)
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static int activity_stats_notifier(struct notifier_block *nb,
  68. unsigned long event, void *dummy)
  69. {
  70. switch (event) {
  71. case PM_SUSPEND_PREPARE:
  72. suspend_time = ktime_get_real();
  73. break;
  74. case PM_POST_SUSPEND:
  75. suspend_time = ktime_sub(ktime_get_real(), suspend_time);
  76. last_transmit = ktime_sub(last_transmit, suspend_time);
  77. }
  78. return 0;
  79. }
  80. static int activity_stats_open(struct inode *inode, struct file *file)
  81. {
  82. return single_open(file, activity_stats_show, PDE_DATA(inode));
  83. }
  84. static const struct file_operations activity_stats_fops = {
  85. .open = activity_stats_open,
  86. .read = seq_read,
  87. .llseek = seq_lseek,
  88. .release = single_release,
  89. };
  90. static struct notifier_block activity_stats_notifier_block = {
  91. .notifier_call = activity_stats_notifier,
  92. };
  93. static int __init activity_stats_init(void)
  94. {
  95. proc_create("activity", S_IRUGO,
  96. init_net.proc_net_stat, &activity_stats_fops);
  97. return register_pm_notifier(&activity_stats_notifier_block);
  98. }
  99. subsys_initcall(activity_stats_init);