Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sthread: do not intercept valgrind nor /bin/sh, and be verbose about what we intercept
[simgrid.git] / src / sthread / sthread_impl.cpp
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
7
8 #include "smpi/smpi.h"
9 #include "xbt/string.hpp"
10 #include <simgrid/actor.h>
11 #include <simgrid/s4u/Actor.hpp>
12 #include <simgrid/s4u/Engine.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
14 #include <simgrid/s4u/NetZone.hpp>
15 #include <simgrid/s4u/Semaphore.hpp>
16 #include <xbt/base.h>
17 #include <xbt/sysdep.h>
18
19 #include "src/internal_config.h"
20 #include "src/sthread/sthread.h"
21
22 #include <cmath>
23 #include <dlfcn.h>
24 #include <pthread.h>
25 #include <semaphore.h>
26 #include <sstream>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string_view>
30 #include <thread>
31
32 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
33 namespace sg4 = simgrid::s4u;
34
35 static sg4::Host* lilibeth = nullptr;
36
37 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
38 {
39   /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
40   for (int i = 0; envp[i] != nullptr; i++)
41     if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
42       printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
43              argv[0]);
44       return raw_main(argc, argv, envp);
45     }
46
47   /* Do not intercept valgrind step 1 */
48   if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")) {
49     printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
50     fflush(stdout);
51     return raw_main(argc, argv, envp);
52   }
53
54   /* If not in SMPI, the old main becomes an actor in a newly created simulation */
55   printf("sthread is intercepting the execution of %s\n", argv[0]);
56   fflush(stdout);
57
58   sg4::Engine e(&argc, argv);
59   auto* zone = sg4::create_full_zone("world");
60   lilibeth   = zone->create_host("Lilibeth", 1e15);
61   zone->seal();
62
63   /* Launch the user's main() on an actor */
64   sthread_enable();
65   sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
66
67   sg4::Engine::get_instance()->run();
68   sthread_disable();
69   XBT_INFO("All threads exited. Terminating the simulation.");
70
71   return 0;
72 }
73
74 struct sthread_mutex {
75   s4u_Mutex* mutex;
76 };
77
78 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
79                    void* arg)
80 {
81   static int TID = 0;
82   TID++;
83   XBT_VERB("Create thread %d", TID);
84   std::string name = std::string("thread ") + std::to_string(TID);
85 #if HAVE_SMPI
86   if (SMPI_is_inited()) {
87     int rank = 0;
88     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
89     name = simgrid::xbt::string_printf("%d:%d", rank, TID);
90   }
91 #endif
92   sg4::ActorPtr actor = sg4::Actor::create(
93       name, lilibeth,
94       [](auto* user_function, auto* param) {
95 #if HAVE_SMPI
96         if (SMPI_is_inited())
97           SMPI_thread_create();
98 #endif
99         sthread_enable();
100         user_function(param);
101         sthread_disable();
102       },
103       start_routine, arg);
104
105   intrusive_ptr_add_ref(actor.get());
106   *thread = reinterpret_cast<unsigned long>(actor.get());
107   return 0;
108 }
109 int sthread_join(sthread_t thread, void** /*retval*/)
110 {
111   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
112   actor->join();
113   intrusive_ptr_release(actor.get());
114
115   return 0;
116 }
117
118 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
119 {
120   auto m = sg4::Mutex::create();
121   intrusive_ptr_add_ref(m.get());
122
123   mutex->mutex = m.get();
124   return 0;
125 }
126
127 int sthread_mutex_lock(sthread_mutex_t* mutex)
128 {
129   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
130   if (mutex->mutex == nullptr)
131     sthread_mutex_init(mutex, nullptr);
132
133   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
134   return 0;
135 }
136
137 int sthread_mutex_trylock(sthread_mutex_t* mutex)
138 {
139   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
140   if (mutex->mutex == nullptr)
141     sthread_mutex_init(mutex, nullptr);
142
143   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
144 }
145
146 int sthread_mutex_unlock(sthread_mutex_t* mutex)
147 {
148   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
149   if (mutex->mutex == nullptr)
150     sthread_mutex_init(mutex, nullptr);
151
152   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
153   return 0;
154 }
155 int sthread_mutex_destroy(sthread_mutex_t* mutex)
156 {
157   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
158   if (mutex->mutex == nullptr)
159     sthread_mutex_init(mutex, nullptr);
160
161   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
162   return 0;
163 }
164 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
165 {
166   auto s = sg4::Semaphore::create(value);
167   intrusive_ptr_add_ref(s.get());
168
169   sem->sem = s.get();
170   return 0;
171 }
172 int sthread_sem_destroy(sthread_sem_t* sem)
173 {
174   intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
175   return 0;
176 }
177 int sthread_sem_post(sthread_sem_t* sem)
178 {
179   static_cast<sg4::Semaphore*>(sem->sem)->release();
180   return 0;
181 }
182 int sthread_sem_wait(sthread_sem_t* sem)
183 {
184   static_cast<sg4::Semaphore*>(sem->sem)->acquire();
185   return 0;
186 }
187 int sthread_sem_trywait(sthread_sem_t* sem)
188 {
189   auto* s = static_cast<sg4::Semaphore*>(sem->sem);
190   if (s->would_block()) {
191     errno = EAGAIN;
192     return -1;
193   }
194   s->acquire();
195   return 0;
196 }
197 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
198 {
199   if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
200                                                               static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
201     errno = ETIMEDOUT;
202     return -1;
203   }
204   return 0;
205 }
206
207 int sthread_gettimeofday(struct timeval* tv)
208 {
209   if (tv) {
210     double now   = simgrid::s4u::Engine::get_clock();
211     double secs  = trunc(now);
212     double usecs = (now - secs) * 1e6;
213     tv->tv_sec   = static_cast<time_t>(secs);
214     tv->tv_usec  = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
215   }
216   return 0;
217 }
218
219 void sthread_sleep(double seconds)
220 {
221   simgrid::s4u::this_actor::sleep_for(seconds);
222 }
223
224 #if 0
225 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
226     *cond = sg_cond_init();
227     return 0;
228 }
229
230 int pthread_cond_signal(pthread_cond_t *cond) {
231         sg_cond_notify_one(*cond);
232     return 0;
233 }
234
235 int pthread_cond_broadcast(pthread_cond_t *cond) {
236         sg_cond_notify_all(*cond);
237     return 0;
238 }
239
240 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
241         sg_cond_wait(*cond, *mutex);
242     return 0;
243 }
244
245 int pthread_cond_destroy(pthread_cond_t *cond) {
246         sg_cond_destroy(*cond);
247     return 0;
248 }
249 #endif