getrusage(): fill ru_inblock and ru_oublock fields if possible
authorEric Dumazet <dada1@cosmosbay.com>
Fri, 11 May 2007 05:22:37 +0000 (22:22 -0700)
committerLinus Torvalds <torvalds@woody.linux-foundation.org>
Fri, 11 May 2007 15:29:34 +0000 (08:29 -0700)
If CONFIG_TASK_IO_ACCOUNTING is defined, we update io accounting counters for
each task.

This patch permits reporting of values using the well known getrusage()
syscall, filling ru_inblock and ru_oublock instead of null values.

As TASK_IO_ACCOUNTING currently counts bytes counts, we approximate blocks
count doing : nr_blocks = nr_bytes / 512

Example of use :
----------------------
After patch is applied, /usr/bin/time command can now give a good
approximation of IO that the process had to do.

$ /usr/bin/time grep tototo /usr/include/*
Command exited with non-zero status 1
0.00user 0.02system 0:02.11elapsed 1%CPU (0avgtext+0avgdata 0maxresident)k
24288inputs+0outputs (0major+259minor)pagefaults 0swaps

$ /usr/bin/time dd if=/dev/zero of=/tmp/testfile count=1000
1000+0 enregistrements lus
1000+0 enregistrements écrits
512000 octets (512 kB) copiés, 0,00326601 seconde, 157 MB/s
0.00user 0.00system 0:00.00elapsed 80%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+3000outputs (0major+299minor)pagefaults 0swaps

Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Cc: Oleg Nesterov <oleg@tv-sign.ru>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/sched.h
include/linux/task_io_accounting_ops.h
kernel/exit.c
kernel/fork.c
kernel/sys.c

index 17b72d88c4cb4c048f1f24e3d5b8f1e6b28b6529..75f44379b7e98091d7d0cb78f19de5bbf7b5fe5c 100644 (file)
@@ -469,6 +469,7 @@ struct signal_struct {
        cputime_t utime, stime, cutime, cstime;
        unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
        unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
+       unsigned long inblock, oublock, cinblock, coublock;
 
        /*
         * Cumulative ns of scheduled CPU time for dead threads in the
index df2a319106b2bd82e1518fdbf307d97bb96c8ed5..1218733ec6b58c27ea894f13a7b61f2f7f9d1623 100644 (file)
@@ -10,11 +10,29 @@ static inline void task_io_account_read(size_t bytes)
        current->ioac.read_bytes += bytes;
 }
 
+/*
+ * We approximate number of blocks, because we account bytes only.
+ * A 'block' is 512 bytes
+ */
+static inline unsigned long task_io_get_inblock(const struct task_struct *p)
+{
+       return p->ioac.read_bytes >> 9;
+}
+
 static inline void task_io_account_write(size_t bytes)
 {
        current->ioac.write_bytes += bytes;
 }
 
+/*
+ * We approximate number of blocks, because we account bytes only.
+ * A 'block' is 512 bytes
+ */
+static inline unsigned long task_io_get_oublock(const struct task_struct *p)
+{
+       return p->ioac.write_bytes >> 9;
+}
+
 static inline void task_io_account_cancelled_write(size_t bytes)
 {
        current->ioac.cancelled_write_bytes += bytes;
@@ -31,10 +49,20 @@ static inline void task_io_account_read(size_t bytes)
 {
 }
 
+static inline unsigned long task_io_get_inblock(const struct task_struct *p)
+{
+       return 0;
+}
+
 static inline void task_io_account_write(size_t bytes)
 {
 }
 
+static inline unsigned long task_io_get_oublock(const struct task_struct *p)
+{
+       return 0;
+}
+
 static inline void task_io_account_cancelled_write(size_t bytes)
 {
 }
index b0c6f0c3a2dfadd5e36647f2b0e5e5699174330d..7a5fd77f8fb01e0c6ef72f63972c98699bab92c9 100644 (file)
@@ -42,6 +42,7 @@
 #include <linux/audit.h> /* for audit_free() */
 #include <linux/resource.h>
 #include <linux/blkdev.h>
+#include <linux/task_io_accounting_ops.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -113,6 +114,8 @@ static void __exit_signal(struct task_struct *tsk)
                sig->nvcsw += tsk->nvcsw;
                sig->nivcsw += tsk->nivcsw;
                sig->sched_time += tsk->sched_time;
+               sig->inblock += task_io_get_inblock(tsk);
+               sig->oublock += task_io_get_oublock(tsk);
                sig = NULL; /* Marker for below. */
        }
 
@@ -1193,6 +1196,12 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
                        p->nvcsw + sig->nvcsw + sig->cnvcsw;
                psig->cnivcsw +=
                        p->nivcsw + sig->nivcsw + sig->cnivcsw;
+               psig->cinblock +=
+                       task_io_get_inblock(p) +
+                       sig->inblock + sig->cinblock;
+               psig->coublock +=
+                       task_io_get_oublock(p) +
+                       sig->oublock + sig->coublock;
                spin_unlock_irq(&p->parent->sighand->siglock);
        }
 
index 5dd3979747f5ef8e1e217d039f14ad8759e38984..da92e01aba6b0bf7f8842d3d5afe8eb72dfc292a 100644 (file)
@@ -875,6 +875,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
        sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
        sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
        sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
+       sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
        sig->sched_time = 0;
        INIT_LIST_HEAD(&sig->cpu_timers[0]);
        INIT_LIST_HEAD(&sig->cpu_timers[1]);
index cdb7e9457ba6596336b2618282f418bf13626e30..ec319bbb0bd4ac7c6644f00f26383017ca54ffd7 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/signal.h>
 #include <linux/cn_proc.h>
 #include <linux/getcpu.h>
+#include <linux/task_io_accounting_ops.h>
 
 #include <linux/compat.h>
 #include <linux/syscalls.h>
@@ -2082,6 +2083,8 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
                        r->ru_nivcsw = p->signal->cnivcsw;
                        r->ru_minflt = p->signal->cmin_flt;
                        r->ru_majflt = p->signal->cmaj_flt;
+                       r->ru_inblock = p->signal->cinblock;
+                       r->ru_oublock = p->signal->coublock;
 
                        if (who == RUSAGE_CHILDREN)
                                break;
@@ -2093,6 +2096,8 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
                        r->ru_nivcsw += p->signal->nivcsw;
                        r->ru_minflt += p->signal->min_flt;
                        r->ru_majflt += p->signal->maj_flt;
+                       r->ru_inblock += p->signal->inblock;
+                       r->ru_oublock += p->signal->oublock;
                        t = p;
                        do {
                                utime = cputime_add(utime, t->utime);
@@ -2101,6 +2106,8 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
                                r->ru_nivcsw += t->nivcsw;
                                r->ru_minflt += t->min_flt;
                                r->ru_majflt += t->maj_flt;
+                               r->ru_inblock += task_io_get_inblock(t);
+                               r->ru_oublock += task_io_get_oublock(t);
                                t = next_thread(t);
                        } while (t != p);
                        break;