File: ring.c

package info (click to toggle)
xfsdump 3.2.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: ansic: 45,797; sh: 3,449; makefile: 512
file content (493 lines) | stat: -rw-r--r-- 11,097 bytes parent folder | download | duplicates (2)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that it would 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 the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <assert.h>

#include "config.h"

#include "types.h"
#include "qlock.h"
#include "cldmgr.h"
#include "ring.h"

static int ring_worker_entry(void *ringctxp);

ring_t *
ring_create(size_t ringlen,
	     size_t bufsz,
	     bool_t pinpr,
	     ix_t drive_index,
	     int (*readfunc)(void *clientctxp, char *bufp),
	     int (*writefunc)(void *clientctxp, char *bufp),
	     void *clientctxp,
	     int *rvalp)
{
	bool_t ok;
	ring_t *ringp;
	size_t mix;

	/* pre-initialize return value
	 */
	*rvalp = 0;

	/* allocate a ring descriptor
	 */
	ringp = (ring_t *)calloc(1, sizeof(ring_t));
	assert(ringp);
	ringp->r_len = ringlen;
	ringp->r_clientctxp = clientctxp;
	ringp->r_readfunc = readfunc;
	ringp->r_writefunc = writefunc;

	/* allocate counting semaphores for the ready and active queues,
	 * and initialize the queue input and output indices.
	 */
	ringp->r_ready_qsemh = qsem_alloc(ringlen);
	ringp->r_active_qsemh = qsem_alloc(0);
	ringp->r_ready_in_ix = 0;
	ringp->r_ready_out_ix = 0;
	ringp->r_active_in_ix = 0;
	ringp->r_active_out_ix = 0;
	ringp->r_client_cnt = 0;
	ringp->r_worker_cnt = 0;

	/* initialize the meters
	 */
	ringp->r_client_msgcnt = 0;
	ringp->r_worker_msgcnt = 0;
	ringp->r_client_blkcnt = 0;
	ringp->r_worker_blkcnt = 0;
	ringp->r_first_io_time = 0;
	ringp->r_all_io_cnt = 0;

	/* allocate the ring messages
	 */
	ringp->r_msgp = (ring_msg_t *)calloc(ringlen, sizeof(ring_msg_t));
	assert(ringp->r_msgp);

	/* allocate the buffers and initialize the messages
	 */
	for (mix = 0; mix < ringlen; mix++) {
		ring_msg_t *msgp = &ringp->r_msgp[mix];
		msgp->rm_mix = mix;
		msgp->rm_op = RING_OP_NONE;
		msgp->rm_stat = RING_STAT_INIT;
		msgp->rm_user = 0;
		msgp->rm_loc = RING_LOC_READY;

		msgp->rm_bufp = (char *)memalign(PGSZ, bufsz);
		if (!msgp->rm_bufp) {
			*rvalp = ENOMEM;
			return 0;
		}
		if (pinpr) {
			int rval;
			rval = mlock((void *)msgp->rm_bufp, bufsz);
			if (rval) {
				if (errno == ENOMEM) {
					*rvalp = E2BIG;
					return 0;
				}
				if (errno == EPERM) {
					*rvalp = EPERM;
					return 0;
				}
				assert(0);
			}
		}
	}

	/* kick off the worker thread
	 */
	ok = cldmgr_create(ring_worker_entry,
			    drive_index,
			    _("worker"),
			    ringp);
	assert(ok);

	return ringp;
}

ring_msg_t *
ring_get(ring_t *ringp)
{
	ring_msg_t *msgp;

	/* assert client currently holds no messages
	 */
	assert(ringp->r_client_cnt == 0);

	/* bump client message count and note if client needs to block
	 */
	ringp->r_client_msgcnt++;
	if (qsemPwouldblock(ringp->r_ready_qsemh)) {
		ringp->r_client_blkcnt++;
	}

	/* block until msg available on ready queue ("P")
	 */
	qsemP(ringp->r_ready_qsemh);

	/* get a pointer to the next msg on the queue
	 */
	msgp = &ringp->r_msgp[ringp->r_ready_out_ix];

	/* assert the message is where it belongs
	 */
	assert(msgp->rm_loc == RING_LOC_READY);

	/* verify the message index has not become corrupted
	 */
	assert(msgp->rm_mix == ringp->r_ready_out_ix);

	/* bump the output index
	 */
	ringp->r_ready_out_ix = (ringp->r_ready_out_ix + 1)
				%
				ringp->r_len;

	/* update the message location
	 */
	msgp->rm_loc = RING_LOC_CLIENT;

	/* bump the count of messages held by the client
	 */
	ringp->r_client_cnt++;

	/* return the msg to the client
	 */
	return msgp;
}

