|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import ArgumentParser |
| 14 | +import Foundation |
| 15 | +import RegexBuilder |
| 16 | +import class TSCBasic.Process |
| 17 | + |
| 18 | +package struct ActiveRequestsCommand: AsyncParsableCommand { |
| 19 | + package static let configuration: CommandConfiguration = CommandConfiguration( |
| 20 | + commandName: "active-requests", |
| 21 | + abstract: "Shows the requests that are currently being handled by sourcekit-lsp.", |
| 22 | + discussion: "This command only works on macOS." |
| 23 | + ) |
| 24 | + |
| 25 | + @Option( |
| 26 | + name: .customLong("log-file"), |
| 27 | + help: """ |
| 28 | + Instead of reading the currently executing requests from recent system messages, read them from a log file, \ |
| 29 | + generated by `log show`. |
| 30 | + """ |
| 31 | + ) |
| 32 | + var logFile: String? |
| 33 | + |
| 34 | + package init() {} |
| 35 | + |
| 36 | + /// Read the last 3 minutes of OSLog output, including signpost messages. |
| 37 | + package func readOSLog() async throws -> String { |
| 38 | + var data = Data() |
| 39 | + let process = Process( |
| 40 | + arguments: [ |
| 41 | + "/usr/bin/log", |
| 42 | + "show", |
| 43 | + "--last", "3m", |
| 44 | + "--predicate", #"subsystem = "org.swift.sourcekit-lsp.message-handling" AND process = "sourcekit-lsp""#, |
| 45 | + "--signpost", |
| 46 | + ], |
| 47 | + outputRedirection: .stream( |
| 48 | + stdout: { data += $0 }, |
| 49 | + stderr: { _ in } |
| 50 | + ) |
| 51 | + ) |
| 52 | + try process.launch() |
| 53 | + try await process.waitUntilExit() |
| 54 | + guard let result = String(data: data, encoding: .utf8) else { |
| 55 | + throw GenericError("Failed to decode string from OS Log") |
| 56 | + } |
| 57 | + return result |
| 58 | + } |
| 59 | + |
| 60 | + package func run() async throws { |
| 61 | + let log: String |
| 62 | + if let logFile { |
| 63 | + log = try String(contentsOf: URL(fileURLWithPath: logFile), encoding: .utf8) |
| 64 | + } else { |
| 65 | + log = try await readOSLog() |
| 66 | + } |
| 67 | + let logParseRegex = Regex { |
| 68 | + /.*/ |
| 69 | + "[spid 0x" |
| 70 | + Capture { // Signpost ID |
| 71 | + OneOrMore(.hexDigit) |
| 72 | + } |
| 73 | + ", process, " |
| 74 | + ZeroOrMore(.whitespace) |
| 75 | + Capture { // Event ("begin", "event", "end") |
| 76 | + /[a-z]+/ |
| 77 | + } |
| 78 | + "]" |
| 79 | + ZeroOrMore(.any) |
| 80 | + } |
| 81 | + var messagesBySignpostID: [Substring: [Substring]] = [:] |
| 82 | + var endedSignposts: Set<Substring> = [] |
| 83 | + for line in log.split(separator: "\n") { |
| 84 | + guard let match = try logParseRegex.wholeMatch(in: line) else { |
| 85 | + continue |
| 86 | + } |
| 87 | + let (signpostID, event) = (match.1, match.2) |
| 88 | + messagesBySignpostID[signpostID, default: []].append(line) |
| 89 | + if event == "end" { |
| 90 | + endedSignposts.insert(signpostID) |
| 91 | + } |
| 92 | + } |
| 93 | + let activeSignpostMessages = |
| 94 | + messagesBySignpostID |
| 95 | + .filter({ !endedSignposts.contains($0.key) }) |
| 96 | + .sorted(by: { $0.key < $1.key }) |
| 97 | + .flatMap(\.value) |
| 98 | + print(activeSignpostMessages.joined(separator: "\n")) |
| 99 | + } |
| 100 | +} |
0 commit comments