diff options
author | Patrick Ohly <patrick.ohly@intel.com> | 2013-07-11 11:30:32 +0200 |
---|---|---|
committer | Patrick Ohly <patrick.ohly@intel.com> | 2013-07-11 11:30:32 +0200 |
commit | 2bb4e8ceb66a72542065d8fa822547aabbdb10b1 (patch) | |
tree | 398c8b594c823066fa187b9015a3ed8e64643293 | |
parent | 630d756460970fbca31f9266d0235fd9d16012e7 (diff) |
Linux: speed up getSystemNowAs(TCTX_SYSTEM)
getSystemNowAs(TCTX_SYSTEM) is called often as part of logging. The
showed up a one of the hotspots during syncing. The TzOffsetSeconds()
showed up as one of the hotspots.
Using localtime_r() and the glibc tm_gmtoff extensions improved
runtime of a sync with a fairly low amount of logging from 25s to 22s.
-rwxr-xr-x | src/platform_adapters/linux/platform_time.cpp | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/platform_adapters/linux/platform_time.cpp b/src/platform_adapters/linux/platform_time.cpp index 3c87c62..d008a91 100755 --- a/src/platform_adapters/linux/platform_time.cpp +++ b/src/platform_adapters/linux/platform_time.cpp @@ -10,6 +10,12 @@ * 2004-11-15 : luz : extracted from lineartime.h */ +// For tm_gmtoff on Linux. +#ifndef _BSD_SOURCE +# define _BSD_SOURCE 1 +#endif +#include <time.h> + #include "prefix_file.h" #include "lineartime.h" @@ -45,20 +51,37 @@ lineartime_t getSystemNowAs(timecontext_t aTimeContext,GZones *aGZones, bool noO #ifdef NOW_WITH_MILLISECONDS // high precision time, UTC based struct timeval tv; - struct timezone tz; // gettimeofday return seconds and milliseconds since start of the UNIX epoch - gettimeofday(&tv,&tz); + gettimeofday(&tv,NULL); lineartime_t systime = (tv.tv_sec*secondToLinearTimeFactor+UnixToLineartimeOffset) + (tv.tv_usec*secondToLinearTimeFactor/1000000); #else // standard precision time (unix time), base is UTC + time_t t = time(NULL); lineartime_t systime = - time(NULL)*secondToLinearTimeFactor+UnixToLineartimeOffset; + t*secondToLinearTimeFactor+UnixToLineartimeOffset; #endif // - return as-is if requested time zone is UTC if (noOffset || TCTX_IS_UTC(aTimeContext)) return systime; // return as-is +#ifndef ANDROID + // - shortcut: conversion to system time is faster when done using + // glibc. tm_gmtoff is a glibc extension, so don't try to use it on + // Android. + if (TCTX_IS_SYSTEM(aTimeContext)) { + struct tm tm; + if (localtime_r( +#ifdef NOW_WITH_MILLISECONDS + &tv.tv_sec +#else + &t +#endif + , &tm)) { + return systime + (tm.tm_gmtoff * secondToLinearTimeFactor); + } + } +#endif // - convert to requested zone sInt32 aOffsSeconds; if (!TzOffsetSeconds(systime,TCTX_UTC,aTimeContext,aOffsSeconds,aGZones)) |