1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Refactor usec to msec in common/time.c.

The implementation provides usec resolution but this is not needed in practice and it makes the interface more complicated due to the extra zeros.
This commit is contained in:
David Steele 2018-03-21 09:18:48 -04:00
parent 31830bdc55
commit b234f43c9d
10 changed files with 66 additions and 53 deletions

View File

@ -13,6 +13,14 @@
<release-list>
<release date="XXXX-XX-XX" version="2.02dev" title="UNDER DEVELOPMENT">
<release-core-list>
<release-development-list>
<release-item>
<p>Refactor <id>usec</id> to <id>msec</id> in <code>common/time.c</code>. The implementation provides <id>usec</id> resolution but this is not needed in practice and it makes the interface more complicated due to the extra zeros.</p>
</release-item>
</release-development-list>
</release-core-list>
<release-test-list>
<release-feature-list>
<release-item>

View File

@ -39,7 +39,7 @@ push @EXPORT, qw(backrestBin backrestBinSet);
# Defines the current version of the BackRest executable. The version number is used to track features but does not affect what
# repositories or manifests can be read - that's the job of the format number.
#-----------------------------------------------------------------------------------------------------------------------------------
use constant BACKREST_VERSION => '2.01';
use constant BACKREST_VERSION => '2.02dev';
push @EXPORT, qw(BACKREST_VERSION);
# Format Format Number

View File

@ -6,7 +6,7 @@ package pgBackRest::LibCAuto;
# Library version (.999 indicates development version)
sub libcAutoVersion
{
return '2.01';
return '2.02.999';
}
# Configuration option value constants

View File

@ -179,12 +179,12 @@ logInternal(LogLevel logLevel, const char *fileName, const char *functionName, i
// Add time
if (logTimestamp)
{
TimeUSec logTimeUSec = timeUSec();
time_t logTime = (time_t)(logTimeUSec / USEC_PER_SEC);
TimeMSec logTimeMSec = timeMSec();
time_t logTimeSec = (time_t)(logTimeMSec / MSEC_PER_SEC);
bufferPos += strftime(logBuffer + bufferPos, LOG_BUFFER_SIZE - bufferPos, "%Y-%m-%d %H:%M:%S", localtime(&logTime));
bufferPos += strftime(logBuffer + bufferPos, LOG_BUFFER_SIZE - bufferPos, "%Y-%m-%d %H:%M:%S", localtime(&logTimeSec));
bufferPos += (size_t)snprintf(
logBuffer + bufferPos, LOG_BUFFER_SIZE - bufferPos, ".%03d ", (int)(logTimeUSec / 1000 % 1000));
logBuffer + bufferPos, LOG_BUFFER_SIZE - bufferPos, ".%03d ", (int)(logTimeMSec % 1000));
}
// Add process and aligned log level

View File

@ -1,30 +1,35 @@
/***********************************************************************************************************************************
Time Management
***********************************************************************************************************************************/
#include <sys/time.h>
#include "sys/time.h"
#include "common/time.h"
#include "common/type.h"
/***********************************************************************************************************************************
Epoch time in microseconds
Constants describing number of sub-units in an interval
***********************************************************************************************************************************/
TimeUSec
timeUSec()
#define MSEC_PER_USEC ((TimeMSec)1000)
/***********************************************************************************************************************************
Epoch time in milliseconds
***********************************************************************************************************************************/
TimeMSec
timeMSec()
{
struct timeval currentTime;
gettimeofday(&currentTime, NULL);
return ((TimeUSec)currentTime.tv_sec * USEC_PER_SEC) + (TimeUSec)currentTime.tv_usec;
return ((TimeMSec)currentTime.tv_sec * MSEC_PER_SEC) + (TimeMSec)currentTime.tv_usec / MSEC_PER_USEC;
}
/***********************************************************************************************************************************
Sleep for specified microseconds
Sleep for specified milliseconds
***********************************************************************************************************************************/
void
sleepUSec(TimeUSec sleepUSec)
sleepMSec(TimeMSec sleepMSec)
{
struct timeval delay;
delay.tv_sec = (__time_t)(sleepUSec / USEC_PER_SEC);
delay.tv_usec = (__time_t)(sleepUSec % USEC_PER_SEC);
delay.tv_sec = (__time_t)(sleepMSec / MSEC_PER_SEC);
delay.tv_usec = (__time_t)(sleepMSec % MSEC_PER_SEC * 1000);
select(0, NULL, NULL, NULL, &delay);
}

