lib/tracy/src/cli/args/context.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.ContextArgs {
  7     if (args.len == 0) return error.InvalidArguments;
  8     var parsed = types.ContextArgs{ .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.context.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.context.Sort.fromName(args[index]) orelse return error.UnsupportedSort;
 39         } else if (std.mem.eql(u8, args[index], "--thread")) {
 40             index += 1;
 41             if (index >= args.len) return error.InvalidArguments;
 42             parsed.options.thread = try scalar.parseU64(args[index]);
 43         } else if (std.mem.eql(u8, args[index], "--cpu")) {
 44             index += 1;
 45             if (index >= args.len) return error.InvalidArguments;
 46             parsed.options.cpu = @intCast(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 "context args parse filters window and grouping" {
 89     const parsed = try parse(&.{
 90         "events.jsonl",
 91         "--format",
 92         "jsonl",
 93         "--top",
 94         "12",
 95         "--occurrences",
 96         "4",
 97         "--group",
 98         "reason",
 99         "--sort",
100         "latency",
101         "--thread",
102         "7",
103         "--cpu",
104         "2",
105         "--around-ns",
106         "1000",
107         "--window-ns",
108         "200",
109         "--match",
110         "mutex",
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.context.Group.reason, parsed.options.group);
118     try std.testing.expectEqual(tracy.context.Sort.latency, parsed.options.sort);
119     try std.testing.expectEqual(@as(?u64, 7), parsed.options.thread);
120     try std.testing.expectEqual(@as(?u32, 2), parsed.options.cpu);
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("mutex", parsed.options.match.?);
124     try std.testing.expect(parsed.options.ignore_case);
125 }