delayacct.c 4.7 KB

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