View File

@ -9,17 +9,17 @@ Time Management
/***********************************************************************************************************************************
Time types
***********************************************************************************************************************************/
typedef uint64 TimeUSec;
typedef uint64 TimeMSec;
/***********************************************************************************************************************************
Constants describing number if sub-units in a second
Constants describing number of sub-units in an interval
***********************************************************************************************************************************/
#define USEC_PER_SEC ((TimeUSec)1000000)
#define MSEC_PER_SEC ((TimeMSec)1000)
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
void sleepUSec(TimeUSec);
TimeUSec timeUSec();
void sleepMSec(TimeMSec sleepMSec);
TimeMSec timeMSec();
#endif

View File

@ -11,10 +11,10 @@ Contains information about the wait handler
struct Wait
{
MemContext *memContext; // Context that contains the wait handler
TimeUSec waitTime; // Total time to wait (in usec)
TimeUSec sleepTime; // Next sleep time (in usec)
TimeUSec sleepPrevTime; // Previous time slept (in usec)
TimeUSec beginTime; // Time the wait began (in epoch usec)
TimeMSec waitTime; // Total time to wait (in usec)
TimeMSec sleepTime; // Next sleep time (in usec)
TimeMSec sleepPrevTime; // Previous time slept (in usec)
TimeMSec beginTime; // Time the wait began (in epoch usec)
};
/***********************************************************************************************************************************
@ -37,17 +37,17 @@ waitNew(double waitTime)
this->memContext = MEM_CONTEXT_NEW();
// Store time
this->waitTime = (TimeUSec)(waitTime * USEC_PER_SEC);
this->waitTime = (TimeMSec)(waitTime * MSEC_PER_SEC);
// Calculate first sleep time -- start with 1/10th of a second for anything >= 1 second
if (this->waitTime >= USEC_PER_SEC)
this->sleepTime = USEC_PER_SEC / 10;
if (this->waitTime >= MSEC_PER_SEC)
this->sleepTime = MSEC_PER_SEC / 10;
// Unless the wait time is really small -- in that case divide wait time by 10
else
this->sleepTime = this->waitTime / 10;
// Get beginning time
this->beginTime = timeUSec();
this->beginTime = timeMSec();
}
MEM_CONTEXT_NEW_END();
@ -66,16 +66,16 @@ waitMore(Wait *this)
if (this->sleepTime > 0)
{
// Sleep required amount
sleepUSec(this->sleepTime);
sleepMSec(this->sleepTime);
// Get the end time
TimeUSec elapsedTime = timeUSec() - this->beginTime;
TimeMSec elapsedTime = timeMSec() - this->beginTime;
// Is there more time to go?
if (elapsedTime < this->waitTime)
{
// Calculate sleep time as a sum of current and last (a Fibonacci-type sequence)
TimeUSec sleepNextTime = this->sleepTime + this->sleepPrevTime;
TimeMSec sleepNextTime = this->sleepTime + this->sleepPrevTime;
// Make sure sleep time does not go beyond end time (this won't be negative because of the if condition above)
if (sleepNextTime > this->waitTime - elapsedTime)

View File

@ -17,6 +17,6 @@ Standard binary name
/***********************************************************************************************************************************
Version of the software. Currently this value is maintained in Version.pm and updated by test.pl.
***********************************************************************************************************************************/
#define PGBACKREST_VERSION "2.01"
#define PGBACKREST_VERSION "2.02dev"
#endif

View File

