-
Notifications
You must be signed in to change notification settings - Fork 28
/
balancer_partrr.h
64 lines (54 loc) · 2.06 KB
/
balancer_partrr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2012-2013, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
#ifndef SRC_DATASTRUCTURES_BALANCER_PARTRR_H_
#define SRC_DATASTRUCTURES_BALANCER_PARTRR_H_
#include "datastructures/balancer.h"
#include "util/malloc.h"
#include "util/platform.h"
#include "util/threadlocals.h"
namespace scal {
class BalancerPartitionedRoundRobin {
public:
BalancerPartitionedRoundRobin(uint64_t partitions, uint64_t num_queues) {
num_queues_ = num_queues;
partitions_ = partitions;
enqueue_rrs_ = static_cast<uint64_t**>(calloc(
partitions_, sizeof(*enqueue_rrs_)));
dequeue_rrs_ = static_cast<uint64_t**>(calloc(
partitions_, sizeof(*dequeue_rrs_)));
for (uint64_t i = 0; i < partitions_; i++) {
enqueue_rrs_[i] = scal::get<uint64_t>(scal::kCachePrefetch);
dequeue_rrs_[i] = scal::get<uint64_t>(scal::kCachePrefetch);
*enqueue_rrs_[i] = (num_queues_/partitions_) * i;
*dequeue_rrs_[i] = (num_queues_/partitions_) * i;
}
}
_always_inline uint64_t get_id() {
uint64_t thread_id = scal::ThreadContext::get().thread_id();
return __sync_fetch_and_add(dequeue_rrs_[thread_id % partitions_], 1) % num_queues_;
}
_always_inline uint64_t put_id() {
uint64_t thread_id = scal::ThreadContext::get().thread_id();
return __sync_fetch_and_add(enqueue_rrs_[thread_id % partitions_], 1) % num_queues_;
}
/*
uint64_t get(uint64_t num_queues, MSQueue<uint64_t> **queues, bool enqueue) {
uint64_t thread_id = scal::ThreadContext::get().thread_id();
if (enqueue) {
return __sync_fetch_and_add(enqueue_rrs_[thread_id % partitions_], 1)
% num_queues;
} else {
return __sync_fetch_and_add(dequeue_rrs_[thread_id % partitions_], 1)
% num_queues;
}
}
*/
private:
uint64_t num_queues_;
uint64_t partitions_;
uint64_t **enqueue_rrs_;
uint64_t **dequeue_rrs_;
};
} // namespace scal
#endif // SRC_DATASTRUCTURES_BALANCER_PARTRR_H_