Skip to content

Commit

Permalink
[flex_counter]: Add delay correction to poll (sonic-net#286)
Browse files Browse the repository at this point in the history
* [flex_counter]: Add delay correction to poll

Measure the delay that it takes to read counters from hardware and
adjust sleep time by it.

Signed-off-by: marian-pritsak <marianp@mellanox.com>

* Remove debug print
  • Loading branch information
marian-pritsak authored and lguohan committed Jan 29, 2018
1 parent ac42493 commit d22467e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions syncd/syncd_flex_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,16 @@ void FlexCounter::collectCounters(
}

void FlexCounter::runPlugins(
_In_ swss::DBConnector& db)
_In_ swss::DBConnector& db,
_In_ uint32_t pollInterval)
{
SWSS_LOG_ENTER();

const std::vector<std::string> argv =
{
std::to_string(COUNTERS_DB),
COUNTERS_TABLE,
std::to_string(m_pollInterval * 1000)
std::to_string(pollInterval * 1000)
};

std::vector<std::string> portList;
Expand Down Expand Up @@ -513,17 +514,27 @@ void FlexCounter::flexCounterThread(void)

swss::DBConnector db(COUNTERS_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0);
swss::Table countersTable(&db, COUNTERS_TABLE);
uint32_t correction = 0;

while (m_runFlexCounterThread)
{
{
auto start = std::chrono::steady_clock::now();
std::lock_guard<std::mutex> lock(g_mutex);
collectCounters(countersTable);
runPlugins(db);
auto finish = std::chrono::steady_clock::now();

uint32_t delay = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count());
uint32_t newCorrection = delay % m_pollInterval;

// Run plugins with corrected interval
// First we subtract correction from previous sleep and then add delay from current counters read
runPlugins(db, m_pollInterval - correction + delay);
correction = newCorrection;
}

std::unique_lock<std::mutex> lk(m_mtxSleep);
m_cvSleep.wait_for(lk, std::chrono::milliseconds(m_pollInterval));
m_cvSleep.wait_for(lk, std::chrono::milliseconds(m_pollInterval - correction));
}
}

Expand Down
2 changes: 1 addition & 1 deletion syncd/syncd_flex_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class FlexCounter
static void removeInstance(uint32_t pollInterval);

void collectCounters(_In_ swss::Table &countersTable);
void runPlugins(_In_ swss::DBConnector& db);
void runPlugins(_In_ swss::DBConnector& db, _In_ uint32_t pollInterval);
void flexCounterThread(void);
void startFlexCounterThread(void);
void endFlexCounterThread(void);
Expand Down

0 comments on commit d22467e

Please sign in to comment.