avr32: add two new at91 to cpu.h definition
[linux-2.6/mini2440.git] / kernel / delayacct.c
blobead9b610aa71af7b1bd689c50ee616cf142e64cc
1 /* delayacct.c - per-task delay accounting
3 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
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.
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.
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/taskstats.h>
19 #include <linux/time.h>
20 #include <linux/sysctl.h>
21 #include <linux/delayacct.h>
23 int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
24 struct kmem_cache *delayacct_cache;
26 static int __init delayacct_setup_disable(char *str)
28 delayacct_on = 0;
29 return 1;
31 __setup("nodelayacct", delayacct_setup_disable);
33 void delayacct_init(void)
35 delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
36 delayacct_tsk_init(&init_task);
39 void __delayacct_tsk_init(struct task_struct *tsk)
41 tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
42 if (tsk->delays)
43 spin_lock_init(&tsk->delays->lock);
47 * Start accounting for a delay statistic using
48 * its starting timestamp (@start)
51 static inline void delayacct_start(struct timespec *start)
53 do_posix_clock_monotonic_gettime(start);
57 * Finish delay accounting for a statistic using
58 * its timestamps (@start, @end), accumalator (@total) and @count
61 static void delayacct_end(struct timespec *start, struct timespec *end,
62 u64 *total, u32 *count)
64 struct timespec ts;
65 s64 ns;
66 unsigned long flags;
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;
74 spin_lock_irqsave(&current->delays->lock, flags);
75 *total += ns;
76 (*count)++;
77 spin_unlock_irqrestore(&current->delays->lock, flags);
80 void __delayacct_blkio_start(void)
82 delayacct_start(&current->delays->blkio_start);
85 void __delayacct_blkio_end(void)
87 if (current->delays->flags & DELAYACCT_PF_SWAPIN)
88 /* Swapin block I/O */
89 delayacct_end(&current->delays->blkio_start,
90 &current->delays->blkio_end,
91 &current->delays->swapin_delay,
92 &current->delays->swapin_count);
93 else /* Other block I/O */
94 delayacct_end(&current->delays->blkio_start,
95 &current->delays->blkio_end,
96 &current->delays->blkio_delay,
97 &current->delays->blkio_count);
100 int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
102 s64 tmp;
103 unsigned long t1;
104 unsigned long long t2, t3;
105 unsigned long flags;
106 struct timespec ts;
108 /* Though tsk->delays accessed later, early exit avoids
109 * unnecessary returning of other data
111 if (!tsk->delays)
112 goto done;
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;
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;
126 * No locking available for sched_info (and too expensive to add one)
127 * Mitigate by taking snapshot of values
129 t1 = tsk->sched_info.pcount;
130 t2 = tsk->sched_info.run_delay;
131 t3 = tsk->se.sum_exec_runtime;
133 d->cpu_count += t1;
135 tmp = (s64)d->cpu_delay_total + t2;
136 d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
138 tmp = (s64)d->cpu_run_virtual_total + t3;
139 d->cpu_run_virtual_total =
140 (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
142 /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
144 spin_lock_irqsave(&tsk->delays->lock, flags);
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;
149 tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
150 d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
151 d->blkio_count += tsk->delays->blkio_count;
152 d->swapin_count += tsk->delays->swapin_count;
153 d->freepages_count += tsk->delays->freepages_count;
154 spin_unlock_irqrestore(&tsk->delays->lock, flags);
156 done:
157 return 0;
160 __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
162 __u64 ret;
163 unsigned long flags;
165 spin_lock_irqsave(&tsk->delays->lock, flags);
166 ret = nsec_to_clock_t(tsk->delays->blkio_delay +
167 tsk->delays->swapin_delay);
168 spin_unlock_irqrestore(&tsk->delays->lock, flags);
169 return ret;
172 void __delayacct_freepages_start(void)
174 delayacct_start(&current->delays->freepages_start);
177 void __delayacct_freepages_end(void)
179 delayacct_end(&current->delays->freepages_start,
180 &current->delays->freepages_end,
181 &current->delays->freepages_delay,
182 &current->delays->freepages_count);