From 9d92ceb797cc687b44b82eff7b2ed7b7f99e089b Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 28 Mar 2023 16:38:43 -0700 Subject: [PATCH] Only output number of executed instructions in `swift-parser-cli` on macOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `libproc.h` is not available on iOS and I’m not sure how it behaves on Linux. Since `swift-parser-cli` is only a testing utility, take the safe approach and only output the number of executed instructions on macOS. --- .../include/InstructionsExecuted.h | 4 ++-- .../_InstructionCounter/src/InstructionsExecuted.c | 14 ++++++++++++++ Sources/swift-parser-cli/swift-parser-cli.swift | 6 +++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Sources/_InstructionCounter/include/InstructionsExecuted.h b/Sources/_InstructionCounter/include/InstructionsExecuted.h index 49f8964370a..39fef28bfe8 100644 --- a/Sources/_InstructionCounter/include/InstructionsExecuted.h +++ b/Sources/_InstructionCounter/include/InstructionsExecuted.h @@ -12,6 +12,6 @@ #include -/// Returns the number of instructions the process has executed since it was -/// launched. +/// On macOS returns the number of instructions the process has executed since +/// it was launched, on all other platforms returns 0. uint64_t getInstructionsExecuted(); diff --git a/Sources/_InstructionCounter/src/InstructionsExecuted.c b/Sources/_InstructionCounter/src/InstructionsExecuted.c index d071e1a55b0..132d45fa799 100644 --- a/Sources/_InstructionCounter/src/InstructionsExecuted.c +++ b/Sources/_InstructionCounter/src/InstructionsExecuted.c @@ -10,7 +10,16 @@ // //===----------------------------------------------------------------------===// +#if __APPLE__ +#include +#if TARGET_OS_MAC && !TARGET_OS_IPHONE +#define TARGET_IS_MACOS 1 +#endif +#endif + #include "InstructionsExecuted.h" + +#ifdef TARGET_IS_MACOS #include #include @@ -21,3 +30,8 @@ uint64_t getInstructionsExecuted() { } return 0; } +#else +uint64_t getInstructionsExecuted() { + return 0; +} +#endif diff --git a/Sources/swift-parser-cli/swift-parser-cli.swift b/Sources/swift-parser-cli/swift-parser-cli.swift index cf9637f5ad7..1e87ff2f3d0 100644 --- a/Sources/swift-parser-cli/swift-parser-cli.swift +++ b/Sources/swift-parser-cli/swift-parser-cli.swift @@ -190,7 +190,11 @@ class PerformanceTest: ParsableCommand { let endDate = Date() print("Time: \(endDate.timeIntervalSince(start) / Double(self.iterations) * 1000)ms") - print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))") + if endInstructions != startInstructions { + // endInstructions == startInstructions only happens if we are on non-macOS + // platforms that don't support `getInstructionsExecuted`. Don't display anything. + print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))") + } } }