time.c (1901B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: time.c 8 * Purpose: Time management 9 */ 10 11 12 #include <sys/types.h> 13 #include <sys/time.h> 14 #if defined(linux) || (__STRICT_ANSI__) 15 #include <time.h> 16 #endif 17 #include <sal/core/time.h> 18 19 #ifdef COMPILER_HAS_DOUBLE 20 21 /* 22 * Function: 23 * sal_time_double 24 * Purpose: 25 * Returns the time since boot in seconds. 26 * Returns: 27 * Double precision floating-point seconds. 28 * Notes: 29 * Useful for benchmarking. 30 * Made to be as accurate as possible on the given platform. 31 */ 32 33 double 34 sal_time_double(void) 35 { 36 struct timeval tv; 37 gettimeofday(&tv, 0); 38 return (tv.tv_sec + tv.tv_usec * 0.000001); 39 } 40 41 #endif /* COMPILER_HAS_DOUBLE */ 42 43 44 /* 45 * Function: 46 * sal_time_usecs 47 * Purpose: 48 * Returns the relative time in microseconds modulo 2^32. 49 * Returns: 50 * Time in microseconds modulo 2^32 51 * Notes: 52 * The precision is limited to the Unix clock period. 53 * Time is monotonic if supported by the O/S. 54 */ 55 56 sal_usecs_t 57 sal_time_usecs(void) 58 { 59 struct timeval ltv; 60 #ifdef CLOCK_MONOTONIC 61 struct timespec lts; 62 if (clock_gettime(CLOCK_MONOTONIC, <s) == 0) { 63 return (lts.tv_sec * SECOND_USEC + lts.tv_nsec / 1000); 64 } 65 #endif 66 /* Fall back to RTC if monotonic clock unavailable */ 67 gettimeofday(<v, 0); 68 return (ltv.tv_sec * SECOND_USEC + ltv.tv_usec); 69 } 70 71 /* 72 * Function: 73 * sal_time 74 * Purpose: 75 * Return the current time in seconds since 00:00, Jan 1, 1970. 76 * Returns: 77 * Time in seconds 78 * Notes: 79 * This routine must be implemented so it is safe to call from 80 * an interrupt routine. It is used for timestamping and other 81 * purposes. 82 */ 83 84 sal_time_t 85 sal_time(void) 86 { 87 /* "Interrupt safe" since intrs are fake for PLISIM */ 88 return (sal_time_t) time(0); 89 }