Back to home page

LXR

 
 

    


0001 /* delayacct.c - per-task delay accounting
0002  *
0003  * Copyright (C) Shailabh Nagar, IBM Corp. 2006
0004  *
0005  * This program is free software;  you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it would be useful, but
0011  * WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
0013  * the GNU General Public License for more details.
0014  */
0015 
0016 #include <linux/sched.h>
0017 #include <linux/slab.h>
0018 #include <linux/taskstats.h>
0019 #include <linux/time.h>
0020 #include <linux/sysctl.h>
0021 #include <linux/delayacct.h>
0022 #include <linux/module.h>
0023 
0024 int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
0025 EXPORT_SYMBOL_GPL(delayacct_on);
0026 struct kmem_cache *delayacct_cache;
0027 
0028 static int __init delayacct_setup_disable(char *str)
0029 {
0030     delayacct_on = 0;
0031     return 1;
0032 }
0033 __setup("nodelayacct", delayacct_setup_disable);
0034 
0035 void delayacct_init(void)
0036 {
0037     delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
0038     delayacct_tsk_init(&init_task);
0039 }
0040 
0041 void __delayacct_tsk_init(struct task_struct *tsk)
0042 {
0043     tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
0044     if (tsk->delays)
0045         spin_lock_init(&tsk->delays->lock);
0046 }
0047 
0048 /*
0049  * Finish delay accounting for a statistic using its timestamps (@start),
0050  * accumalator (@total) and @count
0051  */
0052 static void delayacct_end(u64 *start, u64 *total, u32 *count)
0053 {
0054     s64 ns = ktime_get_ns() - *start;
0055     unsigned long flags;
0056 
0057     if (ns > 0) {
0058         spin_lock_irqsave(&current->delays->lock, flags);
0059         *total += ns;
0060         (*count)++;
0061         spin_unlock_irqrestore(&current->delays->lock, flags);
0062     }
0063 }
0064 
0065 void __delayacct_blkio_start(void)
0066 {
0067     current->delays->blkio_start = ktime_get_ns();
0068 }
0069 
0070 void __delayacct_blkio_end(void)
0071 {
0072     if (current->delays->flags & DELAYACCT_PF_SWAPIN)
0073         /* Swapin block I/O */
0074         delayacct_end(&current->delays->blkio_start,
0075             &current->delays->swapin_delay,
0076             &current->delays->swapin_count);
0077     else    /* Other block I/O */
0078         delayacct_end(&current->delays->blkio_start,
0079             &current->delays->blkio_delay,
0080             &current->delays->blkio_count);
0081 }
0082 
0083 int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
0084 {
0085     cputime_t utime, stime, stimescaled, utimescaled;
0086     unsigned long long t2, t3;
0087     unsigned long flags, t1;
0088     s64 tmp;
0089 
0090     task_cputime(tsk, &utime, &stime);
0091     tmp = (s64)d->cpu_run_real_total;
0092     tmp += cputime_to_nsecs(utime + stime);
0093     d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
0094 
0095     task_cputime_scaled(tsk, &utimescaled, &stimescaled);
0096     tmp = (s64)d->cpu_scaled_run_real_total;
0097     tmp += cputime_to_nsecs(utimescaled + stimescaled);
0098     d->cpu_scaled_run_real_total =
0099         (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
0100 
0101     /*
0102      * No locking available for sched_info (and too expensive to add one)
0103      * Mitigate by taking snapshot of values
0104      */
0105     t1 = tsk->sched_info.pcount;
0106     t2 = tsk->sched_info.run_delay;
0107     t3 = tsk->se.sum_exec_runtime;
0108 
0109     d->cpu_count += t1;
0110 
0111     tmp = (s64)d->cpu_delay_total + t2;
0112     d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
0113 
0114     tmp = (s64)d->cpu_run_virtual_total + t3;
0115     d->cpu_run_virtual_total =
0116         (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
0117 
0118     /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
0119 
0120     spin_lock_irqsave(&tsk->delays->lock, flags);
0121     tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
0122     d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
0123     tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
0124     d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
0125     tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
0126     d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
0127     d->blkio_count += tsk->delays->blkio_count;
0128     d->swapin_count += tsk->delays->swapin_count;
0129     d->freepages_count += tsk->delays->freepages_count;
0130     spin_unlock_irqrestore(&tsk->delays->lock, flags);
0131 
0132     return 0;
0133 }
0134 
0135 __u64 __delayacct_blkio_ticks(struct task_struct *tsk)
0136 {
0137     __u64 ret;
0138     unsigned long flags;
0139 
0140     spin_lock_irqsave(&tsk->delays->lock, flags);
0141     ret = nsec_to_clock_t(tsk->delays->blkio_delay +
0142                 tsk->delays->swapin_delay);
0143     spin_unlock_irqrestore(&tsk->delays->lock, flags);
0144     return ret;
0145 }
0146 
0147 void __delayacct_freepages_start(void)
0148 {
0149     current->delays->freepages_start = ktime_get_ns();
0150 }
0151 
0152 void __delayacct_freepages_end(void)
0153 {
0154     delayacct_end(&current->delays->freepages_start,
0155             &current->delays->freepages_delay,
0156             &current->delays->freepages_count);
0157 }
0158