Skip to content

Commit

Permalink
[CCInstr] Add a field in the task to record the time of last call
Browse files Browse the repository at this point in the history
  • Loading branch information
whatsthecraic committed Mar 11, 2024
1 parent ba5345f commit 3ec59cd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ SRCS := \
simplevector runtime_intrinsics precompile jloptions mtarraylist \
threading partr stackwalk gc gc-debug gc-pages gc-stacks gc-alloc-profiler gc-page-profiler method \
jlapi signal-handling safepoint timing subtype rtutils gc-heap-snapshot \
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall \
cancellation-instrumentation

RT_LLVMLINK :=
CG_LLVMLINK :=
Expand Down
47 changes: 47 additions & 0 deletions src/cancellation-instrumentation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* cancellation_instrumentation.cpp
*
* Created on: 9 Mar 2024
* Author: ddeleo
*/
#include "julia.h"
#include "julia_internal.h"

#include <iostream>
#include <unistd.h>

using namespace std;

extern "C" {

static uint64_t g_min_update_interval = 0;
static uint64_t get_min_update_interval() {
// this is inherently thread-safe, but that's okay as we're only interested on a rough estimate
// for how many clock cycles correspond to 1 second, more or less.
if (g_min_update_interval == 0) {
// sleep can be interrupted by a signal before the expected sleep time. In this case it will
// return with a value != 0 and we repeat the measurement.
int rc = 0;
do {
uint64_t t0 = cycleclock();
rc = sleep(1); // 1 second
uint64_t t1 = cycleclock();
g_min_update_interval = t1 - t0;
} while (rc != 0);
}
return g_min_update_interval;
}

JL_DLLEXPORT bool update_epoch(bool force) {
jl_task_t *task = jl_current_task;
uint64_t t0 = task->instr_last_epoch;
uint64_t t1 = cycleclock();
if (!force && ((t1 - t0) <= get_min_update_interval())){
return false;
} else {
task->instr_last_epoch = t1;
return true;
}
}

} // extern C
3 changes: 3 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,9 @@ typedef struct _jl_task_t {
uint16_t reentrant_timing; // How many times we've reentered timing
unsigned int copy_stack:31; // sizeof stack for copybuf
unsigned int started:1;

// cancellation instrumentation:
uint64_t instr_last_epoch;
} jl_task_t;

#define JL_TASK_STATE_RUNNABLE 0
Expand Down
4 changes: 4 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,10 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, jl_value_t *completion
#ifdef _COMPILER_ASAN_ENABLED_
t->ctx.asan_fake_stack = NULL;
#endif

// Cancellation instrumentation
t->instr_last_epoch = 0;

return t;
}

Expand Down

0 comments on commit 3ec59cd

Please sign in to comment.