Skip to content

Commit

Permalink
use grace period rather than sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Feb 20, 2022
1 parent 418bc26 commit afb5878
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,20 @@ void usr2_handler(int sig, siginfo_t *info, void *ctx)
errno = errno_save;
}

// Because SIGUSR1 is dual-purpose, and the timer can have trailing signals after being deleted,
// a 2-second grace period is imposed to ignore any trailing timer-created signals so they don't get
// confused for user triggers
uint64_t last_timer_delete_time = 0;

int timer_graceperiod_elapsed(void)
{
return jl_hrtime() > (last_timer_delete_time + 2e9);
}

#if defined(HAVE_TIMER)
// Linux-style
#include <time.h>
#include <string.h> // for memset
#include <timefuncs.h> // for sleep_ms

static timer_t timerprof;
static struct itimerspec itsprof;
Expand Down Expand Up @@ -542,10 +551,7 @@ JL_DLLEXPORT void jl_profile_stop_timer(void)
{
if (running) {
timer_delete(timerprof);
// Because SIGUSR1 is multipurpose, care must be taken for running = 0 to be set after the timer has fully stopped.
// There may be a pending signal emitted from the timer so wait a few timer cycles
// sleep_ms((nsecprof / GIGA) * 1000 * 3);
sleep_ms(500);
last_timer_delete_time = jl_hrtime();
running = 0;
}
}
Expand All @@ -554,7 +560,6 @@ JL_DLLEXPORT void jl_profile_stop_timer(void)
// BSD-style timers
#include <string.h>
#include <sys/time.h>
#include <timefuncs.h> // for sleep_ms
struct itimerval timerprof;

JL_DLLEXPORT int jl_profile_start_timer(void)
Expand All @@ -577,10 +582,7 @@ JL_DLLEXPORT void jl_profile_stop_timer(void)
if (running) {
memset(&timerprof, 0, sizeof(timerprof));
setitimer(ITIMER_PROF, &timerprof, NULL);
// Because SIGUSR1 is multipurpose, care must be taken for running = 0 to be set after the timer has fully stopped.
// There may be a pending signal emitted from the timer so wait a few timer cycles
// sleep_ms((nsecprof / GIGA) * 1000 * 3);
sleep_ms(500);
last_timer_delete_time = jl_hrtime();
running = 0;
}
}
Expand Down Expand Up @@ -790,7 +792,7 @@ static void *signal_listener(void *arg)
}
#else
if (sig == SIGUSR1) {
if (running != 1)
if (running != 1 && timer_graceperiod_elapsed())
trigger_profile_peek();
doexit = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Profile/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ if Sys.isbsd() || Sys.islinux()
@assert occursin("started", s)
@assert process_running(p)
for _ in 1:2
sleep(2)
sleep(2.5)
if Sys.isbsd()
kill(p, 29) # SIGINFO
elseif Sys.islinux()
Expand Down

0 comments on commit afb5878

Please sign in to comment.