mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christoph Lameter <clameter@sgi.com>
To: Ulrich Drepper <drepper@redhat.com>
Cc: johnstul@us.ibm.com, Ulrich.Windl@rz.uni-regensburg.de,
	george@mvista.com, jbarnes@sgi.com, linux-kernel@vger.kernel.org,
	libc-alpha@sources.redhat.com
Subject: Posix compliant CLOCK_PROCESS/THREAD_CPUTIME_ID V4
Date: Tue, 28 Sep 2004 20:25:48 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0409282017340.18604@schroedinger.engr.sgi.com> (raw)
In-Reply-To: <4159B920.3040802@redhat.com>

George asked for a test program so I wrote one and debugged the patch.
The test program uses syscall to bypass glibc processing. I have been
working on a patch for glibc but that gets a bit complicated
because backwards compatibility has to be kept. Maybe tomorrow.
Found also that glibc allows the setting of these clocks so I also
implemented that and used it in the test program.  Setting these
clocks modifies stime and utime directly, which may not be such a good
idea. Do we really need to be able to set these clocks?

So it actually works now. Test output, test program and revised patch:

christoph@athlon:~$ ./test_cputime
Single Thread Testing
  CLOCK_THREAD_CPUTIME_ID=          0.373943152 resolution= 0.000999848
 CLOCK_PROCESS_CPUTIME_ID=          0.373943152 resolution= 0.000999848
Multi Thread Testing
Starting Thread: 0 1 2 3 4 5 6 7 8 9
 Joining Thread: 0 1 2 3 4 5 6 7 8 9
0 Cycles=      0 Thread=  0.000000000ns Process=  0.000000000ns
1 Cycles=1000000 Thread=  0.037994224ns Process=  0.507922784ns
2 Cycles=2000000 Thread=  0.073988752ns Process=  0.097985104ns
3 Cycles=3000000 Thread=  0.108983432ns Process=  0.612906824ns
4 Cycles=4000000 Thread=  0.146977656ns Process=  0.657899984ns
5 Cycles=5000000 Thread=  0.182972184ns Process=  0.739887520ns
6 Cycles=6000000 Thread=  0.217966864ns Process=  1.456778536ns
7 Cycles=7000000 Thread=  0.254961240ns Process=  1.461777776ns
8 Cycles=8000000 Thread=  0.290955768ns Process=  1.627752544ns
9 Cycles=9000000 Thread=  0.326950296ns Process=  1.641750416ns

Clock status at the end of the timer tests:
          Gettimeofday() = 1096427813.738929000
           CLOCK_REALTIME= 1096427813.738941000 resolution= 0.000999848
          CLOCK_MONOTONIC=        161.938418328 resolution= 0.000999848
 CLOCK_PROCESS_CPUTIME_ID=          1.641750416 resolution= 0.000999848
  CLOCK_THREAD_CPUTIME_ID=          0.000000000 resolution= 0.000999848

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <asm/unistd.h>
#include <pthread.h>

#define clock_getres(x,y) syscall(__NR_clock_getres, x,y)
#define clock_gettime(x,y) syscall(__NR_clock_gettime, x, y)
#define clock_settime(x,y) syscall(__NR_clock_settime, x, y)

void pr(int clock,const char *n)
{
	struct timespec tv = {1,2};
	struct timespec res = {3,4};
	int rc;


	rc=clock_getres(clock,&res);
	if (rc) {
		printf("getres return code on %s=%d errno=%d\n",n,rc,errno);
	}
	rc=clock_gettime(clock,&tv);
	if (rc) {
		printf("gettime return code on %s=%d errno=%d\n",n,rc, errno);
	}
	else
	printf("%25s=% 11d.%09d resolution=% 2d.%09d\n",n,tv.tv_sec,tv.tv_nsec,res.tv_sec,res.tv_nsec);
}

int y;

void kx(long long x) {
	y=x;
};

struct timespec zero;

pthread_t thread[10];

struct tinfo {
	int i;
	struct timespec ttime,ptime;
} tinf[10];

void *thread_function(void *x) {
	struct tinfo *t=x;
	int i;

	for(i=1;i< t->i;i++) kx(1000000000000LL/i);
	clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t->ttime);
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&t->ptime);
}

