Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932822AbdCGU2c (ORCPT ); Tue, 7 Mar 2017 15:28:32 -0500 Received: from mail.windriver.com ([147.11.1.11]:64536 "EHLO mail.windriver.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751633AbdCGU23 (ORCPT ); Tue, 7 Mar 2017 15:28:29 -0500 Subject: Re: [PATCH] taskstats: Add e/u/stime for TGID command To: Andrew Morton References: <1488508424-12322-1-git-send-email-xiao.zhang@windriver.com> <20170306144017.1a2cec98a3fe3855fd0504c6@linux-foundation.org> Cc: xiao.zhang@windriver.com, bsingharora@gmail.com, linux-kernel@vger.kernel.org, Oleg Nesterov From: ZhangXiao Message-ID: <58BE08E4.5030603@windriver.com> Date: Tue, 7 Mar 2017 09:12:04 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <20170306144017.1a2cec98a3fe3855fd0504c6@linux-foundation.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2314 Lines: 77 在 2017年03月07日 06:40, Andrew Morton 写道: > On Fri, 3 Mar 2017 10:33:44 +0800 Zhang Xiao wrote: > >> Add elapsed time, user CPU time and system CPU time to >> thread group status request. >> >> --- a/kernel/taskstats.c >> +++ b/kernel/taskstats.c >> @@ -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); > > 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? > yes, that makes the code more readable. :-) Thanks Xiao > --- a/kernel/taskstats.c~taskstats-add-e-u-stime-for-tgid-command-fix > +++ a/kernel/taskstats.c > @@ -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; > _ > >