8000 Feature/911 sem wait time measures by kzangeli · Pull Request #915 · telefonicaid/fiware-orion · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature/911 sem wait time measures #915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- Add: New CLI parameter for the mutex policy: -mutexPolicy (Issue #910)

- Add: Measuring the accumulated time waiting for the DB semaphores.
The measures are added to the REST request /statistics, but only if
the new CLI parameter -mutexTimeStat is set (Issue #911)
5 changes: 4 additions & 1 deletion src/app/contextBroker/contextBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ char rush[256];
long dbTimeout;
long httpTimeout;
char mutexPolicy[16];
bool mutexTimeStat;



Expand Down Expand Up @@ -247,6 +248,7 @@ char mutexPolicy[16];
#define HTTP_TMO_DESC "timeout in milliseconds for forwards and notifications"
#define MAX_L 900000
#define MUTEX_POLICY_DESC "mutex policy (none/read/write/all)"
#define MUTEX_TIMESTAT_DESC "measure total semaphore waiting time"



Expand Down Expand Up @@ -284,6 +286,7 @@ PaArgument paArgs[] =

{ "-httpTimeout", &httpTimeout, "HTTP_TIMEOUT", PaLong, PaOpt, -1, -1, MAX_L, HTTP_TMO_DESC },
{ "-mutexPolicy", mutexPolicy, "MUTEX_POLICY", PaString, PaOpt, _i "all", PaNL, PaNL, MUTEX_POLICY_DESC },
{ "-mutexTimeStat", &mutexTimeStat, "MUTEX_TIME_STAT", PaBool, PaOpt, false, false, true, MUTEX_TIMESTAT_DESC },

PA_END_OF_ARGS
};
Expand Down Expand Up @@ -1435,7 +1438,7 @@ int main(int argC, char* argV[])

pidFile();
SemRequestType policy = policyGet(mutexPolicy);
orionInit(orionExit, ORION_VERSION, policy);
orionInit(orionExit, ORION_VERSION, policy, mutexTimeStat);
mongoInit(dbHost, rplSet, dbName, user, pwd, dbTimeout);
contextBrokerInit(ngsi9Only, dbName, mtenant);
curl_global_init(CURL_GLOBAL_NOTHING);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/common/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ int startTime = -1;
int statisticsTime = -1;
OrionExitFunction orionExitFunction = NULL;
static struct timeval logStartTime;
bool semTimeStatistics = false;



