mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dario Faggioli <raistlin@linux.it>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	torbenh <torbenh@gmx.de>, oleg <oleg@redhat.com>,
	john.stultz@linaro.org, roland@redhat.com,
	Ingo Molnar <mingo@elte.hu>,
	Peter Zijlstra <peterz@infradead.org>,
	Stanislaw Gruszka <sgruszka@redhat.com>,
	Dhaval Giani <dhaval.giani@gmail.com>,
	Randy Dunlap <rdunlap@xenotime.net>
Subject: [PATCH resend] Reading POSIX CPU timer from outside the process.
Date: Tue, 28 Dec 2010 11:55:42 +0100	[thread overview]
Message-ID: <1293533742.2899.1028.camel@Palantir> (raw)
In-Reply-To: <1293121303.3390.185.camel@Palantir>

[-- Attachment #1: Type: text/plain, Size: 3423 bytes --]

Trying to call clock_getcpuclockid (and then clock_gettime) on a thread
from outside the process that spawned it results in this:
  ### Testing tid 24207: CPU-time clock for PID 24207 is 1.132371729 seconds
  ### Testing tid 24209: clock_getcpuclockid: Success

OTOH, if full-fledged processes are involved, the behaviour is this:
  ### Testing tid 24218: CPU-time clock for PID 24218 is 0.001059305 seconds
  ### Testing tid 24220: CPU-time clock for PID 24220 is 1.044057391 seconds

Test programs available here: http://gitorious.org/clockid

All that because clock_getcpuclockid forbids accessing thread
specific CPU-time clocks from outside the thread group, but this is not
requested (e.g., by POSIX), or at least no indication that it should fail
can be found in `man clock_getcpuclockid' and alike.
Moreover, having such capability could be useful, if you want to monitor
the execution of a bunch of thread from some kind of "manager" which might
not be part of the same process. A typical example is the JACK graph-manager.

Therefore, this commit makes clock_getcpuclockid behave as follows:
 - if it's called on a tid which is also a PID (i.e., the thread is
   a thread group leader), it returns the CLOCK_PROCESS_CPUTIME_ID of
   the process;
 - if it's called on a tid of a non-group leader, it returns the
   CLOCK_THREAD_CPUTIME_ID of such specific thread.

This enables the following behaviour, for the threaded and process-based
scenarios, respectively:

  ### Testing tid 24704: CPU-time clock for PID 24704 is 1.049570008 seconds
  ### Testing tid 24706: CPU-time clock for PID 24706 is 1.028650801 seconds

  ### Testing tid 24715: CPU-time clock for PID 24715 is 0.000957685 seconds
  ### Testing tid 24717: CPU-time clock for PID 24717 is 1.045351509 seconds

Signed-off-by: Dario Faggioli <raistlin@linux.it>
---
 kernel/posix-cpu-timers.c |   13 +++++--------
 1 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 05bb717..ddbbabc 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -39,10 +39,9 @@ static int check_clock(const clockid_t which_clock)
 
 	rcu_read_lock();
 	p = find_task_by_vpid(pid);
-	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
-		   same_thread_group(p, current) : has_group_leader_pid(p))) {
+	if (!p || (CPUCLOCK_PERTHREAD(which_clock) &&
+		   same_thread_group(p, current) && !has_group_leader_pid(p)))
 		error = -EINVAL;
-	}
 	rcu_read_unlock();
 
 	return error;
@@ -349,11 +348,9 @@ int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
 		rcu_read_lock();
 		p = find_task_by_vpid(pid);
 		if (p) {
-			if (CPUCLOCK_PERTHREAD(which_clock)) {
-				if (same_thread_group(p, current)) {
-					error = cpu_clock_sample(which_clock,
-								 p, &rtn);
-				}
+			if (CPUCLOCK_PERTHREAD(which_clock) ||
+			    !thread_group_leader(p)) {
+				error = cpu_clock_sample(which_clock, p, &rtn);
 			} else {
 				read_lock(&tasklist_lock);
 				if (thread_group_leader(p) && p->sighand) {
-- 
1.7.2.3


-- 
<<This happens because I choose it to happen!>> (Raistlin Majere)
----------------------------------------------------------------------
Dario Faggioli, ReTiS Lab, Scuola Superiore Sant'Anna, Pisa  (Italy)

http://retis.sssup.it/people/faggioli -- dario.faggioli@jabber.org

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2010-12-28 10:55 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-23 16:21 [PATCH] Read THREAD_CPUTIME clock from other processes Dario Faggioli
2010-12-23 16:44 ` Oleg Nesterov
2010-12-23 17:38   ` Dario Faggioli
2010-12-23 18:12     ` Oleg Nesterov
2010-12-24 11:36     ` Dario Faggioli
2010-12-23 17:21 ` Randy Dunlap
2010-12-23 17:43   ` Dario Faggioli
2010-12-28 10:55 ` Dario Faggioli [this message]
2010-12-28 16:38   ` [PATCH resend] Reading POSIX CPU timer from outside the process Oleg Nesterov
2010-12-28 21:38     ` Dario Faggioli
2010-12-29 13:21       ` Oleg Nesterov
2010-12-29 14:10         ` Dario Faggioli
2010-12-29 18:30           ` Oleg Nesterov
2010-12-30 17:45         ` torbenh
2011-01-04 11:01           ` Dario Faggioli
2011-01-06 16:06             ` torbenh
2011-01-07 19:28 ` [PATCH] Read THREAD_CPUTIME clock from other processes Roland McGrath
2011-01-07 19:35   ` Oleg Nesterov
2011-01-07 19:50     ` Roland McGrath
2011-01-07 19:49       ` Oleg Nesterov
2011-01-07 19:58         ` Roland McGrath
2011-01-07 19:56       ` Peter Zijlstra
2011-01-08 11:12   ` Dario Faggioli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1293533742.2899.1028.camel@Palantir \
    --to=raistlin@linux.it \
    --cc=dhaval.giani@gmail.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rdunlap@xenotime.net \
    --cc=roland@redhat.com \
    --cc=sgruszka@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=torbenh@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome