From 6cbf65642a9d094feddf820b206c1a3af70d5fd9 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Sun, 11 Feb 2007 13:42:29 +0000 Subject: [PATCH] Move duplicate timestamp detection into lutil_gettime() --- include/lutil.h | 1 + libraries/liblutil/csn.c | 17 +---------------- libraries/liblutil/utils.c | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/lutil.h b/include/lutil.h index c5b9c528cf..49ea795193 100644 --- a/include/lutil.h +++ b/include/lutil.h @@ -158,6 +158,7 @@ typedef struct lutil_tm { int tm_mon; /* month 0-11 */ int tm_year; /* year - 1900 */ int tm_usec; /* microseconds */ + int tm_usub; /* submicro */ } lutil_tm; typedef struct lutil_timet { diff --git a/libraries/liblutil/csn.c b/libraries/liblutil/csn.c index bab1dd9a8b..4c3213f741 100644 --- a/libraries/liblutil/csn.c +++ b/libraries/liblutil/csn.c @@ -52,29 +52,14 @@ size_t lutil_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod) { struct lutil_tm tm; - static unsigned int csnop; - static int prev_sec, prev_usec; - - unsigned int op; int n; lutil_gettime( &tm ); - if ( tm.tm_sec > prev_sec || ( tm.tm_sec == prev_sec && - tm.tm_usec > prev_usec )) { - prev_sec = tm.tm_sec; - prev_usec = tm.tm_usec; - csnop = 0; - } else { - tm.tm_sec = prev_sec; - tm.tm_usec = prev_usec; - } - op = csnop++; - n = snprintf( buf, len, "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, - tm.tm_min, tm.tm_sec, tm.tm_usec, op, replica, mod ); + tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod ); if( n < 0 ) return 0; return ( (size_t) n < len ) ? n : 0; diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index 3f34a319fe..3891cfacf1 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -282,6 +282,8 @@ void lutil_gettime( struct lutil_tm *tm ) { static LARGE_INTEGER cFreq; + static LARGE_INTEGER prevCount; + static int subs; static int offset; LARGE_INTEGER count; SYSTEMTIME st; @@ -306,6 +308,18 @@ lutil_gettime( struct lutil_tm *tm ) offset = ( usec - st.wMilliseconds ) * 1000; } + /* It shouldn't ever go backwards, but multiple CPUs might + * be able to hit in the same tick. + */ + if ( count.QuadPart <= prevCount.QuadPart ) { + subs++; + } else { + subs = 0; + prevCount = count; + } + + tm->tm_usub = subs; + /* convert to microseconds */ count.QuadPart *= 1000000; count.QuadPart /= cFreq.QuadPart; @@ -329,6 +343,9 @@ void lutil_gettime( struct lutil_tm *ltm ) { struct timeval tv; + static struct timeval prevTv; + static int subs; + #ifdef HAVE_GMTIME_R struct tm tm_buf; #endif @@ -338,6 +355,16 @@ lutil_gettime( struct lutil_tm *ltm ) gettimeofday( &tv, NULL ); t = tv.tv_sec; + if ( tv.tv_sec < prevTv.tv_sec + || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec == prevTv.tv_usec )) { + subs++; + } else { + subs = 0; + prevTv = tv; + } + + ltm->tm_usub = subs; + #ifdef HAVE_GMTIME_R tm = gmtime_r( &t, &tm_buf ); #else -- 2.39.5