diff --git a/Package.swift b/Package.swift index 27cd21c3167..a62ae22b39b 100644 --- a/Package.swift +++ b/Package.swift @@ -48,6 +48,7 @@ let package = Package( .library(name: "SwiftRefactor", type: .static, targets: ["SwiftRefactor"]), ], targets: [ + .target(name: "_InstructionCounter"), .target( name: "SwiftBasicFormat", dependencies: ["SwiftSyntax"], @@ -148,7 +149,7 @@ let package = Package( .executableTarget( name: "swift-parser-cli", dependencies: [ - "SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators", + "_InstructionCounter", "SwiftDiagnostics", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics", "SwiftOperators", .product(name: "ArgumentParser", package: "swift-argument-parser"), ] ), diff --git a/Sources/_InstructionCounter/include/InstructionsExecuted.h b/Sources/_InstructionCounter/include/InstructionsExecuted.h new file mode 100644 index 00000000000..49f8964370a --- /dev/null +++ b/Sources/_InstructionCounter/include/InstructionsExecuted.h @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#include + +/// Returns the number of instructions the process has executed since it was +/// launched. +uint64_t getInstructionsExecuted(); diff --git a/Sources/_InstructionCounter/src/InstructionsExecuted.c b/Sources/_InstructionCounter/src/InstructionsExecuted.c new file mode 100644 index 00000000000..d071e1a55b0 --- /dev/null +++ b/Sources/_InstructionCounter/src/InstructionsExecuted.c @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#include "InstructionsExecuted.h" +#include +#include + +uint64_t getInstructionsExecuted() { + struct rusage_info_v4 ru; + if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) { + return ru.ri_instructions; + } + return 0; +} diff --git a/Sources/swift-parser-cli/swift-parser-cli.swift b/Sources/swift-parser-cli/swift-parser-cli.swift index 133b4c09c5e..cf9637f5ad7 100644 --- a/Sources/swift-parser-cli/swift-parser-cli.swift +++ b/Sources/swift-parser-cli/swift-parser-cli.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +import _InstructionCounter import SwiftDiagnostics import SwiftSyntax import SwiftParser @@ -177,6 +178,7 @@ class PerformanceTest: ParsableCommand { .map { try Data(contentsOf: $0) } let start = Date() + let startInstructions = getInstructionsExecuted() for _ in 0..