Skip to content

Commit 260080d

Browse files
committed
add tracing
1 parent ff9c777 commit 260080d

8 files changed

Lines changed: 195 additions & 24 deletions

File tree

‎src/windows/wslopenvmm/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_custom_command(
2626
OUTPUT ${WSLOPENVMM_DLL}
2727
COMMAND ${CMAKE_COMMAND} -E env "VM_SERVICE_PROTO=${VM_SERVICE_PROTO}" ${CARGO_EXECUTABLE} build --manifest-path ${CMAKE_CURRENT_LIST_DIR}/Cargo.toml --target ${RUST_TARGET} --target-dir ${CARGO_TARGET_DIR} ${CARGO_BUILD_ARGUMENTS}
2828
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CARGO_OUTPUT} ${WSLOPENVMM_DLL}
29-
DEPENDS ${VM_SERVICE_PROTO} Cargo.lock Cargo.toml build.rs src/client.rs src/lib.rs src/af_unix.rs src/rpc.rs wslopenvmm.h
29+
DEPENDS ${VM_SERVICE_PROTO} Cargo.lock Cargo.toml build.rs src/client.rs src/lib.rs src/af_unix.rs src/rpc.rs src/diagnostics.rs wslopenvmm.h
3030
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
3131
VERBATIM)
3232

‎src/windows/wslopenvmm/Cargo.lock‎

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/windows/wslopenvmm/Cargo.toml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
hyper-util = { version = "0.1", features = ["tokio"] }
1313
parking_lot = "0.12"
14+
smallvec = "1"
1415
socket2 = "0.6"
1516
prost = "0.14"
1617
prost-types = "0.14"
1718
tonic = "0.14"
1819
tonic-prost = "0.14"
1920
tokio = { version = "1", features = ["net", "rt", "time", "macros", "sync"] }
2021
tower = "0.5"
22+
tracing = "0.1"
23+
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "registry"] }
2124
windows = { version = "0.62", features = ["Win32_Foundation"] }
22-
windows-sys = { version = "0.61", features = ["Win32_Networking_WinSock"] }
25+
windows-sys = { version = "0.61", features = ["Win32_Networking_WinSock", "Win32_System_Diagnostics_Debug"] }
2326

2427
[dev-dependencies]
2528
tokio-stream = { version = "0.1", features = ["net"] }

