Compare commits

...

3 Commits

Author SHA1 Message Date
YuQing f0484579e0 tests/Makefile add mblock_benchmark 2025-07-18 11:21:18 +08:00
YuQing 6a18162a12 add src/tests/mblock_benchmark.c 2025-07-18 11:06:36 +08:00
YuQing 8e834f7165 src/spinlock.[hc]: use pthread spinlock 2025-07-18 11:04:53 +08:00
5 changed files with 134 additions and 9 deletions

1
.gitignore vendored
View File

@ -62,6 +62,7 @@ src/tests/test_sorted_array
src/tests/test_sorted_queue src/tests/test_sorted_queue
src/tests/test_thread_local src/tests/test_thread_local
src/tests/test_memcpy src/tests/test_memcpy
src/tests/mblock_benchmark
# other # other
*.swp *.swp

View File

@ -39,9 +39,8 @@ static inline int fc_futex(int *ptr, int op, int val)
int fc_spinlock_init(FCSpinlock *lock, int *cond) int fc_spinlock_init(FCSpinlock *lock, int *cond)
{ {
#ifdef OS_LINUX #ifdef OS_LINUX
lock->mutex = 0;
lock->cond = cond; lock->cond = cond;
return 0; return pthread_spin_init(&lock->mutex, 0);
#else #else
return init_pthread_lock_cond_pair(&lock->lcp); return init_pthread_lock_cond_pair(&lock->lcp);
#endif #endif
@ -58,7 +57,7 @@ void fc_spinlock_destroy(FCSpinlock *lock)
int fc_spinlock_lock(FCSpinlock *lock) int fc_spinlock_lock(FCSpinlock *lock)
{ {
#ifdef OS_LINUX #ifdef OS_LINUX
return fc_futex(&lock->mutex, FUTEX_LOCK_PI_PRIVATE, 0); return pthread_spin_lock(&lock->mutex);
#else #else
return pthread_mutex_lock(&lock->lcp.lock); return pthread_mutex_lock(&lock->lcp.lock);
#endif #endif
@ -67,7 +66,7 @@ int fc_spinlock_lock(FCSpinlock *lock)
int fc_spinlock_trylock(FCSpinlock *lock) int fc_spinlock_trylock(FCSpinlock *lock)
{ {
#ifdef OS_LINUX #ifdef OS_LINUX
return fc_futex(&lock->mutex, FUTEX_TRYLOCK_PI_PRIVATE, 0); return pthread_spin_trylock(&lock->mutex);
#else #else
return pthread_mutex_trylock(&lock->lcp.lock); return pthread_mutex_trylock(&lock->lcp.lock);
#endif #endif
@ -76,7 +75,7 @@ int fc_spinlock_trylock(FCSpinlock *lock)
int fc_spinlock_unlock(FCSpinlock *lock) int fc_spinlock_unlock(FCSpinlock *lock)
{ {
#ifdef OS_LINUX #ifdef OS_LINUX
return fc_futex(&lock->mutex, FUTEX_UNLOCK_PI_PRIVATE, 0); return pthread_spin_unlock(&lock->mutex);
#else #else
return pthread_mutex_unlock(&lock->lcp.lock); return pthread_mutex_unlock(&lock->lcp.lock);
#endif #endif
@ -88,11 +87,11 @@ int fc_spinlock_wait(FCSpinlock *lock, const int expected)
int result; int result;
int lock_ret; int lock_ret;
if ((result=fc_futex(&lock->mutex, FUTEX_UNLOCK_PI_PRIVATE, 0)) != 0) { if ((result=pthread_spin_unlock(&lock->mutex)) != 0) {
return result; return result;
} }
result = fc_futex(lock->cond, FUTEX_WAIT_PRIVATE, expected); result = fc_futex(lock->cond, FUTEX_WAIT_PRIVATE, expected);
lock_ret = fc_futex(&lock->mutex, FUTEX_LOCK_PI_PRIVATE, 0); lock_ret = pthread_spin_lock(&lock->mutex);
return result == 0 ? lock_ret : result; return result == 0 ? lock_ret : result;
#else #else
return pthread_cond_wait(&lock->lcp.cond, &lock->lcp.lock); return pthread_cond_wait(&lock->lcp.cond, &lock->lcp.lock);

View File

@ -21,7 +21,7 @@
typedef struct fc_spinlock_t { typedef struct fc_spinlock_t {
#ifdef OS_LINUX #ifdef OS_LINUX
int mutex; pthread_spinlock_t mutex;
int *cond; int *cond;
#else #else
pthread_lock_cond_pair_t lcp; pthread_lock_cond_pair_t lcp;

View File

@ -11,7 +11,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo
test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \ test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \
test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \ test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \
test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \ test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \
test_thread_local test_memcpy test_thread_local test_memcpy mblock_benchmark
all: $(ALL_PRGS) all: $(ALL_PRGS)

View File

@ -0,0 +1,125 @@
/*
* Copyright (c) 2025 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "fastcommon/logger.h"
#include "fastcommon/shared_func.h"
#include "fastcommon/pthread_func.h"
#include "fastcommon/fast_mblock.h"
static int thread_count = 4;
static int64_t loop_count = 10000000;
static struct fast_mblock_man mblock;
static void *thread_run(void *args)
{
int thread_index;
int i;
void *obj;
thread_index = (long)args;
printf("thread #%d start\n", thread_index);
for (i=0; i<loop_count; i++) {
obj = fast_mblock_alloc_object(&mblock);
fast_mblock_free_object(&mblock, obj);
}
printf("thread #%d done\n", thread_index);
return NULL;
}
int main(int argc, char *argv[])
{
const int stack_size = 64 * 1024;
int result;
int limit;
int i;
bool continue_flag = true;
int64_t qps;
pthread_t *tids;
pthread_attr_t thread_attr;
int64_t start_time;
int64_t end_time;
int64_t time_used;
log_init();
g_log_context.log_level = LOG_DEBUG;
limit = (thread_count + 1) / 2;
fast_mblock_manager_init();
if ((result=fast_mblock_init_ex1(&mblock, "mblock", 1024,
limit, limit, NULL, NULL, true)) != 0)
{
return result;
}
fast_mblock_set_need_wait(&mblock, true, &continue_flag);
if ((result=pthread_attr_init(&thread_attr)) != 0) {
logError("file: "__FILE__", line: %d, "
"call pthread_attr_init fail, "
"errno: %d, error info: %s",
__LINE__, result, STRERROR(result));
return result;
}
if ((result=pthread_attr_setstacksize(&thread_attr,
stack_size)) != 0)
{
logError("file: "__FILE__", line: %d, "
"call pthread_attr_setstacksize to %d fail, "
"errno: %d, error info: %s", __LINE__,
stack_size, result, STRERROR(result));
return result;
}
tids = fc_malloc(sizeof(pthread_t) * thread_count);
start_time = get_current_time_us();
for (i=0; i<thread_count; i++) {
if ((result=pthread_create(tids + i, &thread_attr,
thread_run, (void *)((long)i))) != 0)
{
logError("file: "__FILE__", line: %d, "
"create thread failed, "
"errno: %d, error info: %s",
__LINE__, result, STRERROR(result));
return result;
}
}
for (i=0; i<thread_count; i++) {
pthread_join(tids[i], NULL);
}
end_time = get_current_time_us();
time_used = end_time - start_time;
qps = (thread_count * loop_count * 1000 * 1000) / time_used;
printf("time used: %"PRId64" ms, QPS: %"PRId64"\n", time_used / 1000, qps);
free(tids);
fast_mblock_manager_stat_print(false);
fast_mblock_destroy(&mblock);
return 0;
}