blob: 2fc065fb1754fa848ea832332cbc5c07c4c016d8 [file] [log] [blame]
Willy Tarreau7df4f9a2017-08-28 20:44:51 +02001/*
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.
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020010 */
Uwe Kleine-König033692e2018-07-02 22:05:20 +020011
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020012#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;
Kees Cook49404662017-10-25 03:30:01 -070027 struct led_classdev *led_cdev;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020028 u64 last_used;
29 u64 last_boot;
30 int time_left;
31 int state;
32 int invert;
33};
34
Kees Cook49404662017-10-25 03:30:01 -070035static void led_activity_function(struct timer_list *t)
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020036{
Kees Cook49404662017-10-25 03:30:01 -070037 struct activity_data *activity_data = from_timer(activity_data, t,
38 timer);
39 struct led_classdev *led_cdev = activity_data->led_cdev;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020040 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
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020059 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 */
Arnd Bergmannd30c8d22018-06-19 10:18:56 +020076 curr_boot = ktime_get_boot_ns() * cpus;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +020077 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
152static 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
161static 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
179static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
180
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200181static int activity_activate(struct led_classdev *led_cdev)
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200182{
183 struct activity_data *activity_data;
184 int rc;
185
186 activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
187 if (!activity_data)
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200188 return 0;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200189
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);
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200194 return 0;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200195 }
196
Kees Cook49404662017-10-25 03:30:01 -0700197 activity_data->led_cdev = led_cdev;
198 timer_setup(&activity_data->timer, led_activity_function, 0);
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200199 if (!led_cdev->blink_brightness)
200 led_cdev->blink_brightness = led_cdev->max_brightness;
Kees Cook49404662017-10-25 03:30:01 -0700201 led_activity_function(&activity_data->timer);
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200202 set_bit(LED_BLINK_SW, &led_cdev->work_flags);
203 led_cdev->activated = true;
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200204
205 return 0;
Willy Tarreau7df4f9a2017-08-28 20:44:51 +0200206}
207
208static void activity_deactivate(struct led_classdev *led_cdev)
209{
210 struct activity_data *activity_data = led_cdev->trigger_data;
211
212 if (led_cdev->activated) {
213 del_timer_sync(&activity_data->timer);
214 device_remove_file(led_cdev->dev, &dev_attr_invert);
215 kfree(activity_data);
216 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
217 led_cdev->activated = false;
218 }
219}
220
221static struct led_trigger activity_led_trigger = {
222 .name = "activity",
223 .activate = activity_activate,
224 .deactivate = activity_deactivate,
225};
226
227static int activity_reboot_notifier(struct notifier_block *nb,
228 unsigned long code, void *unused)
229{
230 led_trigger_unregister(&activity_led_trigger);
231 return NOTIFY_DONE;
232}
233
234static int activity_panic_notifier(struct notifier_block *nb,
235 unsigned long code, void *unused)
236{
237 panic_detected = 1;
238 return NOTIFY_DONE;
239}
240
241static struct notifier_block activity_reboot_nb = {
242 .notifier_call = activity_reboot_notifier,
243};
244
245static struct notifier_block activity_panic_nb = {
246 .notifier_call = activity_panic_notifier,
247};
248
249static int __init activity_init(void)
250{
251 int rc = led_trigger_register(&activity_led_trigger);
252
253 if (!rc) {
254 atomic_notifier_chain_register(&panic_notifier_list,
255 &activity_panic_nb);
256 register_reboot_notifier(&activity_reboot_nb);
257 }
258 return rc;
259}
260
261static void __exit activity_exit(void)
262{
263 unregister_reboot_notifier(&activity_reboot_nb);
264 atomic_notifier_chain_unregister(&panic_notifier_list,
265 &activity_panic_nb);
266 led_trigger_unregister(&activity_led_trigger);
267}
268
269module_init(activity_init);
270module_exit(activity_exit);
271
272MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
273MODULE_DESCRIPTION("Activity LED trigger");
Uwe Kleine-König033692e2018-07-02 22:05:20 +0200274MODULE_LICENSE("GPL v2");