Compare commits
No commits in common. "f0484579e082dc75e22cdd9a553e527512acd683" and "a256976600d4b2789f898cbb24c09d9d90318f42" have entirely different histories.
f0484579e0
...
a256976600
|
|
@ -62,7 +62,6 @@ src/tests/test_sorted_array
|
|||
src/tests/test_sorted_queue
|
||||
src/tests/test_thread_local
|
||||
src/tests/test_memcpy
|
||||
src/tests/mblock_benchmark
|
||||
|
||||
# other
|
||||
*.swp
|
||||
|
|
|
|||
|
|
@ -39,8 +39,9 @@ static inline int fc_futex(int *ptr, int op, int val)
|
|||
int fc_spinlock_init(FCSpinlock *lock, int *cond)
|
||||
{
|
||||
#ifdef OS_LINUX
|
||||
lock->mutex = 0;
|
||||
lock->cond = cond;
|
||||
return pthread_spin_init(&lock->mutex, 0);
|
||||
return 0;
|
||||
#else
|
||||
return init_pthread_lock_cond_pair(&lock->lcp);
|
||||
#endif
|
||||
|
|
@ -57,7 +58,7 @@ void fc_spinlock_destroy(FCSpinlock *lock)
|
|||
int fc_spinlock_lock(FCSpinlock *lock)
|
||||
{
|
||||
#ifdef OS_LINUX
|
||||
return pthread_spin_lock(&lock->mutex);
|
||||
return fc_futex(&lock->mutex, FUTEX_LOCK_PI_PRIVATE, 0);
|
||||
#else
|
||||
return pthread_mutex_lock(&lock->lcp.lock);
|
||||
#endif
|
||||
|
|
@ -66,7 +67,7 @@ int fc_spinlock_lock(FCSpinlock *lock)
|
|||
int fc_spinlock_trylock(FCSpinlock *lock)
|
||||
{
|
||||
#ifdef OS_LINUX
|
||||
return pthread_spin_trylock(&lock->mutex);
|
||||
return fc_futex(&lock->mutex, FUTEX_TRYLOCK_PI_PRIVATE, 0);
|
||||
#else
|
||||
return pthread_mutex_trylock(&lock->lcp.lock);
|
||||
#endif
|
||||
|
|
@ -75,7 +76,7 @@ int fc_spinlock_trylock(FCSpinlock *lock)
|
|||
int fc_spinlock_unlock(FCSpinlock *lock)
|
||||
{
|
||||
#ifdef OS_LINUX
|
||||
return pthread_spin_unlock(&lock->mutex);
|
||||
return fc_futex(&lock->mutex, FUTEX_UNLOCK_PI_PRIVATE, 0);
|
||||
#else
|
||||
return pthread_mutex_unlock(&lock->lcp.lock);
|
||||
#endif
|
||||
|
|
@ -87,11 +88,11 @@ int fc_spinlock_wait(FCSpinlock *lock, const int expected)
|
|||
int result;
|
||||
int lock_ret;
|
||||
|
||||
if ((result=pthread_spin_unlock(&lock->mutex)) != 0) {
|
||||
if ((result=fc_futex(&lock->mutex, FUTEX_UNLOCK_PI_PRIVATE, 0)) != 0) {
|
||||
return result;
|
||||
}
|
||||
result = fc_futex(lock->cond, FUTEX_WAIT_PRIVATE, expected);
|
||||
lock_ret = pthread_spin_lock(&lock->mutex);
|
||||
lock_ret = fc_futex(&lock->mutex, FUTEX_LOCK_PI_PRIVATE, 0);
|
||||
return result == 0 ? lock_ret : result;
|
||||
#else
|
||||
return pthread_cond_wait(&lock->lcp.cond, &lock->lcp.lock);
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
typedef struct fc_spinlock_t {
|
||||
#ifdef OS_LINUX
|
||||
pthread_spinlock_t mutex;
|
||||
int mutex;
|
||||
int *cond;
|
||||
#else
|
||||
pthread_lock_cond_pair_t lcp;
|
||||
|
|
|
|||
|
|
@ -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_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_thread_local test_memcpy mblock_benchmark
|
||||
test_thread_local test_memcpy
|
||||
|
||||
all: $(ALL_PRGS)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,125 +0,0 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
Loading…
Reference in New Issue