Skip to content

Commit

Permalink
Add unit test for trap flow flex counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Junchao-Mellanox committed Nov 9, 2021
1 parent 6e5cf82 commit 5b77bf7
Show file tree
Hide file tree
Showing 8 changed files with 489 additions and 10 deletions.
34 changes: 27 additions & 7 deletions syncd/FlexCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

using namespace syncd;
using namespace std;

#define MUTEX std::unique_lock<std::mutex> _lock(m_mtx);
#define MUTEX_UNLOCK _lock.unlock();
Expand All @@ -18,6 +19,7 @@ FlexCounter::FlexCounter(
_In_ const std::string& instanceId,
_In_ std::shared_ptr<sairedis::SaiInterface> vendorSai,
_In_ const std::string& dbCounters):
m_readyToPoll(false),
m_pollInterval(0),
m_instanceId(instanceId),
m_vendorSai(vendorSai),
Expand Down Expand Up @@ -633,7 +635,14 @@ void FlexCounter::setTunnelCounterList(
for (auto &counter : counterIds)
{
if (isTunnelCounterSupported(counter))
SWSS_LOG_NOTICE("Tunnel %s does not have supported counters", sai_serialize_object_id(tunnelRid).c_str());
{
supportedIds.push_back(counter);
}
}

if (supportedIds.empty())
{
SWSS_LOG_NOTICE("Tunnel %s does not have supported counters", sai_serialize_object_id(tunnelRid).c_str());
return;
}

Expand Down Expand Up @@ -1190,7 +1199,7 @@ void FlexCounter::addCounterPlugin(
}

// notify thread to start polling
m_pollCond.notify_all();
notifyPoll();
}

bool FlexCounter::isEmpty()
Expand Down Expand Up @@ -2138,10 +2147,7 @@ void FlexCounter::flexCounterThreadRunFunction()
MUTEX_UNLOCK; // explicit unlock

// nothing to collect, wait until notified

std::unique_lock<std::mutex> lk(m_mtxSleep);

m_pollCond.wait(lk); // wait on mutex
waitPoll();
}
}

Expand Down Expand Up @@ -2169,7 +2175,7 @@ void FlexCounter::endFlexCounterThread(void)

m_runFlexCounterThread = false;

m_pollCond.notify_all();
notifyPoll();

m_cvSleep.notify_all();

Expand Down Expand Up @@ -3136,5 +3142,19 @@ void FlexCounter::addCounter(
}

// notify thread to start polling
notifyPoll();
}

void FlexCounter::waitPoll()
{
std::unique_lock<std::mutex> lk(m_mtxSleep);
m_pollCond.wait(lk, [&](){return m_readyToPoll;});
m_readyToPoll = false;
}

void FlexCounter::notifyPoll()
{
std::unique_lock<std::mutex> lk(m_mtxSleep);
m_readyToPoll = true;
m_pollCond.notify_all();
}
7 changes: 7 additions & 0 deletions syncd/FlexCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ namespace syncd
void removeCollectCountersHandler(
_In_ const std::string& key);

private:
void waitPoll();

void notifyPoll();

private: // plugins

std::set<std::string> m_queuePlugins;
Expand Down Expand Up @@ -540,6 +545,8 @@ namespace syncd

std::condition_variable m_pollCond;

bool m_readyToPoll;

uint32_t m_pollInterval;

std::string m_instanceId;
Expand Down
5 changes: 5 additions & 0 deletions unittest/meta/TestSaiSerialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ TEST(SaiSerialize, sai_serialize_tunnel_stat)
EXPECT_EQ(sai_serialize_tunnel_stat(SAI_TUNNEL_STAT_IN_OCTETS), "SAI_TUNNEL_STAT_IN_OCTETS");
}

TEST(SaiSerialize, sai_serialize_counter_stat)
{
EXPECT_EQ(sai_serialize_counter_stat(SAI_COUNTER_STAT_PACKETS), "SAI_COUNTER_STAT_PACKETS");
}

TEST(SaiSerialize, sai_serialize_queue_attr)
{
EXPECT_EQ(sai_serialize_queue_attr(SAI_QUEUE_ATTR_TYPE), "SAI_QUEUE_ATTR_TYPE");
Expand Down
11 changes: 8 additions & 3 deletions unittest/syncd/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/syncd -I$(top_srcdir)/lib -I$(top_srcdir)/vslib
AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/syncd -I$(top_srcdir)/lib -I$(top_srcdir)/vslib -I$(top_srcdir)/meta

bin_PROGRAMS = tests

LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main

tests_SOURCES = main.cpp \
TestCommandLineOptions.cpp
../../meta/DummySaiInterface.cpp \
MockHelper.cpp \
TestCommandLineOptions.cpp \
TestFlexCounter.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/syncd/libSyncd.a -lswsscommon -lpthread -L$(top_srcdir)/lib/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/syncd/libSyncd.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/lib/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)

TESTS = tests
36 changes: 36 additions & 0 deletions unittest/syncd/MockHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "VidManager.h"
#include "swss/dbconnector.h"
#include "swss/table.h"

namespace test_syncd {
sai_object_type_t mock_objectTypeQuery_result;
void mockVidManagerObjectTypeQuery(sai_object_type_t mock_result)
{
mock_objectTypeQuery_result = mock_result;
}
}

namespace syncd {
sai_object_type_t VidManager::objectTypeQuery(_In_ sai_object_id_t objectId)
{
return test_syncd::mock_objectTypeQuery_result;
}
}

/*
namespace swss {
DBConnector::DBConnector(const std::string& dbName, unsigned int timeout, bool isTcpConn)
{
}
DBConnector *DBConnector::newConnector(unsigned int timeout) const
{
return nullptr;
}
Table::Table(RedisPipeline *pipeline, const std::string &tableName, bool buffered)
{
}
}*/
5 changes: 5 additions & 0 deletions unittest/syncd/MockHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace test_syncd {
void mockVidManagerObjectTypeQuery(sai_object_type_t);
}
Loading

0 comments on commit 5b77bf7

Please sign in to comment.