-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinthread.c
135 lines (116 loc) · 4.13 KB
/
pinthread.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
/*
* Copyright (c) 2015 Franco (nextime) Lanza <[email protected]>
*
* pinthread [https://git.devuan.org/packages-base/pinthread]
*
* pinthread is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*
* compile with :
* $ gcc -Wall -D_GNU_SOURCE -fpic -shared -o pinthread.so pinthread.c -ldl -lpthread
*
* and test it with :
* LD_PRELOAD=/path/to/pinthread.so something
*
* Environment variables:
* - PINTHREAD_CORE: set to the core number you want to pin to.
* if omitted, it defaults to the result of
* sched_getcpu()
*
* - PINTHREAD_PNAMES: accept a space separated list of strings,
* if any string match the process name or
* PINTHREAD_PNAMES is empty/omitted, it will
* pin all thread according to PINTHREAD_CORE,
* otherwise it will just call the "real"
* pthread_create without any pinning.
*
*/
//#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <dlfcn.h>
#include <sched.h> //cpu_set_t , CPU_SET
#include <libgen.h>
#include <stdbool.h>
#include <string.h>
static char *procname;
static bool pinthread_override = false;
static unsigned int ncore;
static unsigned int setcore;
static int (*real_pthread_create)(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
static void main_pinthread(int argc, char* argv[], char* envp[])
{
char *pch;
char *msg;
cpu_set_t mask; /* Define your cpu_set bit mask. */
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
procname = basename(argv[0]);
msg = getenv("PINTHREAD_PNAMES");
if(msg == NULL)
{
pinthread_override = true;
} else {
pch = strtok (msg," ");
while (pch != NULL)
{
if (!strcmp(procname,pch))
{
pinthread_override = true;
pch = NULL;
} else {
pch = strtok (NULL," ");
}
}
}
ncore = sysconf (_SC_NPROCESSORS_CONF);
msg = getenv("PINTHREAD_CORE");
if (msg != NULL)
{
setcore = (unsigned int) strtoul(msg, (char **)NULL, 10);
if(setcore >= ncore)
{
fprintf(stderr, "E:PINTHREAD wrong value for PINTHREAD_CORE: %u - using default.\n", setcore);
setcore = sched_getcpu();
}
} else {
setcore = sched_getcpu();
}
CPU_SET(setcore, &mask);
// make sure the main thread is running on the same core:
sched_setaffinity(getpid(), sizeof(mask), &mask);
real_pthread_create = dlsym(RTLD_NEXT,"pthread_create");
if ((msg=dlerror())!=NULL)
fprintf(stderr, "E:PINTHREAD pthread_create dlsym failed : %s\n", msg);
//printf("*wrapping done\n");
}
__attribute__((section(".init_array"))) void (* p_main_pinthread)(int,char*[],char*[]) = &main_pinthread;
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{
int ret;
cpu_set_t mask; /* Define your cpu_set bit mask. */
CPU_ZERO(&mask); /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(setcore, &mask);
//printf("*about to call original pthread_create\n");
ret = real_pthread_create(thread,attr,start_routine,arg);
if(pinthread_override)
pthread_setaffinity_np(*thread, sizeof(mask), &mask);
return ret;
}