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