rt_monitor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include <linux/slab.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/sched.h>
  4. #include <linux/seq_file.h>
  5. #include <linux/kallsyms.h>
  6. #include <linux/utsname.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/kernel_stat.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/tick.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/list_sort.h>
  13. #include "mt_sched_mon.h"
  14. #define MAX_THROTTLE_COUNT 5
  15. #ifdef CONFIG_MT_ENG_BUILD
  16. #define MAX_THREAD_COUNT 50000
  17. /* max debug thread count, if reach the level, stop store new thread informaiton*/
  18. #define MAX_TIME (5*60*60)
  19. /* max debug time, if reach the level, stop and clear the debug information*/
  20. #else
  21. #define MAX_THREAD_COUNT 10000
  22. /* max debug thread count, if reach the level, stop store new thread informaiton*/
  23. #define MAX_TIME (1*60*60)
  24. /* max debug time, if reach the level, stop and clear the debug information*/
  25. #endif
  26. struct mt_rt_mon_struct {
  27. struct list_head list;
  28. pid_t pid;
  29. int prio;
  30. char comm[TASK_COMM_LEN];
  31. u64 cputime;
  32. u64 cputime_init;
  33. u64 cost_cputime;
  34. u32 cputime_percen_6;
  35. u64 isr_time;
  36. u64 cost_isrtime;
  37. };
  38. static struct mt_rt_mon_struct mt_rt_mon_head = {
  39. .list = LIST_HEAD_INIT(mt_rt_mon_head.list),
  40. };
  41. static int mt_rt_mon_enabled;
  42. static int rt_mon_count;
  43. static unsigned long long rt_start_ts, rt_end_ts, rt_dur_ts;
  44. static DEFINE_SPINLOCK(mt_rt_mon_lock);
  45. static struct mt_rt_mon_struct buffer[MAX_THROTTLE_COUNT];
  46. static int rt_mon_count_buffer;
  47. static unsigned long long rt_start_ts_buffer, rt_end_ts_buffer, rt_dur_ts_buffer;
  48. /*
  49. * Ease the printing of nsec fields:
  50. */
  51. static long long nsec_high(unsigned long long nsec)
  52. {
  53. if ((long long)nsec < 0) {
  54. nsec = -nsec;
  55. do_div(nsec, 1000000);
  56. return -nsec;
  57. }
  58. do_div(nsec, 1000000);
  59. return nsec;
  60. }
  61. #define SPLIT_NS_H(x) nsec_high(x)
  62. static unsigned long nsec_low(unsigned long long nsec)
  63. {
  64. if ((long long)nsec < 0)
  65. nsec = -nsec;
  66. return do_div(nsec, 1000000);
  67. }
  68. #define SPLIT_NS_L(x) nsec_low(x)
  69. static void store_rt_mon_info(struct task_struct *p)
  70. {
  71. struct mt_rt_mon_struct *mtmon;
  72. unsigned long irq_flags;
  73. mtmon = kmalloc(sizeof(struct mt_rt_mon_struct), GFP_ATOMIC);
  74. if (!mtmon)
  75. return;
  76. memset(mtmon, 0, sizeof(struct mt_rt_mon_struct));
  77. INIT_LIST_HEAD(&(mtmon->list));
  78. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  79. rt_mon_count++;
  80. mtmon->pid = p->pid;
  81. mtmon->prio = p->prio;
  82. strcpy(mtmon->comm, p->comm);
  83. mtmon->cputime = p->se.sum_exec_runtime;
  84. mtmon->cputime_init = p->se.sum_exec_runtime;
  85. mtmon->isr_time = p->se.mtk_isr_time;
  86. mtmon->cost_cputime = 0;
  87. mtmon->cost_isrtime = 0;
  88. list_add(&(mtmon->list), &(mt_rt_mon_head.list));
  89. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  90. }
  91. void setup_mt_rt_mon_info(struct task_struct *p)
  92. {
  93. struct mt_rt_mon_struct *tmp;
  94. int find = 0;
  95. unsigned long irq_flags;
  96. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  97. if (0 == mt_rt_mon_enabled) {
  98. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  99. return;
  100. }
  101. if (rt_mon_count >= MAX_THREAD_COUNT) {
  102. pr_err("mtmon thread count larger the max level %d.\n",
  103. MAX_THREAD_COUNT);
  104. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  105. return;
  106. }
  107. list_for_each_entry(tmp, &mt_rt_mon_head.list, list) {
  108. if (!find && (tmp->pid == p->pid)) {
  109. tmp->prio = p->prio;
  110. strcpy(tmp->comm, p->comm);
  111. tmp->cputime = p->se.sum_exec_runtime;
  112. tmp->cputime_init = p->se.sum_exec_runtime;
  113. tmp->isr_time = p->se.mtk_isr_time;
  114. find = 1;
  115. }
  116. }
  117. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  118. if (!find)
  119. store_rt_mon_info(p);
  120. }
  121. void start_rt_mon_task(void)
  122. {
  123. struct task_struct *g, *p;
  124. unsigned long flags, irq_flags;
  125. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  126. mt_rt_mon_enabled = 1;
  127. rt_start_ts = sched_clock();
  128. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  129. read_lock_irqsave(&tasklist_lock, flags);
  130. do_each_thread(g, p) {
  131. if (!task_has_rt_policy(p))
  132. continue;
  133. setup_mt_rt_mon_info(p);
  134. } while_each_thread(g, p);
  135. read_unlock_irqrestore(&tasklist_lock, flags);
  136. }
  137. void stop_rt_mon_task(void)
  138. {
  139. struct mt_rt_mon_struct *tmp;
  140. struct task_struct *tsk;
  141. unsigned long long cost_cputime = 0;
  142. unsigned long irq_flags;
  143. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  144. mt_rt_mon_enabled = 0;
  145. rt_end_ts = sched_clock();
  146. rt_dur_ts = rt_end_ts - rt_start_ts;
  147. do_div(rt_dur_ts, 1000000); /* put prof_dur_ts to ms */
  148. list_for_each_entry(tmp, &mt_rt_mon_head.list, list) {
  149. tsk = find_task_by_vpid(tmp->pid);
  150. if (tsk && task_has_rt_policy(tsk)) {
  151. tmp->cputime = tsk->se.sum_exec_runtime;
  152. tmp->isr_time =
  153. tsk->se.mtk_isr_time - tmp->isr_time;
  154. tmp->cost_isrtime += tmp->isr_time;
  155. strcpy(tmp->comm, tsk->comm);
  156. tmp->prio = tsk->prio;
  157. }
  158. if (tmp->cputime >= (tmp->cputime_init + tmp->cost_isrtime)) {
  159. cost_cputime =
  160. tmp->cputime - tmp->cost_isrtime - tmp->cputime_init;
  161. tmp->cost_cputime += cost_cputime;
  162. if (rt_dur_ts == 0)
  163. cost_cputime = 0;
  164. else
  165. do_div(cost_cputime, rt_dur_ts);
  166. tmp->cputime_percen_6 = cost_cputime;
  167. } else {
  168. tmp->cost_cputime = 0;
  169. tmp->cputime_percen_6 = 0;
  170. tmp->cost_isrtime = 0;
  171. }
  172. }
  173. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  174. }
  175. void reset_rt_mon_list(void)
  176. {
  177. struct mt_rt_mon_struct *tmp, *tmp2;
  178. struct task_struct *tsk;
  179. unsigned long irq_flags;
  180. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  181. mt_rt_mon_enabled = 0;
  182. list_for_each_entry_safe(tmp, tmp2, &mt_rt_mon_head.list, list) {
  183. tsk = find_task_by_vpid(tmp->pid);
  184. if (tsk && task_has_rt_policy(tsk)) {
  185. tmp->cost_cputime = 0;
  186. tmp->cputime_percen_6 = 0;
  187. tmp->cost_isrtime = 0;
  188. tmp->prio = tsk->prio;
  189. tmp->cputime_init = tsk->se.sum_exec_runtime;
  190. tmp->isr_time = tsk->se.mtk_isr_time;
  191. } else {
  192. rt_mon_count--;
  193. list_del(&(tmp->list));
  194. kfree(tmp);
  195. }
  196. }
  197. rt_end_ts = 0;
  198. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  199. }
  200. static int mt_rt_mon_cmp(void *priv, struct list_head *a, struct list_head *b)
  201. {
  202. struct mt_rt_mon_struct *mon_a, *mon_b;
  203. mon_a = list_entry(a, struct mt_rt_mon_struct, list);
  204. mon_b = list_entry(b, struct mt_rt_mon_struct, list);
  205. if (mon_a->cost_cputime > mon_b->cost_cputime)
  206. return -1;
  207. if (mon_a->cost_cputime < mon_b->cost_cputime)
  208. return 1;
  209. return 0;
  210. }
  211. void mt_rt_mon_print_task(void)
  212. {
  213. unsigned long irq_flags;
  214. int count = 0;
  215. struct mt_rt_mon_struct *tmp;
  216. rt_mon_count_buffer = rt_mon_count;
  217. rt_start_ts_buffer = rt_start_ts;
  218. rt_end_ts_buffer = rt_end_ts;
  219. rt_dur_ts_buffer = rt_dur_ts;
  220. pr_err(
  221. "sched: mon_count = %d monitor start[%lld.%06lu] end[%lld.%06lu] dur[%lld.%06lu]\n",
  222. rt_mon_count, SPLIT_NS_H(rt_start_ts), SPLIT_NS_L(rt_start_ts),
  223. SPLIT_NS_H(rt_end_ts), SPLIT_NS_L(rt_end_ts),
  224. SPLIT_NS_H((rt_end_ts - rt_start_ts)),
  225. SPLIT_NS_L((rt_end_ts - rt_start_ts)));
  226. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  227. list_sort(NULL, &mt_rt_mon_head.list, mt_rt_mon_cmp);
  228. list_for_each_entry(tmp, &mt_rt_mon_head.list, list) {
  229. memcpy(&buffer[count], tmp, sizeof(struct mt_rt_mon_struct));
  230. count++;
  231. pr_err("sched:[%s] pid:%d prio:%d cputime[%lld.%06lu] percen[%d.%04d%%] isr_time[%lld.%06lu]\n",
  232. tmp->comm, tmp->pid, tmp->prio,
  233. SPLIT_NS_H(tmp->cost_cputime), SPLIT_NS_L(tmp->cost_cputime),
  234. tmp->cputime_percen_6 / 10000, tmp->cputime_percen_6 % 10000,
  235. SPLIT_NS_H(tmp->isr_time), SPLIT_NS_L(tmp->isr_time));
  236. if (count == MAX_THROTTLE_COUNT)
  237. break;
  238. }
  239. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  240. }
  241. void mt_rt_mon_print_task_from_buffer(void)
  242. {
  243. int i;
  244. pr_err("last throttle information start\n");
  245. pr_err("sched: mon_count = %d monitor start[%lld.%06lu] end[%lld.%06lu] dur[%lld.%06lu]\n",
  246. rt_mon_count_buffer, SPLIT_NS_H(rt_start_ts_buffer), SPLIT_NS_L(rt_start_ts_buffer),
  247. SPLIT_NS_H(rt_end_ts_buffer), SPLIT_NS_L(rt_end_ts_buffer),
  248. SPLIT_NS_H((rt_end_ts_buffer - rt_start_ts_buffer)),
  249. SPLIT_NS_L((rt_end_ts_buffer - rt_start_ts_buffer)));
  250. for (i = 0 ; i < MAX_THROTTLE_COUNT ; i++) {
  251. pr_err("sched:[%s] pid:%d prio:%d cputime[%lld.%06lu] percen[%d.%04d%%] isr_time[%lld.%06lu]\n",
  252. buffer[i].comm, buffer[i].pid, buffer[i].prio,
  253. SPLIT_NS_H(buffer[i].cost_cputime), SPLIT_NS_L(buffer[i].cost_cputime),
  254. buffer[i].cputime_percen_6 / 10000, buffer[i].cputime_percen_6 % 10000,
  255. SPLIT_NS_H(buffer[i].isr_time), SPLIT_NS_L(buffer[i].isr_time));
  256. }
  257. pr_err("last throttle information end\n");
  258. }
  259. void mt_rt_mon_switch(int on)
  260. {
  261. if (on == MON_RESET)
  262. reset_rt_mon_list();
  263. if (mt_rt_mon_enabled == 1) {
  264. if (on == MON_STOP)
  265. stop_rt_mon_task();
  266. } else {
  267. if (on == MON_START)
  268. start_rt_mon_task();
  269. }
  270. }
  271. void save_mt_rt_mon_info(struct task_struct *p, unsigned long long ts)
  272. {
  273. unsigned long long mon_now_ts;
  274. unsigned long irq_flags;
  275. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  276. if (0 == mt_rt_mon_enabled) {
  277. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  278. return;
  279. }
  280. if (p->policy != SCHED_FIFO && p->policy != SCHED_RR) {
  281. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  282. return;
  283. }
  284. if (rt_mon_count >= MAX_THREAD_COUNT) {
  285. pr_err("mtmon thread count larger the max level %d.\n", MAX_THREAD_COUNT);
  286. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  287. return;
  288. }
  289. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  290. mon_now_ts = sched_clock();
  291. rt_dur_ts = mon_now_ts - rt_start_ts;
  292. do_div(rt_dur_ts, 1000000); /* put prof_dur_ts to ms */
  293. if (rt_dur_ts >= MAX_TIME * 1000) {
  294. pr_err("mtmon debug time larger than the max time %d.\n", MAX_TIME);
  295. mt_rt_mon_switch(MON_RESET);
  296. return;
  297. }
  298. store_rt_mon_info(p);
  299. }
  300. void end_mt_rt_mon_info(struct task_struct *p)
  301. {
  302. struct mt_rt_mon_struct *tmp;
  303. unsigned long irq_flags;
  304. int find = 0;
  305. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  306. /* check profiling enable flag */
  307. if (0 == mt_rt_mon_enabled) {
  308. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  309. return;
  310. }
  311. if (!task_has_rt_policy(p)) {
  312. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  313. return;
  314. }
  315. list_for_each_entry(tmp, &mt_rt_mon_head.list, list) {
  316. if (p->pid == tmp->pid) {
  317. tmp->prio = p->prio;
  318. strcpy(tmp->comm, p->comm);
  319. /* update cputime */
  320. tmp->cputime = p->se.sum_exec_runtime;
  321. tmp->isr_time = p->se.mtk_isr_time - tmp->isr_time;
  322. tmp->cost_isrtime += tmp->isr_time;
  323. find = 1;
  324. break;
  325. }
  326. }
  327. if (!find)
  328. pr_err("pid:%d can't be found in mtsched proc_info.\n", p->pid);
  329. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  330. }
  331. void check_mt_rt_mon_info(struct task_struct *p)
  332. {
  333. struct mt_rt_mon_struct *tmp;
  334. unsigned long irq_flags;
  335. unsigned long long cost_cputime = 0;
  336. int find = 0;
  337. spin_lock_irqsave(&mt_rt_mon_lock, irq_flags);
  338. /* check profiling enable flag */
  339. if (0 == mt_rt_mon_enabled) {
  340. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  341. return;
  342. }
  343. list_for_each_entry(tmp, &mt_rt_mon_head.list, list) {
  344. if (!find && p->pid == tmp->pid) {
  345. tmp->prio = p->prio;
  346. strcpy(tmp->comm, p->comm);
  347. tmp->cputime = p->se.sum_exec_runtime;
  348. if (!task_has_rt_policy(p)) {
  349. tmp->isr_time =
  350. p->se.mtk_isr_time - tmp->isr_time;
  351. tmp->cost_isrtime += tmp->isr_time;
  352. if (tmp->cputime >= (tmp->cputime_init + tmp->cost_isrtime)) {
  353. cost_cputime =
  354. tmp->cputime - tmp->cost_isrtime - tmp->cputime_init;
  355. tmp->cost_cputime += cost_cputime;
  356. } else {
  357. tmp->cost_cputime = 0;
  358. }
  359. }
  360. tmp->cputime_init = p->se.sum_exec_runtime;
  361. tmp->isr_time = p->se.mtk_isr_time;
  362. find = 1;
  363. break;
  364. }
  365. }
  366. spin_unlock_irqrestore(&mt_rt_mon_lock, irq_flags);
  367. if (!find && task_has_rt_policy(p))
  368. store_rt_mon_info(p);
  369. }
  370. int mt_rt_mon_enable(void)
  371. {
  372. return mt_rt_mon_enabled;
  373. }