ledtrig-activity.c 8.0 KB

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