mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
To: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: "Ingo Molnar" <mingo@elte.hu>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Spencer Candland" <spencer@bluehost.com>,
	"Américo Wang" <xiyou.wangcong@gmail.com>,
	linux-kernel@vger.kernel.org, "Oleg Nesterov" <oleg@redhat.com>,
	"Balbir Singh" <balbir@in.ibm.com>
Subject: reproducer: utime decreasing
Date: Wed, 02 Dec 2009 17:29:05 +0900	[thread overview]
Message-ID: <4B162551.7050303@jp.fujitsu.com> (raw)
In-Reply-To: <20091123101612.GC25978@dhcp-lab-161.englab.brq.redhat.com>

Following is a sample program that can reproduce the problem
in several ways.

I tested my patch using this reproducer, and confirmed that
applying both of Stanislaw's do_sys_times() patch and my
thread_group_times() patch is required to fix the problem.


Thanks,
H.Seto

===
/*
 * Sample program to demonstrate time decreasing on thread exit
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/times.h>

#define DEFAULT_THREADS 500

unsigned long lpt;
int looptype;
int samplesleep;
int threads;

void *child (void *thread_id)
{
	struct tms t[12];
  	int i, j;
	unsigned long ret = 0, id = (unsigned long)thread_id;

	if (looptype) {
		/*
		 * discont:
		 *
		 *  Loop tricky, to make a significant gap between
		 *  task->{u,s}time and task_times().
		 *
		 *	runtime of a thread  = 0.5 tick * 1000 = 500 ms
		 *	task->{u,s}time	     = (??? , 0)
		 *	task_times()         = ideally (500, 0)
		 */
		for (j = 0; j < 1000; j++) {
			for (i = 0; i < lpt/2; i++)
				;
			usleep(0);
		}
	} else {
		/*
		 * cont:
		 *
		 *  Loop without tricks.
		 *
		 *	runtime of a thread  = 500 ms
		 *	task->{u,s}time      = (500, 0)
		 *	task_times()         = (500, 0)
		 */
		for (i = 0; i < lpt * 500; i++)
			;
	}

	if (!(id % 4))
		/* bother other threads */
		pthread_exit((void *)ret);

	for (i = 0; i < 12; i++) {
		times(&t[i]);
		if (samplesleep)
			usleep(0);
	}

	for (i = 0; i + 5 < 12; i++) {
		/*
		 *  +----+----+----+----+----+
		 * i+0  i+1  i+2  i+3  i+4  i+5
		 *            ^^^^^^
		 *          check here
		 */
		if (t[i+2].tms_utime > t[i+3].tms_utime
		    || t[i+2].tms_stime > t[i+3].tms_stime) {

		   printf("[%4ld] %s decreased %3d: "
			  "(%d %d) (%d %d) [%d %d]<->[%d %d] (%d %d) (%d %d)\n",
				id,
				t[i+2].tms_utime > t[i+3].tms_utime ?
							"utime" : "stime",
				t[i+2].tms_utime > t[i+3].tms_utime ?
				  t[i+3].tms_utime - t[i+2].tms_utime :
				  t[i+3].tms_stime - t[i+2].tms_stime,
				t[i+0].tms_utime, t[i+0].tms_stime,
				t[i+1].tms_utime, t[i+1].tms_stime,
				t[i+2].tms_utime, t[i+2].tms_stime,
				t[i+3].tms_utime, t[i+3].tms_stime,
				t[i+4].tms_utime, t[i+4].tms_stime,
				t[i+5].tms_utime, t[i+5].tms_stime);
		   ret = 1;
		}
	}

	pthread_exit((void *)ret);
}

void get_loops_per_tick(void)
{
	struct tms t1, t2;
	unsigned long i, mloop = 1000 * 1000 * 1000;

	times(&t1);
	for (i = 0; i < mloop; i++)
		;
	times(&t2);

	lpt = mloop / ((t2.tms_utime - t1.tms_utime) * 10);
}

void do_test(int c)
{
	struct tms t1, t2;
	clock_t j1, j2;
	pthread_t *th;
	unsigned long i, ret = 0;

	th = calloc(threads, sizeof(pthread_t));
	if (!th)
		return;

	looptype    = !!(c & 0x1) ? 1 : 0;
	samplesleep = !!(c & 0x2) ? 1 : 0;
	printf("looptype    : %s\n", looptype ? "discont" : "cont");
	printf("samplesleep : %s\n", samplesleep ? "yes" : "no");

	printf(" ## start ##\n");
	j1 = times(&t1);
	for (i = 0; i < threads; i++)
		pthread_create (&th[i], NULL, child, (void *)i);
	for (i = 0; i < threads; i++) {
		int r;
		pthread_join(th[i], (void *)&r);
		ret += (int)r;
	}
	j2 = times(&t2);
	printf(" ## done. ##\n");
	printf(" loop total:\n");
	printf("  user   : %7d ms\n", (t2.tms_utime - t1.tms_utime) * 10);
	printf("  system : %7d ms\n", (t2.tms_stime - t1.tms_stime) * 10);
	printf("  elapse : %7d ms\n", (j2 - j1) * 10);
	printf("  error  : %d\n\n", ret);

	printf("result: %s\n\n", ret ? "BAD" : "GOOD");
}

