LCOV - code coverage report
Current view: top level - src/core/support - time_precise.h (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 4 0.0 %
Date: 2015-10-10 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #ifndef GRPC_CORE_SUPPORT_TIME_PRECISE_H_
      35             : #define GRPC_CORE_SUPPORT_TIME_PRECISE_H_
      36             : 
      37             : #include <grpc/support/sync.h>
      38             : #include <grpc/support/time.h>
      39             : #include <stdio.h>
      40             : 
      41             : #ifdef GRPC_TIMERS_RDTSC
      42             : #if defined(__i386__)
      43             : static void gpr_get_cycle_counter(long long int *clk) {
      44             :   long long int ret;
      45             :   __asm__ volatile("rdtsc" : "=A"(ret));
      46             :   *clk = ret;
      47             : }
      48             : 
      49             : // ----------------------------------------------------------------
      50             : #elif defined(__x86_64__) || defined(__amd64__)
      51             : static void gpr_get_cycle_counter(long long int *clk) {
      52             :   unsigned long long low, high;
      53             :   __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
      54             :   *clk = (high << 32) | low;
      55             : }
      56             : #endif
      57             : 
      58             : static gpr_once precise_clock_init = GPR_ONCE_INIT;
      59             : static long long cycles_per_second = 0;
      60             : static void gpr_precise_clock_init() {
      61             :   time_t start = time(NULL);
      62             :   gpr_precise_clock start_cycle;
      63             :   gpr_precise_clock end_cycle;
      64             :   while (time(NULL) == start)
      65             :     ;
      66             :   gpr_get_cycle_counter(&start_cycle);
      67             :   while (time(NULL) == start + 1)
      68             :     ;
      69             :   gpr_get_cycle_counter(&end_cycle);
      70             :   cycles_per_second = end_cycle - start_cycle;
      71             : }
      72             : 
      73             : static double grpc_precise_clock_scaling_factor() {
      74             :   gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
      75             :   return 1e6 / cycles_per_second;
      76             : }
      77             : 
      78             : static void gpr_precise_clock_now(gpr_timespec *clk) {
      79             :   long long int counter;
      80             :   gpr_get_cycle_counter(&counter);
      81             :   clk->clock = GPR_CLOCK_REALTIME;
      82             :   clk->tv_sec = counter / cycles_per_second;
      83             :   clk->tv_nsec = counter % cycles_per_second;
      84             : }
      85             : 
      86             : #else  /* GRPC_TIMERS_RDTSC */
      87           0 : static void gpr_precise_clock_now(gpr_timespec *clk) {
      88           0 :   *clk = gpr_now(GPR_CLOCK_REALTIME);
      89           0 :   clk->clock_type = GPR_CLOCK_PRECISE;
      90           0 : }
      91             : #endif /* GRPC_TIMERS_RDTSC */
      92             : 
      93             : #endif /* GRPC_CORE_SUPPORT_TIME_PRECISE_ */

Generated by: LCOV version 1.10