18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Activity LED trigger
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Willy Tarreau <w@1wt.eu>
68c2ecf20Sopenharmony_ci * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h>
128c2ecf20Sopenharmony_ci#include <linux/leds.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/reboot.h>
158c2ecf20Sopenharmony_ci#include <linux/sched.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/timer.h>
188c2ecf20Sopenharmony_ci#include "../leds.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int panic_detected;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct activity_data {
238c2ecf20Sopenharmony_ci	struct timer_list timer;
248c2ecf20Sopenharmony_ci	struct led_classdev *led_cdev;
258c2ecf20Sopenharmony_ci	u64 last_used;
268c2ecf20Sopenharmony_ci	u64 last_boot;
278c2ecf20Sopenharmony_ci	int time_left;
288c2ecf20Sopenharmony_ci	int state;
298c2ecf20Sopenharmony_ci	int invert;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic void led_activity_function(struct timer_list *t)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct activity_data *activity_data = from_timer(activity_data, t,
358c2ecf20Sopenharmony_ci							 timer);
368c2ecf20Sopenharmony_ci	struct led_classdev *led_cdev = activity_data->led_cdev;
378c2ecf20Sopenharmony_ci	unsigned int target;
388c2ecf20Sopenharmony_ci	unsigned int usage;
398c2ecf20Sopenharmony_ci	int delay;
408c2ecf20Sopenharmony_ci	u64 curr_used;
418c2ecf20Sopenharmony_ci	u64 curr_boot;
428c2ecf20Sopenharmony_ci	s32 diff_used;
438c2ecf20Sopenharmony_ci	s32 diff_boot;
448c2ecf20Sopenharmony_ci	int cpus;
458c2ecf20Sopenharmony_ci	int i;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
488c2ecf20Sopenharmony_ci		led_cdev->blink_brightness = led_cdev->new_blink_brightness;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (unlikely(panic_detected)) {
518c2ecf20Sopenharmony_ci		/* full brightness in case of panic */
528c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
538c2ecf20Sopenharmony_ci		return;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	cpus = 0;
578c2ecf20Sopenharmony_ci	curr_used = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	for_each_possible_cpu(i) {
608c2ecf20Sopenharmony_ci		struct kernel_cpustat kcpustat;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		kcpustat_cpu_fetch(&kcpustat, i);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		curr_used += kcpustat.cpustat[CPUTIME_USER]
658c2ecf20Sopenharmony_ci			  +  kcpustat.cpustat[CPUTIME_NICE]
668c2ecf20Sopenharmony_ci			  +  kcpustat.cpustat[CPUTIME_SYSTEM]
678c2ecf20Sopenharmony_ci			  +  kcpustat.cpustat[CPUTIME_SOFTIRQ]
688c2ecf20Sopenharmony_ci			  +  kcpustat.cpustat[CPUTIME_IRQ];
698c2ecf20Sopenharmony_ci		cpus++;
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* We come here every 100ms in the worst case, so that's 100M ns of
738c2ecf20Sopenharmony_ci	 * cumulated time. By dividing by 2^16, we get the time resolution
748c2ecf20Sopenharmony_ci	 * down to 16us, ensuring we won't overflow 32-bit computations below
758c2ecf20Sopenharmony_ci	 * even up to 3k CPUs, while keeping divides cheap on smaller systems.
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci	curr_boot = ktime_get_boottime_ns() * cpus;
788c2ecf20Sopenharmony_ci	diff_boot = (curr_boot - activity_data->last_boot) >> 16;
798c2ecf20Sopenharmony_ci	diff_used = (curr_used - activity_data->last_used) >> 16;
808c2ecf20Sopenharmony_ci	activity_data->last_boot = curr_boot;
818c2ecf20Sopenharmony_ci	activity_data->last_used = curr_used;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (diff_boot <= 0 || diff_used < 0)
848c2ecf20Sopenharmony_ci		usage = 0;
858c2ecf20Sopenharmony_ci	else if (diff_used >= diff_boot)
868c2ecf20Sopenharmony_ci		usage = 100;
878c2ecf20Sopenharmony_ci	else
888c2ecf20Sopenharmony_ci		usage = 100 * diff_used / diff_boot;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * Now we know the total boot_time multiplied by the number of CPUs, and
928c2ecf20Sopenharmony_ci	 * the total idle+wait time for all CPUs. We'll compare how they evolved
938c2ecf20Sopenharmony_ci	 * since last call. The % of overall CPU usage is :
948c2ecf20Sopenharmony_ci	 *
958c2ecf20Sopenharmony_ci	 *      1 - delta_idle / delta_boot
968c2ecf20Sopenharmony_ci	 *
978c2ecf20Sopenharmony_ci	 * What we want is that when the CPU usage is zero, the LED must blink
988c2ecf20Sopenharmony_ci	 * slowly with very faint flashes that are detectable but not disturbing
998c2ecf20Sopenharmony_ci	 * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
1008c2ecf20Sopenharmony_ci	 * blinking frequency to increase up to the point where the load is
1018c2ecf20Sopenharmony_ci	 * enough to saturate one core in multi-core systems or 50% in single
1028c2ecf20Sopenharmony_ci	 * core systems. At this point it should reach 10 Hz with a 10/90 duty
1038c2ecf20Sopenharmony_ci	 * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
1048c2ecf20Sopenharmony_ci	 * remains stable (10 Hz) and only the duty cycle increases to report
1058c2ecf20Sopenharmony_ci	 * the activity, up to the point where we have 90ms ON, 10ms OFF when
1068c2ecf20Sopenharmony_ci	 * all cores are saturated. It's important that the LED never stays in
1078c2ecf20Sopenharmony_ci	 * a steady state so that it's easy to distinguish an idle or saturated
1088c2ecf20Sopenharmony_ci	 * machine from a hung one.
1098c2ecf20Sopenharmony_ci	 *
1108c2ecf20Sopenharmony_ci	 * This gives us :
1118c2ecf20Sopenharmony_ci	 *   - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
1128c2ecf20Sopenharmony_ci	 *     (10ms ON, 90ms OFF)
1138c2ecf20Sopenharmony_ci	 *   - below target :
1148c2ecf20Sopenharmony_ci	 *      ON_ms  = 10
1158c2ecf20Sopenharmony_ci	 *      OFF_ms = 90 + (1 - usage/target) * 900
1168c2ecf20Sopenharmony_ci	 *   - above target :
1178c2ecf20Sopenharmony_ci	 *      ON_ms  = 10 + (usage-target)/(100%-target) * 80
1188c2ecf20Sopenharmony_ci	 *      OFF_ms = 90 - (usage-target)/(100%-target) * 80
1198c2ecf20Sopenharmony_ci	 *
1208c2ecf20Sopenharmony_ci	 * In order to keep a good responsiveness, we cap the sleep time to
1218c2ecf20Sopenharmony_ci	 * 100 ms and keep track of the sleep time left. This allows us to
1228c2ecf20Sopenharmony_ci	 * quickly change it if needed.
1238c2ecf20Sopenharmony_ci	 */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	activity_data->time_left -= 100;
1268c2ecf20Sopenharmony_ci	if (activity_data->time_left <= 0) {
1278c2ecf20Sopenharmony_ci		activity_data->time_left = 0;
1288c2ecf20Sopenharmony_ci		activity_data->state = !activity_data->state;
1298c2ecf20Sopenharmony_ci		led_set_brightness_nosleep(led_cdev,
1308c2ecf20Sopenharmony_ci			(activity_data->state ^ activity_data->invert) ?
1318c2ecf20Sopenharmony_ci			led_cdev->blink_brightness : LED_OFF);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	target = (cpus > 1) ? (100 / cpus) : 50;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (usage < target)
1378c2ecf20Sopenharmony_ci		delay = activity_data->state ?
1388c2ecf20Sopenharmony_ci			10 :                        /* ON  */
1398c2ecf20Sopenharmony_ci			990 - 900 * usage / target; /* OFF */
1408c2ecf20Sopenharmony_ci	else
1418c2ecf20Sopenharmony_ci		delay = activity_data->state ?
1428c2ecf20Sopenharmony_ci			10 + 80 * (usage - target) / (100 - target) : /* ON  */
1438c2ecf20Sopenharmony_ci			90 - 80 * (usage - target) / (100 - target);  /* OFF */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (!activity_data->time_left || delay <= activity_data->time_left)
1478c2ecf20Sopenharmony_ci		activity_data->time_left = delay;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	delay = min_t(int, activity_data->time_left, 100);
1508c2ecf20Sopenharmony_ci	mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic ssize_t led_invert_show(struct device *dev,
1548c2ecf20Sopenharmony_ci                               struct device_attribute *attr, char *buf)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct activity_data *activity_data = led_trigger_get_drvdata(dev);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", activity_data->invert);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic ssize_t led_invert_store(struct device *dev,
1628c2ecf20Sopenharmony_ci                                struct device_attribute *attr,
1638c2ecf20Sopenharmony_ci                                const char *buf, size_t size)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	struct activity_data *activity_data = led_trigger_get_drvdata(dev);
1668c2ecf20Sopenharmony_ci	unsigned long state;
1678c2ecf20Sopenharmony_ci	int ret;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	ret = kstrtoul(buf, 0, &state);
1708c2ecf20Sopenharmony_ci	if (ret)
1718c2ecf20Sopenharmony_ci		return ret;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	activity_data->invert = !!state;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	return size;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic struct attribute *activity_led_attrs[] = {
1818c2ecf20Sopenharmony_ci	&dev_attr_invert.attr,
1828c2ecf20Sopenharmony_ci	NULL
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(activity_led);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int activity_activate(struct led_classdev *led_cdev)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct activity_data *activity_data;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
1918c2ecf20Sopenharmony_ci	if (!activity_data)
1928c2ecf20Sopenharmony_ci		return -ENOMEM;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	led_set_trigger_data(led_cdev, activity_data);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	activity_data->led_cdev = led_cdev;
1978c2ecf20Sopenharmony_ci	timer_setup(&activity_data->timer, led_activity_function, 0);
1988c2ecf20Sopenharmony_ci	if (!led_cdev->blink_brightness)
1998c2ecf20Sopenharmony_ci		led_cdev->blink_brightness = led_cdev->max_brightness;
2008c2ecf20Sopenharmony_ci	led_activity_function(&activity_data->timer);
2018c2ecf20Sopenharmony_ci	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return 0;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic void activity_deactivate(struct led_classdev *led_cdev)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct activity_data *activity_data = led_get_trigger_data(led_cdev);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	del_timer_sync(&activity_data->timer);
2118c2ecf20Sopenharmony_ci	kfree(activity_data);
2128c2ecf20Sopenharmony_ci	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic struct led_trigger activity_led_trigger = {
2168c2ecf20Sopenharmony_ci	.name       = "activity",
2178c2ecf20Sopenharmony_ci	.activate   = activity_activate,
2188c2ecf20Sopenharmony_ci	.deactivate = activity_deactivate,
2198c2ecf20Sopenharmony_ci	.groups     = activity_led_groups,
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic int activity_reboot_notifier(struct notifier_block *nb,
2238c2ecf20Sopenharmony_ci                                    unsigned long code, void *unused)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	led_trigger_unregister(&activity_led_trigger);
2268c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int activity_panic_notifier(struct notifier_block *nb,
2308c2ecf20Sopenharmony_ci                                   unsigned long code, void *unused)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	panic_detected = 1;
2338c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic struct notifier_block activity_reboot_nb = {
2378c2ecf20Sopenharmony_ci	.notifier_call = activity_reboot_notifier,
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic struct notifier_block activity_panic_nb = {
2418c2ecf20Sopenharmony_ci	.notifier_call = activity_panic_notifier,
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int __init activity_init(void)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	int rc = led_trigger_register(&activity_led_trigger);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (!rc) {
2498c2ecf20Sopenharmony_ci		atomic_notifier_chain_register(&panic_notifier_list,
2508c2ecf20Sopenharmony_ci					       &activity_panic_nb);
2518c2ecf20Sopenharmony_ci		register_reboot_notifier(&activity_reboot_nb);
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci	return rc;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic void __exit activity_exit(void)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	unregister_reboot_notifier(&activity_reboot_nb);
2598c2ecf20Sopenharmony_ci	atomic_notifier_chain_unregister(&panic_notifier_list,
2608c2ecf20Sopenharmony_ci					 &activity_panic_nb);
2618c2ecf20Sopenharmony_ci	led_trigger_unregister(&activity_led_trigger);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cimodule_init(activity_init);
2658c2ecf20Sopenharmony_cimodule_exit(activity_exit);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ciMODULE_AUTHOR("Willy Tarreau <w@1wt.eu>");
2688c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Activity LED trigger");
2698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
270