Skip to content

Commit

Permalink
Merge pull request #107 from nickbroon/cleaner_logging
Browse files Browse the repository at this point in the history
Cleaner logging
  • Loading branch information
ogasser authored Feb 14, 2018
2 parents fd68332 + c0b4e79 commit be70d52
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 108 deletions.
102 changes: 21 additions & 81 deletions src/common/ConcurrentQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
template<class T>
class ConcurrentQueue
{
private:

inline bool do_pop(T* res)
{
lock.lock();
*res = queue.front();
queue.pop();
poppedCount++;
count--;
lock.unlock();

pushSemaphore.post();

DPRINTF_DEBUG( "(%s) element popped", ownerName.c_str());

return true;
}

public:
/**
* default queue size
Expand Down Expand Up @@ -75,111 +93,33 @@ class ConcurrentQueue

inline bool pop(T* res)
{
DPRINTF_DEBUG( "(%s) trying to pop element (%d elements in queue)",
(ownerName.empty() ? "<owner not set>" : ownerName.c_str()),
maxEntries-pushSemaphore.getCount());
if (!popSemaphore.wait()) {
DPRINTF_INFO("(%s) failed to pop element, program is being shut down?", ownerName.c_str());
return false;
}

lock.lock();
*res = queue.front();
queue.pop();
poppedCount++;
count--;
lock.unlock();

pushSemaphore.post();

DPRINTF_DEBUG( "(%s) element popped", ownerName.c_str());

return true;
return do_pop(res);
};

// try to pop an entry from the queue before timeout occurs
// if successful, res will hold the popped entry and true will be returned
// of the timeout has been reached, res will be set to NULL and false will be returned
inline bool pop(long timeout_ms, T *res)
{
DPRINTF_DEBUG( "(%s) trying to pop element (%d elements in queue)", ownerName.c_str(), count);
// try to get an item from the queue
if(!popSemaphore.wait(timeout_ms)) {
// timeout occured
DPRINTF_DEBUG( "(%s) timeout", ownerName.c_str());
return false;
}

// popSemaphore.wait() succeeded, now pop the frontmost element
lock.lock();
*res = queue.front();
queue.pop();
poppedCount++;
count--;
lock.unlock();

pushSemaphore.post();

DPRINTF_DEBUG( "(%s) element popped", ownerName.c_str());

return true;
return do_pop(res);
}

// like pop above, but with absolute time instead of delta.
// use this instead of the above, makes things easier!
inline bool popAbs(const struct timeval &timeout, T *res)
{
DPRINTF_DEBUG( "(%s) trying to pop element (%d elements in queue)", ownerName.c_str(), count);

if (popSemaphore.waitAbs(timeout)) {
// popSemaphore.wait() succeeded, now pop the frontmost element
lock.lock();
*res = queue.front();
queue.pop();
poppedCount++;
count--;
lock.unlock();

pushSemaphore.post();

DPRINTF_DEBUG( "(%s) element popped", ownerName.c_str());

return true;
} else {
// timeout occured
DPRINTF_DEBUG( "(%s) timeout or program shutdown", ownerName.c_str());
*res = 0;

return false;
}
}

// like pop above, but with absolute time instead of delta.
// use this instead of the above, makes things easier!
inline bool popAbs(const struct timespec& timeout, T *res)
{
DPRINTF_DEBUG( "(%s) trying to pop element (%d elements in queue)", ownerName.c_str(), count);

if (popSemaphore.waitAbs(timeout)) {
// popSemaphore.wait() succeeded, now pop the frontmost element
lock.lock();
*res = queue.front();
queue.pop();
poppedCount++;
count--;
lock.unlock();

pushSemaphore.post();

DPRINTF_DEBUG( "(%s) element popped", ownerName.c_str());

return true;
return do_pop(res);
}
else {
// timeout occured
DPRINTF_DEBUG( "(%s) timeout or program shutdown", ownerName.c_str());
*res = 0;

return false;
}
}
Expand Down
23 changes: 1 addition & 22 deletions src/common/TimeoutSemaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ class TimeoutSemaphore
return false;
break;
default:
// semaphore could not be aquired because of several reasons, but none are fatal
DPRINTF_DEBUG( "timedwait (<0) returned with %d (%s)", errno, strerror(errno));
return false;
}
}
Expand All @@ -120,7 +118,6 @@ class TimeoutSemaphore
retval = sem_timedwait(sem, &timeout);
#endif
if (retval != 0 && errno != ETIMEDOUT) {
DPRINTF_DEBUG( "timedwait (>=0) returned with %d: %s", errno, strerror(errno));
switch (errno) {
case EINVAL:
/*
Expand All @@ -141,8 +138,7 @@ class TimeoutSemaphore
return false;
break;
default:
// semaphore could not be aquired because of several reasons, but none are fatal
DPRINTF_DEBUG( "timedwait (>=0) returned with %d", errno);
break;
}
}
if (errno == ETIMEDOUT) {
Expand All @@ -164,20 +160,6 @@ class TimeoutSemaphore
return true;
}


/**
* see documentation for waitAbs(struct timespec)
* has same functionality, just different parameter
*/
inline bool waitAbs(const struct timeval& timeout)
{
struct timespec ts;

TIMEVAL_TO_TIMESPEC(&timeout, &ts);

return waitAbs(ts);
}

// like wait() but with absolute time instead of delta. makes things easier!
// Use this instead of the above function
inline bool waitAbs(const struct timespec& ts)
Expand Down Expand Up @@ -215,9 +197,6 @@ class TimeoutSemaphore
return false;
break;
default:
// semaphore not acquired for non-fatal reasons
DPRINTF_DEBUG( "timedwait returned with %s",
strerror(errno));
return false;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/ConnectionQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,10 @@ class ConnectionQueue : public Adapter<T>, public Timer
struct timespec nexttimeout;
if (!processTimeouts(nexttimeout)) {
if (!queue.pop(&element)) {
DPRINTF_INFO("queue.pop failed - timeout?");
continue;
}
} else {
if (!queue.popAbs(nexttimeout, &element)) {
DPRINTF_INFO("queue.pop failed - timeout?");
continue;
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/modules/ipfix/aggregator/BaseAggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,12 @@ void BaseAggregator::exporterThread()
}

gettimeofday(&curtime, 0);
DPRINTF_DEBUG("Aggregator: starting Export");
for (size_t i = 0; i < rules->count; i++) {
rules->rule[i]->hashtable->expireFlows();
}
struct timeval endtime;
gettimeofday(&endtime, 0);
timeval_subtract(&difftime, &endtime, &curtime);

DPRINTF_DEBUG("Aggregator: export took %.03f secs", (float)difftime.tv_usec/1000000+difftime.tv_sec);
}

if (getShutdownProperly()) {
Expand Down

0 comments on commit be70d52

Please sign in to comment.