Skip to content

Commit 7cfbfba

Browse files
Copilotmattkur
andauthored
petri: openhcl logs now at appropriate level with HyperV backend (#1690)
The OpenHCL logs in TDX HyperV tests were appearing as INFO level regardless of their actual kernel log level, making debugging difficult when viewing test results. ## Problem In `petri/src/tracing.rs`, the `kmsg_log_task` function was hardcoding all OpenHCL log entries as `Level::INFO` when calling `log_file.write_entry()`. This meant that kernel emergency, alert, critical, error, warning, and debug messages all appeared with the same severity level in test results. ## Solution 1. **Added kernel level mapping function**: Created `kernel_level_to_tracing_level()` that properly maps Linux kernel log levels to tracing levels: - Kernel 0-3 (Emergency, Alert, Critical, Error) → `Level::ERROR` - Kernel 4 (Warning) → `Level::WARN` - Kernel 5-6 (Notice, Info) → `Level::INFO` - Kernel 7 (Debug) → `Level::DEBUG` - Unknown levels → `Level::INFO` (fallback) 2. **Updated kmsg_log_task**: Modified the function to use the actual log level from kmsg entries via `write_entry_fmt()` instead of hardcoded INFO: ```rust // Before log_file.write_entry(message.display(false)); // After let level = kernel_level_to_tracing_level(message.level); log_file.write_entry_fmt(None, level, format_args!("{}", message.display(false))); ``` 3. **Added comprehensive tests**: Included unit tests to verify the mapping works correctly for all kernel log levels. ## Impact TDX HyperV tests like `hyperv_openhcl_uefi_x64[tdx](vhd(windows_datacenter_core_2025_x64))` will now show proper log levels in test results: - 🔴 **ERROR** for critical issues that need immediate attention - 🟡 **WARN** for warnings that should be investigated - 🔵 **INFO** for normal informational messages - 🟢 **DEBUG** for debug-level diagnostics This makes debugging test failures much easier by allowing developers to quickly identify and prioritize issues based on their actual severity. Fixes #1686. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mattkur <13772048+mattkur@users.noreply.github.com> Co-authored-by: Matt LaFayette (Kurjanowicz) <mattkur@microsoft.com>
1 parent a3f6faf commit 7cfbfba

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

petri/src/tracing.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use futures::AsyncReadExt;
1010
use futures::StreamExt;
1111
use futures::io::BufReader;
1212
use jiff::Timestamp;
13+
use kmsg::KmsgParsedEntry;
1314
use parking_lot::Mutex;
1415
use std::collections::HashMap;
1516
use std::io::Write as _;
@@ -324,6 +325,17 @@ pub async fn log_stream(
324325
Ok(())
325326
}
326327

328+
/// Maps kernel log levels to tracing levels.
329+
fn kernel_level_to_tracing_level(kernel_level: u8) -> Level {
330+
match kernel_level {
331+
0..=3 => Level::ERROR,
332+
4 => Level::WARN,
333+
5..=6 => Level::INFO,
334+
7 => Level::DEBUG,
335+
_ => Level::INFO,
336+
}
337+
}
338+
327339
/// read from the kmsg stream and write entries to the log
328340
pub async fn kmsg_log_task(
329341
log_file: PetriLogFile,
@@ -332,8 +344,9 @@ pub async fn kmsg_log_task(
332344
while let Some(data) = file_stream.next().await {
333345
match data {
334346
Ok(data) => {
335-
let message = kmsg::KmsgParsedEntry::new(&data)?;
336-
log_file.write_entry(message.display(false));
347+
let message = KmsgParsedEntry::new(&data)?;
348+
let level = kernel_level_to_tracing_level(message.level);
349+
log_file.write_entry_fmt(None, level, format_args!("{}", message.display(false)));
337350
}
338351
Err(err) => {
339352
tracing::info!("kmsg disconnected: {err:?}");
@@ -344,3 +357,31 @@ pub async fn kmsg_log_task(
344357

345358
Ok(())
346359
}
360+
361+
#[cfg(test)]
362+
mod tests {
363+
use super::*;
364+
365+
#[test]
366+
fn test_kernel_level_to_tracing_level() {
367+
// Test emergency to error levels (0-3)
368+
assert_eq!(kernel_level_to_tracing_level(0), Level::ERROR);
369+
assert_eq!(kernel_level_to_tracing_level(1), Level::ERROR);
370+
assert_eq!(kernel_level_to_tracing_level(2), Level::ERROR);
371+
assert_eq!(kernel_level_to_tracing_level(3), Level::ERROR);
372+
373+
// Test warning level (4)
374+
assert_eq!(kernel_level_to_tracing_level(4), Level::WARN);
375+
376+
// Test notice and info levels (5-6)
377+
assert_eq!(kernel_level_to_tracing_level(5), Level::INFO);
378+
assert_eq!(kernel_level_to_tracing_level(6), Level::INFO);
379+
380+
// Test debug level (7)
381+
assert_eq!(kernel_level_to_tracing_level(7), Level::DEBUG);
382+
383+
// Test unknown level (fallback)
384+
assert_eq!(kernel_level_to_tracing_level(8), Level::INFO);
385+
assert_eq!(kernel_level_to_tracing_level(255), Level::INFO);
386+
}
387+
}

0 commit comments

Comments
 (0)