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

vslib port counter support #1274

Closed
wants to merge 1 commit into from
Closed

vslib port counter support #1274

wants to merge 1 commit into from

Conversation

vishnushetty
Copy link
Contributor

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.

_In_ std::string& if_name,
_Out_ uint64_t &counter)
{
struct {
Copy link
Collaborator

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

Copy link
Contributor Author

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 [] = {
Copy link
Collaborator

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

Copy link
Contributor Author

@vishnushetty vishnushetty Aug 15, 2023

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++)
Copy link
Collaborator

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

Copy link
Contributor Author

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");
Copy link
Collaborator

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;
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. thanks

Copy link
Collaborator

@kcudnik kcudnik left a 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");
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@vishnushetty vishnushetty Aug 16, 2023

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

Copy link
Collaborator

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)
Copy link
Collaborator

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

Copy link
Contributor Author

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());
Copy link
Collaborator

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 ?

Copy link
Contributor Author

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());
Copy link
Collaborator

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

Copy link
Contributor Author

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);
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added rc check

@vishnushetty
Copy link
Contributor Author

/easycla

Comment on lines +257 to +258
_In_ sai_object_id_t port_id,
_In_ const sai_stat_id_t counter_id,
Copy link
Collaborator

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

Copy link
Contributor Author

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use camel case ifName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Comment on lines +89 to +98
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);

Copy link
Collaborator

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

Copy link
Contributor Author

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);
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@vishnushetty
Copy link
Contributor Author

I will close this PR, raise on wrong fork. Use below PR
#1275
for further comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants