delayacct.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 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(spinlock_t *lock, u64 *start, u64 *total, u32 *count)
  49. {
  50. s64 ns = ktime_get_ns() - *start;
  51. unsigned long flags;
  52. if (ns > 0) {
  53. spin_lock_irqsave(lock, flags);
  54. *total += ns;
  55. (*count)++;
  56. spin_unlock_irqrestore(lock, flags);
  57. }
  58. }
  59. void __delayacct_blkio_start(void)
  60. {
  61. current->delays->blkio_start = ktime_get_ns();
  62. }
  63. /*
  64. * We cannot rely on the `current` macro, as we haven't yet switched back to
  65. * the process being woken.
  66. */
  67. void __delayacct_blkio_end(struct task_struct *p)
  68. {
  69. struct task_delay_info *delays = p->delays;
  70. u64 *total;
  71. u32 *count;
  72. if (p->delays->flags & DELAYACCT_PF_SWAPIN) {
  73. total = &delays->swapin_delay;
  74. count = &delays->swapin_count;
  75. } else {
  76. total = &delays->blkio_delay;
  77. count = &delays->blkio_count;
  78. }
  79. delayacct_end(&delays->lock, &delays->blkio_start, total, count);
  80. }
  81. int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
  82. {
  83. u64 utime, stime, stimescaled, utimescaled;
  84. unsigned long long t2, t3;
  85. unsigned long flags, t1;
  86. s64 tmp;
  87. task_cputime(tsk, &utime, &stime);
  88. tmp = (s64)d->cpu_run_real_total;
  89. tmp += utime + stime;
  90. d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
  91. task_cputime_scaled(tsk, &utimescaled, &stimescaled);
  92. tmp = (s64)d->cpu_scaled_run_real_total;
  93. tmp += utimescaled + stimescaled;
  94. d->cpu_scaled_run_real_total =
  95. (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
  96. /*
  97. * No locking available for sched_info (and too expensive to add one)
  98. * Mitigate by taking snapshot of values
  99. */
  100. t1 = tsk->sched_info.pcount;
  101. t2 = tsk->sched_info.run_delay;
  102. t3 = tsk->se.sum_exec_runtime;
  103. d->cpu_count += t1;
  104. tmp = (s64)d->cpu_delay_total + t2;
  105. d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
  106. tmp = (s64)d->cpu_run_virtual_total + t3;
  107. d->cpu_run_virtual_total =
  108. (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
  109. /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
  110. spin_lock_irqsave(&tsk->delays->lock, flags);
  111. tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
  112. d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
  113. tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
  114. d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
  115. tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
  116. d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
  117. d->blkio_count += tsk->delays->blkio_count;
  118. d->swapin_count += tsk->delays->swapin_count;
  119. d->freepages_count += tsk->delays->freepages_count;
  120. spin_unlock_irqrestore(&tsk->delays->lock, flags);
  121. return 0;
  122. }
  123. __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
  124. {
  125. __u64 ret;
  126. unsigned long flags;
  127. spin_lock_irqsave(&tsk->delays->lock, flags);
  128. ret = nsec_to_clock_t(tsk->delays->blkio_delay +
  129. tsk->delays->swapin_delay);
  130. spin_unlock_irqrestore(&tsk->delays->lock, flags);
  131. return ret;
  132. }
  133. void __delayacct_freepages_start(void)
  134. {
  135. current->delays->freepages_start = ktime_get_ns();
  136. }
  137. void __delayacct_freepages_end(void)
  138. {
  139. delayacct_end(
  140. &current->delays->lock,
  141. &current->delays->freepages_start,
  142. &current->delays->freepages_delay,
  143. &current->delays->freepages_count);
  144. }