blob: 4609ce2043eb3677905b5ad5decc803e9cd40b7b [file] [log] [blame]
Mike Chan1f657852010-05-28 14:32:19 -07001/* 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
17#include <linux/proc_fs.h>
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070018#include <linux/seq_file.h>
Mike Chan1f657852010-05-28 14:32:19 -070019#include <linux/suspend.h>
20#include <net/net_namespace.h>
21
22/*
23 * Track transmission rates in buckets (power of 2).
24 * 1,2,4,8...512 seconds.
25 *
26 * Buckets represent the count of network transmissions at least
27 * N seconds apart, where N is 1 << bucket index.
28 */
29#define BUCKET_MAX 10
30
31/* Track network activity frequency */
32static unsigned long activity_stats[BUCKET_MAX];
33static ktime_t last_transmit;
34static ktime_t suspend_time;
35static DEFINE_SPINLOCK(activity_lock);
36
37void activity_stats_update(void)
38{
39 int i;
40 unsigned long flags;
41 ktime_t now;
42 s64 delta;
43
44 spin_lock_irqsave(&activity_lock, flags);
45 now = ktime_get();
46 delta = ktime_to_ns(ktime_sub(now, last_transmit));
47
48 for (i = BUCKET_MAX - 1; i >= 0; i--) {
49 /*
50 * Check if the time delta between network activity is within the
51 * minimum bucket range.
52 */
53 if (delta < (1000000000ULL << i))
54 continue;
55
56 activity_stats[i]++;
57 last_transmit = now;
58 break;
59 }
60 spin_unlock_irqrestore(&activity_lock, flags);
61}
62
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070063static int activity_stats_show(struct seq_file *m, void *v)
Mike Chan1f657852010-05-28 14:32:19 -070064{
65 int i;
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070066 int ret;
Mike Chan1f657852010-05-28 14:32:19 -070067
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070068 seq_printf(m, "Min Bucket(sec) Count\n");
Mike Chan1f657852010-05-28 14:32:19 -070069
70 for (i = 0; i < BUCKET_MAX; i++) {
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070071 ret = seq_printf(m, "%15d %lu\n", 1 << i, activity_stats[i]);
72 if (ret)
73 return ret;
Mike Chan1f657852010-05-28 14:32:19 -070074 }
Mike Chan1f657852010-05-28 14:32:19 -070075
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070076 return 0;
Mike Chan1f657852010-05-28 14:32:19 -070077}
78
79static int activity_stats_notifier(struct notifier_block *nb,
80 unsigned long event, void *dummy)
81{
82 switch (event) {
83 case PM_SUSPEND_PREPARE:
84 suspend_time = ktime_get_real();
85 break;
86
87 case PM_POST_SUSPEND:
88 suspend_time = ktime_sub(ktime_get_real(), suspend_time);
89 last_transmit = ktime_sub(last_transmit, suspend_time);
90 }
91
92 return 0;
93}
94
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -070095static int activity_stats_open(struct inode *inode, struct file *file)
96{
97 return single_open(file, activity_stats_show, PDE_DATA(inode));
98}
99
100static const struct file_operations activity_stats_fops = {
101 .open = activity_stats_open,
102 .read = seq_read,
103 .llseek = seq_lseek,
104 .release = seq_release,
105};
106
Mike Chan1f657852010-05-28 14:32:19 -0700107static struct notifier_block activity_stats_notifier_block = {
108 .notifier_call = activity_stats_notifier,
109};
110
111static int __init activity_stats_init(void)
112{
Arve Hjønnevåg4af1c502013-05-13 20:39:30 -0700113 proc_create("activity", S_IRUGO,
114 init_net.proc_net_stat, &activity_stats_fops);
Mike Chan1f657852010-05-28 14:32:19 -0700115 return register_pm_notifier(&activity_stats_notifier_block);
116}
117
118subsys_initcall(activity_stats_init);
119