Skip to contentSkip to conversation

taskstats: Add e/u/stime for TGID command

PATCH Applied Zhang Xiao 2 participants 3 messages Mar 3, 2017 – Mar 7, 2017 Taskstats statistics interface

lore
Share & tools
lore

Zhang Xiao

author
#0 lore raw

PATCH 1/1 taskstats: Add e/u/stime for TGID command

Add elapsed time, user CPU time and system CPU time to thread group status request.

---
 kernel/taskstats.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
kernel/taskstats.c +11 -0
index 8a5e442..802a2bb 100644
@@ -210,6 +210,7 @@ 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;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -238,6 +239,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 = ktime_get_ns() - 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);
Signature · 1 line
2.1.4
#1 lore raw
Quoted 20 lines · On Fri, 3 Mar 2017 10:33:44 +0800 Zhang Xiao <[email protected]> wrote:

Add elapsed time, user CPU time and system CPU time to thread group status request.

kernel/taskstats.c +4 -0
@@ -210,6 +210,7 @@ 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;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -238,6 +239,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 = ktime_get_ns() - tsk->start_time;
+		/* Convert to micro seconds */

kernel/taskstats.c static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) · line 254

Quoted 10 lines
kernel/taskstats.c +7 -0
+		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);

hm, OK, we were just leaving these at zero. Seems sane to me.

It would be more efficient and perhaps more deterministic to sample ktime just once?

kernel/taskstats.c renamed from kernel/taskstats.c~taskstats-add-e-u-stime-for-tgid-command-fix +3 -1
@@ -211,6 +211,7 @@ static int fill_stats_for_tgid(pid_t tgi
 	unsigned long flags;
 	int rc = -ESRCH;
 	u64 delta, utime, stime;
+	u64 start_time;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -228,6 +229,7 @@ static int fill_stats_for_tgid(pid_t tgi
 		memset(stats, 0, sizeof(*stats));
 
 	tsk = first;
+	start_time = ktime_get_ns();
 	do {
 		if (tsk->exit_state)
 			continue;
@@ -240,7 +242,7 @@ static int fill_stats_for_tgid(pid_t tgi
 		delayacct_add_tsk(stats, tsk);
 
 		/* calculate task elapsed time in nsec */
-		delta = ktime_get_ns() - tsk->start_time;
+		delta = start_time - tsk->start_time;
 		/* Convert to micro seconds */
 		do_div(delta, NSEC_PER_USEC);
 		stats->ac_etime += delta;

_

author
#2 lore raw
Quoted 35 lines · Andrew Morton
Quoted 20 lines · On Fri, 3 Mar 2017 10:33:44 +0800 Zhang Xiao <[email protected]> wrote:

Add elapsed time, user CPU time and system CPU time to thread group status request.

kernel/taskstats.c +4 -0
@@ -210,6 +210,7 @@ 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;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -238,6 +239,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 = ktime_get_ns() - tsk->start_time;
+		/* Convert to micro seconds */

kernel/taskstats.c static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) · line 254

Quoted 10 lines
kernel/taskstats.c +7 -0
+		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);

hm, OK, we were just leaving these at zero. Seems sane to me.

Quoted 3 lines

It would be more efficient and perhaps more deterministic to sample ktime just once?

yes, that makes the code more readable. :-)

Thanks
Xiao

Quoted 30 lines
kernel/taskstats.c renamed from kernel/taskstats.c~taskstats-add-e-u-stime-for-tgid-command-fix +3 -1
@@ -211,6 +211,7 @@ static int fill_stats_for_tgid(pid_t tgi
 	unsigned long flags;
 	int rc = -ESRCH;
 	u64 delta, utime, stime;
+	u64 start_time;
 
 	/*
 	 * Add additional stats from live tasks except zombie thread group
@@ -228,6 +229,7 @@ static int fill_stats_for_tgid(pid_t tgi
 		memset(stats, 0, sizeof(*stats));
 
 	tsk = first;
+	start_time = ktime_get_ns();
 	do {
 		if (tsk->exit_state)
 			continue;
@@ -240,7 +242,7 @@ static int fill_stats_for_tgid(pid_t tgi
 		delayacct_add_tsk(stats, tsk);
 
 		/* calculate task elapsed time in nsec */
-		delta = ktime_get_ns() - tsk->start_time;
+		delta = start_time - tsk->start_time;
 		/* Convert to micro seconds */
 		do_div(delta, NSEC_PER_USEC);
 		stats->ac_etime += delta;

_