Skip to content

Commit 6ab58cc

Browse files
authored
Reduce syscall overhead in metrics updates (#2940)
1 parent 618a440 commit 6ab58cc

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

native/core/src/execution/jni_api.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ struct ExecutionContext {
136136
pub metrics_update_interval: Option<Duration>,
137137
// The last update time of metrics
138138
pub metrics_last_update_time: Instant,
139+
/// Counter to avoid checking time on every poll iteration (reduces syscalls)
140+
pub poll_count_since_metrics_check: u32,
139141
/// The time it took to create the native plan and configure the context
140142
pub plan_creation_time: Duration,
141143
/// DataFusion SessionContext
@@ -272,6 +274,7 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_createPlan(
272274
metrics,
273275
metrics_update_interval,
274276
metrics_last_update_time: Instant::now(),
277+
poll_count_since_metrics_check: 0,
275278
plan_creation_time,
276279
session_ctx: Arc::new(session),
277280
debug_native,
@@ -512,11 +515,16 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_executePlan(
512515
let poll_output = poll!(next_item);
513516

514517
// update metrics at interval
518+
// Only check time every 100 polls to reduce syscall overhead
515519
if let Some(interval) = exec_context.metrics_update_interval {
516-
let now = Instant::now();
517-
if now - exec_context.metrics_last_update_time >= interval {
518-
update_metrics(&mut env, exec_context)?;
519-
exec_context.metrics_last_update_time = now;
520+
exec_context.poll_count_since_metrics_check += 1;
521+
if exec_context.poll_count_since_metrics_check >= 100 {
522+
let now = Instant::now();
523+
if now - exec_context.metrics_last_update_time >= interval {
524+
update_metrics(&mut env, exec_context)?;
525+
exec_context.metrics_last_update_time = now;
526+
}
527+
exec_context.poll_count_since_metrics_check = 0;
520528
}
521529
}
522530

0 commit comments

Comments
 (0)