Skip to content

Commit 9d92ceb

Browse files
committed
Only output number of executed instructions in swift-parser-cli on macOS
`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.
1 parent 96e6850 commit 9d92ceb

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Sources/_InstructionCounter/include/InstructionsExecuted.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
#include <unistd.h>
1414

15-
/// Returns the number of instructions the process has executed since it was
16-
/// launched.
15+
/// On macOS returns the number of instructions the process has executed since
16+
/// it was launched, on all other platforms returns 0.
1717
uint64_t getInstructionsExecuted();

Sources/_InstructionCounter/src/InstructionsExecuted.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if __APPLE__
14+
#include <TargetConditionals.h>
15+
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
16+
#define TARGET_IS_MACOS 1
17+
#endif
18+
#endif
19+
1320
#include "InstructionsExecuted.h"
21+
22+
#ifdef TARGET_IS_MACOS
1423
#include <libproc.h>
1524
#include <sys/resource.h>
1625

@@ -21,3 +30,8 @@ uint64_t getInstructionsExecuted() {
2130
}
2231
return 0;
2332
}
33+
#else
34+
uint64_t getInstructionsExecuted() {
35+
return 0;
36+
}
37+
#endif

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ class PerformanceTest: ParsableCommand {
190190
let endDate = Date()
191191

192192
print("Time: \(endDate.timeIntervalSince(start) / Double(self.iterations) * 1000)ms")
193-
print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))")
193+
if endInstructions != startInstructions {
194+
// endInstructions == startInstructions only happens if we are on non-macOS
195+
// platforms that don't support `getInstructionsExecuted`. Don't display anything.
196+
print("Instructions: \(Double(endInstructions - startInstructions) / Double(self.iterations))")
197+
}
194198
}
195199
}
196200

0 commit comments

Comments
 (0)