Skip to content

Commit 1057fbf

Browse files
authored
Merge pull request #2393 from Kobzol/self-profile-measure
Perform self-profile compression in a separate thread
2 parents 7819923 + ccd96b5 commit 1057fbf

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

collector/src/bin/collector.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,9 @@ fn build_async_runtime() -> Runtime {
18161816
// We want to minimize noise from the runtime
18171817
builder
18181818
.worker_threads(1)
1819-
.max_blocking_threads(1)
1819+
.max_blocking_threads(8)
1820+
// Do not keep blocking threads alive for long
1821+
.thread_keep_alive(Duration::from_secs(1))
18201822
.enable_time()
18211823
.enable_io();
18221824
builder.build().expect("built runtime")

collector/src/self_profile.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,49 @@ impl SelfProfileStorage for S3SelfProfileStorage {
192192
let upload = tempfile::NamedTempFile::new().context("cannot create temporary file")?;
193193
match files {
194194
SelfProfileFiles::Eight { file } => {
195+
let start = Instant::now();
195196
let data = tokio::fs::read(file)
196197
.await
197198
.context("cannot read self-profile data")?;
198-
let mut data = snap::read::FrameEncoder::new(&data[..]);
199-
let mut compressed = Vec::new();
200-
data.read_to_end(&mut compressed)
201-
.context("cannot compress self-profile data")?;
199+
log::trace!(
200+
"Read self-profile duration: {}, size: {}",
201+
start.elapsed().as_secs_f64(),
202+
data.len()
203+
);
204+
let start = Instant::now();
205+
206+
// This is synchronous and blocks the event loop, so we should do it on a
207+
// worker thread
208+
let compressed = tokio::task::spawn_blocking(move || {
209+
let mut data = snap::read::FrameEncoder::new(&data[..]);
210+
let mut compressed = Vec::new();
211+
212+
data.read_to_end(&mut compressed)
213+
.context("cannot compress self-profile data")?;
214+
anyhow::Ok(compressed)
215+
})
216+
.await??;
217+
218+
log::trace!(
219+
"Compress self-profile duration: {}, size: {}",
220+
start.elapsed().as_secs_f64(),
221+
compressed.len()
222+
);
223+
let start = Instant::now();
202224
tokio::fs::write(upload.path(), &compressed)
203225
.await
204226
.context("cannot write compressed self-profile data")?;
227+
log::trace!(
228+
"Write self-profile duration: {}",
229+
start.elapsed().as_secs_f64()
230+
);
231+
compressed
205232
}
206233
};
207234

208235
log::info!("Uploading self-profile to {}", file_path.display());
209236

237+
let start = Instant::now();
210238
let output = tokio::process::Command::new("aws")
211239
.arg("s3")
212240
.arg("cp")
@@ -232,6 +260,10 @@ impl SelfProfileStorage for S3SelfProfileStorage {
232260
String::from_utf8_lossy(&output.stderr)
233261
));
234262
}
263+
log::trace!(
264+
"Upload self-profile duration: {}",
265+
start.elapsed().as_secs_f64()
266+
);
235267
Ok(())
236268
})
237269
}

0 commit comments

Comments
 (0)