summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfumingwei <[email protected]>2023-09-05 21:57:46 +0800
committerfumingwei <[email protected]>2023-09-06 14:47:15 +0800
commit2693d6332e0368f90d4de86ffc4c0a1369d4f247 (patch)
tree08810ffae483c49d6ac23d96f3341192a79f957e /src
parentd30b79a2fe8fb940ded4265882572bc9602606e9 (diff)
support mulit type lockv3.0.15
Diffstat (limited to 'src')
-rw-r--r--src/fieldstat_dynamic.cpp139
-rw-r--r--src/fieldstat_internal.h24
-rw-r--r--src/line_protocol_output.cpp20
3 files changed, 178 insertions, 5 deletions
diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp
index 275a91d..a0d195e 100644
--- a/src/fieldstat_dynamic.cpp
+++ b/src/fieldstat_dynamic.cpp
@@ -25,12 +25,30 @@ 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 *));
+#if USING_SPINLOCK
instance->uthash_locks = (struct uthash_spinlock *)calloc(n_thread, sizeof(struct uthash_spinlock));
for(int i = 0; i < n_thread; i++)
{
struct uthash_spinlock *uthash_lock = instance->uthash_locks + i;
pthread_spin_init(&(uthash_lock->lock), PTHREAD_PROCESS_SHARED);
}
+#endif
+#if USING_RWLOCK
+ instance->uthash_locks = (struct uthash_rwlock *)calloc(n_thread, sizeof(struct uthash_rwlock));
+ for(int i = 0; i < n_thread; i++)
+ {
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + i;
+ pthread_rwlock_init(&(uthash_lock->lock), NULL);
+ }
+#endif
+#if USING_MUTEX
+ instance->uthash_locks = (struct uthash_mutex *)calloc(n_thread, sizeof(struct uthash_mutex));
+ for(int i = 0; i < n_thread; i++)
+ {
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + i;
+ pthread_mutex_init(&(uthash_lock->lock), NULL);
+ }
+#endif
return instance;
}
@@ -97,7 +115,7 @@ void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance
table_metric_free(instance->table_metrics[i]);
instance->table_metrics[i] = NULL;
}
-
+#if USING_SPINLOCK
if(instance->uthash_locks)
{
for(i = 0; i < instance->n_thread; i++)
@@ -108,6 +126,31 @@ void fieldstat_dynamic_instance_free(struct fieldstat_dynamic_instance *instance
free((void *)instance->uthash_locks);
instance->uthash_locks = NULL;
}
+#endif
+#if USING_RWLOCK
+ if(instance->uthash_locks)
+ {
+ for(i = 0; i < instance->n_thread; i++)
+ {
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + i;
+ pthread_rwlock_destroy(&(uthash_lock->lock));
+ }
+ free((void *)instance->uthash_locks);
+ instance->uthash_locks = NULL;
+ }
+#endif
+#if USING_MUTEX
+ if(instance->uthash_locks)
+ {
+ for(i = 0; i < instance->n_thread; i++)
+ {
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + i;
+ pthread_mutex_destroy(&(uthash_lock->lock));
+ }
+ free((void *)instance->uthash_locks);
+ instance->uthash_locks = NULL;
+ }
+#endif
free(instance->n_thread_dynamic_metric);
instance->n_thread_dynamic_metric = NULL;
free(instance);
@@ -345,10 +388,30 @@ static struct metric * read_dynamic_metric(struct fieldstat_dynamic_instance *in
return NULL;
}
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id;
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_rwlock_rdlock(&(uthash_lock->lock));
+#endif
+
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
+
HASH_FIND(hh, *head, dynamic_metric_key, dynamic_metric_keylen, find);
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
if(find == NULL)
{
return NULL;
@@ -424,10 +487,28 @@ static struct metric * create_dynamic_table_metric(struct fieldstat_dynamic_inst
value->metrics[i] = metric;
}
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id;
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_rwlock_wrlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
HASH_ADD_KEYPTR(hh, *head, value->metric_key, value->metric_keylen, value);
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
return value->metrics[column_id];
}
@@ -475,10 +556,28 @@ static struct metric * create_dynamic_metric(struct fieldstat_dynamic_instance *
}
*(insert->metrics) = metric;
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id;
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_rwlock_wrlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
HASH_ADD_KEYPTR(hh, *head, insert->metric_key, insert->metric_keylen, insert);
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
return metric;
}
@@ -588,11 +687,28 @@ static struct metric **read_dynamic_row_metrics(
struct dynamic_metric *find = NULL;
head = &instance->n_thread_dynamic_metric[thread_id];
-
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id;
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_rwlock_rdlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
HASH_FIND(hh, *head, metric_key, metric_keylen, find);
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
if(find == NULL)
{
return NULL;
@@ -656,11 +772,28 @@ static struct metric **create_dynamic_table_row_metrics(
metric->table_column_id = i;
value->metrics[i] = metric;
}
-
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + thread_id;
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_rwlock_wrlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + thread_id;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
HASH_ADD_KEYPTR(hh, *head, value->metric_key, value->metric_keylen, value);
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
return value->metrics;
}
diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h
index 132f9f5..628f348 100644
--- a/src/fieldstat_internal.h
+++ b/src/fieldstat_internal.h
@@ -68,6 +68,10 @@
#define CACHE_LINE_SIZE 64
+#define USING_SPINLOCK 1
+#define USING_RWLOCK 0
+#define USING_MUTEX 0
+
enum field_calc_algo
{
FS_CALC_CURRENT=0,
@@ -226,6 +230,18 @@ struct uthash_spinlock
pthread_spinlock_t lock;
} __attribute__((aligned(CACHE_LINE_SIZE)));
+
+struct uthash_rwlock
+{
+ pthread_rwlock_t lock;
+} __attribute__((aligned(CACHE_LINE_SIZE)));
+
+struct uthash_mutex
+{
+ pthread_mutex_t lock;
+} __attribute__((aligned(CACHE_LINE_SIZE)));
+
+
struct fieldstat_dynamic_instance
{
char name[INSTANCE_NAME_LEN];
@@ -247,7 +263,15 @@ struct fieldstat_dynamic_instance
int n_thread;
int output_type; // 0b0000:not output, 0b1000:output file, 0b0100:output line_protocol, 0b0010: output statsd, 0b0001: output prometheus
//pthread_spinlock_t *uthash_locks;
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_locks;
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_locks;
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_locks;
+#endif
};
void prometheus_endpoint_instance_output(struct http_request_s* request);
diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp
index 3c687ed..5c01512 100644
--- a/src/line_protocol_output.cpp
+++ b/src/line_protocol_output.cpp
@@ -481,9 +481,18 @@ int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *insta
for(int i = 0; i < instance->n_thread; i++)
{
std::vector<std::string> line_buf_to_send;
+#if USING_SPINLOCK
struct uthash_spinlock *uthash_lock = instance->uthash_locks + i;
-
pthread_spin_lock(&(uthash_lock->lock));
+#endif
+#if USING_RWLOCK
+ struct uthash_rwlock *uthash_lock = instance->uthash_locks + i;
+ pthread_rwlock_wrlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ struct uthash_mutex *uthash_lock = instance->uthash_locks + i;
+ pthread_mutex_lock(&(uthash_lock->lock));
+#endif
head = &instance->n_thread_dynamic_metric[i];
HASH_ITER(hh, *head, dyn_metric, tmp_dyn_metric)
{
@@ -504,8 +513,15 @@ int line_protocol_dynamic_metric_output(struct fieldstat_dynamic_instance *insta
/* copy the line_buf as str to vector line_buf_to_send */
line_buf_to_send.push_back(std::string(line_buf));
}
+#if USING_SPINLOCK
pthread_spin_unlock(&(uthash_lock->lock));
-
+#endif
+#if USING_RWLOCK
+ pthread_rwlock_unlock(&(uthash_lock->lock));
+#endif
+#if USING_MUTEX
+ pthread_mutex_unlock(&(uthash_lock->lock));
+#endif
for (std::vector<std::string>::iterator it = line_buf_to_send.begin(); it != line_buf_to_send.end(); ++it)
{
const std::string& str = *it;