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

Use butil::ThreadLocal to store keytable #2645

Merged
merged 8 commits into from
Jul 7, 2024
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
96 changes: 71 additions & 25 deletions src/bthread/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <pthread.h>
#include "butil/macros.h"
#include "butil/atomicops.h"
#include "butil/thread_key.h"
#include "bvar/passive_status.h"
#include "bthread/errno.h" // EAGAIN
#include "bthread/task_group.h" // TaskGroup
Expand Down Expand Up @@ -204,14 +205,56 @@ class BAIDU_CACHELINE_ALIGNMENT KeyTable {
SubKeyTable* _subs[KEY_1STLEVEL_SIZE];
};

struct KeyTableList {
KeyTableList() {
keytable = NULL;
}
~KeyTableList() {
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::KeyTable* old_kt = bthread::tls_bls.keytable;
while (keytable) {
bthread::KeyTable* kt = keytable;
keytable = kt->next;
bthread::tls_bls.keytable = kt;
if (g) {
g->current_task()->local_storage.keytable = kt;
}
chenBright marked this conversation as resolved.
Show resolved Hide resolved
delete kt;
if (old_kt == kt) {
old_kt = NULL;
}
g = bthread::tls_task_group;
}
bthread::tls_bls.keytable = old_kt;
if(g) {
g->current_task()->local_storage.keytable = old_kt;
}
}
KeyTable* keytable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class 类型不建议直接把成员暴露成public,要么改成struct,要么封装成getter/setter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成了struct

};

