File: sb_threads.c

package info (click to toggle)
sysbench 0.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,204 kB
  • ctags: 1,143
  • sloc: ansic: 9,139; sh: 8,933; xml: 1,610; makefile: 114
file content (195 lines) | stat: -rw-r--r-- 4,279 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef _WIN32
# include "sb_win.h"
#endif

#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif

#include "sysbench.h"

/* How to test scheduler pthread_yield or sched_yield */
#ifdef HAVE_PTHREAD_YIELD
#define YIELD pthread_yield 
#elif defined (_WIN32)
#define YIELD SwitchToThread
#else
#define YIELD sched_yield
#endif

/* Threads test arguments */
static sb_arg_t threads_args[] =
{
  {
    "thread-yields", "number of yields to do per request", SB_ARG_TYPE_INT,
  "1000"
  },
  {"thread-locks", "number of locks per thread", SB_ARG_TYPE_INT, "8"},
  {NULL, NULL, SB_ARG_TYPE_NULL, NULL}
}; 

/* Threads test operations */
static int threads_init(void);
static int threads_prepare(void);
static void threads_print_mode(void);
static sb_request_t threads_get_request(int tid);
static int threads_execute_request(sb_request_t *, int);
static int threads_cleanup(void);

static sb_test_t threads_test =
{
  "threads",
  "Threads subsystem performance test",
  {
    threads_init,
    threads_prepare,
    NULL,
    threads_print_mode,
    threads_get_request,
    threads_execute_request,
    NULL,
    NULL,
    threads_cleanup,
    NULL
  },
  {
    NULL,
    NULL,
    NULL,
    NULL
  },
  threads_args,
  {NULL, NULL}
};

static unsigned int thread_yields;
static unsigned int thread_locks;
static pthread_mutex_t *test_mutexes;
static unsigned int req_performed;


int register_test_threads(sb_list_t *tests)
{
  SB_LIST_ADD_TAIL(&threads_test.listitem, tests);

  return 0;
}


int threads_init(void)
{
  thread_yields = sb_get_value_int("thread-yields");
  thread_locks = sb_get_value_int("thread-locks");
  req_performed = 0;
  
  return 0;
}


int threads_prepare(void)
{
  unsigned int i;

  test_mutexes = (pthread_mutex_t *)malloc(thread_locks *
                                           sizeof(pthread_mutex_t));
  if (test_mutexes == NULL)
  {
    log_text(LOG_FATAL, "Memory allocation failure!");
    return 1;
  }
  
  for(i = 0; i < thread_locks; i++)
    pthread_mutex_init(test_mutexes + i, NULL);

  return 0;
}


int threads_cleanup(void)
{
  unsigned int i;

  for(i=0; i < thread_locks; i++)
    pthread_mutex_destroy(test_mutexes + i);
  free(test_mutexes);
  
  return 0;
}


sb_request_t threads_get_request(int tid)
{
  sb_request_t         sb_req;
  sb_threads_request_t *threads_req = &sb_req.u.threads_request;

  (void)tid; /* unused */
  
  SB_THREAD_MUTEX_LOCK();
  if (req_performed >= sb_globals.max_requests)
  {
    sb_req.type = SB_REQ_TYPE_NULL;
    SB_THREAD_MUTEX_UNLOCK();
    return sb_req;
  }
  
  sb_req.type = SB_REQ_TYPE_THREADS;
  threads_req->lock_num = req_performed % thread_locks; 
  req_performed++;
  SB_THREAD_MUTEX_UNLOCK();

  return sb_req;
}


int threads_execute_request(sb_request_t *sb_req, int thread_id)
{
  unsigned int         i;
  sb_threads_request_t *threads_req = &sb_req->u.threads_request;
  log_msg_t           msg;
  log_msg_oper_t      op_msg;
  
  /* Prepare log message */
  msg.type = LOG_MSG_TYPE_OPER;
  msg.data = &op_msg;

  LOG_EVENT_START(msg, thread_id);
  for(i = 0; i < thread_yields; i++)
  {
    pthread_mutex_lock(&test_mutexes[threads_req->lock_num]);
    YIELD();
    pthread_mutex_unlock(&test_mutexes[threads_req->lock_num]);
  }
  LOG_EVENT_STOP(msg, thread_id);

  return 0;
}


void threads_print_mode(void)
{
  log_text(LOG_INFO, "Doing thread subsystem performance test");
  log_text(LOG_INFO, "Thread yields per test: %d Locks used: %d",
         thread_yields, thread_locks);
}