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

thread_migrate strategy changes from random to round-robin #163

Merged
merged 1 commit into from
Jul 25, 2023
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
12 changes: 6 additions & 6 deletions thread/workerpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include <algorithm>
#include <random>
#include <thread>
#include <atomic>

namespace photon {

Expand All @@ -34,19 +35,17 @@ class WorkPool::impl {
photon::mutex worker_mtx;
std::vector<std::thread> owned_std_threads;
std::vector<photon::vcpu_base *> vcpus;
std::atomic<uint64_t> vcpu_index{0};
photon::semaphore queue_sem;
photon::semaphore ready_vcpu;
photon::condition_variable exit_cv;
photon::common::RingChannel<
LockfreeMPMCRingQueue<Delegate<void>, RING_SIZE>>
ring;

std::random_device rd;
std::mt19937 gen;
int mode;

impl(size_t vcpu_num, int ev_engine, int io_engine, int mode)
: queue_sem(0), ready_vcpu(0), gen(rd()), mode(mode) {
: queue_sem(0), ready_vcpu(0), mode(mode) {
for (size_t i = 0; i < vcpu_num; ++i) {
owned_std_threads.emplace_back(
&WorkPool::impl::worker_thread_routine, this, ev_engine,
Expand Down Expand Up @@ -134,8 +133,9 @@ class WorkPool::impl {
}

photon::vcpu_base *get_vcpu_in_pool(size_t index) {
if (index >= vcpus.size()) {
index = gen() % vcpus.size();
auto size = vcpus.size();
if (index >= size) {
index = vcpu_index++ % size;
}
return vcpus[index];
}
Expand Down
2 changes: 1 addition & 1 deletion thread/workerpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class WorkPool {
*
* @param th Photon thread that goint to migrate
* @param index Which vcpu in pool to migrate to. if index is not in range
* [0, vcpu_num), it will choose random one in pool.
* [0, vcpu_num), it will choose the next one in pool (round-robin).
* @return int 0 for success, and <0 means failed to migrate.
*/
int thread_migrate(photon::thread* th = CURRENT, size_t index = -1UL) {
Expand Down