rq_stats.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2015, 2017, 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/cpu.h>
  8. #include <linux/kobject.h>
  9. #include <linux/sysfs.h>
  10. #include <linux/module.h>
  11. #include <trace/hooks/sched.h>
  12. #define MAX_LONG_SIZE 24
  13. #define DEFAULT_DEF_TIMER_JIFFIES 5
  14. struct rq_data {
  15. unsigned long def_timer_jiffies;
  16. unsigned long def_timer_last_jiffy;
  17. int64_t def_start_time;
  18. struct attribute_group *attr_group;
  19. struct kobject *kobj;
  20. struct work_struct def_timer_work;
  21. };
  22. static struct rq_data rq_info;
  23. static struct workqueue_struct *rq_wq;
  24. spinlock_t rq_lock;
  25. static void wakeup_user(void *ignore, void *extra)
  26. {
  27. unsigned long jiffy_gap;
  28. jiffy_gap = jiffies - rq_info.def_timer_last_jiffy;
  29. if (jiffy_gap >= rq_info.def_timer_jiffies) {
  30. rq_info.def_timer_last_jiffy = jiffies;
  31. queue_work(rq_wq, &rq_info.def_timer_work);
  32. }
  33. }
  34. static void def_work_fn(struct work_struct *work)
  35. {
  36. /* Notify polling threads on change of value */
  37. sysfs_notify(rq_info.kobj, NULL, "def_timer_ms");
  38. }
  39. static ssize_t show_def_timer_ms(struct kobject *kobj,
  40. struct kobj_attribute *attr, char *buf)
  41. {
  42. int64_t diff;
  43. unsigned int udiff;
  44. diff = ktime_to_ns(ktime_get()) - rq_info.def_start_time;
  45. do_div(diff, 1000 * 1000);
  46. udiff = (unsigned int) diff;
  47. return scnprintf(buf, MAX_LONG_SIZE, "%u\n", udiff);
  48. }
  49. static ssize_t store_def_timer_ms(struct kobject *kobj,
  50. struct kobj_attribute *attr, const char *buf, size_t count)
  51. {
  52. unsigned int val = 0;
  53. if (kstrtouint(buf, 0, &val))
  54. return -EINVAL;
  55. rq_info.def_timer_jiffies = msecs_to_jiffies(val);
  56. rq_info.def_start_time = ktime_to_ns(ktime_get());
  57. return count;
  58. }
  59. static struct kobj_attribute def_timer_ms_attr =
  60. __ATTR(def_timer_ms, 0600, show_def_timer_ms,
  61. store_def_timer_ms);
  62. static struct attribute *rq_attrs[] = {
  63. &def_timer_ms_attr.attr,
  64. NULL,
  65. };
  66. static struct attribute_group rq_attr_group = {
  67. .attrs = rq_attrs,
  68. };
  69. static int init_rq_attribs(void)
  70. {
  71. int err;
  72. rq_info.attr_group = &rq_attr_group;
  73. /* Create /sys/devices/system/cpu/cpu0/rq-stats/... */
  74. rq_info.kobj = kobject_create_and_add("rq-stats",
  75. &get_cpu_device(0)->kobj);
  76. if (!rq_info.kobj)
  77. return -ENOMEM;
  78. err = sysfs_create_group(rq_info.kobj, rq_info.attr_group);
  79. if (err)
  80. kobject_put(rq_info.kobj);
  81. else
  82. kobject_uevent(rq_info.kobj, KOBJ_ADD);
  83. return err;
  84. }
  85. static int __init msm_rq_stats_init(void)
  86. {
  87. int ret;
  88. #ifndef CONFIG_SMP
  89. /* Bail out if this is not an SMP Target */
  90. return -EPERM;
  91. #endif
  92. rq_wq = create_singlethread_workqueue("rq_stats");
  93. WARN_ON(!rq_wq);
  94. INIT_WORK(&rq_info.def_timer_work, def_work_fn);
  95. spin_lock_init(&rq_lock);
  96. rq_info.def_timer_jiffies = DEFAULT_DEF_TIMER_JIFFIES;
  97. rq_info.def_timer_last_jiffy = 0;
  98. ret = init_rq_attribs();
  99. register_trace_android_vh_jiffies_update(wakeup_user, NULL);
  100. return ret;
  101. }
  102. late_initcall(msm_rq_stats_init);
  103. MODULE_DESCRIPTION("QCOM Run Queue Stats");
  104. MODULE_LICENSE("GPL v2");