‎src/windows/wslopenvmm/src/client.rs‎

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct VmConfigHandle {
4040

4141
impl VmConfigHandle {
4242
pub fn new() -> Self {
43+
crate::diagnostics::ensure_tracing_init();
4344
Self {
4445
builder: Mutex::new(VmConfigBuilder::new()),
4546
}
@@ -51,12 +52,29 @@ impl VmConfigHandle {
5152

5253
pub fn create_vm(&self, socket_path: String, timeout_ms: u32) -> Result<VmHandle, HRESULT> {
5354
if timeout_ms == 0 {
55+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm requires a nonzero timeout");
5456
return Err(E_INVALIDARG);
5557
}
5658
let timeout = Duration::from_millis(u64::from(timeout_ms));
57-
let deadline = Instant::now() + timeout;
58-
let mut builder = self.builder.try_lock_until(deadline).ok_or(rpc::TIMEOUT)?;
59-
builder.create_vm(socket_path, timeout, deadline)
59+
let started = Instant::now();
60+
let deadline = started + timeout;
61+
tracing::info!(target: "wslopenvmm::rpc", "CreateVm starting with timeout {timeout_ms} ms");
62+
let Some(mut builder) = self.builder.try_lock_until(deadline) else {
63+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm timed out waiting for the configuration lock; no request sent");
64+
return Err(rpc::TIMEOUT);
65+
};
66+
let result = builder.create_vm(socket_path, timeout, deadline);
67+
match &result {
68+
Ok(_) => {
69+
tracing::info!(target: "wslopenvmm::rpc", "CreateVm completed in {:?}", started.elapsed())
70+
}
71+
Err(error) => tracing::warn!(
72+
target: "wslopenvmm::rpc",
73+
"CreateVm failed with HRESULT {:#010x} after {:?}; configuration status: {:?}",
74+
error.0, started.elapsed(), builder.status
75+
),
76+
}
77+
result
6078
}
6179
}
6280

@@ -94,31 +112,42 @@ impl VmConfigBuilder {
94112
deadline: Instant,
95113
) -> Result<VmHandle, HRESULT> {
96114
if self.status == VmConfigStatus::CreationOutcomeUnknown {
115+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm rejected: previous creation outcome is unknown");
97116
return Err(rpc::INVALID_STATE);
98117
}
99118
let runtime = match tokio::runtime::Builder::new_current_thread()
100119
.enable_all()
101120
.build()
102121
{
103122
Ok(runtime) => runtime,
104-
Err(_) => return Err(E_FAIL),
123+
Err(_) => {
124+
tracing::error!(target: "wslopenvmm::rpc", "CreateVm could not initialize the RPC runtime");
125+
return Err(E_FAIL);
126+
}
105127
};
106128
let remaining = deadline.saturating_duration_since(Instant::now());
107129
if remaining.is_zero() {
130+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm deadline expired before connecting");
108131
return Err(rpc::TIMEOUT);
109132
}
110-
let channel =
111-
match runtime.block_on(af_unix::connect_channel(socket_path.into(), remaining)) {
112-
Ok(channel) => channel,
113-
Err(error) => return Err(rpc::io_error_to_hresult(&error)),
114-
};
133+
let channel = match runtime
134+
.block_on(af_unix::connect_channel(socket_path.into(), remaining))
135+
{
136+
Ok(channel) => channel,
137+
Err(error) => {
138+
let result = rpc::io_error_to_hresult(&error);
139+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm connection failed with HRESULT {:#010x}; no creation request sent", result.0);
140+
return Err(result);
141+
}
142+
};
115143
let mut client = VmClient::new(channel);
116144
let request = CreateVmRequest {
117145
config: Some(self.config.clone()),
118146
log_id: String::new(),
119147
};
120148
let remaining = deadline.saturating_duration_since(Instant::now());
121149
if remaining.is_zero() {
150+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm deadline expired before dispatch");
122151
return Err(rpc::TIMEOUT);
123152
}
124153
match runtime.block_on(rpc::execute(
@@ -130,6 +159,7 @@ impl VmConfigBuilder {
130159
Err(error) => {
131160
if error.uncertain {
132161
self.status = VmConfigStatus::CreationOutcomeUnknown;
162+
tracing::warn!(target: "wslopenvmm::rpc", "CreateVm outcome is unknown; discard the configuration and process before retrying");
133163
}
134164
Err(error.result)
135165
}
@@ -261,23 +291,29 @@ impl VmHandle {
261291

262292
pub fn cancel_requests(&self) -> HRESULT {
263293
self.cancellation.send_replace(true);
294+
tracing::info!(target: "wslopenvmm::rpc", "VM request cancellation signalled");
264295
S_OK
265296
}
266297

267298
fn with_operation(
268299
&self,
300+
name: &'static str,
269301
cleanup: bool,
270302
operation: impl FnOnce(&mut VmHandleInner, Instant, Option<watch::Receiver<bool>>) -> HRESULT,
271303
) -> HRESULT {
272-
let deadline = Instant::now() + self.timeout;
304+
let started = Instant::now();
305+
let deadline = started + self.timeout;
306+
tracing::info!(target: "wslopenvmm::rpc", "{name} starting");
273307
let Some(mut inner) = self.inner.try_lock_until(deadline) else {
274308
// No request was sent, so lock contention alone does not invalidate the VM.
309+
tracing::warn!(target: "wslopenvmm::rpc", "{name} timed out waiting for the VM lock; no request sent");
275310
return rpc::TIMEOUT;
276311
};
277312
if *self.cancellation.borrow() {
278313
inner.status = VmStatus::RecoveryRequired;
279314
}
280315
if !cleanup && inner.status == VmStatus::RecoveryRequired {
316+
tracing::warn!(target: "wslopenvmm::rpc", "{name} rejected: VM requires recovery");
281317
return rpc::INVALID_STATE;
282318
}
283319
let cancellation = if cleanup {
@@ -290,11 +326,24 @@ impl VmHandle {
290326
if *self.cancellation.borrow() {
291327
inner.status = VmStatus::RecoveryRequired;
292328
}
329+
if result.is_err() {
330+
tracing::warn!(
331+
target: "wslopenvmm::rpc",
332+
"{name} failed with HRESULT {:#010x} after {:?}; VM status: {:?}",
333+
result.0, started.elapsed(), inner.status
334+
);
335+
} else {
336+
tracing::info!(
337+
target: "wslopenvmm::rpc",
338+
"{name} completed in {:?}; VM status: {:?}",
339+
started.elapsed(), inner.status
340+
);
341+
}
293342
result
294343
}
295344

296345
pub fn resume_vm(&self) -> HRESULT {
297-
self.with_operation(false, |inner, deadline, cancellation| {
346+
self.with_operation("ResumeVm", false, |inner, deadline, cancellation| {
298347
self.rpc(
299348
inner,
300349
deadline,
@@ -306,7 +355,7 @@ impl VmHandle {
306355
}
307356

308357
pub fn teardown_vm(&self) -> HRESULT {
309-
self.with_operation(true, |inner, deadline, cancellation| {
358+
self.with_operation("TeardownVm", true, |inner, deadline, cancellation| {
310359
let result = self.rpc(
311360
inner,
312361
deadline,
@@ -322,7 +371,7 @@ impl VmHandle {
322371
}
323372

324373
pub fn quit(&self) -> HRESULT {
325-
self.with_operation(true, |inner, deadline, cancellation| {
374+
self.with_operation("Quit", true, |inner, deadline, cancellation| {
326375
self.rpc(
327376
inner,
328377
deadline,
@@ -356,7 +405,7 @@ impl VmHandle {
356405
}
357406

358407
pub fn add_share(&self, tag: String, host_path: String, read_only: bool) -> HRESULT {
359-
self.with_operation(false, |inner, deadline, cancellation| {
408+
self.with_operation("AddShare", false, |inner, deadline, cancellation| {
360409
if inner.shares.contains_key(&tag) {
361410
return HRESULT::from_win32(ERROR_ALREADY_EXISTS.0);
362411
}
@@ -393,7 +442,7 @@ impl VmHandle {
393442
}
394443

395444
pub fn remove_share(&self, tag: &str) -> HRESULT {
396-
self.with_operation(false, |inner, deadline, cancellation| {
445+
self.with_operation("RemoveShare", false, |inner, deadline, cancellation| {
397446
let Some(instance_id) = inner.shares.get(tag) else {
398447
return HRESULT::from_win32(ERROR_NOT_FOUND.0);
399448
};
@@ -428,6 +477,7 @@ impl VmHandle {
428477
{
429478
let remaining = deadline.saturating_duration_since(Instant::now());
430479
if remaining.is_zero() {
480+
tracing::warn!(target: "wslopenvmm::rpc", "RPC deadline expired before dispatch; no request sent");
431481
return rpc::TIMEOUT;
432482
}
433483
let result = inner.runtime.block_on(rpc::execute(
@@ -443,6 +493,7 @@ impl VmHandle {
443493
Err(error) => {
444494
if error.uncertain {
445495
inner.status = VmStatus::RecoveryRequired;
496+
tracing::warn!(target: "wslopenvmm::rpc", "Uncertain RPC outcome; VM requires teardown and recreation");
446497
}
447498
error.result
448499
}
@@ -457,7 +508,12 @@ impl VmHandle {
457508
host_path: String,
458509
read_only: bool,
459510
) -> HRESULT {
460-
self.with_operation(false, |inner, deadline, cancellation| {
511+
let name = if modify_type == ModifyType::Add {
512+
"AttachScsiDisk"
513+
} else {
514+
"DetachScsiDisk"
515+
};
516+
self.with_operation(name, false, |inner, deadline, cancellation| {
461517
let request = ModifyResourceRequest {
462518
r#type: modify_type as i32,
463519
resource: Some(vmservice::modify_resource_request::Resource::ScsiDisk(
@@ -488,7 +544,12 @@ impl VmHandle {
488544
tcp: bool,
489545
family: i32,
490546
) -> HRESULT {
491-
self.with_operation(false, |inner, deadline, cancellation| {
547+
let name = if modify_type == ModifyType::Update {
548+
"BindPort"
549+
} else {
550+
"UnbindPort"
551+
};
552+
self.with_operation(name, false, |inner, deadline, cancellation| {
492553
let host_address = match family {
493554
family if family == i32::from(AF_INET) => "127.0.0.1",
494555
family if family == i32::from(AF_INET6) => "::1",

‎src/windows/wslopenvmm/src/client/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fn cancellation_racing_success_requires_recovery() {
474474
let fixture = Fixture::new();
475475
let vm = &fixture.vm.as_ref().unwrap().0;
476476
assert_eq!(
477-
vm.with_operation(cleanup, |inner, _, _| {
477+
vm.with_operation("CancellationRace", cleanup, |inner, _, _| {
478478
assert_eq!(inner.status, VmStatus::Active);
479479
assert_eq!(vm.cancel_requests(), S_OK);
480480
S_OK

0 commit comments

Comments
 (0)