2  * Copyright (c) 2008-2009 Apple Inc. All rights reserved. 
   4  * @APPLE_APACHE_LICENSE_HEADER_START@ 
   6  * Licensed under the Apache License, Version 2.0 (the "License"); 
   7  * you may not use this file except in compliance with the License. 
   8  * You may obtain a copy of the License at 
  10  *     http://www.apache.org/licenses/LICENSE-2.0 
  12  * Unless required by applicable law or agreed to in writing, software 
  13  * distributed under the License is distributed on an "AS IS" BASIS, 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  15  * See the License for the specific language governing permissions and 
  16  * limitations under the License. 
  18  * @APPLE_APACHE_LICENSE_HEADER_END@ 
  24 struct __dispatch_benchmark_data_s 
{ 
  25         mach_timebase_info_data_t tbi
; 
  33 _dispatch_benchmark_init(void *context
) 
  35         struct __dispatch_benchmark_data_s 
*bdata 
= context
; 
  36         // try and simulate performance of real benchmark as much as possible 
  37         // keep 'f', 'c' and 'cnt' in registers 
  38         register void (*f
)(void *) = bdata
->func
; 
  39         register void *c 
= bdata
->ctxt
; 
  40         register size_t cnt 
= bdata
->count
; 
  41         uint64_t start
, delta
; 
  50         kr 
= mach_timebase_info(&bdata
->tbi
); 
  51         dispatch_assert_zero(kr
); 
  53         start 
= mach_absolute_time(); 
  58         delta 
= mach_absolute_time() - start
; 
  61         lcost 
*= bdata
->tbi
.numer
; 
  62         lcost 
/= bdata
->tbi
.denom
; 
  65         bdata
->loop_cost 
= lcost
; 
  70 dispatch_benchmark(size_t count
, void (^block
)(void)) 
  72         struct Block_basic 
*bb 
= (void *)block
; 
  73         return dispatch_benchmark_f(count
, block
, (void *)bb
->Block_invoke
); 
  78 dispatch_benchmark_f(size_t count
, register void *ctxt
, register void (*func
)(void *)) 
  80         static struct __dispatch_benchmark_data_s bdata 
= { 
  81                 .func 
= (void *)dummy_function
, 
  82                 .count 
= 10000000ul, // ten million 
  84         static dispatch_once_t pred
; 
  85         uint64_t ns
, start
, delta
; 
  87         __uint128_t conversion
, big_denom
; 
  89         long double conversion
, big_denom
; 
  93         dispatch_once_f(&pred
, &bdata
, _dispatch_benchmark_init
); 
  95         if (slowpath(count 
== 0)) { 
  99         start 
= mach_absolute_time(); 
 104         delta 
= mach_absolute_time() - start
; 
 107         conversion 
*= bdata
.tbi
.numer
; 
 108         big_denom 
= bdata
.tbi
.denom
; 
 110         conversion 
/= big_denom
; 
 113         return ns 
- bdata
.loop_cost
;