-
Notifications
You must be signed in to change notification settings - Fork 1
/
linux_os_util.c
162 lines (134 loc) · 3.47 KB
/
linux_os_util.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2023 by Intel Corporation
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "cpuid_topology.h"
/*
* Constants, values for local use
*/
#define BYTES_IN_KB (1024)
#define BYTES_IN_MB (1048576)
/*
* Os_DisplayTopology
*
* Display the topology using OS related information
*
* Arguments:
* None
*
* Return:
* None
*/
void Os_DisplayTopology(void)
{
/*
* One example, applications may parse the output.
*/
printf("*********************************\n");
printf("**** Linux OS /proc/cpuinfo ****\n\n");
system("cat /proc/cpuinfo\n");
printf("\n\n");
}
/*
* Os_Platform_Read_Cpuid
*
* OS/Compiler Specific implementation of reading CPUID.
*
* Arguments:
* Leaf, Subleaf, CPUID Data Structure
*
* Return:
* None
*/
void Os_Platform_Read_Cpuid(unsigned int Leaf, unsigned int Subleaf, PCPUID_REGISTERS pCpuidRegisters)
{
unsigned int ReturnEax;
unsigned int ReturnEbx;
unsigned int ReturnEcx;
unsigned int ReturnEdx;
asm ("movl %4, %%eax\n"
"movl %5, %%ecx\n"
"CPUID\n"
"movl %%eax, %0\n"
"movl %%ebx, %1\n"
"movl %%ecx, %2\n"
"movl %%edx, %3\n"
: "=r" (ReturnEax), "=r" (ReturnEbx), "=r" (ReturnEcx), "=r" (ReturnEdx)
: "r" (Leaf), "r" (Subleaf)
: "%eax", "%ebx", "%ecx", "%edx"
);
pCpuidRegisters->x.Register.Eax = ReturnEax;
pCpuidRegisters->x.Register.Ebx = ReturnEbx;
pCpuidRegisters->x.Register.Ecx = ReturnEcx;
pCpuidRegisters->x.Register.Edx = ReturnEdx;
}
/*
* Os_GetNumberOfProcessors
*
* Get the number of processors
*
* Arguments:
* None
*
* Return:
* Number of processors
*/
unsigned int Os_GetNumberOfProcessors(void)
{
unsigned int NumberOfProcessors;
NumberOfProcessors = (unsigned int)get_nprocs();
return NumberOfProcessors;
}
/*
* Os_SetAffinity
*
* Set the current thread affinity
*
* Arguments:
* Processor Number
*
* Return:
* None
*/
void Os_SetAffinity(unsigned int ProcessorNumber)
{
cpu_set_t *cpu_set;
unsigned int NumberOfProcessors;
unsigned int SetSize;
/*
* Get the size of the maximum number of configured processors.
*/
NumberOfProcessors = get_nprocs_conf();
/*
* Something larger comes in we will just go with it.
*/
if (ProcessorNumber > NumberOfProcessors)
{
NumberOfProcessors = ProcessorNumber;
}
cpu_set = CPU_ALLOC(NumberOfProcessors);
if (cpu_set)
{
SetSize = CPU_ALLOC_SIZE(NumberOfProcessors);
CPU_ZERO_S(SetSize, cpu_set);
CPU_SET_S(ProcessorNumber, SetSize, cpu_set);
sched_setaffinity(getpid(),SetSize, cpu_set);
CPU_FREE(cpu_set);
}
}