delayacct.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* delayacct.h - 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 will be useful,
  11. * but 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. */
  16. #ifndef _LINUX_DELAYACCT_H
  17. #define _LINUX_DELAYACCT_H
  18. #include <uapi/linux/taskstats.h>
  19. /*
  20. * Per-task flags relevant to delay accounting
  21. * maintained privately to avoid exhausting similar flags in sched.h:PF_*
  22. * Used to set current->delays->flags
  23. */
  24. #define DELAYACCT_PF_SWAPIN 0x00000001 /* I am doing a swapin */
  25. #define DELAYACCT_PF_BLKIO 0x00000002 /* I am waiting on IO */
  26. #ifdef CONFIG_TASK_DELAY_ACCT
  27. struct task_delay_info {
  28. raw_spinlock_t lock;
  29. unsigned int flags; /* Private per-task flags */
  30. /* For each stat XXX, add following, aligned appropriately
  31. *
  32. * struct timespec XXX_start, XXX_end;
  33. * u64 XXX_delay;
  34. * u32 XXX_count;
  35. *
  36. * Atomicity of updates to XXX_delay, XXX_count protected by
  37. * single lock above (split into XXX_lock if contention is an issue).
  38. */
  39. /*
  40. * XXX_count is incremented on every XXX operation, the delay
  41. * associated with the operation is added to XXX_delay.
  42. * XXX_delay contains the accumulated delay time in nanoseconds.
  43. */
  44. u64 blkio_start; /* Shared by blkio, swapin */
  45. u64 blkio_delay; /* wait for sync block io completion */
  46. u64 swapin_delay; /* wait for swapin block io completion */
  47. u32 blkio_count; /* total count of the number of sync block */
  48. /* io operations performed */
  49. u32 swapin_count; /* total count of the number of swapin block */
  50. /* io operations performed */
  51. u64 freepages_start;
  52. u64 freepages_delay; /* wait for memory reclaim */
  53. u32 freepages_count; /* total count of memory reclaim */
  54. };
  55. #endif
  56. #include <linux/sched.h>
  57. #include <linux/slab.h>
  58. #ifdef CONFIG_TASK_DELAY_ACCT
  59. extern int delayacct_on; /* Delay accounting turned on/off */
  60. extern struct kmem_cache *delayacct_cache;
  61. extern void delayacct_init(void);
  62. extern void __delayacct_tsk_init(struct task_struct *);
  63. extern void __delayacct_tsk_exit(struct task_struct *);
  64. extern void __delayacct_blkio_start(void);
  65. extern void __delayacct_blkio_end(struct task_struct *);
  66. extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *);
  67. extern __u64 __delayacct_blkio_ticks(struct task_struct *);
  68. extern void __delayacct_freepages_start(void);
  69. extern void __delayacct_freepages_end(void);
  70. static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
  71. {
  72. if (p->delays)
  73. return (p->delays->flags & DELAYACCT_PF_BLKIO);
  74. else
  75. return 0;
  76. }
  77. static inline void delayacct_set_flag(int flag)
  78. {
  79. if (current->delays)
  80. current->delays->flags |= flag;
  81. }
  82. static inline void delayacct_clear_flag(int flag)
  83. {
  84. if (current->delays)
  85. current->delays->flags &= ~flag;
  86. }
  87. static inline void delayacct_tsk_init(struct task_struct *tsk)
  88. {
  89. /* reinitialize in case parent's non-null pointer was dup'ed*/
  90. tsk->delays = NULL;
  91. if (delayacct_on)
  92. __delayacct_tsk_init(tsk);
  93. }
  94. /* Free tsk->delays. Called from bad fork and __put_task_struct
  95. * where there's no risk of tsk->delays being accessed elsewhere
  96. */
  97. static inline void delayacct_tsk_free(struct task_struct *tsk)
  98. {
  99. if (tsk->delays)
  100. kmem_cache_free(delayacct_cache, tsk->delays);
  101. tsk->delays = NULL;
  102. }
  103. static inline void delayacct_blkio_start(void)
  104. {
  105. delayacct_set_flag(DELAYACCT_PF_BLKIO);
  106. if (current->delays)
  107. __delayacct_blkio_start();
  108. }
  109. static inline void delayacct_blkio_end(struct task_struct *p)
  110. {
  111. if (p->delays)
  112. __delayacct_blkio_end(p);
  113. delayacct_clear_flag(DELAYACCT_PF_BLKIO);
  114. }
  115. static inline int delayacct_add_tsk(struct taskstats *d,
  116. struct task_struct *tsk)
  117. {
  118. if (!delayacct_on || !tsk->delays)
  119. return 0;
  120. return __delayacct_add_tsk(d, tsk);
  121. }
  122. static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
  123. {
  124. if (tsk->delays)
  125. return __delayacct_blkio_ticks(tsk);
  126. return 0;
  127. }
  128. static inline void delayacct_freepages_start(void)
  129. {
  130. if (current->delays)
  131. __delayacct_freepages_start();
  132. }
  133. static inline void delayacct_freepages_end(void)
  134. {
  135. if (current->delays)
  136. __delayacct_freepages_end();
  137. }
  138. #else
  139. static inline void delayacct_set_flag(int flag)
  140. {}
  141. static inline void delayacct_clear_flag(int flag)
  142. {}
  143. static inline void delayacct_init(void)
  144. {}
  145. static inline void delayacct_tsk_init(struct task_struct *tsk)
  146. {}
  147. static inline void delayacct_tsk_free(struct task_struct *tsk)
  148. {}
  149. static inline void delayacct_blkio_start(void)
  150. {}
  151. static inline void delayacct_blkio_end(struct task_struct *p)
  152. {}
  153. static inline int delayacct_add_tsk(struct taskstats *d,
  154. struct task_struct *tsk)
  155. { return 0; }
  156. static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
  157. { return 0; }
  158. static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
  159. { return 0; }
  160. static inline void delayacct_freepages_start(void)
  161. {}
  162. static inline void delayacct_freepages_end(void)
  163. {}
  164. #endif /* CONFIG_TASK_DELAY_ACCT */
  165. #endif