-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin-sched.c
78 lines (66 loc) · 2.09 KB
/
builtin-sched.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
/*
Copyright (C) 2013
Baptiste Lepers <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2, 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.
*/
#include "parse.h"
#include "builtin-sched.h"
/*
* Sets the affinity of threads.
* Places threads close to their memory.
*/
static int nb_sched_apps = 0;
static char **sched_apps = NULL;
void sched_add_application(char *app) {
sched_apps = realloc(sched_apps, (nb_sched_apps+1)*sizeof(*sched_apps));
sched_apps[nb_sched_apps] = strdup(app);
nb_sched_apps++;
}
static rbtree sched_tree;
void sched_init() {
sched_tree = rbtree_create();
}
void sched_parse(struct s* s) {
if(!s->ibs_dc_phys)
return;
int app_num = -1, i;
for(i = 0; i < nb_sched_apps; i++) {
if(!strcmp(get_app(s), sched_apps[i])) {
app_num = i;
break;
}
}
if(app_num == -1)
return;
int *v = rbtree_lookup(sched_tree, (void*)(long)get_tid(s), pointer_cmp);
if(!v) {
v = calloc(max_node, sizeof(*v));
rbtree_insert(sched_tree, (void*)(long)get_tid(s), v, pointer_cmp);
}
v[phys_to_node(s->ibs_dc_phys)]++;
}
static int sched_do(void *key, void *value) {
int max = 0, max_index = 0, i, *v = value;
for(i = 0; i < max_node; i++) {
if(v[i] > max) {
max = v[i];
max_index = i;
}
}
if(sched_setaffinity((int)(long)key, sizeof(cpu_set_t), die_cpu_set(max_index)) < 0) {
printf("Error when setting affinity of pid %d\n", (int)(long)key);
}
return 0;
}
void sched_show() {
rbtree_print(sched_tree, sched_do);
}