File: swim_test_ev.c

package info (click to toggle)
tarantool 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 85,364 kB
  • sloc: ansic: 513,760; cpp: 69,489; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,173; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (357 lines) | stat: -rw-r--r-- 9,608 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
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
/*
 * Copyright 2010-2019, Tarantool AUTHORS, please see AUTHORS file.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. 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 <COPYRIGHT HOLDER> ``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
 * <COPYRIGHT HOLDER> 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 "swim_test_ev.h"
#include "swim_test_transport.h"
#include "trivia/util.h"
#include "swim/swim_ev.h"
#include "tarantool_ev.h"
#define HEAP_FORWARD_DECLARATION
#include "salad/heap.h"
#include "assoc.h"
#include "say.h"
#include <stdbool.h>

/** Global watch, propagated by new events. */
static double watch = 0;

/**
 * Increasing event identifiers are used to preserve order of
 * events with the same deadline.
 */
static int event_id = 0;

/**
 * SWIM testing event loop has two event types - natural libev
 * events like timer, and artificial like fake socket blocking.
 */
enum swim_event_type {
	SWIM_EVENT_TIMER,
	SWIM_EVENT_BRK,
};

struct swim_event;

typedef void (*swim_event_process_f)(struct swim_event *, struct ev_loop *);
typedef void (*swim_event_delete_f)(struct swim_event *);

/**
 * An isolated event loop not visible to the fiber scheduler,
 * where it is safe to use fake file descriptors, manually invoke
 * callbacks etc.
 */
static struct ev_loop *test_loop;

struct ev_loop *
swim_loop(void)
{
	return test_loop;
}

/**
 * Base event. It is stored in the event heap and virtualizes
 * other events.
 */
struct swim_event {
	/** Type, for assertions only. */
	enum swim_event_type type;
	/**
	 * When that event should be invoked according to the fake
	 * watch.
	 */
	double deadline;
	/** A link in the event heap. */
	struct heap_node in_event_heap;
	/** ID to sort events with the same deadline. */
	int id;
	/**
	 * Process the event. Usually the event is deleted right
	 * after that.
	 */
	swim_event_process_f process;
	/** Just delete the event. Called on event heap reset. */
	swim_event_delete_f delete;
};

/**
 * Heap comparator. Heap's top stores an event with the nearest
 * deadline and the smallest ID in that deadline.
 */
static inline bool
swim_event_less(const struct swim_event *e1, const struct swim_event *e2)
{
	if (e1->deadline == e2->deadline)
		return e1->id < e2->id;
	return e1->deadline < e2->deadline;
}

#define HEAP_NAME event_heap
#define HEAP_LESS(h, e1, e2) swim_event_less(e1, e2)
#define heap_value_t struct swim_event
#define heap_value_attr in_event_heap
#include "salad/heap.h"

/** Event heap. Event loop pops them from here. */
static heap_t event_heap;

/** Libev watcher is matched to exactly one event here. */
static struct mh_i64ptr_t *events_hash;

/**
 * Create a new event which should call @a process after @a delay
 * fake seconds. @A delete is called explicitly when the event
 * is deleted by SWIM explicitly, and when the event heap is
 * reset.
 */
static void
swim_event_create(struct swim_event *e, enum swim_event_type type, double delay,
		  swim_event_process_f process, swim_event_delete_f delete)
{
	e->deadline = swim_time() + delay;
	e->id = event_id++;
	e->process = process;
	e->delete = delete;
	e->type = type;
	event_heap_insert(&event_heap, e);
}

/** Destroy a basic event. */
static inline void
swim_event_destroy(struct swim_event *e)
{
	event_heap_delete(&event_heap, e);
}

/** Destroy a event and free its resources. */
static inline void
swim_event_delete(struct swim_event *e)
{
	e->delete(e);
}

/** Find an event by @a watcher. */
static struct swim_event *
swim_event_by_ev(struct ev_watcher *watcher)
{
	mh_int_t rc = mh_i64ptr_find(events_hash, (uint64_t) watcher, NULL);
	if (rc == mh_end(events_hash))
		return NULL;
	return (struct swim_event *) mh_i64ptr_node(events_hash, rc)->val;
}

/** Timer event generated by libev. */
struct swim_timer_event {
	struct swim_event base;
	/**
	 * Libev watcher. Used to store callback and to find the
	 * event by watcher pointer. It is necessary because SWIM
	 * operates by libev watchers.
	 */
	struct ev_watcher *watcher;
};

/** Destroy a timer event and free its resources. */
static void
swim_timer_event_delete(struct swim_event *e)
{
	assert(e->type == SWIM_EVENT_TIMER);
	struct swim_timer_event *te = (struct swim_timer_event *) e;
	mh_int_t rc = mh_i64ptr_find(events_hash, (uint64_t) te->watcher, NULL);
	assert(rc != mh_end(events_hash));
	mh_i64ptr_del(events_hash, rc, NULL);
	swim_event_destroy(e);
	free(te);
}

