1 // SPDX-License-Identifier: GPL-2.0-only
6 * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
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>
20 static int panic_detected;
22 struct activity_data {
23 struct timer_list timer;
24 struct led_classdev *led_cdev;
32 static void led_activity_function(struct timer_list *t)
34 struct activity_data *activity_data = from_timer(activity_data, t,
36 struct led_classdev *led_cdev = activity_data->led_cdev;
47 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
48 led_cdev->blink_brightness = led_cdev->new_blink_brightness;
50 if (unlikely(panic_detected)) {
51 /* full brightness in case of panic */
52 led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
59 for_each_possible_cpu(i) {
60 struct kernel_cpustat kcpustat;
62 kcpustat_cpu_fetch(&kcpustat, i);
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];
72 /* We come here every 100ms in the worst case, so that's 100M ns of
73 * cumulated time. By dividing by 2^16, we get the time resolution
74 * down to 16us, ensuring we won't overflow 32-bit computations below
75 * even up to 3k CPUs, while keeping divides cheap on smaller systems.
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;
83 if (diff_boot <= 0 || diff_used < 0)
85 else if (diff_used >= diff_boot)
88 usage = 100 * diff_used / diff_boot;
91 * Now we know the total boot_time multiplied by the number of CPUs, and
92 * the total idle+wait time for all CPUs. We'll compare how they evolved
93 * since last call. The % of overall CPU usage is :
95 * 1 - delta_idle / delta_boot
97 * What we want is that when the CPU usage is zero, the LED must blink
98 * slowly with very faint flashes that are detectable but not disturbing
99 * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
100 * blinking frequency to increase up to the point where the load is
101 * enough to saturate one core in multi-core systems or 50% in single
102 * core systems. At this point it should reach 10 Hz with a 10/90 duty
103 * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
104 * remains stable (10 Hz) and only the duty cycle increases to report
105 * the activity, up to the point where we have 90ms ON, 10ms OFF when
106 * all cores are saturated. It's important that the LED never stays in
107 * a steady state so that it's easy to distinguish an idle or saturated
108 * machine from a hung one.
111 * - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
112 * (10ms ON, 90ms OFF)
115 * OFF_ms = 90 + (1 - usage/target) * 900
117 * ON_ms = 10 + (usage-target)/(100%-target) * 80
118 * OFF_ms = 90 - (usage-target)/(100%-target) * 80
120 * In order to keep a good responsiveness, we cap the sleep time to
121 * 100 ms and keep track of the sleep time left. This allows us to
122 * quickly change it if needed.
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);
134 target = (cpus > 1) ? (100 / cpus) : 50;
137 delay = activity_data->state ?
139 990 - 900 * usage / target; /* OFF */
141 delay = activity_data->state ?
142 10 + 80 * (usage - target) / (100 - target) : /* ON */
143 90 - 80 * (usage - target) / (100 - target); /* OFF */
146 if (!activity_data->time_left || delay <= activity_data->time_left)
147 activity_data->time_left = delay;
149 delay = min_t(int, activity_data->time_left, 100);
150 mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
153 static ssize_t led_invert_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
156 struct activity_data *activity_data = led_trigger_get_drvdata(dev);
158 return sprintf(buf, "%u\n", activity_data->invert);
161 static ssize_t led_invert_store(struct device *dev,
162 struct device_attribute *attr,
163 const char *buf, size_t size)
165 struct activity_data *activity_data = led_trigger_get_drvdata(dev);
169 ret = kstrtoul(buf, 0, &state);
173 activity_data->invert = !!state;
178 static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
180 static struct attribute *activity_led_attrs[] = {
181 &dev_attr_invert.attr,
184 ATTRIBUTE_GROUPS(activity_led);
186 static int activity_activate(struct led_classdev *led_cdev)
188 struct activity_data *activity_data;
190 activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
194 led_set_trigger_data(led_cdev, activity_data);
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);
206 static void activity_deactivate(struct led_classdev *led_cdev)
208 struct activity_data *activity_data = led_get_trigger_data(led_cdev);
210 del_timer_sync(&activity_data->timer);
211 kfree(activity_data);
212 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
215 static struct led_trigger activity_led_trigger = {
217 .activate = activity_activate,
218 .deactivate = activity_deactivate,
219 .groups = activity_led_groups,
222 static int activity_reboot_notifier(struct notifier_block *nb,
223 unsigned long code, void *unused)
225 led_trigger_unregister(&activity_led_trigger);
229 static int activity_panic_notifier(struct notifier_block *nb,
230 unsigned long code, void *unused)
236 static struct notifier_block activity_reboot_nb = {
237 .notifier_call = activity_reboot_notifier,
240 static struct notifier_block activity_panic_nb = {
241 .notifier_call = activity_panic_notifier,
244 static int __init activity_init(void)
246 int rc = led_trigger_register(&activity_led_trigger);
249 atomic_notifier_chain_register(&panic_notifier_list,
251 register_reboot_notifier(&activity_reboot_nb);
256 static void __exit activity_exit(void)
258 unregister_reboot_notifier(&activity_reboot_nb);
259 atomic_notifier_chain_unregister(&panic_notifier_list,
261 led_trigger_unregister(&activity_led_trigger);
264 module_init(activity_init);
265 module_exit(activity_exit);
268 MODULE_DESCRIPTION("Activity LED trigger");
269 MODULE_LICENSE("GPL v2");