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/time.h>
19 #include <linux/sysctl.h>
20 #include <linux/delayacct.h>
22 int delayacct_on __read_mostly; /* Delay accounting turned on/off */
23 kmem_cache_t *delayacct_cache;
25 static int __init delayacct_setup_enable(char *str)
30 __setup("delayacct", delayacct_setup_enable);
32 void delayacct_init(void)
34 delayacct_cache = kmem_cache_create("delayacct_cache",
35 sizeof(struct task_delay_info),
39 delayacct_tsk_init(&init_task);
42 void __delayacct_tsk_init(struct task_struct *tsk)
44 tsk->delays = kmem_cache_zalloc(delayacct_cache, SLAB_KERNEL);
46 spin_lock_init(&tsk->delays->lock);
49 void __delayacct_tsk_exit(struct task_struct *tsk)
51 kmem_cache_free(delayacct_cache, tsk->delays);
56 * Start accounting for a delay statistic using
57 * its starting timestamp (@start)
60 static inline void delayacct_start(struct timespec *start)
62 do_posix_clock_monotonic_gettime(start);
66 * Finish delay accounting for a statistic using
67 * its timestamps (@start, @end), accumalator (@total) and @count
70 static void delayacct_end(struct timespec *start, struct timespec *end,
71 u64 *total, u32 *count)
76 do_posix_clock_monotonic_gettime(end);
77 ts = timespec_sub(*end, *start);
78 ns = timespec_to_ns(&ts);
82 spin_lock(¤t->delays->lock);
85 spin_unlock(¤t->delays->lock);
88 void __delayacct_blkio_start(void)
90 delayacct_start(¤t->delays->blkio_start);
93 void __delayacct_blkio_end(void)
95 if (current->delays->flags & DELAYACCT_PF_SWAPIN)
96 /* Swapin block I/O */
97 delayacct_end(¤t->delays->blkio_start,
98 ¤t->delays->blkio_end,
99 ¤t->delays->swapin_delay,
100 ¤t->delays->swapin_count);
101 else /* Other block I/O */
102 delayacct_end(¤t->delays->blkio_start,
103 ¤t->delays->blkio_end,
104 ¤t->delays->blkio_delay,
105 ¤t->delays->blkio_count);