|
| 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 | + u64 last_used; |
| 28 | + u64 last_boot; |
| 29 | + int time_left; |
| 30 | + int state; |
| 31 | + int invert; |
| 32 | +}; |
| 33 | + |
| 34 | +static void led_activity_function(unsigned long data) |
| 35 | +{ |
| 36 | + struct led_classdev *led_cdev = (struct led_classdev *)data; |
| 37 | + struct activity_data *activity_data = led_cdev->trigger_data; |
| 38 | + struct timespec boot_time; |
| 39 | + unsigned int target; |
| 40 | + unsigned int usage; |
| 41 | + int delay; |
| 42 | + u64 curr_used; |
| 43 | + u64 curr_boot; |
| 44 | + s32 diff_used; |
| 45 | + s32 diff_boot; |
| 46 | + int cpus; |
| 47 | + int i; |
| 48 | + |
| 49 | + if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags)) |
| 50 | + led_cdev->blink_brightness = led_cdev->new_blink_brightness; |
| 51 | + |
| 52 | + if (unlikely(panic_detected)) { |
| 53 | + /* full brightness in case of panic */ |
| 54 | + led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + get_monotonic_boottime(&boot_time); |
| 59 | + |
| 60 | + cpus = 0; |
| 61 | + curr_used = 0; |
| 62 | + |
| 63 | + for_each_possible_cpu(i) { |
| 64 | + curr_used += kcpustat_cpu(i).cpustat[CPUTIME_USER] |
| 65 | + + kcpustat_cpu(i).cpustat[CPUTIME_NICE] |
| 66 | + + kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM] |
| 67 | + + kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ] |
| 68 | + + kcpustat_cpu(i).cpustat[CPUTIME_IRQ]; |
| 69 | + cpus++; |
| 70 | + } |
| 71 | + |
| 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. |
| 76 | + */ |
| 77 | + curr_boot = timespec_to_ns(&boot_time) * 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; |
| 82 | + |
| 83 | + if (diff_boot <= 0 || diff_used < 0) |
| 84 | + usage = 0; |
| 85 | + else if (diff_used >= diff_boot) |
| 86 | + usage = 100; |
| 87 | + else |
| 88 | + usage = 100 * diff_used / diff_boot; |
| 89 | + |
| 90 | + /* |
| 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 : |
| 94 | + * |
| 95 | + * 1 - delta_idle / delta_boot |
| 96 | + * |
| 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. |
| 109 | + * |
| 110 | + * This gives us : |
| 111 | + * - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle |
| 112 | + * (10ms ON, 90ms OFF) |
| 113 | + * - below target : |
| 114 | + * ON_ms = 10 |
| 115 | + * OFF_ms = 90 + (1 - usage/target) * 900 |
| 116 | + * - above target : |
| 117 | + * ON_ms = 10 + (usage-target)/(100%-target) * 80 |
| 118 | + * OFF_ms = 90 - (usage-target)/(100%-target) * 80 |
| 119 | + * |
| 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. |
| 123 | + */ |
| 124 | + |
| 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); |
| 132 | + } |
| 133 | + |
| 134 | + target = (cpus > 1) ? (100 / cpus) : 50; |
| 135 | + |
| 136 | + if (usage < target) |
| 137 | + delay = activity_data->state ? |
| 138 | + 10 : /* ON */ |
| 139 | + 990 - 900 * usage / target; /* OFF */ |
| 140 | + else |
| 141 | + delay = activity_data->state ? |
| 142 | + 10 + 80 * (usage - target) / (100 - target) : /* ON */ |
| 143 | + 90 - 80 * (usage - target) / (100 - target); /* OFF */ |
| 144 | + |
| 145 | + |
| 146 | + if (!activity_data->time_left || delay <= activity_data->time_left) |
| 147 | + activity_data->time_left = delay; |
| 148 | + |
| 149 | + delay = min_t(int, activity_data->time_left, 100); |
| 150 | + mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay)); |
| 151 | +} |
| 152 | + |
| 153 | +static ssize_t led_invert_show(struct device *dev, |
| 154 | + struct device_attribute *attr, char *buf) |
| 155 | +{ |
| 156 | + struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 157 | + struct activity_data *activity_data = led_cdev->trigger_data; |
| 158 | + |
| 159 | + return sprintf(buf, "%u\n", activity_data->invert); |
| 160 | +} |
| 161 | + |
| 162 | +static ssize_t led_invert_store(struct device *dev, |
| 163 | + struct device_attribute *attr, |
| 164 | + const char *buf, size_t size) |
| 165 | +{ |
| 166 | + struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 167 | + struct activity_data *activity_data = led_cdev->trigger_data; |
| 168 | + unsigned long state; |
| 169 | + int ret; |
| 170 | + |
| 171 | + ret = kstrtoul(buf, 0, &state); |
| 172 | + if (ret) |
| 173 | + return ret; |
| 174 | + |
| 175 | + activity_data->invert = !!state; |
| 176 | + |
| 177 | + return size; |
| 178 | +} |
| 179 | + |
| 180 | +static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store); |
| 181 | + |
| 182 | +static void activity_activate(struct led_classdev *led_cdev) |
| 183 | +{ |
| 184 | + struct activity_data *activity_data; |
| 185 | + int rc; |
| 186 | + |
| 187 | + activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL); |
| 188 | + if (!activity_data) |
| 189 | + return; |
| 190 | + |
| 191 | + led_cdev->trigger_data = activity_data; |
| 192 | + rc = device_create_file(led_cdev->dev, &dev_attr_invert); |
| 193 | + if (rc) { |
| 194 | + kfree(led_cdev->trigger_data); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + setup_timer(&activity_data->timer, |
| 199 | + led_activity_function, (unsigned long)led_cdev); |
| 200 | + if (!led_cdev->blink_brightness) |
| 201 | + led_cdev->blink_brightness = led_cdev->max_brightness; |
| 202 | + led_activity_function(activity_data->timer.data); |
| 203 | + set_bit(LED_BLINK_SW, &led_cdev->work_flags); |
| 204 | + led_cdev->activated = true; |
| 205 | +} |
| 206 | + |
| 207 | +static void activity_deactivate(struct led_classdev *led_cdev) |
| 208 | +{ |
| 209 | + struct activity_data *activity_data = led_cdev->trigger_data; |
| 210 | + |
| 211 | + if (led_cdev->activated) { |
| 212 | + del_timer_sync(&activity_data->timer); |
| 213 | + device_remove_file(led_cdev->dev, &dev_attr_invert); |
| 214 | + kfree(activity_data); |
| 215 | + clear_bit(LED_BLINK_SW, &led_cdev->work_flags); |
| 216 | + led_cdev->activated = false; |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +static struct led_trigger activity_led_trigger = { |
| 221 | + .name = "activity", |
| 222 | + .activate = activity_activate, |
| 223 | + .deactivate = activity_deactivate, |
| 224 | +}; |
| 225 | + |
| 226 | +static int activity_reboot_notifier(struct notifier_block *nb, |
| 227 | + unsigned long code, void *unused) |
| 228 | +{ |
| 229 | + led_trigger_unregister(&activity_led_trigger); |
| 230 | + return NOTIFY_DONE; |
| 231 | +} |
| 232 | + |
| 233 | +static int activity_panic_notifier(struct notifier_block *nb, |
| 234 | + unsigned long code, void *unused) |
| 235 | +{ |
| 236 | + panic_detected = 1; |
| 237 | + return NOTIFY_DONE; |
| 238 | +} |
| 239 | + |
| 240 | +static struct notifier_block activity_reboot_nb = { |
| 241 | + .notifier_call = activity_reboot_notifier, |
| 242 | +}; |
| 243 | + |
| 244 | +static struct notifier_block activity_panic_nb = { |
| 245 | + .notifier_call = activity_panic_notifier, |
| 246 | +}; |
| 247 | + |
| 248 | +static int __init activity_init(void) |
| 249 | +{ |
| 250 | + int rc = led_trigger_register(&activity_led_trigger); |
| 251 | + |
| 252 | + if (!rc) { |
| 253 | + atomic_notifier_chain_register(&panic_notifier_list, |
| 254 | + &activity_panic_nb); |
| 255 | + register_reboot_notifier(&activity_reboot_nb); |
| 256 | + } |
| 257 | + return rc; |
| 258 | +} |
| 259 | + |
| 260 | +static void __exit activity_exit(void) |
| 261 | +{ |
| 262 | + unregister_reboot_notifier(&activity_reboot_nb); |
| 263 | + atomic_notifier_chain_unregister(&panic_notifier_list, |
| 264 | + &activity_panic_nb); |
| 265 | + led_trigger_unregister(&activity_led_trigger); |
| 266 | +} |
| 267 | + |
| 268 | +module_init(activity_init); |
| 269 | +module_exit(activity_exit); |
| 270 | + |
| 271 | +MODULE_AUTHOR("Willy Tarreau <w@1wt.eu>"); |
| 272 | +MODULE_DESCRIPTION("Activity LED trigger"); |
| 273 | +MODULE_LICENSE("GPL"); |
0 commit comments