1 /*
2  * Activity LED trigger
3  *
4  * Copyright (C) 2017 Willy Tarreau <w@1wt.eu>
5  * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
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 
23 static int panic_detected;
24 
25 struct 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 
35 static 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 		/* full brightness in case of panic */
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 	/* We come here every 100ms in the worst case, so that's 100M ns of
72 	 * cumulated time. By dividing by 2^16, we get the time resolution
73 	 * down to 16us, ensuring we won't overflow 32-bit computations below
74 	 * even up to 3k CPUs, while keeping divides cheap on smaller systems.
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 	 * Now we know the total boot_time multiplied by the number of CPUs, and
91 	 * the total idle+wait time for all CPUs. We'll compare how they evolved
92 	 * since last call. The % of overall CPU usage is :
93 	 *
94 	 *      1 - delta_idle / delta_boot
95 	 *
96 	 * What we want is that when the CPU usage is zero, the LED must blink
97 	 * slowly with very faint flashes that are detectable but not disturbing
98 	 * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
99 	 * blinking frequency to increase up to the point where the load is
100 	 * enough to saturate one core in multi-core systems or 50% in single
101 	 * core systems. At this point it should reach 10 Hz with a 10/90 duty
102 	 * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
103 	 * remains stable (10 Hz) and only the duty cycle increases to report
104 	 * the activity, up to the point where we have 90ms ON, 10ms OFF when
105 	 * all cores are saturated. It's important that the LED never stays in
106 	 * a steady state so that it's easy to distinguish an idle or saturated
107 	 * machine from a hung one.
108 	 *
109 	 * This gives us :
110 	 *   - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
111 	 *     (10ms ON, 90ms OFF)
112 	 *   - below target :
113 	 *      ON_ms  = 10
114 	 *      OFF_ms = 90 + (1 - usage/target) * 900
115 	 *   - above target :
116 	 *      ON_ms  = 10 + (usage-target)/(100%-target) * 80
117 	 *      OFF_ms = 90 - (usage-target)/(100%-target) * 80
118 	 *
119 	 * In order to keep a good responsiveness, we cap the sleep time to
120 	 * 100 ms and keep track of the sleep time left. This allows us to
121 	 * quickly change it if needed.
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 :                        /* ON  */
138 			990 - 900 * usage / target; /* OFF */
139 	else
140 		delay = activity_data->state ?
141 			10 + 80 * (usage - target) / (100 - target) : /* ON  */
142 			90 - 80 * (usage - target) / (100 - target);  /* OFF */
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 
152 static ssize_t led_invert_show(struct device *dev,
153                                struct device_attribute *attr, char *buf)
154 {
155 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
156 	struct activity_data *activity_data = led_cdev->trigger_data;
157 
158 	return sprintf(buf, "%u\n", activity_data->invert);
159 }
160 
161 static ssize_t led_invert_store(struct device *dev,
162                                 struct device_attribute *attr,
163                                 const char *buf, size_t size)
164 {
165 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
166 	struct activity_data *activity_data = led_cdev->trigger_data;
167 	unsigned long state;
168 	int ret;
169 
170 	ret = kstrtoul(buf, 0, &state);
171 	if (ret)
172 		return ret;
173 
174 	activity_data->invert = !!state;
175 
176 	return size;
177 }
178 
179 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
180 
181 static void activity_activate(struct led_classdev *led_cdev)
182 {
183 	struct activity_data *activity_data;
184 	int rc;
185 
186 	activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
187 	if (!activity_data)
188 		return;
189 
190 	led_cdev->trigger_data = activity_data;
191 	rc = device_create_file(led_cdev->dev, &dev_attr_invert);
192 	if (rc) {
193 		kfree(led_cdev->trigger_data);
194 		return;
195 	}
196 
197 	activity_data->led_cdev = led_cdev;
198 	timer_setup(&activity_data->timer, led_activity_function, 0);
199 	if (!led_cdev->blink_brightness)
200 		led_cdev->blink_brightness = led_cdev->max_brightness;
201 	led_activity_function(&activity_data->timer);
202 	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
203 	led_cdev->activated = true;
204 }
205 
206 static void activity_deactivate(struct led_classdev *led_cdev)
207 {
208 	struct activity_data *activity_data = led_cdev->trigger_data;
209 
210 	if (led_cdev->activated) {
211 		del_timer_sync(&activity_data->timer);
212 		device_remove_file(led_cdev->dev, &dev_attr_invert);
213 		kfree(activity_data);
214 		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
215 		led_cdev->activated = false;
216 	}
217 }
218 
219 static struct led_trigger activity_led_trigger = {
220 	.name       = "activity",
221 	.activate   = activity_activate,
222 	.deactivate = activity_deactivate,
223 };
224 
225 static int activity_reboot_notifier(struct notifier_block *nb,
226                                     unsigned long code, void *unused)
227 {
228 	led_trigger_unregister(&activity_led_trigger);
229 	return NOTIFY_DONE;
230 }
231 
232 static int activity_panic_notifier(struct notifier_block *nb,
233                                    unsigned long code, void *unused)
234 {
235 	panic_detected = 1;
236 	return NOTIFY_DONE;
237 }
238 
239 static struct notifier_block activity_reboot_nb = {
240 	.notifier_call = activity_reboot_notifier,
241 };
242 
243 static struct notifier_block activity_panic_nb = {
244 	.notifier_call = activity_panic_notifier,
245 };
246 
247 static int __init activity_init(void)
248 {
249 	int rc = led_trigger_register(&activity_led_trigger);
250 
251 	if (!rc) {
252 		atomic_notifier_chain_register(&panic_notifier_list,
253 					       &activity_panic_nb);
254 		register_reboot_notifier(&activity_reboot_nb);
255 	}
256 	return rc;
257 }
258 
259 static void __exit activity_exit(void)
260 {
261 	unregister_reboot_notifier(&activity_reboot_nb);
262 	atomic_notifier_chain_unregister(&panic_notifier_list,
263 					 &activity_panic_nb);
264 	led_trigger_unregister(&activity_led_trigger);
265 }
266 
267 module_init(activity_init);
268 module_exit(activity_exit);
269 
270 MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
271 MODULE_DESCRIPTION("Activity LED trigger");
272 MODULE_LICENSE("GPL");
273