Skip to content

Commit 90a3327

Browse files
committed
tfscheduler: improve flp token selection bias
1 parent ddad638 commit 90a3327

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

src/TfScheduler/TfSchedulerTokenManager.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ bool TfSchedulerTokenManager::connectTfBuilder(const std::string &pTfBuilderId,
182182

183183
void TfSchedulerTokenManager::TokenManagerThread()
184184
{
185-
static unsigned sRandomSeed = 0;
186185
std::uint64_t lSpinCounter = 0;
187186
std::uint64_t lNumReqSinceReset = 1000; // prevent throttling on init
188187

@@ -229,7 +228,7 @@ void TfSchedulerTokenManager::TokenManagerThread()
229228

230229
const bool lHaveReq = mTokenRequestQueue.consume_one([&](TokenRequestInfo &lReqInfo) {
231230
lReqInfo.mRequest.mTokensRequested &= mTokens;
232-
lReplyTokenIdx = lReqInfo.mRequest.mTokensRequested.random_idx(sRandomSeed++);
231+
lReplyTokenIdx = lReqInfo.mRequest.mTokensRequested.random_idx();
233232
assert ((lReplyTokenIdx == 0) || (lReplyTokenIdx && (lReqInfo.mRequest.mTokensRequested.get(lReplyTokenIdx) == true)));
234233
lReplyEp = lReqInfo.mReplyEp;
235234
});

src/common/base/Utilities.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,23 +359,26 @@ struct TokenBitfield {
359359
return sInvalidIdx;
360360
}
361361

362-
inline TokenBitfieldIndexType random_idx(TokenBitfieldIndexType seed) const {
363-
seed = (seed ^ 0x4a981d8f) % NUM_TOKENS; // make sure index can wrap
362+
inline TokenBitfieldIndexType random_idx() const {
364363

365-
for (TokenBitfieldIndexType i = 0; i < NUM_TOKENS; i++) {
366-
if (mRequestTokenBitset[(seed % NUM_TOKENS) / NUM_ELEM_BITS] == 0) {
367-
// jump to the next element
368-
seed += NUM_ELEM_BITS;
369-
continue;
370-
}
364+
TokenBitfieldIndexType idx = (rand() % NUM_TOKENS) + 1; // make sure index can wrap
365+
366+
int idxcnt = 0;
367+
TokenBitfieldIndexType idxarr[4] = { 0 };
371368

372-
if (get(seed)) {
373-
return (seed % NUM_TOKENS);
369+
for (TokenBitfieldIndexType i = 0; i < NUM_TOKENS; i++, idx = (idx % NUM_TOKENS) + 1) {
370+
assert (idx > 0);
371+
assert (idx <= NUM_TOKENS);
372+
if (get(idx)) {
373+
idxarr[idxcnt++] = idx;
374+
375+
if (idxcnt == 4) {
376+
break;
377+
}
374378
}
375-
seed++;
376379
}
377380

378-
return sInvalidIdx;
381+
return (idxcnt == 0) ? sInvalidIdx : idxarr[rand() % idxcnt];
379382
}
380383

381384
inline TokenBitfield& operator&=(const TokenBitfield& b) {

src/tests/test_Bitmap.cxx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,48 @@ BOOST_AUTO_TEST_CASE(GetNextSeqNameTest)
4848
TokenBitfield<256> lField1;
4949
TokenBitfield<256> lField2;
5050

51-
lField1.set(73);
5251
lField1.set(53);
52+
lField1.set(73);
53+
lField1.set(196);
54+
lField1.set(197);
55+
lField1.set(198);
56+
lField1.set(199);
57+
lField1.set(200);
58+
lField1.set(201);
59+
lField1.set(202);
5360

5461
lField2.set(23);
5562
lField2.set(73);
63+
lField2.set(196);
64+
lField2.set(197);
65+
lField2.set(198);
66+
lField2.set(199);
67+
lField2.set(200);
68+
lField2.set(201);
69+
lField2.set(202);
70+
5671
lField2 &= lField1;
57-
BOOST_CHECK(lField2.first() == 73);
58-
BOOST_CHECK(lField2.random_idx(0) == 73);
59-
BOOST_CHECK(lField2.random_idx(1) == 73);
60-
BOOST_CHECK(lField2.random_idx(2) == 73);
61-
BOOST_CHECK(lField2.random_idx(3) == 73);
62-
BOOST_CHECK(lField2.random_idx(443) == 73);
63-
BOOST_CHECK(lField2.random_idx(2222) == 73);
64-
65-
lField2.clr(73);
66-
BOOST_CHECK(lField2.empty());
72+
73+
// lField2.set_all();
74+
75+
int hist[256] = { 0 };
76+
77+
BOOST_CHECK(lField2.get(73));
78+
79+
80+
81+
for (int i = 0; i < 100000; i ++) {
82+
hist[lField2.random_idx() - 1] += 1;
83+
}
84+
85+
86+
for (int i = 0; i < 256; i ++) {
87+
if (hist[i]) {
88+
std::cout << "hist[" << i << "] = " << hist[i] << std::endl;
89+
}
90+
}
91+
92+
BOOST_CHECK(true);
6793
}
6894

6995

0 commit comments

Comments
 (0)