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