Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394/linux1...
[deliverable/linux.git] / kernel / delayacct.c
CommitLineData
ca74e92b
SN
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>
6952b61d 18#include <linux/taskstats.h>
ca74e92b
SN
19#include <linux/time.h>
20#include <linux/sysctl.h>
21#include <linux/delayacct.h>
22
163ecdff 23int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
e18b890b 24struct kmem_cache *delayacct_cache;
ca74e92b 25
163ecdff 26static int __init delayacct_setup_disable(char *str)
ca74e92b 27{
163ecdff 28 delayacct_on = 0;
ca74e92b
SN
29 return 1;
30}
163ecdff 31__setup("nodelayacct", delayacct_setup_disable);
ca74e92b
SN
32
33void delayacct_init(void)
34{
0a31bd5f 35 delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
ca74e92b
SN
36 delayacct_tsk_init(&init_task);
37}
38
39void __delayacct_tsk_init(struct task_struct *tsk)
40{
e94b1766 41 tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
ca74e92b
SN
42 if (tsk->delays)
43 spin_lock_init(&tsk->delays->lock);
44}
45
ca74e92b
SN
46/*
47 * Start accounting for a delay statistic using
48 * its starting timestamp (@start)
49 */
50
51static 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
61static void delayacct_end(struct timespec *start, struct timespec *end,
62 u64 *total, u32 *count)
63{
64 struct timespec ts;
65 s64 ns;
64efade1 66 unsigned long flags;
ca74e92b
SN
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
64efade1 74 spin_lock_irqsave(&current->delays->lock, flags);
ca74e92b
SN
75 *total += ns;
76 (*count)++;
64efade1 77 spin_unlock_irqrestore(&current->delays->lock, flags);
ca74e92b
SN
78}
79
0ff92245
SN
80void __delayacct_blkio_start(void)
81{
82 delayacct_start(&current->delays->blkio_start);
83}
84
85void __delayacct_blkio_end(void)
86{
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);
98}
6f44993f
SN
99
100int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
101{
102 s64 tmp;
172ba844
BS
103 unsigned long t1;
104 unsigned long long t2, t3;
64efade1 105 unsigned long flags;
172ba844 106 struct timespec ts;
6f44993f 107
6f44993f
SN
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
c66f08be
MN
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
6f44993f
SN
125 /*
126 * No locking available for sched_info (and too expensive to add one)
127 * Mitigate by taking snapshot of values
128 */
2d72376b 129 t1 = tsk->sched_info.pcount;
6f44993f 130 t2 = tsk->sched_info.run_delay;
9c2c4802 131 t3 = tsk->se.sum_exec_runtime;
6f44993f
SN
132
133 d->cpu_count += t1;
134
172ba844 135 tmp = (s64)d->cpu_delay_total + t2;
6f44993f
SN
136 d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
137
172ba844 138 tmp = (s64)d->cpu_run_virtual_total + t3;
6f44993f
SN
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
64efade1 144 spin_lock_irqsave(&tsk->delays->lock, flags);
6f44993f
SN
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;
016ae219
KK
149 tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
150 d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
6f44993f
SN
151 d->blkio_count += tsk->delays->blkio_count;
152 d->swapin_count += tsk->delays->swapin_count;
016ae219 153 d->freepages_count += tsk->delays->freepages_count;
64efade1 154 spin_unlock_irqrestore(&tsk->delays->lock, flags);
6f44993f
SN
155
156done:
6f44993f
SN
157 return 0;
158}
25890454
SN
159
160__u64 __delayacct_blkio_ticks(struct task_struct *tsk)
161{
162 __u64 ret;
64efade1 163 unsigned long flags;
25890454 164
64efade1 165 spin_lock_irqsave(&tsk->delays->lock, flags);
25890454
SN
166 ret = nsec_to_clock_t(tsk->delays->blkio_delay +
167 tsk->delays->swapin_delay);
64efade1 168 spin_unlock_irqrestore(&tsk->delays->lock, flags);
25890454
SN
169 return ret;
170}
171
873b4771
KK
172void __delayacct_freepages_start(void)
173{
174 delayacct_start(&current->delays->freepages_start);
175}
176
177void __delayacct_freepages_end(void)
178{
179 delayacct_end(&current->delays->freepages_start,
180 &current->delays->freepages_end,
181 &current->delays->freepages_delay,
182 &current->delays->freepages_count);
183}
184
This page took 0.431556 seconds and 5 git commands to generate.