Skip to main content

cargo_zng/
trace.rs

1use std::{
2    collections::{HashMap, HashSet},
3    io::{Read as _, Write},
4    path::PathBuf,
5    time::SystemTime,
6};
7
8use clap::*;
9use serde::Deserialize as _;
10
11use crate::util::glob_walker;
12
13#[derive(Args, Debug, Default)]
14pub struct TraceArgs {
15    /// Path or command to run the Zng executable
16    ///
17    /// Example: `cargo zng trace "./some/exe"` or `cargo zng trace -- cargo run exe`
18    #[arg(trailing_var_arg = true)]
19    command: Vec<String>,
20
21    /// env_logger style filter
22    #[arg(long, short, default_value = "trace")]
23    filter: String,
24
25    /// Output JSON file
26    ///
27    /// {timestamp} and {ts} is replaced with a timestamp in microseconds from Unix epoch
28    #[arg(long, short, default_value = "./trace-{timestamp}.json")]
29    output: String,
30}
31
32pub fn run(args: TraceArgs) {
33    let mut cmd = {
34        let mut cmd = args.command.into_iter().peekable();
35        if let Some(c) = cmd.peek()
36            && c == "--"
37        {
38            cmd.next();
39        }
40        if let Some(c) = cmd.next() {
41            let mut o = std::process::Command::new(c);
42            o.args(cmd);
43            o
44        } else {
45            fatal!("COMMAND is required")
46        }
47    };
48
49    let ts = SystemTime::now()
50        .duration_since(SystemTime::UNIX_EPOCH)
51        .unwrap_or_default()
52        .as_micros()
53        .to_string();
54
55    let tmp = std::env::temp_dir().join("cargo-zng-trace");
56    if let Err(e) = std::fs::create_dir_all(&tmp) {
57        fatal!("cannot create temp dir, {e}");
58    }
59    let out_dir = tmp.join(&ts);
60    let _ = std::fs::remove_dir_all(&out_dir);
61
62    let out_file = PathBuf::from(args.output.replace("{timestamp}", &ts).replace("{ts}", &ts));
63    if let Some(p) = out_file.parent()
64        && let Err(e) = std::fs::create_dir_all(p)
65    {
66        fatal!("cannot output to {}, {e}", out_file.display());
67    }
68    let mut out = match std::fs::File::create(&out_file) {
69        Ok(f) => f,
70        Err(e) => fatal!("cannot output to {}, {e}", out_file.display()),
71    };
72
73    cmd.env("ZNG_RECORD_TRACE", "")
74        .env("ZNG_RECORD_TRACE_DIR", &tmp)
75        .env("ZNG_RECORD_TRACE_FILTER", args.filter)
76        .env("ZNG_RECORD_TRACE_TIMESTAMP", &ts);
77
78    let mut cmd = match cmd.spawn() {
79        Ok(c) => c,
80        Err(e) => fatal!("cannot run, {e}"),
81    };
82
83    let code = match cmd.wait() {
84        Ok(s) => s.code().unwrap_or(0),
85        Err(e) => {
86            error!("cannot wait command exit, {e}");
87            101
88        }
89    };
90
91    if !out_dir.exists() {
92        fatal!("run did not save any trace\nnote: the feature \"trace_recorder\" must be enabled during build")
93    }
94
95    println!("merging trace files...");
96
97    out.write_all(b"[\n")
98        .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
99    let mut separator = "";
100
101    for trace in glob_walker(out_dir.join("*.json").display().to_string().as_str(), false)
102        .ok()
103        .into_iter()
104        .flatten()
105    {
106        let trace = match trace {
107            Ok(t) => t.into_path(),
108            Err(e) => {
109                error!("error globing trace files, {e}");
110                continue;
111            }
112        };
113        let json = match std::fs::read_to_string(&trace) {
114            Ok(s) => s,
115            Err(e) => {
116                error!("cannot read {}, {e}", trace.display());
117                continue;
118            }
119        };
120
121        let name_sys_pid = trace
122            .file_name()
123            .unwrap_or_default()
124            .to_string_lossy()
125            .strip_suffix(".json")
126            .unwrap_or_default()
127            .to_owned();
128        let name_sys_pid = match name_sys_pid.parse::<u64>() {
129            Ok(i) => i,
130            Err(_) => {
131                error!("expected only {{pid}}.json files");
132                continue;
133            }
134        };
135
136        // skip the array opening
137        let json = json.trim_start();
138        if !json.starts_with('[') {
139            error!("unknown format in {}", trace.display());
140            continue;
141        }
142        let json = &json[1..];
143
144        let mut thread_ids = HashSet::new();
145        // peek thread ids
146        {
147            let mut search = json;
148            let prefix = r#""tid":"#;
149            while let Some(i) = search.find(prefix) {
150                search = &search[i + prefix.len()..];
151                if let Some(i) = search.find(',') {
152                    let tid = &search[..i];
153                    if let Ok(tid) = tid.parse::<u64>() {
154                        thread_ids.insert(tid);
155                    }
156                    search = &search[i + 1..];
157                }
158            }
159        }
160        let mut custom_thread_id = 9000u64;
161        let mut custom_threads = HashMap::new();
162
163        let mut reader = std::io::Cursor::new(json.as_bytes());
164        loop {
165            // skip white space and commas to the next object
166            let mut pos = reader.position();
167            let mut buf = [0u8];
168            while reader.read(&mut buf).is_ok() {
169                if !b" \r\n\t,".contains(&buf[0]) {
170                    break;
171                }
172                pos = reader.position();
173            }
174            reader.set_position(pos);
175            let mut de = serde_json::Deserializer::from_reader(&mut reader);
176            match serde_json::Value::deserialize(&mut de) {
177                Ok(mut entry) => {
178                    // patch "pid" to be unique
179                    if let Some(serde_json::Value::Number(pid)) = entry.get_mut("pid") {
180                        if pid.as_u64() != Some(1) {
181                            error!("expected only pid:1 in trace file");
182                            continue;
183                        }
184                        *pid = serde_json::Number::from(name_sys_pid);
185                    }
186
187                    // convert custom entries to actual trace format
188                    match &mut entry {
189                        serde_json::Value::Object(entry) => {
190                            enum Phase {
191                                Event,
192                                Begin,
193                                End,
194                                Other,
195                            }
196                            let mut ph = Phase::Other;
197                            if let Some(serde_json::Value::String(p)) = entry.get("ph") {
198                                match p.as_str() {
199                                    "i" => ph = Phase::Event,
200                                    "B" => ph = Phase::Begin,
201                                    "E" => ph = Phase::End,
202                                    _ => {}
203                                }
204                            }
205                            if let Some(serde_json::Value::Object(args)) = entry.get_mut("args") {
206                                match ph {
207                                    Phase::Event => {
208                                        // convert the INFO message process name to actual "process_name" metadata
209                                        if let Some(serde_json::Value::String(msg)) = args.get("message")
210                                            && let Some(rest) = msg.strip_prefix("pid: ")
211                                            && let Some((sys_pid, p_name)) = rest.split_once(", name: ")
212                                            && let Ok(sys_pid) = sys_pid.parse::<u64>()
213                                            && name_sys_pid == sys_pid
214                                        {
215                                            let args = format_args!(
216                                                r#"{separator}{{"ph":"M","pid":{sys_pid},"name":"process_name","args":{{"name":"{p_name}"}}}}"#,
217                                            );
218                                            out.write_fmt(args)
219                                                .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
220                                        } else if let Some(serde_json::Value::String(custom_thread)) = args.remove("thread")
221                                            && let Some(serde_json::Value::Number(n)) = entry.get_mut("tid")
222                                        {
223                                            let tid = match custom_threads.entry(custom_thread) {
224                                                std::collections::hash_map::Entry::Occupied(e) => *e.get(),
225                                                std::collections::hash_map::Entry::Vacant(e) => {
226                                                    let name = serde_json::Value::String(e.key().clone());
227
228                                                    while !thread_ids.insert(custom_thread_id) {
229                                                        custom_thread_id = custom_thread_id.wrapping_add(1);
230                                                    }
231                                                    e.insert(custom_thread_id);
232
233                                                    let args = format_args!(
234                                                        r#"{separator}{{"ph":"M","pid":{name_sys_pid},"tid":{custom_thread_id},"name":"thread_name","args":{{"name":{name}}}}}"#,
235                                                    );
236                                                    out.write_fmt(args)
237                                                        .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
238
239                                                    custom_thread_id
240                                                }
241                                            };
242                                            *n = tid.into();
243                                        }
244                                    }
245                                    Phase::Begin | Phase::End => {
246                                        // convert "thread" arg to actual tid
247                                        if let Some(serde_json::Value::String(custom_thread)) = args.remove("thread")
248                                            && let Some(serde_json::Value::Number(n)) = entry.get_mut("tid")
249                                        {
250                                            let tid = match custom_threads.entry(custom_thread.clone()) {
251                                                std::collections::hash_map::Entry::Occupied(e) => *e.get(),
252                                                std::collections::hash_map::Entry::Vacant(e) => {
253                                                    let name = serde_json::Value::String(e.key().clone());
254
255                                                    while !thread_ids.insert(custom_thread_id) {
256                                                        custom_thread_id = custom_thread_id.wrapping_add(1);
257                                                    }
258                                                    e.insert(custom_thread_id);
259
260                                                    let args = format_args!(
261                                                        r#"{separator}{{"ph":"M","pid":{name_sys_pid},"tid":{custom_thread_id},"name":"thread_name","args":{{"name":{name}}}}}"#,
262                                                    );
263                                                    out.write_fmt(args)
264                                                        .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
265
266                                                    custom_thread_id
267                                                }
268                                            };
269                                            *n = tid.into();
270                                        }
271                                    }
272                                    Phase::Other => {}
273                                }
274                            }
275                        }
276                        _ => {
277                            error!("unknown format in {}", trace.display());
278                        }
279                    }
280
281                    out.write_all(separator.as_bytes())
282                        .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
283                    serde_json::to_writer(&mut out, &entry).unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
284                    separator = ",\n";
285                }
286                Err(_) => break,
287            }
288        }
289    }
290
291    out.write_all(b"\n]")
292        .unwrap_or_else(|e| fatal!("cannot write {}, {e}", out_file.display()));
293    println!("saved to {}", out_file.display());
294
295    if code == 0 {
296        crate::util::exit();
297    } else {
298        // forward the exit code from the exe or cmd
299        std::process::exit(code);
300    }
301}