-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsb_mutex.c
More file actions
179 lines (137 loc) · 4.14 KB
/
Copy pathsb_mutex.c
File metadata and controls
179 lines (137 loc) · 4.14 KB
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
/* Copyright (C) 2004 MySQL AB
Copyright (C) 2004-2018 Alexey Kopytov <akopytov@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
#include "sysbench.h"
#include "sb_ck_pr.h"
#include "sb_rand.h"
typedef struct
{
pthread_mutex_t mutex;
char pad[256];
} thread_lock;
/* Mutex test arguments */
static sb_arg_t mutex_args[] =
{
SB_OPT("mutex-num", "total size of mutex array", "4096", INT),
SB_OPT("mutex-locks", "number of mutex locks to do per thread", "50000", INT),
SB_OPT("mutex-loops", "number of empty loops to do outside mutex lock",
"10000", INT),
SB_OPT("mutex-cs-loops", "number of increment operations to do inside mutex lock",
"10000", INT),
SB_OPT_END
};
/* Mutex test operations */
static int mutex_init(void);
static void mutex_print_mode(void);
static sb_event_t mutex_next_event(int);
static int mutex_execute_event(sb_event_t *, int);
static int mutex_done(void);
static sb_test_t mutex_test =
{
.sname = "mutex",
.lname = "Mutex performance test",
.ops = {
.init = mutex_init,
.print_mode = mutex_print_mode,
.next_event = mutex_next_event,
.execute_event = mutex_execute_event,
.done = mutex_done
},
.args = mutex_args
};
static thread_lock *thread_locks;
static unsigned int mutex_num;
static unsigned int mutex_loops;
static unsigned int mutex_locks;
static unsigned int mutex_cs_loops;
static unsigned int global_var;
static TLS int tls_counter;
int register_test_mutex(sb_list_t *tests)
{
SB_LIST_ADD_TAIL(&mutex_test.listitem, tests);
return 0;
}
int mutex_init(void)
{
unsigned int i;
mutex_num = sb_get_value_int("mutex-num");
mutex_loops = sb_get_value_int("mutex-loops");
mutex_locks = sb_get_value_int("mutex-locks");
mutex_cs_loops = sb_get_value_int("mutex-cs-loops");
thread_locks = (thread_lock *)malloc(mutex_num * sizeof(thread_lock));
if (thread_locks == NULL)
{
log_text(LOG_FATAL, "Memory allocation failure!");
return 1;
}
for (i = 0; i < mutex_num; i++)
pthread_mutex_init(&thread_locks[i].mutex, NULL);
return 0;
}
int mutex_done(void)
{
unsigned int i;
for(i=0; i < mutex_num; i++)
pthread_mutex_destroy(&thread_locks[i].mutex);
free(thread_locks);
return 0;
}
sb_event_t mutex_next_event(int thread_id)
{
sb_event_t sb_req;
sb_mutex_request_t *mutex_req = &sb_req.u.mutex_request;
(void) thread_id; /* unused */
/* Perform only one request per thread */
if (tls_counter++ > 0)
sb_req.type = SB_REQ_TYPE_NULL;
else
{
sb_req.type = SB_REQ_TYPE_MUTEX;
mutex_req->nlocks = mutex_locks;
mutex_req->nloops = mutex_loops;
mutex_req->ncs_loops = mutex_cs_loops;
}
return sb_req;
}
int mutex_execute_event(sb_event_t *sb_req, int thread_id)
{
unsigned int i;
unsigned int j;
unsigned int current_lock;
sb_mutex_request_t *mutex_req = &sb_req->u.mutex_request;
(void) thread_id; /* unused */
do
{
current_lock = sb_rand_uniform(0, mutex_num - 1);
for (i = 0; i < mutex_req->nloops; i++)
ck_pr_barrier();
pthread_mutex_lock(&thread_locks[current_lock].mutex);
for (j = 0; j < mutex_req->ncs_loops; j++)
global_var += j;
pthread_mutex_unlock(&thread_locks[current_lock].mutex);
mutex_req->nlocks--;
}
while (mutex_req->nlocks > 0);
return 0;
}
void mutex_print_mode(void)
{
log_text(LOG_INFO, "Doing mutex performance test");
}