Expand Down Expand Up @@ -90,7 +91,7 @@ void transactionIdSet(void)
*
* orionInit -
*/
void orionInit(OrionExitFunction exitFunction, const char* version, SemRequestType reqPolicy)
void orionInit(OrionExitFunction exitFunction, const char* version, SemRequestType reqPolicy, bool semTimeStat)
{
// Give the rest library the correct version string of this executable
versionSet(version);
Expand All @@ -99,7 +100,7 @@ void orionInit(OrionExitFunction exitFunction, const char* version, SemRequestTy
orionExitFunction = exitFunction;

// Initialize the semaphore used by mongoBackend
semInit(reqPolicy);
semInit(reqPolicy, semTimeStat);

// Set timer object (singleton)
setTimer(new Timer());
Expand Down
3 changes: 2 additions & 1 deletion src/lib/common/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ extern bool harakiri;
extern int startTime;
extern int statisticsTime;
extern OrionExitFunction orionExitFunction;
extern bool semTimeStatistics;



/* ****************************************************************************
*
* orionInit -
*/
extern void orionInit(OrionExitFunction exitFunction, const char* version, SemRequestType reqPolicy);
extern void orionInit(OrionExitFunction exitFunction, const char* version, SemRequestType reqPolicy, bool semTimeStat);



Expand Down
199 changes: 198 additions & 1 deletion src/lib/common/sem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <semaphore.h>
#include <errno.h>
#include <time.h>

#include "logMsg/logMsg.h"
#include "logMsg/traceLevels.h"
Expand All @@ -43,6 +44,16 @@ static SemRequestType reqPolicy;



/* ****************************************************************************
*
* Time measuring variables -
*/
static struct timespec accReqSemTime = { 0, 0 };
static struct timespec accMongoSemTime = { 0, 0 };
static struct timespec accTransSemTime = { 0, 0 };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also do the same for the transaction semaphore (I forget about it when I wrote #911)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7cccbfb



/* ****************************************************************************
*
* semInit -
Expand All @@ -55,7 +66,7 @@ static SemRequestType reqPolicy;
* -1 on failure
*
*/
int semInit(SemRequestType _reqPolicy, int shared, int takenInitially)
int semInit(SemRequestType _reqPolicy, bool semTimeStat, int shared, int takenInitially)
{
if (sem_init(&reqSem, shared, takenInitially) == -1)
{
Expand All @@ -76,11 +87,50 @@ int semInit(SemRequestType _reqPolicy, int shared, int takenInitially)
}

reqPolicy = _reqPolicy;

// Measure accumulated semaphore waiting time?
semTimeStatistics = semTimeStat;
return 0;
}



/* ****************************************************************************
*
* clock_difftime -
*/
static void clock_difftime(struct timespec* endTime, struct timespec* startTime, struct timespec* diffTime)
{
diffTime->tv_nsec = endTime->tv_nsec - startTime->tv_nsec;
diffTime->tv_sec = endTime->tv_sec - startTime->tv_sec;

if (diffTime->tv_nsec < 0)
{
diffTime->tv_sec -= 1;
diffTime->tv_nsec += 1000000000;
}
}



/* ****************************************************************************
*
* clock_addtime -
*/
static void clock_addtime(struct timespec* accTime, struct timespec* diffTime)
{
accTime->tv_nsec += diffTime->tv_nsec;
accTime->tv_sec += diffTime->tv_sec;

if (accTime->tv_nsec >= 1000000000)
{
accTime->tv_sec += 1;
accTime->tv_nsec -= 1000000000;
}
}



/* ****************************************************************************
*
* reqSemTake -
Expand Down Expand Up @@ -108,7 +158,26 @@ int reqSemTake(const char* who, const char* what, SemRequestType reqType, bool*
}

LM_T(LmtReqSem, ("%s taking the 'req' semaphore for '%s'", who, what));

struct timespec startTime;
struct timespec endTime;
struct timespec diffTime;

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &startTime);
}

r = sem_wait(&reqSem);

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &endTime);

clock_difftime(&endTime, &startTime, &diffTime);
clock_addtime(&accReqSemTime, &diffTime);
}

LM_T(LmtReqSem, ("%s has the 'req' semaphore", who));

*taken = true;
Expand All @@ -117,6 +186,96 @@ int reqSemTake(const char* who, const char* what, SemRequestType reqType, bool*



/* ****************************************************************************
*
* semTimeReqGet - get accumulated req semaphore waiting time
*/
void semTimeReqGet(char* buf, int bufLen)
{
if (semTimeStatistics)
{
snprintf(buf, bufLen, "%lu.%09d", accReqSemTime.tv_sec, (int) accReqSemTime.tv_nsec);
}
else
{
snprintf(buf, bufLen, "Disabled");
}
}



/* ****************************************************************************
*
* semTimeMongoGet - get accumulated mongo semaphore waiting time
*/
void semTimeMongoGet(char* buf, int bufLen)
{
if (semTimeStatistics)
{
snprintf(buf, bufLen, "%lu.%09d", accMongoSemTime.tv_sec, (int) accMongoSemTime.tv_nsec);
}
else
{
snprintf(buf, bufLen, "Disabled");
}
}



/* ****************************************************************************
*
* semTimeTransGet - get accumulated trans semaphore waiting time
*/
void semTimeTransGet(char* buf, int bufLen)
{
if (semTimeStatistics)
{
snprintf(buf, bufLen, "%lu.%09d", accTransSemTime.tv_sec, (int) accTransSemTime.tv_nsec);
}
else
{
snprintf(buf, bufLen, "Disabled");
}
}



/* ****************************************************************************
*
* semTimeReqReset -
*/
void semTimeReqReset(void)
{
accReqSemTime.tv_sec = 0;
accReqSemTime.tv_nsec = 0;
}



/* ****************************************************************************
*
* semTimeMongoReset -
*/
void semTimeMongoReset(void)
{
accMongoSemTime.tv_sec = 0;
accMongoSemTime.tv_nsec = 0;
}



/* ****************************************************************************
*
* semTimeTransReset -
*/
void semTimeTransReset(void)
{
accTransSemTime.tv_sec = 0;
accTransSemTime.tv_nsec = 0;
}



/* ****************************************************************************
*
* mongoSemTake -
Expand All @@ -126,7 +285,26 @@ int mongoSemTake(const char* who, const char* what)
int r;

LM_T(LmtMongoSem, ("%s taking the 'mongo' semaphore for '%s'", who, what));

struct timespec startTime;
struct timespec endTime;
struct timespec diffTime;

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &startTime);
}

r = sem_wait(&mongoSem);

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &endTime);

clock_difftime(&endTime, &startTime, &diffTime);
clock_addtime(&accMongoSemTime, &diffTime);
}

LM_T(LmtMongoSem, ("%s has the 'mongo' semaphore", who));

return r;
Expand All @@ -143,7 +321,26 @@ int transSemTake(const char* who, const char* what)
int r;

LM_T(LmtTransSem, ("%s taking the 'trans' semaphore for '%s'", who, what));

struct timespec startTime;
struct timespec endTime;
struct timespec diffTime;

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &startTime);
}

r = sem_wait(&transSem);

if (semTimeStatistics)
{
clock_gettime(CLOCK_REALTIME, &endTime);

clock_difftime(&endTime, &startTime, &diffTime);
clock_addtime(&accTransSemTime, &diffTime);
}

LM_T(LmtTransSem, ("%s has the 'trans' semaphore", who));

return r;
Expand Down
28 changes: 27 additions & 1 deletion src/lib/common/sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ typedef enum SemRequestType
*
* semInit -
*/
extern int semInit(SemRequestType _reqPolicy = SemReadWriteOp, int shared = 0, int takenInitially = 1);
extern int semInit
(
SemRequestType _reqPolicy = SemReadWriteOp,
bool semTimeStat = false,
int shared = 0,
int takenInitially = 1
);



Expand All @@ -69,4 +75,24 @@ extern int reqSemGive(const char* who, const char* what = NULL, bool taken = tru
extern int mongoSemGive(const char* who, const char* what = NULL);
extern int transSemGive(const char* who, const char* what = NULL);



/* ****************************************************************************
*
* semTimeXxxGet - get accumulated semaphore waiting time
*/
extern void semTimeReqGet(char* buf, int bufLen);
extern void semTimeMongoGet(char* buf, int bufLen);
extern void semTimeTransGet(char* buf, int bufLen);



/* ****************************************************************************
*
* semTimeXxxReset -
*/
extern void semTimeReqReset(void);
extern void semTimeMongoReset(void);
extern void semTimeTransReset(void);

#endif // SRC_LIB_COMMON_SEM_H_
Loading
0