-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfox-thread.c
280 lines (227 loc) · 7.44 KB
/
fox-thread.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* - FOX - A tool for testing Open-Channel SSDs
* - Thread geometry distribution
*
* Copyright (C) 2016, IT University of Copenhagen. All rights reserved.
* Written by Ivan Luiz Picoli <[email protected]>
*
* Funding support provided by CAPES Foundation, Ministry of Education
* of Brazil, Brasilia - DF 70040-020, Brazil.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <liblightnvm.h>
#include <string.h>
#include "fox.h"
static uint8_t *th_ch;
static uint8_t *nodes_ch; /* set in config lun, used to pick a
* node id within the channel */
void fox_wait_for_ready (struct fox_workload *wl)
{
pthread_mutex_lock(&wl->start_mut);
if (wl->stats->flags ^ FOX_FLAG_READY)
pthread_cond_wait(&wl->start_con, &wl->start_mut);
pthread_mutex_unlock(&wl->start_mut);
}
void fox_wait_for_monitor (struct fox_workload *wl)
{
pthread_mutex_lock(&wl->monitor_mut);
if (wl->stats->flags ^ FOX_FLAG_MONITOR)
pthread_cond_wait(&wl->monitor_con, &wl->monitor_mut);
pthread_mutex_unlock(&wl->monitor_mut);
}
static int fox_config_ch (struct fox_node *node)
{
int ch_th, mod_ch, i, add;
if (node->wl->channels > node->wl->geo->nchannels ||
node->wl->channels == 0) {
printf("thread: Invalid number of channels.\n");
return -1;
}
add = 0;
if (node->wl->channels >= node->wl->nthreads) {
ch_th = node->wl->channels / node->wl->nthreads;
mod_ch = node->wl->channels % node->wl->nthreads;
if (mod_ch && (node->nid >= (node->wl->nthreads - mod_ch)))
add++;
} else {
ch_th = 1;
}
node->ch = malloc(sizeof(uint8_t) * ch_th + add);
if (!node->ch)
return -1;
for (i = 0; i < ch_th; i++) {
if (node->nid > node->wl->channels - 1)
node->ch[i] = node->nid % node->wl->channels;
else
node->ch[i] = node->nid * ch_th + i;
th_ch[node->ch[i]]++;
}
if (add) {
node->ch[ch_th] = node->wl->channels - (node->wl->nthreads - node->nid);
th_ch[node->ch[ch_th]]++;
}
node->nchs = ch_th + add;
return 0;
}
static int fox_config_lun (struct fox_node *node)
{
int lun_th, mod_lun, i, add, n_th, nid;
if (node->wl->luns > node->wl->geo->nluns || node->wl->luns == 0) {
printf("thread: Invalid number of LUNs.\n");
return -1;
}
n_th = th_ch[node->ch[0]];
nid = nodes_ch[node->ch[0]];
add = 0;
if (node->wl->luns >= th_ch[node->ch[0]]) {
lun_th = node->wl->luns / n_th;
mod_lun = node->wl->luns % n_th;
if (mod_lun && (nid >= (n_th - mod_lun)))
add++;
} else {
lun_th = 1;
}
node->lun = malloc(sizeof(uint8_t) * lun_th + add);
if (!node->lun)
return -1;
for (i = 0; i < lun_th; i++) {
if (nid > node->wl->luns - 1)
node->lun[i] = nid % node->wl->luns;
else
node->lun[i] = nid * lun_th + i;
}
if (add)
node->lun[lun_th] = node->wl->luns - (n_th - nid);
node->nluns = lun_th + add;
for (i = 0; i < node->nchs; i++) {
nodes_ch[node->ch[i]]++;
}
return 0;
}
static void fox_show_geo_dist (struct fox_node *node)
{
int node_i, ch_i, lun_i;
printf("\n --- GEOMETRY DISTRIBUTION [TID: (CH LUN)] --- \n");
for (node_i = 0; node_i < node[0].wl->nthreads; node_i++) {
if (node_i % 4 == 0)
printf ("\n");
printf(" [%d:", node[node_i].nid);
for (ch_i = 0; ch_i < node[node_i].nchs; ch_i++) {
for (lun_i = 0; lun_i < node[node_i].nluns; lun_i++)
printf(" (%d %d)", node[node_i].ch[ch_i],
node[node_i].lun[lun_i]);
}
printf("] ");
}
printf ("\n");
}
static void *fox_thread_node (void * arg)
{
int ret;
struct fox_node *node = (struct fox_node *) arg;
ret = node->engine->start(node);
if (ret)
printf ("thread: Thread %d has failed.\n", node->nid);
return NULL;
}
struct fox_node *fox_create_threads (struct fox_workload *wl)
{
int li, ci, i, err = 0;
struct fox_node *node;
if (!wl)
goto ERR;
th_ch = calloc (sizeof(uint8_t) * wl->channels, 1);
if (!th_ch)
goto ERR;
nodes_ch = calloc (sizeof(uint8_t) * wl->channels, 1);
if (!nodes_ch)
goto FREE_TC;
node = malloc (sizeof(struct fox_node) * wl->nthreads);
if (!node) {
printf ("thread: Memory allocation failed.\n");
goto FREE_NC;
}
for (ci = 0; ci < wl->nthreads; ci++) {
node[ci].wl = wl;
node[ci].nid = ci;
node[ci].nblks = wl->blks;
node[ci].npgs = wl->pgs;
node[ci].delay = 0;
if (fox_init_stats (&node[ci].stats))
goto EXIT_CH;
if (fox_config_ch(&node[ci])) {
printf("thread: Failed to start. id: %d\n", ci);
fox_exit_stats (&node[ci].stats);
goto EXIT_CH;
}
}
for (li = 0; li < wl->nthreads; li++) {
if (fox_config_lun(&node[li])) {
printf("thread: Failed to start. id: %d\n", li);
goto EXIT_LUN;
}
}
fox_show_geo_dist (node);
for (i = 0; i < wl->nthreads; i++) {
node[i].engine = wl->engine;
if(pthread_create (&node[i].tid, NULL, fox_thread_node, &node[i]))
printf("thread: Failed to start. id: %d\n", i);
}
return node;
EXIT_LUN:
for (i = 0; i < li; i++)
free (node[i].lun);
err++;
EXIT_CH:
for (i = 0; i < ci; i++) {
fox_exit_stats (&node[i].stats);
free (node[i].ch);
}
free (node);
err++;
FREE_NC:
free (nodes_ch);
err++;
FREE_TC:
free (th_ch);
err++;
ERR:
printf("\nthread: Failure while starting threads: %d\n", err);
return NULL;
}
void fox_exit_threads (struct fox_node *nodes)
{
int i;
for (i = 0; i < nodes[0].wl->nthreads; i++) {
free (nodes[i].ch);
free (nodes[i].lun);
fox_exit_stats (&nodes[i].stats);
pthread_join(nodes[i].tid, NULL);
}
free (nodes);
free(th_ch);
free(nodes_ch);
}