su_time.c 11.1 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2005,2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_time
 * @CFILE su_time.c
 *
 * @brief Implementation of OS-independent time functions.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Jari Selin <Jari.Selin@nokia.com>
32
 * @author Kai Vehmanen <first.surname@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
33 34 35 36 37 38 39 40 41 42 43
 * 
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <time.h>

44
#include "sofia-sip/su_types.h"
Pekka Pessi's avatar
Pekka Pessi committed
45 46
#include "su_module_debug.h"

Pekka Pessi's avatar
Pekka Pessi committed
47 48 49 50 51 52 53 54
/* Include bodies of inlined functions */
#undef su_inline
#define su_inline
#undef SU_HAVE_INLINE
#define SU_HAVE_INLINE 1

#include "sofia-sip/su_time.h"

Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61 62 63
#if HAVE_SYS_TIME_H
#include <sys/time.h> /* Get struct timeval */
#endif

/**@defgroup su_time Time Handling
 *
 * OS-independent timing functions and types for the @b su library.
 *  
 * The @b su library provides three different time formats with different
Pekka Pessi's avatar
Pekka Pessi committed
64
 * ranges and epochs in <sofia-sip/su_time.h>:
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
 *
 *   - #su_time_t, second and microsecond as 32-bit values since 1900,
 *   - #su_duration_t, milliseconds between two times, and
 *   - #su_ntp_t, standard NTP timestamp (seconds since 1900 as 
 *     a fixed-point 64-bit value with 32 bits representing subsecond 
 *     value).
 */

/**
 * Compare two timestamps.
 * 
 * The function su_time_cmp() compares two su_time_t timestamps.
 * 
 * @param t1 first NTP timestamp in su_time_t structure
 * @param t2 second NTP timestamp in su_time_t structure
 * 
 * @retval  Negative, if @a t1 is before @a t2,
 * @retval  Zero, if @a t1 is same as @a t2, or
 * @retval  Positive, if @a t1 is after @a t2.
 * 
 */
long su_time_cmp(su_time_t const t1, su_time_t const t2)
{
88 89 90 91 92 93 94 95 96 97 98 99
  long retval = 0;

  if (t1.tv_sec > t2.tv_sec) 
    retval = 1;
  else if (t1.tv_sec < t2.tv_sec) 
    retval = -1;
  else {
    if (t1.tv_usec > t2.tv_usec) 
      retval = 1;
    else if (t1.tv_usec < t2.tv_usec) 
      retval = -1;
  }
Pekka Pessi's avatar
Pekka Pessi committed
100 101 102 103

  return retval;
}

Pekka Pessi's avatar
Pekka Pessi committed
104
/**@def SU_TIME_CMP(t1, t2) 
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108 109 110 111 112 113 114 115
 *
 * Compare two timestamps.
 *
 * The macro SU_TIME_CMP() compares two su_time_t timestamps.
 *
 * @param t1   first NTP timestamp in su_time_t structure
 * @param t2   second NTP timestamp in su_time_t  structure
 * 
 * @retval negative, if t1 is before t2,
 * @retval zero,     if t1 is same as t2, or
 * @retval positive, if t1 is after t2.
Pekka Pessi's avatar
Pekka Pessi committed
116 117
 *
 * @hideinitializer
Pekka Pessi's avatar
Pekka Pessi committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
 */

/** Difference between two timestamps.
 *
 * The function returns difference between two timestamps 
 * in seconds (t1 - t2).
 *
 * @param t1   first timeval
 * @param t2   second timeval
 * 
 * @return
 *    The difference between two timestamps in seconds as a double.
 */
double su_time_diff(su_time_t const t1, su_time_t const t2)
{
  return
    ((double)t1.tv_sec - (double)t2.tv_sec) 
    + (double)((long)t1.tv_usec - (long)t2.tv_usec) / 1000000.0;
}

/** Get current time.
 *
 *   Return the current timestamp in su_time_t structure.
 *
 * @return
 *   The structure containing the current NTP timestamp.
 */
su_time_t su_now(void)
{
  su_time_t retval;
  su_time(&retval);
  return retval;
}

/** Print su_time_t timestamp.
 *
 *   This function prints a su_time_t timestamp as a decimal number to the 
 *   given buffer.
 *
 * @param s    pointer to buffer
 * @param n    buffer size
 * @param tv   pointer to the timeval object
 * 
 * @return 
 *   The number of characters printed, excluding the final @c NUL.
 */
int su_time_print(char *s, int n, su_time_t const *tv)
{
#ifdef _WIN32
  return _snprintf(s, n, "%lu.%06lu", tv->tv_sec, tv->tv_usec);
#else
  return snprintf(s, n, "%lu.%06lu", tv->tv_sec, tv->tv_usec);
#endif
}

