delayacct.c 4.9 KB

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