1 /* net/activity_stats.c
3 * Copyright (C) 2010 Google, Inc.
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.
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.
14 * Author: Mike Chan (mike@android.com)
17 #include <linux/proc_fs.h>
18 #include <linux/suspend.h>
19 #include <net/net_namespace.h>
22 * Track transmission rates in buckets (power of 2).
23 * 1,2,4,8...512 seconds.
25 * Buckets represent the count of network transmissions at least
26 * N seconds apart, where N is 1 << bucket index.
30 /* Track network activity frequency */
31 static unsigned long activity_stats[BUCKET_MAX];
32 static ktime_t last_transmit;
33 static ktime_t suspend_time;
34 static DEFINE_SPINLOCK(activity_lock);
36 void activity_stats_update(void)
43 spin_lock_irqsave(&activity_lock, flags);
45 delta = ktime_to_ns(ktime_sub(now, last_transmit));
47 for (i = BUCKET_MAX - 1; i >= 0; i--) {
49 * Check if the time delta between network activity is within the
50 * minimum bucket range.
52 if (delta < (1000000000ULL << i))
59 spin_unlock_irqrestore(&activity_lock, flags);
62 static int activity_stats_read_proc(char *page, char **start, off_t off,
63 int count, int *eof, void *data)
69 /* Only print if offset is 0, or we have enough buffer space */
70 if (off || count < (30 * BUCKET_MAX + 22))
73 len = snprintf(p, count, "Min Bucket(sec) Count\n");
77 for (i = 0; i < BUCKET_MAX; i++) {
78 len = snprintf(p, count, "%15d %lu\n", 1 << i, activity_stats[i]);
87 static int activity_stats_notifier(struct notifier_block *nb,
88 unsigned long event, void *dummy)
91 case PM_SUSPEND_PREPARE:
92 suspend_time = ktime_get_real();
96 suspend_time = ktime_sub(ktime_get_real(), suspend_time);
97 last_transmit = ktime_sub(last_transmit, suspend_time);
103 static struct notifier_block activity_stats_notifier_block = {
104 .notifier_call = activity_stats_notifier,
107 static int __init activity_stats_init(void)
109 create_proc_read_entry("activity", S_IRUGO,
110 init_net.proc_net_stat, activity_stats_read_proc, NULL);
111 return register_pm_notifier(&activity_stats_notifier_block);
114 subsys_initcall(activity_stats_init);