int main(int argc, char **argv)
{
	int i;

	threads = argc > 1 ? atoi(argv[1]) : DEFAULT_THREADS;

	printf("### Prep:\n");
	get_loops_per_tick();
	printf("loops_per_tick: %ld\n", lpt);
	printf("threads       : %d\n\n", threads);

	printf("### Test:\n");
	for (i = 0; i < 4; i++)
		do_test(i);
}


  parent reply	other threads:[~2009-12-02  8:29 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-04  0:23 utime/stime decreasing on thread exit Spencer Candland
2009-11-04  6:49 ` Hidetoshi Seto
2009-11-05  5:24   ` Hidetoshi Seto
2009-11-09 14:49     ` Peter Zijlstra
2009-11-09 17:20       ` Oleg Nesterov
2009-11-09 17:27         ` Oleg Nesterov
2009-11-09 17:31         ` Peter Zijlstra
2009-11-09 19:23           ` Oleg Nesterov
2009-11-09 19:32             ` Peter Zijlstra
2009-11-10 10:44             ` Stanislaw Gruszka
2009-11-10 17:40               ` Oleg Nesterov
2009-11-10 18:24                 ` Stanislaw Gruszka
2009-11-10 19:23                   ` Oleg Nesterov
2009-11-17 12:48                     ` Stanislaw Gruszka
2009-11-17 12:57                       ` [PATCH] posix-cpu-timers: reset expire cache when no timer is running Stanislaw Gruszka
2009-11-10  5:42       ` utime/stime decreasing on thread exit Hidetoshi Seto
2009-11-10  5:47         ` [PATCH] fix granularity of task_u/stime() Hidetoshi Seto
2009-11-11 12:11           ` Stanislaw Gruszka
2009-11-12  0:00             ` Hidetoshi Seto
2009-11-12  2:49               ` Hidetoshi Seto
2009-11-12  2:55                 ` Américo Wang
2009-11-12  4:16                   ` Hidetoshi Seto
2009-11-12  4:33                     ` [PATCH] fix granularity of task_u/stime(), v2 Hidetoshi Seto
2009-11-12 14:15                       ` Peter Zijlstra
2009-11-12 14:49                       ` Stanislaw Gruszka
2009-11-12 15:00                         ` Peter Zijlstra
2009-11-12 15:40                           ` Stanislaw Gruszka
2009-11-13 12:42                             ` [PATCH] sys_times: fix utime/stime decreasing on thread exit Stanislaw Gruszka
2009-11-13 13:16                               ` Peter Zijlstra
2009-11-13 14:12                                 ` Balbir Singh
2009-11-13 15:36                                 ` Stanislaw Gruszka
2009-11-13 17:05                                   ` Peter Zijlstra
2009-11-16 19:32                             ` [PATCH] fix granularity of task_u/stime(), v2 Spencer Candland
2009-11-17 13:08                               ` Stanislaw Gruszka
2009-11-17 13:24                                 ` Peter Zijlstra
2009-11-19 18:17                                   ` Stanislaw Gruszka
2009-11-20  2:00                                     ` Hidetoshi Seto
2009-11-23 10:09                                       ` Stanislaw Gruszka
2009-11-23 10:16                                         ` [PATCH] cputime: avoid do_sys_times() races with __exit_signal() Stanislaw Gruszka
2009-11-30  9:20                                           ` [PATCH 1/2] cputime: remove prev_{u,s}time if VIRT_CPU_ACCOUNTING Hidetoshi Seto
2009-11-30  9:21                                           ` [PATCH 2/2] cputime: introduce thread_group_times() Hidetoshi Seto
2009-11-30 14:54                                             ` Stanislaw Gruszka
2009-12-01  1:02                                               ` Hidetoshi Seto
2009-12-02  8:26                                           ` [PATCH -v2 1/2] sched, cputime: cleanups related to task_times() Hidetoshi Seto
2009-12-02 15:17                                             ` Peter Zijlstra
2009-12-02 15:29                                               ` Balbir Singh
2009-12-03  0:21                                                 ` Hidetoshi Seto
2009-12-02 15:57                                             ` Peter Zijlstra
2009-12-02 17:33                                             ` [tip:sched/core] sched, cputime: Cleanups " tip-bot for Hidetoshi Seto
2009-12-02  8:28                                           ` [PATCH -v2 2/2] sched, cputime: introduce thread_group_times() Hidetoshi Seto
2009-12-02 15:58                                             ` Peter Zijlstra
2009-12-02 17:33                                             ` [tip:sched/core] sched, cputime: Introduce thread_group_times() tip-bot for Hidetoshi Seto
2009-12-02  8:29                                           ` Hidetoshi Seto [this message]
2009-12-02  8:32                                           ` reproducer: invisible utime Hidetoshi Seto
2009-11-23 10:25                                         ` [PATCH] fix granularity of task_u/stime(), v2 Balbir Singh
2009-11-23 10:46                                           ` Stanislaw Gruszka
2009-11-24  5:33                                         ` Hidetoshi Seto
2009-11-18 22:38                                 ` Spencer Candland
2009-11-23  9:52                         ` Stanislaw Gruszka
2009-11-12 18:12                       ` [tip:sched/core] sched: Fix granularity of task_u/stime() tip-bot for Hidetoshi Seto
2009-11-13  9:40                         ` Stanislaw Gruszka
2009-11-13 23:09                         ` Ingo Molnar
2009-11-16  2:44                           ` Hidetoshi Seto

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=4B162551.7050303@jp.fujitsu.com \
    --to=seto.hidetoshi@jp.fujitsu.com \
    --cc=balbir@in.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sgruszka@redhat.com \
    --cc=spencer@bluehost.com \
    --cc=tglx@linutronix.de \
    --cc=xiyou.wangcong@gmail.com \
    /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