Conversation
| _In_ std::string& if_name, | ||
| _Out_ uint64_t &counter) | ||
| { | ||
| struct { |
There was a problem hiding this comment.
please declare struct in separate file or in *.h file of SwithcState
| struct { | ||
| sai_stat_id_t id; | ||
| std::string name; | ||
| } counter_map [] = { |
There was a problem hiding this comment.
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.
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.
pleae keep code quality, add space after if, empty line before if and after }, delcare uint_32 i inside for, this is not C
| 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.
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.
this should be obtained automatically, if you declare this entries as std::vector
There was a problem hiding this comment.
replaced with std::map no size
There was a problem hiding this comment.
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.
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>
kcudnik
left a comment
There was a problem hiding this comment.
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.
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.
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.
There was a problem hiding this comment.
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.
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.
keep code quality, add space after each if
| { | ||
| 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.
should you return error after this ?
| { | ||
| 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.
use std string to concat this to not deal wit buffers sizes
| /* 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.
you don't check getPortStat error code
There was a problem hiding this comment.
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.
keep also naming in camel case, portId, counterId
| _In_ const sai_stat_id_t counter_id, | ||
| _Out_ uint64_t &counter) | ||
| { | ||
| std::string if_name; |
| 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.
those methods can be protected, no need to make them public
There was a problem hiding this comment.
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.
if you are using reference, use & on the left of the param like in case of if_name, also use camelCase, ifName, counterId etc
|
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.