/** Time difference in milliseconds.
 *
175
 * Calculates the duration from t2 to t1 in milliseconds.  
Pekka Pessi's avatar
Pekka Pessi committed
176 177 178 179
 *
 * @param t1   after time
 * @param t2   before time
 * 
180 181 182 183 184
 * @return The duration in milliseconds between the two times.
 * If the difference is bigger than #SU_DURATION_MAX, return #SU_DURATION_MAX
 * instead.
 * If the difference is smaller than -#SU_DURATION_MAX, return
 * -#SU_DURATION_MAX.
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
 */
su_duration_t su_duration(su_time_t const t1, su_time_t const t2)
{
  su_duration_t diff, udiff, tdiff;

  diff = t1.tv_sec - t2.tv_sec;
  udiff = t1.tv_usec - t2.tv_usec;

  tdiff = diff * 1000 + udiff / 1000;

  if (diff > (SU_DURATION_MAX / 1000) || (diff > 0 && diff > tdiff))
    return SU_DURATION_MAX;
  if (diff < (-SU_DURATION_MAX / 1000) || (diff < 0 && diff < tdiff))
    return -SU_DURATION_MAX;

  return tdiff;
}

typedef uint64_t su_t64_t;	/* time with 64 bits */

const uint32_t su_res32 = 1000000UL;
const su_t64_t su_res64 = (su_t64_t)1000000UL;

#define SU_TIME_TO_T64(tv) \
  (su_res64 * (su_t64_t)(tv).tv_sec + (su_t64_t)(tv).tv_usec)
#define SU_DUR_TO_T64(d) (1000 * (int64_t)(d))

212 213
#define E9 (1000000000)

Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220 221 222 223
/** Get NTP timestamp.
 *
 * The function su_ntp_now() returns the current NTP timestamp.  NTP
 * timestamp is seconds elapsed since January 1st, 1900.
 * 
 * @return
 * The current time as NTP timestamp is returned.
 */
