1717#include " threadFilter.h"
1818#include " counters.h"
1919#include " os.h"
20+ #include " reverse_bits.h"
2021#include < stdlib.h>
2122#include < string.h>
2223
@@ -85,12 +86,24 @@ void ThreadFilter::clear() {
8586 _size = 0 ;
8687}
8788
89+ // The mapping has to be reversible: f(f(x)) == x
90+ int ThreadFilter::mapThreadId (int thread_id) {
91+ // We want to map the thread_id inside the same bitmap
92+ static_assert (BITMAP_SIZE >= (u16 )0xffff , " Potential verflow" );
93+ u16 lower16 = (u16 )(thread_id & 0xffff );
94+ lower16 = reverse16 (lower16);
95+ int tid = (thread_id & ~0xffff ) | lower16;
96+ return tid;
97+ }
98+
8899bool ThreadFilter::accept (int thread_id) {
100+ thread_id = mapThreadId (thread_id);
89101 u64 *b = bitmap (thread_id);
90102 return b != NULL && (word (b, thread_id) & (1ULL << (thread_id & 0x3f )));
91103}
92104
93105void ThreadFilter::add (int thread_id) {
106+ thread_id = mapThreadId (thread_id);
94107 u64 *b = bitmap (thread_id);
95108 if (b == NULL ) {
96109 b = (u64 *)OS::safeAlloc (BITMAP_SIZE);
@@ -111,6 +124,7 @@ void ThreadFilter::add(int thread_id) {
111124}
112125
113126void ThreadFilter::remove (int thread_id) {
127+ thread_id = mapThreadId (thread_id);
114128 u64 *b = bitmap (thread_id);
115129 if (b == NULL ) {
116130 return ;
@@ -132,7 +146,10 @@ void ThreadFilter::collect(std::vector<int> &v) {
132146 // order here
133147 u64 word = __atomic_load_n (&b[j], __ATOMIC_ACQUIRE);
134148 while (word != 0 ) {
135- v.push_back (start_id + j * 64 + __builtin_ctzl (word));
149+ int tid = start_id + j * 64 + __builtin_ctzl (word);
150+ // restore thread id
151+ tid = mapThreadId (tid);
152+ v.push_back (tid);
136153 word &= (word - 1 );
137154 }
138155 }
0 commit comments