int main(char argc, char *argv[])
{
	struct timespec tv;
	int i;

	/* Waste some time */
	printf("Single Thread Testing\n");

	for(i=1;i<10000000;i++) kx(1000000000000LL/i);
	pr(CLOCK_THREAD_CPUTIME_ID,"CLOCK_THREAD_CPUTIME_ID");
	pr(CLOCK_PROCESS_CPUTIME_ID,"CLOCK_PROCESS_CPUTIME_ID");
	/* Waste some more time in threads */
	printf("Multi Thread Testing\nStarting Thread:");
	clock_settime(CLOCK_PROCESS_CPUTIME_ID,&zero);
	for(i=0;i<10;i++) {
		tinf[i].i=i*1000000;
		if (pthread_create(&thread[i], NULL, thread_function, tinf+i))
			perror("thread");
		else
			printf(" %d",i);
	}
	printf("\n Joining Thread:");
	for(i=0;i<10;i++) if (pthread_join( thread[i], NULL)) perror("join"); else printf(" %d",i);
	printf("\n");
	for(i=0;i<10;i++) {
		printf("%d Cycles=%7d Thread=% 3d.%09dns Process=% 3d.%09dns\n",i,tinf[i].i,tinf[i].ttime.tv_sec,tinf[i].ttime.tv_nsec,tinf[i].ptime.tv_sec,tinf[i].ptime.tv_nsec);
	}
	gettimeofday((struct timeval *)&tv);
	tv.tv_nsec = tv.tv_nsec*1000;
	printf("\nClock status at the end of the timer tests:\n");
	printf("          Gettimeofday() =% 11d.%09d\n",tv.tv_sec,tv.tv_nsec);
	pr(CLOCK_REALTIME,"CLOCK_REALTIME");
	pr(CLOCK_MONOTONIC,"CLOCK_MONOTONIC");
	pr(CLOCK_PROCESS_CPUTIME_ID,"CLOCK_PROCESS_CPUTIME_ID");
	pr(CLOCK_THREAD_CPUTIME_ID,"CLOCK_THREAD_CPUTIME_ID");
	printf("\n");
}

Index: linux-2.6.9-rc2/kernel/posix-timers.c
===================================================================
--- linux-2.6.9-rc2.orig/kernel/posix-timers.c	2004-09-12 22:32:48.000000000 -0700
+++ linux-2.6.9-rc2/kernel/posix-timers.c	2004-09-28 19:49:59.919624581 -0700
@@ -10,6 +10,10 @@
  * 2004-06-01  Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
  *			     Copyright (C) 2004 Boris Hu
  *
+ * 2004-07-27 Provide POSIX compliant clocks
+ *		CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID.
+ *		by Christoph Lameter
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or (at
@@ -133,18 +137,10 @@
  *	    resolution.	 Here we define the standard CLOCK_REALTIME as a
  *	    1/HZ resolution clock.
  *
- * CPUTIME & THREAD_CPUTIME: We are not, at this time, definding these
- *	    two clocks (and the other process related clocks (Std
- *	    1003.1d-1999).  The way these should be supported, we think,
- *	    is to use large negative numbers for the two clocks that are
- *	    pinned to the executing process and to use -pid for clocks
- *	    pinned to particular pids.	Calls which supported these clock
- *	    ids would split early in the function.
- *
  * RESOLUTION: Clock resolution is used to round up timer and interval
  *	    times, NOT to report clock times, which are reported with as
  *	    much resolution as the system can muster.  In some cases this
- *	    resolution may depend on the underlaying clock hardware and
+ *	    resolution may depend on the underlying clock hardware and
  *	    may not be quantifiable until run time, and only then is the
  *	    necessary code is written.	The standard says we should say
  *	    something about this issue in the documentation...
@@ -162,7 +158,7 @@
  *
  *          At this time all functions EXCEPT clock_nanosleep can be
  *          redirected by the CLOCKS structure.  Clock_nanosleep is in
- *          there, but the code ignors it.
+ *          there, but the code ignores it.
  *
  * Permissions: It is assumed that the clock_settime() function defined
  *	    for each clock will take care of permission checks.	 Some
@@ -198,6 +194,10 @@
 	struct timespec *tp, struct timespec *mo);
 int do_posix_clock_monotonic_gettime(struct timespec *tp);
 int do_posix_clock_monotonic_settime(struct timespec *tp);
+int do_posix_clock_process_gettime(struct timespec *tp);
+int do_posix_clock_process_settime(struct timespec *tp);
+int do_posix_clock_thread_gettime(struct timespec *tp);
+int do_posix_clock_thread_settime(struct timespec *tp);
 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);

 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
