Skip to content

[POC] Improve select and selectable to return same priority event by event happen time. #981

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

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 8 additions & 4 deletions common/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void Select::addSelectable(Selectable *selectable)

if (selectable->initializedWithData())
{
selectable->updateEarliestEventTime();
m_ready.insert(selectable);
}

Expand All @@ -68,6 +69,7 @@ void Select::removeSelectable(Selectable *selectable)
const int fd = selectable->getFd();

m_objects.erase(fd);
selectable->resetEarliestEventTime();
m_ready.erase(selectable);

int res = ::epoll_ctl(m_epoll_fd, EPOLL_CTL_DEL, fd, NULL);
Expand Down Expand Up @@ -130,6 +132,7 @@ int Select::poll_descriptors(Selectable **c, unsigned int timeout, bool interrup
SWSS_LOG_ERROR("readData error: %s", ex.what());
return Select::ERROR;
}
sel->updateEarliestEventTime();
m_ready.insert(sel);
}

Expand All @@ -138,12 +141,9 @@ int Select::poll_descriptors(Selectable **c, unsigned int timeout, bool interrup
auto sel = *m_ready.begin();

m_ready.erase(sel);
// we must update clock only when the selector out of the m_ready
// otherwise we break invariant of the m_ready
sel->updateLastUsedTime();

if (!sel->hasData())
{
sel->resetEarliestEventTime();
continue;
}

Expand All @@ -154,6 +154,10 @@ int Select::poll_descriptors(Selectable **c, unsigned int timeout, bool interrup
// reinsert Selectable back to the m_ready set, when there're more messages in the cache
m_ready.insert(sel);
}
else
{
sel->resetEarliestEventTime();
}

sel->updateAfterRead();

Expand Down
11 changes: 6 additions & 5 deletions common/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ class Select
return false;

/* if the priorities are equal */
/* use Selectable which was selected earlier */
if (a->getLastUsedTime() < b->getLastUsedTime())
/* use Selectable which was select earlier */
if (a->getEarliestEventTime() < b->getEarliestEventTime())
return true;
else if (a->getLastUsedTime() > b->getLastUsedTime())
else if (a->getEarliestEventTime() > b->getEarliestEventTime())
return false;

/* when a == b */
return false;
// https://en.cppreference.com/w/cpp/container/set
// two objects a and b are considered equivalent if neither compares less than the other: !comp(a, b) && !comp(b, a).
return (a != b);
}
};

Expand Down
26 changes: 19 additions & 7 deletions common/selectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

namespace swss {

using SelectableTimeType = std::chrono::time_point<std::chrono::steady_clock>;

class Selectable
{
public:
Selectable(int pri = 0) : m_priority(pri),
m_last_used_time(std::chrono::steady_clock::now()) {}
m_earliest_event_time(std::chrono::steady_clock::now()) {}

virtual ~Selectable() = default;

Expand Down Expand Up @@ -62,22 +64,32 @@ class Selectable

friend class Select;

// only Select class can access and update m_last_used_time
// only Select class can access and update m_earliest_event_time


std::chrono::time_point<std::chrono::steady_clock> getLastUsedTime() const
std::chrono::time_point<std::chrono::steady_clock> getEarliestEventTime() const
{
return m_last_used_time;
return m_earliest_event_time;
}

void updateLastUsedTime()
void updateEarliestEventTime()
{
m_last_used_time = std::chrono::steady_clock::now();
if (m_earliest_event_time != SelectableTimeType::max())
{
return;
}

m_earliest_event_time = std::chrono::steady_clock::now();
}

void resetEarliestEventTime()
{
m_earliest_event_time = SelectableTimeType::max();
}

int m_priority; // defines priority of Selectable inside Select
// higher value is higher priority
std::chrono::time_point<std::chrono::steady_clock> m_last_used_time;
SelectableTimeType m_earliest_event_time;
};

}
Expand Down
Loading