delayacct.c 4.3 KB

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