LCOV - code coverage report
Current view: top level - src/core/support - time_posix.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 25 27 92.6 %
Date: 2015-10-10 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          54 : static struct timespec timespec_from_gpr(gpr_timespec gts) {
      47             :   struct timespec rv;
      48          54 :   rv.tv_sec = gts.tv_sec;
      49          54 :   rv.tv_nsec = gts.tv_nsec;
      50          54 :   return rv;
      51             : }
      52             : 
      53             : #if _POSIX_TIMERS > 0
      54    20233716 : static gpr_timespec gpr_from_timespec(struct timespec ts,
      55             :                                       gpr_clock_type clock_type) {
      56             :   gpr_timespec rv;
      57    20233716 :   rv.tv_sec = ts.tv_sec;
      58    20233716 :   rv.tv_nsec = (int)ts.tv_nsec;
      59    20233716 :   rv.clock_type = clock_type;
      60    20233716 :   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        2501 : void gpr_time_init(void) {}
      67             : 
      68    20098479 : gpr_timespec gpr_now(gpr_clock_type clock_type) {
      69             :   struct timespec now;
      70    20098479 :   GPR_ASSERT(clock_type != GPR_TIMESPAN);
      71    20098479 :   if (clock_type == GPR_CLOCK_PRECISE) {
      72             :     gpr_timespec ret;
      73           0 :     gpr_precise_clock_now(&ret);
      74           0 :     return ret;
      75             :   } else {
      76    20098479 :     clock_gettime(clockid_for_gpr_clock[clock_type], &now);
      77    20097672 :     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             :   mach_timebase_info(&tb);
      93             :   g_time_scale = tb.numer;
      94             :   g_time_scale /= tb.denom;
      95             :   g_time_start = mach_absolute_time();
      96             : }
      97             : 
      98             : gpr_timespec gpr_now(gpr_clock_type clock) {
      99             :   gpr_timespec now;
     100             :   struct timeval now_tv;
     101             :   double now_dbl;
     102             : 
     103             :   now.clock_type = clock;
     104             :   switch (clock) {
     105             :     case GPR_CLOCK_REALTIME:
     106             :       gettimeofday(&now_tv, NULL);
     107             :       now.tv_sec = now_tv.tv_sec;
     108             :       now.tv_nsec = now_tv.tv_usec * 1000;
     109             :       break;
     110             :     case GPR_CLOCK_MONOTONIC:
     111             :       now_dbl = (mach_absolute_time() - g_time_start) * g_time_scale;
     112             :       now.tv_sec = (time_t)(now_dbl * 1e-9);
     113             :       now.tv_nsec = (int)(now_dbl - ((double)now.tv_sec) * 1e9);
     114             :       break;
     115             :     case GPR_CLOCK_PRECISE:
     116             :       gpr_precise_clock_now(&now);
     117             :       break;
     118             :     case GPR_TIMESPAN:
     119             :       abort();
     120             :   }
     121             : 
     122             :   return now;
     123             : }
     124             : #endif
     125             : 
     126          54 : void gpr_sleep_until(gpr_timespec until) {
     127             :   gpr_timespec now;
     128             :   gpr_timespec delta;
     129             :   struct timespec delta_ts;
     130             :   int ns_result;
     131             : 
     132             :   for (;;) {
     133             :     /* We could simplify by using clock_nanosleep instead, but it might be
     134             :      * slightly less portable. */
     135          54 :     now = gpr_now(until.clock_type);
     136          54 :     if (gpr_time_cmp(until, now) <= 0) {
     137          51 :       return;
     138             :     }
     139             : 
     140          54 :     delta = gpr_time_sub(until, now);
     141          54 :     delta_ts = timespec_from_gpr(delta);
     142             :     GRPC_SCHEDULING_START_BLOCKING_REGION;
     143          54 :     ns_result = nanosleep(&delta_ts, NULL);
     144             :     GRPC_SCHEDULING_END_BLOCKING_REGION;
     145          54 :     if (ns_result == 0) {
     146          51 :       break;
     147             :     }
     148           3 :   }
     149             : }
     150             : 
     151             : #endif /* GPR_POSIX_TIME */

Generated by: LCOV version 1.10