From bcece0f9a647a15bf073294ee29c369fbb400f27 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Sun, 29 Nov 2020 18:27:28 +0100 Subject: [PATCH 1/9] first try to make Extrae threadsafe, especially for irregular pthread creation --- src/tracer/hwc/common_hwc.c | 22 ++++++- src/tracer/hwc/papi_hwc.c | 16 ++++- src/tracer/mode.c | 9 +++ src/tracer/mode.h | 13 +++- src/tracer/signals.c | 2 +- src/tracer/wrappers/API/wrapper.c | 62 ++++++++++++++----- src/tracer/wrappers/MPI/mpi_wrapper.c | 3 +- src/tracer/wrappers/pthread/pthread_wrapper.c | 7 ++- 8 files changed, 111 insertions(+), 23 deletions(-) diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index 557ca65f..a6dece8a 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -50,6 +50,9 @@ #if defined(ENABLE_PEBS_SAMPLING) # include "sampling-intel-pebs.h" #endif +#include + +static pthread_rwlock_t pThread_mtx_HWC_Start_Counters = PTHREAD_RWLOCK_INITIALIZER; /*------------------------------------------------ Global Variables ---------*/ int HWCEnabled = FALSE; /* Have the HWC been started? */ @@ -373,6 +376,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) /* Allocate memory if this process has not been forked */ if (!forked) { + pthread_rwlock_wrlock(&pThread_mtx_HWC_Start_Counters); HWC_Thread_Initialized = (int *) malloc (sizeof(int) * num_threads); ASSERT(HWC_Thread_Initialized!=NULL, "Cannot allocate memory for HWC_Thread_Initialized!"); @@ -392,6 +396,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) ASSERT(Accumulated_HWC[i]!=NULL, "Cannot allocate memory for Accumulated_HWC"); HWC_Accum_Reset(i); } + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); if (HWC_num_sets <= 0) return; @@ -424,7 +429,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) void HWC_Restart_Counters (int old_num_threads, int new_num_threads) { int i; - + pthread_rwlock_wrlock(&pThread_mtx_HWC_Start_Counters); #if defined(PAPI_COUNTERS) for (i = 0; i < HWC_num_sets; i++) HWCBE_PAPI_Allocate_eventsets_per_thread (i, old_num_threads, new_num_threads); @@ -465,6 +470,7 @@ void HWC_Restart_Counters (int old_num_threads, int new_num_threads) HWC_current_timebegin[i] = 0; HWC_current_glopsbegin[i] = 0; } + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); } /** @@ -629,6 +635,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) if (HWCEnabled) { + pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( Accumulated_HWC[tid] ); @@ -642,6 +649,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) #endif Accumulated_HWC_Valid[tid] = TRUE; + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); } return (HWCEnabled && accum_ok); } @@ -655,8 +663,10 @@ int HWC_Accum_Reset (unsigned int tid) { if (HWCEnabled) { + pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); Accumulated_HWC_Valid[tid] = FALSE; memset(Accumulated_HWC[tid], 0, MAX_HWC * sizeof(long long)); + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); return 1; } else return 0; @@ -665,7 +675,11 @@ int HWC_Accum_Reset (unsigned int tid) /** Returns whether Accumulated_HWC contains valid values or not */ int HWC_Accum_Valid_Values (unsigned int tid) { - return ( HWCEnabled ? Accumulated_HWC_Valid[tid] : 0 ); + int ret; + pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); + ret = ( HWCEnabled ? Accumulated_HWC_Valid[tid] : 0 ); + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + return ret; } /** @@ -677,7 +691,9 @@ int HWC_Accum_Copy_Here (unsigned int tid, long long *store_buffer) { if (HWCEnabled) { + pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); memcpy(store_buffer, Accumulated_HWC[tid], MAX_HWC * sizeof(long long)); + pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); return 1; } else return 0; @@ -693,10 +709,12 @@ int HWC_Accum_Add_Here (unsigned int tid, long long *store_buffer) int i; if (HWCEnabled) { + pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); for (i=0; i #if defined(IS_BGL_MACHINE) # define COUNTERS_INFO @@ -69,6 +70,8 @@ static HWC_Definition_t *hwc_used = NULL; static unsigned num_hwc_used = 0; +static pthread_rwlock_t pThread_mtx_HWC_sets = PTHREAD_RWLOCK_INITIALIZER; + static void HWCBE_PAPI_AddDefinition (unsigned event_code, char *code, char *description) { int found = FALSE; @@ -107,6 +110,7 @@ int HWCBE_PAPI_Allocate_eventsets_per_thread (int num_set, int old_thread_num, i { int i; + pthread_rwlock_wrlock(&pThread_mtx_HWC_sets); HWC_sets[num_set].eventsets = (int *) realloc (HWC_sets[num_set].eventsets, sizeof(int)*new_thread_num); if (HWC_sets[num_set].eventsets == NULL) { @@ -116,6 +120,7 @@ int HWCBE_PAPI_Allocate_eventsets_per_thread (int num_set, int old_thread_num, i for (i = old_thread_num; i < new_thread_num; i++) HWC_sets[num_set].eventsets[i] = PAPI_NULL; + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return TRUE; } @@ -207,6 +212,7 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count ncounters = MAX_HWC; } + pthread_rwlock_wrlock(&pThread_mtx_HWC_sets); HWC_sets = (struct HWC_Set_t *) realloc (HWC_sets, sizeof(struct HWC_Set_t)* (HWC_num_sets+1)); if (HWC_sets == NULL) { @@ -345,6 +351,7 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count pretended_set); HWC_sets[num_set].domain = PAPI_DOM_USER; } + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); HWCBE_PAPI_Allocate_eventsets_per_thread (num_set, 0, Backend_getNumberOfThreads()); @@ -669,6 +676,7 @@ int __in_PAPI_read_BG = FALSE; #endif int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { + pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); int EventSet = HWCEVTSET(tid); #if !defined(IS_BG_MACHINE) @@ -676,8 +684,10 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return 0; } + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return 1; #else if (!__in_PAPI_read_BG) @@ -687,13 +697,17 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return 0; } __in_PAPI_read_BG = FALSE; + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return 1; } - else + else { + pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return 0; + } #endif } diff --git a/src/tracer/mode.c b/src/tracer/mode.c index cac9083a..0bdb17ab 100644 --- a/src/tracer/mode.c +++ b/src/tracer/mode.c @@ -31,6 +31,7 @@ #include "hwc.h" #include "mode.h" #include "utils.h" +#include int *MPI_Deepness = NULL; int *Current_Trace_Mode = NULL; @@ -45,6 +46,8 @@ int Starting_Trace_Mode = TRACE_MODE_DETAIL; unsigned long long BurstsMode_Threshold = 10000000; /* 10ms */ int BurstsMode_MPI_Stats = ENABLED; +static pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize = PTHREAD_RWLOCK_INITIALIZER; + static int is_ValidMode (int mode) { switch(mode) @@ -72,6 +75,7 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) size = sizeof(int) * new_num_threads; + pthread_rwlock_wrlock(&pThread_mtx_Trace_Mode_reInitialize); MPI_Deepness = (int *)realloc(MPI_Deepness, size); if (MPI_Deepness == NULL) { @@ -115,6 +119,7 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) Pending_Trace_Mode_Change[i] = FALSE; First_Trace_Mode[i] = TRUE; } + pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); return TRUE; } @@ -153,6 +158,7 @@ int Trace_Mode_Initialize (int num_threads) void Trace_Mode_Change (int tid, iotimer_t time) { + pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); if (Pending_Trace_Mode_Change[tid] || First_Trace_Mode[tid]) { if (Future_Trace_Mode[tid] != Current_Trace_Mode[tid] || First_Trace_Mode[tid]) @@ -173,6 +179,7 @@ void Trace_Mode_Change (int tid, iotimer_t time) Pending_Trace_Mode_Change[tid] = FALSE; First_Trace_Mode[tid] = FALSE; } + pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); } void @@ -186,11 +193,13 @@ Trace_mode_switch(void) * number of threads, only the "old" threads will use burst mode, while the * "new" ones will continue in detail. */ + pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); for (i=0; i extern int *MPI_Deepness; extern int *Current_Trace_Mode; extern int *Pending_Trace_Mode_Change; -#define CURRENT_TRACE_MODE(tid) Current_Trace_Mode[tid] +extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; + +#define CURRENT_TRACE_MODE(tid) Current_Trace_Mode[tid]\ +// { \ +// int ret; \ +// pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); \ +// ret = Current_Trace_Mode[tid]; \ +// pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); \ +// return ret; \ +// } + #define PENDING_TRACE_MODE_CHANGE(tid) Pending_Trace_Mode_Change[tid] #define INCREASE_MPI_DEEPNESS(tid) (MPI_Deepness[tid]++) diff --git a/src/tracer/signals.c b/src/tracer/signals.c index 094d873e..60cfd2c1 100644 --- a/src/tracer/signals.c +++ b/src/tracer/signals.c @@ -50,7 +50,7 @@ * Flushes the buffers to disk and disables tracing * ----------------------------------------------------------------------- */ -int sigInhibited = FALSE; +volatile int sigInhibited = FALSE; void Signals_Inhibit() { diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index 2c23b014..c4d93240 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -23,6 +23,14 @@ #include "common.h" +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include + #ifdef HAVE_STDIO_H # include #endif @@ -157,7 +165,11 @@ int Extrae_Flush_Wrapper (Buffer_t *buffer); static int requestedDynamicMemoryInstrumentation = FALSE; +static pthread_mutex_t pThreadChangeNumberOfThreads_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_rwlock_t pThread_mtx_Realloc = PTHREAD_RWLOCK_INITIALIZER; +static pthread_rwlock_t pThread_mtx_Extrae_inInstrumentation = PTHREAD_RWLOCK_INITIALIZER; +extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; #if defined(STANDALONE) module_t *Modules = NULL; @@ -487,11 +499,14 @@ void Extrae_AnnotateCPU (UINT64 timestamp) #if defined(HAVE_SCHED_GETCPU) int cpu = sched_getcpu(); + pthread_rwlock_rdlock(&pThread_mtx_Realloc); if (cpu != LastCPUEvent[THREADID] || AlwaysEmitCPUEvent) { LastCPUEvent[THREADID] = cpu; TRACE_EVENT (timestamp, GETCPU_EV, cpu); } + + pthread_rwlock_unlock(&pThread_mtx_Realloc); #else UNREFERENCED_PARAMETER(timestamp); #endif @@ -1422,6 +1437,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork UNREFERENCED_PARAMETER(world_size); #endif + pthread_rwlock_wrlock(&pThread_mtx_Realloc); if (!forked) { xmalloc(TracingBuffer, num_threads * sizeof(Buffer_t *)); @@ -1434,7 +1450,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork for (i = 0; i < num_threads; i++) Allocate_buffer_and_file (i, forked); - + pthread_rwlock_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1446,17 +1462,16 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork static int Reallocate_buffers_and_files (int new_num_threads) { int i; - + pthread_rwlock_wrlock(&pThread_mtx_Realloc); xrealloc(TracingBuffer, TracingBuffer, new_num_threads * sizeof(Buffer_t *)); xrealloc(LastCPUEmissionTime, LastCPUEmissionTime, new_num_threads * sizeof(iotimer_t)); xrealloc(LastCPUEvent, LastCPUEvent, new_num_threads * sizeof(int)); #if defined(SAMPLING_SUPPORT) xrealloc(SamplingBuffer, SamplingBuffer, new_num_threads * sizeof(Buffer_t *)); #endif - for (i = get_maximum_NumOfThreads(); i < new_num_threads; i++) Allocate_buffer_and_file (i, FALSE); - + pthread_rwlock_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1961,11 +1976,17 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) { unsigned new_num_threads = numberofthreads; + pthread_mutex_lock (&pThreadChangeNumberOfThreads_mtx); + if (EXTRAE_INITIALIZED()) { /* Just modify things if there are more threads */ if (new_num_threads > get_maximum_NumOfThreads()) { + char cur_host_name[100]; + cur_host_name[99] = '\0'; + gethostname(cur_host_name, 99); + fprintf(stderr, "%s OS_TID:%ld Start Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); unsigned u; #if defined(ENABLE_PEBS_SAMPLING) @@ -2013,7 +2034,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) #if defined(ENABLE_PEBS_SAMPLING) Extrae_IntelPEBS_resumeSampling(); #endif - + fprintf(stderr, "%s OS_TID:%ld End Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); } else current_NumOfThreads = new_num_threads; @@ -2026,6 +2047,8 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) current_NumOfThreads = new_num_threads; } + pthread_mutex_unlock (&pThreadChangeNumberOfThreads_mtx); + return TRUE; } @@ -2591,26 +2614,33 @@ static int *Extrae_inSampling = NULL; int Backend_inInstrumentation (unsigned thread) { + pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); + int ret = FALSE; if ((Extrae_inInstrumentation != NULL) && (Extrae_inSampling != NULL)) - return (Extrae_inInstrumentation[thread] || Extrae_inSampling[thread]); - else - return FALSE; + ret = (Extrae_inInstrumentation[thread] || Extrae_inSampling[thread]); + pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); + return ret; } -void Backend_setInSampling (unsigned thread, int insampling) -{ - if (Extrae_inSampling != NULL) - Extrae_inSampling[thread] = insampling; -} +void Backend_setInSampling (unsigned thread, int insampling) +{ + pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); + if (Extrae_inSampling != NULL) + Extrae_inSampling[thread] = insampling; + pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); +} void Backend_setInInstrumentation (unsigned thread, int ininstrumentation) { + pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); if (Extrae_inInstrumentation != NULL) Extrae_inInstrumentation[thread] = ininstrumentation; + pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); } void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) { + pthread_rwlock_wrlock(&pThread_mtx_Extrae_inInstrumentation); Extrae_inInstrumentation = (int*) realloc (Extrae_inInstrumentation, sizeof(int)*nthreads); if (Extrae_inInstrumentation == NULL) { @@ -2625,6 +2655,7 @@ void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) ": Failed to allocate memory for inSampling structure\n"); exit (-1); } + pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); } void Backend_Enter_Instrumentation () @@ -2675,9 +2706,10 @@ void Backend_Enter_Instrumentation () #endif /* Check whether we will fill the buffer soon (or now) */ + pthread_rwlock_rdlock(&pThread_mtx_Realloc); if (Buffer_RemainingEvents(TracingBuffer[thread]) <= NEVENTS) Buffer_ExecuteFlushCallback (TracingBuffer[thread]); - + pthread_rwlock_unlock(&pThread_mtx_Realloc); /* Record the time when this is happening we need this for subsequent calls to TIME, as this is being cached in clock routines */ @@ -2710,8 +2742,10 @@ void Backend_Leave_Instrumentation (void) } /* Change trace mode? (issue from API) */ + pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); if (PENDING_TRACE_MODE_CHANGE(thread) && MPI_Deepness[thread] == 0) Trace_Mode_Change(thread, LAST_READ_TIME); + pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); Backend_setInInstrumentation (thread, FALSE); } diff --git a/src/tracer/wrappers/MPI/mpi_wrapper.c b/src/tracer/wrappers/MPI/mpi_wrapper.c index 36d5f867..af479554 100644 --- a/src/tracer/wrappers/MPI/mpi_wrapper.c +++ b/src/tracer/wrappers/MPI/mpi_wrapper.c @@ -264,7 +264,8 @@ void translateLocalToGlobalRank (MPI_Comm comm, MPI_Group group, int partner_loc PMPI_Group_size(remote_group, &remote_group_size); local_ranks = (int *)malloc(sizeof(int) * remote_group_size); world_ranks = (int *)malloc(sizeof(int) * remote_group_size); - for (int i = 0; i < remote_group_size; i++) local_ranks[i] = i; + int i; + for (i = 0; i < remote_group_size; i++) local_ranks[i] = i; PMPI_Group_translate_ranks (remote_group, remote_group_size, local_ranks, CommWorldRanks, world_ranks); diff --git a/src/tracer/wrappers/pthread/pthread_wrapper.c b/src/tracer/wrappers/pthread/pthread_wrapper.c index e8d755e7..55afcfd2 100644 --- a/src/tracer/wrappers/pthread/pthread_wrapper.c +++ b/src/tracer/wrappers/pthread/pthread_wrapper.c @@ -74,15 +74,13 @@ static int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *) = NULL; static int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; static int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; -static pthread_mutex_t extrae_pthread_create_mutex; +static pthread_mutex_t extrae_pthread_create_mutex = PTHREAD_MUTEX_INITIALIZER; #endif /* PIC */ static void GetpthreadHookPoints (int rank) { #if defined(PIC) - /* Create mutex to protect pthread_create calls */ - pthread_mutex_init (&extrae_pthread_create_mutex, NULL); /* Obtain @ for pthread_create */ pthread_create_real = @@ -221,6 +219,9 @@ static void * pthread_create_hook (void *p1) /* Wake up the calling thread */ pthread_barrier_wait_real(&(i->barrier)); + if (pthread_create_real == NULL) + GetpthreadHookPoints(0); + Backend_Enter_Instrumentation (); Probe_pthread_Function_Entry (routine); Backend_Leave_Instrumentation (); From e1f66bfa089d9315988f4f3941a021e220233c7c Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 11:57:58 +0100 Subject: [PATCH 2/9] further tests with thread safety --- src/tracer/Makefile.am | 1 + src/tracer/hwc/common_hwc.c | 46 ++-- src/tracer/hwc/papi_hwc.c | 51 ++-- src/tracer/mode.c | 10 +- src/tracer/pthread_redirect.c | 190 +++++++++++++ src/tracer/pthread_redirect.h | 59 +++++ src/tracer/signals.c | 13 +- src/tracer/wrappers/API/wrapper.c | 10 +- src/tracer/wrappers/pthread/Makefile.am | 3 +- src/tracer/wrappers/pthread/pthread_wrapper.c | 249 +++++++++--------- 10 files changed, 464 insertions(+), 168 deletions(-) create mode 100644 src/tracer/pthread_redirect.c create mode 100644 src/tracer/pthread_redirect.h diff --git a/src/tracer/Makefile.am b/src/tracer/Makefile.am index cbdb36c7..d5a92471 100644 --- a/src/tracer/Makefile.am +++ b/src/tracer/Makefile.am @@ -153,6 +153,7 @@ WRAPPERS_CORE = \ core_SRCS = \ calltrace.c calltrace.h \ signals.c signals.h \ + pthread_redirect.c pthread_redirect.h \ xml-parse.c xml-parse.h \ UF_gcc_instrument.c UF_gcc_instrument.h \ UF_xl_instrument.c UF_xl_instrument.h \ diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index a6dece8a..032893f7 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -51,6 +51,7 @@ # include "sampling-intel-pebs.h" #endif #include +#include "pthread_redirect.h" static pthread_rwlock_t pThread_mtx_HWC_Start_Counters = PTHREAD_RWLOCK_INITIALIZER; @@ -106,7 +107,11 @@ int HWC_IsEnabled() */ int HWC_Get_Current_Set (int threadid) { - return HWC_current_set[threadid]; + int ret; + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + ret = HWC_current_set[threadid]; + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + return ret; } /* @@ -197,9 +202,11 @@ void HWC_Stop_Current_Set (UINT64 time, int thread_id) { /* make sure we don't loose the current counter values */ Extrae_counters_at_Time_Wrapper(time); - + + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); /* Actually stop the counters */ HWCBE_STOP_SET (time, HWC_current_set[thread_id], thread_id); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); } } @@ -212,8 +219,10 @@ void HWC_Start_Current_Set (UINT64 countglops, UINT64 time, int thread_id) /* If there are less than 2 sets, don't do anything! */ if (HWC_num_sets > 0) { + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); /* Actually start the counters */ HWCBE_START_SET (countglops, time, HWC_current_set[thread_id], thread_id); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); } } @@ -229,11 +238,13 @@ void HWC_Start_Next_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); /* Move to the next set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = (HWC_current_set[thread_id] + 1) % HWC_num_sets; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -250,11 +261,13 @@ void HWC_Start_Previous_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); /* Move to the previous set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = ((HWC_current_set[thread_id] - 1) < 0) ? (HWC_num_sets - 1) : (HWC_current_set[thread_id] - 1) ; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -376,7 +389,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) /* Allocate memory if this process has not been forked */ if (!forked) { - pthread_rwlock_wrlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); HWC_Thread_Initialized = (int *) malloc (sizeof(int) * num_threads); ASSERT(HWC_Thread_Initialized!=NULL, "Cannot allocate memory for HWC_Thread_Initialized!"); @@ -396,10 +409,11 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) ASSERT(Accumulated_HWC[i]!=NULL, "Cannot allocate memory for Accumulated_HWC"); HWC_Accum_Reset(i); } - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); - if (HWC_num_sets <= 0) + if (HWC_num_sets <= 0) { return; + } HWCEnabled = TRUE; } @@ -429,7 +443,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) void HWC_Restart_Counters (int old_num_threads, int new_num_threads) { int i; - pthread_rwlock_wrlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); #if defined(PAPI_COUNTERS) for (i = 0; i < HWC_num_sets; i++) HWCBE_PAPI_Allocate_eventsets_per_thread (i, old_num_threads, new_num_threads); @@ -470,7 +484,7 @@ void HWC_Restart_Counters (int old_num_threads, int new_num_threads) HWC_current_timebegin[i] = 0; HWC_current_glopsbegin[i] = 0; } - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); } /** @@ -635,7 +649,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) if (HWCEnabled) { - pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( Accumulated_HWC[tid] ); @@ -649,7 +663,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) #endif Accumulated_HWC_Valid[tid] = TRUE; - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); } return (HWCEnabled && accum_ok); } @@ -663,10 +677,8 @@ int HWC_Accum_Reset (unsigned int tid) { if (HWCEnabled) { - pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); Accumulated_HWC_Valid[tid] = FALSE; memset(Accumulated_HWC[tid], 0, MAX_HWC * sizeof(long long)); - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); return 1; } else return 0; @@ -676,9 +688,9 @@ int HWC_Accum_Reset (unsigned int tid) int HWC_Accum_Valid_Values (unsigned int tid) { int ret; - pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); ret = ( HWCEnabled ? Accumulated_HWC_Valid[tid] : 0 ); - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); return ret; } @@ -691,9 +703,9 @@ int HWC_Accum_Copy_Here (unsigned int tid, long long *store_buffer) { if (HWCEnabled) { - pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); memcpy(store_buffer, Accumulated_HWC[tid], MAX_HWC * sizeof(long long)); - pthread_rwlock_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); return 1; } else return 0; @@ -709,12 +721,12 @@ int HWC_Accum_Add_Here (unsigned int tid, long long *store_buffer) int i; if (HWCEnabled) { - pthread_rwlock_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); for (i=0; i +#include "pthread_redirect.h" #if defined(IS_BGL_MACHINE) # define COUNTERS_INFO @@ -110,17 +111,18 @@ int HWCBE_PAPI_Allocate_eventsets_per_thread (int num_set, int old_thread_num, i { int i; - pthread_rwlock_wrlock(&pThread_mtx_HWC_sets); + //mtx_rw_wrlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); HWC_sets[num_set].eventsets = (int *) realloc (HWC_sets[num_set].eventsets, sizeof(int)*new_thread_num); if (HWC_sets[num_set].eventsets == NULL) { fprintf (stderr, PACKAGE_NAME": Cannot allocate memory for HWC_set\n"); + // mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return FALSE; } for (i = old_thread_num; i < new_thread_num; i++) HWC_sets[num_set].eventsets[i] = PAPI_NULL; - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + //mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return TRUE; } @@ -212,11 +214,12 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count ncounters = MAX_HWC; } - pthread_rwlock_wrlock(&pThread_mtx_HWC_sets); + mtx_rw_wrlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); HWC_sets = (struct HWC_Set_t *) realloc (HWC_sets, sizeof(struct HWC_Set_t)* (HWC_num_sets+1)); if (HWC_sets == NULL) { fprintf (stderr, PACKAGE_NAME": Cannot allocate memory for HWC_set (rank %d)\n", rank); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } @@ -286,6 +289,7 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count { if (rank == 0) fprintf (stderr, PACKAGE_NAME": Set %d of counters seems to be empty/invalid, skipping\n", pretended_set); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } @@ -351,7 +355,6 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count pretended_set); HWC_sets[num_set].domain = PAPI_DOM_USER; } - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); HWCBE_PAPI_Allocate_eventsets_per_thread (num_set, 0, Backend_getNumberOfThreads()); @@ -388,8 +391,10 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count Add_Overflows_To_Set (rank, num_set, pretended_set, num_overflows, overflow_counters, overflow_values); #endif + int ret = HWC_sets[num_set].num_counters; + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); - return HWC_sets[num_set].num_counters; + return ret; } #if defined(PAPI_SAMPLING_SUPPORT) @@ -410,6 +415,7 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread #endif int rc; + // pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); /* The given set is a valid one? */ if (numset < 0 || numset >= HWC_num_sets) return FALSE; @@ -469,6 +475,7 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread fprintf (stderr, PACKAGE_NAME": errno = %d\n", errno); } } + // pthread_rwlock_unlock(&pThread_mtx_HWC_sets); return rc == PAPI_OK; } @@ -506,7 +513,7 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) int state; int i; unsigned t; - + mtx_rw_wrlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); if (PAPI_state (HWCEVTSET(THREADID), &state) == PAPI_OK) { if (state & PAPI_RUNNING) @@ -538,8 +545,8 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) } } #endif - xfree (HWC_sets); - + xfree (HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); PAPI_shutdown(); } } @@ -607,6 +614,7 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) if (HWC_num_sets <= 0) return FALSE; + //pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); if (forked) { PAPI_stop (HWCEVTSET(threadid), NULL); @@ -666,9 +674,10 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) #if defined(ENABLE_PEBS_SAMPLING) Extrae_IntelPEBS_startSampling(); -#endif - - return HWC_Thread_Initialized[threadid]; +#endif + int ret = HWC_Thread_Initialized[threadid]; + //pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + return ret; } #if defined(IS_BG_MACHINE) @@ -676,7 +685,7 @@ int __in_PAPI_read_BG = FALSE; #endif int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { - pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); + mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); int EventSet = HWCEVTSET(tid); #if !defined(IS_BG_MACHINE) @@ -684,10 +693,10 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 1; #else if (!__in_PAPI_read_BG) @@ -697,15 +706,15 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } __in_PAPI_read_BG = FALSE; - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 1; } else { - pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } #endif @@ -713,23 +722,29 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) int HWCBE_PAPI_Reset (unsigned int tid) { + mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); if (PAPI_reset(HWCEVTSET(tid)) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_reset failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 0; } + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 1; } int HWCBE_PAPI_Accum (unsigned int tid, long long *store_buffer) { + mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); if (PAPI_accum(HWCEVTSET(tid), store_buffer) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_accum failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); - return 0; + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + return 0; } + mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); return 1; } diff --git a/src/tracer/mode.c b/src/tracer/mode.c index 0bdb17ab..0375ea10 100644 --- a/src/tracer/mode.c +++ b/src/tracer/mode.c @@ -46,7 +46,7 @@ int Starting_Trace_Mode = TRACE_MODE_DETAIL; unsigned long long BurstsMode_Threshold = 10000000; /* 10ms */ int BurstsMode_MPI_Stats = ENABLED; -static pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize = PTHREAD_RWLOCK_INITIALIZER; +pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize = PTHREAD_RWLOCK_INITIALIZER; static int is_ValidMode (int mode) { @@ -62,11 +62,13 @@ static int is_ValidMode (int mode) void Trace_Mode_CleanUp (void) { + pthread_rwlock_wrlock(&pThread_mtx_Trace_Mode_reInitialize); xfree (MPI_Deepness); xfree (Current_Trace_Mode); xfree (Future_Trace_Mode); xfree (Pending_Trace_Mode_Change); xfree (First_Trace_Mode); + pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); } int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) @@ -126,7 +128,11 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) int Trace_Mode_FirstMode (unsigned thread) { - return First_Trace_Mode[thread]; + int ret; + pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + ret = First_Trace_Mode[thread]; + pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + return ret; } int Trace_Mode_Initialize (int num_threads) diff --git a/src/tracer/pthread_redirect.c b/src/tracer/pthread_redirect.c new file mode 100644 index 00000000..af9bbc01 --- /dev/null +++ b/src/tracer/pthread_redirect.c @@ -0,0 +1,190 @@ +/*****************************************************************************\ + * ANALYSIS PERFORMANCE TOOLS * + * Extrae * + * Instrumentation package for parallel applications * + ***************************************************************************** + * ___ This library is free software; you can redistribute it and/or * + * / __ modify it under the terms of the GNU LGPL as published * + * / / _____ by the Free Software Foundation; either version 2.1 * + * / / / \ of the License, or (at your option) any later version. * + * ( ( ( B S C ) * + * \ \ \_____/ This library is distributed in 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 LGPL for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this library; if not, write to the Free Software Foundation, * + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + * The GNU LEsser General Public License is contained in the file COPYING. * + * --------- * + * Barcelona Supercomputing Center - Centro Nacional de Supercomputacion * +\*****************************************************************************/ + +#include "pthread_redirect.h" + +int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*) = NULL; +int (*pthread_join_real)(pthread_t,void**) = NULL; +int (*pthread_detach_real)(pthread_t) = NULL; +void (*pthread_exit_real)(void*) = NULL; +int (*pthread_barrier_wait_real)(pthread_barrier_t *barrier) = NULL; + +int (*pthread_mutex_lock_real)(pthread_mutex_t*) = NULL; +int (*pthread_mutex_trylock_real)(pthread_mutex_t*) = NULL; +int (*pthread_mutex_timedlock_real)(pthread_mutex_t*,const struct timespec *) = NULL; +int (*pthread_mutex_unlock_real)(pthread_mutex_t*) = NULL; + +int (*pthread_cond_broadcast_real)(pthread_cond_t*) = NULL; +int (*pthread_cond_timedwait_real)(pthread_cond_t*,pthread_mutex_t*,const struct timespec *) = NULL; +int (*pthread_cond_signal_real)(pthread_cond_t*) = NULL; +int (*pthread_cond_wait_real)(pthread_cond_t*,pthread_mutex_t*) = NULL; + +int (*pthread_rwlock_rdlock_real)(pthread_rwlock_t *) = NULL; +int (*pthread_rwlock_tryrdlock_real)(pthread_rwlock_t *) = NULL; +int (*pthread_rwlock_timedrdlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; +int (*pthread_rwlock_wrlock_real)(pthread_rwlock_t *) = NULL; +int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *) = NULL; +int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; +int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; + +void GetpthreadHookPoints (int rank) +{ +// #if defined(PIC) + + /* Obtain @ for pthread_create */ + pthread_create_real = + (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) + dlsym (RTLD_NEXT, "pthread_create"); + if (pthread_create_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_create in DSOs!!\n"); + + /* Obtain @ for pthread_join */ + pthread_join_real = + (int(*)(pthread_t,void**)) dlsym (RTLD_NEXT, "pthread_join"); + if (pthread_join_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_join in DSOs!!\n"); + + /* Obtain @ for pthread_barrier_wait */ + pthread_barrier_wait_real = + (int(*)(pthread_barrier_t *)) dlsym (RTLD_NEXT, "pthread_barrier_wait"); + if (pthread_barrier_wait_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_barrier_wait in DSOs!!\n"); + + /* Obtain @ for pthread_detach */ + pthread_detach_real = (int(*)(pthread_t)) dlsym (RTLD_NEXT, "pthread_detach"); + if (pthread_detach_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_detach in DSOs!!\n"); + + /* Obtain @ for pthread_exit */ + pthread_exit_real = (void(*)(void*)) dlsym (RTLD_NEXT, "pthread_exit"); + if (pthread_exit_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_exit in DSOs!!\n"); + + /* Obtain @ for pthread_mutex_lock */ + pthread_mutex_lock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_lock"); + if (pthread_mutex_lock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_lock in DSOs!!\n"); + + /* Obtain @ for pthread_mutex_unlock */ + pthread_mutex_unlock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_unlock"); + if (pthread_mutex_unlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_unlock in DSOs!!\n"); + + /* Obtain @ for pthread_mutex_trylock */ + pthread_mutex_trylock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_trylock"); + if (pthread_mutex_trylock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_trylock in DSOs!!\n"); + + /* Obtain @ for pthread_mutex_timedlock */ + pthread_mutex_timedlock_real = (int(*)(pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_mutex_timedlock"); + if (pthread_mutex_timedlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_mutex_timedlock in DSOs!!\n"); + + /* Obtain @ for pthread_cond_signal */ + pthread_cond_signal_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_signal"); + if (pthread_cond_signal_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_signal in DSOs!!\n"); + + /* Obtain @ for pthread_cond_broadcast */ + pthread_cond_broadcast_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_broadcast"); + if (pthread_cond_broadcast_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_broadcast in DSOs!!\n"); + + /* Obtain @ for pthread_cond_wait */ + pthread_cond_wait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_cond_wait"); + if (pthread_cond_wait_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_wait in DSOs!!\n"); + + /* Obtain @ for pthread_cond_timedwait */ + pthread_cond_timedwait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_cond_timedwait"); + if (pthread_cond_timedwait_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_timedwait in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_rdlock */ + pthread_rwlock_rdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_rdlock"); + if (pthread_rwlock_rdlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_rdlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_tryrdlock */ + pthread_rwlock_tryrdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_tryrdlock"); + if (pthread_rwlock_tryrdlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_timedrdlock */ + pthread_rwlock_timedrdlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedrdlock"); + if (pthread_rwlock_timedrdlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_rwlock */ + pthread_rwlock_wrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_wrlock"); + if (pthread_rwlock_wrlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_wrlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_tryrwlock */ + pthread_rwlock_trywrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_trywrlock"); + if (pthread_rwlock_trywrlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_timedrwlock */ + pthread_rwlock_timedwrlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedwrlock"); + if (pthread_rwlock_timedwrlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); + + /* Obtain @ for pthread_rwlock_unlock */ + pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); + if (pthread_rwlock_unlock_real == NULL && rank == 0) + fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_unlock in DSOs!!\n"); +// #else + // fprintf (stderr, PACKAGE_NAME": Warning! pthread instrumentation requires linking with shared library!\n"); +// #endif /* PIC */ +} + +void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { + pthread_rwlock_wrlock_real(lock); + + char cur_host_name[100]; + cur_host_name[99] = '\0'; + gethostname(cur_host_name, 99); + + //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_wrlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); +} + +void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { + pthread_rwlock_rdlock_real(lock); + + char cur_host_name[100]; + cur_host_name[99] = '\0'; + gethostname(cur_host_name, 99); + + //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_rdlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); +} + +void mtx_rw_unlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { + pthread_rwlock_unlock_real(lock); + + char cur_host_name[100]; + cur_host_name[99] = '\0'; + gethostname(cur_host_name, 99); + + //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_unlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); +} \ No newline at end of file diff --git a/src/tracer/pthread_redirect.h b/src/tracer/pthread_redirect.h new file mode 100644 index 00000000..bf53cf17 --- /dev/null +++ b/src/tracer/pthread_redirect.h @@ -0,0 +1,59 @@ +#ifndef __PTHREAD_REDIRECT_H__ +#define __PTHREAD_REDIRECT_H__ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include + +extern int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*); +extern int (*pthread_join_real)(pthread_t,void**); +extern int (*pthread_detach_real)(pthread_t); +extern void (*pthread_exit_real)(void*); +extern int (*pthread_barrier_wait_real)(pthread_barrier_t *barrier); + +extern int (*pthread_mutex_lock_real)(pthread_mutex_t*); +extern int (*pthread_mutex_trylock_real)(pthread_mutex_t*); +extern int (*pthread_mutex_timedlock_real)(pthread_mutex_t*,const struct timespec *); +extern int (*pthread_mutex_unlock_real)(pthread_mutex_t*); + +extern int (*pthread_cond_broadcast_real)(pthread_cond_t*); +extern int (*pthread_cond_timedwait_real)(pthread_cond_t*,pthread_mutex_t*,const struct timespec *); +extern int (*pthread_cond_signal_real)(pthread_cond_t*); +extern int (*pthread_cond_wait_real)(pthread_cond_t*,pthread_mutex_t*); + +extern int (*pthread_rwlock_rdlock_real)(pthread_rwlock_t *); +extern int (*pthread_rwlock_tryrdlock_real)(pthread_rwlock_t *); +extern int (*pthread_rwlock_timedrdlock_real)(pthread_rwlock_t *, const struct timespec *); +extern int (*pthread_rwlock_wrlock_real)(pthread_rwlock_t *); +extern int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *); +extern int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *); +extern int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *); + +int GetpThreadIdentifier (void); + +// ============================= Locking helper =================================== + +void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); + +void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); + +void mtx_rw_unlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); + +#ifndef mtx_rw_wrlock +#define mtx_rw_wrlock(mtx, name) mtx_rw_wrlock_caller(mtx, name, __func__) +#endif + +#ifndef mtx_rw_rdlock +#define mtx_rw_rdlock(mtx, name) mtx_rw_rdlock_caller(mtx, name, __func__) +#endif + +#ifndef mtx_rw_unlock +#define mtx_rw_unlock(mtx, name) mtx_rw_unlock_caller(mtx, name, __func__) +#endif + +#endif \ No newline at end of file diff --git a/src/tracer/signals.c b/src/tracer/signals.c index 60cfd2c1..e6be819d 100644 --- a/src/tracer/signals.c +++ b/src/tracer/signals.c @@ -42,6 +42,7 @@ # include # endif #endif +#include /* #define DBG_SIGNALS */ @@ -52,19 +53,29 @@ volatile int sigInhibited = FALSE; +static pthread_mutex_t pThread_mtx_sigInhibited = PTHREAD_MUTEX_INITIALIZER; + void Signals_Inhibit() { + pthread_mutex_lock(&pThread_mtx_sigInhibited); sigInhibited = TRUE; + pthread_mutex_unlock(&pThread_mtx_sigInhibited); } void Signals_Desinhibit() { + pthread_mutex_lock(&pThread_mtx_sigInhibited); sigInhibited = FALSE; + pthread_mutex_unlock(&pThread_mtx_sigInhibited); } int Signals_Inhibited() { - return sigInhibited; + int ret; + pthread_mutex_lock(&pThread_mtx_sigInhibited); + ret = sigInhibited; + pthread_mutex_unlock(&pThread_mtx_sigInhibited); + return ret; } int Deferred_Signal_FlushAndTerminate = FALSE; diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index c4d93240..559f9d60 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -1983,10 +1983,10 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) /* Just modify things if there are more threads */ if (new_num_threads > get_maximum_NumOfThreads()) { - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - fprintf(stderr, "%s OS_TID:%ld Start Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); + // char cur_host_name[100]; + // cur_host_name[99] = '\0'; + // gethostname(cur_host_name, 99); + // fprintf(stderr, "%s OS_TID:%ld Start Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); unsigned u; #if defined(ENABLE_PEBS_SAMPLING) @@ -2034,7 +2034,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) #if defined(ENABLE_PEBS_SAMPLING) Extrae_IntelPEBS_resumeSampling(); #endif - fprintf(stderr, "%s OS_TID:%ld End Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); + // fprintf(stderr, "%s OS_TID:%ld End Backend_ChangeNumberOfThreads: %d\n", cur_host_name, syscall(SYS_gettid), new_num_threads); } else current_NumOfThreads = new_num_threads; diff --git a/src/tracer/wrappers/pthread/Makefile.am b/src/tracer/wrappers/pthread/Makefile.am index 16f9cd29..558ceebb 100644 --- a/src/tracer/wrappers/pthread/Makefile.am +++ b/src/tracer/wrappers/pthread/Makefile.am @@ -3,7 +3,8 @@ include $(top_srcdir)/PATHS # Wrappers for pthread instrumentation WRAPPERS_PTHREAD = \ pthread_wrapper.c pthread_wrapper.h \ - pthread_probe.c pthread_probe.h + pthread_probe.c pthread_probe.h \ + pthread_redirect.c pthread_redirect.h noinst_LTLIBRARIES = libwrap_pthread.la diff --git a/src/tracer/wrappers/pthread/pthread_wrapper.c b/src/tracer/wrappers/pthread/pthread_wrapper.c index 55afcfd2..f80d93d7 100644 --- a/src/tracer/wrappers/pthread/pthread_wrapper.c +++ b/src/tracer/wrappers/pthread/pthread_wrapper.c @@ -42,6 +42,7 @@ #endif #include "wrapper.h" +#include "pthread_redirect.h" #include "trace_macros.h" #include "pthread_probe.h" @@ -50,145 +51,145 @@ #if defined(PIC) -static int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*) = NULL; -static int (*pthread_join_real)(pthread_t,void**) = NULL; -static int (*pthread_detach_real)(pthread_t) = NULL; -static void (*pthread_exit_real)(void*) = NULL; -static int (*pthread_barrier_wait_real)(pthread_barrier_t *barrier) = NULL; - -static int (*pthread_mutex_lock_real)(pthread_mutex_t*) = NULL; -static int (*pthread_mutex_trylock_real)(pthread_mutex_t*) = NULL; -static int (*pthread_mutex_timedlock_real)(pthread_mutex_t*,const struct timespec *) = NULL; -static int (*pthread_mutex_unlock_real)(pthread_mutex_t*) = NULL; - -static int (*pthread_cond_broadcast_real)(pthread_cond_t*) = NULL; -static int (*pthread_cond_timedwait_real)(pthread_cond_t*,pthread_mutex_t*,const struct timespec *) = NULL; -static int (*pthread_cond_signal_real)(pthread_cond_t*) = NULL; -static int (*pthread_cond_wait_real)(pthread_cond_t*,pthread_mutex_t*) = NULL; - -static int (*pthread_rwlock_rdlock_real)(pthread_rwlock_t *) = NULL; -static int (*pthread_rwlock_tryrdlock_real)(pthread_rwlock_t *) = NULL; -static int (*pthread_rwlock_timedrdlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; -static int (*pthread_rwlock_wrlock_real)(pthread_rwlock_t *) = NULL; -static int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *) = NULL; -static int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; -static int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; +// static int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*) = NULL; +// static int (*pthread_join_real)(pthread_t,void**) = NULL; +// static int (*pthread_detach_real)(pthread_t) = NULL; +// static void (*pthread_exit_real)(void*) = NULL; +// static int (*pthread_barrier_wait_real)(pthread_barrier_t *barrier) = NULL; + +// static int (*pthread_mutex_lock_real)(pthread_mutex_t*) = NULL; +// static int (*pthread_mutex_trylock_real)(pthread_mutex_t*) = NULL; +// static int (*pthread_mutex_timedlock_real)(pthread_mutex_t*,const struct timespec *) = NULL; +// static int (*pthread_mutex_unlock_real)(pthread_mutex_t*) = NULL; + +// static int (*pthread_cond_broadcast_real)(pthread_cond_t*) = NULL; +// static int (*pthread_cond_timedwait_real)(pthread_cond_t*,pthread_mutex_t*,const struct timespec *) = NULL; +// static int (*pthread_cond_signal_real)(pthread_cond_t*) = NULL; +// static int (*pthread_cond_wait_real)(pthread_cond_t*,pthread_mutex_t*) = NULL; + +// static int (*pthread_rwlock_rdlock_real)(pthread_rwlock_t *) = NULL; +// static int (*pthread_rwlock_tryrdlock_real)(pthread_rwlock_t *) = NULL; +// static int (*pthread_rwlock_timedrdlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; +// static int (*pthread_rwlock_wrlock_real)(pthread_rwlock_t *) = NULL; +// static int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *) = NULL; +// static int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; +// static int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; static pthread_mutex_t extrae_pthread_create_mutex = PTHREAD_MUTEX_INITIALIZER; #endif /* PIC */ -static void GetpthreadHookPoints (int rank) -{ -#if defined(PIC) - - /* Obtain @ for pthread_create */ - pthread_create_real = - (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) - dlsym (RTLD_NEXT, "pthread_create"); - if (pthread_create_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_create in DSOs!!\n"); - - /* Obtain @ for pthread_join */ - pthread_join_real = - (int(*)(pthread_t,void**)) dlsym (RTLD_NEXT, "pthread_join"); - if (pthread_join_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_join in DSOs!!\n"); - - /* Obtain @ for pthread_barrier_wait */ - pthread_barrier_wait_real = - (int(*)(pthread_barrier_t *)) dlsym (RTLD_NEXT, "pthread_barrier_wait"); - if (pthread_barrier_wait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_barrier_wait in DSOs!!\n"); - - /* Obtain @ for pthread_detach */ - pthread_detach_real = (int(*)(pthread_t)) dlsym (RTLD_NEXT, "pthread_detach"); - if (pthread_detach_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_detach in DSOs!!\n"); - - /* Obtain @ for pthread_exit */ - pthread_exit_real = (void(*)(void*)) dlsym (RTLD_NEXT, "pthread_exit"); - if (pthread_exit_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_exit in DSOs!!\n"); - - /* Obtain @ for pthread_mutex_lock */ - pthread_mutex_lock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_lock"); - if (pthread_mutex_lock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_lock in DSOs!!\n"); +// static void GetpthreadHookPoints (int rank) +// { +// #if defined(PIC) + +// /* Obtain @ for pthread_create */ +// pthread_create_real = +// (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) +// dlsym (RTLD_NEXT, "pthread_create"); +// if (pthread_create_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_create in DSOs!!\n"); + +// /* Obtain @ for pthread_join */ +// pthread_join_real = +// (int(*)(pthread_t,void**)) dlsym (RTLD_NEXT, "pthread_join"); +// if (pthread_join_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_join in DSOs!!\n"); + +// /* Obtain @ for pthread_barrier_wait */ +// pthread_barrier_wait_real = +// (int(*)(pthread_barrier_t *)) dlsym (RTLD_NEXT, "pthread_barrier_wait"); +// if (pthread_barrier_wait_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_barrier_wait in DSOs!!\n"); + +// /* Obtain @ for pthread_detach */ +// pthread_detach_real = (int(*)(pthread_t)) dlsym (RTLD_NEXT, "pthread_detach"); +// if (pthread_detach_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_detach in DSOs!!\n"); + +// /* Obtain @ for pthread_exit */ +// pthread_exit_real = (void(*)(void*)) dlsym (RTLD_NEXT, "pthread_exit"); +// if (pthread_exit_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_exit in DSOs!!\n"); + +// /* Obtain @ for pthread_mutex_lock */ +// pthread_mutex_lock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_lock"); +// if (pthread_mutex_lock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_lock in DSOs!!\n"); - /* Obtain @ for pthread_mutex_unlock */ - pthread_mutex_unlock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_unlock"); - if (pthread_mutex_unlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_unlock in DSOs!!\n"); +// /* Obtain @ for pthread_mutex_unlock */ +// pthread_mutex_unlock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_unlock"); +// if (pthread_mutex_unlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_unlock in DSOs!!\n"); - /* Obtain @ for pthread_mutex_trylock */ - pthread_mutex_trylock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_trylock"); - if (pthread_mutex_trylock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_trylock in DSOs!!\n"); - - /* Obtain @ for pthread_mutex_timedlock */ - pthread_mutex_timedlock_real = (int(*)(pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_mutex_timedlock"); - if (pthread_mutex_timedlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_mutex_timedlock in DSOs!!\n"); - - /* Obtain @ for pthread_cond_signal */ - pthread_cond_signal_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_signal"); - if (pthread_cond_signal_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_signal in DSOs!!\n"); +// /* Obtain @ for pthread_mutex_trylock */ +// pthread_mutex_trylock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_trylock"); +// if (pthread_mutex_trylock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_trylock in DSOs!!\n"); + +// /* Obtain @ for pthread_mutex_timedlock */ +// pthread_mutex_timedlock_real = (int(*)(pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_mutex_timedlock"); +// if (pthread_mutex_timedlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_mutex_timedlock in DSOs!!\n"); + +// /* Obtain @ for pthread_cond_signal */ +// pthread_cond_signal_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_signal"); +// if (pthread_cond_signal_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_signal in DSOs!!\n"); - /* Obtain @ for pthread_cond_broadcast */ - pthread_cond_broadcast_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_broadcast"); - if (pthread_cond_broadcast_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_broadcast in DSOs!!\n"); +// /* Obtain @ for pthread_cond_broadcast */ +// pthread_cond_broadcast_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_broadcast"); +// if (pthread_cond_broadcast_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_broadcast in DSOs!!\n"); - /* Obtain @ for pthread_cond_wait */ - pthread_cond_wait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_cond_wait"); - if (pthread_cond_wait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_wait in DSOs!!\n"); +// /* Obtain @ for pthread_cond_wait */ +// pthread_cond_wait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_cond_wait"); +// if (pthread_cond_wait_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_wait in DSOs!!\n"); - /* Obtain @ for pthread_cond_timedwait */ - pthread_cond_timedwait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_cond_timedwait"); - if (pthread_cond_timedwait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_timedwait in DSOs!!\n"); +// /* Obtain @ for pthread_cond_timedwait */ +// pthread_cond_timedwait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_cond_timedwait"); +// if (pthread_cond_timedwait_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_timedwait in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_rdlock */ - pthread_rwlock_rdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_rdlock"); - if (pthread_rwlock_rdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_rdlock in DSOs!!\n"); +// /* Obtain @ for pthread_rwlock_rdlock */ +// pthread_rwlock_rdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_rdlock"); +// if (pthread_rwlock_rdlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_rdlock in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_tryrdlock */ - pthread_rwlock_tryrdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_tryrdlock"); - if (pthread_rwlock_tryrdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); +// /* Obtain @ for pthread_rwlock_tryrdlock */ +// pthread_rwlock_tryrdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_tryrdlock"); +// if (pthread_rwlock_tryrdlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_timedrdlock */ - pthread_rwlock_timedrdlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedrdlock"); - if (pthread_rwlock_timedrdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); +// /* Obtain @ for pthread_rwlock_timedrdlock */ +// pthread_rwlock_timedrdlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedrdlock"); +// if (pthread_rwlock_timedrdlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_rwlock */ - pthread_rwlock_wrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_wrlock"); - if (pthread_rwlock_wrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_wrlock in DSOs!!\n"); +// /* Obtain @ for pthread_rwlock_rwlock */ +// pthread_rwlock_wrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_wrlock"); +// if (pthread_rwlock_wrlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_wrlock in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_tryrwlock */ - pthread_rwlock_trywrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_trywrlock"); - if (pthread_rwlock_trywrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); +// /* Obtain @ for pthread_rwlock_tryrwlock */ +// pthread_rwlock_trywrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_trywrlock"); +// if (pthread_rwlock_trywrlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); - /* Obtain @ for pthread_rwlock_timedrwlock */ - pthread_rwlock_timedwrlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedwrlock"); - if (pthread_rwlock_timedwrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); - - /* Obtain @ for pthread_rwlock_unlock */ - pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); - if (pthread_rwlock_unlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_unlock in DSOs!!\n"); -#else - fprintf (stderr, PACKAGE_NAME": Warning! pthread instrumentation requires linking with shared library!\n"); -#endif /* PIC */ -} +// /* Obtain @ for pthread_rwlock_timedrwlock */ +// pthread_rwlock_timedwrlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedwrlock"); +// if (pthread_rwlock_timedwrlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); + +// /* Obtain @ for pthread_rwlock_unlock */ +// pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); +// if (pthread_rwlock_unlock_real == NULL && rank == 0) +// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_unlock in DSOs!!\n"); +// #else +// fprintf (stderr, PACKAGE_NAME": Warning! pthread instrumentation requires linking with shared library!\n"); +// #endif /* PIC */ +// } /* @@ -318,7 +319,7 @@ int pthread_create (pthread_t* p1, const pthread_attr_t* p2, res = pthread_create_real (p1, p2, p3, p4); /* Stop protecting the region, more pthread creations can enter */ - pthread_mutex_unlock_real (&extrae_pthread_create_mutex); + pthread_mutex_unlock_real(&extrae_pthread_create_mutex); } else if (pthread_create_real != NULL) { From e518cf77e27dd4721e7e73cd15fa452514a3e651 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 13:41:34 +0100 Subject: [PATCH 3/9] experiment with directly calling locking without rerunning through instrumentation --- src/tracer/hwc/papi_hwc.c | 8 +- src/tracer/mode.c | 33 ++-- src/tracer/mode.h | 6 +- src/tracer/pthread_redirect.c | 42 ++--- src/tracer/pthread_redirect.h | 22 +++ src/tracer/wrappers/API/wrapper.c | 64 +++++--- src/tracer/wrappers/MPI/hash_table.c | 20 ++- src/tracer/wrappers/OMP/ompt-helper.c | 37 +++-- src/tracer/wrappers/pthread/pthread_wrapper.c | 144 +----------------- 9 files changed, 158 insertions(+), 218 deletions(-) diff --git a/src/tracer/hwc/papi_hwc.c b/src/tracer/hwc/papi_hwc.c index 2d429d80..63d9fe5b 100644 --- a/src/tracer/hwc/papi_hwc.c +++ b/src/tracer/hwc/papi_hwc.c @@ -415,7 +415,7 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread #endif int rc; - // pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); + // pthread_rwlock_rdlock_real(&pThread_mtx_HWC_sets); /* The given set is a valid one? */ if (numset < 0 || numset >= HWC_num_sets) return FALSE; @@ -475,7 +475,7 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread fprintf (stderr, PACKAGE_NAME": errno = %d\n", errno); } } - // pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + // pthread_rwlock_unlock_real(&pThread_mtx_HWC_sets); return rc == PAPI_OK; } @@ -614,7 +614,7 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) if (HWC_num_sets <= 0) return FALSE; - //pthread_rwlock_rdlock(&pThread_mtx_HWC_sets); + //pthread_rwlock_rdlock_real(&pThread_mtx_HWC_sets); if (forked) { PAPI_stop (HWCEVTSET(threadid), NULL); @@ -676,7 +676,7 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) Extrae_IntelPEBS_startSampling(); #endif int ret = HWC_Thread_Initialized[threadid]; - //pthread_rwlock_unlock(&pThread_mtx_HWC_sets); + //pthread_rwlock_unlock_real(&pThread_mtx_HWC_sets); return ret; } diff --git a/src/tracer/mode.c b/src/tracer/mode.c index 0375ea10..570244bd 100644 --- a/src/tracer/mode.c +++ b/src/tracer/mode.c @@ -31,7 +31,6 @@ #include "hwc.h" #include "mode.h" #include "utils.h" -#include int *MPI_Deepness = NULL; int *Current_Trace_Mode = NULL; @@ -62,13 +61,13 @@ static int is_ValidMode (int mode) void Trace_Mode_CleanUp (void) { - pthread_rwlock_wrlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_wrlock_real(&pThread_mtx_Trace_Mode_reInitialize); xfree (MPI_Deepness); xfree (Current_Trace_Mode); xfree (Future_Trace_Mode); xfree (Pending_Trace_Mode_Change); xfree (First_Trace_Mode); - pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); } int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) @@ -77,7 +76,7 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) size = sizeof(int) * new_num_threads; - pthread_rwlock_wrlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_wrlock_real(&pThread_mtx_Trace_Mode_reInitialize); MPI_Deepness = (int *)realloc(MPI_Deepness, size); if (MPI_Deepness == NULL) { @@ -121,22 +120,28 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) Pending_Trace_Mode_Change[i] = FALSE; First_Trace_Mode[i] = TRUE; } - pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); return TRUE; } int Trace_Mode_FirstMode (unsigned thread) { + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + int ret; - pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); ret = First_Trace_Mode[thread]; - pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); return ret; } int Trace_Mode_Initialize (int num_threads) { + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + int res = Trace_Mode_reInitialize (0, num_threads); /* Show configuration */ @@ -164,7 +169,10 @@ int Trace_Mode_Initialize (int num_threads) void Trace_Mode_Change (int tid, iotimer_t time) { - pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); if (Pending_Trace_Mode_Change[tid] || First_Trace_Mode[tid]) { if (Future_Trace_Mode[tid] != Current_Trace_Mode[tid] || First_Trace_Mode[tid]) @@ -185,7 +193,7 @@ void Trace_Mode_Change (int tid, iotimer_t time) Pending_Trace_Mode_Change[tid] = FALSE; First_Trace_Mode[tid] = FALSE; } - pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); } void @@ -193,19 +201,22 @@ Trace_mode_switch(void) { unsigned i; + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + /* * XXX Should this be Backend_getMaximumOfThreads()? If we decrease the * number of threads, switch tracing mode and then increase again the the * number of threads, only the "old" threads will use burst mode, while the * "new" ones will continue in detail. */ - pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); for (i=0; i +#include "pthread_redirect.h" extern int *MPI_Deepness; extern int *Current_Trace_Mode; @@ -37,9 +37,9 @@ extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; #define CURRENT_TRACE_MODE(tid) Current_Trace_Mode[tid]\ // { \ // int ret; \ -// pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); \ +// pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); \ // ret = Current_Trace_Mode[tid]; \ -// pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); \ +// pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); \ // return ret; \ // } diff --git a/src/tracer/pthread_redirect.c b/src/tracer/pthread_redirect.c index af9bbc01..ee784ba0 100644 --- a/src/tracer/pthread_redirect.c +++ b/src/tracer/pthread_redirect.c @@ -56,106 +56,106 @@ void GetpthreadHookPoints (int rank) (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) dlsym (RTLD_NEXT, "pthread_create"); if (pthread_create_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_create in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_create in DSOs!!\n"); /* Obtain @ for pthread_join */ pthread_join_real = (int(*)(pthread_t,void**)) dlsym (RTLD_NEXT, "pthread_join"); if (pthread_join_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_join in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_join in DSOs!!\n"); /* Obtain @ for pthread_barrier_wait */ pthread_barrier_wait_real = (int(*)(pthread_barrier_t *)) dlsym (RTLD_NEXT, "pthread_barrier_wait"); if (pthread_barrier_wait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_barrier_wait in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_barrier_wait in DSOs!!\n"); /* Obtain @ for pthread_detach */ pthread_detach_real = (int(*)(pthread_t)) dlsym (RTLD_NEXT, "pthread_detach"); if (pthread_detach_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_detach in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_detach in DSOs!!\n"); /* Obtain @ for pthread_exit */ pthread_exit_real = (void(*)(void*)) dlsym (RTLD_NEXT, "pthread_exit"); if (pthread_exit_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_exit in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_exit in DSOs!!\n"); /* Obtain @ for pthread_mutex_lock */ pthread_mutex_lock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_lock"); if (pthread_mutex_lock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_lock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_lock in DSOs!!\n"); /* Obtain @ for pthread_mutex_unlock */ pthread_mutex_unlock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_unlock"); if (pthread_mutex_unlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_unlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_unlock in DSOs!!\n"); /* Obtain @ for pthread_mutex_trylock */ pthread_mutex_trylock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_trylock"); if (pthread_mutex_trylock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_trylock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_trylock in DSOs!!\n"); /* Obtain @ for pthread_mutex_timedlock */ pthread_mutex_timedlock_real = (int(*)(pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_mutex_timedlock"); if (pthread_mutex_timedlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_mutex_timedlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_mutex_timedlock in DSOs!!\n"); /* Obtain @ for pthread_cond_signal */ pthread_cond_signal_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_signal"); if (pthread_cond_signal_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_signal in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_cond_signal in DSOs!!\n"); /* Obtain @ for pthread_cond_broadcast */ pthread_cond_broadcast_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_broadcast"); if (pthread_cond_broadcast_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_broadcast in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_cond_broadcast in DSOs!!\n"); /* Obtain @ for pthread_cond_wait */ pthread_cond_wait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_cond_wait"); if (pthread_cond_wait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_wait in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_cond_wait in DSOs!!\n"); /* Obtain @ for pthread_cond_timedwait */ pthread_cond_timedwait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_cond_timedwait"); if (pthread_cond_timedwait_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_timedwait in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_cond_timedwait in DSOs!!\n"); /* Obtain @ for pthread_rwlock_rdlock */ pthread_rwlock_rdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_rdlock"); if (pthread_rwlock_rdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_rdlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_rdlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_tryrdlock */ pthread_rwlock_tryrdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_tryrdlock"); if (pthread_rwlock_tryrdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_timedrdlock */ pthread_rwlock_timedrdlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedrdlock"); if (pthread_rwlock_timedrdlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_rwlock */ pthread_rwlock_wrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_wrlock"); if (pthread_rwlock_wrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_wrlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_wrlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_tryrwlock */ pthread_rwlock_trywrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_trywrlock"); if (pthread_rwlock_trywrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_timedrwlock */ pthread_rwlock_timedwrlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedwrlock"); if (pthread_rwlock_timedwrlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); /* Obtain @ for pthread_rwlock_unlock */ pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); if (pthread_rwlock_unlock_real == NULL && rank == 0) - fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_unlock in DSOs!!\n"); + fprintf (stderr, "Unable to find pthread_rwlock_unlock in DSOs!!\n"); // #else - // fprintf (stderr, PACKAGE_NAME": Warning! pthread instrumentation requires linking with shared library!\n"); + // fprintf (stderr, "Warning! pthread instrumentation requires linking with shared library!\n"); // #endif /* PIC */ } diff --git a/src/tracer/pthread_redirect.h b/src/tracer/pthread_redirect.h index bf53cf17..5271f659 100644 --- a/src/tracer/pthread_redirect.h +++ b/src/tracer/pthread_redirect.h @@ -1,9 +1,31 @@ #ifndef __PTHREAD_REDIRECT_H__ #define __PTHREAD_REDIRECT_H__ +#include "common.h" + #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif +#include +#include +#ifdef HAVE_PTHREAD_H +# include +#endif +#ifdef HAVE_DLFCN_H +# define __USE_GNU +# include +# undef __USE_GNU +#endif +#ifdef HAVE_STDIO_H +# include +#endif +#ifdef HAVE_STDLIB_H +# include +#endif +#ifdef HAVE_TIME_H +# include +#endif + #include #include #include diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index 559f9d60..6be353e5 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -161,6 +161,7 @@ #include "syscall_probe.h" #include "syscall_wrapper.h" #include "taskid.h" +#include "pthread_redirect.h" int Extrae_Flush_Wrapper (Buffer_t *buffer); @@ -499,14 +500,17 @@ void Extrae_AnnotateCPU (UINT64 timestamp) #if defined(HAVE_SCHED_GETCPU) int cpu = sched_getcpu(); - pthread_rwlock_rdlock(&pThread_mtx_Realloc); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&pThread_mtx_Realloc); if (cpu != LastCPUEvent[THREADID] || AlwaysEmitCPUEvent) { LastCPUEvent[THREADID] = cpu; TRACE_EVENT (timestamp, GETCPU_EV, cpu); } - pthread_rwlock_unlock(&pThread_mtx_Realloc); + pthread_rwlock_unlock_real(&pThread_mtx_Realloc); #else UNREFERENCED_PARAMETER(timestamp); #endif @@ -1418,6 +1422,9 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork { int i; + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); + #if 0 /* MRNET */ /* FIXME: Temporarily disabled. new_buffer_size overflows when target_mbs > 1000 */ if (MRNet_isEnabled()) @@ -1437,7 +1444,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork UNREFERENCED_PARAMETER(world_size); #endif - pthread_rwlock_wrlock(&pThread_mtx_Realloc); + pthread_rwlock_wrlock_real(&pThread_mtx_Realloc); if (!forked) { xmalloc(TracingBuffer, num_threads * sizeof(Buffer_t *)); @@ -1450,7 +1457,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork for (i = 0; i < num_threads; i++) Allocate_buffer_and_file (i, forked); - pthread_rwlock_unlock(&pThread_mtx_Realloc); + pthread_rwlock_unlock_real(&pThread_mtx_Realloc); return TRUE; } @@ -1462,7 +1469,10 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork static int Reallocate_buffers_and_files (int new_num_threads) { int i; - pthread_rwlock_wrlock(&pThread_mtx_Realloc); + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_wrlock_real(&pThread_mtx_Realloc); xrealloc(TracingBuffer, TracingBuffer, new_num_threads * sizeof(Buffer_t *)); xrealloc(LastCPUEmissionTime, LastCPUEmissionTime, new_num_threads * sizeof(iotimer_t)); xrealloc(LastCPUEvent, LastCPUEvent, new_num_threads * sizeof(int)); @@ -1471,7 +1481,7 @@ static int Reallocate_buffers_and_files (int new_num_threads) #endif for (i = get_maximum_NumOfThreads(); i < new_num_threads; i++) Allocate_buffer_and_file (i, FALSE); - pthread_rwlock_unlock(&pThread_mtx_Realloc); + pthread_rwlock_unlock_real(&pThread_mtx_Realloc); return TRUE; } @@ -2614,33 +2624,45 @@ static int *Extrae_inSampling = NULL; int Backend_inInstrumentation (unsigned thread) { - pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); int ret = FALSE; if ((Extrae_inInstrumentation != NULL) && (Extrae_inSampling != NULL)) ret = (Extrae_inInstrumentation[thread] || Extrae_inSampling[thread]); - pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); + pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); return ret; } void Backend_setInSampling (unsigned thread, int insampling) { - pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); if (Extrae_inSampling != NULL) Extrae_inSampling[thread] = insampling; - pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); + pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); } void Backend_setInInstrumentation (unsigned thread, int ininstrumentation) { - pthread_rwlock_rdlock(&pThread_mtx_Extrae_inInstrumentation); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); if (Extrae_inInstrumentation != NULL) Extrae_inInstrumentation[thread] = ininstrumentation; - pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); + pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); } void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) { - pthread_rwlock_wrlock(&pThread_mtx_Extrae_inInstrumentation); + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_wrlock_real(&pThread_mtx_Extrae_inInstrumentation); Extrae_inInstrumentation = (int*) realloc (Extrae_inInstrumentation, sizeof(int)*nthreads); if (Extrae_inInstrumentation == NULL) { @@ -2655,7 +2677,7 @@ void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) ": Failed to allocate memory for inSampling structure\n"); exit (-1); } - pthread_rwlock_unlock(&pThread_mtx_Extrae_inInstrumentation); + pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); } void Backend_Enter_Instrumentation () @@ -2705,11 +2727,14 @@ void Backend_Enter_Instrumentation () } #endif + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + /* Check whether we will fill the buffer soon (or now) */ - pthread_rwlock_rdlock(&pThread_mtx_Realloc); + pthread_rwlock_rdlock_real(&pThread_mtx_Realloc); if (Buffer_RemainingEvents(TracingBuffer[thread]) <= NEVENTS) Buffer_ExecuteFlushCallback (TracingBuffer[thread]); - pthread_rwlock_unlock(&pThread_mtx_Realloc); + pthread_rwlock_unlock_real(&pThread_mtx_Realloc); /* Record the time when this is happening we need this for subsequent calls to TIME, as this is being cached in clock routines */ @@ -2741,11 +2766,14 @@ void Backend_Leave_Instrumentation (void) Extrae_AnnotateCPU(LAST_READ_TIME); } + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + /* Change trace mode? (issue from API) */ - pthread_rwlock_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); if (PENDING_TRACE_MODE_CHANGE(thread) && MPI_Deepness[thread] == 0) Trace_Mode_Change(thread, LAST_READ_TIME); - pthread_rwlock_unlock(&pThread_mtx_Trace_Mode_reInitialize); + pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); Backend_setInInstrumentation (thread, FALSE); } diff --git a/src/tracer/wrappers/MPI/hash_table.c b/src/tracer/wrappers/MPI/hash_table.c index e7acd050..889b8ece 100644 --- a/src/tracer/wrappers/MPI/hash_table.c +++ b/src/tracer/wrappers/MPI/hash_table.c @@ -27,7 +27,7 @@ #include #include #include "hash_table.h" - +#include "pthread_redirect.h" /*** Prototypes ***/ static inline int xtr_hash_search (xtr_hash_t *hash, uintptr_t key, xtr_hash_cell_t **previous_out, xtr_hash_cell_t **cell_out) __attribute__((always_inline)); @@ -159,13 +159,15 @@ void xtr_hash_free(xtr_hash_t *hash) */ int xtr_hash_add (xtr_hash_t *hash, uintptr_t key, void *data) { + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); // Apply the hash function to find corresponding cell in the head array xtr_hash_cell_t *cell = XTR_HASH_GET_CELL_FOR_KEY(hash, key); xtr_hash_cell_t *cell_to_add = cell; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_wrlock(&hash->lock); + pthread_rwlock_wrlock_real(&hash->lock); } #if defined(DEBUG) @@ -199,7 +201,7 @@ int xtr_hash_add (xtr_hash_t *hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock(&hash->lock); + pthread_rwlock_unlock_real(&hash->lock); } return 1; @@ -259,11 +261,13 @@ static int xtr_hash_search (xtr_hash_t *hash, uintptr_t key, xtr_hash_cell_t **p */ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) { + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); xtr_hash_cell_t *previous = NULL, *cell = NULL; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_rdlock(&hash->lock); + pthread_rwlock_rdlock_real(&hash->lock); } #if defined(DEBUG) @@ -283,7 +287,7 @@ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock(&hash->lock); + pthread_rwlock_unlock_real(&hash->lock); } return 0; @@ -303,12 +307,14 @@ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) */ int xtr_hash_fetch (xtr_hash_t * hash, uintptr_t key, void *data) { + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); int found = 0; xtr_hash_cell_t *previous = NULL, *cell = NULL; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_wrlock(&hash->lock); + pthread_rwlock_wrlock_real(&hash->lock); } #if defined(DEBUG) @@ -341,7 +347,7 @@ int xtr_hash_fetch (xtr_hash_t * hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock(&hash->lock); + pthread_rwlock_unlock_real(&hash->lock); } return found; diff --git a/src/tracer/wrappers/OMP/ompt-helper.c b/src/tracer/wrappers/OMP/ompt-helper.c index c5b4a0bd..e994f01b 100644 --- a/src/tracer/wrappers/OMP/ompt-helper.c +++ b/src/tracer/wrappers/OMP/ompt-helper.c @@ -34,7 +34,7 @@ #endif #include "ompt-helper.h" - +#include "pthread_redirect.h" /* Relation between parallel id and parallel function */ typedef struct ompt_parallel_id_pf_st @@ -165,8 +165,11 @@ void Extrae_OMPT_register_ompt_task_id_tf (ompt_task_id_t ompt_tid, const void *tf, int implicit) { unsigned u; + + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); - pthread_rwlock_wrlock (&mutex_tid_tf); + pthread_rwlock_wrlock_real(&mutex_tid_tf); if (n_ompt_tids_tf == n_allocated_ompt_tids_tf) { #if defined(DEBUG) @@ -205,7 +208,7 @@ void Extrae_OMPT_register_ompt_task_id_tf (ompt_task_id_t ompt_tid, #endif break; } - pthread_rwlock_unlock (&mutex_tid_tf); + pthread_rwlock_unlock_real(&mutex_tid_tf); } /* Extrae_OMPT_unregister_ompt_task_id_tf @@ -217,7 +220,10 @@ void Extrae_OMPT_unregister_ompt_task_id_tf (ompt_task_id_t ompt_tid) { unsigned u; - pthread_rwlock_wrlock (&mutex_tid_tf); + if (pthread_rwlock_wrlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_wrlock_real(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) { @@ -230,7 +236,7 @@ void Extrae_OMPT_unregister_ompt_task_id_tf (ompt_task_id_t ompt_tid) #endif break; } - pthread_rwlock_unlock (&mutex_tid_tf); + pthread_rwlock_unlock_real(&mutex_tid_tf); } } @@ -242,7 +248,10 @@ const void * Extrae_OMPT_get_tf_task_id (ompt_task_id_t ompt_tid, unsigned u; const void *ptr = NULL; - pthread_rwlock_rdlock (&mutex_tid_tf); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) { @@ -253,7 +262,7 @@ const void * Extrae_OMPT_get_tf_task_id (ompt_task_id_t ompt_tid, *taskctr = ompt_tids_tf[u].task_ctr; break; } - pthread_rwlock_unlock (&mutex_tid_tf); + pthread_rwlock_unlock_real(&mutex_tid_tf); return ptr; } @@ -264,7 +273,10 @@ void Extrae_OMPT_tf_task_id_set_running (ompt_task_id_t ompt_tid, int b) { unsigned u; - pthread_rwlock_rdlock (&mutex_tid_tf); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) @@ -273,7 +285,7 @@ void Extrae_OMPT_tf_task_id_set_running (ompt_task_id_t ompt_tid, int b) break; } - pthread_rwlock_unlock (&mutex_tid_tf); + pthread_rwlock_unlock_real(&mutex_tid_tf); } @@ -285,7 +297,10 @@ int Extrae_OMPT_tf_task_id_is_running (ompt_task_id_t ompt_tid) unsigned u; int res = FALSE; - pthread_rwlock_rdlock (&mutex_tid_tf); + if (pthread_rwlock_rdlock_real == NULL) + GetpthreadHookPoints(0); + + pthread_rwlock_rdlock_real(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) @@ -294,7 +309,7 @@ int Extrae_OMPT_tf_task_id_is_running (ompt_task_id_t ompt_tid) break; } - pthread_rwlock_unlock (&mutex_tid_tf); + pthread_rwlock_unlock_real(&mutex_tid_tf); return res; } diff --git a/src/tracer/wrappers/pthread/pthread_wrapper.c b/src/tracer/wrappers/pthread/pthread_wrapper.c index f80d93d7..8704c76d 100644 --- a/src/tracer/wrappers/pthread/pthread_wrapper.c +++ b/src/tracer/wrappers/pthread/pthread_wrapper.c @@ -49,148 +49,6 @@ //#define DEBUG //#define DEBUG_MUTEX -#if defined(PIC) - -// static int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*) = NULL; -// static int (*pthread_join_real)(pthread_t,void**) = NULL; -// static int (*pthread_detach_real)(pthread_t) = NULL; -// static void (*pthread_exit_real)(void*) = NULL; -// static int (*pthread_barrier_wait_real)(pthread_barrier_t *barrier) = NULL; - -// static int (*pthread_mutex_lock_real)(pthread_mutex_t*) = NULL; -// static int (*pthread_mutex_trylock_real)(pthread_mutex_t*) = NULL; -// static int (*pthread_mutex_timedlock_real)(pthread_mutex_t*,const struct timespec *) = NULL; -// static int (*pthread_mutex_unlock_real)(pthread_mutex_t*) = NULL; - -// static int (*pthread_cond_broadcast_real)(pthread_cond_t*) = NULL; -// static int (*pthread_cond_timedwait_real)(pthread_cond_t*,pthread_mutex_t*,const struct timespec *) = NULL; -// static int (*pthread_cond_signal_real)(pthread_cond_t*) = NULL; -// static int (*pthread_cond_wait_real)(pthread_cond_t*,pthread_mutex_t*) = NULL; - -// static int (*pthread_rwlock_rdlock_real)(pthread_rwlock_t *) = NULL; -// static int (*pthread_rwlock_tryrdlock_real)(pthread_rwlock_t *) = NULL; -// static int (*pthread_rwlock_timedrdlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; -// static int (*pthread_rwlock_wrlock_real)(pthread_rwlock_t *) = NULL; -// static int (*pthread_rwlock_trywrlock_real)(pthread_rwlock_t *) = NULL; -// static int (*pthread_rwlock_timedwrlock_real)(pthread_rwlock_t *, const struct timespec *) = NULL; -// static int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; - -static pthread_mutex_t extrae_pthread_create_mutex = PTHREAD_MUTEX_INITIALIZER; - -#endif /* PIC */ - -// static void GetpthreadHookPoints (int rank) -// { -// #if defined(PIC) - -// /* Obtain @ for pthread_create */ -// pthread_create_real = -// (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) -// dlsym (RTLD_NEXT, "pthread_create"); -// if (pthread_create_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_create in DSOs!!\n"); - -// /* Obtain @ for pthread_join */ -// pthread_join_real = -// (int(*)(pthread_t,void**)) dlsym (RTLD_NEXT, "pthread_join"); -// if (pthread_join_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_join in DSOs!!\n"); - -// /* Obtain @ for pthread_barrier_wait */ -// pthread_barrier_wait_real = -// (int(*)(pthread_barrier_t *)) dlsym (RTLD_NEXT, "pthread_barrier_wait"); -// if (pthread_barrier_wait_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_barrier_wait in DSOs!!\n"); - -// /* Obtain @ for pthread_detach */ -// pthread_detach_real = (int(*)(pthread_t)) dlsym (RTLD_NEXT, "pthread_detach"); -// if (pthread_detach_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_detach in DSOs!!\n"); - -// /* Obtain @ for pthread_exit */ -// pthread_exit_real = (void(*)(void*)) dlsym (RTLD_NEXT, "pthread_exit"); -// if (pthread_exit_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_exit in DSOs!!\n"); - -// /* Obtain @ for pthread_mutex_lock */ -// pthread_mutex_lock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_lock"); -// if (pthread_mutex_lock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_lock in DSOs!!\n"); - -// /* Obtain @ for pthread_mutex_unlock */ -// pthread_mutex_unlock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_unlock"); -// if (pthread_mutex_unlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_unlock in DSOs!!\n"); - -// /* Obtain @ for pthread_mutex_trylock */ -// pthread_mutex_trylock_real = (int(*)(pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_mutex_trylock"); -// if (pthread_mutex_trylock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_trylock in DSOs!!\n"); - -// /* Obtain @ for pthread_mutex_timedlock */ -// pthread_mutex_timedlock_real = (int(*)(pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_mutex_timedlock"); -// if (pthread_mutex_timedlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_mutex_timedlock in DSOs!!\n"); - -// /* Obtain @ for pthread_cond_signal */ -// pthread_cond_signal_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_signal"); -// if (pthread_cond_signal_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_signal in DSOs!!\n"); - -// /* Obtain @ for pthread_cond_broadcast */ -// pthread_cond_broadcast_real = (int(*)(pthread_cond_t*)) dlsym (RTLD_NEXT, "pthread_cond_broadcast"); -// if (pthread_cond_broadcast_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_broadcast in DSOs!!\n"); - -// /* Obtain @ for pthread_cond_wait */ -// pthread_cond_wait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*)) dlsym (RTLD_NEXT, "pthread_cond_wait"); -// if (pthread_cond_wait_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_wait in DSOs!!\n"); - -// /* Obtain @ for pthread_cond_timedwait */ -// pthread_cond_timedwait_real = (int(*)(pthread_cond_t*,pthread_mutex_t*,const struct timespec*)) dlsym (RTLD_NEXT, "pthread_cond_timedwait"); -// if (pthread_cond_timedwait_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_cond_timedwait in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_rdlock */ -// pthread_rwlock_rdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_rdlock"); -// if (pthread_rwlock_rdlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_rdlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_tryrdlock */ -// pthread_rwlock_tryrdlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_tryrdlock"); -// if (pthread_rwlock_tryrdlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_tryrdlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_timedrdlock */ -// pthread_rwlock_timedrdlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedrdlock"); -// if (pthread_rwlock_timedrdlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedrdlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_rwlock */ -// pthread_rwlock_wrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_wrlock"); -// if (pthread_rwlock_wrlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_wrlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_tryrwlock */ -// pthread_rwlock_trywrlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_trywrlock"); -// if (pthread_rwlock_trywrlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_trywrlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_timedrwlock */ -// pthread_rwlock_timedwrlock_real = (int(*)(pthread_rwlock_t *, const struct timespec *)) dlsym (RTLD_NEXT, "pthread_rwlock_timedwrlock"); -// if (pthread_rwlock_timedwrlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_timedwrlock in DSOs!!\n"); - -// /* Obtain @ for pthread_rwlock_unlock */ -// pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); -// if (pthread_rwlock_unlock_real == NULL && rank == 0) -// fprintf (stderr, PACKAGE_NAME": Unable to find pthread_rwlock_unlock in DSOs!!\n"); -// #else -// fprintf (stderr, PACKAGE_NAME": Warning! pthread instrumentation requires linking with shared library!\n"); -// #endif /* PIC */ -// } - /* INJECTED CODE -- INJECTED CODE -- INJECTED CODE -- INJECTED CODE @@ -738,7 +596,7 @@ int pthread_cond_timedwait (pthread_cond_t *c, pthread_mutex_t *m, const struct return res; } -int pthread_rwlock_rdlock (pthread_rwlock_t *l) +int pthread_rwlock_rdlock(pthread_rwlock_t *l) { int res = 0; From ed9c89ab06a1fd7768c6e3bd28e99d9970c87b02 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 17:15:33 +0100 Subject: [PATCH 4/9] switch to new interface and changed internal locks to that as well --- src/tracer/calltrace.c | 8 +- src/tracer/clocks/clock.c | 16 +++- src/tracer/hwc/common_hwc.c | 46 +++++----- src/tracer/hwc/papi_hwc.c | 50 +++++------ src/tracer/mode.c | 34 +++----- src/tracer/mode.h | 8 -- src/tracer/pthread_redirect.c | 35 ++++++++ src/tracer/pthread_redirect.h | 17 +++- src/tracer/signals.c | 22 ++--- src/tracer/wrappers/API/buffers.c | 5 +- src/tracer/wrappers/API/trace_buffers.h | 2 + src/tracer/wrappers/API/wrapper.c | 93 ++++++++------------- src/tracer/wrappers/MALLOC/malloc_wrapper.c | 12 +-- src/tracer/wrappers/MPI/hash_table.c | 18 ++-- src/tracer/wrappers/OMP/ompt-helper.c | 35 +++----- src/tracer/wrappers/pthread/Makefile.am | 3 +- 16 files changed, 203 insertions(+), 201 deletions(-) diff --git a/src/tracer/calltrace.c b/src/tracer/calltrace.c index a894f92d..386fe687 100644 --- a/src/tracer/calltrace.c +++ b/src/tracer/calltrace.c @@ -28,6 +28,7 @@ #include "trace_macros.h" #include "wrapper.h" #include "common_hwc.h" +#include "pthread_redirect.h" //#define DEBUG //#define MPICALLER_DEBUG @@ -87,14 +88,19 @@ void Extrae_trace_callers (iotimer_t time, int offset, int type) { if (Trace_Caller[type][current_deep-offset]) { + mtx_rw_rdlock(&pThread_mtx_Realloc); TRACE_EVENT(time, CALLER_EVENT_TYPE(type, current_deep-offset+1), (UINT64)ip); + mtx_rw_unlock(&pThread_mtx_Realloc); } } #if defined(SAMPLING_SUPPORT) else if (type == CALLER_SAMPLING) { - if (Trace_Caller[type][current_deep-offset]) + if (Trace_Caller[type][current_deep-offset]) { + mtx_rw_rdlock(&pThread_mtx_Realloc); SAMPLE_EVENT_NOHWC(time, SAMPLING_EV+current_deep-offset+1, (UINT64) ip); + mtx_rw_unlock(&pThread_mtx_Realloc); + } } #endif } diff --git a/src/tracer/clocks/clock.c b/src/tracer/clocks/clock.c index 8324ce85..ce9b22db 100644 --- a/src/tracer/clocks/clock.c +++ b/src/tracer/clocks/clock.c @@ -72,6 +72,9 @@ # error "Unhandled clock type" #endif +#include "pthread_redirect.h" +static pthread_rwlock_t pThread_mtx_extrae_last_read_clock = PTHREAD_RWLOCK_INITIALIZER; + static UINT64 *_extrae_last_read_clock = NULL; static unsigned ClockType = REAL_CLOCK; iotimer_t (*get_clock)(); @@ -89,7 +92,11 @@ unsigned Clock_getType (void) /* We obtain the last read time */ UINT64 Clock_getLastReadTime (unsigned thread) { - return _extrae_last_read_clock[thread]; + UINT64 ret; + mtx_rw_rdlock(&pThread_mtx_extrae_last_read_clock); + ret = _extrae_last_read_clock[thread]; + mtx_rw_unlock(&pThread_mtx_extrae_last_read_clock); + return ret; } /* We obtain the current time, but we don't store it in the last read time */ @@ -103,24 +110,31 @@ UINT64 Clock_getCurrentTime_nstore (void) UINT64 Clock_getCurrentTime (unsigned thread) { UINT64 tmp = Clock_getCurrentTime_nstore (); + mtx_rw_rdlock(&pThread_mtx_extrae_last_read_clock); _extrae_last_read_clock[thread] = tmp; + mtx_rw_unlock(&pThread_mtx_extrae_last_read_clock); return tmp; } void Clock_AllocateThreads (unsigned numthreads) { + mtx_rw_wrlock(&pThread_mtx_extrae_last_read_clock); _extrae_last_read_clock = (UINT64*) realloc (_extrae_last_read_clock, sizeof(UINT64)*numthreads); if (_extrae_last_read_clock == NULL) { fprintf (stderr, PACKAGE_NAME": Cannot allocate timing memory for %u threads\n", numthreads); + mtx_rw_unlock(&pThread_mtx_extrae_last_read_clock); exit (-1); } + mtx_rw_unlock(&pThread_mtx_extrae_last_read_clock); } void Clock_CleanUp (void) { + mtx_rw_wrlock(&pThread_mtx_extrae_last_read_clock); xfree (_extrae_last_read_clock); + mtx_rw_unlock(&pThread_mtx_extrae_last_read_clock); } void Clock_Initialize (unsigned numthreads) diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index 032893f7..9b340a7b 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -108,9 +108,9 @@ int HWC_IsEnabled() int HWC_Get_Current_Set (int threadid) { int ret; - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); ret = HWC_current_set[threadid]; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); return ret; } @@ -203,10 +203,10 @@ void HWC_Stop_Current_Set (UINT64 time, int thread_id) /* make sure we don't loose the current counter values */ Extrae_counters_at_Time_Wrapper(time); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); /* Actually stop the counters */ HWCBE_STOP_SET (time, HWC_current_set[thread_id], thread_id); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } } @@ -219,10 +219,10 @@ void HWC_Start_Current_Set (UINT64 countglops, UINT64 time, int thread_id) /* If there are less than 2 sets, don't do anything! */ if (HWC_num_sets > 0) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); /* Actually start the counters */ HWCBE_START_SET (countglops, time, HWC_current_set[thread_id], thread_id); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } } @@ -238,13 +238,13 @@ void HWC_Start_Next_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); /* Move to the next set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = (HWC_current_set[thread_id] + 1) % HWC_num_sets; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -261,13 +261,13 @@ void HWC_Start_Previous_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); /* Move to the previous set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = ((HWC_current_set[thread_id] - 1) < 0) ? (HWC_num_sets - 1) : (HWC_current_set[thread_id] - 1) ; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -389,7 +389,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) /* Allocate memory if this process has not been forked */ if (!forked) { - mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); HWC_Thread_Initialized = (int *) malloc (sizeof(int) * num_threads); ASSERT(HWC_Thread_Initialized!=NULL, "Cannot allocate memory for HWC_Thread_Initialized!"); @@ -409,7 +409,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) ASSERT(Accumulated_HWC[i]!=NULL, "Cannot allocate memory for Accumulated_HWC"); HWC_Accum_Reset(i); } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); if (HWC_num_sets <= 0) { return; @@ -443,7 +443,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) void HWC_Restart_Counters (int old_num_threads, int new_num_threads) { int i; - mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); #if defined(PAPI_COUNTERS) for (i = 0; i < HWC_num_sets; i++) HWCBE_PAPI_Allocate_eventsets_per_thread (i, old_num_threads, new_num_threads); @@ -484,7 +484,7 @@ void HWC_Restart_Counters (int old_num_threads, int new_num_threads) HWC_current_timebegin[i] = 0; HWC_current_glopsbegin[i] = 0; } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } /** @@ -608,12 +608,14 @@ int HWC_Read (unsigned int tid, UINT64 time, long long *store_buffer) if (HWCEnabled) { + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( store_buffer ); read_ok = HWCBE_READ (tid, store_buffer); reset_ok = (Reset_After_Read ? HWCBE_RESET (tid) : TRUE); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } return (HWCEnabled && read_ok && reset_ok); } @@ -649,7 +651,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( Accumulated_HWC[tid] ); @@ -663,7 +665,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) #endif Accumulated_HWC_Valid[tid] = TRUE; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } return (HWCEnabled && accum_ok); } @@ -688,9 +690,9 @@ int HWC_Accum_Reset (unsigned int tid) int HWC_Accum_Valid_Values (unsigned int tid) { int ret; - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); ret = ( HWCEnabled ? Accumulated_HWC_Valid[tid] : 0 ); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); return ret; } @@ -703,9 +705,9 @@ int HWC_Accum_Copy_Here (unsigned int tid, long long *store_buffer) { if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); memcpy(store_buffer, Accumulated_HWC[tid], MAX_HWC * sizeof(long long)); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); return 1; } else return 0; @@ -721,12 +723,12 @@ int HWC_Accum_Add_Here (unsigned int tid, long long *store_buffer) int i; if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters, "pThread_mtx_HWC_Start_Counters"); + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); for (i=0; i= HWC_num_sets) return FALSE; @@ -475,7 +475,7 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread fprintf (stderr, PACKAGE_NAME": errno = %d\n", errno); } } - // pthread_rwlock_unlock_real(&pThread_mtx_HWC_sets); + //mtx_rw_unlock(&pThread_mtx_HWC_sets); return rc == PAPI_OK; } @@ -513,7 +513,7 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) int state; int i; unsigned t; - mtx_rw_wrlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_wrlock(&pThread_mtx_HWC_sets); if (PAPI_state (HWCEVTSET(THREADID), &state) == PAPI_OK) { if (state & PAPI_RUNNING) @@ -546,7 +546,7 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) } #endif xfree (HWC_sets); - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); PAPI_shutdown(); } } @@ -614,7 +614,7 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) if (HWC_num_sets <= 0) return FALSE; - //pthread_rwlock_rdlock_real(&pThread_mtx_HWC_sets); + //mtx_rw_rdlock(&pThread_mtx_HWC_sets); if (forked) { PAPI_stop (HWCEVTSET(threadid), NULL); @@ -676,7 +676,7 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) Extrae_IntelPEBS_startSampling(); #endif int ret = HWC_Thread_Initialized[threadid]; - //pthread_rwlock_unlock_real(&pThread_mtx_HWC_sets); + //mtx_rw_unlock(&pThread_mtx_HWC_sets); return ret; } @@ -685,7 +685,7 @@ int __in_PAPI_read_BG = FALSE; #endif int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_rdlock(&pThread_mtx_HWC_sets); int EventSet = HWCEVTSET(tid); #if !defined(IS_BG_MACHINE) @@ -693,10 +693,10 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 1; #else if (!__in_PAPI_read_BG) @@ -706,15 +706,15 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } __in_PAPI_read_BG = FALSE; - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 1; } else { - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } #endif @@ -722,29 +722,29 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) int HWCBE_PAPI_Reset (unsigned int tid) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_rdlock(&pThread_mtx_HWC_sets); if (PAPI_reset(HWCEVTSET(tid)) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_reset failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 1; } int HWCBE_PAPI_Accum (unsigned int tid, long long *store_buffer) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_rdlock(&pThread_mtx_HWC_sets); if (PAPI_accum(HWCEVTSET(tid), store_buffer) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_accum failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets, "pThread_mtx_HWC_sets"); + mtx_rw_unlock(&pThread_mtx_HWC_sets); return 1; } diff --git a/src/tracer/mode.c b/src/tracer/mode.c index 570244bd..8ab56a6f 100644 --- a/src/tracer/mode.c +++ b/src/tracer/mode.c @@ -61,13 +61,13 @@ static int is_ValidMode (int mode) void Trace_Mode_CleanUp (void) { - pthread_rwlock_wrlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_wrlock(&pThread_mtx_Trace_Mode_reInitialize); xfree (MPI_Deepness); xfree (Current_Trace_Mode); xfree (Future_Trace_Mode); xfree (Pending_Trace_Mode_Change); xfree (First_Trace_Mode); - pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); } int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) @@ -76,7 +76,7 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) size = sizeof(int) * new_num_threads; - pthread_rwlock_wrlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_wrlock(&pThread_mtx_Trace_Mode_reInitialize); MPI_Deepness = (int *)realloc(MPI_Deepness, size); if (MPI_Deepness == NULL) { @@ -120,28 +120,22 @@ int Trace_Mode_reInitialize (int old_num_threads, int new_num_threads) Pending_Trace_Mode_Change[i] = FALSE; First_Trace_Mode[i] = TRUE; } - pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); return TRUE; } int Trace_Mode_FirstMode (unsigned thread) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - int ret; - pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); ret = First_Trace_Mode[thread]; - pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); return ret; } int Trace_Mode_Initialize (int num_threads) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - int res = Trace_Mode_reInitialize (0, num_threads); /* Show configuration */ @@ -169,10 +163,7 @@ int Trace_Mode_Initialize (int num_threads) void Trace_Mode_Change (int tid, iotimer_t time) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); if (Pending_Trace_Mode_Change[tid] || First_Trace_Mode[tid]) { if (Future_Trace_Mode[tid] != Current_Trace_Mode[tid] || First_Trace_Mode[tid]) @@ -193,7 +184,7 @@ void Trace_Mode_Change (int tid, iotimer_t time) Pending_Trace_Mode_Change[tid] = FALSE; First_Trace_Mode[tid] = FALSE; } - pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); } void @@ -201,22 +192,19 @@ Trace_mode_switch(void) { unsigned i; - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - /* + /* * XXX Should this be Backend_getMaximumOfThreads()? If we decrease the * number of threads, switch tracing mode and then increase again the the * number of threads, only the "old" threads will use burst mode, while the * "new" ones will continue in detail. */ - pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); for (i=0; i # endif #endif -#include +#include "pthread_redirect.h" /* #define DBG_SIGNALS */ @@ -57,24 +57,24 @@ static pthread_mutex_t pThread_mtx_sigInhibited = PTHREAD_MUTEX_INITIALIZER; void Signals_Inhibit() { - pthread_mutex_lock(&pThread_mtx_sigInhibited); + mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); sigInhibited = TRUE; - pthread_mutex_unlock(&pThread_mtx_sigInhibited); + mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); } void Signals_Desinhibit() { - pthread_mutex_lock(&pThread_mtx_sigInhibited); + mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); sigInhibited = FALSE; - pthread_mutex_unlock(&pThread_mtx_sigInhibited); + mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); } int Signals_Inhibited() { int ret; - pthread_mutex_lock(&pThread_mtx_sigInhibited); + mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); ret = sigInhibited; - pthread_mutex_unlock(&pThread_mtx_sigInhibited); + mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); return ret; } @@ -231,20 +231,20 @@ void Signals_CondInit (Condition_t *cond) void Signals_CondWait (Condition_t *cond) { - pthread_mutex_lock(&(cond->ConditionMutex)); + mtx_lock(&(cond->ConditionMutex)); while (cond->WaitingForCondition) { pthread_cond_wait(&(cond->WaitCondition), &(cond->ConditionMutex)); } - pthread_mutex_unlock(&(cond->ConditionMutex)); + mtx_unlock(&(cond->ConditionMutex)); } void Signals_CondWakeUp (Condition_t *cond) { - pthread_mutex_lock(&(cond->ConditionMutex)); + mtx_lock(&(cond->ConditionMutex)); cond->WaitingForCondition = FALSE; pthread_cond_signal(&(cond->WaitCondition)); - pthread_mutex_unlock(&(cond->ConditionMutex)); + mtx_unlock(&(cond->ConditionMutex)); } #endif /* HAVE_ONLINE */ diff --git a/src/tracer/wrappers/API/buffers.c b/src/tracer/wrappers/API/buffers.c index f59b8b36..d13ce07f 100644 --- a/src/tracer/wrappers/API/buffers.c +++ b/src/tracer/wrappers/API/buffers.c @@ -55,6 +55,7 @@ #include "buffers.h" #include "utils.h" +#include "pthread_redirect.h" #define EVENT_INDEX(buffer, event) (event - Buffer_GetFirst(buffer)) #define ALL_BITS_SET 0xFFFFFFFF @@ -356,7 +357,7 @@ event_t * Buffer_GetNext (Buffer_t *buffer, event_t *current) void Buffer_Lock (Buffer_t *buffer) { #if defined(HAVE_ONLINE) - pthread_mutex_lock(&(buffer->Lock)); + mtx_lock(&(buffer->Lock)); #else UNREFERENCED_PARAMETER(buffer); #endif @@ -365,7 +366,7 @@ void Buffer_Lock (Buffer_t *buffer) void Buffer_Unlock (Buffer_t *buffer) { #if defined(HAVE_ONLINE) - pthread_mutex_unlock(&(buffer->Lock)); + mtx_unlock(&(buffer->Lock)); #else UNREFERENCED_PARAMETER(buffer); #endif diff --git a/src/tracer/wrappers/API/trace_buffers.h b/src/tracer/wrappers/API/trace_buffers.h index c943072b..6be94ee4 100644 --- a/src/tracer/wrappers/API/trace_buffers.h +++ b/src/tracer/wrappers/API/trace_buffers.h @@ -26,10 +26,12 @@ #include "buffers.h" #include "signals.h" +#include "pthread_redirect.h" /* Don't like these externs -> Declare fetch functions in wrapper.c and include prototypes in wrapper.h ? */ extern Buffer_t **TracingBuffer; extern Buffer_t **SamplingBuffer; +extern pthread_rwlock_t pThread_mtx_Realloc; #if defined(__cplusplus) extern "C" { diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index 6be353e5..003c96f2 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -168,7 +168,7 @@ int Extrae_Flush_Wrapper (Buffer_t *buffer); static int requestedDynamicMemoryInstrumentation = FALSE; static pthread_mutex_t pThreadChangeNumberOfThreads_mtx = PTHREAD_MUTEX_INITIALIZER; -static pthread_rwlock_t pThread_mtx_Realloc = PTHREAD_RWLOCK_INITIALIZER; +pthread_rwlock_t pThread_mtx_Realloc = PTHREAD_RWLOCK_INITIALIZER; static pthread_rwlock_t pThread_mtx_Extrae_inInstrumentation = PTHREAD_RWLOCK_INITIALIZER; extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; @@ -500,17 +500,13 @@ void Extrae_AnnotateCPU (UINT64 timestamp) #if defined(HAVE_SCHED_GETCPU) int cpu = sched_getcpu(); - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_Realloc); if (cpu != LastCPUEvent[THREADID] || AlwaysEmitCPUEvent) { LastCPUEvent[THREADID] = cpu; TRACE_EVENT (timestamp, GETCPU_EV, cpu); } - - pthread_rwlock_unlock_real(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_Realloc); #else UNREFERENCED_PARAMETER(timestamp); #endif @@ -1422,9 +1418,6 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork { int i; - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); - #if 0 /* MRNET */ /* FIXME: Temporarily disabled. new_buffer_size overflows when target_mbs > 1000 */ if (MRNet_isEnabled()) @@ -1444,7 +1437,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork UNREFERENCED_PARAMETER(world_size); #endif - pthread_rwlock_wrlock_real(&pThread_mtx_Realloc); + mtx_rw_wrlock(&pThread_mtx_Realloc); if (!forked) { xmalloc(TracingBuffer, num_threads * sizeof(Buffer_t *)); @@ -1457,7 +1450,7 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork for (i = 0; i < num_threads; i++) Allocate_buffer_and_file (i, forked); - pthread_rwlock_unlock_real(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1469,10 +1462,8 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork static int Reallocate_buffers_and_files (int new_num_threads) { int i; - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); - pthread_rwlock_wrlock_real(&pThread_mtx_Realloc); + mtx_rw_wrlock(&pThread_mtx_Realloc); xrealloc(TracingBuffer, TracingBuffer, new_num_threads * sizeof(Buffer_t *)); xrealloc(LastCPUEmissionTime, LastCPUEmissionTime, new_num_threads * sizeof(iotimer_t)); xrealloc(LastCPUEvent, LastCPUEvent, new_num_threads * sizeof(int)); @@ -1481,7 +1472,7 @@ static int Reallocate_buffers_and_files (int new_num_threads) #endif for (i = get_maximum_NumOfThreads(); i < new_num_threads; i++) Allocate_buffer_and_file (i, FALSE); - pthread_rwlock_unlock_real(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1573,7 +1564,7 @@ void Backend_Flush_pThread (pthread_t t) // Protect race condition with the master thread flushing the buffers at // the end of this pthread and the process - pthread_mutex_lock(&pthreadFreeBuffer_mtx); + mtx_lock(&pthreadFreeBuffer_mtx); // TracingBuffer may have been already free'd by the master thread if the process finished if (TracingBuffer != NULL) @@ -1599,7 +1590,7 @@ void Backend_Flush_pThread (pthread_t t) } #endif - pthread_mutex_unlock(&pthreadFreeBuffer_mtx); + mtx_unlock(&pthreadFreeBuffer_mtx); break; } @@ -1626,13 +1617,13 @@ void Backend_NotifyNewPthread (void) { int numthreads; - pthread_mutex_lock (&pThreadIdentifier_mtx); + mtx_lock(&pThreadIdentifier_mtx); numthreads = Backend_getNumberOfThreads(); Backend_SetpThreadIdentifier (numthreads); Backend_ChangeNumberOfThreads (numthreads+1); - pthread_mutex_unlock (&pThreadIdentifier_mtx); + mtx_unlock (&pThreadIdentifier_mtx); } #endif @@ -1986,7 +1977,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) { unsigned new_num_threads = numberofthreads; - pthread_mutex_lock (&pThreadChangeNumberOfThreads_mtx); + mtx_lock(&pThreadChangeNumberOfThreads_mtx); if (EXTRAE_INITIALIZED()) { @@ -2057,7 +2048,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) current_NumOfThreads = new_num_threads; } - pthread_mutex_unlock (&pThreadChangeNumberOfThreads_mtx); + mtx_unlock(&pThreadChangeNumberOfThreads_mtx); return TRUE; } @@ -2475,7 +2466,7 @@ void Backend_Finalize (void) for (thread = 0; thread < get_maximum_NumOfThreads(); thread++) { // Protects race condition with Backend_Flush_pThread - pthread_mutex_lock(&pthreadFreeBuffer_mtx); + mtx_lock(&pthreadFreeBuffer_mtx); // If buffer was working in circular mode, change it to flush mode to dump the final data into the trace if ((circular_buffering) && (!online_mode)) @@ -2492,7 +2483,7 @@ void Backend_Finalize (void) Extrae_Flush_Wrapper_setCounters (TRUE); - pthread_mutex_unlock(&pthreadFreeBuffer_mtx); + mtx_unlock(&pthreadFreeBuffer_mtx); } /* Final write files to disk, include renaming of the filenames, @@ -2501,7 +2492,7 @@ void Backend_Finalize (void) for (thread = 0; thread < get_maximum_NumOfThreads(); thread++) { // Protects race condition with Backend_Flush_pThread - pthread_mutex_lock(&pthreadFreeBuffer_mtx); + mtx_lock(&pthreadFreeBuffer_mtx); if (TRACING_BUFFER(thread) != NULL) { @@ -2510,7 +2501,7 @@ void Backend_Finalize (void) Backend_Finalize_close_mpits (getpid(), thread, FALSE); } - pthread_mutex_unlock(&pthreadFreeBuffer_mtx); + mtx_unlock(&pthreadFreeBuffer_mtx); } /* Free allocated memory */ @@ -2524,7 +2515,7 @@ void Backend_Finalize (void) pThreads[thread] = (pthread_t)0; #endif // Protects race condition with Backend_Flush_pThread - pthread_mutex_lock(&pthreadFreeBuffer_mtx); + mtx_lock(&pthreadFreeBuffer_mtx); if (TRACING_BUFFER(thread) != NULL) { Buffer_Free (TRACING_BUFFER(thread)); @@ -2537,7 +2528,7 @@ void Backend_Finalize (void) SAMPLING_BUFFER(thread) = NULL; } #endif - pthread_mutex_unlock(&pthreadFreeBuffer_mtx); + mtx_unlock(&pthreadFreeBuffer_mtx); } xfree(LastCPUEmissionTime); xfree(LastCPUEvent); @@ -2606,14 +2597,14 @@ void Backend_Finalize (void) int pid; Extrae_getAppendingEventsToGivenPID (&pid); // Protects race condition with Backend_Flush_pThread - pthread_mutex_lock(&pthreadFreeBuffer_mtx); + mtx_lock(&pthreadFreeBuffer_mtx); if (TRACING_BUFFER(THREADID) != NULL) { Buffer_Flush(TRACING_BUFFER(THREADID)); for (thread = 0; thread < get_maximum_NumOfThreads(); thread++) Backend_Finalize_close_mpits (pid, thread, TRUE); } - pthread_mutex_unlock(&pthreadFreeBuffer_mtx); + mtx_unlock(&pthreadFreeBuffer_mtx); remove_temporal_files (); } } @@ -2624,45 +2615,33 @@ static int *Extrae_inSampling = NULL; int Backend_inInstrumentation (unsigned thread) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_rdlock(&pThread_mtx_Extrae_inInstrumentation); int ret = FALSE; if ((Extrae_inInstrumentation != NULL) && (Extrae_inSampling != NULL)) ret = (Extrae_inInstrumentation[thread] || Extrae_inSampling[thread]); - pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_unlock(&pThread_mtx_Extrae_inInstrumentation); return ret; } void Backend_setInSampling (unsigned thread, int insampling) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_rdlock(&pThread_mtx_Extrae_inInstrumentation); if (Extrae_inSampling != NULL) Extrae_inSampling[thread] = insampling; - pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_unlock(&pThread_mtx_Extrae_inInstrumentation); } void Backend_setInInstrumentation (unsigned thread, int ininstrumentation) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_wrlock(&pThread_mtx_Extrae_inInstrumentation); if (Extrae_inInstrumentation != NULL) Extrae_inInstrumentation[thread] = ininstrumentation; - pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_unlock(&pThread_mtx_Extrae_inInstrumentation); } void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) { - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_wrlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_wrlock(&pThread_mtx_Extrae_inInstrumentation); Extrae_inInstrumentation = (int*) realloc (Extrae_inInstrumentation, sizeof(int)*nthreads); if (Extrae_inInstrumentation == NULL) { @@ -2677,7 +2656,7 @@ void Backend_ChangeNumberOfThreads_InInstrumentation (unsigned nthreads) ": Failed to allocate memory for inSampling structure\n"); exit (-1); } - pthread_rwlock_unlock_real(&pThread_mtx_Extrae_inInstrumentation); + mtx_rw_unlock(&pThread_mtx_Extrae_inInstrumentation); } void Backend_Enter_Instrumentation () @@ -2727,14 +2706,11 @@ void Backend_Enter_Instrumentation () } #endif - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - /* Check whether we will fill the buffer soon (or now) */ - pthread_rwlock_rdlock_real(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_Realloc); if (Buffer_RemainingEvents(TracingBuffer[thread]) <= NEVENTS) Buffer_ExecuteFlushCallback (TracingBuffer[thread]); - pthread_rwlock_unlock_real(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_Realloc); /* Record the time when this is happening we need this for subsequent calls to TIME, as this is being cached in clock routines */ @@ -2766,14 +2742,11 @@ void Backend_Leave_Instrumentation (void) Extrae_AnnotateCPU(LAST_READ_TIME); } - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - /* Change trace mode? (issue from API) */ - pthread_rwlock_rdlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); if (PENDING_TRACE_MODE_CHANGE(thread) && MPI_Deepness[thread] == 0) Trace_Mode_Change(thread, LAST_READ_TIME); - pthread_rwlock_unlock_real(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); Backend_setInInstrumentation (thread, FALSE); } diff --git a/src/tracer/wrappers/MALLOC/malloc_wrapper.c b/src/tracer/wrappers/MALLOC/malloc_wrapper.c index aef13f5a..849f5075 100644 --- a/src/tracer/wrappers/MALLOC/malloc_wrapper.c +++ b/src/tracer/wrappers/MALLOC/malloc_wrapper.c @@ -98,7 +98,7 @@ static void Extrae_malloctrace_add (void *p, size_t s) unsigned u; assert (real_realloc != NULL); - pthread_mutex_lock (&mutex_allocations); + mtx_lock(&mutex_allocations); if (nmallocentries == nmallocentries_allocated) { @@ -124,7 +124,7 @@ static void Extrae_malloctrace_add (void *p, size_t s) break; } - pthread_mutex_unlock (&mutex_allocations); + mtx_unlock(&mutex_allocations); } } @@ -136,7 +136,7 @@ static int Extrae_malloctrace_remove (const void *p) { unsigned u; - pthread_mutex_lock (&mutex_allocations); + mtx_lock(&mutex_allocations); for (u = 0; u < nmallocentries_allocated; u++) if (mallocentries[u] == p) @@ -148,7 +148,7 @@ static int Extrae_malloctrace_remove (const void *p) break; } - pthread_mutex_unlock (&mutex_allocations); + mtx_unlock(&mutex_allocations); } return found; } @@ -157,7 +157,7 @@ static size_t Extrae_malloctrace_replace (const void *p1, void *p2, size_t s) { size_t prev_sz = 0; - pthread_mutex_lock (&mutex_allocations); + mtx_lock(&mutex_allocations); int replaced = FALSE; if (p1) @@ -206,7 +206,7 @@ static size_t Extrae_malloctrace_replace (const void *p1, void *p2, size_t s) } } - pthread_mutex_unlock (&mutex_allocations); + mtx_unlock(&mutex_allocations); return prev_sz; } diff --git a/src/tracer/wrappers/MPI/hash_table.c b/src/tracer/wrappers/MPI/hash_table.c index 889b8ece..95fda38b 100644 --- a/src/tracer/wrappers/MPI/hash_table.c +++ b/src/tracer/wrappers/MPI/hash_table.c @@ -159,15 +159,13 @@ void xtr_hash_free(xtr_hash_t *hash) */ int xtr_hash_add (xtr_hash_t *hash, uintptr_t key, void *data) { - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); // Apply the hash function to find corresponding cell in the head array xtr_hash_cell_t *cell = XTR_HASH_GET_CELL_FOR_KEY(hash, key); xtr_hash_cell_t *cell_to_add = cell; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_wrlock_real(&hash->lock); + mtx_rw_wrlock(&hash->lock); } #if defined(DEBUG) @@ -201,7 +199,7 @@ int xtr_hash_add (xtr_hash_t *hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock_real(&hash->lock); + mtx_rw_unlock(&hash->lock); } return 1; @@ -261,13 +259,11 @@ static int xtr_hash_search (xtr_hash_t *hash, uintptr_t key, xtr_hash_cell_t **p */ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) { - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); xtr_hash_cell_t *previous = NULL, *cell = NULL; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_rdlock_real(&hash->lock); + mtx_rw_rdlock(&hash->lock); } #if defined(DEBUG) @@ -287,7 +283,7 @@ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock_real(&hash->lock); + mtx_rw_unlock(&hash->lock); } return 0; @@ -307,14 +303,12 @@ int xtr_hash_query (xtr_hash_t *hash, uintptr_t key, void *data) */ int xtr_hash_fetch (xtr_hash_t * hash, uintptr_t key, void *data) { - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); int found = 0; xtr_hash_cell_t *previous = NULL, *cell = NULL; if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_wrlock_real(&hash->lock); + mtx_rw_wrlock(&hash->lock); } #if defined(DEBUG) @@ -347,7 +341,7 @@ int xtr_hash_fetch (xtr_hash_t * hash, uintptr_t key, void *data) if (hash->flags & XTR_HASH_LOCK) { - pthread_rwlock_unlock_real(&hash->lock); + mtx_rw_unlock(&hash->lock); } return found; diff --git a/src/tracer/wrappers/OMP/ompt-helper.c b/src/tracer/wrappers/OMP/ompt-helper.c index e994f01b..56654b59 100644 --- a/src/tracer/wrappers/OMP/ompt-helper.c +++ b/src/tracer/wrappers/OMP/ompt-helper.c @@ -166,10 +166,7 @@ void Extrae_OMPT_register_ompt_task_id_tf (ompt_task_id_t ompt_tid, { unsigned u; - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_wrlock_real(&mutex_tid_tf); + mtx_rw_wrlock(&mutex_tid_tf); if (n_ompt_tids_tf == n_allocated_ompt_tids_tf) { #if defined(DEBUG) @@ -208,7 +205,7 @@ void Extrae_OMPT_register_ompt_task_id_tf (ompt_task_id_t ompt_tid, #endif break; } - pthread_rwlock_unlock_real(&mutex_tid_tf); + mtx_rw_unlock(&mutex_tid_tf); } /* Extrae_OMPT_unregister_ompt_task_id_tf @@ -220,10 +217,7 @@ void Extrae_OMPT_unregister_ompt_task_id_tf (ompt_task_id_t ompt_tid) { unsigned u; - if (pthread_rwlock_wrlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_wrlock_real(&mutex_tid_tf); + mtx_rw_wrlock(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) { @@ -236,7 +230,7 @@ void Extrae_OMPT_unregister_ompt_task_id_tf (ompt_task_id_t ompt_tid) #endif break; } - pthread_rwlock_unlock_real(&mutex_tid_tf); + mtx_rw_unlock(&mutex_tid_tf); } } @@ -248,10 +242,7 @@ const void * Extrae_OMPT_get_tf_task_id (ompt_task_id_t ompt_tid, unsigned u; const void *ptr = NULL; - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&mutex_tid_tf); + mtx_rw_rdlock(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) { @@ -262,7 +253,7 @@ const void * Extrae_OMPT_get_tf_task_id (ompt_task_id_t ompt_tid, *taskctr = ompt_tids_tf[u].task_ctr; break; } - pthread_rwlock_unlock_real(&mutex_tid_tf); + mtx_rw_unlock(&mutex_tid_tf); return ptr; } @@ -273,10 +264,7 @@ void Extrae_OMPT_tf_task_id_set_running (ompt_task_id_t ompt_tid, int b) { unsigned u; - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&mutex_tid_tf); + mtx_rw_rdlock(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) @@ -285,7 +273,7 @@ void Extrae_OMPT_tf_task_id_set_running (ompt_task_id_t ompt_tid, int b) break; } - pthread_rwlock_unlock_real(&mutex_tid_tf); + mtx_rw_unlock(&mutex_tid_tf); } @@ -297,10 +285,7 @@ int Extrae_OMPT_tf_task_id_is_running (ompt_task_id_t ompt_tid) unsigned u; int res = FALSE; - if (pthread_rwlock_rdlock_real == NULL) - GetpthreadHookPoints(0); - - pthread_rwlock_rdlock_real(&mutex_tid_tf); + mtx_rw_rdlock(&mutex_tid_tf); for (u = 0; u < n_allocated_ompt_tids_tf; u++) if (ompt_tids_tf[u].tid == ompt_tid) @@ -309,7 +294,7 @@ int Extrae_OMPT_tf_task_id_is_running (ompt_task_id_t ompt_tid) break; } - pthread_rwlock_unlock_real(&mutex_tid_tf); + mtx_rw_unlock(&mutex_tid_tf); return res; } diff --git a/src/tracer/wrappers/pthread/Makefile.am b/src/tracer/wrappers/pthread/Makefile.am index 558ceebb..fab8e848 100644 --- a/src/tracer/wrappers/pthread/Makefile.am +++ b/src/tracer/wrappers/pthread/Makefile.am @@ -3,8 +3,7 @@ include $(top_srcdir)/PATHS # Wrappers for pthread instrumentation WRAPPERS_PTHREAD = \ pthread_wrapper.c pthread_wrapper.h \ - pthread_probe.c pthread_probe.h \ - pthread_redirect.c pthread_redirect.h + pthread_probe.c pthread_probe.h noinst_LTLIBRARIES = libwrap_pthread.la From 74fefb363a57b999b02989d30e4a83861977a259 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 17:59:02 +0100 Subject: [PATCH 5/9] fixed some parts --- src/tracer/hwc/common_hwc.c | 4 ++++ src/tracer/mode.h | 2 +- src/tracer/signals.c | 12 ++++++------ src/tracer/wrappers/API/buffers.h | 2 +- src/tracer/wrappers/pthread/pthread_wrapper.c | 2 ++ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index 9b340a7b..69889c31 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -609,12 +609,14 @@ int HWC_Read (unsigned int tid, UINT64 time, long long *store_buffer) if (HWCEnabled) { mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_Realloc); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( store_buffer ); read_ok = HWCBE_READ (tid, store_buffer); reset_ok = (Reset_After_Read ? HWCBE_RESET (tid) : TRUE); + mtx_rw_unlock(&pThread_mtx_Realloc); mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } return (HWCEnabled && read_ok && reset_ok); @@ -652,6 +654,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) if (HWCEnabled) { mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_Realloc); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( Accumulated_HWC[tid] ); @@ -665,6 +668,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) #endif Accumulated_HWC_Valid[tid] = TRUE; + mtx_rw_unlock(&pThread_mtx_Realloc); mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } return (HWCEnabled && accum_ok); diff --git a/src/tracer/mode.h b/src/tracer/mode.h index 8fa01ba4..1b848a4a 100644 --- a/src/tracer/mode.h +++ b/src/tracer/mode.h @@ -34,7 +34,7 @@ extern int *Pending_Trace_Mode_Change; extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; -#define CURRENT_TRACE_MODE(tid) Current_Trace_Mode[tid]\ +#define CURRENT_TRACE_MODE(tid) Current_Trace_Mode[tid] #define PENDING_TRACE_MODE_CHANGE(tid) Pending_Trace_Mode_Change[tid] #define INCREASE_MPI_DEEPNESS(tid) (MPI_Deepness[tid]++) diff --git a/src/tracer/signals.c b/src/tracer/signals.c index 70c7bd1e..8aa72658 100644 --- a/src/tracer/signals.c +++ b/src/tracer/signals.c @@ -57,24 +57,24 @@ static pthread_mutex_t pThread_mtx_sigInhibited = PTHREAD_MUTEX_INITIALIZER; void Signals_Inhibit() { - mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_lock(&pThread_mtx_sigInhibited); sigInhibited = TRUE; - mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_unlock(&pThread_mtx_sigInhibited); } void Signals_Desinhibit() { - mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_lock(&pThread_mtx_sigInhibited); sigInhibited = FALSE; - mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_unlock(&pThread_mtx_sigInhibited); } int Signals_Inhibited() { int ret; - mtx_lock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_lock(&pThread_mtx_sigInhibited); ret = sigInhibited; - mtx_unlock(&pThread_mtx_sigInhibited, "pThread_mtx_sigInhibited"); + mtx_unlock(&pThread_mtx_sigInhibited); return ret; } diff --git a/src/tracer/wrappers/API/buffers.h b/src/tracer/wrappers/API/buffers.h index d31d067e..bf3915f7 100644 --- a/src/tracer/wrappers/API/buffers.h +++ b/src/tracer/wrappers/API/buffers.h @@ -39,7 +39,7 @@ #include "record.h" #define LOCK_AT_INSERT 1 -//#define LOCK_AT_FLUSH 1 +#define LOCK_AT_FLUSH 1 typedef int Mask_t; diff --git a/src/tracer/wrappers/pthread/pthread_wrapper.c b/src/tracer/wrappers/pthread/pthread_wrapper.c index 8704c76d..5e857dd5 100644 --- a/src/tracer/wrappers/pthread/pthread_wrapper.c +++ b/src/tracer/wrappers/pthread/pthread_wrapper.c @@ -56,6 +56,8 @@ */ +static pthread_rwlock_t extrae_pthread_create_mutex = PTHREAD_RWLOCK_INITIALIZER; + #if defined(PIC) struct pthread_create_info From 1f01c58320c8187e6d3612865905f15a0e5bd0f4 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 23:07:42 +0100 Subject: [PATCH 6/9] fixed some races --- src/tracer/hwc/common_hwc.c | 21 +++++++++++++----- src/tracer/hwc/common_hwc.h | 3 +++ src/tracer/hwc/papi_hwc.c | 2 ++ src/tracer/hwc/pmapi_hwc.c | 2 ++ src/tracer/wrappers/API/buffers.c | 32 +++++++++++++++------------ src/tracer/wrappers/API/buffers.h | 6 ++--- src/tracer/wrappers/API/wrapper.c | 6 +++-- src/tracer/wrappers/MPI/mpi_wrapper.c | 10 +++++++-- 8 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index 69889c31..18c4e0c0 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -54,6 +54,7 @@ #include "pthread_redirect.h" static pthread_rwlock_t pThread_mtx_HWC_Start_Counters = PTHREAD_RWLOCK_INITIALIZER; +pthread_mutex_t pThread_mtx_HWC_current_changetype = PTHREAD_MUTEX_INITIALIZER; /*------------------------------------------------ Global Variables ---------*/ int HWCEnabled = FALSE; /* Have the HWC been started? */ @@ -307,12 +308,13 @@ static inline int CheckForHWCSetChange_TIME (UINT64 countglops, UINT64 time, int int ret = 0; // fprintf (stderr, "HWC_current_timebegin[%d]=%llu HWC_current_changeat=%llu time = %llu\n", THREADID, HWC_current_timebegin[threadid], HWC_current_changeat, time); - + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); if (HWC_current_timebegin[threadid] + HWC_current_changeat < time) { HWC_Start_Next_Set (countglops, time, threadid); ret = 1; } + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); return ret; } @@ -325,12 +327,16 @@ static inline int CheckForHWCSetChange_TIME (UINT64 countglops, UINT64 time, int */ int HWC_Check_Pending_Set_Change (UINT64 countglops, UINT64 time, int thread_id) { - if (HWC_current_changetype == CHANGE_GLOPS) + mtx_lock(&pThread_mtx_HWC_current_changetype); + if (HWC_current_changetype == CHANGE_GLOPS) { + mtx_unlock(&pThread_mtx_HWC_current_changetype); return CheckForHWCSetChange_GLOPS(countglops, time, thread_id); - else if (HWC_current_changetype == CHANGE_TIME) + } else if (HWC_current_changetype == CHANGE_TIME) { + mtx_unlock(&pThread_mtx_HWC_current_changetype); return CheckForHWCSetChange_TIME(countglops, time, thread_id); - else - return 0; + } + mtx_unlock(&pThread_mtx_HWC_current_changetype); + return 0; } /** @@ -340,7 +346,7 @@ int HWC_Check_Pending_Set_Change (UINT64 countglops, UINT64 time, int thread_id) void HWC_Initialize (int options) { int num_threads = Backend_getMaximumOfThreads(); - + mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); HWC_current_set = (int *)malloc(sizeof(int) * num_threads); ASSERT(HWC_current_set != NULL, "Cannot allocate memory for HWC_current_set"); memset (HWC_current_set, 0, sizeof(int) * num_threads); @@ -350,6 +356,7 @@ void HWC_Initialize (int options) HWC_current_glopsbegin = (unsigned long long *)malloc(sizeof(unsigned long long) * num_threads); ASSERT(HWC_current_glopsbegin != NULL, "Cannot allocate memory for HWC_current_glopsbegin"); + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); HWCBE_INITIALIZE(options); } @@ -422,6 +429,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) HWCEnabled = HWCBE_START_COUNTERS_THREAD (time, 0, forked); /* Inherit hwc set change values from thread 0 */ + mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); for (i = 1; i < num_threads; i++) { /* @@ -433,6 +441,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) HWC_current_timebegin[i] = HWC_current_timebegin[0]; HWC_current_glopsbegin[i] = HWC_current_glopsbegin[0]; } + mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } /** diff --git a/src/tracer/hwc/common_hwc.h b/src/tracer/hwc/common_hwc.h index da36ae8b..d99d202b 100644 --- a/src/tracer/hwc/common_hwc.h +++ b/src/tracer/hwc/common_hwc.h @@ -30,6 +30,7 @@ #include "num_hwc.h" #include "hwc_version.h" #include "hwc.h" +#include "pthread_redirect.h" /*------------------------------------------------ Structures ---------------*/ @@ -79,6 +80,8 @@ extern unsigned long long * HWC_current_glopsbegin; extern enum ChangeType_t HWC_current_changetype; extern int * HWC_current_set; +extern pthread_rwlock_t pThread_mtx_HWC_current_changetype; + /*------------------------------------------------ Useful macros ------------*/ /** diff --git a/src/tracer/hwc/papi_hwc.c b/src/tracer/hwc/papi_hwc.c index 2a35bd60..d1eccdcd 100644 --- a/src/tracer/hwc/papi_hwc.c +++ b/src/tracer/hwc/papi_hwc.c @@ -421,7 +421,9 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread return FALSE; HWC_current_changeat = HWC_sets[numset].change_at; + mtx_lock(&pThread_mtx_HWC_current_changetype); HWC_current_changetype = HWC_sets[numset].change_type; + mtx_unlock(&pThread_mtx_HWC_current_changetype); HWC_current_timebegin[threadid] = time; HWC_current_glopsbegin[threadid] = countglops; diff --git a/src/tracer/hwc/pmapi_hwc.c b/src/tracer/hwc/pmapi_hwc.c index c286c7df..fba2f12d 100644 --- a/src/tracer/hwc/pmapi_hwc.c +++ b/src/tracer/hwc/pmapi_hwc.c @@ -261,7 +261,9 @@ int HWCBE_PMAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int threa return FALSE; HWC_current_changeat = HWC_sets[numset].change_at; + mtx_lock(&pThread_mtx_HWC_current_changetype); HWC_current_changetype = HWC_sets[numset].change_type; + mtx_unlock(&pThread_mtx_HWC_current_changetype); HWC_current_timebegin[threadid] = time; HWC_current_glopsbegin[threadid] = countglops; diff --git a/src/tracer/wrappers/API/buffers.c b/src/tracer/wrappers/API/buffers.c index d13ce07f..9e875b82 100644 --- a/src/tracer/wrappers/API/buffers.c +++ b/src/tracer/wrappers/API/buffers.c @@ -57,6 +57,8 @@ #include "utils.h" #include "pthread_redirect.h" +extern pthread_rwlock_t pThread_mtx_Realloc; + #define EVENT_INDEX(buffer, event) (event - Buffer_GetFirst(buffer)) #define ALL_BITS_SET 0xFFFFFFFF @@ -76,12 +78,12 @@ static void DataBlocks_Free (DataBlocks_t *blocks); Buffer_t * new_Buffer (int n_events, char *file, int enable_cache) { Buffer_t *buffer = NULL; -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) #if 0 pthread_mutexattr_t attr; #endif int rc; -#endif +// #endif xmalloc(buffer, sizeof(Buffer_t)); buffer->FillCount = 0; @@ -117,7 +119,7 @@ Buffer_t * new_Buffer (int n_events, char *file, int enable_cache) } } -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) #if 0 pthread_mutexattr_init( &attr ); pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE_NP ); @@ -140,7 +142,7 @@ Buffer_t * new_Buffer (int n_events, char *file, int enable_cache) exit(1); } #endif -#endif +// #endif xmalloc(buffer->Masks, n_events * sizeof(Mask_t)); Mask_Wipe(buffer); @@ -164,9 +166,9 @@ void Buffer_Free (Buffer_t *buffer) if (buffer != NULL) { xfree (buffer->FirstEvt); -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) pthread_mutex_destroy(&(buffer->Lock)); -#endif +// #endif xfree (buffer->Masks); xfree (buffer->CachedEvents); @@ -356,20 +358,20 @@ event_t * Buffer_GetNext (Buffer_t *buffer, event_t *current) void Buffer_Lock (Buffer_t *buffer) { -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) mtx_lock(&(buffer->Lock)); -#else - UNREFERENCED_PARAMETER(buffer); -#endif +// #else +// UNREFERENCED_PARAMETER(buffer); +// #endif } void Buffer_Unlock (Buffer_t *buffer) { -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) mtx_unlock(&(buffer->Lock)); -#else - UNREFERENCED_PARAMETER(buffer); -#endif +// #else +// UNREFERENCED_PARAMETER(buffer); +// #endif } /* @@ -463,6 +465,7 @@ void Buffer_InsertSingle(Buffer_t *buffer, event_t *new_event) { #if defined(LOCK_AT_INSERT) Buffer_Lock (buffer); + mtx_rw_rdlock(&pThread_mtx_Realloc); #endif if (Buffer_IsFull (buffer)) @@ -481,6 +484,7 @@ void Buffer_InsertSingle(Buffer_t *buffer, event_t *new_event) buffer->FillCount ++; #if defined(LOCK_AT_INSERT) + mtx_rw_unlock(&pThread_mtx_Realloc); Buffer_Unlock (buffer); #endif } diff --git a/src/tracer/wrappers/API/buffers.h b/src/tracer/wrappers/API/buffers.h index bf3915f7..47d1d31a 100644 --- a/src/tracer/wrappers/API/buffers.h +++ b/src/tracer/wrappers/API/buffers.h @@ -39,7 +39,7 @@ #include "record.h" #define LOCK_AT_INSERT 1 -#define LOCK_AT_FLUSH 1 +//#define LOCK_AT_FLUSH 1 typedef int Mask_t; @@ -56,9 +56,9 @@ struct Buffer int fd; -#if defined(HAVE_ONLINE) +// #if defined(HAVE_ONLINE) pthread_mutex_t Lock; -#endif +// #endif Mask_t *Masks; int (*FlushCallback)(struct Buffer *); diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index 003c96f2..34eada3f 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -329,11 +329,13 @@ char tmp_dir[TMP_DIR_LEN]; int PENDING_TRACE_CPU_EVENT(int thread_id, iotimer_t current_time) { + mtx_rw_rdlock(&pThread_mtx_Realloc); if ((LastCPUEmissionTime[thread_id] == 0) || (((current_time - LastCPUEmissionTime[thread_id]) > MinimumCPUEventTime) && MinimumCPUEventTime > 0)) { LastCPUEmissionTime[thread_id] = current_time; + mtx_rw_unlock(&pThread_mtx_Realloc); return 1; } - + mtx_rw_unlock(&pThread_mtx_Realloc); return 0; } @@ -2670,6 +2672,7 @@ void Backend_Enter_Instrumentation () Backend_setInInstrumentation (thread, TRUE); /* Check if we have to fill the sampling buffer */ + mtx_rw_rdlock(&pThread_mtx_Realloc); #if defined(SAMPLING_SUPPORT) if (Extrae_get_DumpBuffersAtInstrumentation()) if (Buffer_IsFull (SAMPLING_BUFFER(THREADID))) @@ -2707,7 +2710,6 @@ void Backend_Enter_Instrumentation () #endif /* Check whether we will fill the buffer soon (or now) */ - mtx_rw_rdlock(&pThread_mtx_Realloc); if (Buffer_RemainingEvents(TracingBuffer[thread]) <= NEVENTS) Buffer_ExecuteFlushCallback (TracingBuffer[thread]); mtx_rw_unlock(&pThread_mtx_Realloc); diff --git a/src/tracer/wrappers/MPI/mpi_wrapper.c b/src/tracer/wrappers/MPI/mpi_wrapper.c index af479554..6cd9af41 100644 --- a/src/tracer/wrappers/MPI/mpi_wrapper.c +++ b/src/tracer/wrappers/MPI/mpi_wrapper.c @@ -343,8 +343,10 @@ static void Traceja_Persistent_Request (MPI_Request* reqid, iotimer_t temps) * tag : message tag or MPI_ANY_TAG commid: Communicator id * aux: request id */ + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); TRACE_MPIEVENT_NOHWC (temps, MPI_PERSIST_REQ_EV, p_request->tipus, src_world, size, p_request->tag, p_request->comm, p_request->req); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); } @@ -3116,8 +3118,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) if (cancel_flag) { // Communication was cancelled + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); TRACE_MPIEVENT_NOHWC (ts, MPI_REQUEST_CANCELLED_EV, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, request); - + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); CancelRequest(request); } else @@ -3136,8 +3139,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) getCommDataFromStatus(status, MPI_BYTE, request_data.commid, request_data.group, &size, &tag, &src_world); updateStats_P2P(global_mpi_stats, src_world, size, 0); - + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); TRACE_MPIEVENT_NOHWC (ts, MPI_IRECVED_EV, EMPTY, src_world, size, tag, request_data.commid, request); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); } else { @@ -3146,7 +3150,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) /* This case would also trigger if a receive request was not found in the hash (e.g. hash full) This should not happen unless there's errors in xtr_hash_add or we've missed instrumenting any recv calls. */ + mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); TRACE_MPIEVENT_NOHWC (ts, MPI_IRECVED_EV, EMPTY, EMPTY, EMPTY, status->MPI_TAG, EMPTY, request); + mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); } } } From 8f3b712a42e6f0fcaf2d1ede112892d39c423c15 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Mon, 30 Nov 2020 23:46:45 +0100 Subject: [PATCH 7/9] fixed type --- src/tracer/hwc/common_hwc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tracer/hwc/common_hwc.h b/src/tracer/hwc/common_hwc.h index d99d202b..5ff58122 100644 --- a/src/tracer/hwc/common_hwc.h +++ b/src/tracer/hwc/common_hwc.h @@ -80,7 +80,7 @@ extern unsigned long long * HWC_current_glopsbegin; extern enum ChangeType_t HWC_current_changetype; extern int * HWC_current_set; -extern pthread_rwlock_t pThread_mtx_HWC_current_changetype; +extern pthread_mutex_t pThread_mtx_HWC_current_changetype; /*------------------------------------------------ Useful macros ------------*/ From 05528e52001bdd55397562449ff792f6823d29c3 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Tue, 1 Dec 2020 19:56:01 +0100 Subject: [PATCH 8/9] hopefully fixed data races. at least for my workloads it works now without a crash or seg fault also with high number of threads --- src/tracer/calltrace.c | 10 ++- src/tracer/hwc/common_hwc.c | 64 +++++++------- src/tracer/hwc/papi_hwc.c | 4 +- src/tracer/mode.c | 18 ++-- src/tracer/mode.h | 2 - src/tracer/pthread_redirect.c | 111 +++++++++++++++++++----- src/tracer/wrappers/API/buffers.c | 6 +- src/tracer/wrappers/API/trace_buffers.h | 1 - src/tracer/wrappers/API/wrapper.c | 36 ++++---- src/tracer/wrappers/MPI/mpi_wrapper.c | 18 ++-- 10 files changed, 160 insertions(+), 110 deletions(-) diff --git a/src/tracer/calltrace.c b/src/tracer/calltrace.c index 386fe687..39e96c38 100644 --- a/src/tracer/calltrace.c +++ b/src/tracer/calltrace.c @@ -30,6 +30,8 @@ #include "common_hwc.h" #include "pthread_redirect.h" +extern pthread_rwlock_t pThread_mtx_change_number_threads; + //#define DEBUG //#define MPICALLER_DEBUG @@ -88,18 +90,18 @@ void Extrae_trace_callers (iotimer_t time, int offset, int type) { if (Trace_Caller[type][current_deep-offset]) { - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); TRACE_EVENT(time, CALLER_EVENT_TYPE(type, current_deep-offset+1), (UINT64)ip); - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } } #if defined(SAMPLING_SUPPORT) else if (type == CALLER_SAMPLING) { if (Trace_Caller[type][current_deep-offset]) { - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); SAMPLE_EVENT_NOHWC(time, SAMPLING_EV+current_deep-offset+1, (UINT64) ip); - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } } #endif diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index 18c4e0c0..c383cd73 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -53,8 +53,8 @@ #include #include "pthread_redirect.h" -static pthread_rwlock_t pThread_mtx_HWC_Start_Counters = PTHREAD_RWLOCK_INITIALIZER; pthread_mutex_t pThread_mtx_HWC_current_changetype = PTHREAD_MUTEX_INITIALIZER; +extern pthread_rwlock_t pThread_mtx_change_number_threads; /*------------------------------------------------ Global Variables ---------*/ int HWCEnabled = FALSE; /* Have the HWC been started? */ @@ -109,9 +109,9 @@ int HWC_IsEnabled() int HWC_Get_Current_Set (int threadid) { int ret; - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); ret = HWC_current_set[threadid]; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return ret; } @@ -204,10 +204,10 @@ void HWC_Stop_Current_Set (UINT64 time, int thread_id) /* make sure we don't loose the current counter values */ Extrae_counters_at_Time_Wrapper(time); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); /* Actually stop the counters */ HWCBE_STOP_SET (time, HWC_current_set[thread_id], thread_id); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } } @@ -220,10 +220,10 @@ void HWC_Start_Current_Set (UINT64 countglops, UINT64 time, int thread_id) /* If there are less than 2 sets, don't do anything! */ if (HWC_num_sets > 0) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); /* Actually start the counters */ HWCBE_START_SET (countglops, time, HWC_current_set[thread_id], thread_id); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } } @@ -239,13 +239,13 @@ void HWC_Start_Next_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); /* Move to the next set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = (HWC_current_set[thread_id] + 1) % HWC_num_sets; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -262,13 +262,13 @@ void HWC_Start_Previous_Set (UINT64 countglops, UINT64 time, int thread_id) { HWC_Stop_Current_Set (time, thread_id); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); /* Move to the previous set */ if (HWC_current_changeto == CHANGE_SEQUENTIAL) HWC_current_set[thread_id] = ((HWC_current_set[thread_id] - 1) < 0) ? (HWC_num_sets - 1) : (HWC_current_set[thread_id] - 1) ; else if (HWC_current_changeto == CHANGE_RANDOM) HWC_current_set[thread_id] = random()%HWC_num_sets; - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); HWC_Start_Current_Set (countglops, time, thread_id); } @@ -308,13 +308,13 @@ static inline int CheckForHWCSetChange_TIME (UINT64 countglops, UINT64 time, int int ret = 0; // fprintf (stderr, "HWC_current_timebegin[%d]=%llu HWC_current_changeat=%llu time = %llu\n", THREADID, HWC_current_timebegin[threadid], HWC_current_changeat, time); - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (HWC_current_timebegin[threadid] + HWC_current_changeat < time) { HWC_Start_Next_Set (countglops, time, threadid); ret = 1; } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return ret; } @@ -346,7 +346,7 @@ int HWC_Check_Pending_Set_Change (UINT64 countglops, UINT64 time, int thread_id) void HWC_Initialize (int options) { int num_threads = Backend_getMaximumOfThreads(); - mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_wrlock(&pThread_mtx_change_number_threads); HWC_current_set = (int *)malloc(sizeof(int) * num_threads); ASSERT(HWC_current_set != NULL, "Cannot allocate memory for HWC_current_set"); memset (HWC_current_set, 0, sizeof(int) * num_threads); @@ -356,7 +356,7 @@ void HWC_Initialize (int options) HWC_current_glopsbegin = (unsigned long long *)malloc(sizeof(unsigned long long) * num_threads); ASSERT(HWC_current_glopsbegin != NULL, "Cannot allocate memory for HWC_current_glopsbegin"); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); HWCBE_INITIALIZE(options); } @@ -396,7 +396,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) /* Allocate memory if this process has not been forked */ if (!forked) { - mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_wrlock(&pThread_mtx_change_number_threads); HWC_Thread_Initialized = (int *) malloc (sizeof(int) * num_threads); ASSERT(HWC_Thread_Initialized!=NULL, "Cannot allocate memory for HWC_Thread_Initialized!"); @@ -416,7 +416,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) ASSERT(Accumulated_HWC[i]!=NULL, "Cannot allocate memory for Accumulated_HWC"); HWC_Accum_Reset(i); } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); if (HWC_num_sets <= 0) { return; @@ -429,7 +429,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) HWCEnabled = HWCBE_START_COUNTERS_THREAD (time, 0, forked); /* Inherit hwc set change values from thread 0 */ - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); for (i = 1; i < num_threads; i++) { /* @@ -441,7 +441,7 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) HWC_current_timebegin[i] = HWC_current_timebegin[0]; HWC_current_glopsbegin[i] = HWC_current_glopsbegin[0]; } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } /** @@ -452,7 +452,6 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) void HWC_Restart_Counters (int old_num_threads, int new_num_threads) { int i; - mtx_rw_wrlock(&pThread_mtx_HWC_Start_Counters); #if defined(PAPI_COUNTERS) for (i = 0; i < HWC_num_sets; i++) HWCBE_PAPI_Allocate_eventsets_per_thread (i, old_num_threads, new_num_threads); @@ -493,7 +492,6 @@ void HWC_Restart_Counters (int old_num_threads, int new_num_threads) HWC_current_timebegin[i] = 0; HWC_current_glopsbegin[i] = 0; } - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); } /** @@ -617,16 +615,14 @@ int HWC_Read (unsigned int tid, UINT64 time, long long *store_buffer) if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( store_buffer ); read_ok = HWCBE_READ (tid, store_buffer); reset_ok = (Reset_After_Read ? HWCBE_RESET (tid) : TRUE); - mtx_rw_unlock(&pThread_mtx_Realloc); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } return (HWCEnabled && read_ok && reset_ok); } @@ -662,8 +658,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (!HWC_Thread_Initialized[tid]) HWCBE_START_COUNTERS_THREAD(time, tid, FALSE); TOUCH_LASTFIELD( Accumulated_HWC[tid] ); @@ -677,8 +672,7 @@ int HWC_Accum (unsigned int tid, UINT64 time) #endif Accumulated_HWC_Valid[tid] = TRUE; - mtx_rw_unlock(&pThread_mtx_Realloc); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } return (HWCEnabled && accum_ok); } @@ -703,9 +697,9 @@ int HWC_Accum_Reset (unsigned int tid) int HWC_Accum_Valid_Values (unsigned int tid) { int ret; - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); ret = ( HWCEnabled ? Accumulated_HWC_Valid[tid] : 0 ); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return ret; } @@ -718,9 +712,9 @@ int HWC_Accum_Copy_Here (unsigned int tid, long long *store_buffer) { if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); memcpy(store_buffer, Accumulated_HWC[tid], MAX_HWC * sizeof(long long)); - mtx_rw_unlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; } else return 0; @@ -736,12 +730,12 @@ int HWC_Accum_Add_Here (unsigned int tid, long long *store_buffer) int i; if (HWCEnabled) { - mtx_rw_rdlock(&pThread_mtx_HWC_Start_Counters); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); for (i=0; i int (*pthread_create_real)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*) = NULL; int (*pthread_join_real)(pthread_t,void**) = NULL; @@ -159,67 +160,129 @@ void GetpthreadHookPoints (int rank) // #endif /* PIC */ } +// #define DEBUG_PRINT_LOCKING + +#ifdef DEBUG_PRINT_LOCKING +typedef struct lock_idx { + char name[100]; +} lock_idx; + +__thread int current_lock_level[100]; +__thread lock_idx lock_assignments[100]; +__thread int cur_idx = -1; +__thread int current_lock_level_init = 0; + +static void init_lock_level(){ + int i; + for(i = 0; i < 100; i++) { + current_lock_level[i] = 0; + } + current_lock_level_init = 1; +} + +static int get_lock_idx(const char *name) { + if (!current_lock_level_init) + init_lock_level(); + + int i; + for(i = 0; i <= cur_idx; i++) { + if(strcmp(name, lock_assignments[i].name) == 0) { + return i; + } + } + + int tmp_idx = ++cur_idx; + strncpy(lock_assignments[tmp_idx].name, name, strlen(name)); + return tmp_idx; +} +#endif //DEBUG_PRINT_LOCKING + void mtx_lock_caller(pthread_mutex_t* lock, char* name, char const * caller_name) { if(pthread_mutex_lock_real == NULL) GetpthreadHookPoints(0); +// #ifdef DEBUG_PRINT_LOCKING +// int tmp_idx = get_lock_idx(name); +// char cur_host_name[100]; +// cur_host_name[99] = '\0'; +// gethostname(cur_host_name, 99); +// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_lock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +// #endif pthread_mutex_lock_real(lock); - - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - - //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_wrlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); +// #ifdef DEBUG_PRINT_LOCKING +// current_lock_level[tmp_idx]++; +// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_lock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +// #endif } void mtx_unlock_caller(pthread_mutex_t* lock, char* name, char const * caller_name){ // if(pthread_mutex_unlock_real == NULL) // GetpthreadHookPoints(0); +// #ifdef DEBUG_PRINT_LOCKING +// int tmp_idx = get_lock_idx(name); +// char cur_host_name[100]; +// cur_host_name[99] = '\0'; +// gethostname(cur_host_name, 99); +// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_unlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +// #endif pthread_mutex_unlock_real(lock); - - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - - //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_wrlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); +// #ifdef DEBUG_PRINT_LOCKING +// current_lock_level[tmp_idx]--; +// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_unlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +// #endif } void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { if(pthread_rwlock_wrlock_real == NULL) GetpthreadHookPoints(0); - pthread_rwlock_wrlock_real(lock); - +#ifdef DEBUG_PRINT_LOCKING + int tmp_idx = get_lock_idx(name); char cur_host_name[100]; cur_host_name[99] = '\0'; gethostname(cur_host_name, 99); - - //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_wrlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_wrlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif + pthread_rwlock_wrlock_real(lock); +#ifdef DEBUG_PRINT_LOCKING + current_lock_level[tmp_idx]++; + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_wrlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif } void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { if(pthread_rwlock_rdlock_real == NULL) GetpthreadHookPoints(0); - pthread_rwlock_rdlock_real(lock); - +#ifdef DEBUG_PRINT_LOCKING + int tmp_idx = get_lock_idx(name); char cur_host_name[100]; cur_host_name[99] = '\0'; gethostname(cur_host_name, 99); - - //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_rdlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_rdlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif + pthread_rwlock_rdlock_real(lock); +#ifdef DEBUG_PRINT_LOCKING + current_lock_level[tmp_idx]++; + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_rdlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif } void mtx_rw_unlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { // if(pthread_rwlock_unlock_real == NULL) // GetpthreadHookPoints(0); - pthread_rwlock_unlock_real(lock); - +#ifdef DEBUG_PRINT_LOCKING + int tmp_idx = get_lock_idx(name); char cur_host_name[100]; cur_host_name[99] = '\0'; gethostname(cur_host_name, 99); - - //fprintf(stderr, "DEBUG_LOCK\t%s OS_TID:%ld\t%s\tmtx_rw_unlock\t%s\n", cur_host_name, syscall(SYS_gettid), name, caller_name); + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_unlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif + pthread_rwlock_unlock_real(lock); +#ifdef DEBUG_PRINT_LOCKING + current_lock_level[tmp_idx]--; + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_unlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); +#endif } \ No newline at end of file diff --git a/src/tracer/wrappers/API/buffers.c b/src/tracer/wrappers/API/buffers.c index 9e875b82..fd210d2b 100644 --- a/src/tracer/wrappers/API/buffers.c +++ b/src/tracer/wrappers/API/buffers.c @@ -57,7 +57,7 @@ #include "utils.h" #include "pthread_redirect.h" -extern pthread_rwlock_t pThread_mtx_Realloc; +extern pthread_rwlock_t pThread_mtx_change_number_threads; #define EVENT_INDEX(buffer, event) (event - Buffer_GetFirst(buffer)) #define ALL_BITS_SET 0xFFFFFFFF @@ -465,7 +465,7 @@ void Buffer_InsertSingle(Buffer_t *buffer, event_t *new_event) { #if defined(LOCK_AT_INSERT) Buffer_Lock (buffer); - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); #endif if (Buffer_IsFull (buffer)) @@ -484,7 +484,7 @@ void Buffer_InsertSingle(Buffer_t *buffer, event_t *new_event) buffer->FillCount ++; #if defined(LOCK_AT_INSERT) - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); Buffer_Unlock (buffer); #endif } diff --git a/src/tracer/wrappers/API/trace_buffers.h b/src/tracer/wrappers/API/trace_buffers.h index 6be94ee4..a71a215e 100644 --- a/src/tracer/wrappers/API/trace_buffers.h +++ b/src/tracer/wrappers/API/trace_buffers.h @@ -31,7 +31,6 @@ /* Don't like these externs -> Declare fetch functions in wrapper.c and include prototypes in wrapper.h ? */ extern Buffer_t **TracingBuffer; extern Buffer_t **SamplingBuffer; -extern pthread_rwlock_t pThread_mtx_Realloc; #if defined(__cplusplus) extern "C" { diff --git a/src/tracer/wrappers/API/wrapper.c b/src/tracer/wrappers/API/wrapper.c index 34eada3f..19419444 100644 --- a/src/tracer/wrappers/API/wrapper.c +++ b/src/tracer/wrappers/API/wrapper.c @@ -166,11 +166,9 @@ int Extrae_Flush_Wrapper (Buffer_t *buffer); static int requestedDynamicMemoryInstrumentation = FALSE; -static pthread_mutex_t pThreadChangeNumberOfThreads_mtx = PTHREAD_MUTEX_INITIALIZER; +pthread_rwlock_t pThread_mtx_change_number_threads = PTHREAD_RWLOCK_INITIALIZER; -pthread_rwlock_t pThread_mtx_Realloc = PTHREAD_RWLOCK_INITIALIZER; static pthread_rwlock_t pThread_mtx_Extrae_inInstrumentation = PTHREAD_RWLOCK_INITIALIZER; -extern pthread_rwlock_t pThread_mtx_Trace_Mode_reInitialize; #if defined(STANDALONE) module_t *Modules = NULL; @@ -329,13 +327,13 @@ char tmp_dir[TMP_DIR_LEN]; int PENDING_TRACE_CPU_EVENT(int thread_id, iotimer_t current_time) { - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if ((LastCPUEmissionTime[thread_id] == 0) || (((current_time - LastCPUEmissionTime[thread_id]) > MinimumCPUEventTime) && MinimumCPUEventTime > 0)) { LastCPUEmissionTime[thread_id] = current_time; - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; } - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } @@ -502,13 +500,17 @@ void Extrae_AnnotateCPU (UINT64 timestamp) #if defined(HAVE_SCHED_GETCPU) int cpu = sched_getcpu(); - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); + int unlocked = 0; if (cpu != LastCPUEvent[THREADID] || AlwaysEmitCPUEvent) { LastCPUEvent[THREADID] = cpu; + mtx_rw_unlock(&pThread_mtx_change_number_threads); + unlocked = 1; TRACE_EVENT (timestamp, GETCPU_EV, cpu); } - mtx_rw_unlock(&pThread_mtx_Realloc); + if(!unlocked) + mtx_rw_unlock(&pThread_mtx_change_number_threads); #else UNREFERENCED_PARAMETER(timestamp); #endif @@ -1439,7 +1441,6 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork UNREFERENCED_PARAMETER(world_size); #endif - mtx_rw_wrlock(&pThread_mtx_Realloc); if (!forked) { xmalloc(TracingBuffer, num_threads * sizeof(Buffer_t *)); @@ -1452,7 +1453,6 @@ static int Allocate_buffers_and_files (int world_size, int num_threads, int fork for (i = 0; i < num_threads; i++) Allocate_buffer_and_file (i, forked); - mtx_rw_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1465,7 +1465,6 @@ static int Reallocate_buffers_and_files (int new_num_threads) { int i; - mtx_rw_wrlock(&pThread_mtx_Realloc); xrealloc(TracingBuffer, TracingBuffer, new_num_threads * sizeof(Buffer_t *)); xrealloc(LastCPUEmissionTime, LastCPUEmissionTime, new_num_threads * sizeof(iotimer_t)); xrealloc(LastCPUEvent, LastCPUEvent, new_num_threads * sizeof(int)); @@ -1474,7 +1473,6 @@ static int Reallocate_buffers_and_files (int new_num_threads) #endif for (i = get_maximum_NumOfThreads(); i < new_num_threads; i++) Allocate_buffer_and_file (i, FALSE); - mtx_rw_unlock(&pThread_mtx_Realloc); return TRUE; } @@ -1979,7 +1977,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) { unsigned new_num_threads = numberofthreads; - mtx_lock(&pThreadChangeNumberOfThreads_mtx); + mtx_rw_wrlock(&pThread_mtx_change_number_threads); if (EXTRAE_INITIALIZED()) { @@ -2050,7 +2048,7 @@ int Backend_ChangeNumberOfThreads (unsigned numberofthreads) current_NumOfThreads = new_num_threads; } - mtx_unlock(&pThreadChangeNumberOfThreads_mtx); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return TRUE; } @@ -2672,7 +2670,7 @@ void Backend_Enter_Instrumentation () Backend_setInInstrumentation (thread, TRUE); /* Check if we have to fill the sampling buffer */ - mtx_rw_rdlock(&pThread_mtx_Realloc); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); #if defined(SAMPLING_SUPPORT) if (Extrae_get_DumpBuffersAtInstrumentation()) if (Buffer_IsFull (SAMPLING_BUFFER(THREADID))) @@ -2712,14 +2710,14 @@ void Backend_Enter_Instrumentation () /* Check whether we will fill the buffer soon (or now) */ if (Buffer_RemainingEvents(TracingBuffer[thread]) <= NEVENTS) Buffer_ExecuteFlushCallback (TracingBuffer[thread]); - mtx_rw_unlock(&pThread_mtx_Realloc); + mtx_rw_unlock(&pThread_mtx_change_number_threads); /* Record the time when this is happening we need this for subsequent calls to TIME, as this is being cached in clock routines */ current_time = TIME; if (Trace_Mode_FirstMode(thread)) - Trace_Mode_Change (thread, current_time); + Trace_Mode_Change (thread, current_time); #if defined(PAPI_COUNTERS) || defined(PMAPI_COUNTERS) /* Must change counters? check only at detail tracing, at bursty @@ -2745,10 +2743,10 @@ void Backend_Leave_Instrumentation (void) } /* Change trace mode? (issue from API) */ - mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (PENDING_TRACE_MODE_CHANGE(thread) && MPI_Deepness[thread] == 0) Trace_Mode_Change(thread, LAST_READ_TIME); - mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_change_number_threads); Backend_setInInstrumentation (thread, FALSE); } diff --git a/src/tracer/wrappers/MPI/mpi_wrapper.c b/src/tracer/wrappers/MPI/mpi_wrapper.c index 6cd9af41..bab17c66 100644 --- a/src/tracer/wrappers/MPI/mpi_wrapper.c +++ b/src/tracer/wrappers/MPI/mpi_wrapper.c @@ -85,6 +85,8 @@ # define MPI_F_STATUSES_IGNORE ((MPI_Fint *) 0) #endif +extern pthread_rwlock_t pThread_mtx_change_number_threads; + /* He d'incloure la capc,alera del misc_wrapper per poder comenc,ar a tracejar quan es cridi al MPI_init i acabar al MPI_Finalize. @@ -343,10 +345,10 @@ static void Traceja_Persistent_Request (MPI_Request* reqid, iotimer_t temps) * tag : message tag or MPI_ANY_TAG commid: Communicator id * aux: request id */ - mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); TRACE_MPIEVENT_NOHWC (temps, MPI_PERSIST_REQ_EV, p_request->tipus, src_world, size, p_request->tag, p_request->comm, p_request->req); - mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } @@ -3118,9 +3120,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) if (cancel_flag) { // Communication was cancelled - mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); TRACE_MPIEVENT_NOHWC (ts, MPI_REQUEST_CANCELLED_EV, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, request); - mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_change_number_threads); CancelRequest(request); } else @@ -3139,9 +3141,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) getCommDataFromStatus(status, MPI_BYTE, request_data.commid, request_data.group, &size, &tag, &src_world); updateStats_P2P(global_mpi_stats, src_world, size, 0); - mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); TRACE_MPIEVENT_NOHWC (ts, MPI_IRECVED_EV, EMPTY, src_world, size, tag, request_data.commid, request); - mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } else { @@ -3150,9 +3152,9 @@ void ProcessRequest(iotimer_t ts, MPI_Request request, MPI_Status *status) /* This case would also trigger if a receive request was not found in the hash (e.g. hash full) This should not happen unless there's errors in xtr_hash_add or we've missed instrumenting any recv calls. */ - mtx_rw_rdlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); TRACE_MPIEVENT_NOHWC (ts, MPI_IRECVED_EV, EMPTY, EMPTY, EMPTY, status->MPI_TAG, EMPTY, request); - mtx_rw_unlock(&pThread_mtx_Trace_Mode_reInitialize); + mtx_rw_unlock(&pThread_mtx_change_number_threads); } } } From 7a154cb30179e39b5a98a08b3c3a4e240ff5da60 Mon Sep 17 00:00:00 2001 From: Jannis Klinkenberg Date: Wed, 6 Jan 2021 21:36:20 +0100 Subject: [PATCH 9/9] some code cleaning an reduction of different locks --- src/tracer/hwc/common_hwc.c | 4 - src/tracer/hwc/papi_hwc.c | 38 ++---- src/tracer/pthread_redirect.c | 126 +++++++++--------- src/tracer/pthread_redirect.h | 10 +- src/tracer/wrappers/pthread/pthread_wrapper.c | 2 +- 5 files changed, 84 insertions(+), 96 deletions(-) diff --git a/src/tracer/hwc/common_hwc.c b/src/tracer/hwc/common_hwc.c index c383cd73..9e736a3c 100644 --- a/src/tracer/hwc/common_hwc.c +++ b/src/tracer/hwc/common_hwc.c @@ -346,7 +346,6 @@ int HWC_Check_Pending_Set_Change (UINT64 countglops, UINT64 time, int thread_id) void HWC_Initialize (int options) { int num_threads = Backend_getMaximumOfThreads(); - mtx_rw_wrlock(&pThread_mtx_change_number_threads); HWC_current_set = (int *)malloc(sizeof(int) * num_threads); ASSERT(HWC_current_set != NULL, "Cannot allocate memory for HWC_current_set"); memset (HWC_current_set, 0, sizeof(int) * num_threads); @@ -356,7 +355,6 @@ void HWC_Initialize (int options) HWC_current_glopsbegin = (unsigned long long *)malloc(sizeof(unsigned long long) * num_threads); ASSERT(HWC_current_glopsbegin != NULL, "Cannot allocate memory for HWC_current_glopsbegin"); - mtx_rw_unlock(&pThread_mtx_change_number_threads); HWCBE_INITIALIZE(options); } @@ -396,7 +394,6 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) /* Allocate memory if this process has not been forked */ if (!forked) { - mtx_rw_wrlock(&pThread_mtx_change_number_threads); HWC_Thread_Initialized = (int *) malloc (sizeof(int) * num_threads); ASSERT(HWC_Thread_Initialized!=NULL, "Cannot allocate memory for HWC_Thread_Initialized!"); @@ -416,7 +413,6 @@ void HWC_Start_Counters (int num_threads, UINT64 time, int forked) ASSERT(Accumulated_HWC[i]!=NULL, "Cannot allocate memory for Accumulated_HWC"); HWC_Accum_Reset(i); } - mtx_rw_unlock(&pThread_mtx_change_number_threads); if (HWC_num_sets <= 0) { return; diff --git a/src/tracer/hwc/papi_hwc.c b/src/tracer/hwc/papi_hwc.c index eadb5f63..658cecce 100644 --- a/src/tracer/hwc/papi_hwc.c +++ b/src/tracer/hwc/papi_hwc.c @@ -71,7 +71,7 @@ static HWC_Definition_t *hwc_used = NULL; static unsigned num_hwc_used = 0; -static pthread_rwlock_t pThread_mtx_HWC_sets = PTHREAD_RWLOCK_INITIALIZER; +extern pthread_rwlock_t pThread_mtx_change_number_threads; static void HWCBE_PAPI_AddDefinition (unsigned event_code, char *code, char *description) { @@ -111,18 +111,15 @@ int HWCBE_PAPI_Allocate_eventsets_per_thread (int num_set, int old_thread_num, i { int i; - //mtx_rw_wrlock(&pThread_mtx_HWC_sets); HWC_sets[num_set].eventsets = (int *) realloc (HWC_sets[num_set].eventsets, sizeof(int)*new_thread_num); if (HWC_sets[num_set].eventsets == NULL) { fprintf (stderr, PACKAGE_NAME": Cannot allocate memory for HWC_set\n"); - // mtx_rw_unlock(&pThread_mtx_HWC_sets); return FALSE; } for (i = old_thread_num; i < new_thread_num; i++) HWC_sets[num_set].eventsets[i] = PAPI_NULL; - //mtx_rw_unlock(&pThread_mtx_HWC_sets); return TRUE; } @@ -214,15 +211,12 @@ int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **count ncounters = MAX_HWC; } - mtx_rw_wrlock(&pThread_mtx_HWC_sets); HWC_sets = (struct HWC_Set_t *) realloc (HWC_sets, sizeof(struct HWC_Set_t)* (HWC_num_sets+1)); if (HWC_sets == NULL) { fprintf (stderr, PACKAGE_NAME": Cannot allocate memory for HWC_set (rank %d)\n", rank); - mtx_rw_unlock(&pThread_mtx_HWC_sets); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets); /* Initialize this set */ HWC_sets[num_set].num_counters = 0; @@ -413,7 +407,6 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread #endif int rc; - //mtx_rw_rdlock(&pThread_mtx_HWC_sets); /* The given set is a valid one? */ if (numset < 0 || numset >= HWC_num_sets) return FALSE; @@ -475,7 +468,6 @@ int HWCBE_PAPI_Start_Set (UINT64 countglops, UINT64 time, int numset, int thread fprintf (stderr, PACKAGE_NAME": errno = %d\n", errno); } } - //mtx_rw_unlock(&pThread_mtx_HWC_sets); return rc == PAPI_OK; } @@ -513,7 +505,6 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) int state; int i; unsigned t; - mtx_rw_wrlock(&pThread_mtx_HWC_sets); if (PAPI_state (HWCEVTSET(THREADID), &state) == PAPI_OK) { if (state & PAPI_RUNNING) @@ -546,7 +537,6 @@ void HWCBE_PAPI_CleanUp (unsigned nthreads) } #endif xfree (HWC_sets); - mtx_rw_unlock(&pThread_mtx_HWC_sets); PAPI_shutdown(); } } @@ -614,7 +604,6 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) if (HWC_num_sets <= 0) return FALSE; - //mtx_rw_rdlock(&pThread_mtx_HWC_sets); if (forked) { PAPI_stop (HWCEVTSET(threadid), NULL); @@ -676,7 +665,6 @@ int HWCBE_PAPI_Init_Thread (UINT64 time, int threadid, int forked) Extrae_IntelPEBS_startSampling(); #endif int ret = HWC_Thread_Initialized[threadid]; - //mtx_rw_unlock(&pThread_mtx_HWC_sets); return ret; } @@ -685,7 +673,7 @@ int __in_PAPI_read_BG = FALSE; #endif int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); int EventSet = HWCEVTSET(tid); #if !defined(IS_BG_MACHINE) @@ -693,10 +681,10 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; #else if (!__in_PAPI_read_BG) @@ -706,15 +694,15 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) { fprintf (stderr, PACKAGE_NAME": PAPI_read failed for thread %d evtset %d (%s:%d)\n", tid, EventSet, __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } __in_PAPI_read_BG = FALSE; - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; } else { - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } #endif @@ -722,29 +710,29 @@ int HWCBE_PAPI_Read (unsigned int tid, long long *store_buffer) int HWCBE_PAPI_Reset (unsigned int tid) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (PAPI_reset(HWCEVTSET(tid)) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_reset failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; } int HWCBE_PAPI_Accum (unsigned int tid, long long *store_buffer) { - mtx_rw_rdlock(&pThread_mtx_HWC_sets); + mtx_rw_rdlock(&pThread_mtx_change_number_threads); if (PAPI_accum(HWCEVTSET(tid), store_buffer) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_accum failed for thread %d evtset %d (%s:%d)\n", \ tid, HWCEVTSET(tid), __FILE__, __LINE__); - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 0; } - mtx_rw_unlock(&pThread_mtx_HWC_sets); + mtx_rw_unlock(&pThread_mtx_change_number_threads); return 1; } diff --git a/src/tracer/pthread_redirect.c b/src/tracer/pthread_redirect.c index f27bb139..b65d3b5f 100644 --- a/src/tracer/pthread_redirect.c +++ b/src/tracer/pthread_redirect.c @@ -50,8 +50,7 @@ int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *) = NULL; void GetpthreadHookPoints (int rank) { -// #if defined(PIC) - +#if defined(PIC) /* Obtain @ for pthread_create */ pthread_create_real = (int(*)(pthread_t*,const pthread_attr_t*,void *(*) (void *),void*)) @@ -155,27 +154,34 @@ void GetpthreadHookPoints (int rank) pthread_rwlock_unlock_real = (int(*)(pthread_rwlock_t*)) dlsym (RTLD_NEXT, "pthread_rwlock_unlock"); if (pthread_rwlock_unlock_real == NULL && rank == 0) fprintf (stderr, "Unable to find pthread_rwlock_unlock in DSOs!!\n"); -// #else - // fprintf (stderr, "Warning! pthread instrumentation requires linking with shared library!\n"); -// #endif /* PIC */ +#else + fprintf (stderr, "Warning! pthread instrumentation requires linking with shared library!\n"); +#endif /* PIC */ } // #define DEBUG_PRINT_LOCKING #ifdef DEBUG_PRINT_LOCKING +#define LOCK_LVL_PADDING_FACTOR 1 +#define LOCK_LVL_NUM_LOCKS 200 +#define LOCK_LVL_MAX_LEN_HOSTNAME 100 + +static pthread_mutex_t mtx_output_stderr = PTHREAD_MUTEX_INITIALIZER; + typedef struct lock_idx { - char name[100]; + char name[LOCK_LVL_MAX_LEN_HOSTNAME]; + // more features? } lock_idx; -__thread int current_lock_level[100]; -__thread lock_idx lock_assignments[100]; +__thread int current_lock_level[LOCK_LVL_NUM_LOCKS*LOCK_LVL_PADDING_FACTOR]; // use padding to avoid false sharing +__thread lock_idx lock_assignments[LOCK_LVL_NUM_LOCKS*LOCK_LVL_PADDING_FACTOR]; // use padding to avoid false sharing __thread int cur_idx = -1; __thread int current_lock_level_init = 0; static void init_lock_level(){ int i; - for(i = 0; i < 100; i++) { - current_lock_level[i] = 0; + for(i = 0; i < LOCK_LVL_NUM_LOCKS; i++) { + current_lock_level[i*LOCK_LVL_PADDING_FACTOR] = 0; } current_lock_level_init = 1; } @@ -186,103 +192,101 @@ static int get_lock_idx(const char *name) { int i; for(i = 0; i <= cur_idx; i++) { - if(strcmp(name, lock_assignments[i].name) == 0) { + if(strcmp(name, lock_assignments[i*LOCK_LVL_PADDING_FACTOR].name) == 0) { return i; } } int tmp_idx = ++cur_idx; - strncpy(lock_assignments[tmp_idx].name, name, strlen(name)); + strncpy(lock_assignments[tmp_idx*LOCK_LVL_PADDING_FACTOR].name, name, strlen(name)); return tmp_idx; } -#endif //DEBUG_PRINT_LOCKING -void mtx_lock_caller(pthread_mutex_t* lock, char* name, char const * caller_name) { +static void mtx_print_message(const char *lock_type, const char *lock_name, int lock_name_idx, const char *caller_name, int before) { + char cur_host_name[LOCK_LVL_MAX_LEN_HOSTNAME]; + cur_host_name[LOCK_LVL_MAX_LEN_HOSTNAME-1] = '\0'; + gethostname(cur_host_name, LOCK_LVL_MAX_LEN_HOSTNAME-1); + pthread_mutex_lock_real(&mtx_output_stderr); + if(before) { + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\t%s\tBEFORE\tidx=%d\tLvl=%d\tCaller=%s\n", cur_host_name, syscall(SYS_gettid), lock_type, lock_name, lock_name_idx, current_lock_level[lock_name_idx], caller_name); + } else { + fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\t%s\tAFTER\tidx=%d\tLvl=%d\tCaller=%s\n", cur_host_name, syscall(SYS_gettid), lock_type, lock_name, lock_name_idx, current_lock_level[lock_name_idx], caller_name); + } + fflush(stderr); + pthread_mutex_unlock_real(&mtx_output_stderr); +} +#endif /* DEBUG_PRINT_LOCKING */ + +void mtx_lock_caller(pthread_mutex_t* lock, const char *name, const char *caller_name) { if(pthread_mutex_lock_real == NULL) GetpthreadHookPoints(0); -// #ifdef DEBUG_PRINT_LOCKING -// int tmp_idx = get_lock_idx(name); -// char cur_host_name[100]; -// cur_host_name[99] = '\0'; -// gethostname(cur_host_name, 99); -// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_lock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); -// #endif +#ifdef DEBUG_PRINT_LOCKING + const char *lock_type = "mtx_lock"; + int tmp_idx = get_lock_idx(name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 1); +#endif pthread_mutex_lock_real(lock); -// #ifdef DEBUG_PRINT_LOCKING -// current_lock_level[tmp_idx]++; -// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_lock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); -// #endif +#ifdef DEBUG_PRINT_LOCKING + current_lock_level[tmp_idx]++; + mtx_print_message(lock_type, name, tmp_idx, caller_name, 0); +#endif } -void mtx_unlock_caller(pthread_mutex_t* lock, char* name, char const * caller_name){ - // if(pthread_mutex_unlock_real == NULL) - // GetpthreadHookPoints(0); - -// #ifdef DEBUG_PRINT_LOCKING -// int tmp_idx = get_lock_idx(name); -// char cur_host_name[100]; -// cur_host_name[99] = '\0'; -// gethostname(cur_host_name, 99); -// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_unlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); -// #endif +void mtx_unlock_caller(pthread_mutex_t* lock, const char *name, const char *caller_name){ +#ifdef DEBUG_PRINT_LOCKING + const char *lock_type = "mtx_unlock"; + int tmp_idx = get_lock_idx(name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 1); +#endif pthread_mutex_unlock_real(lock); -// #ifdef DEBUG_PRINT_LOCKING -// current_lock_level[tmp_idx]--; -// fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_unlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); -// #endif +#ifdef DEBUG_PRINT_LOCKING + current_lock_level[tmp_idx]--; + mtx_print_message(lock_type, name, tmp_idx, caller_name, 0); +#endif } -void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { +void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name) { if(pthread_rwlock_wrlock_real == NULL) GetpthreadHookPoints(0); #ifdef DEBUG_PRINT_LOCKING + const char *lock_type = "mtx_rw_wrlock"; int tmp_idx = get_lock_idx(name); - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_wrlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 1); #endif pthread_rwlock_wrlock_real(lock); #ifdef DEBUG_PRINT_LOCKING current_lock_level[tmp_idx]++; - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_wrlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 0); #endif } -void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { +void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name) { if(pthread_rwlock_rdlock_real == NULL) GetpthreadHookPoints(0); #ifdef DEBUG_PRINT_LOCKING + const char *lock_type = "mtx_rw_rdlock"; int tmp_idx = get_lock_idx(name); - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_rdlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 1); #endif pthread_rwlock_rdlock_real(lock); #ifdef DEBUG_PRINT_LOCKING current_lock_level[tmp_idx]++; - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_rdlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 0); #endif } -void mtx_rw_unlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name) { - // if(pthread_rwlock_unlock_real == NULL) - // GetpthreadHookPoints(0); - +void mtx_rw_unlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name) { #ifdef DEBUG_PRINT_LOCKING + const char *lock_type = "mtx_rw_unlock"; int tmp_idx = get_lock_idx(name); - char cur_host_name[100]; - cur_host_name[99] = '\0'; - gethostname(cur_host_name, 99); - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_unlock\tBEFORE\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 1); #endif pthread_rwlock_unlock_real(lock); #ifdef DEBUG_PRINT_LOCKING current_lock_level[tmp_idx]--; - fprintf(stderr, "DEBUG_LOCK\t%s\tOS_TID:\t%ld\t%s\tmtx_rw_unlock\tAFTER\t%d\t%s\n", cur_host_name, syscall(SYS_gettid), name, current_lock_level[tmp_idx], caller_name); + mtx_print_message(lock_type, name, tmp_idx, caller_name, 0); #endif } \ No newline at end of file diff --git a/src/tracer/pthread_redirect.h b/src/tracer/pthread_redirect.h index 49e5b02c..efe9dafd 100644 --- a/src/tracer/pthread_redirect.h +++ b/src/tracer/pthread_redirect.h @@ -59,15 +59,15 @@ extern int (*pthread_rwlock_unlock_real)(pthread_rwlock_t *); int GetpThreadIdentifier (void); // ============================= Locking helper =================================== -void mtx_lock_caller(pthread_mutex_t* lock, char* name, char const * caller_name); +void mtx_lock_caller(pthread_mutex_t* lock, const char *name, const char *caller_name); -void mtx_unlock_caller(pthread_mutex_t* lock, char* name, char const * caller_name); +void mtx_unlock_caller(pthread_mutex_t* lock, const char *name, const char *caller_name); -void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); +void mtx_rw_wrlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name); -void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); +void mtx_rw_rdlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name); -void mtx_rw_unlock_caller(pthread_rwlock_t* lock, char* name, char const * caller_name); +void mtx_rw_unlock_caller(pthread_rwlock_t* lock, const char *name, const char *caller_name); #ifndef mtx_lock #define mtx_lock(mtx) mtx_lock_caller(mtx, #mtx, __func__) diff --git a/src/tracer/wrappers/pthread/pthread_wrapper.c b/src/tracer/wrappers/pthread/pthread_wrapper.c index 5e857dd5..5a8436dc 100644 --- a/src/tracer/wrappers/pthread/pthread_wrapper.c +++ b/src/tracer/wrappers/pthread/pthread_wrapper.c @@ -56,7 +56,7 @@ */ -static pthread_rwlock_t extrae_pthread_create_mutex = PTHREAD_RWLOCK_INITIALIZER; +static pthread_mutex_t extrae_pthread_create_mutex = PTHREAD_MUTEX_INITIALIZER; #if defined(PIC)