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 activity_data *activity_data = led_trigger_get_drvdata(dev);
156 
157 	return sprintf(buf, "%u\n", activity_data->invert);
158 }
159 
160 static 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 
177 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
178 
179 static struct attribute *activity_led_attrs[] = {
180 	&dev_attr_invert.attr,
181 	NULL
182 };
183 ATTRIBUTE_GROUPS(activity_led);
184 
185 static 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 
205 static 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 
214 static struct led_trigger activity_led_trigger = {
215 	.name       = "activity",
216 	.activate   = activity_activate,
217 	.deactivate = activity_deactivate,
218 	.groups     = activity_led_groups,
219 };
220 
221 static 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 
228 static 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 
235 static struct notifier_block activity_reboot_nb = {
236 	.notifier_call = activity_reboot_notifier,
237 };
238 
239 static struct notifier_block activity_panic_nb = {
240 	.notifier_call = activity_panic_notifier,
241 };
242 
243 static 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 
255 static 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 
263 module_init(activity_init);
264 module_exit(activity_exit);
265 
266 MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
267 MODULE_DESCRIPTION("Activity LED trigger");
268 MODULE_LICENSE("GPL v2");
269