[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20080617131036.GB8696@Krystal>
Date: Tue, 17 Jun 2008 09:10:36 -0400
From: Mathieu Desnoyers <compudj@...stal.dyndns.org>
To: Eduard - Gabriel Munteanu <eduard.munteanu@...ux360.ro>
Cc: Jens Axboe <jens.axboe@...cle.com>,
Pekka Enberg <penberg@...helsinki.fi>,
Tom Zanussi <tzanussi@...il.com>, akpm@...ux-foundation.org,
linux-kernel@...r.kernel.org, righi.andrea@...il.com
Subject: Re: [PATCH 2/3] relay: Fix race condition which occurs when
reading across CPUs.
* Eduard - Gabriel Munteanu (eduard.munteanu@...ux360.ro) wrote:
> On Tue, 17 Jun 2008 15:39:34 +0300
> Eduard - Gabriel Munteanu <eduard.munteanu@...ux360.ro> wrote:
>
> > kmemtrace will use the affine versions and set CPU affinity anyway,
> > but it would be nice to have a consistent behavior from relay's part.
> >
> >
> > Cheers,
> > Eduard
>
> BTW, I wrote this stuff and tested affinity behavior. It seems like
> migration always occurs fast enough, even if under pressure. My machine
> has two CPUs, so there are a few hardcoded values.
>
Do you get the same results when not using a buffered read to read
/proc/PID/stat ? Also, since the internal proc handle might buffer the
information, closing the file and re-opening it after the set affinity
would not be a bad idea.
Mathieu
> #define _GNU_SOURCE
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <unistd.h>
> #include <string.h>
> #include <sched.h>
>
> unsigned long get_current_cpu(FILE *stat_fp)
> {
> char buf[256], *p;
> unsigned long n_delims = 0, ret;
> size_t count;
>
> fseek(stat_fp, 0, SEEK_SET);
> count = fread(buf, 1, 255, stat_fp);
> buf[count] = 0;
>
> p = buf;
> while (n_delims < 38)
> if (*p++ == ' ')
> n_delims++;
> sscanf(p, "%lu", &ret);
>
> return ret;
> }
>
> int main(void)
> {
> FILE *stat_fp;
> char stat_filename[255];
> int err;
> unsigned long old_cpu, new_cpu, cpu;
> pid_t pid;
> cpu_set_t cpuset;
>
> pid = getpid();
> sprintf(stat_filename, "/proc/%lu/stat", pid);
> stat_fp = fopen(stat_filename, "r");
> if (!stat_fp) {
> printf("Couldn't open %lu", pid);
> return 1;
> }
>
> old_cpu = get_current_cpu(stat_fp);
> new_cpu = (old_cpu + 1) % 2;
>
> printf("Started on CPU %lu, PID %lu, setting affinity to CPU %lu...\n",
> old_cpu, pid, new_cpu);
>
> err = sched_getaffinity(pid, sizeof(cpu_set_t), &cpuset);
> if (err == -1){
> printf("Could not retrieve the affinity!\n");
> return 1;
> }
> printf("Previous affinity: %d %d\n",
> CPU_ISSET(0, &cpuset), CPU_ISSET(1, &cpuset));
> CPU_ZERO(&cpuset);
> CPU_SET(new_cpu, &cpuset);
> err = sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset);
> if (err == -1) {
> printf("Could not set affinity!\n");
> return 1;
> }
> err = sched_getaffinity(pid, sizeof(cpu_set_t), &cpuset);
> if (err == -1){
> printf("Could not retrieve the affinity!\n");
> return 1;
> }
> printf("Current affinity: %d %d\n",
> CPU_ISSET(0, &cpuset), CPU_ISSET(1, &cpuset));
>
> cpu = get_current_cpu(stat_fp);
> if (cpu != new_cpu) {
> printf("Process migration did not occur immediately!\n"
> "--- we were supposed to be on %lu, but we're on %lu\n",
> new_cpu, cpu);
> /* return 1; */
> }
>
> for (;;) {
> cpu = get_current_cpu(stat_fp);
> if (cpu != new_cpu) {
> printf("Oh, we arrived on a different CPU!\n");
> /* return 1; */
> }
> }
>
> return 0;
> }
>
--
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists