From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] open_posix_testsuite: add set_affinity_single()
Date: Thu, 12 Nov 2015 14:09:52 +0100 [thread overview]
Message-ID: <20151112130952.GB19653@rei.lan> (raw)
In-Reply-To: <46ff3d4f262abe122b1579681c0e568857c3f9dc.1447332053.git.jstancek@redhat.com>
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
next prev parent reply other threads:[~2015-11-12 13:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-12 12:45 [LTP] [PATCH] open_posix_testsuite: add set_affinity_single() Jan Stancek
2015-11-12 13:05 ` Jan Stancek
2015-11-12 13:09 ` Cyril Hrubis [this message]
2015-11-12 13:41 ` Jan Stancek
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=20151112130952.GB19653@rei.lan \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.