The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <compudj@krystal.dyndns.org>
To: Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
Cc: Jens Axboe <jens.axboe@oracle.com>,
	Pekka Enberg <penberg@cs.helsinki.fi>,
	Tom Zanussi <tzanussi@gmail.com>,
	akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
	righi.andrea@gmail.com
Subject: Re: [PATCH 2/3] relay: Fix race condition which occurs when reading across CPUs.
Date: Tue, 17 Jun 2008 09:10:36 -0400	[thread overview]
Message-ID: <20080617131036.GB8696@Krystal> (raw)
In-Reply-To: <20080617154923.36195161@linux360.ro>

* Eduard - Gabriel Munteanu (eduard.munteanu@linux360.ro) wrote:
> On Tue, 17 Jun 2008 15:39:34 +0300
> Eduard - Gabriel Munteanu <eduard.munteanu@linux360.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

  reply	other threads:[~2008-06-17 13:10 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-13  1:09 [PATCH 2/3] relay: Fix race condition which occurs when reading across CPUs Eduard - Gabriel Munteanu
2008-06-14  4:26 ` Tom Zanussi
2008-06-14 15:11   ` Eduard - Gabriel Munteanu
2008-06-14 16:16     ` Pekka Enberg
2008-06-16  5:38       ` Tom Zanussi
2008-06-16  6:19         ` Pekka Enberg
2008-06-17  4:52           ` Tom Zanussi
2008-06-16 12:22     ` Mathieu Desnoyers
2008-06-16 13:22       ` Eduard - Gabriel Munteanu
2008-06-16 16:46         ` Jens Axboe
2008-06-16 18:18           ` Pekka Enberg
2008-06-16 18:23             ` Mathieu Desnoyers
2008-06-16 18:28             ` Jens Axboe
2008-06-17 12:39               ` Eduard - Gabriel Munteanu
2008-06-17 12:49                 ` Eduard - Gabriel Munteanu
2008-06-17 13:10                   ` Mathieu Desnoyers [this message]
2008-06-17 13:35                     ` Eduard - Gabriel Munteanu
2008-06-17 13:50                       ` Mathieu Desnoyers
2008-06-17 14:55                         ` Eduard - Gabriel Munteanu
2008-06-17 12:55                 ` Mathieu Desnoyers
2008-06-17 13:21                   ` Eduard - Gabriel Munteanu
  -- strict thread matches above, loose matches on Subject: below --
2008-06-12 20:26 Eduard - Gabriel Munteanu
2008-06-12 22:58 ` Andrea Righi
2008-06-12 23:15   ` Eduard - Gabriel Munteanu

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=20080617131036.GB8696@Krystal \
    --to=compudj@krystal.dyndns.org \
    --cc=akpm@linux-foundation.org \
    --cc=eduard.munteanu@linux360.ro \
    --cc=jens.axboe@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@cs.helsinki.fi \
    --cc=righi.andrea@gmail.com \
    --cc=tzanussi@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