Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VOQ][saidump] Enhance saidump with new option -r to parser the JSON file and displays/format the right output, fix compiling error #1335

Open
wants to merge 7 commits into
base: 202305
Choose a base branch
from
3 changes: 1 addition & 2 deletions saidump/Makefile.am
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ AM_CXXFLAGS = $(SAIINC) -I$(top_srcdir)/lib

bin_PROGRAMS = saidump

saidump_SOURCES = saidump.cpp
saidump_SOURCES = main.cpp saidump.cpp
saidump_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
saidump_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) $(CODE_COVERAGE_CXXFLAGS)
saidump_LDADD = -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta \
-L$(top_srcdir)/lib/.libs -lsairedis -lzmq $(CODE_COVERAGE_LIBS)

noinst_LIBRARIES = libsaidump.a
libsaidump_a_SOURCES = saidump.cpp
AM_CPPFLAGS = -D_UNITTEST_
21 changes: 21 additions & 0 deletions saidump/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "saidump.h"

using namespace syncd;

int main(int argc, char **argv)
{
SWSS_LOG_ENTER();
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_DEBUG);
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE);
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_INFO);
JunhongMao marked this conversation as resolved.
Show resolved Hide resolved

SaiDump m_saiDump;

if(SAI_STATUS_SUCCESS != m_saiDump.handleCmdLine(argc, argv))
{
return EXIT_FAILURE;
}

m_saiDump.dumpFromRedisDb();
return EXIT_SUCCESS;
}
127 changes: 43 additions & 84 deletions saidump/saidump.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
#include <inttypes.h>
#include <string>
#include <set>
#include <sstream>
#include <iostream>
#include <fstream>
#include <regex>
#include <climits>

extern "C" {
#include <sai.h>
}

#include "swss/table.h"
#include "meta/sai_serialize.h"
#include "sairediscommon.h"
#include "swss/json.hpp"
#include "saidump.h"
#include <getopt.h>
using namespace syncd;

// TODO split to multiple cpp

using namespace swss;
using json = nlohmann::json;
CmdOptions g_cmdOptions;
static std::map<sai_object_id_t, const TableMap*> g_oid_map;

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

Expand All @@ -42,15 +19,14 @@ void printUsage()
std::cout << " Print out this message" << std::endl;
}

CmdOptions handleCmdLine(int argc, char **argv)
sai_status_t SaiDump::handleCmdLine(int argc, char **argv)
{
SWSS_LOG_ENTER();

CmdOptions options;

options.dumpTempView = false;
options.dumpGraph = false;
options.rdbJSonSizeLimit = RDB_JSON_MAX_SIZE;
sai_status_t status = SAI_STATUS_SUCCESS;
dumpTempView = false;
dumpGraph = false;
rdbJSonSizeLimit = RDB_JSON_MAX_SIZE;

const char* const optstring = "gtr:m:h";
uint64_t result = 0;
Expand Down Expand Up @@ -80,17 +56,20 @@ CmdOptions handleCmdLine(int argc, char **argv)
{
case 'g':
SWSS_LOG_NOTICE("Dumping graph");
options.dumpGraph = true;
dumpGraph = true;
status = SAI_STATUS_SUCCESS;
break;

case 't':
SWSS_LOG_NOTICE("Dumping temp view");
options.dumpTempView = true;
dumpTempView = true;
status = SAI_STATUS_SUCCESS;
break;

case 'r':
SWSS_LOG_NOTICE("Dumping from %s", optarg);
options.rdbJsonFile = std::string(optarg);
rdbJsonFile = std::string(optarg);
status = SAI_STATUS_SUCCESS;
break;

case 'm':
Expand All @@ -110,9 +89,9 @@ CmdOptions handleCmdLine(int argc, char **argv)
exit(EXIT_SUCCESS);
}

options.rdbJSonSizeLimit = result * 1024 * 1024;
SWSS_LOG_NOTICE("Configure the RDB JSON MAX size to %llu MB", options.rdbJSonSizeLimit / 1024 / 1024);

rdbJSonSizeLimit = result * 1024 * 1024;
SWSS_LOG_NOTICE("Configure the RDB JSON MAX size to %llu MB", rdbJSonSizeLimit / 1024 / 1024);
status = SAI_STATUS_SUCCESS;
break;

case 'h':
Expand All @@ -129,11 +108,10 @@ CmdOptions handleCmdLine(int argc, char **argv)
exit(EXIT_FAILURE);
}
}

