1717#include <ucm/util/reloc.h>
1818#include <ucm/util/sys.h>
1919#include <ucm/bistro/bistro.h>
20+ #include <ucs/sys/preprocessor.h>
2021#include <ucs/sys/math.h>
2122#include <ucs/sys/checker.h>
2223#include <ucs/arch/bitops.h>
3738 do { \
3839 (_data)->fired_events = 0; \
3940 _call; \
41+ ucm_trace("after %s: fired events = 0x%x", UCS_PP_MAKE_STRING(_call), \
42+ (_data)->fired_events); \
4043 (_data)->out_events &= ~((_event) & (_mask)) | (_data)->fired_events; \
4144 } while(0)
4245
@@ -74,6 +77,9 @@ static ucm_mmap_func_t ucm_mmap_funcs[] = {
7477 { {NULL , NULL }, 0 }
7578};
7679
80+ static pthread_mutex_t ucm_mmap_install_mutex = PTHREAD_MUTEX_INITIALIZER ;
81+ static int ucm_mmap_installed_events = 0 ; /* events that were reported as installed */
82+
7783static void ucm_mmap_event_test_callback (ucm_event_type_t event_type ,
7884 ucm_event_t * event , void * fired_events )
7985{
@@ -89,6 +95,8 @@ static void ucm_mmap_event_test_callback(ucm_event_type_t event_type,
8995static void
9096ucm_fire_mmap_events_internal (int events , ucm_mmap_test_events_data_t * data )
9197{
98+ size_t sbrk_size ;
99+ int sbrk_mask ;
92100 int shmid ;
93101 void * p ;
94102
@@ -129,10 +137,18 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data)
129137 }
130138
131139 if (events & (UCM_EVENT_SBRK |UCM_EVENT_VM_MAPPED |UCM_EVENT_VM_UNMAPPED )) {
132- UCM_FIRE_EVENT (events , UCM_EVENT_SBRK |UCM_EVENT_VM_MAPPED ,
133- data , (void )sbrk (ucm_get_page_size ()));
134- UCM_FIRE_EVENT (events , UCM_EVENT_SBRK |UCM_EVENT_VM_UNMAPPED ,
135- data , (void )sbrk (- ucm_get_page_size ()));
140+ if (RUNNING_ON_VALGRIND ) {
141+ /* on valgrind, doing a non-trivial sbrk() causes heap corruption */
142+ sbrk_size = 0 ;
143+ sbrk_mask = UCM_EVENT_SBRK ;
144+ } else {
145+ sbrk_size = ucm_get_page_size ();
146+ sbrk_mask = UCM_EVENT_SBRK |UCM_EVENT_VM_MAPPED |UCM_EVENT_VM_UNMAPPED ;
147+ }
148+ UCM_FIRE_EVENT (events , (UCM_EVENT_SBRK |UCM_EVENT_VM_MAPPED ) & sbrk_mask ,
149+ data , (void )sbrk (sbrk_size ));
150+ UCM_FIRE_EVENT (events , (UCM_EVENT_SBRK |UCM_EVENT_VM_UNMAPPED ) & sbrk_mask ,
151+ data , (void )sbrk (- sbrk_size ));
136152 }
137153
138154 if (events & UCM_EVENT_MADVISE ) {
@@ -157,7 +173,8 @@ void ucm_fire_mmap_events(int events)
157173 ucm_fire_mmap_events_internal (events , & data );
158174}
159175
160- ucs_status_t ucm_mmap_test_events (int events , int * out_events )
176+ /* Called with lock held */
177+ static ucs_status_t ucm_mmap_test_events (int events )
161178{
162179 ucm_event_handler_t handler ;
163180 ucm_mmap_test_events_data_t data ;
@@ -172,32 +189,27 @@ ucs_status_t ucm_mmap_test_events(int events, int *out_events)
172189 ucm_fire_mmap_events_internal (events , & data );
173190 ucm_event_handler_remove (& handler );
174191
175- * out_events = data .out_events ;
176-
177- ucm_debug ("mmap test: got 0x%x out of 0x%x" , * out_events , events );
192+ ucm_debug ("mmap test: got 0x%x out of 0x%x" , data .out_events , events );
178193
179194 /* Return success if we caught all wanted events */
180- if (!ucs_test_all_flags (* out_events , events )) {
195+ if (!ucs_test_all_flags (data . out_events , events )) {
181196 return UCS_ERR_UNSUPPORTED ;
182197 }
183198
184199 return UCS_OK ;
185200}
186201
187- /* Called with lock held */
188- static ucs_status_t ucm_mmap_test (int events )
202+ ucs_status_t ucm_mmap_test_installed_events (int events )
189203{
190- static int installed_events = 0 ;
191- int out_events = 0 ; /* GCC bug: it reports compilation fail if not initialized */
192204 ucs_status_t status ;
193205
194- if ( ucs_test_all_flags ( installed_events , events )) {
195- /* All requested events are already installed */
196- return UCS_OK ;
197- }
198-
199- status = ucm_mmap_test_events (events , & out_events );
200- installed_events |= out_events ;
206+ /*
207+ * return UCS_OK iff all installed events are actually working
208+ * we don't check the status of events which were not successfully installed
209+ */
210+ pthread_mutex_lock ( & ucm_mmap_install_mutex );
211+ status = ucm_mmap_test_events (events & ucm_mmap_installed_events );
212+ pthread_mutex_unlock ( & ucm_mmap_install_mutex ) ;
201213
202214 return status ;
203215}
@@ -250,14 +262,18 @@ static ucs_status_t ucs_mmap_install_reloc(int events)
250262
251263ucs_status_t ucm_mmap_install (int events )
252264{
253- static pthread_mutex_t install_mutex = PTHREAD_MUTEX_INITIALIZER ;
254265 ucs_status_t status ;
255266
256- pthread_mutex_lock (& install_mutex );
267+ pthread_mutex_lock (& ucm_mmap_install_mutex );
257268
258- status = ucm_mmap_test (events );
259- if (status == UCS_OK ) {
260- goto out_unlock ;
269+ if (ucs_test_all_flags (ucm_mmap_installed_events , events )) {
270+ /* if we already installed these events, check that they are still
271+ * working, and if not - reinstall them.
272+ */
273+ status = ucm_mmap_test_events (events );
274+ if (status == UCS_OK ) {
275+ goto out_unlock ;
276+ }
261277 }
262278
263279 status = ucs_mmap_install_reloc (events );
@@ -266,9 +282,17 @@ ucs_status_t ucm_mmap_install(int events)
266282 goto out_unlock ;
267283 }
268284
269- status = ucm_mmap_test (events );
285+ status = ucm_mmap_test_events (events );
286+ if (status != UCS_OK ) {
287+ ucm_debug ("failed to install mmap events" );
288+ goto out_unlock ;
289+ }
290+
291+ /* status == UCS_OK */
292+ ucm_mmap_installed_events |= events ;
293+ ucm_debug ("mmap installed events = 0x%x" , ucm_mmap_installed_events );
270294
271295out_unlock :
272- pthread_mutex_unlock (& install_mutex );
296+ pthread_mutex_unlock (& ucm_mmap_install_mutex );
273297 return status ;
274298}
0 commit comments