video: rockchip: hdmi: fix certain tv blank when mode switch
[firefly-linux-kernel-4.4.55.git] / net / activity_stats.c
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
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #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 */
32 static unsigned long activity_stats[BUCKET_MAX];
33 static ktime_t last_transmit;
34 static ktime_t suspend_time;
35 static DEFINE_SPINLOCK(activity_lock);
36
37 void 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
63 static int activity_stats_show(struct seq_file *m, void *v)
64 {
65         int i;
66
67         seq_printf(m, "Min Bucket(sec) Count\n");
68
69         for (i = 0; i < BUCKET_MAX; i++) {
70                 seq_printf(m, "%15d %lu\n", 1 << i, activity_stats[i]);
71                 if (seq_has_overflowed(m))
72                         return -ENOSPC;
73         }
74
75         return 0;
76 }
77
78 static int activity_stats_notifier(struct notifier_block *nb,
79                                         unsigned long event, void *dummy)
80 {
81         switch (event) {
82                 case PM_SUSPEND_PREPARE:
83                         suspend_time = ktime_get_real();
84                         break;
85
86                 case PM_POST_SUSPEND:
87                         suspend_time = ktime_sub(ktime_get_real(), suspend_time);
88                         last_transmit = ktime_sub(last_transmit, suspend_time);
89         }
90
91         return 0;
92 }
93
94 static int activity_stats_open(struct inode *inode, struct file *file)
95 {
96         return single_open(file, activity_stats_show, PDE_DATA(inode));
97 }
98
99 static const struct file_operations activity_stats_fops = {
100         .open           = activity_stats_open,
101         .read           = seq_read,
102         .llseek         = seq_lseek,
103         .release        = seq_release,
104 };
105
106 static struct notifier_block activity_stats_notifier_block = {
107         .notifier_call = activity_stats_notifier,
108 };
109
110 static int  __init activity_stats_init(void)
111 {
112         proc_create("activity", S_IRUGO,
113                     init_net.proc_net_stat, &activity_stats_fops);
114         return register_pm_notifier(&activity_stats_notifier_block);
115 }
116
117 subsys_initcall(activity_stats_init);
118