Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 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 | |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/slab.h> |
Alexey Dobriyan | 6952b61 | 2009-09-18 23:55:55 +0400 | [diff] [blame] | 18 | #include <linux/taskstats.h> |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 19 | #include <linux/time.h> |
| 20 | #include <linux/sysctl.h> |
| 21 | #include <linux/delayacct.h> |
| 22 | |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 23 | int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 24 | struct kmem_cache *delayacct_cache; |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 25 | |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 26 | static int __init delayacct_setup_disable(char *str) |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 27 | { |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 28 | delayacct_on = 0; |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 29 | return 1; |
| 30 | } |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 31 | __setup("nodelayacct", delayacct_setup_disable); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 32 | |
| 33 | void delayacct_init(void) |
| 34 | { |
Christoph Lameter | 0a31bd5 | 2007-05-06 14:49:57 -0700 | [diff] [blame] | 35 | delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 36 | delayacct_tsk_init(&init_task); |
| 37 | } |
| 38 | |
| 39 | void __delayacct_tsk_init(struct task_struct *tsk) |
| 40 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 41 | tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 42 | if (tsk->delays) |
| 43 | spin_lock_init(&tsk->delays->lock); |
| 44 | } |
| 45 | |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Start accounting for a delay statistic using |
| 48 | * its starting timestamp (@start) |
| 49 | */ |
| 50 | |
| 51 | static inline void delayacct_start(struct timespec *start) |
| 52 | { |
| 53 | do_posix_clock_monotonic_gettime(start); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Finish delay accounting for a statistic using |
| 58 | * its timestamps (@start, @end), accumalator (@total) and @count |
| 59 | */ |
| 60 | |
| 61 | static void delayacct_end(struct timespec *start, struct timespec *end, |
| 62 | u64 *total, u32 *count) |
| 63 | { |
| 64 | struct timespec ts; |
| 65 | s64 ns; |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 66 | unsigned long flags; |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 67 | |
| 68 | do_posix_clock_monotonic_gettime(end); |
| 69 | ts = timespec_sub(*end, *start); |
| 70 | ns = timespec_to_ns(&ts); |
| 71 | if (ns < 0) |
| 72 | return; |
| 73 | |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 74 | spin_lock_irqsave(¤t->delays->lock, flags); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 75 | *total += ns; |
| 76 | (*count)++; |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 77 | spin_unlock_irqrestore(¤t->delays->lock, flags); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 80 | void __delayacct_blkio_start(void) |
| 81 | { |
| 82 | delayacct_start(¤t->delays->blkio_start); |
| 83 | } |
| 84 | |
| 85 | void __delayacct_blkio_end(void) |
| 86 | { |
| 87 | if (current->delays->flags & DELAYACCT_PF_SWAPIN) |
| 88 | /* Swapin block I/O */ |
| 89 | delayacct_end(¤t->delays->blkio_start, |
| 90 | ¤t->delays->blkio_end, |
| 91 | ¤t->delays->swapin_delay, |
| 92 | ¤t->delays->swapin_count); |
| 93 | else /* Other block I/O */ |
| 94 | delayacct_end(¤t->delays->blkio_start, |
| 95 | ¤t->delays->blkio_end, |
| 96 | ¤t->delays->blkio_delay, |
| 97 | ¤t->delays->blkio_count); |
| 98 | } |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 99 | |
| 100 | int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk) |
| 101 | { |
| 102 | s64 tmp; |
Balbir Singh | 172ba84 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 103 | unsigned long t1; |
| 104 | unsigned long long t2, t3; |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 105 | unsigned long flags; |
Balbir Singh | 172ba84 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 106 | struct timespec ts; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 107 | |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 108 | /* Though tsk->delays accessed later, early exit avoids |
| 109 | * unnecessary returning of other data |
| 110 | */ |
| 111 | if (!tsk->delays) |
| 112 | goto done; |
| 113 | |
| 114 | tmp = (s64)d->cpu_run_real_total; |
| 115 | cputime_to_timespec(tsk->utime + tsk->stime, &ts); |
| 116 | tmp += timespec_to_ns(&ts); |
| 117 | d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp; |
| 118 | |
Michael Neuling | c66f08b | 2007-10-18 03:06:34 -0700 | [diff] [blame] | 119 | tmp = (s64)d->cpu_scaled_run_real_total; |
| 120 | cputime_to_timespec(tsk->utimescaled + tsk->stimescaled, &ts); |
| 121 | tmp += timespec_to_ns(&ts); |
| 122 | d->cpu_scaled_run_real_total = |
| 123 | (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp; |
| 124 | |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 125 | /* |
| 126 | * No locking available for sched_info (and too expensive to add one) |
| 127 | * Mitigate by taking snapshot of values |
| 128 | */ |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 129 | t1 = tsk->sched_info.pcount; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 130 | t2 = tsk->sched_info.run_delay; |
Ken Chen | 9c2c480 | 2008-12-16 23:41:22 -0800 | [diff] [blame] | 131 | t3 = tsk->se.sum_exec_runtime; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 132 | |
| 133 | d->cpu_count += t1; |
| 134 | |
Balbir Singh | 172ba84 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 135 | tmp = (s64)d->cpu_delay_total + t2; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 136 | d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp; |
| 137 | |
Balbir Singh | 172ba84 | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 138 | tmp = (s64)d->cpu_run_virtual_total + t3; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 139 | d->cpu_run_virtual_total = |
| 140 | (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp; |
| 141 | |
| 142 | /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */ |
| 143 | |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 144 | spin_lock_irqsave(&tsk->delays->lock, flags); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 145 | tmp = d->blkio_delay_total + tsk->delays->blkio_delay; |
| 146 | d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp; |
| 147 | tmp = d->swapin_delay_total + tsk->delays->swapin_delay; |
| 148 | d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp; |
Keika Kobayashi | 016ae21 | 2008-07-25 01:48:53 -0700 | [diff] [blame] | 149 | tmp = d->freepages_delay_total + tsk->delays->freepages_delay; |
| 150 | d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp; |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 151 | d->blkio_count += tsk->delays->blkio_count; |
| 152 | d->swapin_count += tsk->delays->swapin_count; |
Keika Kobayashi | 016ae21 | 2008-07-25 01:48:53 -0700 | [diff] [blame] | 153 | d->freepages_count += tsk->delays->freepages_count; |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 154 | spin_unlock_irqrestore(&tsk->delays->lock, flags); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 155 | |
| 156 | done: |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 159 | |
| 160 | __u64 __delayacct_blkio_ticks(struct task_struct *tsk) |
| 161 | { |
| 162 | __u64 ret; |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 163 | unsigned long flags; |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 164 | |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 165 | spin_lock_irqsave(&tsk->delays->lock, flags); |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 166 | ret = nsec_to_clock_t(tsk->delays->blkio_delay + |
| 167 | tsk->delays->swapin_delay); |
Peter Zijlstra | 64efade | 2006-11-05 23:52:10 -0800 | [diff] [blame] | 168 | spin_unlock_irqrestore(&tsk->delays->lock, flags); |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 172 | void __delayacct_freepages_start(void) |
| 173 | { |
| 174 | delayacct_start(¤t->delays->freepages_start); |
| 175 | } |
| 176 | |
| 177 | void __delayacct_freepages_end(void) |
| 178 | { |
| 179 | delayacct_end(¤t->delays->freepages_start, |
| 180 | ¤t->delays->freepages_end, |
| 181 | ¤t->delays->freepages_delay, |
| 182 | ¤t->delays->freepages_count); |
| 183 | } |
| 184 | |