~sysbench-developers/sysbench/missed-dirs-fix

« back to all changes in this revision

Viewing changes to sysbench/tests/threads/sb_threads.c

  • Committer: Alexey Kopytov
  • Date: 2009-05-21 13:55:35 UTC
  • mfrom: (0.1.1 sysbench-0.4)
  • Revision ID: alexey.kopytov@sun.com-20090521135535-h03ioutjyfzv1m30
Reconsiled with the old 0.4 code branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2004 MySQL AB
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
*/
 
17
 
 
18
#ifdef HAVE_CONFIG_H
 
19
# include "config.h"
 
20
#endif
 
21
 
 
22
#ifdef _WIN32
 
23
# include "sb_win.h"
 
24
#endif
 
25
 
 
26
#ifdef HAVE_PTHREAD_H
 
27
# include <pthread.h>
 
28
#endif
 
29
 
 
30
#include "sysbench.h"
 
31
 
 
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
 
37
#else
 
38
#define YIELD sched_yield
 
39
#endif
 
40
 
 
41
/* Threads test arguments */
 
42
static sb_arg_t threads_args[] =
 
43
{
 
44
  {
 
45
    "thread-yields", "number of yields to do per request", SB_ARG_TYPE_INT,
 
46
  "1000"
 
47
  },
 
48
  {"thread-locks", "number of locks per thread", SB_ARG_TYPE_INT, "8"},
 
49
  {NULL, NULL, SB_ARG_TYPE_NULL, NULL}
 
50
}; 
 
51
 
 
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);
 
59
 
 
60
static sb_test_t threads_test =
 
61
{
 
62
  "threads",
 
63
  "Threads subsystem performance test",
 
64
  {
 
65
    threads_init,
 
66
    threads_prepare,
 
67
    NULL,
 
68
    threads_print_mode,
 
69
    threads_get_request,
 
70
    threads_execute_request,
 
71
    NULL,
 
72
    NULL,
 
73
    NULL,
 
74
    threads_cleanup,
 
75
  },
 
76
  {
 
77
    NULL,
 
78
    NULL,
 
79
    NULL,
 
80
    NULL
 
81
  },
 
82
  threads_args,
 
83
  {NULL, NULL}
 
84
};
 
85
 
 
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;
 
90
 
 
91
 
 
92
int register_test_threads(sb_list_t *tests)
 
93
{
 
94
  SB_LIST_ADD_TAIL(&threads_test.listitem, tests);
 
95
 
 
96
  return 0;
 
97
}
 
98
 
 
99
 
 
100
int threads_init(void)
 
101
{
 
102
  thread_yields = sb_get_value_int("thread-yields");
 
103
  thread_locks = sb_get_value_int("thread-locks");
 
104
  req_performed = 0;
 
105
  
 
106
  return 0;
 
107
}
 
108
 
 
109
 
 
110
int threads_prepare(void)
 
111
{
 
112
  unsigned int i;
 
113
 
 
114
  test_mutexes = (pthread_mutex_t *)malloc(thread_locks *
 
115
                                           sizeof(pthread_mutex_t));
 
116
  if (test_mutexes == NULL)
 
117
  {
 
118
    log_text(LOG_FATAL, "Memory allocation failure!");
 
119
    return 1;
 
120
  }
 
121
  
 
122
  for(i = 0; i < thread_locks; i++)
 
123
    pthread_mutex_init(test_mutexes + i, NULL);
 
124
 
 
125
  return 0;
 
126
}
 
127
 
 
128
 
 
129
int threads_cleanup(void)
 
130
{
 
131
  unsigned int i;
 
132
 
 
133
  for(i=0; i < thread_locks; i++)
 
134
    pthread_mutex_destroy(test_mutexes + i);
 
135
  free(test_mutexes);
 
136
  
 
137
  return 0;
 
138
}
 
139
 
 
140
 
 
141
sb_request_t threads_get_request(void)
 
142
{
 
143
  sb_request_t         sb_req;
 
144
  sb_threads_request_t *threads_req = &sb_req.u.threads_request;
 
145
 
 
146
  SB_THREAD_MUTEX_LOCK();
 
147
  if (req_performed >= sb_globals.max_requests)
 
148
  {
 
149
    sb_req.type = SB_REQ_TYPE_NULL;
 
150
    SB_THREAD_MUTEX_UNLOCK();
 
151
    return sb_req;
 
152
  }
 
153
  
 
154
  sb_req.type = SB_REQ_TYPE_THREADS;
 
155
  threads_req->lock_num = req_performed % thread_locks; 
 
156
  req_performed++;
 
157
  SB_THREAD_MUTEX_UNLOCK();
 
158
 
 
159
  return sb_req;
 
160
}
 
161
 
 
162
 
 
163
int threads_execute_request(sb_request_t *sb_req, int thread_id)
 
164
{
 
165
  unsigned int         i;
 
166
  sb_threads_request_t *threads_req = &sb_req->u.threads_request;
 
167
  log_msg_t           msg;
 
168
  log_msg_oper_t      op_msg;
 
169
  
 
170
  /* Prepare log message */
 
171
  msg.type = LOG_MSG_TYPE_OPER;
 
172
  msg.data = &op_msg;
 
173
 
 
174
  LOG_EVENT_START(msg, thread_id);
 
175
  for(i = 0; i < thread_yields; i++)
 
176
  {
 
177
    pthread_mutex_lock(&test_mutexes[threads_req->lock_num]);
 
178
    YIELD();
 
179
    pthread_mutex_unlock(&test_mutexes[threads_req->lock_num]);
 
180
  }
 
181
  LOG_EVENT_STOP(msg, thread_id);
 
182
 
 
183
  return 0;
 
184
}
 
185
 
 
186
 
 
187
void threads_print_mode(void)
 
188
{
 
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);
 
192
}
 
193