Skip to content

Commit

Permalink
repair sigtimedwait profiling
Browse files Browse the repository at this point in the history
also fix race cond. in thread init (_julia_init could read thread state before it was setup)
  • Loading branch information
carnaval committed Sep 18, 2015
1 parent 4971105 commit a38fee2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,20 @@ void usr2_handler(int sig, siginfo_t *info, void *ctx)
}
}

#if defined(HAVE_TIMEDSIGWAIT)
#if defined(HAVE_SIGTIMEDWAIT)

static struct timespec timeoutprof;
DLLEXPORT int jl_profile_start_timer(void)
{
timeoutprof.tv_sec = nsecprof/GIGA;
timeoutprof.tv_nsec = nsecprof%GIGA;
pthread_kill(signals_thread, SIGUSR2); // notify signal handler to start timer
return 0;
}

DLLEXPORT void jl_profile_stop_timer(void)
{
pthread_kill(signals_thread, SIGUSR2);
}

#elif defined(HAVE_TIMER)
Expand Down Expand Up @@ -325,14 +331,14 @@ static void *signal_listener(void *arg)
int sig, critical, profile;
int i;
jl_sigsetset(&sset);
#if defined(HAVE_TIMEDSIGWAIT)
siginfo_t *info;
sigaddset(sset, SIGUSR2);
#ifdef HAVE_SIGTIMEDWAIT
siginfo_t info;
sigaddset(&sset, SIGUSR2);
sigprocmask(SIG_SETMASK, &sset, 0);
#endif
while (1) {
profile = 0;
#if HAVE_TIMEDSIGWAIT
#ifdef HAVE_SIGTIMEDWAIT
if (running) {
sig = sigtimedwait(&sset, &info, &timeoutprof);
}
Expand All @@ -343,15 +349,16 @@ static void *signal_listener(void *arg)
int err = errno;
if (err == EAGAIN) {
// this was a timeout event
profile = true;
profile = 1;
}
else {
assert(err == EINTR);
continue;
}
}
else if (sig == SIGUSR2) {
// notification to start profiler
// notification to toggle profiler
running = !running;
continue;
}
#else
Expand Down
9 changes: 8 additions & 1 deletion src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ static jl_value_t *ti_run_fun(jl_function_t *f, jl_svec_t *args)
return jl_nothing;
}

static uv_barrier_t thread_init_done;

// thread function: used by all except the main thread
void ti_threadfun(void *arg)
{
Expand All @@ -177,7 +179,7 @@ void ti_threadfun(void *arg)
while (ta->state == TI_THREAD_INIT)
cpu_pause();
cpu_lfence();

uv_barrier_wait(&thread_init_done);
// initialize this thread in the thread group
tg = ta->tg;
ti_threadgroup_initthread(tg, ti_tid);
Expand Down Expand Up @@ -295,6 +297,9 @@ void jl_start_threads(void)
jl_all_task_states[0].system_id = pthread_self();
#endif
targs = malloc((jl_n_threads - 1) * sizeof (ti_threadarg_t *));

uv_barrier_init(&thread_init_done, jl_n_threads);

for (i = 0; i < jl_n_threads - 1; ++i) {
targs[i] = (ti_threadarg_t *)malloc(sizeof (ti_threadarg_t));
targs[i]->state = TI_THREAD_INIT;
Expand All @@ -316,6 +321,8 @@ void jl_start_threads(void)
targs[i]->state = TI_THREAD_WORK;
}

uv_barrier_wait(&thread_init_done);

// free the argument array; the threads will free their arguments
free(targs);
}
Expand Down

0 comments on commit a38fee2

Please sign in to comment.