Skip to content

Commit 2bb85d5

Browse files
committed
collect CPU offsets at PlatformStart
1 parent 5377d6f commit 2bb85d5

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

bottlecap/src/lifecycle/invocation/processor.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use crate::{
2222
},
2323
metrics::enhanced::lambda::{EnhancedMetricData, Lambda as EnhancedMetrics},
2424
proc::{
25-
self, CPUData, NetworkData,
26-
constants::{ETC_PATH, PROC_PATH},
25+
self, CPUData, NetworkData, constants::{ETC_PATH, PROC_PATH}
2726
},
2827
tags::{lambda::tags::resolve_runtime_from_proc, provider},
2928
traces::{
@@ -141,15 +140,11 @@ impl Processor {
141140
// Resume tmp, fd, and threads enhanced metrics monitoring
142141
self.enhanced_metrics.resume_usage_metrics_monitoring();
143142

144-
// Collect offsets for network and cpu metrics
145143
let network_offset: Option<NetworkData> = proc::get_network_data().ok();
146-
let cpu_offset: Option<CPUData> = proc::get_cpu_data().ok();
147-
let uptime_offset: Option<f64> = proc::get_uptime().ok();
148-
149144
let enhanced_metric_offsets = Some(EnhancedMetricData {
150145
network_offset,
151-
cpu_offset,
152-
uptime_offset,
146+
cpu_offset: None,
147+
uptime_offset: None,
153148
});
154149
self.context_buffer
155150
.add_enhanced_metric_data(&request_id, enhanced_metric_offsets);
@@ -319,6 +314,23 @@ impl Processor {
319314
.try_into()
320315
.unwrap_or_default();
321316
self.context_buffer.add_start_time(&request_id, start_time);
317+
318+
if self.config.lambda_proc_enhanced_metrics {
319+
let cpu_offset: Option<CPUData> = proc::get_cpu_data().ok();
320+
let uptime_offset: Option<f64> = proc::get_uptime().ok();
321+
if let Some(context) = self.context_buffer.get_mut(&request_id) {
322+
if let Some(ref mut enhanced_data) = context.enhanced_metric_data {
323+
enhanced_data.cpu_offset = cpu_offset;
324+
enhanced_data.uptime_offset = uptime_offset;
325+
} else {
326+
context.enhanced_metric_data = Some(EnhancedMetricData {
327+
network_offset: None,
328+
cpu_offset,
329+
uptime_offset,
330+
});
331+
}
332+
}
333+
}
322334
}
323335

324336
#[allow(clippy::too_many_arguments)]

bottlecap/src/metrics/enhanced/lambda.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ impl Lambda {
369369
let num_cores = cpu_data_end.individual_cpu_idle_times.len() as f64;
370370
let uptime = uptime_data_end - uptime_data_offset;
371371
let total_idle_time = cpu_data_end.total_idle_time_ms - cpu_data_offset.total_idle_time_ms;
372+
373+
// Change in uptime should be positive and greater than total idle time across all cores
374+
if uptime <= 0.0 || (uptime * num_cores) < total_idle_time {
375+
debug!("Invalid uptime, skipping CPU utilization metrics");
376+
return;
377+
}
372378

373379
let mut max_idle_time = 0.0;
374380
let mut min_idle_time = f64::MAX;
@@ -383,6 +389,8 @@ impl Lambda {
383389
cpu_data_offset.individual_cpu_idle_times.get(cpu_name)
384390
{
385391
let idle_time = cpu_idle_time - cpu_idle_time_offset;
392+
let idle_time = idle_time.max(0.0); // Ensure idle time is non-negative
393+
386394
if idle_time < min_idle_time {
387395
min_idle_time = idle_time;
388396
}
@@ -394,15 +402,15 @@ impl Lambda {
394402

395403
// Maximally utilized CPU is the one with the least time spent in the idle process
396404
// Multiply by 100 to report as percentage
397-
let cpu_max_utilization = ((uptime - min_idle_time) / uptime) * 100.0;
405+
let cpu_max_utilization = ((uptime - min_idle_time).max(0.0) / uptime) * 100.0;
398406

399407
// Minimally utilized CPU is the one with the most time spent in the idle process
400408
// Multiply by 100 to report as percentage
401-
let cpu_min_utilization = ((uptime - max_idle_time) / uptime) * 100.0;
409+
let cpu_min_utilization = ((uptime - max_idle_time).max(0.0) / uptime) * 100.0;
402410

403411
// CPU total utilization is the proportion of total non-idle time to the total uptime across all cores
404412
let cpu_total_utilization_decimal =
405-
((uptime * num_cores) - total_idle_time) / (uptime * num_cores);
413+
((uptime * num_cores) - total_idle_time).max(0.0) / (uptime * num_cores);
406414
// Multiply by 100 to report as percentage
407415
let cpu_total_utilization_pct = cpu_total_utilization_decimal * 100.0;
408416
// Multiply by num_cores to report in terms of cores

0 commit comments

Comments
 (0)