lib/tracy/src/cli/args/sample.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const tracy = @import("../../root.zig");
3 const scalar = @import("scalar.zig");
4 const types = @import("types.zig");
5
6 pub fn parse(args: []const []const u8) !types.SampleArgs {
7 if (args.len == 0) return error.InvalidArguments;
8 var parsed = types.SampleArgs{ .path = args[0] };
9 var around_ns: ?u64 = null;
10 var window_ns: ?u64 = null;
11 var index: usize = 1;
12 while (index < args.len) : (index += 1) {
13 if (std.mem.eql(u8, args[index], "--format")) {
14 index += 1;
15 if (index >= args.len) return error.InvalidArguments;
16 if (std.mem.eql(u8, args[index], "text")) {
17 parsed.format = .text;
18 } else if (std.mem.eql(u8, args[index], "jsonl")) {
19 parsed.format = .jsonl;
20 } else {
21 return error.UnsupportedFormat;
22 }
23 } else if (std.mem.eql(u8, args[index], "--top")) {
24 index += 1;
25 if (index >= args.len) return error.InvalidArguments;
26 parsed.options.top = try scalar.parsePositiveUsize(args[index]);
27 } else if (std.mem.eql(u8, args[index], "--occurrences")) {
28 index += 1;
29 if (index >= args.len) return error.InvalidArguments;
30 parsed.options.occurrences = try scalar.parseUsize(args[index]);
31 } else if (std.mem.eql(u8, args[index], "--group")) {
32 index += 1;
33 if (index >= args.len) return error.InvalidArguments;
34 parsed.options.group = tracy.samples.Group.fromName(args[index]) orelse return error.UnsupportedGroup;
35 } else if (std.mem.eql(u8, args[index], "--sort")) {
36 index += 1;
37 if (index >= args.len) return error.InvalidArguments;
38 parsed.options.sort = tracy.samples.Sort.fromName(args[index]) orelse return error.UnsupportedSort;
39 } else if (std.mem.eql(u8, args[index], "--kind")) {
40 index += 1;
41 if (index >= args.len) return error.InvalidArguments;
42 parsed.options.kind = args[index];
43 } else if (std.mem.eql(u8, args[index], "--min-weight")) {
44 index += 1;
45 if (index >= args.len) return error.InvalidArguments;
46 parsed.options.min_weight = try scalar.parseU64(args[index]);
47 } else if (std.mem.eql(u8, args[index], "--thread")) {
48 index += 1;
49 if (index >= args.len) return error.InvalidArguments;
50 parsed.options.thread = try scalar.parseU64(args[index]);
51 } else if (std.mem.eql(u8, args[index], "--since-ns")) {
52 index += 1;
53 if (index >= args.len) return error.InvalidArguments;
54 parsed.options.since_ns = try scalar.parseU64(args[index]);
55 } else if (std.mem.eql(u8, args[index], "--until-ns")) {
56 index += 1;
57 if (index >= args.len) return error.InvalidArguments;
58 parsed.options.until_ns = try scalar.parseU64(args[index]);
59 } else if (std.mem.eql(u8, args[index], "--around-ns")) {
60 index += 1;
61 if (index >= args.len) return error.InvalidArguments;
62 around_ns = try scalar.parseU64(args[index]);
63 } else if (std.mem.eql(u8, args[index], "--window-ns")) {
64 index += 1;
65 if (index >= args.len) return error.InvalidArguments;
66 window_ns = try scalar.parseU64(args[index]);
67 } else if (std.mem.eql(u8, args[index], "--match")) {
68 index += 1;
69 if (index >= args.len) return error.InvalidArguments;
70 parsed.options.match = args[index];
71 } else if (std.mem.eql(u8, args[index], "--ignore-case")) {
72 parsed.options.ignore_case = true;
73 } else {
74 return error.UnknownArgument;
75 }
76 }
77 if (around_ns) |center| {
78 if (parsed.options.since_ns != null or parsed.options.until_ns != null) return error.InvalidArguments;
79 const width = window_ns orelse 1_000_000;
80 const half = width / 2;
81 parsed.options.since_ns = if (center > half) center - half else 0;
82 parsed.options.until_ns = center +| (width - half);
83 } else if (window_ns != null) {
84 return error.InvalidArguments;
85 }
86 if (parsed.options.since_ns != null and parsed.options.until_ns != null and parsed.options.since_ns.? > parsed.options.until_ns.?) {
87 return error.InvalidArguments;
88 }
89 return parsed;
90 }
91
92 test "sample args parse filters window and grouping" {
93 const parsed = try parse(&.{
94 "events.jsonl",
95 "--format",
96 "jsonl",
97 "--top",
98 "12",
99 "--occurrences",
100 "4",
101 "--group",
102 "stack",
103 "--sort",
104 "weight",
105 "--kind",
106 "cpu",
107 "--min-weight",
108 "2",
109 "--thread",
110 "7",
111 "--around-ns",
112 "1000",
113 "--window-ns",
114 "200",
115 "--match",
116 "parse",
117 "--ignore-case",
118 });
119 try std.testing.expectEqualStrings("events.jsonl", parsed.path);
120 try std.testing.expectEqual(types.Format.jsonl, parsed.format);
121 try std.testing.expectEqual(@as(usize, 12), parsed.options.top);
122 try std.testing.expectEqual(@as(usize, 4), parsed.options.occurrences);
123 try std.testing.expectEqual(tracy.samples.Group.stack, parsed.options.group);
124 try std.testing.expectEqual(tracy.samples.Sort.weight, parsed.options.sort);
125 try std.testing.expectEqualStrings("cpu", parsed.options.kind.?);
126 try std.testing.expectEqual(@as(u64, 2), parsed.options.min_weight);
127 try std.testing.expectEqual(@as(?u64, 7), parsed.options.thread);
128 try std.testing.expectEqual(@as(?u64, 900), parsed.options.since_ns);
129 try std.testing.expectEqual(@as(?u64, 1100), parsed.options.until_ns);
130 try std.testing.expectEqualStrings("parse", parsed.options.match.?);
131 try std.testing.expect(parsed.options.ignore_case);
132 }