@ -9,23 +9,23 @@ void
testRun()
{
// *****************************************************************************************************************************
if (testBegin("timeUSec()"))
if (testBegin("timeMSec()"))
{
// Make sure the time returned is between 2017 and 2100
TEST_RESULT_BOOL(timeUSec() > (TimeUSec)1483228800000000, true, "lower range check");
TEST_RESULT_BOOL(timeUSec() < (TimeUSec)4102444800000000, true, "upper range check");
TEST_RESULT_BOOL(timeMSec() > (TimeMSec)1483228800000, true, "lower range check");
TEST_RESULT_BOOL(timeMSec() < (TimeMSec)4102444800000, true, "upper range check");
}
// *****************************************************************************************************************************
if (testBegin("sleepUSec()"))
if (testBegin("sleepMSec()"))
{
// Sleep and measure time slept
TimeUSec begin = timeUSec();
sleepUSec(1400000);
TimeUSec end = timeUSec();
TimeMSec begin = timeMSec();
sleepMSec(1400);
TimeMSec end = timeMSec();
// Check bounds for time slept (within a range of .1 seconds)
TEST_RESULT_BOOL(end - begin > (TimeUSec)1400000, true, "lower range check");
TEST_RESULT_BOOL(end - begin < (TimeUSec)1500000, true, "upper range check");
TEST_RESULT_BOOL(end - begin > (TimeMSec)1400, true, "lower range check");
TEST_RESULT_BOOL(end - begin < (TimeMSec)1500, true, "upper range check");
}
}

View File

@ -17,38 +17,38 @@ testRun()
TEST_ERROR(waitNew(9999999), AssertError, "waitTime must be >= 0.1 and <= 999999.0");
// -------------------------------------------------------------------------------------------------------------------------
TimeUSec begin = timeUSec();
TimeMSec begin = timeMSec();
TEST_ASSIGN(wait, waitNew(0.2), "new wait = 0.2 sec");
TEST_RESULT_DOUBLE(wait->waitTime, 200000, " check wait time");
TEST_RESULT_DOUBLE(wait->sleepTime, 20000, " check sleep time");
TEST_RESULT_DOUBLE(wait->waitTime, 200, " check wait time");
TEST_RESULT_DOUBLE(wait->sleepTime, 20, " check sleep time");
TEST_RESULT_DOUBLE(wait->sleepPrevTime, 0, " check sleep prev time");
TEST_RESULT_BOOL(wait->beginTime > (unsigned long)1483228800000000, true, " check begin time");
TEST_RESULT_BOOL(wait->beginTime > (TimeMSec)1483228800000, true, " check begin time");
while (waitMore(wait));
TimeUSec end = timeUSec();
TimeMSec end = timeMSec();
// Check bounds for time slept (within a range of .1 seconds)
TEST_RESULT_BOOL(end - begin >= wait->waitTime, true, " lower range check");
TEST_RESULT_BOOL(end - begin < wait->waitTime + 100000, true, " upper range check");
TEST_RESULT_BOOL(end - begin < wait->waitTime + 100, true, " upper range check");
TEST_RESULT_VOID(waitFree(wait), " free wait");
// -------------------------------------------------------------------------------------------------------------------------
begin = timeUSec();
begin = timeMSec();
TEST_ASSIGN(wait, waitNew(1.1), "new wait = 1.1 sec");
TEST_RESULT_DOUBLE(wait->waitTime, 1100000, " check wait time");
TEST_RESULT_DOUBLE(wait->sleepTime, 100000, " check sleep time");
TEST_RESULT_DOUBLE(wait->waitTime, 1100, " check wait time");
TEST_RESULT_DOUBLE(wait->sleepTime, 100, " check sleep time");
TEST_RESULT_DOUBLE(wait->sleepPrevTime, 0, " check sleep prev time");
TEST_RESULT_BOOL(wait->beginTime > (unsigned long)1483228800000000, true, " check begin time");
TEST_RESULT_BOOL(wait->beginTime > (TimeMSec)1483228800000, true, " check begin time");
while (waitMore(wait));
end = timeUSec();
end = timeMSec();
// Check bounds for time slept (within a range of .1 seconds)
TEST_RESULT_BOOL(end - begin >= wait->waitTime, true, " lower range check");
TEST_RESULT_BOOL(end - begin < wait->waitTime + 1200000, true, " upper range check");
TEST_RESULT_BOOL(end - begin < wait->waitTime + 1200, true, " upper range check");
TEST_RESULT_VOID(waitFree(wait), " free wait");
}