Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sthread: Intercept gettimeofday + sleep + usleep
[simgrid.git] / src / sthread / sthread.c
1 /* SimGrid's pthread interposer. Redefinition of the pthread symbols (see the comment in sthread.h) */
2
3 #define _GNU_SOURCE
4 #include "src/sthread/sthread.h"
5 #include "src/internal_config.h"
6 #include <dlfcn.h>
7 #include <pthread.h>
8 #include <semaphore.h>
9 #include <stdio.h>
10 #include <unistd.h>
11
12 #if HAVE_VALGRIND_H
13 #include <stdlib.h>
14 #include <valgrind/valgrind.h>
15 #endif
16
17 /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */
18 static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
19 static int (*raw_pthread_join)(pthread_t, void**);
20 static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL;
21 static int (*raw_mutex_lock)(pthread_mutex_t*)                             = NULL;
22 static int (*raw_mutex_trylock)(pthread_mutex_t*)                          = NULL;
23 static int (*raw_mutex_unlock)(pthread_mutex_t*)                           = NULL;
24 static int (*raw_mutex_destroy)(pthread_mutex_t*)                          = NULL;
25 static sem_t* (*raw_sem_open)(const char*, int)                            = NULL;
26 static int (*raw_sem_init)(sem_t*, int, unsigned int)                      = NULL;
27 static int (*raw_sem_wait)(sem_t*)                                         = NULL;
28 static int (*raw_sem_post)(sem_t*)                                         = NULL;
29 static void intercepter_init()
30 {
31   raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create");
32   raw_pthread_join   = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join");
33   raw_mutex_init    = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init");
34   raw_mutex_lock    = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock");
35   raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock");
36   raw_mutex_unlock  = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
37   raw_mutex_destroy = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_destroy");
38
39   raw_sem_open = (sem_t * (*)(const char*, int)) dlsym(RTLD_NEXT, "sem_open");
40   raw_sem_init = (int (*)(sem_t*, int, unsigned int))dlsym(RTLD_NEXT, "sem_init");
41   raw_sem_wait = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_wait");
42   raw_sem_post = (int (*)(sem_t*))dlsym(RTLD_NEXT, "sem_post");
43 }
44
45 static int sthread_inside_simgrid = 1;
46 void sthread_enable(void)
47 { // Start intercepting all pthread calls
48   sthread_inside_simgrid = 0;
49 }
50 void sthread_disable(void)
51 { // Stop intercepting all pthread calls
52   sthread_inside_simgrid = 1;
53 }
54 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg)
55 {
56   if (raw_pthread_create == NULL)
57     intercepter_init();
58
59   if (sthread_inside_simgrid)
60     return raw_pthread_create(thread, attr, start_routine, arg);
61
62   sthread_inside_simgrid = 1;
63   int res                = sthread_create(thread, attr, start_routine, arg);
64   sthread_inside_simgrid = 0;
65   return res;
66 }
67 int pthread_join(pthread_t thread, void** retval)
68 {
69   if (raw_pthread_join == NULL)
70     intercepter_init();
71
72   if (sthread_inside_simgrid)
73     return raw_pthread_join(thread, retval);
74
75   sthread_inside_simgrid = 1;
76   int res                = sthread_join(thread, retval);
77   sthread_inside_simgrid = 0;
78   return res;
79 }
80
81 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
82 {
83   if (raw_mutex_init == NULL)
84     intercepter_init();
85
86   if (sthread_inside_simgrid)
87     return raw_mutex_init(mutex, attr);
88
89   sthread_inside_simgrid = 1;
90   int res                = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
91   sthread_inside_simgrid = 0;
92   return res;
93 }
94
95 int pthread_mutex_lock(pthread_mutex_t* mutex)
96 {
97   if (raw_mutex_lock == NULL)
98     intercepter_init();
99
100   if (sthread_inside_simgrid)
101     return raw_mutex_lock(mutex);
102
103   sthread_inside_simgrid = 1;
104   int res                = sthread_mutex_lock((sthread_mutex_t*)mutex);
105   sthread_inside_simgrid = 0;
106   return res;
107 }
108
109 int pthread_mutex_trylock(pthread_mutex_t* mutex)
110 {
111   if (raw_mutex_trylock == NULL)
112     intercepter_init();
113
114   if (sthread_inside_simgrid)
115     return raw_mutex_trylock(mutex);
116
117   sthread_inside_simgrid = 1;
118   int res                = sthread_mutex_trylock((sthread_mutex_t*)mutex);
119   sthread_inside_simgrid = 0;
120   return res;
121 }
122
123 int pthread_mutex_unlock(pthread_mutex_t* mutex)
124 {
125   if (raw_mutex_unlock == NULL)
126     intercepter_init();
127
128   if (sthread_inside_simgrid)
129     return raw_mutex_unlock(mutex);
130
131   sthread_inside_simgrid = 1;
132   int res                = sthread_mutex_unlock((sthread_mutex_t*)mutex);
133   sthread_inside_simgrid = 0;
134   return res;
135 }
136 int pthread_mutex_destroy(pthread_mutex_t* mutex)
137 {
138   if (raw_mutex_destroy == NULL)
139     intercepter_init();
140
141   if (sthread_inside_simgrid)
142     return raw_mutex_destroy(mutex);
143
144   sthread_inside_simgrid = 1;
145   int res                = sthread_mutex_destroy((sthread_mutex_t*)mutex);
146   sthread_inside_simgrid = 0;
147   return res;
148 }
149
150 int gettimeofday(struct timeval* tv, void* tz)
151 {
152   return sthread_gettimeofday(tv, tz);
153 }
154
155 unsigned int sleep(unsigned int seconds)
156 {
157   sthread_sleep(seconds);
158   return 0;
159 }
160
161 int usleep(useconds_t usec)
162 {
163   sthread_sleep(((double)usec) / 1000000.);
164   return 0;
165 }
166
167 #if 0
168 int sem_init(sem_t *sem, int pshared, unsigned int value) {
169         int res;
170
171         res=raw_sem_init(sem,pshared,value);
172         return res;
173 }
174
175 int sem_wait(sem_t *sem) {
176         int res;
177
178         res = raw_sem_wait(sem);
179         return res;
180 }
181
182 int sem_post(sem_t *sem) {
183         return raw_sem_post(sem);
184 }
185
186 int pthread_join(pthread_t thread, void **retval) {
187         sg_actor_join(thread, -1);
188     return 0;
189 }
190
191 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
192     *cond = sg_cond_init();
193     return 0;
194 }
195
196 int pthread_cond_signal(pthread_cond_t *cond) {
197         sg_cond_notify_one(*cond);
198     return 0;
199 }
200
201 int pthread_cond_broadcast(pthread_cond_t *cond) {
202         sg_cond_notify_all(*cond);
203     return 0;
204 }
205
206 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
207         sg_cond_wait(*cond, *mutex);
208     return 0;
209 }
210
211 int pthread_cond_destroy(pthread_cond_t *cond) {
212         sg_cond_destroy(*cond);
213     return 0;
214 }
215 #endif
216
217 /* Trampoline for the real main() */
218 static int (*raw_main)(int, char**, char**);
219
220 /* Our fake main() that gets called by __libc_start_main() */
221 static int main_hook(int argc, char** argv, char** envp)
222 {
223   return sthread_main(argc, argv, envp, raw_main);
224 }
225
226 /* Wrapper for __libc_start_main() that replaces the real main function with our hooked version. */
227 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
228                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end);
229
230 int __libc_start_main(int (*main)(int, char**, char**), int argc, char** argv, int (*init)(int, char**, char**),
231                       void (*fini)(void), void (*rtld_fini)(void), void* stack_end)
232 {
233   /* Save the real main function address */
234   raw_main = main;
235
236   /* Find the real __libc_start_main()... */
237   typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");
238   /* ... and call it with our custom main function */
239 #if HAVE_VALGRIND_H
240   /* ... unless valgrind is used, and this instance is not the target program (but the valgrind launcher) */
241   if (getenv("VALGRIND_LIB") && !RUNNING_ON_VALGRIND)
242     return orig(raw_main, argc, argv, init, fini, rtld_fini, stack_end);
243 #endif
244   return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
245 }