1
2
3
4
5
6
7
8
9
10
11
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/kernel_stat.h>
15#include <linux/leds.h>
16#include <linux/module.h>
17#include <linux/reboot.h>
18#include <linux/sched.h>
19#include <linux/slab.h>
20#include <linux/timer.h>
21#include "../leds.h"
22
23static int panic_detected;
24
25struct activity_data {
26 struct timer_list timer;
27 struct led_classdev *led_cdev;
28 u64 last_used;
29 u64 last_boot;
30 int time_left;
31 int state;
32 int invert;
33};
34
35static void led_activity_function(struct timer_list *t)
36{
37 struct activity_data *activity_data = from_timer(activity_data, t,
38 timer);
39 struct led_classdev *led_cdev = activity_data->led_cdev;
40 unsigned int target;
41 unsigned int usage;
42 int delay;
43 u64 curr_used;
44 u64 curr_boot;
45 s32 diff_used;
46 s32 diff_boot;
47 int cpus;
48 int i;
49
50 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
51 led_cdev->blink_brightness = led_cdev->new_blink_brightness;
52
53 if (unlikely(panic_detected)) {
54
55 led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
56 return;
57 }
58
59 cpus = 0;
60 curr_used = 0;
61
62 for_each_possible_cpu(i) {
63 curr_used += kcpustat_cpu(i).cpustat[CPUTIME_USER]
64 + kcpustat_cpu(i).cpustat[CPUTIME_NICE]
65 + kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]
66 + kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]
67 + kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
68 cpus++;
69 }
70
71
72
73
74
75
76 curr_boot = ktime_get_boot_ns() * cpus;
77 diff_boot = (curr_boot - activity_data->last_boot) >> 16;
78 diff_used = (curr_used - activity_data->last_used) >> 16;
79 activity_data->last_boot = curr_boot;
80 activity_data->last_used = curr_used;
81
82 if (diff_boot <= 0 || diff_used < 0)
83 usage = 0;
84 else if (diff_used >= diff_boot)
85 usage = 100;
86 else
87 usage = 100 * diff_used / diff_boot;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 activity_data->time_left -= 100;
125 if (activity_data->time_left <= 0) {
126 activity_data->time_left = 0;
127 activity_data->state = !activity_data->state;
128 led_set_brightness_nosleep(led_cdev,
129 (activity_data->state ^ activity_data->invert) ?
130 led_cdev->blink_brightness : LED_OFF);
131 }
132
133 target = (cpus > 1) ? (100 / cpus) : 50;
134
135 if (usage < target)
136 delay = activity_data->state ?
137 10 :
138 990 - 900 * usage / target;
139 else
140 delay = activity_data->state ?
141 10 + 80 * (usage - target) / (100 - target) :
142 90 - 80 * (usage - target) / (100 - target);
143
144
145 if (!activity_data->time_left || delay <= activity_data->time_left)
146 activity_data->time_left = delay;
147
148 delay = min_t(int, activity_data->time_left, 100);
149 mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
150}
151
152static ssize_t led_invert_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
154{
155 struct activity_data *activity_data = led_trigger_get_drvdata(dev);
156
157 return sprintf(buf, "%u\n", activity_data->invert);
158}
159
160static ssize_t led_invert_store(struct device *dev,
161 struct device_attribute *attr,
162 const char *buf, size_t size)
163{
164 struct activity_data *activity_data = led_trigger_get_drvdata(dev);
165 unsigned long state;
166 int ret;
167
168 ret = kstrtoul(buf, 0, &state);
169 if (ret)
170 return ret;
171
172 activity_data->invert = !!state;
173
174 return size;
175}
176
177static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
178
179static struct attribute *activity_led_attrs[] = {
180 &dev_attr_invert.attr,
181 NULL
182};
183ATTRIBUTE_GROUPS(activity_led);
184
185static int activity_activate(struct led_classdev *led_cdev)
186{
187 struct activity_data *activity_data;
188
189 activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
190 if (!activity_data)
191 return -ENOMEM;
192
193 led_set_trigger_data(led_cdev, activity_data);
194
195 activity_data->led_cdev = led_cdev;
196 timer_setup(&activity_data->timer, led_activity_function, 0);
197 if (!led_cdev->blink_brightness)
198 led_cdev->blink_brightness = led_cdev->max_brightness;
199 led_activity_function(&activity_data->timer);
200 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
201
202 return 0;
203}
204
205static void activity_deactivate(struct led_classdev *led_cdev)
206{
207 struct activity_data *activity_data = led_get_trigger_data(led_cdev);
208
209 del_timer_sync(&activity_data->timer);
210 kfree(activity_data);
211 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
212}
213
214static struct led_trigger activity_led_trigger = {
215 .name = "activity",
216 .activate = activity_activate,
217 .deactivate = activity_deactivate,
218 .groups = activity_led_groups,
219};
220
221static int activity_reboot_notifier(struct notifier_block *nb,
222 unsigned long code, void *unused)
223{
224 led_trigger_unregister(&activity_led_trigger);
225 return NOTIFY_DONE;
226}
227
228static int activity_panic_notifier(struct notifier_block *nb,
229 unsigned long code, void *unused)
230{
231 panic_detected = 1;
232 return NOTIFY_DONE;
233}
234
235static struct notifier_block activity_reboot_nb = {
236 .notifier_call = activity_reboot_notifier,
237};
238
239static struct notifier_block activity_panic_nb = {
240 .notifier_call = activity_panic_notifier,
241};
242
243static int __init activity_init(void)
244{
245 int rc = led_trigger_register(&activity_led_trigger);
246
247 if (!rc) {
248 atomic_notifier_chain_register(&panic_notifier_list,
249 &activity_panic_nb);
250 register_reboot_notifier(&activity_reboot_nb);
251 }
252 return rc;
253}
254
255static void __exit activity_exit(void)
256{
257 unregister_reboot_notifier(&activity_reboot_nb);
258 atomic_notifier_chain_unregister(&panic_notifier_list,
259 &activity_panic_nb);
260 led_trigger_unregister(&activity_led_trigger);
261}
262
263module_init(activity_init);
264module_exit(activity_exit);
265
266MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
267MODULE_DESCRIPTION("Activity LED trigger");
268MODULE_LICENSE("GPL v2");
269