1
/* Copyright (C) 2004 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; either version 2 of the License, or
6
(at your option) any later version.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
/* How to test scheduler pthread_yield or sched_yield */
33
#ifdef HAVE_PTHREAD_YIELD
34
#define YIELD pthread_yield
35
#elif defined (_WIN32)
36
#define YIELD SwitchToThread
38
#define YIELD sched_yield
41
/* Threads test arguments */
42
static sb_arg_t threads_args[] =
45
"thread-yields", "number of yields to do per request", SB_ARG_TYPE_INT,
48
{"thread-locks", "number of locks per thread", SB_ARG_TYPE_INT, "8"},
49
{NULL, NULL, SB_ARG_TYPE_NULL, NULL}
52
/* Threads test operations */
53
static int threads_init(void);
54
static int threads_prepare(void);
55
static void threads_print_mode(void);
56
static sb_request_t threads_get_request(void);
57
static int threads_execute_request(sb_request_t *, int);
58
static int threads_cleanup(void);
60
static sb_test_t threads_test =
63
"Threads subsystem performance test",
70
threads_execute_request,
86
static unsigned int thread_yields;
87
static unsigned int thread_locks;
88
static pthread_mutex_t *test_mutexes;
89
static unsigned int req_performed;
92
int register_test_threads(sb_list_t *tests)
94
SB_LIST_ADD_TAIL(&threads_test.listitem, tests);
100
int threads_init(void)
102
thread_yields = sb_get_value_int("thread-yields");
103
thread_locks = sb_get_value_int("thread-locks");
110
int threads_prepare(void)
114
test_mutexes = (pthread_mutex_t *)malloc(thread_locks *
115
sizeof(pthread_mutex_t));
116
if (test_mutexes == NULL)
118
log_text(LOG_FATAL, "Memory allocation failure!");
122
for(i = 0; i < thread_locks; i++)
123
pthread_mutex_init(test_mutexes + i, NULL);
129
int threads_cleanup(void)
133
for(i=0; i < thread_locks; i++)
134
pthread_mutex_destroy(test_mutexes + i);
141
sb_request_t threads_get_request(void)
144
sb_threads_request_t *threads_req = &sb_req.u.threads_request;
146
SB_THREAD_MUTEX_LOCK();
147
if (req_performed >= sb_globals.max_requests)
149
sb_req.type = SB_REQ_TYPE_NULL;
150
SB_THREAD_MUTEX_UNLOCK();
154
sb_req.type = SB_REQ_TYPE_THREADS;
155
threads_req->lock_num = req_performed % thread_locks;
157
SB_THREAD_MUTEX_UNLOCK();
163
int threads_execute_request(sb_request_t *sb_req, int thread_id)
166
sb_threads_request_t *threads_req = &sb_req->u.threads_request;
168
log_msg_oper_t op_msg;
170
/* Prepare log message */
171
msg.type = LOG_MSG_TYPE_OPER;
174
LOG_EVENT_START(msg, thread_id);
175
for(i = 0; i < thread_yields; i++)
177
pthread_mutex_lock(&test_mutexes[threads_req->lock_num]);
179
pthread_mutex_unlock(&test_mutexes[threads_req->lock_num]);
181
LOG_EVENT_STOP(msg, thread_id);
187
void threads_print_mode(void)
189
log_text(LOG_INFO, "Doing thread subsystem performance test");
190
log_text(LOG_INFO, "Thread yields per test: %d Locks used: %d",
191
thread_yields, thread_locks);