Skip to content

Commit

Permalink
[Link Event Damping] Add tracker to track the selectable timers used …
Browse files Browse the repository at this point in the history
…by link event damper.

HLD: sonic-net/SONiC#1071
  • Loading branch information
Ashish Singh committed Oct 17, 2023
1 parent f6e4765 commit 3987c8c
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 0 deletions.
1 change: 1 addition & 0 deletions syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ libSyncd_a_SOURCES = \
SaiObj.cpp \
SaiSwitch.cpp \
SaiSwitchInterface.cpp \
SelectablesTracker.cpp \
ServiceMethodTable.cpp \
SingleReiniter.cpp \
SwitchNotifications.cpp \
Expand Down
42 changes: 42 additions & 0 deletions syncd/SelectableEventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "swss/logger.h"
#include "swss/sal.h"

namespace syncd
{

// This class implements handler for Selectable events.
class SelectableEventHandler {
public:

virtual ~SelectableEventHandler()
{
SWSS_LOG_ENTER();
}

virtual void handleSelectableEvent() = 0;

protected:

SelectableEventHandler()
{
SWSS_LOG_ENTER();
}
};

} // namespace syncd
114 changes: 114 additions & 0 deletions syncd/SelectablesTracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "SelectablesTracker.h"

using namespace syncd;

bool SelectablesTracker::addSelectableToTracker(
swss::Selectable *selectable,
SelectableEventHandler *eventHandler)
{
SWSS_LOG_ENTER();

if (selectable == nullptr)
{
SWSS_LOG_ERROR("Invalid Selectable:Selectable is NULL.");

return false;
}

int fd = selectable->getFd();
if (eventHandler == nullptr)
{
SWSS_LOG_ERROR("Event handler for Selectable with fd: %d is NULL.", fd);

return false;
}

if (m_selectableFdToEventHandlerMap.count(fd) != 0)
{
SWSS_LOG_ERROR("Selectable with fd %d is already in use.", fd);

return false;
}

SWSS_LOG_INFO("Adding the Selectable with fd: %d.", fd);
m_selectableFdToEventHandlerMap[fd] = eventHandler;

return true;
}

bool SelectablesTracker::removeSelectableFromTracker(
swss::Selectable *selectable)
{
SWSS_LOG_ENTER();

if (selectable == nullptr)
{
SWSS_LOG_ERROR("Invalid Selectable:Selectable is NULL.");
return false;
}

int fd = selectable->getFd();

SWSS_LOG_INFO("Removing the Selectable with fd: %d.", fd);
if (m_selectableFdToEventHandlerMap.erase(fd) == 0)
{
SWSS_LOG_ERROR("Selectable with fd %d is not present in the map!", fd);

return false;
}

return true;
}

bool SelectablesTracker::selectableIsTracked(
swss::Selectable *selectable)
{
SWSS_LOG_ENTER();

if ((selectable == nullptr) ||
(m_selectableFdToEventHandlerMap.count(selectable->getFd()) == 0))
{
return false;
}

return true;
}

SelectableEventHandler *SelectablesTracker::getEventHandlerForSelectable(
swss::Selectable *selectable)
{
SWSS_LOG_ENTER();

if (selectable == nullptr)
{
SWSS_LOG_ERROR("Invalid Selectable:Selectable is NULL.");

return nullptr;
}

int fd = selectable->getFd();
auto it = m_selectableFdToEventHandlerMap.find(fd);

if (it == m_selectableFdToEventHandlerMap.end())
{
SWSS_LOG_ERROR("Selectable with fd %d is not present in the map!", fd);

return nullptr;
}

return it->second;
}
72 changes: 72 additions & 0 deletions syncd/SelectablesTracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <unordered_map>

#include "SelectableEventHandler.h"
#include "swss/logger.h"
#include "swss/sal.h"
#include "swss/selectable.h"
#include "swss/selectabletimer.h"

namespace syncd
{
// This class keeps track of Selectable being used by an entity and their
// corresponding EventHandler objects.
class SelectablesTracker
{
public:

SelectablesTracker()
{
SWSS_LOG_ENTER();
}
virtual ~SelectablesTracker()
{
SWSS_LOG_ENTER();
}

// Not copyable or movable.
SelectablesTracker(const SelectablesTracker &) = delete;
SelectablesTracker(SelectablesTracker &&) = delete;
SelectablesTracker &operator=(const SelectablesTracker &) = delete;
SelectablesTracker &operator=(SelectablesTracker &&) = delete;

// Adds the mapping of Selectable and it's corresponding EventHandler object.
virtual bool addSelectableToTracker(
_In_ swss::Selectable *selectable,
_In_ SelectableEventHandler *eventHandler);

// Removes a Selectable from the map.
virtual bool removeSelectableFromTracker(
_In_ swss::Selectable *selectable);

// Checks if Selectable is present in the tracker map.
virtual bool selectableIsTracked(
_In_ swss::Selectable *selectable);

// Gets the EventHandler for a Selectable.
virtual SelectableEventHandler *getEventHandlerForSelectable(
_In_ swss::Selectable *selectable);

private:

using SelectableFdToEventHandlerMap = std::unordered_map<int, SelectableEventHandler *>;

SelectableFdToEventHandlerMap m_selectableFdToEventHandlerMap;
};

} // namespace syncd
1 change: 1 addition & 0 deletions tests/aspell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ config
const
CONST
consts
copyable
counterName
countOnly
cout
Expand Down
1 change: 1 addition & 0 deletions unittest/syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tests_SOURCES = main.cpp \
TestNotificationHandler.cpp \
TestMdioIpcServer.cpp \
TestPortStateChangeHandler.cpp \
TestSelectablesTracker.cpp \
TestVendorSai.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
Expand Down
37 changes: 37 additions & 0 deletions unittest/syncd/MockSelectablesTracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "SelectablesTracker.h"
#include "gmock/gmock.h"

namespace syncd
{

class MockSelectablesTracker : public SelectablesTracker
{
public:

MockSelectablesTracker() : SelectablesTracker() {}

~MockSelectablesTracker() override {}

MOCK_METHOD2(addSelectableToTracker, bool(swss::Selectable *selectable,
SelectableEventHandler *eventHandler) override);

MOCK_METHOD1(removeSelectableFromTracker,
bool(swss::Selectable *selectable) override);
};
} // namespace syncd
Loading

0 comments on commit 3987c8c

Please sign in to comment.