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

[utilities] Create utility classes for interacting with flex counters #1093

Merged
merged 8 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion orchagent/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I flex_counter

CFLAGS_SAI = -I /usr/include/sai

Expand Down Expand Up @@ -55,6 +55,8 @@ orchagent_SOURCES = \
policerorch.cpp \
chassisorch.cpp

orchagent_SOURCES += flex_counter/flex_counter_manager.cpp flex_counter/dynamic_flex_counter_manager.cpp

orchagent_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
orchagent_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_SAI)
orchagent_LDADD = -lnl-3 -lnl-route-3 -lpthread -lsairedis -lswsscommon -lsaimeta -lsaimetadata
Expand Down
88 changes: 88 additions & 0 deletions orchagent/flex_counter/dynamic_flex_counter_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "dynamic_flex_counter_manager.h"

#include "schema.h"
#include "rediscommand.h"
#include "logger.h"
#include "sai_serialize.h"

using std::string;
using std::unordered_map;
using std::unordered_set;
using swss::FieldValueTuple;

DynamicFlexCounterManager::DynamicFlexCounterManager(
const string &group_name,
const StatsMode stats_mode,
const int polling_interval) :
FlexCounterManager(group_name, stats_mode, polling_interval)
{
SWSS_LOG_ENTER();
}

DynamicFlexCounterManager::~DynamicFlexCounterManager()
{
SWSS_LOG_ENTER();
}

// addFlexCounterStat will add a new stat for the given object to poll.
void DynamicFlexCounterManager::addFlexCounterStat(
const sai_object_id_t object_id,
const CounterType counter_type,
const string &counter_stat)
{
SWSS_LOG_ENTER();

auto counter_stats = object_stats.find(object_id);
if (counter_stats == object_stats.end())
{
unordered_set<string> new_stats = { counter_stat };
object_stats.emplace(std::make_pair(object_id, new_stats));
qiluo-msft marked this conversation as resolved.
Show resolved Hide resolved
counter_stats = object_stats.find(object_id);
qiluo-msft marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
counter_stats->second.emplace(counter_stat);
}

FlexCounterManager::setCounterIdList(object_id, counter_type, counter_stats->second);

SWSS_LOG_DEBUG("Added flex stat '%s' to object '%s'", counter_stat.c_str(), sai_serialize_object_id(object_id).c_str());
}

// removeFlexCounterStat will remove a stat from the set of stats the given
// object are polling.
void DynamicFlexCounterManager::removeFlexCounterStat(
const sai_object_id_t object_id,
const CounterType counter_type,
const string &counter_stat)
{
SWSS_LOG_ENTER();

auto counter_stats = object_stats.find(object_id);
if (counter_stats == object_stats.end())
{
SWSS_LOG_DEBUG("Could not find flex stat '%s' on object '%s'",
daall marked this conversation as resolved.
Show resolved Hide resolved
counter_stat.c_str(), sai_serialize_object_id(object_id).c_str());
return;
}

counter_stats->second.erase(counter_stat);

// If we don't have any stats left for this object, delete the flex
// counter entirely.
if (counter_stats->second.empty())
{
object_stats.erase(counter_stats);
FlexCounterManager::clearCounterIdList(object_id);

SWSS_LOG_DEBUG("Flex stat is empty, removing flex counter from object '%s'",
sai_serialize_object_id(object_id).c_str());
return;
}

FlexCounterManager::setCounterIdList(object_id, counter_type, counter_stats->second);

SWSS_LOG_DEBUG("Removing flex stat '%s' from object '%s'",
counter_stat.c_str(),
sai_serialize_object_id(object_id).c_str());
}
33 changes: 33 additions & 0 deletions orchagent/flex_counter/dynamic_flex_counter_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef SWSS_UTIL_DYNAMIC_FLEX_COUNTER_MANAGER_H
#define SWSS_UTIL_DYNAMIC_FLEX_COUNTER_MANAGER_H

#include "flex_counter_manager.h"

// DynamicFlexCounterManager allows users to manage a group of flex counters
// where the objects have highly variable sets of stats to track.
class DynamicFlexCounterManager : public FlexCounterManager
qiluo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
public:
DynamicFlexCounterManager(
const std::string &group_name,
const StatsMode stats_mode,
const int polling_interval);

DynamicFlexCounterManager(const DynamicFlexCounterManager&) = delete;
DynamicFlexCounterManager& operator=(const DynamicFlexCounterManager&) = delete;
~DynamicFlexCounterManager();

void addFlexCounterStat(
const sai_object_id_t object_id,
const CounterType counter_type,
const std::string &counter_stat);
void removeFlexCounterStat(
const sai_object_id_t object_id,
const CounterType counter_type,
const std::string &counter_stat);

private:
std::unordered_map<sai_object_id_t, std::unordered_set<std::string>> object_stats;
};

#endif // SWSS_UTIL_DYNAMIC_FLEX_COUNTER_MANAGER_H
203 changes: 203 additions & 0 deletions orchagent/flex_counter/flex_counter_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#include "flex_counter_manager.h"

#include <vector>
#include "schema.h"
#include "rediscommand.h"
#include "logger.h"
#include "sai_serialize.h"

using std::shared_ptr;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using swss::DBConnector;
using swss::FieldValueTuple;
using swss::ProducerTable;

static const string FLEX_COUNTER_ENABLE("enable");
static const string FLEX_COUNTER_DISABLE("disable");

const unordered_map<StatsMode, string> FlexCounterManager::stats_mode_lookup =
{
{ StatsMode::READ, STATS_MODE_READ },
};

const unordered_map<CounterType, string> FlexCounterManager::counter_id_field_lookup =
{
{ CounterType::PORT_DEBUG, PORT_DEBUG_COUNTER_ID_LIST },
{ CounterType::SWITCH_DEBUG, SWITCH_DEBUG_COUNTER_ID_LIST },
};

