Skip to content

Commit

Permalink
make check_interval adaptive
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 18, 2023
1 parent b112428 commit 3f9a06e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
53 changes: 47 additions & 6 deletions lib/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,6 @@ restart_insn(struct exec_context *ctx)
#endif
}

#define CHECK_INTERVAL 1000

int
check_interrupt(struct exec_context *ctx)
{
Expand Down Expand Up @@ -952,6 +950,34 @@ check_interrupt(struct exec_context *ctx)
return 0;
}

#define CHECK_INTERVAL_MAX UINT32_MAX
#define CHECK_INTERVAL_DEFAULT 1000
#define CHECK_INTERVAL_MIN 1

static void
adjust_check_interval(struct exec_context *ctx, const struct timespec *now,
const struct timespec *last)
{
struct timespec diff;
timespec_sub(now, last, &diff);
uint64_t diff_ms = timespec_to_ms(&diff);
uint32_t check_interval = ctx->check_interval;
if (diff_ms < CHECK_INTERRUPT_INTERVAL_MS / 2) {
if (check_interval <= CHECK_INTERVAL_MAX / 2) {
check_interval *= 2;
} else {
check_interval = CHECK_INTERVAL_MAX;
}
} else if (diff_ms / 2 > CHECK_INTERRUPT_INTERVAL_MS) {
check_interval /= 2;
if (check_interval < CHECK_INTERVAL_MIN) {
check_interval = CHECK_INTERVAL_MIN;
}
}
xlog_trace("check_interval %" PRIu32, check_interval);
ctx->check_interval = check_interval;
}

int
exec_expr(uint32_t funcidx, const struct expr *expr,
const struct localtype *localtype,
Expand Down Expand Up @@ -979,7 +1005,10 @@ exec_expr(uint32_t funcidx, const struct expr *expr,
int
exec_expr_continue(struct exec_context *ctx)
{
uint32_t n = 0;
struct timespec last;
bool has_last = false;
uint32_t n = ctx->check_interval;
assert(n > 0);
while (true) {
int ret;
switch (ctx->event) {
Expand Down Expand Up @@ -1043,16 +1072,27 @@ exec_expr_continue(struct exec_context *ctx)
if (ctx->frames.lsize == 0) {
break;
}
n++;
if (__predict_false(n > CHECK_INTERVAL)) {
n = 0;
n--;
if (__predict_false(n == 0)) {
ret = check_interrupt(ctx);
if (ret != 0) {
if (IS_RESTARTABLE(ret)) {
STAT_INC(ctx->stats.exec_loop_restart);
}
return ret;
}
struct timespec now;
ret = timespec_now(CLOCK_MONOTONIC, &now);
if (ret != 0) {
return ret;
}
if (has_last) {
adjust_check_interval(ctx, &now, &last);
}
last = now;
has_last = true;
n = ctx->check_interval;
assert(n > 0);
}
struct cell *stack = &VEC_NEXTELEM(ctx->stack);
ret = fetch_exec_next_insn(ctx->p, stack, ctx);
Expand Down Expand Up @@ -1275,6 +1315,7 @@ exec_context_init(struct exec_context *ctx, struct instance *inst)
report_init(&ctx->report0);
ctx->report = &ctx->report0;
ctx->restart_type = RESTART_NONE;
ctx->check_interval = CHECK_INTERVAL_DEFAULT;
exec_options_set_defaults(&ctx->options);
}

Expand Down
1 change: 1 addition & 0 deletions lib/exec_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ struct exec_context {
struct cluster *cluster;
unsigned int user_intr_delay_count;
unsigned int user_intr_delay;
uint32_t check_interval;

/* scheduler */
struct sched *sched;
Expand Down
14 changes: 14 additions & 0 deletions lib/timeutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ convert_timespec(clockid_t from_id, clockid_t to_id,
return ret;
}

uint64_t
timespec_to_ms(const struct timespec *tv)
{
if (UINT64_MAX / 1000 < tv->tv_sec) {
return UINT64_MAX;
}
uint64_t ms1 = tv->tv_sec * 1000;
uint64_t ms2 = tv->tv_nsec / 1000000;
if (UINT64_MAX - ms1 < ms2) {
return UINT64_MAX;
}
return ms1 + ms2;
}

int
timespec_sleep(clockid_t id, const struct timespec *absto)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/timeutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ int abstime_to_reltime_ms_roundup(clockid_t id, const struct timespec *abstime,
int *reltime_ms);
int convert_timespec(clockid_t from_id, clockid_t to_id,
const struct timespec *from_ts, struct timespec *result);

uint64_t timespec_to_ms(const struct timespec *tv);
int timespec_sleep(clockid_t id, const struct timespec *absto);

0 comments on commit 3f9a06e

Please sign in to comment.