su_ntp_t su_ntp_now(void)
{
224
  su_nanotime_t now;
Pekka Pessi's avatar
Pekka Pessi committed
225

226
  su_nanotime(&now);
Pekka Pessi's avatar
Pekka Pessi committed
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  if (sizeof(long) == sizeof(now)) {
    return ((now / E9) << 32) | ((((now % E9) << 32) + E9/2) / E9);
  }
  else {
    su_nanotime_t usec;
    int nsec;
    unsigned rem;

    usec = now / 1000;
    nsec = now % 1000;
    
    /*
     * Multiply usec by 4294.967296 (ie. 2**32 / 1E6)
     * 
     * Utilize fact that 4294.967296 == 4295 - 511 / 15625 
     */
    now = 4295 * usec - 511 * usec / 15625;
    rem = (511U * usec) % 15625U;
Pekka Pessi's avatar
Pekka Pessi committed
246
  
247 248 249 250 251 252 253
    /* Multiply nsec by 4.294967296 */
    nsec = 4295 * 125 * nsec - (511 * nsec) / 125;
    nsec -= 8 * rem; /* usec rounding */
    nsec = (nsec + (nsec < 0 ? -62499 : +62499)) / 125000;
    
    return now + nsec;
  }
Pekka Pessi's avatar
Pekka Pessi committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

/** Get NTP seconds.
 *
 * The function su_ntp_sec() returns the seconds elapsed since January 1st,
 * 1900.
 *
 * @return
 * The current time as NTP seconds is returned.
 */
uint32_t su_ntp_sec(void)
{
  su_time_t t;
  su_time(&t);
  return t.tv_sec;
}

/** High 32 bit of NTP timestamp.
 *
 * @param ntp 64bit NTP timestamp.
 *
 * @return The function su_ntp_hi() returns high 32 bits of NTP timestamp.
 */
uint32_t su_ntp_hi(su_ntp_t ntp)
{
  return (uint32_t) (ntp >> 32) & 0xffffffffLU;
}

su_ntp_t su_ntp_hilo(uint32_t hi, uint32_t lo)
{
284
  return ((((su_ntp_t)hi) << 32)) | lo;
Pekka Pessi's avatar
Pekka Pessi committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
}

/** Low 32 bit of NTP timestamp.
 *
 * @param ntp 64bit NTP timestamp.
 *
 * @return The function su_ntp_hi() returns low 32 bits of NTP timestamp.
 */
uint32_t su_ntp_lo(su_ntp_t ntp)
{
  return (uint32_t) ntp & 0xffffffffLU;
}

/** Middle 32 bit of NTP timestamp.
 *
 * @param ntp 64bit NTP timestamp.
 *
 * @return The function su_ntp_mw() returns bits 48..16 (middle word) of NTP
 * timestamp.
 */
uint32_t su_ntp_mw(su_ntp_t ntp)
{
  return (uint32_t) (ntp >> 16) & 0xffffffffLU;
}

su_time_t su_t64_to_time(su_t64_t const us)
{
  su_time_t tv;

  tv.tv_sec = (unsigned long) (us / su_res64);
  tv.tv_usec = (unsigned long) (us % su_res64);

  return tv;
}

/** 
 * Add milliseconds to the time.
 *
 * @param t0  time in seconds and microseconds as @c su_time_t
 * @param dur milliseconds to be added
 */
su_time_t su_time_add(su_time_t t0, su_duration_t dur)
{
  return su_t64_to_time(SU_TIME_TO_T64(t0) + SU_DUR_TO_T64(dur));
}

/** 
 * Add seconds to the time.
 *
 * @param t0  time in seconds and microseconds as @c su_time_t
 * @param sec seconds to be added
 *
 * @return
 * New time as @c su_time_t.
 */
su_time_t su_time_dadd(su_time_t t0, double sec)
{
  return su_t64_to_time(SU_TIME_TO_T64(t0) + (int64_t)(su_res32 * sec));
}

#ifdef WIN32

#include <windows.h>

uint64_t su_nanocounter(void)
{
  static ULONGLONG counterfreq = 0;  /*performance counter frequency*/
  LARGE_INTEGER LargeIntCount = { 0, 0 };
  ULONGLONG count;

  if(!counterfreq) {
    LARGE_INTEGER Freq = { 0, 0 };
    if (!QueryPerformanceFrequency(&Freq)) { 
      SU_DEBUG_1(("su_counter: QueryPerformanceFrequency failed\n"));
      return 0;
    }
    counterfreq = Freq.QuadPart;
  }

  if (!QueryPerformanceCounter(&LargeIntCount)) {
     SU_DEBUG_1(("su_counter: QueryPeformanceCounter failed\n"));
     return 0;
  }
  count = (ULONGLONG) LargeIntCount.QuadPart;
 
  /* return value is in ns */
  return  ((count * 1000 * 1000 * 1000) / counterfreq) ; 
}

uint64_t su_counter(void)
{
  return su_nanocounter() / 1000U;
}

#elif HAVE_CLOCK_GETTIME

/** Return CPU counter value in nanoseconds.
 *
 * The function su_nanocounter() returns a CPU counter value in nanoseconds
 * for timing purposes.
 *
 * Parameters:
 * 
 * @return
 *   The current CPU counter value in nanoseconds
 */
uint64_t su_nanocounter(void)
{
  struct timespec tp;
  struct timeval tv;
  static int init = 0;
  static clockid_t cpu = CLOCK_REALTIME;

# define CLOCK_GETTIMEOFDAY 0xdedbeefUL
  
  if (init == 0) {
    init = 1;

    if (0)
      ;
#if HAVE_CLOCK_GETCPUCLOCKID
    else if (clock_getcpuclockid(0, &cpu) != -1 &&
	     clock_gettime(cpu, &tp) != -1)
      ;
#endif
#ifdef _POSIX_CPUTIME
    else if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp) >= 0)
      cpu = CLOCK_PROCESS_CPUTIME_ID;
#endif
    else if (clock_gettime(CLOCK_REALTIME, &tp) >= 0)
      cpu = CLOCK_REALTIME;
    else
      cpu = CLOCK_GETTIMEOFDAY;
  }

  if (cpu == CLOCK_GETTIMEOFDAY) {
    gettimeofday(&tv, NULL);
    tp.tv_sec = tv.tv_sec, tp.tv_nsec = tv.tv_usec * 1000;
  }
  else if (clock_gettime(cpu, &tp) < 0)
    perror("clock_gettime");

  /* return value is in nanoseconds */
  return 
    (uint64_t)((unsigned long)tp.tv_nsec) + 
    (uint64_t)((unsigned long)tp.tv_sec) * 1000000000ULL;
}

/** Return CPU counter value in microseconds.
 *
 * The function su_counter() returns the CPU counter value in microseconds
 * for timing purposes.
 *
 * Parameters:
 * 
 * @return
 *   The current CPU counter value in microseconds.
 */
uint64_t su_counter(void)
{
  return su_nanocounter() / 1000U;
}

#elif HAVE_GETTIMEOFDAY
uint64_t su_counter(void)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  /* return value is in microseconds */
  return 
    (uint64_t)((unsigned long)tv.tv_usec) + 
    (uint64_t)((unsigned long)tv.tv_sec) * 1000000ULL;
}

uint64_t su_nanocounter(void)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  /* return value is in nanoseconds */
  return 
    (uint64_t)((unsigned long)tv.tv_usec) * 1000ULL + 
    (uint64_t)((unsigned long)tv.tv_sec) * 1000000000ULL;
}

#else

uint64_t su_counter(void)
{
  return (uint64_t)clock();
}

uint64_t su_nanocounter(void)
{
  return (uint64_t)clock() * 1000;
}

#endif /* WIN32 */