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

selectabletimer: add mutex to start() and stop() #614

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 27 additions & 8 deletions common/selectabletimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SelectableTimer::SelectableTimer(const timespec& interval, int pri)
SWSS_LOG_THROW("failed to create timerfd, errno: %s", strerror(errno));
}
setInterval(interval);
m_running = false;
}

SelectableTimer::~SelectableTimer()
Expand All @@ -36,22 +37,40 @@ SelectableTimer::~SelectableTimer()

void SelectableTimer::start()
Copy link
Contributor

@qiluo-msft qiluo-msft May 6, 2022

Choose a reason for hiding this comment

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

start

Could you add new test case which will fail with original code? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

@ds952811 ds952811 May 9, 2022

Choose a reason for hiding this comment

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

Please refer to timer_ut.cpp: line 43-51

{
// Set the timer interval and the timer is automatically started
int rc = timerfd_settime(m_tfd, 0, &m_interval, NULL);
if (rc == -1)
m_mutex.lock();
if (!m_running)
{
SWSS_LOG_THROW("failed to set timerfd, errno: %s", strerror(errno));
// Set the timer interval and the timer is automatically started
int rc = timerfd_settime(m_tfd, 0, &m_interval, NULL);
if (rc == -1)
{
SWSS_LOG_THROW("failed to set timerfd, errno: %s", strerror(errno));
}
else
{
m_running = true;
}
}
m_mutex.unlock();
}

void SelectableTimer::stop()
{
// Set the timer interval and the timer is automatically started
int rc = timerfd_settime(m_tfd, 0, &m_zero, NULL);
if (rc == -1)
m_mutex.lock();
if (m_running)
{
SWSS_LOG_THROW("failed to set timerfd to zero, errno: %s", strerror(errno));
// Set the timer interval and the timer is automatically started
int rc = timerfd_settime(m_tfd, 0, &m_zero, NULL);
if (rc == -1)
{
SWSS_LOG_THROW("failed to set timerfd to zero, errno: %s", strerror(errno));
}
else
{
m_running = false;
}
}
m_mutex.unlock();
}

void SelectableTimer::reset()
Expand Down
3 changes: 3 additions & 0 deletions common/selectabletimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <limits>
#include <mutex>
#include <sys/timerfd.h>
#include "selectable.h"

Expand All @@ -22,6 +23,8 @@ class SelectableTimer : public Selectable
uint64_t readData() override;

private:
std::mutex m_mutex;
bool m_running;
int m_tfd;
itimerspec m_interval;
itimerspec m_zero;
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tests_SOURCES = redis_ut.cpp \
boolean_ut.cpp \
status_code_util_test.cpp \
saiaclschema_ut.cpp \
timer_ut.cpp \
main.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(LIBNL_CFLAGS)
Expand Down
36 changes: 0 additions & 36 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,42 +653,6 @@ TEST(DBConnector, selectableevent)
EXPECT_EQ(value, 2);
}

TEST(DBConnector, selectabletimer)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to timer_ut.cpp

{
timespec interval = { .tv_sec = 1, .tv_nsec = 0 };
SelectableTimer timer(interval);

Select s;
s.addSelectable(&timer);
Selectable *sel;
int result;

// Wait a non started timer
result = s.select(&sel, 2000);
ASSERT_EQ(result, Select::TIMEOUT);

// Wait long enough so we got timer notification first
timer.start();
result = s.select(&sel, 2000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);

// Wait short so we got select timeout first
result = s.select(&sel, 10);
ASSERT_EQ(result, Select::TIMEOUT);

// Wait long enough so we got timer notification first
result = s.select(&sel, 10000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);

// Reset and wait long enough so we got timer notification first
timer.reset();
result = s.select(&sel, 10000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);
}

TEST(Table, basic)
{
TableBasicTest("TABLE_UT_TEST", true);
Expand Down
52 changes: 52 additions & 0 deletions tests/timer_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "common/select.h"
#include "common/selectabletimer.h"
#include "gtest/gtest.h"

using namespace std;
using namespace swss;

TEST(TIMER, selectabletimer)
{
timespec interval = { .tv_sec = 1, .tv_nsec = 0 };
SelectableTimer timer(interval);

Select s;
s.addSelectable(&timer);
Selectable *sel;
int result;

// Wait a non started timer
result = s.select(&sel, 2000);
ASSERT_EQ(result, Select::TIMEOUT);

// Wait long enough so we got timer notification first
timer.start();
result = s.select(&sel, 2000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);

// Wait short so we got select timeout first
result = s.select(&sel, 10);
ASSERT_EQ(result, Select::TIMEOUT);

// Wait long enough so we got timer notification first
result = s.select(&sel, 10000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);

// Reset and wait long enough so we got timer notification first
timer.reset();
result = s.select(&sel, 10000);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);

// Check if the timer gets reset by subsequent timer.start()
Copy link
Contributor

Choose a reason for hiding this comment

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

Check if the timer gets reset by subsequent timer.start()

Could you help me understand what is the behavior of original code in this testcase? How does it fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case of original code, since the timer is created with flag=0, a relative timer is started, and everytime that timer.start() gets invoked, timerfd_settime() will reset the timeout based on the current timestamp, hence it will cause unexpected delay here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean original code will fail on this line?

ASSERT_EQ(result, Select::OBJECT);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it'll fail at that line without the changes in the PR

for (int t = 0; t < 2000; ++t)
{
timer.start();
usleep(1000);
}
result = s.select(&sel, 1);
ASSERT_EQ(result, Select::OBJECT);
ASSERT_EQ(sel, &timer);
}