Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / delayacct.c
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>
18 #include <linux/taskstats.h>
19 #include <linux/time.h>
20 #include <linux/sysctl.h>
21 #include <linux/delayacct.h>
22
23 int delayacct_on __read_mostly = 1;     /* Delay accounting turned on/off */
24 struct kmem_cache *delayacct_cache;
25
26 static int __init delayacct_setup_disable(char *str)
27 {
28         delayacct_on = 0;
29         return 1;
30 }
31 __setup("nodelayacct", delayacct_setup_disable);
32
33 void delayacct_init(void)
34 {
35         delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
36         delayacct_tsk_init(&init_task);
37 }
38
39 void __delayacct_tsk_init(struct task_struct *tsk)
40 {
41         tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
42         if (tsk->delays)
43                 spin_lock_init(&tsk->delays->lock);
44 }
45
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;
66         unsigned long flags;
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
74         spin_lock_irqsave(&current->delays->lock, flags);
75         *total += ns;
76         (*count)++;
77         spin_unlock_irqrestore(&current->delays->lock, flags);
78 }
79
80 void __delayacct_blkio_start(void)
81 {
82         delayacct_start(&current->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(&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 }
99
100 int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
101 {
102         s64 tmp;
103         unsigned long t1;
104         unsigned long long t2, t3;
105         unsigned long flags;
106         struct timespec ts;
107
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
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
125         /*
126          * No locking available for sched_info (and too expensive to add one)
127          * Mitigate by taking snapshot of values
128          */
129         t1 = tsk->sched_info.pcount;
130         t2 = tsk->sched_info.run_delay;
131         t3 = tsk->se.sum_exec_runtime;
132
133         d->cpu_count += t1;
134
135         tmp = (s64)d->cpu_delay_total + t2;
136         d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
137
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;
141
142         /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
143
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);
155
156 done:
157         return 0;
158 }
159
160 __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
161 {
162         __u64 ret;
163         unsigned long flags;
164
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;
170 }
171
172 void __delayacct_freepages_start(void)
173 {
174         delayacct_start(&current->delays->freepages_start);
175 }
176
177 void __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