Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

getrusage(): fill ru_inblock and ru_oublock fields if possible

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>

authored by

Eric Dumazet and committed by
Linus Torvalds
6eaeeaba 02239c29

+46
+1
include/linux/sched.h
··· 469 469 cputime_t utime, stime, cutime, cstime; 470 470 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw; 471 471 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt; 472 + unsigned long inblock, oublock, cinblock, coublock; 472 473 473 474 /* 474 475 * Cumulative ns of scheduled CPU time for dead threads in the
+28
include/linux/task_io_accounting_ops.h
··· 10 10 current->ioac.read_bytes += bytes; 11 11 } 12 12 13 + /* 14 + * We approximate number of blocks, because we account bytes only. 15 + * A 'block' is 512 bytes 16 + */ 17 + static inline unsigned long task_io_get_inblock(const struct task_struct *p) 18 + { 19 + return p->ioac.read_bytes >> 9; 20 + } 21 + 13 22 static inline void task_io_account_write(size_t bytes) 14 23 { 15 24 current->ioac.write_bytes += bytes; 25 + } 26 + 27 + /* 28 + * We approximate number of blocks, because we account bytes only. 29 + * A 'block' is 512 bytes 30 + */ 31 + static inline unsigned long task_io_get_oublock(const struct task_struct *p) 32 + { 33 + return p->ioac.write_bytes >> 9; 16 34 } 17 35 18 36 static inline void task_io_account_cancelled_write(size_t bytes) ··· 49 31 { 50 32 } 51 33 34 + static inline unsigned long task_io_get_inblock(const struct task_struct *p) 35 + { 36 + return 0; 37 + } 38 + 52 39 static inline void task_io_account_write(size_t bytes) 53 40 { 41 + } 42 + 43 + static inline unsigned long task_io_get_oublock(const struct task_struct *p) 44 + { 45 + return 0; 54 46 } 55 47 56 48 static inline void task_io_account_cancelled_write(size_t bytes)
+9
kernel/exit.c
··· 42 42 #include <linux/audit.h> /* for audit_free() */ 43 43 #include <linux/resource.h> 44 44 #include <linux/blkdev.h> 45 + #include <linux/task_io_accounting_ops.h> 45 46 46 47 #include <asm/uaccess.h> 47 48 #include <asm/unistd.h> ··· 114 113 sig->nvcsw += tsk->nvcsw; 115 114 sig->nivcsw += tsk->nivcsw; 116 115 sig->sched_time += tsk->sched_time; 116 + sig->inblock += task_io_get_inblock(tsk); 117 + sig->oublock += task_io_get_oublock(tsk); 117 118 sig = NULL; /* Marker for below. */ 118 119 } 119 120 ··· 1196 1193 p->nvcsw + sig->nvcsw + sig->cnvcsw; 1197 1194 psig->cnivcsw += 1198 1195 p->nivcsw + sig->nivcsw + sig->cnivcsw; 1196 + psig->cinblock += 1197 + task_io_get_inblock(p) + 1198 + sig->inblock + sig->cinblock; 1199 + psig->coublock += 1200 + task_io_get_oublock(p) + 1201 + sig->oublock + sig->coublock; 1199 1202 spin_unlock_irq(&p->parent->sighand->siglock); 1200 1203 } 1201 1204
+1
kernel/fork.c
··· 875 875 sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero; 876 876 sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0; 877 877 sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0; 878 + sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0; 878 879 sig->sched_time = 0; 879 880 INIT_LIST_HEAD(&sig->cpu_timers[0]); 880 881 INIT_LIST_HEAD(&sig->cpu_timers[1]);
+7
kernel/sys.c
··· 29 29 #include <linux/signal.h> 30 30 #include <linux/cn_proc.h> 31 31 #include <linux/getcpu.h> 32 + #include <linux/task_io_accounting_ops.h> 32 33 33 34 #include <linux/compat.h> 34 35 #include <linux/syscalls.h> ··· 2083 2082 r->ru_nivcsw = p->signal->cnivcsw; 2084 2083 r->ru_minflt = p->signal->cmin_flt; 2085 2084 r->ru_majflt = p->signal->cmaj_flt; 2085 + r->ru_inblock = p->signal->cinblock; 2086 + r->ru_oublock = p->signal->coublock; 2086 2087 2087 2088 if (who == RUSAGE_CHILDREN) 2088 2089 break; ··· 2096 2093 r->ru_nivcsw += p->signal->nivcsw; 2097 2094 r->ru_minflt += p->signal->min_flt; 2098 2095 r->ru_majflt += p->signal->maj_flt; 2096 + r->ru_inblock += p->signal->inblock; 2097 + r->ru_oublock += p->signal->oublock; 2099 2098 t = p; 2100 2099 do { 2101 2100 utime = cputime_add(utime, t->utime); ··· 2106 2101 r->ru_nivcsw += t->nivcsw; 2107 2102 r->ru_minflt += t->min_flt; 2108 2103 r->ru_majflt += t->maj_flt; 2104 + r->ru_inblock += task_io_get_inblock(t); 2105 + r->ru_oublock += task_io_get_oublock(t); 2109 2106 t = next_thread(t); 2110 2107 } while (t != p); 2111 2108 break;