diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index 72bbbf2b..5e4ba7be 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -113,12 +113,24 @@ void Controller::SharedMemStats(void * config){ std::set inactiveStreams; Controller::initState(); bool shiftWrites = true; + bool firstRun = true; while(((Util::Config*)config)->is_active){ { tthread::lock_guard guard(Controller::configMutex); tthread::lock_guard guard2(statsMutex); //parse current users statServer.parseEach(parseStatistics); + if (firstRun){ + firstRun = false; + servUpOtherBytes = 0; + servDownOtherBytes = 0; + servUpBytes = 0; + servDownBytes = 0; + for (std::map::iterator it = streamStats.begin(); it != streamStats.end(); ++it){ + it->second.upBytes = 0; + it->second.downBytes = 0; + } + } //wipe old statistics if (sessions.size()){ std::list mustWipe; @@ -238,9 +250,9 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da if (currUp - prevUp < 0 || currDown-prevDown < 0){ INFO_MSG("Negative data usage! %lldu/%lldd (u%lld->%lld) in %s over %s, #%lu", currUp-prevUp, currDown-prevDown, prevUp, currUp, data.streamName().c_str(), data.connector().c_str(), index); } - if (currDown + currUp > COUNTABLE_BYTES){ + if (currDown + currUp >= COUNTABLE_BYTES){ std::string streamName = data.streamName(); - if (prevUp + prevDown < COUNTABLE_BYTES){ + if (sessionType == SESS_UNSET){ if (data.connector() == "INPUT"){ streamStats[streamName].inputs++; streamStats[streamName].currIns++; @@ -254,6 +266,10 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da streamStats[streamName].currViews++; sessionType = SESS_VIEWER; } + } + //If previous < COUNTABLE_BYTES, we haven't counted any data so far. + //We need to count all the data in that case, otherwise we only count the difference. + if (prevUp + prevDown < COUNTABLE_BYTES){ if (!streamName.size() || streamName[0] == 0){ if (streamStats.count(streamName)){streamStats.erase(streamName);} }else{ @@ -267,15 +283,6 @@ void Controller::statSession::update(unsigned long index, IPC::statExchange & da streamStats[streamName].upBytes += currUp - prevUp; streamStats[streamName].downBytes += currDown - prevDown; } - if (sessionType == SESS_UNSET){ - if (data.connector() == "INPUT"){ - sessionType = SESS_INPUT; - }else if (data.connector() == "OUTPUT"){ - sessionType = SESS_OUTPUT; - }else{ - sessionType = SESS_VIEWER; - } - } } } } @@ -347,6 +354,7 @@ void Controller::statSession::ping(const Controller::sessIndex & index, unsigned wipedUp = 0; wipedDown = 0; oldConns.clear(); + sessionType = SESS_UNSET; } }