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
;
40 struct timespec boot_time
;
51 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE
, &led_cdev
->work_flags
))
52 led_cdev
->blink_brightness
= led_cdev
->new_blink_brightness
;
54 if (unlikely(panic_detected
)) {
55 /* full brightness in case of panic */
56 led_set_brightness_nosleep(led_cdev
, led_cdev
->blink_brightness
);
60 get_monotonic_boottime(&boot_time
);
65 for_each_possible_cpu(i
) {
66 curr_used
+= kcpustat_cpu(i
).cpustat
[CPUTIME_USER
]
67 + kcpustat_cpu(i
).cpustat
[CPUTIME_NICE
]
68 + kcpustat_cpu(i
).cpustat
[CPUTIME_SYSTEM
]
69 + kcpustat_cpu(i
).cpustat
[CPUTIME_SOFTIRQ
]
70 + kcpustat_cpu(i
).cpustat
[CPUTIME_IRQ
];
74 /* We come here every 100ms in the worst case, so that's 100M ns of
75 * cumulated time. By dividing by 2^16, we get the time resolution
76 * down to 16us, ensuring we won't overflow 32-bit computations below
77 * even up to 3k CPUs, while keeping divides cheap on smaller systems.
79 curr_boot
= timespec_to_ns(&boot_time
) * cpus
;
80 diff_boot
= (curr_boot
- activity_data
->last_boot
) >> 16;
81 diff_used
= (curr_used
- activity_data
->last_used
) >> 16;
82 activity_data
->last_boot
= curr_boot
;
83 activity_data
->last_used
= curr_used
;
85 if (diff_boot
<= 0 || diff_used
< 0)
87 else if (diff_used
>= diff_boot
)
90 usage
= 100 * diff_used
/ diff_boot
;
93 * Now we know the total boot_time multiplied by the number of CPUs, and
94 * the total idle+wait time for all CPUs. We'll compare how they evolved
95 * since last call. The % of overall CPU usage is :
97 * 1 - delta_idle / delta_boot
99 * What we want is that when the CPU usage is zero, the LED must blink
100 * slowly with very faint flashes that are detectable but not disturbing
101 * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
102 * blinking frequency to increase up to the point where the load is
103 * enough to saturate one core in multi-core systems or 50% in single
104 * core systems. At this point it should reach 10 Hz with a 10/90 duty
105 * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
106 * remains stable (10 Hz) and only the duty cycle increases to report
107 * the activity, up to the point where we have 90ms ON, 10ms OFF when
108 * all cores are saturated. It's important that the LED never stays in
109 * a steady state so that it's easy to distinguish an idle or saturated
110 * machine from a hung one.
113 * - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
114 * (10ms ON, 90ms OFF)
117 * OFF_ms = 90 + (1 - usage/target) * 900
119 * ON_ms = 10 + (usage-target)/(100%-target) * 80
120 * OFF_ms = 90 - (usage-target)/(100%-target) * 80
122 * In order to keep a good responsiveness, we cap the sleep time to
123 * 100 ms and keep track of the sleep time left. This allows us to
124 * quickly change it if needed.
127 activity_data
->time_left
-= 100;
128 if (activity_data
->time_left
<= 0) {
129 activity_data
->time_left
= 0;
130 activity_data
->state
= !activity_data
->state
;
131 led_set_brightness_nosleep(led_cdev
,
132 (activity_data
->state
^ activity_data
->invert
) ?
133 led_cdev
->blink_brightness
: LED_OFF
);
136 target
= (cpus
> 1) ? (100 / cpus
) : 50;
139 delay
= activity_data
->state
?
141 990 - 900 * usage
/ target
; /* OFF */
143 delay
= activity_data
->state
?
144 10 + 80 * (usage
- target
) / (100 - target
) : /* ON */
145 90 - 80 * (usage
- target
) / (100 - target
); /* OFF */
148 if (!activity_data
->time_left
|| delay
<= activity_data
->time_left
)
149 activity_data
->time_left
= delay
;
151 delay
= min_t(int, activity_data
->time_left
, 100);
152 mod_timer(&activity_data
->timer
, jiffies
+ msecs_to_jiffies(delay
));
155 static ssize_t
led_invert_show(struct device
*dev
,
156 struct device_attribute
*attr
, char *buf
)
158 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
159 struct activity_data
*activity_data
= led_cdev
->trigger_data
;
161 return sprintf(buf
, "%u\n", activity_data
->invert
);
164 static ssize_t
led_invert_store(struct device
*dev
,
165 struct device_attribute
*attr
,
166 const char *buf
, size_t size
)
168 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
169 struct activity_data
*activity_data
= led_cdev
->trigger_data
;
173 ret
= kstrtoul(buf
, 0, &state
);
177 activity_data
->invert
= !!state
;
182 static DEVICE_ATTR(invert
, 0644, led_invert_show
, led_invert_store
);
184 static void activity_activate(struct led_classdev
*led_cdev
)
186 struct activity_data
*activity_data
;
189 activity_data
= kzalloc(sizeof(*activity_data
), GFP_KERNEL
);
193 led_cdev
->trigger_data
= activity_data
;
194 rc
= device_create_file(led_cdev
->dev
, &dev_attr_invert
);
196 kfree(led_cdev
->trigger_data
);
200 activity_data
->led_cdev
= led_cdev
;
201 timer_setup(&activity_data
->timer
, led_activity_function
, 0);
202 if (!led_cdev
->blink_brightness
)
203 led_cdev
->blink_brightness
= led_cdev
->max_brightness
;
204 led_activity_function(&activity_data
->timer
);
205 set_bit(LED_BLINK_SW
, &led_cdev
->work_flags
);
206 led_cdev
->activated
= true;
209 static void activity_deactivate(struct led_classdev
*led_cdev
)
211 struct activity_data
*activity_data
= led_cdev
->trigger_data
;
213 if (led_cdev
->activated
) {
214 del_timer_sync(&activity_data
->timer
);
215 device_remove_file(led_cdev
->dev
, &dev_attr_invert
);
216 kfree(activity_data
);
217 clear_bit(LED_BLINK_SW
, &led_cdev
->work_flags
);
218 led_cdev
->activated
= false;
222 static struct led_trigger activity_led_trigger
= {
224 .activate
= activity_activate
,
225 .deactivate
= activity_deactivate
,
228 static int activity_reboot_notifier(struct notifier_block
*nb
,
229 unsigned long code
, void *unused
)
231 led_trigger_unregister(&activity_led_trigger
);
235 static int activity_panic_notifier(struct notifier_block
*nb
,
236 unsigned long code
, void *unused
)
242 static struct notifier_block activity_reboot_nb
= {
243 .notifier_call
= activity_reboot_notifier
,
246 static struct notifier_block activity_panic_nb
= {
247 .notifier_call
= activity_panic_notifier
,
250 static int __init
activity_init(void)
252 int rc
= led_trigger_register(&activity_led_trigger
);
255 atomic_notifier_chain_register(&panic_notifier_list
,
257 register_reboot_notifier(&activity_reboot_nb
);
262 static void __exit
activity_exit(void)
264 unregister_reboot_notifier(&activity_reboot_nb
);
265 atomic_notifier_chain_unregister(&panic_notifier_list
,
267 led_trigger_unregister(&activity_led_trigger
);
270 module_init(activity_init
);
271 module_exit(activity_exit
);
273 MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
274 MODULE_DESCRIPTION("Activity LED trigger");
275 MODULE_LICENSE("GPL");