LCOV - code coverage report
Current view: top level - core/support - time_posix.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 24 27 88.9 %
Date: 2015-12-10 22:15:08 Functions: 5 5 100.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             : #include <grpc/support/port_platform.h>
      35             : #include <src/core/support/time_precise.h>
      36             : 
      37             : #ifdef GPR_POSIX_TIME
      38             : 
      39             : #include <stdlib.h>
      40             : #include <time.h>
      41             : #include <unistd.h>
      42             : #include <grpc/support/log.h>
      43             : #include <grpc/support/time.h>
      44             : #include "src/core/support/block_annotate.h"
      45             : 
      46          55 : static struct timespec timespec_from_gpr(gpr_timespec gts) {
      47             :   struct timespec rv;
      48          55 :   rv.tv_sec = gts.tv_sec;
      49          55 :   rv.tv_nsec = gts.tv_nsec;
      50          55 :   return rv;
      51             : }
      52             : 
      53             : #if _POSIX_TIMERS > 0
      54    23357476 : static gpr_timespec gpr_from_timespec(struct timespec ts,
      55             :                                       gpr_clock_type clock_type) {
      56             :   gpr_timespec rv;
      57    23357476 :   rv.tv_sec = ts.tv_sec;
      58    23393695 :   rv.tv_nsec = (int)ts.tv_nsec;
      59    23357476 :   rv.clock_type = clock_type;
      60    23357476 :   return rv;
      61             : }
      62             : 
      63             : /** maps gpr_clock_type --> clockid_t for clock_gettime */
      64             : static clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC, CLOCK_REALTIME};
      65             : 
      66        3452 : void gpr_time_init(void) { gpr_precise_clock_init(); }
      67             : 
      68    23364098 : gpr_timespec gpr_now(gpr_clock_type clock_type) {
      69             :   struct timespec now;
      70    23364098 :   GPR_ASSERT(clock_type != GPR_TIMESPAN);
      71    23364098 :   if (clock_type == GPR_CLOCK_PRECISE) {
      72             :     gpr_timespec ret;
      73           0 :     gpr_precise_clock_now(&ret);
      74           0 :     return ret;
      75             :   } else {
      76    23364098 :     clock_gettime(clockid_for_gpr_clock[clock_type], &now);
      77    23346158 :     return gpr_from_timespec(now, clock_type);
      78             :   }
      79             : }
      80             : #else
      81             : /* For some reason Apple's OSes haven't implemented clock_gettime. */
      82             : 
      83             : #include <sys/time.h>
      84             : #include <mach/mach.h>
      85             : #include <mach/mach_time.h>
      86             : 
      87             : static double g_time_scale;
      88             : static uint64_t g_time_start;
      89             : 
      90             : void gpr_time_init(void) {
      91             :   mach_timebase_info_data_t tb = {0, 1};
      92             :   gpr_precise_clock_init();
      93             :   mach_timebase_info(&tb);
      94             :   g_time_scale = tb.numer;
      95             :   g_time_scale /= tb.denom;
      96             :   g_time_start = mach_absolute_time();
      97             : }
      98             : 
      99             : gpr_timespec gpr_now(gpr_clock_type clock) {
     100             :   gpr_timespec now;
     101             :   struct timeval now_tv;
     102             :   double now_dbl;
     103             : 
     104             :   now.clock_type = clock;
     105             :   switch (clock) {
     106             :     case GPR_CLOCK_REALTIME:
     107             :       gettimeofday(&now_tv, NULL);
     108             :       now.tv_sec = now_tv.tv_sec;
     109             :       now.tv_nsec = now_tv.tv_usec * 1000;
     110             :       break;
     111             :     case GPR_CLOCK_MONOTONIC:
     112             :       now_dbl = (mach_absolute_time() - g_time_start) * g_time_scale;
     113             :       now.tv_sec = (time_t)(now_dbl * 1e-9);
     114             :       now.tv_nsec = (int)(now_dbl - ((double)now.tv_sec) * 1e9);
     115             :       break;
     116             :     case GPR_CLOCK_PRECISE:
     117             :       gpr_precise_clock_now(&now);
     118             :       break;
     119             :     case GPR_TIMESPAN:
     120             :       abort();
     121             :   }
     122             : 
     123             :   return now;
     124             : }
     125             : #endif
     126             : 
     127          55 : void gpr_sleep_until(gpr_timespec until) {
     128             :   gpr_timespec now;
     129             :   gpr_timespec delta;
     130             :   struct timespec delta_ts;
     131             :   int ns_result;
     132             : 
     133             :   for (;;) {
     134             :     /* We could simplify by using clock_nanosleep instead, but it might be
     135             :      * slightly less portable. */
     136          55 :     now = gpr_now(until.clock_type);
     137          55 :     if (gpr_time_cmp(until, now) <= 0) {
     138          55 :       return;
     139             :     }
     140             : 
     141          55 :     delta = gpr_time_sub(until, now);
     142          55 :     delta_ts = timespec_from_gpr(delta);
     143             :     GRPC_SCHEDULING_START_BLOCKING_REGION;
     144          55 :     ns_result = nanosleep(&delta_ts, NULL);
     145             :     GRPC_SCHEDULING_END_BLOCKING_REGION;
     146          55 :     if (ns_result == 0) {
     147          55 :       break;
     148             :     }
     149           0 :   }
     150             : }
     151             : 
     152             : #endif /* GPR_POSIX_TIME */

Generated by: LCOV version 1.11