Skip to content

Commit

Permalink
limit the max log queue size
Browse files Browse the repository at this point in the history
  • Loading branch information
zsummer committed Aug 19, 2016
1 parent 5b4b56d commit 16c209c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
18 changes: 12 additions & 6 deletions log4z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,9 @@ class LogerManager : public ThreadHelper, public ILog4zManager
virtual bool updateConfig();
virtual bool isLoggerEnable(LoggerId id);
virtual unsigned long long getStatusTotalWriteCount(){return _ullStatusTotalWriteFileCount;}
virtual unsigned long long getStatusTotalWriteBytes(){return _ullStatusTotalWriteFileBytes;}
virtual unsigned long long getStatusWaitingCount(){return _ullStatusTotalPushLog - _ullStatusTotalPopLog;}
virtual unsigned long long getStatusTotalWriteBytes() { return _ullStatusTotalWriteFileBytes; }
virtual unsigned long long getStatusTotalPushQueue() { return _ullStatusTotalPushLog; }
virtual unsigned long long getStatusTotalPopQueue() { return _ullStatusTotalPopLog; }
virtual unsigned int getStatusActiveLoggers();
protected:
virtual LogData * makeLogData(LoggerId id, int level);
Expand Down Expand Up @@ -422,7 +423,7 @@ class LogerManager : public ThreadHelper, public ILog4zManager

//! log queue
LockHelper _logLock;
std::list<LogData *> _logs;
std::queue<LogData *> _logs;
std::vector<LogData*> _freeLogDatas;

//show color lock
Expand All @@ -435,6 +436,7 @@ class LogerManager : public ThreadHelper, public ILog4zManager
//Log queue statistics
unsigned long long _ullStatusTotalPushLog;
unsigned long long _ullStatusTotalPopLog;




Expand Down Expand Up @@ -1425,6 +1427,10 @@ bool LogerManager::prePushLog(LoggerId id, int level)
{
return false;
}
if (_logs.size() > LOG4Z_LOG_QUEUE_LIMIT_SIZE)
{
return false;
}
return true;
}
bool LogerManager::pushLog(LogData * pLog, const char * file, int line)
Expand Down Expand Up @@ -1495,7 +1501,7 @@ bool LogerManager::pushLog(LogData * pLog, const char * file, int line)
}

AutoLock l(_logLock);
_logs.push_back(pLog);
_logs.push(pLog);
_ullStatusTotalPushLog ++;
return true;
}
Expand Down Expand Up @@ -1527,7 +1533,7 @@ bool LogerManager::hotChange(LoggerId id, LogDataType ldt, int num, const std::s
memcpy(pLog->_content, text.c_str(), text.length());
pLog->_contentLen = (int)text.length();
AutoLock l(_logLock);
_logs.push_back(pLog);
_logs.push(pLog);
return true;
}

Expand Down Expand Up @@ -1751,7 +1757,7 @@ bool LogerManager::popLog(LogData *& log)
return false;
}
log = _logs.front();
_logs.pop_front();
_logs.pop();
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion log4z.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ const int LOG4Z_LOGGER_MAX = 20;
const int LOG4Z_LOG_BUF_SIZE = 1024 * 8;
//! the max stl container depth.
const int LOG4Z_LOG_CONTAINER_DEPTH = 5;
//! the log queue length limit size.
const int LOG4Z_LOG_QUEUE_LIMIT_SIZE = 10000;

//! all logger synchronous output or not
const bool LOG4Z_ALL_SYNCHRONOUS_OUTPUT = false;
Expand Down Expand Up @@ -350,7 +352,8 @@ class ILog4zManager
virtual bool isLoggerEnable(LoggerId id) = 0;
virtual unsigned long long getStatusTotalWriteCount() = 0;
virtual unsigned long long getStatusTotalWriteBytes() = 0;
virtual unsigned long long getStatusWaitingCount() = 0;
virtual unsigned long long getStatusTotalPushQueue() = 0;
virtual unsigned long long getStatusTotalPopQueue() = 0;
virtual unsigned int getStatusActiveLoggers() = 0;

virtual LogData * makeLogData(LoggerId id, int level) = 0;
Expand Down
22 changes: 9 additions & 13 deletions test/stress_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ LoggerId logid_moniter;
(float)-1.234567, (double)-2.34566, "fffff", \
32423324, 234, 1000, 100.12345678, true

//! limit waiting count
const unsigned int LIMIT_WAITING_COUNT = 50000;

//! process quit.
bool g_quit;

#define STREES_SWITCH 1 // 1 stream, 2 windows format, 3 linux format
#define STREES_SWITCH 3 // 1 stream, 2 windows format, 3 linux format

void multiThreadFunc()
{
Expand Down Expand Up @@ -94,10 +93,7 @@ void multiThreadFunc()



while (ILog4zManager::getRef().getStatusWaitingCount() > LIMIT_WAITING_COUNT)
{
sleepMillisecond(10);
}

}
LOGA("thread quit ... ");
}
Expand Down Expand Up @@ -134,11 +130,11 @@ int main(int argc, char *argv[])
ILog4zManager::getRef().start();

//! ---------
createThread(&multiThreadFunc);
createThread(&multiThreadFunc);
createThread(&multiThreadFunc);
createThread(&multiThreadFunc);
createThread(&multiThreadFunc);
for (int i=0; i<5; i++)
{
createThread(&multiThreadFunc);
}


//! ---------
unsigned long long lastCount = 0;
Expand All @@ -152,7 +148,7 @@ int main(int argc, char *argv[])
lastData += speedData;
LOGI("Stress Status: Write Speed: " << speedCount/5
<< " n/s, Speed: " << speedData/1024/5
<< " KB/s, Waiting: " << ILog4zManager::getRef().getStatusWaitingCount());
<< " KB/s, Waiting: " << ILog4zManager::getRef().getStatusTotalPushQueue() - ILog4zManager::getRef().getStatusTotalPopQueue());
sleepMillisecond(5000);
}

Expand Down

0 comments on commit 16c209c

Please sign in to comment.