// This constructor will create a group that is disabled by default.
FlexCounterManager::FlexCounterManager(
const string &group_name,
const StatsMode stats_mode,
const uint polling_interval) :
group_name(group_name),
enabled(false),
flex_counter_db(new DBConnector(FLEX_COUNTER_DB, DBConnector::DEFAULT_UNIXSOCKET, 0)),
flex_counter_group_table(new ProducerTable(flex_counter_db.get(), FLEX_COUNTER_GROUP_TABLE)),
flex_counter_table(new ProducerTable(flex_counter_db.get(), FLEX_COUNTER_TABLE))
{
SWSS_LOG_ENTER();

vector<FieldValueTuple> field_values =
{
FieldValueTuple(STATS_MODE_FIELD, stats_mode_lookup.at(stats_mode)),
FieldValueTuple(POLL_INTERVAL_FIELD, std::to_string(polling_interval)),
FieldValueTuple(FLEX_COUNTER_STATUS_FIELD, FLEX_COUNTER_DISABLE)
};
flex_counter_group_table->set(group_name, field_values);

SWSS_LOG_DEBUG("Initialized flex counter group '%s'.", group_name.c_str());
}

FlexCounterManager::~FlexCounterManager()
{
SWSS_LOG_ENTER();

flex_counter_group_table->del(group_name);
daall marked this conversation as resolved.
Show resolved Hide resolved
for (const auto &counter: installed_counters)
{
flex_counter_table->del(getFlexCounterTableKey(group_name, counter));
}

SWSS_LOG_DEBUG("Deleted flex counter group '%s'.", group_name.c_str());
}

void FlexCounterManager::updateGroupPollingInterval(
const uint polling_interval)
{
SWSS_LOG_ENTER();

vector<FieldValueTuple> field_values =
{
FieldValueTuple(POLL_INTERVAL_FIELD, std::to_string(polling_interval))
};
flex_counter_group_table->set(group_name, field_values);

SWSS_LOG_DEBUG("Set polling interval for flex counter group '%s' to %d ms.",
group_name.c_str(), polling_interval);
}

// enableFlexCounterGroup will do nothing if the flex counter group is already
// enabled.
void FlexCounterManager::enableFlexCounterGroup()
{
SWSS_LOG_ENTER();

if (enabled)
{
return;
}

vector<FieldValueTuple> field_values =
{
FieldValueTuple(FLEX_COUNTER_STATUS_FIELD, FLEX_COUNTER_ENABLE)
};
flex_counter_group_table->set(group_name, field_values);
enabled = true;

SWSS_LOG_DEBUG("Enabling flex counters for group '%s'.",
group_name.c_str());
}

// disableFlexCounterGroup will do nothing if the flex counter group has been
// disabled.
void FlexCounterManager::disableFlexCounterGroup()
{
SWSS_LOG_ENTER();

if (!enabled)
{
return;
}

vector<FieldValueTuple> field_values =
{
FieldValueTuple(FLEX_COUNTER_STATUS_FIELD, FLEX_COUNTER_DISABLE)
};
flex_counter_group_table->set(group_name, field_values);
enabled = false;

SWSS_LOG_DEBUG("Disabling flex counters for group '%s'.",
group_name.c_str());
}

// setCounterIdList configures a flex counter to poll the set of provided stats
// that are associated with the given object.
void FlexCounterManager::setCounterIdList(
const sai_object_id_t object_id,
const CounterType counter_type,
const unordered_set<string> &counter_stats)
{
SWSS_LOG_ENTER();

auto counter_type_it = counter_id_field_lookup.find(counter_type);
if (counter_type_it == counter_id_field_lookup.end())
{
SWSS_LOG_ERROR("Could not update flex counter id list for group '%s': counter type not found.",
group_name.c_str());
return;
}

std::vector<swss::FieldValueTuple> field_values =
{
FieldValueTuple(counter_type_it->second, serializeCounterStats(counter_stats))
};
flex_counter_table->set(
getFlexCounterTableKey(group_name, object_id), field_values);
installed_counters.insert(object_id);

SWSS_LOG_DEBUG("Updated flex counter id list for object '%lu' in group '%s'.",
object_id,
group_name.c_str());
}

// clearCounterIdList clears all stats that are currently being polled from
// the given object.
void FlexCounterManager::clearCounterIdList(const sai_object_id_t object_id)
{
SWSS_LOG_ENTER();

auto counter_it = installed_counters.find(object_id);
if (counter_it == installed_counters.end())
{
SWSS_LOG_DEBUG("No counters found on object '%lu' in group '%s'.",
object_id,
group_name.c_str());
return;
}

flex_counter_table->del(getFlexCounterTableKey(group_name, object_id));
installed_counters.erase(counter_it);

SWSS_LOG_DEBUG("Cleared flex counter id list for object '%lu' in group '%s'.",
object_id,
group_name.c_str());
}

string FlexCounterManager::getFlexCounterTableKey(
const string &group_name,
const sai_object_id_t object_id) const
{
SWSS_LOG_ENTER();

return group_name + ':' + sai_serialize_object_id(object_id);
daall marked this conversation as resolved.
Show resolved Hide resolved
}

// serializeCounterStats turns a set of stats into a format suitable for FLEX_COUNTER_DB.
string FlexCounterManager::serializeCounterStats(
const unordered_set<string> &counter_stats) const
{
SWSS_LOG_ENTER();

string stats_string;
for (const auto &stat : counter_stats)
{
stats_string.append(",");
daall marked this conversation as resolved.
Show resolved Hide resolved
stats_string.append(stat);
}
return stats_string.substr(1);
}
Loading