-
Notifications
You must be signed in to change notification settings - Fork 0
/
xthread.hh
359 lines (332 loc) · 11 KB
/
xthread.hh
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright (C)
* 2017 - Jinpeng Zhou, [email protected]
* This program 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
/**
* @file xthread.hh
* @brief Functions to handle thread-related operations
* @author Tongping Liu, <http://www.cs.utsa.edu/~tongpingliu/>
* @author Jinpeng Zhou, [email protected]
*/
#ifndef __XTHREAD_HH__
#define __XTHREAD_HH__
#include "threadstruct.hh"
#include "selfmap.hh"
#ifdef ENABLE_ANALYZER
#include "analyzer.hh"
#endif
#ifdef EBANBLE_PREVENTION
#include "prevention.hh"
#endif
extern thread_t *threadsInfo;
extern real_thread_t *threadsInfoReal;
extern uintptr_t globalStackAddr;
extern volatile int aliveThreads;
extern bool isSingleThread;
extern void* textTop;
class xthread {
private:
xthread() { }
public:
static xthread& getInstance() {
static char buf[sizeof(xthread)];
static xthread * theOneTrueObject = new (buf) xthread();
return *theOneTrueObject;
}
/// @brief Initialize the system.
void initialize() {
#ifdef ENABLE_ANALYZER
analyzer::getInstance().initialize();
#endif
WRAP(pthread_mutex_init)(&_gMutex, NULL);
// Initialze the Main thread
thread_t* current = getThreadInfoByIndex(0);
current->tIndex = 0;
current->startRoutine = 0;
initializeRecord(current);
selfmap::getInstance().getTop(¤t->stackTop, &textTop);
_threadIndex = 1;
_threadIndexReal = 0;
_monitor = 0;
installSignalHandler();
// initialize the <pthread_t, thread index> map.
_xmap.initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, xdefines::THREAD_MAP_SIZE);
_xmap.insert((void*)pthread_self(), sizeof(void*), 0);
}
// The end of system.
void finalize(void) {
#ifndef RUNTIME_OVERHEAD
#ifdef ENABLE_ANALYZER
#ifdef MONITOR_THREAD
if(_monitor > 0) pthread_kill(_monitor, 0);
#endif
fprintf(stderr, "start analyzing..\n");
for(int i = 0; i < _threadIndex; i++) {
if(threadsInfo[i].tIndex >= 0) {
threadsInfoReal[_threadIndexReal].dependencies = threadsInfo[i].dependencies;
threadsInfoReal[_threadIndexReal++].depCount = threadsInfo[i].depCount;
}
}
analyzer::getInstance().finalize(_threadIndexReal);
#endif
#endif
}
void installSignalHandler() {
struct sigaction siga;
// Point to the handler function.
siga.sa_flags = SA_RESTART | SA_NODEFER;
siga.sa_handler = sigHandler;
if (sigaction(SIGINT, &siga, NULL) == -1) {
perror ("installing SIGINT failed\n");
exit (-1);
}
if (sigaction(SIGQUIT, &siga, NULL) == -1) {
perror ("installing SIGQUIT failed\n");
exit (-1);
}
if (sigaction(SIGHUP, &siga, NULL) == -1) {
perror ("installing SIGHUP failed\n");
exit (-1);
}
if (sigaction(SIGTERM, &siga, NULL) == -1) {
perror ("installing SIGTERM failed\n");
exit (-1);
}
#ifdef USING_SIGUSR1
if (sigaction(SIGUSR1, &siga, NULL) == -1) {
perror ("installing SIGUSR1 failed\n");
exit (-1);
}
#endif
#ifdef USING_SIGUSR2
if (sigaction(SIGUSR2, &siga, NULL) == -1) {
perror ("installing SIGUSR2 failed\n");
exit (-1);
}
#endif
}
static void sigHandler(int signum) {
if(signum == SIGINT) {
fprintf(stderr, "Recieved SIGINT, Genearting Report:\n");
exit(0);
} else if (signum == SIGQUIT) {
fprintf(stderr, "Recieved SIGQUIT, Generating Report:\n");
exit(0);
} else if (signum == SIGHUP) {
fprintf(stderr, "Recieved SIGHUP, Generating Report:\n");
exit(0);
} else if (signum == SIGTERM) {
fprintf(stderr, "Recieved SIGTERM, Generating Report:\n");
exit(0);
} else if (signum == SIGUSR1) {
fprintf(stderr, "Recieved SIGUSR1, Generating Report:\n");
exit(0);
} else if (signum == SIGUSR2) {
fprintf(stderr, "Recieved SIGUSR2, Generating Report:\n");
exit(0);
}
}
// initialize the thread related data
INLINE static void initializeRecord(thread_t* thread) {
thread->dependencies = new Dependency[xdefines::MAX_DEPENDENCY];
thread->curDep = NULL;
thread->depCount = 0;
thread->isRecursive = false;
if(thread->holdingSet == NULL) {
// initialize for the 1st time
thread->holdingSet = new void*[xdefines::MAX_HOLDING_DEPTH];
thread->dependencyMap = new DependencyAddrHashMap;
thread->dependencyMap->initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, xdefines::MAX_DEPENDENCY);
thread->offsetMap = new OffsetHashMap;
thread->offsetMap->initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, xdefines::MAX_DEPENDENCY);
thread->initOffsetMap = new OffsetHashMap;
thread->initOffsetMap->initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, xdefines::MAX_DEPENDENCY);
} else {
// clear old for re-use
for(DependencyAddrHashMap::iterator iter = thread->dependencyMap->begin(); iter != thread->dependencyMap->end(); iter++) {
thread->dependencyMap->erase(iter.getkey(), 8);
}
for(OffsetHashMap::iterator iter = thread->offsetMap->begin(); iter != thread->offsetMap->end(); iter++) {
thread->offsetMap->erase(iter.getkey(), 8);
}
for(OffsetHashMap::iterator iter = thread->initOffsetMap->begin(); iter != thread->offsetMap->end(); iter++) {
thread->initOffsetMap->erase(iter.getkey(), 8);
}
}
thread->holdingCount = 0;
#ifdef ENABLE_PREVENTION
if(thread->specialHolding == NULL) {
thread->specialHolding = new special_holding[prevention::getInstance().getMergeSetAmount()];
}
#endif
}
INLINE thread_t * getThreadInfoByIndex(int index){
//assert(index < xdefines::MAX_THREADS);
return &threadsInfo[index];
}
/// @ Intercepting the thread_creation operation.
int thread_create(pthread_t * tid, const pthread_attr_t * attr, threadFunction * fn, void * arg) {
int tindex;
// Protect the allocation of thread index.
global_lock();
// Allocate a global thread index for current thread.
if(aliveThreads++ < _threadIndex) {
for(int i = 0; i < _threadIndex; i++) {
if(threadsInfo[i].tIndex < 0) {
// find the available slot
tindex = threadsInfo[i].tIndex = i;
break;
}
}
} else {
tindex = _threadIndex++;
}
#if (!defined RUNTIME_OVERHEAD && defined ENABLE_ANALYZER && defined MONITOR_THREAD)
if(_monitor == 0) {
WRAP(pthread_create)(&_monitor, NULL, monitorThread, threadsInfo);
}
#endif
global_unlock();
thread_t * children = getThreadInfoByIndex(tindex);
children->tIndex = tindex;
children->startRoutine = fn;
children->startArg = arg;
uintptr_t start = globalStackAddr + (uintptr_t)tindex * xdefines::STACK_SIZE;
children->stackTop = (void*)(start + xdefines::STACK_SIZE);
// modify the stack: lowest addr and size
pthread_attr_t iattr;
if(attr == NULL) {
pthread_attr_init(&iattr);
} else {
iattr = *attr;
}
pthread_attr_setstack(&iattr, (void*)start, xdefines::STACK_SIZE);
int ret = WRAP(pthread_create)(tid, &iattr, startThread, (void*)children);
// after real creation
global_lock();
if(ret == 0) {
_xmap.insertIfAbsent((void*)*tid, sizeof(void*), tindex);
} else {
aliveThreads--;
children->tIndex = -1;
}
global_unlock();
return ret;
}
static void* startThread(void* arg) {
thread_t* current = (thread_t*)arg;
isSingleThread = false;
initializeRecord(current);
void* result = current->startRoutine(current->startArg);
return result;
}
#ifdef ENABLE_ANALYZER
INLINE static void checkNew(thread_t* threads, void** lastHolding, int threadIndex, bool* sthNew, int* candidate) {
for(int i = 0; i < threadIndex; i++) {
thread_t* thread = &threads[i];
int holds = thread->holdingCount - 1;
// check holding status
if(holds >= 0 && lastHolding[i] != thread->holdingSet[holds]) {
lastHolding[i] = thread->holdingSet[holds];
*sthNew = true;
if(holds > 0) (*candidate)++;
} else if (holds < 0 && lastHolding[i] != NULL) {
lastHolding[i] = NULL;
*sthNew = true;
}
}
}
static void* monitorThread(void* arg) {
thread_t* threads = (thread_t*)arg;
// current status
void* lastHolding[xdefines::MAX_THREADS] = {NULL};
int notRunning = 0;
bool hasCycle = false;
ChainStack* stack = new ChainStack;
while(1) {
// sleep
sleep(xdefines::MONITOR_PERIOD);
if(aliveThreads < 2) continue; // if single thread, do nothing
int threadIndex = xthread::getInstance().getThreadIndex();
int candidate = 0; // how many threads are holding locks
// check whether there's something new in all threads status
bool sthNew = false;
checkNew(threads, lastHolding, threadIndex, &sthNew, &candidate);
for(int i = 0; i < threadIndex; i++) {
thread_t* thread = &threads[i];
int holds = thread->holdingCount - 1;
// check holding status
if(holds >= 0 && lastHolding[i] != thread->holdingSet[holds]) {
lastHolding[i] = thread->holdingSet[holds];
sthNew = true;
if(holds > 0) candidate++;
} else if (holds < 0 && lastHolding[i] != NULL) {
lastHolding[i] = NULL;
sthNew = true;
}
}
#if 0
if(!sthNew) { // nothing new
if(hasCycle && notRunning++ > xdefines::MONITOR_THRESHOLD) {
fprintf(stderr, "Monitor thread found cycles and nothing new happend during THRESHOLD. Now exit!\n");
exit(0);
}
continue;
}
notRunning = 0;
#else
if(!sthNew) continue;
if(candidate > 1) { // check cycles if at least 2 threads
// check current status and terminate if confirm a deadlock
analyzer::getInstance().analysisCurrent(threadIndex, stack, lastHolding);
}
#endif
}
delete stack;
}
#endif
int thread_join(pthread_t tid, void** retval) {
int ret = WRAP(pthread_join)(tid, retval);
if(ret == 0) {
int joinee = -1;
// update after join
global_lock();
if(!_xmap.find((void*)tid, sizeof(void*), &joinee)) {
fprintf(stderr, "Cannot find joinee index for thread %p\n", (void*)tid);
} else {
thread_t* joineeThread = &threadsInfo[joinee];
joineeThread->tIndex = -1; // for re-use
// save info
threadsInfoReal[_threadIndexReal].dependencies = joineeThread->dependencies;
threadsInfoReal[_threadIndexReal++].depCount = joineeThread->depCount;
}
if(--aliveThreads <= 1) isSingleThread = true;
global_unlock();
}
return ret;
}
INLINE int getThreadIndex() { return _threadIndex; }
private:
pthread_t _monitor;
volatile int _threadIndex; // each thread has an index
volatile int _threadIndexReal; // for detection
typedef HashMap<void*, int, HeapAllocator> threadHashMap;
threadHashMap _xmap; // The hash map that map the address of pthread_t to thread index.
pthread_mutex_t _gMutex; // mutex lock to protect thread index
void global_lock(){ WRAP(pthread_mutex_lock)(&_gMutex); }
void global_unlock(){ WRAP(pthread_mutex_unlock)(&_gMutex); }
};
#endif