@@ -218,6 +218,16 @@
 		.clock_get = do_posix_clock_monotonic_gettime,
 		.clock_set = do_posix_clock_monotonic_settime
 	};
+	struct k_clock clock_thread = {.res = CLOCK_REALTIME_RES,
+		.abs_struct = NULL,
+		.clock_get = do_posix_clock_thread_gettime,
+		.clock_set = do_posix_clock_thread_settime
+	};
+	struct k_clock clock_process = {.res = CLOCK_REALTIME_RES,
+		.abs_struct = NULL,
+		.clock_get = do_posix_clock_process_gettime,
+		.clock_set = do_posix_clock_process_settime
+	};

 #ifdef CONFIG_TIME_INTERPOLATION
 	/* Clocks are more accurate with time interpolators */
@@ -226,6 +236,8 @@

 	register_posix_clock(CLOCK_REALTIME, &clock_realtime);
 	register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
+	register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &clock_process);
+	register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &clock_thread);

 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
 					sizeof (struct k_itimer), 0, 0, NULL, NULL);
@@ -1227,6 +1239,76 @@
 	return -EINVAL;
 }

+/*
+ * Single Unix Specification V3:
+ *
+ * Implementations shall also support the special clockid_t value
+ * CLOCK_THREAD_CPUTIME_ID, which represents the CPU-time clock of the calling
+ * thread when invoking one of the clock_*() or timer_*() functions. For these
+ * clock IDs, the values returned by clock_gettime() and specified by
+ * clock_settime() shall represent the amount of execution time of the thread
+ * associated with the clock.
+ */
+int do_posix_clock_thread_gettime(struct timespec *tp)
+{
+	jiffies_to_timespec(current->utime + current->stime, tp);
+	return 0;
+}
+
+int do_posix_clock_thread_settime(struct timespec *tp)
+{
+	current->stime = 0;
+	current->utime = timespec_to_jiffies(tp);
+	return 0;
+}
+
+/*
+ * Single Unix Specification V3:
+ *
+ * Implementations shall also support the special clockid_t value
+ * CLOCK_PROCESS_CPUTIME_ID, which represents the CPU-time clock of the
+ * calling process when invoking one of the clock_*() or timer_*() functions.
+ * For these clock IDs, the values returned by clock_gettime() and specified
+ * by clock_settime() represent the amount of execution time of the process
+ * associated with the clock.
+ */
+int do_posix_clock_process_gettime(struct timespec *tp)
+{
+	unsigned long ticks;
+	task_t *t;
+
+	/* The signal structure is shared between all threads */
+	ticks = current->signal->utime + current->signal->stime;
+
+	/* Add up the cpu time for all the still running threads of this process */
+	t = current;
+	do {
+		ticks += t->utime + t->stime;
+		t = next_thread(t);
+	} while (t != current);
+
+	jiffies_to_timespec(ticks, tp);
+	return 0;
+}
+
+int do_posix_clock_process_settime(struct timespec *tp)
+{
+	task_t *t;
+	/*
+	 * Set all other threads to zero cs/cutime and then set up the
+	 * desired time in the current thread
+	 */
+
+	for (t = next_thread(current); t != current; t = next_thread(t))
+	{
+		t->stime = 0;
+		t->utime = 0;
+	}
+
+	do_posix_clock_thread_settime(tp);
+	return 0;
+}
+
 asmlinkage long
 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
 {

  parent reply	other threads:[~2004-09-29  3:27 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <B6E8046E1E28D34EB815A11AC8CA312902CD3264@mtv-atc-605e--n.corp.sgi.com>
2004-09-24 12:16 ` [time] add support for CLOCK_THREAD_CPUTIME_ID and CLOCK_PROCESS_CPUTIME_ID Christoph Lameter
2004-09-25  4:25   ` Ulrich Drepper
2004-09-25  5:25     ` Christoph Lameter
2004-09-25  5:54     ` Christoph Lameter
2004-09-25  6:08       ` Ulrich Drepper
2004-09-25 14:51         ` Christoph Lameter
2004-09-25 15:19           ` Ulrich Drepper
2004-09-27 15:03             ` Christoph Lameter
2004-09-27 15:34         ` Christoph Lameter
     [not found]         ` <B6E8046E1E28D34EB815A11AC8CA312902CD327E@mtv-atc-605e--n.corp.sgi.com>
2004-09-27 20:58           ` [RFC] Posix compliant behavior of CLOCK_PROCESS/THREAD_CPUTIME_ID Christoph Lameter
2004-09-27 22:54             ` George Anzinger
2004-09-28 19:18             ` Ulrich Drepper
2004-09-28 19:25               ` Christoph Lameter
2004-09-29  3:25               ` Christoph Lameter [this message]
2004-09-29 17:45                 ` Posix compliant CLOCK_PROCESS/THREAD_CPUTIME_ID V4 George Anzinger
2004-09-29 18:14                   ` Christoph Lameter
2004-09-29 19:27                     ` George Anzinger
2004-09-29 19:34                       ` Christoph Lameter
2004-09-29 19:52                       ` Jesper Juhl
2004-09-30  0:14                         ` patches inline in mail George Anzinger
2004-09-30  3:24                           ` Paul Jackson
2004-10-01  5:29                           ` Andrew Morton
2004-10-01 12:28                             ` Alan Cox
2004-10-01 13:42                               ` Paul Fulghum
2004-10-01 19:53                                 ` Lee Revell
2004-10-01 21:58                               ` George Anzinger
2004-10-02 15:52                                 ` Olaf Dietsche
2004-10-02 15:20                                   ` Alan Cox
2004-10-03 21:01                                     ` [OT] " Guennadi Liakhovetski
2004-10-03 23:18                                       ` Jesper Juhl
2004-10-04  6:20                                       ` Paul Jackson
2004-10-04 19:11                                         ` Guennadi Liakhovetski
2004-10-04  7:26                                       ` Ulrich Windl
2004-10-03 21:35                                     ` George Anzinger
2004-10-04  3:00                                       ` Jim Nelson
2004-10-01  9:04                           ` Jesper Juhl
2004-09-29 19:32                   ` Posix compliant CLOCK_PROCESS/THREAD_CPUTIME_ID V5 Christoph Lameter
2004-10-01 19:57                   ` Posix compliant cpu clocks V6 [0/3]: Rationale and test program Christoph Lameter
     [not found]                   ` <B6E8046E1E28D34EB815A11AC8CA31290322B307@mtv-atc-605e--n.corp.sgi.com>
2004-10-01 19:59                     ` Posix compliant cpu clocks V6 [1/3]: Generic Kernel patch Christoph Lameter
2004-10-01 21:03                       ` Andrew Morton
2004-10-01 20:01                     ` Posix compliant cpu clocks V6 [2/3]: Glibc patch Christoph Lameter
2004-10-02  5:32                       ` Ulrich Drepper
2004-10-04 15:04                         ` Christoph Lameter
2004-10-04 16:27                         ` Christoph Lameter
2004-10-06 13:53                         ` Martijn Sipkema
2004-10-01 20:02                     ` Posix compliant cpu clocks V6 [3/3]: mmtimer provides CLOCK_SGI_CYCLE Christoph Lameter
2004-10-07  4:56                     ` Posix compliant cpu clocks V7 [0/2]: Rationale and test program Christoph Lameter
2004-10-12 20:19                       ` Periodic posix timer support broke between 2.6.9-rc1 and 2.6.9-rc1-bk17 Christoph Lameter
2004-10-12 22:24                         ` George Anzinger
2004-10-13 18:08                         ` Alexander Nyberg
2004-10-13 18:11                           ` Christoph Lameter
     [not found]                     ` <B6E8046E1E28D34EB815A11AC8CA31290322B331@mtv-atc-605e--n.corp.sgi.com>
2004-10-07  4:57                       ` Posix compliant cpu clocks V7 [1/2]: Kernel Patch Christoph Lameter
2004-10-07  4:59                       ` Posix compliant cpu clocks V7 [2/2]: Glibc patch Christoph Lameter
2004-10-21 19:32               ` Posix compliant process clock patch for the linux arch in glibc Christoph Lameter
2004-10-01 21:57             ` [RFC] Posix compliant behavior of CLOCK_PROCESS/THREAD_CPUTIME_ID Roland McGrath
2004-10-01 23:30               ` Christoph Lameter
2004-10-04 18:48               ` RFC: Posix compliant clock_getclockcpuid(pid) to access other processes clocks Christoph Lameter

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=Pine.LNX.4.58.0409282017340.18604@schroedinger.engr.sgi.com \
    --to=clameter@sgi.com \
    --cc=Ulrich.Windl@rz.uni-regensburg.de \
    --cc=drepper@redhat.com \
    --cc=george@mvista.com \
    --cc=jbarnes@sgi.com \
    --cc=johnstul@us.ibm.com \
    --cc=libc-alpha@sources.redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®