ledtrig-activity.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Activity LED trigger
  4. *
  5. * Copyright (C) 2017 Willy Tarreau <[email protected]>
  6. * Partially based on Atsushi Nemoto's ledtrig-heartbeat.c.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/kernel_stat.h>
  11. #include <linux/leds.h>
  12. #include <linux/module.h>
  13. #include <linux/panic_notifier.h>
  14. #include <linux/reboot.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/timer.h>
  18. #include "../leds.h"
  19. static int panic_detected;
  20. struct activity_data {
  21. struct timer_list timer;
  22. struct led_classdev *led_cdev;
  23. u64 last_used;
  24. u64 last_boot;
  25. int time_left;
  26. int state;
  27. int invert;
  28. };
  29. static void led_activity_function(struct timer_list *t)
  30. {
  31. struct activity_data *activity_data = from_timer(activity_data, t,
  32. timer);
  33. struct led_classdev *led_cdev = activity_data->led_cdev;
  34. unsigned int target;
  35. unsigned int usage;
  36. int delay;
  37. u64 curr_used;
  38. u64 curr_boot;
  39. s32 diff_used;
  40. s32 diff_boot;
  41. int cpus;
  42. int i;
  43. if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, &led_cdev->work_flags))
  44. led_cdev->blink_brightness = led_cdev->new_blink_brightness;
  45. if (unlikely(panic_detected)) {
  46. /* full brightness in case of panic */
  47. led_set_brightness_nosleep(led_cdev, led_cdev->blink_brightness);
  48. return;
  49. }
  50. cpus = 0;
  51. curr_used = 0;
  52. for_each_possible_cpu(i) {
  53. struct kernel_cpustat kcpustat;
  54. kcpustat_cpu_fetch(&kcpustat, i);
  55. curr_used += kcpustat.cpustat[CPUTIME_USER]
  56. + kcpustat.cpustat[CPUTIME_NICE]
  57. + kcpustat.cpustat[CPUTIME_SYSTEM]
  58. + kcpustat.cpustat[CPUTIME_SOFTIRQ]
  59. + kcpustat.cpustat[CPUTIME_IRQ];
  60. cpus++;
  61. }
  62. /* We come here every 100ms in the worst case, so that's 100M ns of
  63. * cumulated time. By dividing by 2^16, we get the time resolution
  64. * down to 16us, ensuring we won't overflow 32-bit computations below
  65. * even up to 3k CPUs, while keeping divides cheap on smaller systems.
  66. */
  67. curr_boot = ktime_get_boottime_ns() * cpus;
  68. diff_boot = (curr_boot - activity_data->last_boot) >> 16;
  69. diff_used = (curr_used - activity_data->last_used) >> 16;
  70. activity_data->last_boot = curr_boot;
  71. activity_data->last_used = curr_used;
  72. if (diff_boot <= 0 || diff_used < 0)
  73. usage = 0;
  74. else if (diff_used >= diff_boot)
  75. usage = 100;
  76. else
  77. usage = 100 * diff_used / diff_boot;
  78. /*
  79. * Now we know the total boot_time multiplied by the number of CPUs, and
  80. * the total idle+wait time for all CPUs. We'll compare how they evolved
  81. * since last call. The % of overall CPU usage is :
  82. *
  83. * 1 - delta_idle / delta_boot
  84. *
  85. * What we want is that when the CPU usage is zero, the LED must blink
  86. * slowly with very faint flashes that are detectable but not disturbing
  87. * (typically 10ms every second, or 10ms ON, 990ms OFF). Then we want
  88. * blinking frequency to increase up to the point where the load is
  89. * enough to saturate one core in multi-core systems or 50% in single
  90. * core systems. At this point it should reach 10 Hz with a 10/90 duty
  91. * cycle (10ms ON, 90ms OFF). After this point, the blinking frequency
  92. * remains stable (10 Hz) and only the duty cycle increases to report
  93. * the activity, up to the point where we have 90ms ON, 10ms OFF when
  94. * all cores are saturated. It's important that the LED never stays in
  95. * a steady state so that it's easy to distinguish an idle or saturated
  96. * machine from a hung one.
  97. *
  98. * This gives us :
  99. * - a target CPU usage of min(50%, 100%/#CPU) for a 10% duty cycle
  100. * (10ms ON, 90ms OFF)
  101. * - below target :
  102. * ON_ms = 10
  103. * OFF_ms = 90 + (1 - usage/target) * 900
  104. * - above target :
  105. * ON_ms = 10 + (usage-target)/(100%-target) * 80
  106. * OFF_ms = 90 - (usage-target)/(100%-target) * 80
  107. *
  108. * In order to keep a good responsiveness, we cap the sleep time to
  109. * 100 ms and keep track of the sleep time left. This allows us to
  110. * quickly change it if needed.
  111. */
  112. activity_data->time_left -= 100;
  113. if (activity_data->time_left <= 0) {
  114. activity_data->time_left = 0;
  115. activity_data->state = !activity_data->state;
  116. led_set_brightness_nosleep(led_cdev,
  117. (activity_data->state ^ activity_data->invert) ?
  118. led_cdev->blink_brightness : LED_OFF);
  119. }
  120. target = (cpus > 1) ? (100 / cpus) : 50;
  121. if (usage < target)
  122. delay = activity_data->state ?
  123. 10 : /* ON */
  124. 990 - 900 * usage / target; /* OFF */
  125. else
  126. delay = activity_data->state ?
  127. 10 + 80 * (usage - target) / (100 - target) : /* ON */
  128. 90 - 80 * (usage - target) / (100 - target); /* OFF */
  129. if (!activity_data->time_left || delay <= activity_data->time_left)
  130. activity_data->time_left = delay;
  131. delay = min_t(int, activity_data->time_left, 100);
  132. mod_timer(&activity_data->timer, jiffies + msecs_to_jiffies(delay));
  133. }
  134. static ssize_t led_invert_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. struct activity_data *activity_data = led_trigger_get_drvdata(dev);
  138. return sprintf(buf, "%u\n", activity_data->invert);
  139. }
  140. static ssize_t led_invert_store(struct device *dev,
  141. struct device_attribute *attr,
  142. const char *buf, size_t size)
  143. {
  144. struct activity_data *activity_data = led_trigger_get_drvdata(dev);
  145. unsigned long state;
  146. int ret;
  147. ret = kstrtoul(buf, 0, &state);
  148. if (ret)
  149. return ret;
  150. activity_data->invert = !!state;
  151. return size;
  152. }
  153. static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
  154. static struct attribute *activity_led_attrs[] = {
  155. &dev_attr_invert.attr,
  156. NULL
  157. };
  158. ATTRIBUTE_GROUPS(activity_led);
  159. static int activity_activate(struct led_classdev *led_cdev)
  160. {
  161. struct activity_data *activity_data;
  162. activity_data = kzalloc(sizeof(*activity_data), GFP_KERNEL);
  163. if (!activity_data)
  164. return -ENOMEM;
  165. led_set_trigger_data(led_cdev, activity_data);
  166. activity_data->led_cdev = led_cdev;
  167. timer_setup(&activity_data->timer, led_activity_function, 0);
  168. if (!led_cdev->blink_brightness)
  169. led_cdev->blink_brightness = led_cdev->max_brightness;
  170. led_activity_function(&activity_data->timer);
  171. set_bit(LED_BLINK_SW, &led_cdev->work_flags);
  172. return 0;
  173. }
  174. static void activity_deactivate(struct led_classdev *led_cdev)
  175. {
  176. struct activity_data *activity_data = led_get_trigger_data(led_cdev);
  177. del_timer_sync(&activity_data->timer);
  178. kfree(activity_data);
  179. clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
  180. }
  181. static struct led_trigger activity_led_trigger = {
  182. .name = "activity",
  183. .activate = activity_activate,
  184. .deactivate = activity_deactivate,
  185. .groups = activity_led_groups,
  186. };
  187. static int activity_reboot_notifier(struct notifier_block *nb,
  188. unsigned long code, void *unused)
  189. {
  190. led_trigger_unregister(&activity_led_trigger);
  191. return NOTIFY_DONE;
  192. }
  193. static int activity_panic_notifier(struct notifier_block *nb,
  194. unsigned long code, void *unused)
  195. {
  196. panic_detected = 1;
  197. return NOTIFY_DONE;
  198. }
  199. static struct notifier_block activity_reboot_nb = {
  200. .notifier_call = activity_reboot_notifier,
  201. };
  202. static struct notifier_block activity_panic_nb = {
  203. .notifier_call = activity_panic_notifier,
  204. };
  205. static int __init activity_init(void)
  206. {
  207. int rc = led_trigger_register(&activity_led_trigger);
  208. if (!rc) {
  209. atomic_notifier_chain_register(&panic_notifier_list,
  210. &activity_panic_nb);
  211. register_reboot_notifier(&activity_reboot_nb);
  212. }
  213. return rc;
  214. }
  215. static void __exit activity_exit(void)
  216. {
  217. unregister_reboot_notifier(&activity_reboot_nb);
  218. atomic_notifier_chain_unregister(&panic_notifier_list,
  219. &activity_panic_nb);
  220. led_trigger_unregister(&activity_led_trigger);
  221. }
  222. module_init(activity_init);
  223. module_exit(activity_exit);
  224. MODULE_AUTHOR("Willy Tarreau <[email protected]>");
  225. MODULE_DESCRIPTION("Activity LED trigger");
  226. MODULE_LICENSE("GPL v2");