delayacct.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* delayacct.c - per-task delay accounting
  2. *
  3. * Copyright (C) Shailabh Nagar, IBM Corp. 2006
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it would be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/sched/cputime.h>
  18. #include <linux/slab.h>
  19. #include <linux/taskstats.h>
  20. #include <linux/time.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/delayacct.h>
  23. #include <linux/module.h>
  24. int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
  25. EXPORT_SYMBOL_GPL(delayacct_on);
  26. struct kmem_cache *delayacct_cache;
  27. static int __init delayacct_setup_disable(char *str)
  28. {
  29. delayacct_on = 0;
  30. return 1;
  31. }
  32. __setup("nodelayacct", delayacct_setup_disable);
  33. void delayacct_init(void)
  34. {
  35. delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
  36. delayacct_tsk_init(&init_task);
  37. }
  38. void __delayacct_tsk_init(struct task_struct *tsk)
  39. {
  40. tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
  41. if (tsk->delays)
  42. raw_spin_lock_init(&tsk->delays->lock);
  43. }
  44. /*
  45. * Finish delay accounting for a statistic using its timestamps (@start),
  46. * accumalator (@total) and @count
  47. */
  48. static void delayacct_end(raw_spinlock_t *lock, u64 *start, u64 *total,
  49. u32 *count)
  50. {
  51. s64 ns = ktime_get_ns() - *start;
  52. unsigned long flags;
  53. if (ns > 0) {
  54. raw_spin_lock_irqsave(lock, flags);
  55. *total += ns;
  56. (*count)++;
  57. raw_spin_unlock_irqrestore(lock, flags);
  58. }
  59. }
  60. void __delayacct_blkio_start(void)
  61. {
  62. current->delays->blkio_start = ktime_get_ns();
  63. }
  64. /*
  65. * We cannot rely on the `current` macro, as we haven't yet switched back to
  66. * the process being woken.
  67. */
  68. void __delayacct_blkio_end(struct task_struct *p)
  69. {
  70. struct task_delay_info *delays = p->delays;
  71. u64 *total;
  72. u32 *count;
  73. if (p->delays->flags & DELAYACCT_PF_SWAPIN) {
  74. total = &delays->swapin_delay;
  75. count = &delays->swapin_count;
  76. } else {
  77. total = &delays->blkio_delay;
  78. count = &delays->blkio_count;
  79. }
  80. delayacct_end(&delays->lock, &delays->blkio_start, total, count);
  81. }
  82. int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
  83. {
  84. u64 utime, stime, stimescaled, utimescaled;
  85. unsigned long long t2, t3;
  86. unsigned long flags, t1;
  87. s64 tmp;
  88. task_cputime(tsk, &utime, &stime);
  89. tmp = (s64)d->cpu_run_real_total;
  90. tmp += utime + stime;
  91. d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
  92. task_cputime_scaled(tsk, &utimescaled, &stimescaled);
  93. tmp = (s64)d->cpu_scaled_run_real_total;
  94. tmp += utimescaled + stimescaled;
  95. d->cpu_scaled_run_real_total =
  96. (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
  97. /*
  98. * No locking available for sched_info (and too expensive to add one)
  99. * Mitigate by taking snapshot of values
  100. */
  101. t1 = tsk->sched_info.pcount;
  102. t2 = tsk->sched_info.run_delay;
  103. t3 = tsk->se.sum_exec_runtime;
  104. d->cpu_count += t1;
  105. tmp = (s64)d->cpu_delay_total + t2;
  106. d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
  107. tmp = (s64)d->cpu_run_virtual_total + t3;
  108. d->cpu_run_virtual_total =
  109. (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
  110. /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
  111. raw_spin_lock_irqsave(&tsk->delays->lock, flags);
  112. tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
  113. d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
  114. tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
  115. d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
  116. tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
  117. d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
  118. tmp = d->thrashing_delay_total + tsk->delays->thrashing_delay;
  119. d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
  120. d->blkio_count += tsk->delays->blkio_count;
  121. d->swapin_count += tsk->delays->swapin_count;
  122. d->freepages_count += tsk->delays->freepages_count;
  123. d->thrashing_count += tsk->delays->thrashing_count;
  124. raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
  125. return 0;
  126. }
  127. __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
  128. {
  129. __u64 ret;
  130. unsigned long flags;
  131. raw_spin_lock_irqsave(&tsk->delays->lock, flags);
  132. ret = nsec_to_clock_t(tsk->delays->blkio_delay +
  133. tsk->delays->swapin_delay);
  134. raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
  135. return ret;
  136. }
  137. void __delayacct_freepages_start(void)
  138. {
  139. current->delays->freepages_start = ktime_get_ns();
  140. }
  141. void __delayacct_freepages_end(void)
  142. {
  143. delayacct_end(
  144. &current->delays->lock,
  145. &current->delays->freepages_start,
  146. &current->delays->freepages_delay,
  147. &current->delays->freepages_count);
  148. }
  149. void __delayacct_thrashing_start(void)
  150. {
  151. current->delays->thrashing_start = ktime_get_ns();
  152. }
  153. void __delayacct_thrashing_end(void)
  154. {
  155. delayacct_end(&current->delays->lock,
  156. &current->delays->thrashing_start,
  157. &current->delays->thrashing_delay,
  158. &current->delays->thrashing_count);
  159. }