Re: [PATCH 2/3] relay: Fix race condition which occurs when readingacross CPUs.

From: Eduard - Gabriel Munteanu
Date: Tue Jun 17 2008 - 08:50:49 EST


On Tue, 17 Jun 2008 15:39:34 +0300
Eduard - Gabriel Munteanu <eduard.munteanu@xxxxxxxxxxx> 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.

#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;
}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/