1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3 * Copyright (c) 2015 Rockchip Electronics Co., Ltd.
4 */
5
6 #define MODULE_TAG "mpp_thread_test"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "rk_type.h"
13
14 #include "mpp_debug.h"
15 #include "mpp_thread.h"
16 #include "mpp_time.h"
17
18 // TODO: add thread mutex and condition test case
19
20 #define MAX_THREAD_NUM 10
21 #define MAX_LOCK_LOOP 10000
22
23 static RK_S32 thread_debug = 0;
24 #define thread_dbg(fmt, ...) _mpp_dbg(thread_debug, 1, fmt, ## __VA_ARGS__)
25
26 static pthread_mutex_t mutex_0;
27 static pthread_mutex_t mutex_1;
28 static pthread_cond_t cond_0;
29 static pthread_cond_t cond_1;
30 static volatile RK_S32 flag_0 = 1;
31 static volatile RK_S32 flag_1 = 1;
32
thread_test(void * pdata)33 void *thread_test(void *pdata)
34 {
35 int idx = *((int*)pdata);
36 mpp_log("thread %d is running\n", idx);
37 sleep(1);
38 mpp_log("thread %d done\n", idx);
39 return NULL;
40 }
41
mutex_performance_test_loop_0(void * arg)42 void* mutex_performance_test_loop_0(void *arg)
43 {
44 RK_S32 i = 0;
45
46 for (i = 0; i < MAX_LOCK_LOOP; i++) {
47 thread_dbg("0 %5d lock\n", i);
48 pthread_mutex_lock(&mutex_0);
49
50 thread_dbg("0 %5d wait flag %d\n", i, flag_0);
51 if (flag_0) {
52 pthread_cond_wait(&cond_0, &mutex_0);
53 }
54
55 thread_dbg("0 %5d signal\n", i);
56 pthread_mutex_lock(&mutex_1);
57 flag_1 = 0;
58 pthread_cond_signal(&cond_1);
59 pthread_mutex_unlock(&mutex_1);
60
61 thread_dbg("0 %5d unlock\n", i);
62 flag_0 = 1;
63 pthread_mutex_unlock(&mutex_0);
64 }
65 (void)arg;
66 return NULL;
67 }
68
mutex_performance_test_loop_1(void * arg)69 void *mutex_performance_test_loop_1(void *arg)
70 {
71 RK_S32 i = 0;
72
73 for (i = 0; i < MAX_LOCK_LOOP; i++) {
74 thread_dbg("1 %5d lock\n", i);
75 pthread_mutex_lock(&mutex_1);
76
77 thread_dbg("1 %5d wait flag %d\n", i, flag_1);
78 if (flag_1) {
79 pthread_cond_wait(&cond_1, &mutex_1);
80 }
81
82 thread_dbg("1 %5d signal\n", i);
83 pthread_mutex_lock(&mutex_0);
84 flag_0 = 0;
85 pthread_cond_signal(&cond_0);
86 pthread_mutex_unlock(&mutex_0);
87
88 thread_dbg("1 %5d unlock\n", i);
89 flag_1 = 1;
90 pthread_mutex_unlock(&mutex_1);
91 }
92 (void)arg;
93 return NULL;
94 }
95
mutex_performance_test_once(void)96 void mutex_performance_test_once(void)
97 {
98 pthread_mutexattr_t attr;
99 pthread_mutex_t mutex;
100
101 pthread_mutexattr_init(&attr);
102 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
103
104 pthread_mutex_init(&mutex, &attr);
105 pthread_mutexattr_destroy(&attr);
106
107 pthread_mutex_lock(&mutex);
108 pthread_mutex_unlock(&mutex);
109
110 pthread_mutex_lock(&mutex);
111 pthread_mutex_unlock(&mutex);
112
113 pthread_mutex_lock(&mutex);
114 pthread_mutex_unlock(&mutex);
115
116 pthread_mutex_lock(&mutex);
117 pthread_mutex_unlock(&mutex);
118
119 pthread_mutex_destroy(&mutex);
120 }
121
main()122 int main()
123 {
124 int i;
125 int pdata[MAX_THREAD_NUM];
126 pthread_t threads[MAX_THREAD_NUM];
127 pthread_attr_t attr;
128 pthread_mutexattr_t mutex_attr;
129 void *dummy;
130 RK_S64 time_start, time_end;
131
132 mpp_log("vpu test start\n");
133 pthread_attr_init(&attr);
134 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
135
136 for (i = 0; i < MAX_THREAD_NUM; i++) {
137 pdata[i] = i;
138 pthread_create(&threads[i], &attr, thread_test, &pdata[i]);
139 }
140
141 sleep(2);
142
143 for (i = 0; i < MAX_THREAD_NUM; i++) {
144 pthread_join(threads[i], &dummy);
145 }
146
147 mpp_debug = MPP_DBG_TIMING;
148 time_start = mpp_time();
149
150 for (i = 0; i < MAX_LOCK_LOOP; i++)
151 mutex_performance_test_once();
152
153 time_end = mpp_time();
154 mpp_time_diff(time_start, time_end, 0, "lock unlock test");
155
156 pthread_mutexattr_init(&mutex_attr);
157 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
158
159 pthread_mutex_init(&mutex_0, &mutex_attr);
160 pthread_mutex_init(&mutex_1, &mutex_attr);
161 pthread_cond_init(&cond_0, NULL);
162 pthread_cond_init(&cond_1, NULL);
163 pthread_mutexattr_destroy(&mutex_attr);
164
165 time_start = mpp_time();
166 pthread_create(&threads[0], &attr, mutex_performance_test_loop_0, NULL);
167 flag_0 = 0;
168 pthread_create(&threads[1], &attr, mutex_performance_test_loop_1, NULL);
169
170 pthread_join(threads[0], &dummy);
171 pthread_join(threads[1], &dummy);
172 time_end = mpp_time();
173 mpp_time_diff(time_start, time_end, 0, "lock and signal test");
174
175 pthread_cond_destroy(&cond_0);
176 pthread_cond_destroy(&cond_1);
177
178 pthread_attr_destroy(&attr);
179
180 mpp_debug = 0;
181 mpp_log("vpu test end\n");
182 return 0;
183 }
184
185