1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) Novell Inc. 2011
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms in version 2 of the GNU General Public License as
7f08c3bdfSopenharmony_ci *   published by the Free Software Foundation.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
10f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
16f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci *   Author:  Peter W. Morreale <pmorreale AT novell DOT com>
19f08c3bdfSopenharmony_ci *   Date:    11/08/2011
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci/*
23f08c3bdfSopenharmony_ci * Must be ported to OS's other than Linux
24f08c3bdfSopenharmony_ci */
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#ifdef __linux__
27f08c3bdfSopenharmony_ci# define _GNU_SOURCE
28f08c3bdfSopenharmony_ci# include <sched.h>
29f08c3bdfSopenharmony_ci# include <stdio.h>
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic int set_affinity(int cpu)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	cpu_set_t mask;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	CPU_ZERO(&mask);
36f08c3bdfSopenharmony_ci	CPU_SET(cpu, &mask);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	return (sched_setaffinity(0, sizeof(cpu_set_t), &mask));
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic int get_online_cpu_from_sysfs(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	FILE *f;
44f08c3bdfSopenharmony_ci	int cpu = -1;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	f = fopen("/sys/devices/system/cpu/online", "r");
47f08c3bdfSopenharmony_ci	if (!f)
48f08c3bdfSopenharmony_ci		return -1;
49f08c3bdfSopenharmony_ci	if (!fscanf(f, "%d", &cpu))
50f08c3bdfSopenharmony_ci		cpu = -1;
51f08c3bdfSopenharmony_ci	fclose(f);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	return cpu;
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic int get_online_cpu_from_cpuinfo(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	FILE *f;
59f08c3bdfSopenharmony_ci	int cpu = -1;
60f08c3bdfSopenharmony_ci	char line[4096];
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	f = fopen("/proc/cpuinfo", "r");
63f08c3bdfSopenharmony_ci	if (!f)
64f08c3bdfSopenharmony_ci		return -1;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	while (!feof(f)) {
67f08c3bdfSopenharmony_ci		if (!fgets(line, sizeof(line), f))
68f08c3bdfSopenharmony_ci			return -1;
69f08c3bdfSopenharmony_ci		/*
70f08c3bdfSopenharmony_ci		 * cpuinfo output is not consistent across all archictures,
71f08c3bdfSopenharmony_ci		 * it can be "processor        : N", but for example on s390
72f08c3bdfSopenharmony_ci		 * it's: "processor N: ...", so ignore any non-number
73f08c3bdfSopenharmony_ci		 * after "processor"
74f08c3bdfSopenharmony_ci		 */
75f08c3bdfSopenharmony_ci		if (sscanf(line, "processor%*[^0123456789]%d", &cpu) == 1)
76f08c3bdfSopenharmony_ci			break;
77f08c3bdfSopenharmony_ci	}
78f08c3bdfSopenharmony_ci	fclose(f);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	return cpu;
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic int set_affinity_single(void)
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	int cpu;
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	cpu = get_online_cpu_from_sysfs();
88f08c3bdfSopenharmony_ci	if (cpu >= 0)
89f08c3bdfSopenharmony_ci		goto set_affinity;
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	cpu = get_online_cpu_from_cpuinfo();
92f08c3bdfSopenharmony_ci	if (cpu >= 0)
93f08c3bdfSopenharmony_ci		goto set_affinity;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	fprintf(stderr, "WARNING: Failed to detect online cpu, using cpu=0\n");
96f08c3bdfSopenharmony_ci	cpu = 0;
97f08c3bdfSopenharmony_ciset_affinity:
98f08c3bdfSopenharmony_ci	return set_affinity(cpu);
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci#else
102f08c3bdfSopenharmony_ci#include <errno.h>
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_cistatic int set_affinity_single(void)
105f08c3bdfSopenharmony_ci{
106f08c3bdfSopenharmony_ci	errno = ENOSYS;
107f08c3bdfSopenharmony_ci	return -1;
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci#endif
110