lib/tracy/src/cli/args/fiber.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.FiberArgs {
7 if (args.len == 0) return error.InvalidArguments;
8 var parsed = types.FiberArgs{ .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.fibers.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.fibers.Sort.fromName(args[index]) orelse return error.UnsupportedSort;
39 } else if (std.mem.eql(u8, args[index], "--fiber")) {
40 index += 1;
41 if (index >= args.len) return error.InvalidArguments;
42 parsed.options.fiber = try scalar.parseU64(args[index]);
43 } else if (std.mem.eql(u8, args[index], "--thread")) {
44 index += 1;
45 if (index >= args.len) return error.InvalidArguments;
46 parsed.options.thread = try scalar.parseU64(args[index]);
47 } else if (std.mem.eql(u8, args[index], "--since-ns")) {
48 index += 1;
49 if (index >= args.len) return error.InvalidArguments;
50 parsed.options.since_ns = try scalar.parseU64(args[index]);
51 } else if (std.mem.eql(u8, args[index], "--until-ns")) {
52 index += 1;
53 if (index >= args.len) return error.InvalidArguments;
54 parsed.options.until_ns = try scalar.parseU64(args[index]);
55 } else if (std.mem.eql(u8, args[index], "--around-ns")) {
56 index += 1;
57 if (index >= args.len) return error.InvalidArguments;
58 around_ns = try scalar.parseU64(args[index]);
59 } else if (std.mem.eql(u8, args[index], "--window-ns")) {
60 index += 1;
61 if (index >= args.len) return error.InvalidArguments;
62 window_ns = try scalar.parseU64(args[index]);
63 } else if (std.mem.eql(u8, args[index], "--match")) {
64 index += 1;
65 if (index >= args.len) return error.InvalidArguments;
66 parsed.options.match = args[index];
67 } else if (std.mem.eql(u8, args[index], "--ignore-case")) {
68 parsed.options.ignore_case = true;
69 } else {
70 return error.UnknownArgument;
71 }
72 }
73 if (around_ns) |center| {
74 if (parsed.options.since_ns != null or parsed.options.until_ns != null) return error.InvalidArguments;
75 const width = window_ns orelse 1_000_000;
76 const half = width / 2;
77 parsed.options.since_ns = if (center > half) center - half else 0;
78 parsed.options.until_ns = center +| (width - half);
79 } else if (window_ns != null) {
80 return error.InvalidArguments;
81 }
82 if (parsed.options.since_ns != null and parsed.options.until_ns != null and parsed.options.since_ns.? > parsed.options.until_ns.?) {
83 return error.InvalidArguments;
84 }
85 return parsed;
86 }
87
88 test "fiber args parse filters window grouping and fiber id" {
89 const parsed = try parse(&.{
90 "events.jsonl",
91 "--format",
92 "jsonl",
93 "--top",
94 "12",
95 "--occurrences",
96 "4",
97 "--group",
98 "transition",
99 "--sort",
100 "migrations",
101 "--fiber",
102 "9",
103 "--thread",
104 "7",
105 "--around-ns",
106 "1000",
107 "--window-ns",
108 "200",
109 "--match",
110 "parse",
111 "--ignore-case",
112 });
113 try std.testing.expectEqualStrings("events.jsonl", parsed.path);
114 try std.testing.expectEqual(types.Format.jsonl, parsed.format);
115 try std.testing.expectEqual(@as(usize, 12), parsed.options.top);
116 try std.testing.expectEqual(@as(usize, 4), parsed.options.occurrences);
117 try std.testing.expectEqual(tracy.fibers.Group.transition, parsed.options.group);
118 try std.testing.expectEqual(tracy.fibers.Sort.migrations, parsed.options.sort);
119 try std.testing.expectEqual(@as(?u64, 9), parsed.options.fiber);
120 try std.testing.expectEqual(@as(?u64, 7), parsed.options.thread);
121 try std.testing.expectEqual(@as(?u64, 900), parsed.options.since_ns);
122 try std.testing.expectEqual(@as(?u64, 1100), parsed.options.until_ns);
123 try std.testing.expectEqualStrings("parse", parsed.options.match.?);
124 try std.testing.expect(parsed.options.ignore_case);
125 }