diff options
| author | lijia <[email protected]> | 2018-10-24 09:36:45 +0800 |
|---|---|---|
| committer | lijia <[email protected]> | 2018-10-24 09:36:45 +0800 |
| commit | 86a43b4d325ddc850fa9dc4711670880f35b11e8 (patch) | |
| tree | 8356a056ac9bfb8cf14fcf57f113dd306b4277d1 /test | |
create new project.
Diffstat (limited to 'test')
| -rw-r--r-- | test/mutex_vs_rdlock.c | 138 | ||||
| -rw-r--r-- | test/sport_usable.c | 45 |
2 files changed, 183 insertions, 0 deletions
diff --git a/test/mutex_vs_rdlock.c b/test/mutex_vs_rdlock.c new file mode 100644 index 0000000..e05ee65 --- /dev/null +++ b/test/mutex_vs_rdlock.c @@ -0,0 +1,138 @@ +#include <assert.h> +#include <stdio.h> +#include <unistd.h> +#include <pthread.h> +#include <string.h> +#include <assert.h> +#include <arpa/inet.h> +#include <netinet/tcp.h> +#include <netinet/udp.h> + + + +#define LOCK_TYPE (2) /* 1:mutex; 2:rwlock; 3:atomic */ + +#if LOCK_TYPE==1 +pthread_rwlock_t test_rwlock; +#elif LOCK_TYPE==2 +pthread_mutex_t test_mlock; +#elif LOCK_TYPE==3 +long atomic_lock = 0; +#endif + +unsigned long long g_val; + +unsigned long long g_producer_op_times = 0, g_customer_op_times; + + +static inline void test_lock(long lock_val) +{ +#if LOCK_TYPE==1 + pthread_rwlock_wrlock(&test_rwlock); +#elif LOCK_TYPE==2 + pthread_mutex_lock(&test_mlock); +#elif LOCK_TYPE==3 + while((__sync_or_and_fetch(&atomic_lock, lock_val) & 0xF) == lock_val){ + ; + } +#endif + +} + +static inline void test_rlock(long lock_val) +{ +#if LOCK_TYPE==1 + pthread_rwlock_rdlock(&test_rwlock); +#elif LOCK_TYPE==2 + pthread_mutex_lock(&test_mlock); +#elif LOCK_TYPE==3 + while((__sync_or_and_fetch(&atomic_lock, lock_val) & 0xF) == lock_val){ + ; + } +#endif + +} + +static inline void test_unlock(long lock_mask) +{ +#if LOCK_TYPE==1 + pthread_rwlock_unlock(&test_rwlock); +#elif LOCK_TYPE==2 + pthread_mutex_unlock(&test_mlock); +#elif LOCK_TYPE==3 + __sync_and_and_fetch(&atomic_lock, lock_mask); +#endif +} + +/* + ���bit: producer; + ��2bit : customer; +*/ + +static void *producer_thread(void *arg) +{ + int i; + + //for(i = 0; i < 9999999; i++){ + while(1){ + test_lock(0x01); + g_val++; + test_unlock(0xFE); + g_producer_op_times++; + } + + return NULL; +} + +static void *customer_thread(void *arg) +{ + unsigned long long tmp; + int i; + + //for(i = 0; i < 9999999; i++){ + while(1){ + test_rlock(0x02); + tmp = g_val; + test_unlock(0xFD); + g_customer_op_times++; + } + + return NULL; +} + +static void *stat_thread(void *arg) +{ + unsigned long long last_op_times = 0; + + while(1){ + printf("op PPS: %llu\n", g_customer_op_times + g_producer_op_times - last_op_times); + last_op_times = g_customer_op_times + g_producer_op_times; + sleep(1); + } + + return NULL; +} + +int main(void) +{ +#if LOCK_TYPE==1 + pthread_rwlock_init(&test_rwlock, NULL); +#elif LOCK_TYPE==2 + pthread_mutex_init(&test_mlock, NULL); +#endif + + pthread_t stat_pid, producer_pid, customer_pid; + + pthread_create(&stat_pid, NULL, stat_thread, NULL); + + pthread_create(&producer_pid, NULL, producer_thread, NULL); + pthread_create(&customer_pid, NULL, customer_thread, NULL); + + pthread_join(producer_pid, NULL); + pthread_join(customer_pid, NULL); + + printf("%llu\n", g_val); + + return 0; +} + diff --git a/test/sport_usable.c b/test/sport_usable.c new file mode 100644 index 0000000..1f9ec6b --- /dev/null +++ b/test/sport_usable.c @@ -0,0 +1,45 @@ +#include <stdio.h> + +#define TOT_ACC_GW_NUM (10) +#define THIS_ACC_GW_ID (5) +#define THREAD_NUM (8) + +/* for test */ +static int flwd_act_ip_get_usable_sport(int tid, unsigned short *begin_port, + unsigned short *usable_count) +{ + unsigned short this_gateway_begin_port; + unsigned short this_thread_usable_tot_count; + unsigned short this_gateway_usable_tot_count; + + this_gateway_usable_tot_count = + 64511 / TOT_ACC_GW_NUM; + + this_gateway_begin_port = + this_gateway_usable_tot_count * (THIS_ACC_GW_ID-1) + 1025; + + this_thread_usable_tot_count = + this_gateway_usable_tot_count / THREAD_NUM; + + *usable_count = + this_gateway_usable_tot_count / THREAD_NUM; + + *begin_port = this_gateway_begin_port + (*usable_count) * tid; + + return 0; +} + +int main(void) +{ + int i; + unsigned short begin_port; + unsigned short usable_count; + + for(i =0; i < THREAD_NUM; i++){ + flwd_act_ip_get_usable_sport(i, &begin_port, &usable_count); + printf("thread:%d, begin:%u, count:%u\n", i, begin_port, usable_count); + } + return 0; +} + + |