static KeyTable* borrow_keytable(bthread_keytable_pool_t* pool) {
if (pool != NULL && pool->free_keytables) {
BAIDU_SCOPED_LOCK(pool->mutex);
KeyTable* p = (KeyTable*)pool->free_keytables;
if (p) {
pool->free_keytables = p->next;
if (pool != NULL && (pool->list || pool->free_keytables)) {
KeyTable* p;
pthread_rwlock_rdlock(&pool->rwlock);
auto list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
if (list && list->get()->keytable) {
p = list->get()->keytable;
list->get()->keytable = p->next;
pthread_rwlock_unlock(&pool->rwlock);
return p;
}
pthread_rwlock_unlock(&pool->rwlock);
if (pool->free_keytables) {
pthread_rwlock_wrlock(&pool->rwlock);
p = (KeyTable*)pool->free_keytables;
if (p) {
pool->free_keytables = p->next;
pthread_rwlock_unlock(&pool->rwlock);
return p;
}
pthread_rwlock_unlock(&pool->rwlock);
}
}
return NULL;
}
Expand All @@ -226,14 +269,16 @@ void return_keytable(bthread_keytable_pool_t* pool, KeyTable* kt) {
delete kt;
return;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_rdlock(&pool->rwlock);
if (pool->destroyed) {
mu.unlock();
pthread_rwlock_unlock(&pool->rwlock);
delete kt;
return;
}
kt->next = (KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
auto list = (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全局free_keytables里的kt一旦被借走就永远还不回去free_keytables了吗?这样keytables的复用率会降低吧,如果线程数很多,可能会占用更多的内存

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不会返回给free_keytables了。free_keytables的大小是根据server.option的reserved_thread_local_data变量设置的,默认为0。
不过这里的线程指的应该只是一个bthread_keytable_pool_t pool所对应的server的brpc_worker threads,而不是普通的pthread。
所以相比使用单链表管理,如果brpc_worker thread是公平调度的话,内部的butil::ThreadLocal维护的链表长度应该是比使用一个单链表来说要短,但是性能会更好,因为同样存在回收机制,内存不会多太多。

kt->next = list->get()->keytable;
list->get()->keytable = kt;
pthread_rwlock_unlock(&pool->rwlock);
}

static void cleanup_pthread(void* arg) {
chenBright marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -279,7 +324,8 @@ int bthread_keytable_pool_init(bthread_keytable_pool_t* pool) {
LOG(ERROR) << "Param[pool] is NULL";
return EINVAL;
}
pthread_mutex_init(&pool->mutex, NULL);
pthread_rwlock_init(&pool->rwlock, NULL);
pool->list = new butil::ThreadLocal<bthread::KeyTableList>();
pool->free_keytables = NULL;
pool->destroyed = 0;
return 0;
Expand All @@ -291,16 +337,16 @@ int bthread_keytable_pool_destroy(bthread_keytable_pool_t* pool) {
return EINVAL;
}
bthread::KeyTable* saved_free_keytables = NULL;
{
BAIDU_SCOPED_LOCK(pool->mutex);
if (pool->free_keytables) {
saved_free_keytables = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = NULL;
}
pool->destroyed = 1;
}
pthread_rwlock_wrlock(&pool->rwlock);
pool->destroyed = 1;
delete (butil::ThreadLocal<bthread::KeyTableList>*)pool->list;
saved_free_keytables = (bthread::KeyTable*)pool->free_keytables;
pool->list = NULL;
pool->free_keytables = NULL;
pthread_rwlock_unlock(&pool->rwlock);

// Cheat get/setspecific and destroy the keytables.
bthread::TaskGroup* const g = bthread::tls_task_group;
bthread::TaskGroup* g = bthread::tls_task_group;
bthread::KeyTable* old_kt = bthread::tls_bls.keytable;
while (saved_free_keytables) {
bthread::KeyTable* kt = saved_free_keytables;
Expand All @@ -310,9 +356,7 @@ int bthread_keytable_pool_destroy(bthread_keytable_pool_t* pool) {
g->current_task()->local_storage.keytable = kt;
}
delete kt;
if (old_kt == kt) {
old_kt = NULL;
}
g = bthread::tls_task_group;
}
bthread::tls_bls.keytable = old_kt;
if (g) {
Expand All @@ -330,11 +374,12 @@ int bthread_keytable_pool_getstat(bthread_keytable_pool_t* pool,
LOG(ERROR) << "Param[pool] or Param[stat] is NULL";
return EINVAL;
}
std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_rdlock(&pool->rwlock);
size_t count = 0;
bthread::KeyTable* p = (bthread::KeyTable*)pool->free_keytables;
for (; p; p = p->next, ++count) {}
stat->nfree = count;
pthread_rwlock_unlock(&pool->rwlock);
return 0;
}

Expand Down Expand Up @@ -365,14 +410,15 @@ void bthread_keytable_pool_reserve(bthread_keytable_pool_t* pool,
kt->set_data(key, data);
} // else append kt w/o data.

std::unique_lock<pthread_mutex_t> mu(pool->mutex);
pthread_rwlock_wrlock(&pool->rwlock);
if (pool->destroyed) {
mu.unlock();
pthread_rwlock_unlock(&pool->rwlock);
delete kt;
break;
}
kt->next = (bthread::KeyTable*)pool->free_keytables;
pool->free_keytables = kt;
pthread_rwlock_unlock(&pool->rwlock);
if (data == NULL) {
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bthread/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ inline std::ostream& operator<<(std::ostream& os, bthread_key_t key) {
#endif // __cplusplus

typedef struct {
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
void* list;
void* free_keytables;
int destroyed;
} bthread_keytable_pool_t;
Expand Down
23 changes: 14 additions & 9 deletions test/bthread_key_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,18 @@ TEST(KeyTest, set_tls_before_creating_any_bthread) {

struct PoolData {
bthread_key_t key;
PoolData* expected_data;
PoolData* data;
int seq;
int end_seq;
};

bool use_same_keytable = false;

static void pool_thread_impl(PoolData* data) {
ASSERT_EQ(data->expected_data, (PoolData*)bthread_getspecific(data->key));
if (NULL == bthread_getspecific(data->key)) {
ASSERT_EQ(0, bthread_setspecific(data->key, data));
} else {
use_same_keytable = true;
}
};

Expand Down Expand Up @@ -385,19 +388,21 @@ TEST(KeyTest, using_pool) {
ASSERT_EQ(0, bthread_start_urgent(&bth, &attr, pool_thread, &bth_data));
ASSERT_EQ(0, bthread_join(bth, NULL));
ASSERT_EQ(0, bth_data.seq);
ASSERT_EQ(1, bthread_keytable_pool_size(&pool));

PoolData bth2_data = { key, &bth_data, 0, 3 };
PoolData bth2_data = { key, NULL, 0, 3 };
bthread_t bth2;
ASSERT_EQ(0, bthread_start_urgent(&bth2, &attr2, pool_thread, &bth2_data));
ASSERT_EQ(0, bthread_join(bth2, NULL));
ASSERT_EQ(0, bth2_data.seq);
ASSERT_EQ(1, bthread_keytable_pool_size(&pool));

ASSERT_EQ(0, bthread_keytable_pool_destroy(&pool));

EXPECT_EQ(bth_data.end_seq, bth_data.seq);
EXPECT_EQ(0, bth2_data.seq);
if (use_same_keytable) {
EXPECT_EQ(bth_data.end_seq, bth_data.seq);
EXPECT_EQ(0, bth2_data.seq);
} else {
EXPECT_EQ(bth_data.end_seq, bth_data.seq);
EXPECT_EQ(bth_data.end_seq, bth2_data.seq);
}

ASSERT_EQ(0, bthread_key_delete(key));
}
Expand All @@ -415,7 +420,7 @@ static void lid_dtor(void* tls) {

static void lid_worker_impl(bthread_key_t key) {
ASSERT_EQ(NULL, bthread_getspecific(key));
ASSERT_EQ(0, bthread_setspecific(key, (void*)seq.fetch_add(1)));
ASSERT_EQ(0, bthread_setspecific(key, (void*)lid_seq.fetch_add(1)));
}

static void* lid_worker(void* arg) {
Expand Down
Loading