Skip to content

Commit 8d9cda8

Browse files
author
Ariel Ben-Yehuda
committed
log: add log on unclean stops
1 parent 13abd1b commit 8d9cda8

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

examples/simple/main.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ struct Args {
6767
/// Use the spawn_thread API instead of the Tokio API (does not demonstrate stopping)
6868
#[arg(long)]
6969
spawn_into_thread: bool,
70+
/// Whether to not do a clean stop. Used to test unclean stops.
71+
#[arg(long)]
72+
no_clean_stop: bool,
7073
}
7174

7275
impl Args {
@@ -155,13 +158,21 @@ async fn main_internal(args: Args) -> Result<(), anyhow::Error> {
155158
});
156159
run_slow(&args).await;
157160
} else {
158-
tracing::info!("starting profiler");
159-
let handle = profiler.spawn_controllable()?;
160-
tracing::info!("profiler started");
161+
if args.no_clean_stop {
162+
tracing::info!("starting profiler");
163+
profiler.spawn()?;
164+
tracing::info!("profiler started");
161165

162-
run_slow(&args).await;
166+
run_slow(&args).await;
167+
} else {
168+
tracing::info!("starting profiler");
169+
let handle = profiler.spawn_controllable()?;
170+
tracing::info!("profiler started");
171+
172+
run_slow(&args).await;
163173

164-
handle.stop().await;
174+
handle.stop().await;
175+
}
165176
}
166177

167178
Ok(())

src/profiler.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
};
1111
use std::{
1212
fs::File,
13-
io,
13+
io, mem,
1414
path::{Path, PathBuf},
1515
time::{Duration, SystemTime, SystemTimeError, UNIX_EPOCH},
1616
};
@@ -1068,6 +1068,18 @@ impl Profiler {
10681068
}
10691069

10701070
fn spawn_inner<E: ProfilerEngine>(self, asprof: E) -> Result<RunningProfiler, SpawnError> {
1071+
struct LogOnDrop;
1072+
impl Drop for LogOnDrop {
1073+
fn drop(&mut self) {
1074+
// Tokio will call destructors during runtime shutdown. Have something that will
1075+
// emit a log in that case
1076+
tracing::info!(
1077+
"unable to upload the last jfr due to Tokio runtime shutdown. \
1078+
Add a call to `RunningProfiler::stop` to wait for jfr upload to finish."
1079+
);
1080+
}
1081+
}
1082+
10711083
// Initialize async profiler - needs to be done once.
10721084
E::init_async_profiler()?;
10731085
tracing::info!("successfully initialized async profiler.");
@@ -1089,6 +1101,7 @@ impl Profiler {
10891101
let mut agent_metadata = self.agent_metadata;
10901102
let mut done = false;
10911103

1104+
let guard = LogOnDrop;
10921105
while !done {
10931106
// Wait until a timer or exit event
10941107
tokio::select! {
@@ -1128,6 +1141,7 @@ impl Profiler {
11281141
}
11291142
}
11301143

1144+
mem::forget(guard);
11311145
tracing::info!("profiling task finished");
11321146
});
11331147

tests/integration.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ dir="profiles"
1212
mkdir -p $dir
1313
rm -f $dir/*.jfr
1414

15+
# test the "unable to uploqad the last jfr" logic
16+
rm -rf $dir/short
17+
mkdir $dir/short
18+
./simple --local $dir/short --duration 1s --reporting-interval 10s --no-clean-stop 2>$dir/short/log
19+
cat $dir/short/log
20+
grep "unable to upload the last jfr" $dir/short/log
21+
rm -rf $dir/short
22+
1523
# Pass --worker-threads 16 to make the test much less flaky since there is always some worker thread running
1624
./simple --local $dir --duration 30s --reporting-interval 10s --worker-threads 16 --native-mem 4096
1725

0 commit comments

Comments
 (0)