affinity.cpp 3.81 KB
Newer Older
qon's avatar
qon committed
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifdef _WIN32
#include "pthread_mutex_win32_wrapper.h"
#else
#include <sys/types.h>
#include <sys/syscall.h>
#include <syscall.h>
#include <dirent.h>
#include <pthread.h>
#endif
#include <vector>
#include "affinity.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "os_low_level_helper.h"

#ifndef STD_OUT
#define STD_OUT stdout
#endif

pid_t gettid()
{
#ifdef _WIN32
	return((pid_t) GetCurrentThreadId());
#else
	return((pid_t) syscall(SYS_gettid));
#endif
}

#ifdef _WIN32
pid_t getpid()
{
	return((pid_t) GetCurrentProcessId());
}
#endif

struct threadNameStruct
{
	pid_t thread_id;
	std::string name;
};

class lockClass
{
public:
    lockClass() {pthread_mutex_init(&lock, NULL);}
    ~lockClass() {pthread_mutex_destroy(&lock);}
    std::vector<threadNameStruct> threadNames;
    pthread_mutex_t lock;
};

static lockClass lockedVector;

void setThreadName(char* name)
{
	threadNameStruct tmp;
	tmp.thread_id = gettid();
	tmp.name = name;
	pthread_mutex_lock(&lockedVector.lock);
	lockedVector.threadNames.push_back(tmp);
	pthread_mutex_unlock(&lockedVector.lock);
}

void setUnknownNames(char* name)
{
	pid_t pid = getpid();
#ifndef _WIN32
	char dirname[1024];
	sprintf(dirname, "/proc/%d/task", (int) pid);
	DIR* dp = opendir(dirname);
	if (dp)
	{
		dirent* ent;
		while ((ent = readdir(dp)) != NULL)
		{
			pid_t tid = atoi(ent->d_name);
			if (tid != 0 && tid != pid)
			{
				int found = false;
				for (size_t i = 0;i < lockedVector.threadNames.size();i++)
				{
					if (lockedVector.threadNames[i].thread_id == tid)
					{
						found = true;
					}
				}
				if (found == false)
				{
					threadNameStruct tmp;
					tmp.thread_id = tid;
					tmp.name = name;
					lockedVector.threadNames.push_back(tmp);
				}
			}
		}
	}
#endif
}

void setUnknownAffinity(int count, int* cores)
{
	pid_t pid = getpid();
#ifndef _WIN32
	char dirname[1024];
	sprintf(dirname, "/proc/%d/task", (int) pid);
	DIR* dp = opendir(dirname);
	if (dp)
	{
		dirent* ent;
		while ((ent = readdir(dp)) != NULL)
		{
			pid_t tid = atoi(ent->d_name);
			if (tid != 0 && tid != pid)
			{
				int found = false;
				for (size_t i = 0;i < lockedVector.threadNames.size();i++)
				{
					if (lockedVector.threadNames[i].thread_id == tid)
					{
						found = true;
					}
				}
				if (found == false)
				{
					cpu_set_t tmpset;
					CPU_ZERO(&tmpset);
					for (int i = 0;i < count;i++) CPU_SET(cores[i], &tmpset);
					sched_setaffinity(tid, sizeof(tmpset), &tmpset);
				}
			}
		}
	}
#endif
}

void printThreadPinning()
{
	pid_t pid = getpid();
#ifndef _WIN32
	char dirname[1024];
	sprintf(dirname, "/proc/%d/task", (int) pid);
	DIR* dp = opendir(dirname);
	if (dp)
	{
		dirent* ent;
		fprintf(STD_OUT, "%12s", "");
		for (int i = 0;i < get_number_of_cpu_cores();i++)
		{
			fprintf(STD_OUT, " %2d", i);
		}
		fprintf(STD_OUT, "\n");
		
		while ((ent = readdir(dp)) != NULL)
		{
			pid_t tid = atoi(ent->d_name);
			if (tid != 0)
			{
				fprintf(STD_OUT, "Thread %5d", tid);
				cpu_set_t threadmask;
				sched_getaffinity(tid, sizeof(threadmask), &threadmask);
				for (int i = 0;i < get_number_of_cpu_cores();i++)
				{
					if (CPU_ISSET(i, &threadmask))
					{
						fprintf(STD_OUT, "  X");
					}
					else
					{
						fprintf(STD_OUT, "  .");
					}
				}
				fprintf(STD_OUT, " - ");
				bool found = false;
				for (size_t i = 0;i < lockedVector.threadNames.size();i++)
				{
					if (lockedVector.threadNames[i].thread_id == tid)
					{
						fprintf(STD_OUT, "%s", lockedVector.threadNames[i].name.c_str());
						found = true;
						break;
					}
				}
				if (found == false) fprintf(STD_OUT, "Unknown Thread");
				if (CPU_COUNT(&threadmask) == 1)
				{
					for (int i = 0;i < get_number_of_cpu_cores();i++)
					{
						if (CPU_ISSET(i, &threadmask)) fprintf(STD_OUT, " - Pinned to core %d", i);
					}
				}
				fprintf(STD_OUT, "\n");
			}
		}
		closedir(dp);
	}
#endif
}