Skip to content

Commit

Permalink
[saiasiccmp] Add saiasiccmp tool to compare 2 asic views (sonic-net#791)
Browse files Browse the repository at this point in the history
Will be used to compare 2 independent ASIC view dumps for unittest.
File format must by provided by command:
```
$ ./redisdl.py -d 1 -y
```
from https://github.com/p/redis-dump-load repo 

Signed-off-by: kcudnik <kcudnik@gmail.com>
  • Loading branch information
kcudnik authored Mar 10, 2021
1 parent 623d13b commit d7b431b
Show file tree
Hide file tree
Showing 21 changed files with 59,554 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SUBDIRS = meta lib vslib python

if SYNCD
SUBDIRS += syncd saiplayer saidump saidiscovery saisdkdump tests
SUBDIRS += syncd saiplayer saidump saidiscovery saisdkdump saiasiccmp tests
endif

ACLOCAL_AMFLAGS = -I m4
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ AC_OUTPUT(Makefile
saidump/Makefile
saisdkdump/Makefile
saidiscovery/Makefile
saiasiccmp/Makefile
tests/Makefile
python/setup.py)
1 change: 1 addition & 0 deletions debian/syncd.install
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ usr/bin/saidump
usr/bin/saiplayer
usr/bin/saisdkdump
usr/bin/saidiscovery
usr/bin/saiasiccmp
usr/bin/syncd*
syncd/scripts/* usr/bin
52 changes: 52 additions & 0 deletions saiasiccmp/AsicCmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "AsicCmp.h"
#include "ViewCmp.h"

#include "swss/logger.h"

#include <iostream>

using namespace saiasiccmp;

AsicCmp::AsicCmp(
_In_ std::shared_ptr<CommandLineOptions> options):
m_commandLineOptions(options)
{
SWSS_LOG_ENTER();

// empty
}

bool AsicCmp::compare()
{
SWSS_LOG_ENTER();

auto& args = m_commandLineOptions->m_args;

if (args.size() != 2)
{
SWSS_LOG_ERROR("ERROR: expected 2 input files, but given: %zu", args.size());
return false;
}

try
{
auto a = std::make_shared<View>(args[0]);
auto b = std::make_shared<View>(args[1]);

SWSS_LOG_NOTICE("max objects: %lu %lu", a->m_maxObjectIndex, b->m_maxObjectIndex);

b->translateViewVids(a->m_maxObjectIndex);

ViewCmp cmp(a, b);

return cmp.compareViews(m_commandLineOptions->m_dumpDiffToStdErr);
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;

SWSS_LOG_ERROR("Exception: %s", e.what());

return false;
}
}
26 changes: 26 additions & 0 deletions saiasiccmp/AsicCmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "swss/sal.h"

#include "CommandLineOptions.h"

#include <memory>

namespace saiasiccmp
{
class AsicCmp
{
public:

AsicCmp(
_In_ std::shared_ptr<CommandLineOptions> options);

public:

bool compare();

private:

std::shared_ptr<CommandLineOptions> m_commandLineOptions;
};
}
36 changes: 36 additions & 0 deletions saiasiccmp/CommandLineOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "CommandLineOptions.h"

#include "meta/sai_serialize.h"

#include "swss/logger.h"

#include <sstream>

using namespace saiasiccmp;

CommandLineOptions::CommandLineOptions()
{
SWSS_LOG_ENTER();

// default values for command line options

m_enableLogLevelInfo = false;
m_dumpDiffToStdErr = false;
}

std::string CommandLineOptions::getCommandLineString() const
{
SWSS_LOG_ENTER();

std::stringstream ss;

ss << " EnableLogLevelInfo=" << (m_enableLogLevelInfo ? "YES" : "NO");
ss << " DumpDiffToStdErr=" << (m_dumpDiffToStdErr ? "YES" : "NO");

for (auto &arg: m_args)
{
ss << " " << arg ;
}

return ss.str();
}
29 changes: 29 additions & 0 deletions saiasiccmp/CommandLineOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "swss/sal.h"

#include <string>
#include <vector>

namespace saiasiccmp
{
class CommandLineOptions
{
public:

CommandLineOptions();

virtual ~CommandLineOptions() = default;

public:

virtual std::string getCommandLineString() const;

public:

bool m_enableLogLevelInfo;
bool m_dumpDiffToStdErr;

std::vector<std::string> m_args;
};
}
90 changes: 90 additions & 0 deletions saiasiccmp/CommandLineOptionsParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "CommandLineOptionsParser.h"

#include "meta/sai_serialize.h"

#include "swss/logger.h"

#include <getopt.h>

#include <iostream>

using namespace saiasiccmp;

std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(
_In_ int argc,
_In_ char **argv)
{
SWSS_LOG_ENTER();

auto options = std::make_shared<CommandLineOptions>();

const char* const optstring = "idh";

while (true)
{
static struct option long_options[] =
{
{ "enableLogLevelInfo", no_argument, 0, 'i' },
{ "dumpDiffToStdErr", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};

int option_index = 0;

int c = getopt_long(argc, argv, optstring, long_options, &option_index);

if (c == -1)
{
break;
}

switch (c)
{
case 'i':
options->m_enableLogLevelInfo = true;
break;

case 'd':
options->m_dumpDiffToStdErr = true;
break;

case 'h':
printUsage();
exit(EXIT_SUCCESS);

case '?':
SWSS_LOG_WARN("unknown option %c", optopt);
printUsage();
exit(EXIT_FAILURE);

default:
SWSS_LOG_ERROR("getopt_long failure");
exit(EXIT_FAILURE);
}
}

for (int index = optind; index < argc; index++)
{
options->m_args.push_back(argv[index]);
}

return options;
}

void CommandLineOptionsParser::printUsage()
{
SWSS_LOG_ENTER();

std::cout << "Usage: saiasiccmp [-i] [-d] [-h] file1 file2" << std::endl << std::endl;

std::cout << " file1 and file2 must be in json fromat produced by redis-dump-load" << std::endl;
std::cout << " for example: redisdl.py -d 1 -y" << std::endl << std::endl;

std::cout << " -i --enableLogLevelInfo" << std::endl;
std::cout << " Enable LogLevel INFO" << std::endl;
std::cout << " -d --dumpDiffToStdErr" << std::endl;
std::cout << " Dump asic diff to stderr" << std::endl;
std::cout << " -h --help" << std::endl;
std::cout << " Print out this message" << std::endl;
}
25 changes: 25 additions & 0 deletions saiasiccmp/CommandLineOptionsParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "CommandLineOptions.h"

#include <memory>

namespace saiasiccmp
{
class CommandLineOptionsParser
{
private:

CommandLineOptionsParser() = delete;

~CommandLineOptionsParser() = delete;

public:

static std::shared_ptr<CommandLineOptions> parseCommandLine(
_In_ int argc,
_In_ char **argv);

static void printUsage();
};
}
37 changes: 37 additions & 0 deletions saiasiccmp/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/lib/inc \
-I$(top_srcdir)/SAI/inc \
-I$(top_srcdir)/SAI/meta \
-I$(top_srcdir)/SAI/experimental \
-I$(top_srcdir)/meta \
-I$(top_srcdir)/syncd

bin_PROGRAMS = saiasiccmp

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
else
DBGFLAGS = -g
endif

noinst_LIBRARIES = libAsicCmp.a
libAsicCmp_a_SOURCES = \
AsicCmp.cpp \
View.cpp \
ViewCmp.cpp \
SaiSwitchAsic.cpp \
CommandLineOptions.cpp \
CommandLineOptionsParser.cpp

libAsicCmp_a_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS)

saiasiccmp_SOURCES = main.cpp
saiasiccmp_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS)
saiasiccmp_LDADD = libAsicCmp.a \
-lsaimetadata -lsaimeta -ldl -lhiredis -lswsscommon -lpthread -lzmq \
$(top_srcdir)/syncd/libSyncd.a \
-L$(top_srcdir)/syncd/.libs \
$(top_srcdir)/lib/src/libSaiRedis.a \
-L$(top_srcdir)/meta/.libs

TESTS = test.sh
Loading

0 comments on commit d7b431b

Please sign in to comment.