@@ -230,41 +230,29 @@ func stopTrackingObjects(_: UnsafeMutableRawPointer) -> Int
230
230
231
231
#endif
232
232
233
- class SampleRunner {
234
- #if !os(Linux)
235
- var info = mach_timebase_info_data_t ( numer: 0 , denom: 0 )
236
- #endif
237
- init ( ) {
238
- #if !os(Linux)
239
- mach_timebase_info ( & info)
240
- #endif
241
- }
233
+ protocol SampleRunner {
234
+ func run( _ name: String , fn: ( Int ) -> Void , num_iters: UInt ) -> UInt64
235
+ }
236
+
237
+ #if os(Linux)
238
+ class POSIXSampleRunner : SampleRunner {
242
239
func run( _ name: String , fn: ( Int ) -> Void , num_iters: UInt ) -> UInt64 {
243
240
// Start the timer.
244
241
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
245
242
var str = name
246
243
startTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
247
244
#endif
248
- #if os(Linux)
249
245
var start_ticks = timespec ( tv_sec: 0 , tv_nsec: 0 )
250
246
clock_gettime ( CLOCK_REALTIME, & start_ticks)
251
- #else
252
- let start_ticks = mach_absolute_time ( )
253
- #endif
254
247
fn ( Int ( num_iters) )
255
248
// Stop the timer.
256
- #if os(Linux)
257
249
var end_ticks = timespec ( tv_sec: 0 , tv_nsec: 0 )
258
250
clock_gettime ( CLOCK_REALTIME, & end_ticks)
259
- #else
260
- let end_ticks = mach_absolute_time ( )
261
- #endif
262
251
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
263
252
stopTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
264
253
#endif
265
254
266
255
// Compute the spent time and the scaling factor.
267
- #if os(Linux)
268
256
var elapsed_ticks = timespec ( tv_sec: 0 , tv_nsec: 0 )
269
257
if end_ticks. tv_nsec - start_ticks. tv_nsec < 0 {
270
258
elapsed_ticks. tv_sec = end_ticks. tv_sec - start_ticks. tv_sec - 1
@@ -274,12 +262,34 @@ class SampleRunner {
274
262
elapsed_ticks. tv_nsec = end_ticks. tv_nsec - start_ticks. tv_nsec
275
263
}
276
264
return UInt64 ( elapsed_ticks. tv_sec) * UInt64( 1000000000 ) + UInt64( elapsed_ticks. tv_nsec)
265
+ }
266
+ }
277
267
#else
268
+ class DarwinSampleRunner : SampleRunner {
269
+ var info = mach_timebase_info_data_t ( numer: 0 , denom: 0 )
270
+ init ( ) {
271
+ mach_timebase_info ( & info)
272
+ }
273
+ func run( _ name: String , fn: ( Int ) -> Void , num_iters: UInt ) -> UInt64 {
274
+ // Start the timer.
275
+ #if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
276
+ var str = name
277
+ startTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
278
+ #endif
279
+ let start_ticks = mach_absolute_time ( )
280
+ fn ( Int ( num_iters) )
281
+ // Stop the timer.
282
+ let end_ticks = mach_absolute_time ( )
283
+ #if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
284
+ stopTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
285
+ #endif
286
+
287
+ // Compute the spent time and the scaling factor.
278
288
let elapsed_ticks = end_ticks - start_ticks
279
289
return elapsed_ticks * UInt64( info. numer) / UInt64( info. denom)
280
- #endif
281
290
}
282
291
}
292
+ #endif
283
293
284
294
/// Invoke the benchmark entry point and return the run time in milliseconds.
285
295
func runBench( _ name: String , _ fn: ( Int ) -> Void , _ c: TestConfig ) -> BenchResults {
@@ -290,7 +300,11 @@ func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResu
290
300
print ( " Running \( name) for \( c. numSamples) samples. " )
291
301
}
292
302
293
- let sampler = SampleRunner ( )
303
+ #if os(Linux)
304
+ let sampler : SampleRunner = POSIXSampleRunner ( )
305
+ #else
306
+ let sampler : SampleRunner = DarwinSampleRunner ( )
307
+ #endif
294
308
for s in 0 ..< c. numSamples {
295
309
let time_per_sample : UInt64 = 1_000_000_000 * UInt64( c. iterationScale)
296
310
0 commit comments