Skip to content

Commit 35d18a9

Browse files
author
zhangyi
committed
add new flag "loop"
1 parent 4d960e6 commit 35d18a9

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "py-spy-for-datakit"
3-
version = "0.3.15"
3+
version = "0.3.16"
44
authors = ["Yi Zhang <[email protected]>"]
55
repository = "https://github.com/GuanceCloud/py-spy-for-datakit"
66
homepage = "https://github.com/GuanceCloud/py-spy-for-datakit"

src/config.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub struct Config {
5959
pub service: String,
6060
pub env: String,
6161
pub version: String,
62+
pub loop_duration: u64,
6263
}
6364

6465
#[allow(non_camel_case_types)]
@@ -128,7 +129,8 @@ impl Default for Config {
128129
port: 9529,
129130
service: String::from("unnamed-service"),
130131
env: String::from("unnamed-env"),
131-
version: String::from("unnamed-version")
132+
version: String::from("unnamed-version"),
133+
loop_duration: 0,
132134
}
133135
}
134136
}
@@ -238,6 +240,14 @@ impl Config {
238240
.help("The number of seconds to sample for")
239241
.default_value("60")
240242
.takes_value(true))
243+
.arg(Arg::new("loop")
244+
.short('L')
245+
.long("loop")
246+
.value_name("loop")
247+
.help("continuously run profiler in a loop within the specified seconds, 0 represents infinity")
248+
.default_value("0")
249+
.takes_value(true)
250+
)
241251
.arg(rate.clone())
242252
.arg(subprocesses.clone())
243253
.arg(gil.clone())
@@ -381,7 +391,10 @@ impl Config {
381391
None => RecordDuration::Seconds(60),
382392
Some(seconds) => RecordDuration::Seconds(seconds.parse().expect("invalid duration"))
383393
};
384-
394+
config.loop_duration = match matches.value_of("loop") {
395+
None => 0,
396+
Some(seconds) => seconds.parse().expect("invalid loop parameter")
397+
};
385398
config.host = match matches.value_of("host") {
386399
Some(host) => host.to_owned(),
387400
None => String::from("127.0.0.1")

src/main.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
240240
}
241241
};
242242

243+
let max_total_intervals = match &config.loop_duration {
244+
0 => None,
245+
_ => Some(config.loop_duration * config.sampling_rate)
246+
};
247+
243248
use indicatif::ProgressBar;
244249
let progress = match (config.hide_progress, &config.duration) {
245250
(true, _) => ProgressBar::hidden(),
@@ -257,7 +262,9 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
257262

258263
let mut errors = 0;
259264
let mut intervals = 0;
265+
let mut total_intervals = 0;
260266
let mut samples = 0;
267+
let mut loop_complete = false;
261268
println!();
262269

263270
let running = Arc::new(AtomicBool::new(true));
@@ -294,6 +301,7 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
294301
}
295302

296303
intervals += 1;
304+
total_intervals += 1;
297305
if let Some(max_intervals) = max_intervals {
298306
if intervals >= max_intervals {
299307
exit_message = "";
@@ -314,7 +322,15 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
314322

315323
output = new_output(config)?;
316324
intervals = 0;
317-
profile_start_time = Utc::now()
325+
profile_start_time = Utc::now();
326+
327+
if let Some(total) = max_total_intervals {
328+
if total_intervals >= total {
329+
loop_complete = true;
330+
break;
331+
}
332+
}
333+
318334
} else {
319335
break;
320336
}
@@ -378,13 +394,15 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
378394
if config.command.as_str() == cmd_datakit {
379395
let mut buf:Vec<u8> = Vec::with_capacity(128);
380396
output.write(&mut buf)?;
381-
if let Some(tx) = channel_sender {
382-
if let Err(e) = tx.send(Sample{
383-
start: profile_start_time,
384-
end:Utc::now(),
385-
payload: buf,
386-
}) {
387-
eprintln!("fail to send data to channel: {:?}", e)
397+
if !loop_complete {
398+
if let Some(tx) = channel_sender {
399+
if let Err(e) = tx.send(Sample{
400+
start: profile_start_time,
401+
end:Utc::now(),
402+
payload: buf,
403+
}) {
404+
eprintln!("fail to send data to channel: {:?}", e)
405+
}
388406
}
389407
}
390408

0 commit comments

Comments
 (0)