void
ring_put(ring_t *ringp, ring_msg_t *msgp)
{
	/* assert the client holds exactly one message
	 */
	assert(ringp->r_client_cnt == 1);

	/* assert the client is returning the right message
	 */
	assert(msgp->rm_mix == ringp->r_active_in_ix);

	/* assert the message is where it belongs
	 */
	assert(msgp->rm_loc == RING_LOC_CLIENT);

	/* decrement the count of messages held by the client
	 */
	ringp->r_client_cnt--;

	/* update the message location
	 */
	msgp->rm_loc = RING_LOC_ACTIVE;

	/* bump the active queue input ix
	 */
	ringp->r_active_in_ix = (ringp->r_active_in_ix + 1)
				%
				ringp->r_len;

	/* bump the semaphore for the active queue ("V")
	 */
	qsemV(ringp->r_active_qsemh);
}

void
ring_reset(ring_t *ringp, ring_msg_t *msgp)
{
	size_t mix;

	/* if the client is not holding a message, get the next message
	 */
	if (ringp->r_client_cnt == 0) {
		assert(!msgp);
		msgp = ring_get(ringp);
		assert(msgp);
		assert(ringp->r_client_cnt == 1);
	} else {
		assert(msgp);
		assert(ringp->r_client_cnt == 1);
	}

	/* tell the worker to abort
	 */
	msgp->rm_op = RING_OP_RESET;
	ring_put(ringp, msgp);

	/* wait for the reset to be acknowledged
	 */
	assert(ringp->r_client_cnt == 0);
	do {
		/* pull a message from the ready queue
		 */
		qsemP(ringp->r_ready_qsemh);
		msgp = &ringp->r_msgp[ringp->r_ready_out_ix];
		assert(msgp->rm_loc == RING_LOC_READY);
		ringp->r_ready_out_ix = (ringp->r_ready_out_ix + 1)
					%
					ringp->r_len;
		ringp->r_client_cnt++;
	} while (msgp->rm_stat != RING_STAT_RESETACK);
	assert(ringp->r_client_cnt == ringp->r_len);

	/* re-initialize the ring
	 */
	assert(qsemPavail(ringp->r_ready_qsemh) == 0);
	assert(qsemPavail(ringp->r_active_qsemh) == 0);
	ringp->r_ready_in_ix = 0;
	ringp->r_ready_out_ix = 0;
	ringp->r_active_in_ix = 0;
	ringp->r_active_out_ix = 0;
	ringp->r_client_cnt = 0;
	ringp->r_worker_cnt = 0;
	for (mix = 0; mix < ringp->r_len; mix++) {
		ring_msg_t *msgp = &ringp->r_msgp[mix];
		msgp->rm_mix = mix;
		msgp->rm_op = RING_OP_NONE;
		msgp->rm_stat = RING_STAT_INIT;
		msgp->rm_user = 0;
		msgp->rm_loc = RING_LOC_READY;
		qsemV(ringp->r_ready_qsemh);
	}
	assert(qsemPavail(ringp->r_ready_qsemh) == ringp->r_len);
	assert(qsemPavail(ringp->r_active_qsemh) == 0);
}

void
ring_destroy(ring_t *ringp)
{
	ring_msg_t *msgp;

	/* the client must not be holding a message
	 */
	assert(ringp->r_client_cnt == 0);

	/* get a message
	 */
	msgp = ring_get(ringp);

	/* tell the worker to exit
	 */
	msgp->rm_op = RING_OP_DIE;
	ring_put(ringp, msgp);

	/* wait for the die to be acknowledged
	 */
	do {
		/* pull a message from the ready queue
		 */
		qsemP(ringp->r_ready_qsemh);
		msgp = &ringp->r_msgp[ringp->r_ready_out_ix];
		assert(msgp->rm_loc == RING_LOC_READY);
		ringp->r_ready_out_ix = (ringp->r_ready_out_ix + 1)
					%
					ringp->r_len;
	} while (msgp->rm_stat != RING_STAT_DIEACK);

	/* the worker is dead.
	 */
	qsem_free(ringp->r_ready_qsemh);
	qsem_free(ringp->r_active_qsemh);
	free((void *)ringp);
}


static ring_msg_t *
ring_worker_get(ring_t *ringp)
{
	ring_msg_t *msgp;

	/* assert worker currently holds no messages
	 */
	assert(ringp->r_worker_cnt == 0);

	/* bump worker message count and note if worker needs to block
	 */
	ringp->r_worker_msgcnt++;
	if (qsemPwouldblock(ringp->r_active_qsemh)) {
		ringp->r_worker_blkcnt++;
	}

	/* block until msg available on active queue ("P")
	 */
	qsemP(ringp->r_active_qsemh);

	/* get a pointer to the next msg on the queue
	 */
	msgp = &ringp->r_msgp[ringp->r_active_out_ix];

	/* assert the message is where it belongs
	 */
	assert(msgp->rm_loc == RING_LOC_ACTIVE);

	/* verify the message index has not become corrupted
	 */
	assert(msgp->rm_mix == ringp->r_active_out_ix);

	/* bump the output index
	 */
	ringp->r_active_out_ix = (ringp->r_active_out_ix + 1)
				 %
				 ringp->r_len;

	/* update the message location
	 */
	msgp->rm_loc = RING_LOC_SLAVE;

	/* bump the count of messages held by the worker
	 */
	ringp->r_worker_cnt++;

	/* return the msg to the worker
	 */
	return msgp;
}

