Skip to content

Commit

Permalink
taskstats: add e/u/stime for TGID command
Browse files Browse the repository at this point in the history
The elapsed time, user CPU time and system CPU time for the thread group
status request are presently left at zero.  Fill these in.

[[email protected]: run ktime_get_ns() a single time]
[[email protected]: include linux/sched/cputime.h for task_cputime()]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Zhang Xiao <[email protected]>
Cc: Balbir Singh <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
xzhang1 authored and torvalds committed May 9, 2017
1 parent eaa0d19 commit 8c73342
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kernel/taskstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <linux/pid_namespace.h>
#include <net/genetlink.h>
#include <linux/atomic.h>
#include <linux/sched/cputime.h>

/*
* Maximum length of a cpumask that can be specified in
Expand Down Expand Up @@ -210,6 +211,8 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
struct task_struct *tsk, *first;
unsigned long flags;
int rc = -ESRCH;
u64 delta, utime, stime;
u64 start_time;

/*
* Add additional stats from live tasks except zombie thread group
Expand All @@ -227,6 +230,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
memset(stats, 0, sizeof(*stats));

tsk = first;
start_time = ktime_get_ns();
do {
if (tsk->exit_state)
continue;
Expand All @@ -238,6 +242,16 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
*/
delayacct_add_tsk(stats, tsk);

/* calculate task elapsed time in nsec */
delta = start_time - tsk->start_time;
/* Convert to micro seconds */
do_div(delta, NSEC_PER_USEC);
stats->ac_etime += delta;

task_cputime(tsk, &utime, &stime);
stats->ac_utime += div_u64(utime, NSEC_PER_USEC);
stats->ac_stime += div_u64(stime, NSEC_PER_USEC);

stats->nvcsw += tsk->nvcsw;
stats->nivcsw += tsk->nivcsw;
} while_each_thread(first, tsk);
Expand Down

0 comments on commit 8c73342

Please sign in to comment.