-
Notifications
You must be signed in to change notification settings - Fork 0
/
affinity.c
179 lines (161 loc) · 4.39 KB
/
affinity.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#if OSNUM == 1 // OSNUM_LINUX defined in globdef.h
// Do not define USE_AFFINITY unless you are a real experimenter!
// The advantages are small but there are many possible problems.
// Maybe things can be done to make control of processor affinity
// better. If you finde ways of making good use of it, please
// send a mail to leif(at)sm5bsz.com
//
// #define USE_AFFINITY
#endif
#ifdef USE_AFFINITY
#define _GNU_SOURCE
#include <sched.h>
#endif
#include "globdef.h"
#include <ctype.h>
#include <string.h>
#include "uidef.h"
#include "thrdef.h"
#define MAX_HEAVY_THREADS 4
#ifdef USE_AFFINITY
int heavy_thread_id[MAX_HEAVY_THREADS]= {THREAD_WIDEBAND_DSP,
THREAD_SECOND_FFT,
THREAD_TIMF2,
THREAD_NARROWBAND_DSP
};
void show_aff(int num,cpu_set_t *set)
{
int i, m;
fprintf( stderr,"\n%02d: ",num);
for(i=0; i<no_of_processors; i++) {
m=CPU_ISSET(i, set);
fprintf( stderr,"[%d]",m);
}
}
void set_affinity_masks(void)
{
int i, j, k, m;
int cores;
cpu_set_t light_set, heavy_set;
int no_of_blocked, block;
int blocking_pids[32];
int blocked_cpu[32];
int blocked_cpu_ind[32];
//return;
cores=no_of_processors;
if(hyperthread_flag)cores/=2;
if(cpu_rotate >= cores)cpu_rotate-=cores;
for(i=0; i<cores; i++) {
blocked_cpu[i]=-1;
blocked_cpu_ind[i]=0;
}
k=ui.max_blocked_cpus;
no_of_blocked=0;
if(k >MAX_HEAVY_THREADS)k=MAX_HEAVY_THREADS;
for(j=0; j<MAX_HEAVY_THREADS; j++) {
for(i=0; i<THREAD_MAX; i++) {
if(heavy_thread_id[j] == i) {
if(thread_command_flag[i] > THRFLAG_NOT_ACTIVE &&
thread_command_flag[i] < THRFLAG_RETURNED) {
blocking_pids[no_of_blocked]=thread_pid[i];
no_of_blocked++;
if(no_of_blocked >= k)goto nomore;
goto block_found;
}
}
}
block_found:
;
}
nomore:
;
m=cores/2;
k=0;
for(i=0; i<no_of_blocked; i++) {
blocked_cpu[i]=k;
blocked_cpu_ind[k]=1;
k+=m;
if(k>=cores) {
cpumap:
;
k=0;
m/=2;
if(m==0) {
no_of_blocked=i+1;
goto cpumap_x;
}
}
while(blocked_cpu_ind[k] == 1 && k<cores)k+=m;
if(k>=cores)goto cpumap;
}
cpumap_x:
;
if(hyperthread_flag) {
for(i=0; i<no_of_blocked; i++)blocked_cpu[i+no_of_blocked]=blocked_cpu[i]+cores;
}
i=0;
//cpu_rotate=0;
CPU_ZERO(&light_set);
for(i=0; i<no_of_processors; i++) {
j=0;
while(blocked_cpu[j] != -1) {
if(blocked_cpu[j]==i)goto skip;
j++;
}
k=i+cpu_rotate;
if(k>=no_of_processors)k-=no_of_processors;
CPU_SET(k,&light_set);
skip:
;
}
m=0;
for(i=0; i<THREAD_MAX; i++) {
for(j=0; j<no_of_blocked; j++) {
if(blocking_pids[j] == thread_pid[i]) {
CPU_ZERO(&heavy_set);
block=cpu_rotate+blocked_cpu[m];
if(block>=cores)block-=cores;
CPU_SET(block,&heavy_set);
if(hyperthread_flag) {
block+=cores;
CPU_SET(block,&heavy_set);
}
m++;
sched_setaffinity(thread_pid[i], sizeof(cpu_set_t), &heavy_set);
show_aff(i,&heavy_set);
goto set;
}
}
if(thread_command_flag[i] > THRFLAG_NOT_ACTIVE &&
thread_command_flag[i] < THRFLAG_RETURNED) {
sched_setaffinity(thread_pid[i], sizeof(cpu_set_t), &light_set);
show_aff(i,&light_set);
}
set:
;
}
fprintf( stderr,"\n");
}
#endif
void setup_thread_affinities(void)
{
#ifdef USE_AFFINITY
cpu_rotate=0;
thread_rotate_count=0;
if(ui.max_blocked_cpus == 0)return;
set_affinity_masks();
#endif
}
void fix_thread_affinities(void)
{
#ifdef USE_AFFINITY
if(ui.max_blocked_cpus == 0)return;
// Step through the running threads and set processor affinity.
thread_rotate_count++;
if(thread_rotate_count > 4) {
thread_rotate_count=0;
cpu_rotate++;
set_affinity_masks();
}
#endif
}