Skip to content

Commit d22467e

Browse files
marian-pritsaklguohan
authored andcommitted
[flex_counter]: Add delay correction to poll (sonic-net#286)
* [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 <[email protected]> * Remove debug print
1 parent ac42493 commit d22467e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

syncd/syncd_flex_counter.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,16 @@ void FlexCounter::collectCounters(
471471
}
472472

473473
void FlexCounter::runPlugins(
474-
_In_ swss::DBConnector& db)
474+
_In_ swss::DBConnector& db,
475+
_In_ uint32_t pollInterval)
475476
{
476477
SWSS_LOG_ENTER();
477478

478479
const std::vector<std::string> argv =
479480
{
480481
std::to_string(COUNTERS_DB),
481482
COUNTERS_TABLE,
482-
std::to_string(m_pollInterval * 1000)
483+
std::to_string(pollInterval * 1000)
483484
};
484485

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

514515
swss::DBConnector db(COUNTERS_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0);
515516
swss::Table countersTable(&db, COUNTERS_TABLE);
517+
uint32_t correction = 0;
516518

517519
while (m_runFlexCounterThread)
518520
{
519521
{
522+
auto start = std::chrono::steady_clock::now();
520523
std::lock_guard<std::mutex> lock(g_mutex);
521524
collectCounters(countersTable);
522-
runPlugins(db);
525+
auto finish = std::chrono::steady_clock::now();
526+
527+
uint32_t delay = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count());
528+
uint32_t newCorrection = delay % m_pollInterval;
529+
530+
// Run plugins with corrected interval
531+
// First we subtract correction from previous sleep and then add delay from current counters read
532+
runPlugins(db, m_pollInterval - correction + delay);
533+
correction = newCorrection;
523534
}
524535

525536
std::unique_lock<std::mutex> lk(m_mtxSleep);
526-
m_cvSleep.wait_for(lk, std::chrono::milliseconds(m_pollInterval));
537+
m_cvSleep.wait_for(lk, std::chrono::milliseconds(m_pollInterval - correction));
527538
}
528539
}
529540

syncd/syncd_flex_counter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class FlexCounter
8787
static void removeInstance(uint32_t pollInterval);
8888

8989
void collectCounters(_In_ swss::Table &countersTable);
90-
void runPlugins(_In_ swss::DBConnector& db);
90+
void runPlugins(_In_ swss::DBConnector& db, _In_ uint32_t pollInterval);
9191
void flexCounterThread(void);
9292
void startFlexCounterThread(void);
9393
void endFlexCounterThread(void);

0 commit comments

Comments
 (0)