From d30b79a2fe8fb940ded4265882572bc9602606e9 Mon Sep 17 00:00:00 2001 From: fumingwei Date: Tue, 5 Sep 2023 20:32:16 +0800 Subject: bugfix:性能优化:自旋锁伪共享 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fieldstat_dynamic.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src/fieldstat_dynamic.cpp') diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp index 7b2cb52..275a91d 100644 --- a/src/fieldstat_dynamic.cpp +++ b/src/fieldstat_dynamic.cpp @@ -25,10 +25,11 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n instance->n_thread = n_thread; instance->n_thread_dynamic_metric = (struct dynamic_metric **)calloc(instance->n_thread, sizeof(struct dynamic_metric *)); - instance->uthash_locks = (pthread_spinlock_t*)calloc(n_thread, sizeof(pthread_spinlock_t)); + instance->uthash_locks = (struct uthash_spinlock *)calloc(n_thread, sizeof(struct uthash_spinlock)); for(int i = 0; i < n_thread; i++) { - pthread_spin_init(instance->uthash_locks + i, PTHREAD_PROCESS_SHARED); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + i; + pthread_spin_init(&(uthash_lock->lock), PTHREAD_PROCESS_SHARED); } return instance; @@ -101,7 +102,8 @@ void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance { for(i = 0; i < instance->n_thread; i++) { - pthread_spin_destroy(instance->uthash_locks + i); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + i; + pthread_spin_destroy(&(uthash_lock->lock)); } free((void *)instance->uthash_locks); instance->uthash_locks = NULL; @@ -342,9 +344,11 @@ static struct metric * read_dynamic_metric(struct fieldstat_dynamic_instance *in { return NULL; } - pthread_spin_lock(instance->uthash_locks + thread_id); + + struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id; + pthread_spin_lock(&(uthash_lock->lock)); HASH_FIND(hh, *head, dynamic_metric_key, dynamic_metric_keylen, find); - pthread_spin_unlock(instance->uthash_locks + thread_id); + pthread_spin_unlock(&(uthash_lock->lock)); if(find == NULL) { return NULL; @@ -420,9 +424,10 @@ static struct metric * create_dynamic_table_metric(struct fieldstat_dynamic_inst value->metrics[i] = metric; } - pthread_spin_lock(instance->uthash_locks + thread_id); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id; + pthread_spin_lock(&(uthash_lock->lock)); HASH_ADD_KEYPTR(hh, *head, value->metric_key, value->metric_keylen, value); - pthread_spin_unlock(instance->uthash_locks + thread_id); + pthread_spin_unlock(&(uthash_lock->lock)); return value->metrics[column_id]; } @@ -470,9 +475,10 @@ static struct metric * create_dynamic_metric(struct fieldstat_dynamic_instance * } *(insert->metrics) = metric; - pthread_spin_lock(instance->uthash_locks + thread_id); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id; + pthread_spin_lock(&(uthash_lock->lock)); HASH_ADD_KEYPTR(hh, *head, insert->metric_key, insert->metric_keylen, insert); - pthread_spin_unlock(instance->uthash_locks + thread_id); + pthread_spin_unlock(&(uthash_lock->lock)); return metric; } @@ -583,9 +589,10 @@ static struct metric **read_dynamic_row_metrics( head = &instance->n_thread_dynamic_metric[thread_id]; - pthread_spin_lock(instance->uthash_locks + thread_id); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id; + pthread_spin_lock(&(uthash_lock->lock)); HASH_FIND(hh, *head, metric_key, metric_keylen, find); - pthread_spin_unlock(instance->uthash_locks + thread_id); + pthread_spin_unlock(&(uthash_lock->lock)); if(find == NULL) { return NULL; @@ -650,9 +657,10 @@ static struct metric **create_dynamic_table_row_metrics( value->metrics[i] = metric; } - pthread_spin_lock(instance->uthash_locks + thread_id); + struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id; + pthread_spin_lock(&(uthash_lock->lock)); HASH_ADD_KEYPTR(hh, *head, value->metric_key, value->metric_keylen, value); - pthread_spin_unlock(instance->uthash_locks + thread_id); + pthread_spin_unlock(&(uthash_lock->lock)); return value->metrics; } -- cgit v1.2.3