static void
ring_worker_put(ring_t *ringp, ring_msg_t *msgp)
{
	/* assert the worker holds exactly one message
	 */
	assert(ringp->r_worker_cnt == 1);

	/* assert the worker is returning the right message
	 */
	assert(msgp->rm_mix == ringp->r_ready_in_ix);

	/* assert the message is where it belongs
	 */
	assert(msgp->rm_loc == RING_LOC_SLAVE);

	/* decrement the count of messages held by the worker
	 */
	ringp->r_worker_cnt--;

	/* update the message location
	 */
	msgp->rm_loc = RING_LOC_READY;

	/* bump the ready queue input ix
	 */
	ringp->r_ready_in_ix = (ringp->r_ready_in_ix + 1)
			       %
			       ringp->r_len;

	/* bump the semaphore for the ready queue ("V")
	 */
	qsemV(ringp->r_ready_qsemh);
}

static int
ring_worker_entry(void *ringctxp)
{
	sigset_t blocked_set;
	ring_t *ringp = (ring_t *)ringctxp;
	enum { LOOPMODE_NORMAL, LOOPMODE_IGNORE, LOOPMODE_DIE } loopmode;

	/* block signals, let the main thread handle them
	 */
	sigemptyset(&blocked_set);
	sigaddset(&blocked_set, SIGINT);
	sigaddset(&blocked_set, SIGHUP);
	sigaddset(&blocked_set, SIGTERM);
	sigaddset(&blocked_set, SIGQUIT);
	sigaddset(&blocked_set, SIGALRM);
	pthread_sigmask(SIG_SETMASK, &blocked_set, NULL);

	/* loop reading and precessing messages until told to die
	 */
	for (loopmode = LOOPMODE_NORMAL; loopmode != LOOPMODE_DIE;) {
		ring_msg_t *msgp;
		int rval;

		msgp = ring_worker_get(ringp);
		msgp->rm_rval = 0;

		switch(msgp->rm_op) {
		case RING_OP_READ:
			if (loopmode == LOOPMODE_IGNORE) {
				msgp->rm_stat = RING_STAT_IGNORE;
				break;
			}
			if (!ringp->r_first_io_time) {
				ringp->r_first_io_time = time(0);
				assert(ringp->r_first_io_time);
			}
			rval = (ringp->r_readfunc)(ringp->r_clientctxp,
						      msgp->rm_bufp);
			msgp->rm_rval = rval;
			ringp->r_all_io_cnt++;
			if (msgp->rm_rval == 0) {
				msgp->rm_stat = RING_STAT_OK;
			} else {
				msgp->rm_stat = RING_STAT_ERROR;
				loopmode = LOOPMODE_IGNORE;
			}
			break;
		case RING_OP_WRITE:
			if (loopmode == LOOPMODE_IGNORE) {
				msgp->rm_stat = RING_STAT_IGNORE;
				break;
			}
			if (!ringp->r_first_io_time) {
				ringp->r_first_io_time = time(0);
				assert(ringp->r_first_io_time);
			}
			rval = (ringp->r_writefunc)(ringp->r_clientctxp,
						       msgp->rm_bufp);
			msgp->rm_rval = rval;
			ringp->r_all_io_cnt++;
			if (msgp->rm_rval == 0) {
				msgp->rm_stat = RING_STAT_OK;
			} else {
				msgp->rm_stat = RING_STAT_ERROR;
				loopmode = LOOPMODE_IGNORE;
			}
			break;
		case RING_OP_NOP:
			msgp->rm_stat = RING_STAT_NOPACK;
			break;
		case RING_OP_TRACE:
			msgp->rm_stat = RING_STAT_IGNORE;
			break;
		case RING_OP_RESET:
			loopmode = LOOPMODE_NORMAL;
			msgp->rm_stat = RING_STAT_RESETACK;
			break;
		case RING_OP_DIE:
			msgp->rm_stat = RING_STAT_DIEACK;
			loopmode = LOOPMODE_DIE;
			break;
		default:
			msgp->rm_stat = RING_STAT_IGNORE;
			break;
		}
		ring_worker_put(ringp, msgp);
	}

	return 0;
}