[LTP] [PATCH] open_posix_testsuite: add set_affinity_single()

Cyril Hrubis chrubis@suse.cz
Thu Nov 12 14:09:52 CET 2015


Hi!
> +static int get_present_cpu_from_sysfs(void)
> +{
> +	FILE *f;
> +	int cpu = -1;
> +
> +	f = fopen("/sys/devices/system/cpu/present", "r");
> +	if (!f)
> +		return -1;
> +	fscanf(f, "%d", &cpu);
> +	fclose(f);
> +
> +	return cpu;
> +}
> +
> +static int get_present_cpu_from_cpuinfo(void)
> +{
> +	FILE *f;
> +	int cpu = -1;
> +	char line[4096];
> +
> +	f = fopen("/proc/cpuinfo", "r");
> +	if (!f)
> +		return -1;
> +
> +	while (!feof(f)) {
> +		if (!fgets(line, sizeof(line), f))
> +			return -1;
> +		/*
> +		 * cpuinfo output is not consistent across all archictures,
> +		 * it can be "processor        : N", but for example on s390
> +		 * it's: "processor N: ...", so ignore any non-number
> +		 * after "processor"
> +		 */
> +		if (sscanf(line, "processor%*[^0123456789]%d", &cpu) == 1)
> +			break;
> +	}
> +	fclose(f);
> +
> +	return cpu;
> +}
> +
> +static int set_affinity_single(void)
> +{
> +	int cpu;
> +
> +	cpu = get_present_cpu_from_sysfs();
> +	if (cpu < 0)
> +		cpu = get_present_cpu_from_cpuinfo();

We should probably fallback here to cpu = 0 in case that we have failed
to parse the cpuinfo. Not that it's likely to happen.

So I would do here:

	cpu = get_present_cpu_from_sysfs();

	if (cpu >= 0)
		goto set_affinity;

	cpu = get_present_cpu_from_cpuinfo();

	if (cpu >= 0)
		goto set_affinity;

	fprintf(stderr, "WARNING: Failed to detect present cpu using cpu=0\n");

	cpu = 0;

set_affinity:
	return set_affinity(cpu);

> +	return set_affinity(cpu);
> +}
> +
>  #else
>  static int set_affinity(int cpu)
                            ^
			    You should also rename this stub for
			    non-linux OSes.

Otherwise it looks good, acked.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the Ltp mailing list