| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/os.h> |
| 20 |
#include <pj/compat/high_precision.h> |
| 21 |
|
| 22 |
#if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0 |
| 23 |
|
| 24 |
#define U32MAX (0xFFFFFFFFUL) |
| 25 |
#define NANOSEC (1000000000UL) |
| 26 |
#define USEC (1000000UL) |
| 27 |
#define MSEC (1000) |
| 28 |
|
| 29 |
#define u64tohighprec(u64) ((pj_highprec_t)((pj_int64_t)(u64))) |
| 30 |
|
| 31 |
static pj_highprec_t get_elapsed( const pj_timestamp *start, |
| 32 |
const pj_timestamp *stop ) |
| 33 |
{ |
| 34 |
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 |
| 35 |
return u64tohighprec(stop->u64 - start->u64); |
| 36 |
#else |
| 37 |
pj_highprec_t elapsed_hi, elapsed_lo; |
| 38 |
|
| 39 |
elapsed_hi = stop->u32.hi - start->u32.hi; |
| 40 |
elapsed_lo = stop->u32.lo - start->u32.lo; |
| 41 |
|
| 42 |
|
| 43 |
pj_highprec_mul(elapsed_hi, U32MAX); |
| 44 |
|
| 45 |
return elapsed_hi + elapsed_lo; |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
static pj_highprec_t elapsed_msec( const pj_timestamp *start, |
| 50 |
const pj_timestamp *stop ) |
| 51 |
{ |
| 52 |
pj_timestamp ts_freq; |
| 53 |
pj_highprec_t freq, elapsed; |
| 54 |
|
| 55 |
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS) |
| 56 |
return 0; |
| 57 |
|
| 58 |
|
| 59 |
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 |
| 60 |
freq = u64tohighprec(ts_freq.u64); |
| 61 |
#else |
| 62 |
freq = ts_freq.u32.hi; |
| 63 |
pj_highprec_mul(freq, U32MAX); |
| 64 |
freq += ts_freq.u32.lo; |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
elapsed = get_elapsed(start, stop); |
| 69 |
|
| 70 |
|
| 71 |
pj_highprec_div(freq, MSEC); |
| 72 |
|
| 73 |
|
| 74 |
if (freq == 0) { |
| 75 |
|
| 76 |
pj_highprec_mul(elapsed, MSEC); |
| 77 |
} else { |
| 78 |
pj_highprec_div(elapsed, freq); |
| 79 |
} |
| 80 |
return elapsed; |
| 81 |
} |
| 82 |
|
| 83 |
static pj_highprec_t elapsed_usec( const pj_timestamp *start, |
| 84 |
const pj_timestamp *stop ) |
| 85 |
{ |
| 86 |
pj_timestamp ts_freq; |
| 87 |
pj_highprec_t freq, elapsed; |
| 88 |
|
| 89 |
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS) |
| 90 |
return 0; |
| 91 |
|
| 92 |
|
| 93 |
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 |
| 94 |
freq = u64tohighprec(ts_freq.u64); |
| 95 |
#else |
| 96 |
freq = ts_freq.u32.hi; |
| 97 |
pj_highprec_mul(freq, U32MAX); |
| 98 |
freq += ts_freq.u32.lo; |
| 99 |
#endif |
| 100 |
|
| 101 |
|
| 102 |
if (freq == 0) freq = 1; |
| 103 |
|
| 104 |
|
| 105 |
elapsed = get_elapsed(start, stop); |
| 106 |
|
| 107 |
|
| 108 |
pj_highprec_mul(elapsed, USEC); |
| 109 |
pj_highprec_div(elapsed, freq); |
| 110 |
|
| 111 |
return elapsed; |
| 112 |
} |
| 113 |
|
| 114 |
PJ_DEF(pj_uint32_t) pj_elapsed_nanosec( const pj_timestamp *start, |
| 115 |
const pj_timestamp *stop ) |
| 116 |
{ |
| 117 |
pj_timestamp ts_freq; |
| 118 |
pj_highprec_t freq, elapsed; |
| 119 |
|
| 120 |
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS) |
| 121 |
return 0; |
| 122 |
|
| 123 |
|
| 124 |
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 |
| 125 |
freq = u64tohighprec(ts_freq.u64); |
| 126 |
#else |
| 127 |
freq = ts_freq.u32.hi; |
| 128 |
pj_highprec_mul(freq, U32MAX); |
| 129 |
freq += ts_freq.u32.lo; |
| 130 |
#endif |
| 131 |
|
| 132 |
|
| 133 |
if (freq == 0) freq = 1; |
| 134 |
|
| 135 |
|
| 136 |
elapsed = get_elapsed(start, stop); |
| 137 |
|
| 138 |
|
| 139 |
pj_highprec_mul(elapsed, NANOSEC); |
| 140 |
pj_highprec_div(elapsed, freq); |
| 141 |
|
| 142 |
return (pj_uint32_t)elapsed; |
| 143 |
} |
| 144 |
|
| 145 |
PJ_DEF(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start, |
| 146 |
const pj_timestamp *stop ) |
| 147 |
{ |
| 148 |
return (pj_uint32_t)elapsed_usec(start, stop); |
| 149 |
} |
| 150 |
|
| 151 |
PJ_DEF(pj_uint32_t) pj_elapsed_msec( const pj_timestamp *start, |
| 152 |
const pj_timestamp *stop ) |
| 153 |
{ |
| 154 |
return (pj_uint32_t)elapsed_msec(start, stop); |
| 155 |
} |
| 156 |
|
| 157 |
PJ_DEF(pj_uint64_t) pj_elapsed_msec64(const pj_timestamp *start, |
| 158 |
const pj_timestamp *stop ) |
| 159 |
{ |
| 160 |
return (pj_uint64_t)elapsed_msec(start, stop); |
| 161 |
} |
| 162 |
|
| 163 |
PJ_DEF(pj_time_val) pj_elapsed_time( const pj_timestamp *start, |
| 164 |
const pj_timestamp *stop ) |
| 165 |
{ |
| 166 |
pj_highprec_t elapsed = elapsed_msec(start, stop); |
| 167 |
pj_time_val tv_elapsed; |
| 168 |
|
| 169 |
if (PJ_HIGHPREC_VALUE_IS_ZERO(elapsed)) { |
| 170 |
tv_elapsed.sec = tv_elapsed.msec = 0; |
| 171 |
return tv_elapsed; |
| 172 |
} else { |
| 173 |
pj_highprec_t sec, msec; |
| 174 |
|
| 175 |
sec = elapsed; |
| 176 |
pj_highprec_div(sec, MSEC); |
| 177 |
tv_elapsed.sec = (long)sec; |
| 178 |
|
| 179 |
msec = elapsed; |
| 180 |
pj_highprec_mod(msec, MSEC); |
| 181 |
tv_elapsed.msec = (long)msec; |
| 182 |
|
| 183 |
return tv_elapsed; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
PJ_DEF(pj_uint32_t) pj_elapsed_cycle( const pj_timestamp *start, |
| 188 |
const pj_timestamp *stop ) |
| 189 |
{ |
| 190 |
return stop->u32.lo - start->u32.lo; |
| 191 |
} |
| 192 |
|
| 193 |
PJ_DEF(pj_status_t) pj_gettickcount(pj_time_val *tv) |
| 194 |
{ |
| 195 |
pj_timestamp ts, start; |
| 196 |
pj_status_t status; |
| 197 |
|
| 198 |
if ((status = pj_get_timestamp(&ts)) != PJ_SUCCESS) |
| 199 |
return status; |
| 200 |
|
| 201 |
pj_set_timestamp32(&start, 0, 0); |
| 202 |
*tv = pj_elapsed_time(&start, &ts); |
| 203 |
|
| 204 |
return PJ_SUCCESS; |
| 205 |
} |
| 206 |
|
| 207 |
#endif |
| 208 |
|