trace_cpu_freq_switch.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/percpu.h>
  16. #include <linux/slab.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/ktime.h>
  21. #include <trace/events/power.h>
  22. #include "trace_stat.h"
  23. #include "trace.h"
  24. struct trans {
  25. struct rb_node node;
  26. unsigned int cpu;
  27. unsigned int start_freq;
  28. unsigned int end_freq;
  29. unsigned int min_us;
  30. unsigned int max_us;
  31. ktime_t total_t;
  32. unsigned int count;
  33. };
  34. static struct rb_root freq_trans_tree = RB_ROOT;
  35. static struct trans *tr_search(struct rb_root *root, unsigned int cpu,
  36. unsigned int start_freq, unsigned int end_freq)
  37. {
  38. struct rb_node *node = root->rb_node;
  39. while (node) {
  40. struct trans *tr = container_of(node, struct trans, node);
  41. if (cpu < tr->cpu)
  42. node = node->rb_left;
  43. else if (cpu > tr->cpu)
  44. node = node->rb_right;
  45. else if (start_freq < tr->start_freq)
  46. node = node->rb_left;
  47. else if (start_freq > tr->start_freq)
  48. node = node->rb_right;
  49. else if (end_freq < tr->end_freq)
  50. node = node->rb_left;
  51. else if (end_freq > tr->end_freq)
  52. node = node->rb_right;
  53. else
  54. return tr;
  55. }
  56. return NULL;
  57. }
  58. static int tr_insert(struct rb_root *root, struct trans *tr)
  59. {
  60. struct rb_node **new = &(root->rb_node), *parent = NULL;
  61. while (*new) {
  62. struct trans *this = container_of(*new, struct trans, node);
  63. parent = *new;
  64. if (tr->cpu < this->cpu)
  65. new = &((*new)->rb_left);
  66. else if (tr->cpu > this->cpu)
  67. new = &((*new)->rb_right);
  68. else if (tr->start_freq < this->start_freq)
  69. new = &((*new)->rb_left);
  70. else if (tr->start_freq > this->start_freq)
  71. new = &((*new)->rb_right);
  72. else if (tr->end_freq < this->end_freq)
  73. new = &((*new)->rb_left);
  74. else if (tr->end_freq > this->end_freq)
  75. new = &((*new)->rb_right);
  76. else
  77. return -EINVAL;
  78. }
  79. rb_link_node(&tr->node, parent, new);
  80. rb_insert_color(&tr->node, root);
  81. return 0;
  82. }
  83. struct trans_state {
  84. spinlock_t lock;
  85. unsigned int start_freq;
  86. unsigned int end_freq;
  87. ktime_t start_t;
  88. bool started;
  89. };
  90. static DEFINE_PER_CPU(struct trans_state, freq_trans_state);
  91. static DEFINE_SPINLOCK(state_lock);
  92. static void probe_start(void *ignore, unsigned int start_freq,
  93. unsigned int end_freq, unsigned int cpu)
  94. {
  95. unsigned long flags;
  96. spin_lock_irqsave(&state_lock, flags);
  97. per_cpu(freq_trans_state, cpu).start_freq = start_freq;
  98. per_cpu(freq_trans_state, cpu).end_freq = end_freq;
  99. per_cpu(freq_trans_state, cpu).start_t = ktime_get();
  100. per_cpu(freq_trans_state, cpu).started = true;
  101. spin_unlock_irqrestore(&state_lock, flags);
  102. }
  103. static void probe_end(void *ignore, unsigned int cpu)
  104. {
  105. unsigned long flags;
  106. struct trans *tr;
  107. s64 dur_us;
  108. ktime_t dur_t, end_t = ktime_get();
  109. spin_lock_irqsave(&state_lock, flags);
  110. if (!per_cpu(freq_trans_state, cpu).started)
  111. goto out;
  112. dur_t = ktime_sub(end_t, per_cpu(freq_trans_state, cpu).start_t);
  113. dur_us = ktime_to_us(dur_t);
  114. tr = tr_search(&freq_trans_tree, cpu,
  115. per_cpu(freq_trans_state, cpu).start_freq,
  116. per_cpu(freq_trans_state, cpu).end_freq);
  117. if (!tr) {
  118. tr = kzalloc(sizeof(*tr), GFP_ATOMIC);
  119. if (!tr) {
  120. WARN_ONCE(1, "CPU frequency trace is now invalid!\n");
  121. goto out;
  122. }
  123. tr->start_freq = per_cpu(freq_trans_state, cpu).start_freq;
  124. tr->end_freq = per_cpu(freq_trans_state, cpu).end_freq;
  125. tr->cpu = cpu;
  126. tr->min_us = UINT_MAX;
  127. tr_insert(&freq_trans_tree, tr);
  128. }
  129. tr->total_t = ktime_add(tr->total_t, dur_t);
  130. tr->count++;
  131. if (dur_us > tr->max_us)
  132. tr->max_us = dur_us;
  133. if (dur_us < tr->min_us)
  134. tr->min_us = dur_us;
  135. per_cpu(freq_trans_state, cpu).started = false;
  136. out:
  137. spin_unlock_irqrestore(&state_lock, flags);
  138. }
  139. static void *freq_switch_stat_start(struct tracer_stat *trace)
  140. {
  141. struct rb_node *n;
  142. unsigned long flags;
  143. spin_lock_irqsave(&state_lock, flags);
  144. n = rb_first(&freq_trans_tree);
  145. spin_unlock_irqrestore(&state_lock, flags);
  146. return n;
  147. }
  148. static void *freq_switch_stat_next(void *prev, int idx)
  149. {
  150. struct rb_node *n;
  151. unsigned long flags;
  152. spin_lock_irqsave(&state_lock, flags);
  153. n = rb_next(prev);
  154. spin_unlock_irqrestore(&state_lock, flags);
  155. return n;
  156. }
  157. static int freq_switch_stat_show(struct seq_file *s, void *p)
  158. {
  159. unsigned long flags;
  160. struct trans *tr = p;
  161. spin_lock_irqsave(&state_lock, flags);
  162. seq_printf(s, "%3d %9d %8d %5d %6lld %6d %6d\n", tr->cpu,
  163. tr->start_freq, tr->end_freq, tr->count,
  164. div_s64(ktime_to_us(tr->total_t), tr->count),
  165. tr->min_us, tr->max_us);
  166. spin_unlock_irqrestore(&state_lock, flags);
  167. return 0;
  168. }
  169. static void freq_switch_stat_release(void *stat)
  170. {
  171. struct trans *tr = stat;
  172. unsigned long flags;
  173. spin_lock_irqsave(&state_lock, flags);
  174. rb_erase(&tr->node, &freq_trans_tree);
  175. spin_unlock_irqrestore(&state_lock, flags);
  176. kfree(tr);
  177. }
  178. static int freq_switch_stat_headers(struct seq_file *s)
  179. {
  180. seq_printf(s, "CPU START_KHZ END_KHZ COUNT AVG_US MIN_US MAX_US\n");
  181. seq_printf(s, " | | | | | | |\n");
  182. return 0;
  183. }
  184. struct tracer_stat freq_switch_stats __read_mostly = {
  185. .name = "cpu_freq_switch",
  186. .stat_start = freq_switch_stat_start,
  187. .stat_next = freq_switch_stat_next,
  188. .stat_show = freq_switch_stat_show,
  189. .stat_release = freq_switch_stat_release,
  190. .stat_headers = freq_switch_stat_headers
  191. };
  192. static void trace_freq_switch_disable(void)
  193. {
  194. unregister_stat_tracer(&freq_switch_stats);
  195. unregister_trace_cpu_frequency_switch_end(probe_end, NULL);
  196. unregister_trace_cpu_frequency_switch_start(probe_start, NULL);
  197. pr_info("disabled cpu frequency switch time profiling\n");
  198. }
  199. static int trace_freq_switch_enable(void)
  200. {
  201. int ret;
  202. ret = register_trace_cpu_frequency_switch_start(probe_start, NULL);
  203. if (ret)
  204. goto out;
  205. ret = register_trace_cpu_frequency_switch_end(probe_end, NULL);
  206. if (ret)
  207. goto err_register_switch_end;
  208. ret = register_stat_tracer(&freq_switch_stats);
  209. if (ret)
  210. goto err_register_stat_tracer;
  211. pr_info("enabled cpu frequency switch time profiling\n");
  212. return 0;
  213. err_register_stat_tracer:
  214. unregister_trace_cpu_frequency_switch_end(probe_end, NULL);
  215. err_register_switch_end:
  216. register_trace_cpu_frequency_switch_start(probe_start, NULL);
  217. out:
  218. pr_err("failed to enable cpu frequency switch time profiling\n");
  219. return ret;
  220. }
  221. static DEFINE_MUTEX(debugfs_lock);
  222. static bool trace_freq_switch_enabled;
  223. static int debug_toggle_tracing(void *data, u64 val)
  224. {
  225. int ret = 0;
  226. mutex_lock(&debugfs_lock);
  227. if (val == 1 && !trace_freq_switch_enabled)
  228. ret = trace_freq_switch_enable();
  229. else if (val == 0 && trace_freq_switch_enabled)
  230. trace_freq_switch_disable();
  231. else if (val > 1)
  232. ret = -EINVAL;
  233. if (!ret)
  234. trace_freq_switch_enabled = val;
  235. mutex_unlock(&debugfs_lock);
  236. return ret;
  237. }
  238. static int debug_tracing_state_get(void *data, u64 *val)
  239. {
  240. mutex_lock(&debugfs_lock);
  241. *val = trace_freq_switch_enabled;
  242. mutex_unlock(&debugfs_lock);
  243. return 0;
  244. }
  245. DEFINE_SIMPLE_ATTRIBUTE(debug_tracing_state_fops, debug_tracing_state_get,
  246. debug_toggle_tracing, "%llu\n");
  247. static int __init trace_freq_switch_init(void)
  248. {
  249. struct dentry *d_tracer = tracing_init_dentry();
  250. if (!d_tracer)
  251. return 0;
  252. debugfs_create_file("cpu_freq_switch_profile_enabled",
  253. S_IRUGO | S_IWUSR, d_tracer, NULL, &debug_tracing_state_fops);
  254. return 0;
  255. }
  256. late_initcall(trace_freq_switch_init);