delayacct.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/slab.h>
  17. #include <linux/taskstats.h>
  18. #include <linux/time.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/delayacct.h>
  21. #include <linux/module.h>
  22. int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
  23. EXPORT_SYMBOL_GPL(delayacct_on);
  24. struct kmem_cache *delayacct_cache;
  25. static int __init delayacct_setup_disable(char *str)
  26. {
  27. delayacct_on = 0;
  28. return 1;
  29. }
  30. __setup("nodelayacct", delayacct_setup_disable);
  31. void delayacct_init(void)
  32. {
  33. delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
  34. delayacct_tsk_init(&init_task);
  35. }
  36. void __delayacct_tsk_init(struct task_struct *tsk)
  37. {
  38. tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
  39. if (tsk->delays)
  40. spin_lock_init(&tsk->delays->lock);
  41. }
  42. /*
  43. * Start accounting for a delay statistic using
  44. * its starting timestamp (@start)
  45. */
  46. static inline void delayacct_start(struct timespec *start)
  47. {
  48. do_posix_clock_monotonic_gettime(start);
  49. }
  50. /*
  51. * Finish delay accounting for a statistic using
  52. * its timestamps (@start, @end), accumalator (@total) and @count
  53. */
  54. static void delayacct_end(struct timespec *start, struct timespec *end,
  55. u64 *total, u32 *count)
  56. {
  57. struct timespec ts;
  58. s64 ns;
  59. unsigned long flags;
  60. do_posix_clock_monotonic_gettime(end);
  61. ts = timespec_sub(*end, *start);
  62. ns = timespec_to_ns(&ts);
  63. if (ns < 0)
  64. return;
  65. spin_lock_irqsave(&current->delays->lock, flags);
  66. *total += ns;
  67. (*count)++;
  68. spin_unlock_irqrestore(&current->delays->lock, flags);
  69. }
  70. void __delayacct_blkio_start(void)
  71. {
  72. delayacct_start(&current->delays->blkio_start);
  73. }
  74. void __delayacct_blkio_end(void)
  75. {
  76. if (current->delays->flags & DELAYACCT_PF_SWAPIN)
  77. /* Swapin block I/O */
  78. delayacct_end(&current->delays->blkio_start,
  79. &current->delays->blkio_end,
  80. &current->delays->swapin_delay,
  81. &current->delays->swapin_count);
  82. else /* Other block I/O */
  83. delayacct_end(&current->delays->blkio_start,
  84. &current->delays->blkio_end,
  85. &current->delays->blkio_delay,
  86. &current->delays->blkio_count);
  87. }
  88. int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
  89. {
  90. s64 tmp;
  91. unsigned long t1;
  92. unsigned long long t2, t3;
  93. unsigned long flags;
  94. struct timespec ts;
  95. cputime_t utime, stime, stimescaled, utimescaled;
  96. /* Though tsk->delays accessed later, early exit avoids
  97. * unnecessary returning of other data
  98. */
  99. if (!tsk->delays)
  100. goto done;
  101. tmp = (s64)d->cpu_run_real_total;
  102. task_cputime(tsk, &utime, &stime);
  103. cputime_to_timespec(utime + stime, &ts);
  104. tmp += timespec_to_ns(&ts);
  105. d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
  106. tmp = (s64)d->cpu_scaled_run_real_total;
  107. task_cputime_scaled(tsk, &utimescaled, &stimescaled);
  108. cputime_to_timespec(utimescaled + stimescaled, &ts);
  109. tmp += timespec_to_ns(&ts);
  110. d->cpu_scaled_run_real_total =
  111. (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
  112. /*
  113. * No locking available for sched_info (and too expensive to add one)
  114. * Mitigate by taking snapshot of values
  115. */
  116. t1 = tsk->sched_info.pcount;
  117. t2 = tsk->sched_info.run_delay;
  118. t3 = tsk->se.sum_exec_runtime;
  119. d->cpu_count += t1;
  120. tmp = (s64)d->cpu_delay_total + t2;
  121. d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
  122. tmp = (s64)d->cpu_run_virtual_total + t3;
  123. d->cpu_run_virtual_total =
  124. (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
  125. /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
  126. spin_lock_irqsave(&tsk->delays->lock, flags);
  127. tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
  128. d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
  129. tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
  130. d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
  131. tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
  132. d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
  133. d->blkio_count += tsk->delays->blkio_count;
  134. d->swapin_count += tsk->delays->swapin_count;
  135. d->freepages_count += tsk->delays->freepages_count;
  136. spin_unlock_irqrestore(&tsk->delays->lock, flags);
  137. done:
  138. return 0;
  139. }
  140. __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
  141. {
  142. __u64 ret;
  143. unsigned long flags;
  144. spin_lock_irqsave(&tsk->delays->lock, flags);
  145. ret = nsec_to_clock_t(tsk->delays->blkio_delay +
  146. tsk->delays->swapin_delay);
  147. spin_unlock_irqrestore(&tsk->delays->lock, flags);
  148. return ret;
  149. }
  150. void __delayacct_freepages_start(void)
  151. {
  152. delayacct_start(&current->delays->freepages_start);
  153. }
  154. void __delayacct_freepages_end(void)
  155. {
  156. delayacct_end(&current->delays->freepages_start,
  157. &current->delays->freepages_end,
  158. &current->delays->freepages_delay,
  159. &current->delays->freepages_count);
  160. }