return options;
return status;
}

size_t get_max_attr_len(const TableMap& map)
size_t SaiDump::get_max_attr_len(const TableMap& map)
{
SWSS_LOG_ENTER();

Expand All @@ -147,7 +125,7 @@ size_t get_max_attr_len(const TableMap& map)
return max;
}

std::string pad_string(std::string s, size_t pad)
std::string SaiDump::pad_string(std::string s, size_t pad)
{
SWSS_LOG_ENTER();

Expand All @@ -161,7 +139,7 @@ std::string pad_string(std::string s, size_t pad)
return s;
}

const TableMap* get_table_map(sai_object_id_t object_id)
const TableMap* SaiDump::get_table_map(sai_object_id_t object_id)
{
SWSS_LOG_ENTER();

Expand All @@ -176,7 +154,7 @@ const TableMap* get_table_map(sai_object_id_t object_id)
return it->second;
}

void print_attributes(size_t indent, const TableMap& map)
void SaiDump::print_attributes(size_t indent, const TableMap& map)
{
SWSS_LOG_ENTER();

Expand Down Expand Up @@ -204,7 +182,7 @@ void print_attributes(size_t indent, const TableMap& map)
#define GV_ROOT_COLOR "0.650 0.200 1.000"
#define GV_NODE_COLOR "0.650 0.500 1.000"

void dumpGraph(const TableDump& td)
void SaiDump::dumpGraphFun(const TableDump& td)
{
SWSS_LOG_ENTER();

Expand Down Expand Up @@ -437,7 +415,7 @@ void dumpGraph(const TableDump& td)
/**
* @brief Process the input JSON file to make sure it's a valid JSON file for the JSON library.
*/
sai_status_t preProcessFile(const std::string file_name)
sai_status_t SaiDump::preProcessFile(const std::string file_name)
{
SWSS_LOG_ENTER();

Expand All @@ -451,11 +429,11 @@ sai_status_t preProcessFile(const std::string file_name)

input_file.seekg(0, std::ios::end); // Move to the end of the file
uint64_t file_size = input_file.tellg(); // Get the current position
SWSS_LOG_NOTICE("Get %s's size %" PRIu64 " Bytes, limit: %" PRIu64 " MB.", file_name.c_str(), file_size, g_cmdOptions.rdbJSonSizeLimit / 1024 / 1024);
SWSS_LOG_NOTICE("Get %s's size %" PRIu64 " Bytes, limit: %" PRIu64 " MB.", file_name.c_str(), file_size, rdbJSonSizeLimit / 1024 / 1024);

if (file_size >= g_cmdOptions.rdbJSonSizeLimit)
if (file_size >= rdbJSonSizeLimit)
{
SWSS_LOG_ERROR_AND_STDERR("Get %s's size failure or its size %" PRIu64 " >= %" PRIu64 " MB.", file_name.c_str(), file_size, g_cmdOptions.rdbJSonSizeLimit / 1024 / 1024);
SWSS_LOG_ERROR_AND_STDERR("Get %s's size failure or its size %" PRIu64 " >= %" PRIu64 " MB.", file_name.c_str(), file_size, rdbJSonSizeLimit / 1024 / 1024);
return SAI_STATUS_FAILURE;
}

Expand Down Expand Up @@ -488,15 +466,20 @@ sai_status_t preProcessFile(const std::string file_name)
return SAI_STATUS_SUCCESS;
}

sai_status_t dumpFromRedisRdbJson(const std::string file_name)
sai_status_t SaiDump::dumpFromRedisRdbJson()
{
SWSS_LOG_ENTER();

std::ifstream input_file(file_name);
if (SAI_STATUS_FAILURE == preProcessFile(rdbJsonFile))
{
return SAI_STATUS_FAILURE;
}

std::ifstream input_file(rdbJsonFile);

if (!input_file.is_open())
{
SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", file_name.c_str());
SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", rdbJsonFile.c_str());
return SAI_STATUS_FAILURE;
}

Expand Down Expand Up @@ -571,50 +554,34 @@ sai_status_t dumpFromRedisRdbJson(const std::string file_name)
}
catch (std::exception &ex)
{
SWSS_LOG_ERROR_AND_STDERR("JSON file %s is invalid.", file_name.c_str());
SWSS_LOG_ERROR_AND_STDERR("JSON file %s is invalid.", rdbJsonFile.c_str());
SWSS_LOG_ERROR_AND_STDERR("JSON parsing error: %s.", ex.what());
}

return SAI_STATUS_FAILURE;
}

#ifndef _UNITTEST_
int main(int argc, char **argv)
void SaiDump::dumpFromRedisDb()
{
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_DEBUG);

SWSS_LOG_ENTER();

swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_NOTICE);

swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_INFO);

g_cmdOptions = handleCmdLine(argc, argv);


if (g_cmdOptions.rdbJsonFile.size() > 0)
if (rdbJsonFile.size() > 0)
{
if (SAI_STATUS_FAILURE == preProcessFile(g_cmdOptions.rdbJsonFile))
{
return EXIT_FAILURE;
}

return dumpFromRedisRdbJson(g_cmdOptions.rdbJsonFile);
dumpFromRedisRdbJson();
return;
}

swss::DBConnector db("ASIC_DB", 0);

std::string table = ASIC_STATE_TABLE;

if (g_cmdOptions.dumpTempView)
if (dumpTempView)
{
table = TEMP_PREFIX + table;
}

swss::Table t(&db, table);

TableDump dump;

t.dump(dump);

for (const auto&key: dump)
Expand All @@ -632,33 +599,25 @@ int main(int argc, char **argv)
{
sai_object_id_t object_id;
sai_deserialize_object_id(str_object_id, object_id);

g_oid_map[object_id] = &key.second;
}
}

if (g_cmdOptions.dumpGraph)
if (dumpGraph)
{
dumpGraph(dump);

return EXIT_SUCCESS;
dumpGraphFun(dump);
return;
}

for (const auto&key: dump)
{
auto start = key.first.find_first_of(":");
auto str_object_type = key.first.substr(0, start);
auto str_object_id = key.first.substr(start + 1);

std::cout << str_object_type << " " << str_object_id << " " << std::endl;

size_t indent = 4;

print_attributes(indent, key.second);

std::cout << std::endl;
}

return EXIT_SUCCESS;
}
#endif
57 changes: 45 additions & 12 deletions saidump/saidump.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
#pragma once

extern "C" {
#include <sai.h>
}
#include <inttypes.h>
#include <set>
#include <regex>
#include <climits>
#include <getopt.h>
#include "swss/table.h"
#include "meta/sai_serialize.h"
#include "sairediscommon.h"
#include "swss/json.hpp"

using namespace swss;
using json = nlohmann::json;

// Default value: 100MB
constexpr int64_t RDB_JSON_MAX_SIZE = 1024 * 1024 * 100;
static constexpr int64_t RDB_JSON_MAX_SIZE = 1024 * 1024 * 100;

struct CmdOptions
namespace syncd
{
bool skipAttributes;
bool dumpTempView;
bool dumpGraph;
std::string rdbJsonFile;
uint64_t rdbJSonSizeLimit;
};
class SaiDump
{
public:
SaiDump() = default;
~SaiDump() = default;
sai_status_t handleCmdLine(int argc, char **argv);
void dumpFromRedisDb();
void printUsage();
sai_status_t dumpFromRedisRdbJson();
sai_status_t preProcessFile(const std::string file_name);

public:
std::string rdbJsonFile;
uint64_t rdbJSonSizeLimit;

private:
bool skipAttributes;
bool dumpTempView;
bool dumpGraph;
std::map<sai_object_id_t, const TableMap*> g_oid_map;

void printUsage();
CmdOptions handleCmdLine(int argc, char **argv);
sai_status_t dumpFromRedisRdbJson(const std::string file_name);
sai_status_t preProcessFile(const std::string file_name);
private:
size_t get_max_attr_len(const TableMap& map);
std::string pad_string(std::string s, size_t pad);
const TableMap* get_table_map(sai_object_id_t object_id);
void dumpGraphFun(const TableDump& td);
void print_attributes(size_t indent, const TableMap& map);
};
}
Loading
Loading