Re: Inconsistent timing results of multithreaded program on an SMPmachine.

From: Paul Jackson
Date: Sun Nov 20 2005 - 09:15:17 EST

Marcel wrote:
> Instead what I've got was an oscillation where the maximum time was twice
> and more than the minimum!! For a short test results ranged ~7sec to ~16 ...

Just for grins, try displaying which cpu each thread runs on. Display the
return from "latestcpu(getpid())" in the two per-thread printf's, to display
the thread's cpu at the beginning and end of each compute_thread(). Perhaps
you will notice that the per-thread cpu correlates with the test times.

* int latestcpu(pid_t pid)
* Copyright (C) 2005 Silicon Graphics, Inc.
* This code is subject to the terms and conditions of the
* GNU General Public License.
* Return most recent CPU on which task pid executed.
* The last used CPU is visible for a given pid as field #39
* (starting with #1) in the file /proc/<pid>/stat. Currently
* this file has 41 fields, in which case this is the 3rd to
* the last field.

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <limits.h>
#include <linux/limits.h>

int latestcpu(pid_t pid)
char buf[PATH_MAX];
int fd;
int cpu = -1;

snprintf(buf, sizeof(buf), "/proc/%d/stat", pid);
fd = open(buf, O_RDONLY);
buf[0] = 0; /* in case fd < 0 and read() is no-op */
read(fd, buf, sizeof(buf));
sscanf(buf, "%*u %*s %*s %*u %*u %*u %*u %*u %*u %*u "
"%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u "
"%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u "
"%*u %*u %*u %*u %*u %*u %*u %*u %u", /* 39th field */
return cpu;

I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <pj@xxxxxxx> 1.925.600.0401
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at