/** Create a new timer event. */
static void
swim_timer_event_new(struct ev_watcher *watcher, double delay);

/** Process a timer event and delete it. */
static void
swim_timer_event_process(struct swim_event *e, struct ev_loop *loop)
{
	assert(e->type == SWIM_EVENT_TIMER);
	struct ev_watcher *w = ((struct swim_timer_event *) e)->watcher;
	struct ev_timer *t = (struct ev_timer *) w;
	swim_timer_event_delete(e);
	t->at = 0;
	if (t->repeat > 0)
		swim_timer_event_new(w, t->repeat);
	ev_invoke(loop, w, EV_TIMER);
}

static void
swim_timer_event_new(struct ev_watcher *watcher, double delay)
{
	struct swim_timer_event *e =
		(struct swim_timer_event *) malloc(sizeof(*e));
	assert(e != NULL);
	swim_event_create(&e->base, SWIM_EVENT_TIMER, delay,
			  swim_timer_event_process, swim_timer_event_delete);
	e->watcher = watcher;
	assert(swim_event_by_ev(watcher) == NULL);
	struct mh_i64ptr_node_t node = {(uint64_t) watcher, e};
	mh_int_t rc = mh_i64ptr_put(events_hash, &node, NULL, NULL);
	(void) rc;
	assert(rc != mh_end(events_hash));
}

/**
 * Breakpoint event for debug. It does nothing but stops the event
 * loop after a timeout to allow highlevel API to check some
 * cases. The main feature is that a test can choose that timeout,
 * while natural SWIM events usually are out of control. That
 * events allows to check conditions between natural events.
 */
struct swim_brk_event {
	struct swim_event base;
};

/** Delete a breakpoint event. */
static void
swim_brk_event_delete(struct swim_event *e)
{
	assert(e->type == SWIM_EVENT_BRK);
	swim_event_destroy(e);
	free(e);
}

/**
 * Breakpoint event processing is nothing but the event deletion.
 */
static void
swim_brk_event_process(struct swim_event *e, struct ev_loop *loop)
{
	(void) loop;
	assert(e->type == SWIM_EVENT_BRK);
	swim_brk_event_delete(e);
}

void
swim_ev_set_brk(double delay)
{
	struct swim_brk_event *e = (struct swim_brk_event *) malloc(sizeof(*e));
	assert(e != NULL);
	swim_event_create(&e->base, SWIM_EVENT_BRK, delay,
			  swim_brk_event_process, swim_brk_event_delete);
}

/** Implementation of global time visible in SWIM. */
double
swim_time(void)
{
	return watch;
}

/**
 * Start of a timer generates a delayed event. If a timer is
 * already started - nothing happens.
 */
void
swim_ev_timer_start(struct ev_loop *loop, struct ev_timer *base)
{
	(void) loop;
	if (swim_event_by_ev((struct ev_watcher *) base) != NULL)
		return;
	/* Create the periodic watcher and one event. */
	swim_timer_event_new((struct ev_watcher *) base, base->at);
}

void
swim_ev_timer_again(struct ev_loop *loop, struct ev_timer *base)
{
	(void) loop;
	if (swim_event_by_ev((struct ev_watcher *) base) != NULL)
		return;
	/* Create the periodic watcher and one event. */
	swim_timer_event_new((struct ev_watcher *) base, base->repeat);
}

/** Time stop cancels the event if the timer is active. */
void
swim_ev_timer_stop(struct ev_loop *loop, struct ev_timer *base)
{
	(void) loop;
	/*
	 * Delete the watcher and its events. Should be only one.
	 */
	struct swim_event *e = swim_event_by_ev((struct ev_watcher *) base);
	if (e == NULL)
		return;
	swim_event_delete(e);
}

/** Process all the events with the next nearest deadline. */
void
swim_test_ev_do_loop_step(struct ev_loop *loop)
{
	struct swim_event *e = event_heap_top(&event_heap);
	if (e != NULL) {
		assert(e->deadline >= watch);
		/* Multiple events can have the same deadline. */
		watch = e->deadline;
		say_verbose("Loop watch %f", watch);
		do {
			e->process(e, loop);
			e = event_heap_top(&event_heap);
		} while (e != NULL && e->deadline == watch);
	}
}

void
swim_test_ev_reset(void)
{
	struct swim_event *e;
	while ((e = event_heap_top(&event_heap)) != NULL)
		swim_event_delete(e);
	assert(mh_size(events_hash) == 0);
	event_id = 0;
	watch = 0;
}

void
swim_test_ev_init(void)
{
	events_hash = mh_i64ptr_new();
	assert(events_hash != NULL);
	event_heap_create(&event_heap);
	test_loop = ev_loop_new(0);
	assert(test_loop != NULL);
}

void
swim_test_ev_free(void)
{
	swim_test_ev_reset();
	event_heap_destroy(&event_heap);
	mh_i64ptr_delete(events_hash);
	ev_loop_destroy(test_loop);
}