-
Notifications
You must be signed in to change notification settings - Fork 273
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
vslib port counter support #1274
Conversation
_In_ std::string& if_name, | ||
_Out_ uint64_t &counter) | ||
{ | ||
struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please declare struct in separate file or in *.h file of SwithcState
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
struct { | ||
sai_stat_id_t id; | ||
std::string name; | ||
} counter_map [] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this map also should be declared in SwithcState.h and ssigned in cpp file as const, no need to delcare it in function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. replacing with std::map.
uint32_t i; | ||
sai_status_t rc = SAI_STATUS_SUCCESS; | ||
|
||
for(i = 0; i < counter_map_size; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pleae keep code quality, add space after if, empty line before if and after }, delcare uint_32 i inside for, this is not C
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
if(counter_map[i].id == counter_id) | ||
{ | ||
sprintf(cmd, "/bin/cat /sys/class/net/%s/statistics/%s", if_name.c_str(), counter_map[i].name.c_str()); | ||
fp = popen(cmd, "r"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declare FILE here not at the top of funcion
{ SAI_PORT_STAT_IF_OUT_ERRORS, "tx_errors" }, | ||
{ SAI_PORT_STAT_IF_OUT_DISCARDS, "tx_dropped" } | ||
}; | ||
uint32_t counter_map_size = 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be obtained automatically, if you declare this entries as std::vector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with std::map no size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know how to make map const? for now i made it just private initialized in constructor. Do you have an example for const map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try:
static const std::map<x,y> m_map;
and it must be initialized in cpp file take a look here: https://stackoverflow.com/questions/2636303/how-to-initialize-a-private-static-const-map-in-c
const std::map<x,y>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please address comments
if(counter_map[i].id == counter_id) | ||
{ | ||
sprintf(cmd, "/bin/cat /sys/class/net/%s/statistics/%s", if_name.c_str(), counter_map[i].name.c_str()); | ||
fp = popen(cmd, "r"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's preffered to use isostream, you don't need to worry about closing file, iostream will close that when will get out of scope, and you cen return errors directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any c++ stream for this purpose? This is not efficient but dont know alternate option for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't work. Will go with popen for now.
std::string filename = "/sys/class/net/" + ifName + "/statistics/" + counterName;
std::ifstream istrm(filename, std::ios::in);
SWSS_LOG_NOTICE("counter id found %d name %s", counterId, counterName.c_str());
if (!istrm.is_open())
{
SWSS_LOG_ERROR("failed to open %s", filename.c_str());
counter = -1;
return SAI_STATUS_FAILURE;
}
else
{
if (istrm.read(reinterpret_cast<char*>(&counter), sizeof counter)) // binary input
{
SWSS_LOG_NOTICE("counter id found %d name %s val %lu",
counterId, counterName.c_str(), counter);
}
}
}
read not returning right values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you tries using ">>" operator ? istrem >> counter;
/* zero out counters */ | ||
counter = 0; | ||
|
||
if(getTapNameFromPortId(port_id, if_name) == false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep code quality, add space after each if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
{ | ||
if(getNetStat(counter_id, if_name, counter) != SAI_STATUS_SUCCESS) | ||
{ | ||
SWSS_LOG_ERROR("Port stat get failed %s", if_name.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you return error after this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
{ | ||
if(counter_map[i].id == counter_id) | ||
{ | ||
sprintf(cmd, "/bin/cat /sys/class/net/%s/statistics/%s", if_name.c_str(), counter_map[i].name.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use std string to concat this to not deal wit buffers sizes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
/* In non unit test mode fetch port counters from host interface */ | ||
if (!enabled && (object_type == SAI_OBJECT_TYPE_PORT)) | ||
{ | ||
getPortStat(object_id, id, counter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't check getPortStat error code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added rc check
/easycla |
_In_ sai_object_id_t port_id, | ||
_In_ const sai_stat_id_t counter_id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep also naming in camel case, portId, counterId
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
_In_ const sai_stat_id_t counter_id, | ||
_Out_ uint64_t &counter) | ||
{ | ||
std::string if_name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use camel case ifName
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
sai_status_t getPortStat( | ||
_In_ sai_object_id_t port_id, | ||
_In_ const sai_stat_id_t counter_id, | ||
_Out_ uint64_t &counters); | ||
|
||
sai_status_t getNetStat( | ||
_In_ sai_stat_id_t counter_id, | ||
_In_ std::string& if_name, | ||
_Out_ uint64_t &counter); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those methods can be protected, no need to make them public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made it private
sai_status_t getNetStat( | ||
_In_ sai_stat_id_t counter_id, | ||
_In_ std::string& if_name, | ||
_Out_ uint64_t &counter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you are using reference, use & on the left of the param like in case of if_name, also use camelCase, ifName, counterId etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
I will close this PR, raise on wrong fork. Use below PR |
In sonic-vs the 'show interface counters' is not supported (port counters). This PR adds support for it. This would be useful in VS debugging and automation. The port counters are fetched from corresponding host interface, not supported counters set to zero as earlier.