sched_stats.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #ifdef CONFIG_SCHEDSTATS
  2. /*
  3. * bump this up when changing the output format or the meaning of an existing
  4. * format, so that tools can adapt (or abort)
  5. */
  6. #define SCHEDSTAT_VERSION 15
  7. static int show_schedstat(struct seq_file *seq, void *v)
  8. {
  9. int cpu;
  10. int mask_len = DIV_ROUND_UP(NR_CPUS, 32) * 9;
  11. char *mask_str = kmalloc(mask_len, GFP_KERNEL);
  12. if (mask_str == NULL)
  13. return -ENOMEM;
  14. seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
  15. seq_printf(seq, "timestamp %lu\n", jiffies);
  16. for_each_online_cpu(cpu) {
  17. struct rq *rq = cpu_rq(cpu);
  18. #ifdef CONFIG_SMP
  19. struct sched_domain *sd;
  20. int dcount = 0;
  21. #endif
  22. /* runqueue-specific stats */
  23. seq_printf(seq,
  24. "cpu%d %u %u %u %u %u %u %llu %llu %lu",
  25. cpu, rq->yld_count,
  26. rq->sched_switch, rq->sched_count, rq->sched_goidle,
  27. rq->ttwu_count, rq->ttwu_local,
  28. rq->rq_cpu_time,
  29. rq->rq_sched_info.run_delay, rq->rq_sched_info.pcount);
  30. seq_printf(seq, "\n");
  31. #ifdef CONFIG_SMP
  32. /* domain-specific stats */
  33. rcu_read_lock();
  34. for_each_domain(cpu, sd) {
  35. enum cpu_idle_type itype;
  36. cpumask_scnprintf(mask_str, mask_len,
  37. sched_domain_span(sd));
  38. seq_printf(seq, "domain%d %s", dcount++, mask_str);
  39. for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
  40. itype++) {
  41. seq_printf(seq, " %u %u %u %u %u %u %u %u",
  42. sd->lb_count[itype],
  43. sd->lb_balanced[itype],
  44. sd->lb_failed[itype],
  45. sd->lb_imbalance[itype],
  46. sd->lb_gained[itype],
  47. sd->lb_hot_gained[itype],
  48. sd->lb_nobusyq[itype],
  49. sd->lb_nobusyg[itype]);
  50. }
  51. seq_printf(seq,
  52. " %u %u %u %u %u %u %u %u %u %u %u %u\n",
  53. sd->alb_count, sd->alb_failed, sd->alb_pushed,
  54. sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
  55. sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
  56. sd->ttwu_wake_remote, sd->ttwu_move_affine,
  57. sd->ttwu_move_balance);
  58. }
  59. rcu_read_unlock();
  60. #endif
  61. }
  62. kfree(mask_str);
  63. return 0;
  64. }
  65. static int schedstat_open(struct inode *inode, struct file *file)
  66. {
  67. unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
  68. char *buf = kmalloc(size, GFP_KERNEL);
  69. struct seq_file *m;
  70. int res;
  71. if (!buf)
  72. return -ENOMEM;
  73. res = single_open(file, show_schedstat, NULL);
  74. if (!res) {
  75. m = file->private_data;
  76. m->buf = buf;
  77. m->size = size;
  78. } else
  79. kfree(buf);
  80. return res;
  81. }
  82. static const struct file_operations proc_schedstat_operations = {
  83. .open = schedstat_open,
  84. .read = seq_read,
  85. .llseek = seq_lseek,
  86. .release = single_release,
  87. };
  88. static int __init proc_schedstat_init(void)
  89. {
  90. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  91. return 0;
  92. }
  93. module_init(proc_schedstat_init);
  94. /*
  95. * Expects runqueue lock to be held for atomicity of update
  96. */
  97. static inline void
  98. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  99. {
  100. if (rq) {
  101. rq->rq_sched_info.run_delay += delta;
  102. rq->rq_sched_info.pcount++;
  103. }
  104. }
  105. /*
  106. * Expects runqueue lock to be held for atomicity of update
  107. */
  108. static inline void
  109. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  110. {
  111. if (rq)
  112. rq->rq_cpu_time += delta;
  113. }
  114. static inline void
  115. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  116. {
  117. if (rq)
  118. rq->rq_sched_info.run_delay += delta;
  119. }
  120. # define schedstat_inc(rq, field) do { (rq)->field++; } while (0)
  121. # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0)
  122. # define schedstat_set(var, val) do { var = (val); } while (0)
  123. #else /* !CONFIG_SCHEDSTATS */
  124. static inline void
  125. rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
  126. {}
  127. static inline void
  128. rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
  129. {}
  130. static inline void
  131. rq_sched_info_depart(struct rq *rq, unsigned long long delta)
  132. {}
  133. # define schedstat_inc(rq, field) do { } while (0)
  134. # define schedstat_add(rq, field, amt) do { } while (0)
  135. # define schedstat_set(var, val) do { } while (0)
  136. #endif
  137. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  138. static inline void sched_info_reset_dequeued(struct task_struct *t)
  139. {
  140. t->sched_info.last_queued = 0;
  141. }
  142. /*
  143. * We are interested in knowing how long it was from the *first* time a
  144. * task was queued to the time that it finally hit a cpu, we call this routine
  145. * from dequeue_task() to account for possible rq->clock skew across cpus. The
  146. * delta taken on each cpu would annul the skew.
  147. */
  148. static inline void sched_info_dequeued(struct task_struct *t)
  149. {
  150. unsigned long long now = task_rq(t)->clock, delta = 0;
  151. if (unlikely(sched_info_on()))
  152. if (t->sched_info.last_queued)
  153. delta = now - t->sched_info.last_queued;
  154. sched_info_reset_dequeued(t);
  155. t->sched_info.run_delay += delta;
  156. rq_sched_info_dequeued(task_rq(t), delta);
  157. }
  158. /*
  159. * Called when a task finally hits the cpu. We can now calculate how
  160. * long it was waiting to run. We also note when it began so that we
  161. * can keep stats on how long its timeslice is.
  162. */
  163. static void sched_info_arrive(struct task_struct *t)
  164. {
  165. unsigned long long now = task_rq(t)->clock, delta = 0;
  166. if (t->sched_info.last_queued)
  167. delta = now - t->sched_info.last_queued;
  168. sched_info_reset_dequeued(t);
  169. t->sched_info.run_delay += delta;
  170. t->sched_info.last_arrival = now;
  171. t->sched_info.pcount++;
  172. rq_sched_info_arrive(task_rq(t), delta);
  173. }
  174. /*
  175. * This function is only called from enqueue_task(), but also only updates
  176. * the timestamp if it is already not set. It's assumed that
  177. * sched_info_dequeued() will clear that stamp when appropriate.
  178. */
  179. static inline void sched_info_queued(struct task_struct *t)
  180. {
  181. if (unlikely(sched_info_on()))
  182. if (!t->sched_info.last_queued)
  183. t->sched_info.last_queued = task_rq(t)->clock;
  184. }
  185. /*
  186. * Called when a process ceases being the active-running process, either
  187. * voluntarily or involuntarily. Now we can calculate how long we ran.
  188. * Also, if the process is still in the TASK_RUNNING state, call
  189. * sched_info_queued() to mark that it has now again started waiting on
  190. * the runqueue.
  191. */
  192. static inline void sched_info_depart(struct task_struct *t)
  193. {
  194. unsigned long long delta = task_rq(t)->clock -
  195. t->sched_info.last_arrival;
  196. rq_sched_info_depart(task_rq(t), delta);
  197. if (t->state == TASK_RUNNING)
  198. sched_info_queued(t);
  199. }
  200. /*
  201. * Called when tasks are switched involuntarily due, typically, to expiring
  202. * their time slice. (This may also be called when switching to or from
  203. * the idle task.) We are only called when prev != next.
  204. */
  205. static inline void
  206. __sched_info_switch(struct task_struct *prev, struct task_struct *next)
  207. {
  208. struct rq *rq = task_rq(prev);
  209. /*
  210. * prev now departs the cpu. It's not interesting to record
  211. * stats about how efficient we were at scheduling the idle
  212. * process, however.
  213. */
  214. if (prev != rq->idle)
  215. sched_info_depart(prev);
  216. if (next != rq->idle)
  217. sched_info_arrive(next);
  218. }
  219. static inline void
  220. sched_info_switch(struct task_struct *prev, struct task_struct *next)
  221. {
  222. if (unlikely(sched_info_on()))
  223. __sched_info_switch(prev, next);
  224. }
  225. #else
  226. #define sched_info_queued(t) do { } while (0)
  227. #define sched_info_reset_dequeued(t) do { } while (0)
  228. #define sched_info_dequeued(t) do { } while (0)
  229. #define sched_info_switch(t, next) do { } while (0)
  230. #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */
  231. /*
  232. * The following are functions that support scheduler-internal time accounting.
  233. * These functions are generally called at the timer tick. None of this depends
  234. * on CONFIG_SCHEDSTATS.
  235. */
  236. /**
  237. * account_group_user_time - Maintain utime for a thread group.
  238. *
  239. * @tsk: Pointer to task structure.
  240. * @cputime: Time value by which to increment the utime field of the
  241. * thread_group_cputime structure.
  242. *
  243. * If thread group time is being maintained, get the structure for the
  244. * running CPU and update the utime field there.
  245. */
  246. static inline void account_group_user_time(struct task_struct *tsk,
  247. cputime_t cputime)
  248. {
  249. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  250. if (!cputimer->running)
  251. return;
  252. spin_lock(&cputimer->lock);
  253. cputimer->cputime.utime =
  254. cputime_add(cputimer->cputime.utime, cputime);
  255. spin_unlock(&cputimer->lock);
  256. }
  257. /**
  258. * account_group_system_time - Maintain stime for a thread group.
  259. *
  260. * @tsk: Pointer to task structure.
  261. * @cputime: Time value by which to increment the stime field of the
  262. * thread_group_cputime structure.
  263. *
  264. * If thread group time is being maintained, get the structure for the
  265. * running CPU and update the stime field there.
  266. */
  267. static inline void account_group_system_time(struct task_struct *tsk,
  268. cputime_t cputime)
  269. {
  270. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  271. if (!cputimer->running)
  272. return;
  273. spin_lock(&cputimer->lock);
  274. cputimer->cputime.stime =
  275. cputime_add(cputimer->cputime.stime, cputime);
  276. spin_unlock(&cputimer->lock);
  277. }
  278. /**
  279. * account_group_exec_runtime - Maintain exec runtime for a thread group.
  280. *
  281. * @tsk: Pointer to task structure.
  282. * @ns: Time value by which to increment the sum_exec_runtime field
  283. * of the thread_group_cputime structure.
  284. *
  285. * If thread group time is being maintained, get the structure for the
  286. * running CPU and update the sum_exec_runtime field there.
  287. */
  288. static inline void account_group_exec_runtime(struct task_struct *tsk,
  289. unsigned long long ns)
  290. {
  291. struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
  292. if (!cputimer->running)
  293. return;
  294. spin_lock(&cputimer->lock);
  295. cputimer->cputime.sum_exec_runtime += ns;
  296. spin_unlock(&cputimer->lock);
  297. }