4 * Copyright (C) 2017 Willy Tarreau <w@1wt.eu>
5 * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
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.
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>
23 static int panic_detected
;
25 struct activity_data
{
26 struct timer_list timer
;
27 struct led_classdev
*led_cdev
;
35 static void led_activity_function(struct timer_list
*t
)
37 struct activity_data
*activity_data
= from_timer(activity_data
, t
,
39 struct led_classdev
*led_cdev
= activity_data
->led_cdev
;
50 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE
, &led_cdev
->work_flags
))
51 led_cdev
->blink_brightness
= led_cdev
->new_blink_brightness
;
53 if (unlikely(panic_detected
)) {
54 /* full brightness in case of panic */
55 led_set_brightness_nosleep(led_cdev
, led_cdev
->blink_brightness
);
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
];
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.
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
;
82 if (diff_boot
<= 0 || diff_used
< 0)
84 else if (diff_used
>= diff_boot
)
87 usage
= 100 * diff_used
/ diff_boot
;
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 :
94 * 1 - delta_idle / delta_boot
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.
110 * - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
111 * (10ms ON, 90ms OFF)
114 * OFF_ms = 90 + (1 - usage/target) * 900
116 * ON_ms = 10 + (usage-target)/(100%-target) * 80
117 * OFF_ms = 90 - (usage-target)/(100%-target) * 80
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.
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
);
133 target
= (cpus
> 1) ? (100 / cpus
) : 50;
136 delay
= activity_data
->state
?
138 990 - 900 * usage
/ target
; /* OFF */
140 delay
= activity_data
->state
?
141 10 + 80 * (usage
- target
) / (100 - target
) : /* ON */
142 90 - 80 * (usage
- target
) / (100 - target
); /* OFF */
145 if (!activity_data
->time_left
|| delay
<= activity_data
->time_left
)
146 activity_data
->time_left
= delay
;
148 delay
= min_t(int, activity_data
->time_left
, 100);
149 mod_timer(&activity_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
152 static ssize_t
led_invert_show(struct device
*dev
,
153 struct device_attribute
*attr
, char *buf
)
155 struct activity_data
*activity_data
= led_trigger_get_drvdata(dev
);
157 return sprintf(buf
, "%u\n", activity_data
->invert
);
160 static ssize_t
led_invert_store(struct device
*dev
,
161 struct device_attribute
*attr
,
162 const char *buf
, size_t size
)
164 struct activity_data
*activity_data
= led_trigger_get_drvdata(dev
);
168 ret
= kstrtoul(buf
, 0, &state
);
172 activity_data
->invert
= !!state
;
177 static DEVICE_ATTR(invert
, 0644, led_invert_show
, led_invert_store
);
179 static struct attribute
*activity_led_attrs
[] = {
180 &dev_attr_invert
.attr
,
183 ATTRIBUTE_GROUPS(activity_led
);
185 static int activity_activate(struct led_classdev
*led_cdev
)
187 struct activity_data
*activity_data
;
189 activity_data
= kzalloc(sizeof(*activity_data
), GFP_KERNEL
);
193 led_set_trigger_data(led_cdev
, activity_data
);
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
);
205 static void activity_deactivate(struct led_classdev
*led_cdev
)
207 struct activity_data
*activity_data
= led_get_trigger_data(led_cdev
);
209 del_timer_sync(&activity_data
->timer
);
210 kfree(activity_data
);
211 clear_bit(LED_BLINK_SW
, &led_cdev
->work_flags
);
214 static struct led_trigger activity_led_trigger
= {
216 .activate
= activity_activate
,
217 .deactivate
= activity_deactivate
,
218 .groups
= activity_led_groups
,
221 static int activity_reboot_notifier(struct notifier_block
*nb
,
222 unsigned long code
, void *unused
)
224 led_trigger_unregister(&activity_led_trigger
);
228 static int activity_panic_notifier(struct notifier_block
*nb
,
229 unsigned long code
, void *unused
)
235 static struct notifier_block activity_reboot_nb
= {
236 .notifier_call
= activity_reboot_notifier
,
239 static struct notifier_block activity_panic_nb
= {
240 .notifier_call
= activity_panic_notifier
,
243 static int __init
activity_init(void)
245 int rc
= led_trigger_register(&activity_led_trigger
);
248 atomic_notifier_chain_register(&panic_notifier_list
,
250 register_reboot_notifier(&activity_reboot_nb
);
255 static void __exit
activity_exit(void)
257 unregister_reboot_notifier(&activity_reboot_nb
);
258 atomic_notifier_chain_unregister(&panic_notifier_list
,
260 led_trigger_unregister(&activity_led_trigger
);
263 module_init(activity_init
);
264 module_exit(activity_exit
);
266 MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
267 MODULE_DESCRIPTION("Activity LED trigger");
268 MODULE_LICENSE("GPL v2");