Skip to content

Commit ed568b2

Browse files
authored
Use less CPU for slow targets (#36)
This commit re-introduces buffered writing, using less CPU when we have slow targets. Once we have a need for GB/s range writing we'll need to figure something else out but for now this is good enough. I'm more interested in addressing generated data quality. Signed-off-by: Brian L. Troutwine <brian@troutwine.us>
1 parent 88f4947 commit ed568b2

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "file_gen"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
authors = ["Brian L. Troutwine <brian@troutwine.us>"]
55
edition = "2018"
66
license = "MIT"
@@ -70,6 +70,6 @@ default-features = false
7070
features = ["rt", "rt-multi-thread", "macros", "fs", "io-util"]
7171

7272
[profile.release]
73-
# lto = true # Optimize our binary at link stage.
74-
# codegen-units = 1 # Increases compile time but improves optmization alternatives.
75-
# opt-level = 3 # Optimize with 'all' optimization flipped on. May produce larger binaries than 's' or 'z'.
73+
lto = true # Optimize our binary at link stage.
74+
codegen-units = 1 # Increases compile time but improves optmization alternatives.
75+
opt-level = 3 # Optimize with 'all' optimization flipped on. May produce larger binaries than 's' or 'z'.

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ throughput as possible. In the 0.3 series we have pivoted to a slower line
5454
construction mechanism but relied on prebuilding blocks to write, dramatically
5555
improving total throughput up to the limit of 4Gb/s per target duplicate. This
5656
is currently a hard limit based on a u32 embedded in the program. See "Weird
57-
Quirks".
57+
Quirks". To avoid excessive CPU use for slow targets we currently used buffered
58+
writing, which appears to limit out at just above 150Mb/s per target, more than
59+
enough for the needs of the vector project today.
5860

5961
## Weird Quirks
6062

@@ -65,3 +67,6 @@ limitation needs to be lifted we'll have to contribute a fix upstream, or adjust
6567
our rate limiting approach.
6668

6769
Json generation is painfully slow. I'm very open to alternative approaches.
70+
71+
The use of Arbitrary has been... interesting. You'll find that Ascii generates a
72+
lot of empty lines.

src/file.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::convert::TryInto;
1212
use std::num::{NonZeroU32, NonZeroU64};
1313
use std::path::PathBuf;
1414
use tokio::fs;
15-
use tokio::io::AsyncWriteExt;
15+
use tokio::io::{AsyncWriteExt, BufWriter};
1616

1717
#[derive(Debug)]
1818
pub enum Error {
@@ -200,18 +200,21 @@ impl Log {
200200
&labels
201201
);
202202

203-
let mut fp = fs::OpenOptions::new()
204-
.create(true)
205-
.truncate(false)
206-
.write(true)
207-
.open(&self.path)
208-
.await?;
203+
let mut fp = BufWriter::with_capacity(
204+
ONE_MEBIBYTE * 100,
205+
fs::OpenOptions::new()
206+
.create(true)
207+
.truncate(false)
208+
.write(true)
209+
.open(&self.path)
210+
.await?,
211+
);
209212

210213
for (total_bytes, total_newlines, block) in self.block_cache.iter().cycle() {
211214
self.rate_limiter.until_n_ready(*total_bytes).await?;
212215

213216
{
214-
fp.write(block).await?;
217+
fp.write_all(block).await?;
215218
// block.len() and total_bytes are the same numeric value but we
216219
// avoid needing to get a plain value from a non-zero by calling
217220
// len here.
@@ -231,12 +234,15 @@ impl Log {
231234
// Open a new fp to `self.path`, replacing `fp`. Any holders of
232235
// the file pointer still have it but the file no longer has a
233236
// name.
234-
fp = fs::OpenOptions::new()
235-
.create(true)
236-
.truncate(false)
237-
.write(true)
238-
.open(&self.path)
239-
.await?;
237+
fp = BufWriter::with_capacity(
238+
ONE_MEBIBYTE * 100,
239+
fs::OpenOptions::new()
240+
.create(true)
241+
.truncate(false)
242+
.write(true)
243+
.open(&self.path)
244+
.await?,
245+
);
240246
bytes_written = 0;
241247
counter!("file_rotated", 1, &labels);
242248
}

src/payload/ascii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a> Arbitrary<'a> for Member {
2323
}
2424

2525
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
26-
(100, Some(6144)) // 100B to 6KiB
26+
(0, Some(6144)) // 100B to 6KiB
2727
}
2828
}
2929

0 commit comments

Comments
 (0)