File: slow_shutdown.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (339 lines) | stat: -rw-r--r-- 7,216 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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mps.h"

// #include <Windows.h> // uncomment for 'Sleep' below.

#define assert(cond, msg)							\
	if (!(cond)) {									\
		printf("Assertion failed: %s\n", msg);		\
		exit(1);									\
	}

#define null NULL

const size_t wordSize = sizeof(void *);

mps_arena_t arena;
mps_chain_t chain;
mps_fmt_t format;
mps_pool_t pool;
mps_ap_t ap;
mps_thr_t thread;
mps_root_t threadRoot;

mps_pool_t loPool;
mps_ap_t loAp;

static void check(mps_res_t result, const char *msg) {
	if (result != MPS_RES_OK) {
		printf("MPS error: %s\n", msg);
		exit(1);
	}
}

static mps_gen_param_s generationParams[] = {
	{ 2*1024*1024, 0.85 },
	{ 4*1024*1024, 0.45 },
	{ 8*1024*1024, 0.10 },
};

enum ObjTag {
	object,
	fwd2,
	fwd,
	pad1,
	pad,
};

struct Object {
	size_t tag;

	size_t value;
	Object *next;
};

struct Fwd2 {
	size_t tag;
	mps_addr_t to;
};

struct Fwd {
	size_t tag;
	mps_addr_t to;
	size_t size;
};

struct Pad1 {
	size_t tag;
};

struct Pad {
	size_t tag;
	size_t size;
};

union MpsObj {
	size_t tag;
	Object obj;
	Fwd2 fwd2;
	Fwd fwd;
	Pad1 pad1;
	Pad pad;
};

static size_t mpsSize(mps_addr_t at) {
	MpsObj *o = (MpsObj *)at;
	switch (o->tag) {
	case object:
		return sizeof(Object);
	case fwd2:
		return sizeof(Fwd2);
	case fwd:
		return o->fwd.size;
	case pad1:
		return sizeof(Pad1);
	case pad:
		return o->pad.size;
	default:
		assert(false, "Invalid type");
	}
}

static mps_addr_t mpsSkip(mps_addr_t at) {
	return (char *)at + mpsSize(at);
}

static mps_res_t mpsScan(mps_ss_t ss, mps_addr_t base, mps_addr_t limit) {
	MPS_SCAN_BEGIN(ss) {
		for (mps_addr_t at = base; at < limit; at = mpsSkip(at)) {
			MpsObj *o = (MpsObj *)at;
			mps_res_t result;
			mps_addr_t tmp;

			switch (o->tag) {
			case object:
				tmp = o->obj.next;
				result = MPS_FIX12(ss, &tmp);
				if (result != MPS_RES_OK)
					return result;
				o->obj.next = (Object *)tmp;
				break;
			}
		}
	} MPS_SCAN_END(ss);
	return MPS_RES_OK;
}

static void mpsMakeFwd(mps_addr_t at, mps_addr_t to) {
	size_t size = mpsSize(at);

	MpsObj *o = (MpsObj *)at;
	if (size <= sizeof(Fwd2)) {
		assert(size == sizeof(Fwd2), "Too small object!");
		o->fwd2.tag = fwd2;
		o->fwd2.to = to;
	} else {
		o->fwd.tag = fwd;
		o->fwd.to = to;
		o->fwd.size = size;
	}
}

static mps_addr_t mpsIsFwd(mps_addr_t at) {
	MpsObj *o = (MpsObj *)at;
	switch (o->tag) {
	case fwd2:
		return o->fwd2.to;
	case fwd:
		return o->fwd.to;
	default:
		return null;
	}
}

static void mpsMakePad(mps_addr_t at, size_t size) {
	MpsObj *o = (MpsObj *)at;
	if (size <= sizeof(Pad1)) {
		assert(size == sizeof(Pad1), "Too small to pad!");
		o->pad1.tag = pad1;
	} else {
		o->pad.tag = pad;
		o->pad.size = size;
	}
}

Object *alloc(size_t value = 0, Object *next = null) {
	mps_addr_t memory;
	Object *r;
	do {
		check(mps_reserve(&memory, ap, sizeof(Object)), "Out of memory.");

		r = (Object *)memory;
		r->tag = object;
		r->value = value;
		r->next = next;
	} while (!mps_commit(ap, memory, sizeof(Object)));

	return r;
}

Object *allocLo(size_t value = 0, Object *next = null) {
	mps_addr_t memory;
	Object *r;
	do {
		check(mps_reserve(&memory, loAp, sizeof(Object)), "Out of memory.");

		r = (Object *)memory;
		r->tag = object;
		r->value = value;
		r->next = next;
	} while (!mps_commit(loAp, memory, sizeof(Object)));

	return r;
}

void collect() {
	mps_arena_collect(arena);
	mps_arena_release(arena);
}

// Allocate 'n' nodes in a linked list.
Object *createList(size_t n) {
	if (n == 0)
		return null;

	Object *first = alloc(0);
	Object *last = first;
	for (size_t i = 1; i < n; i++) {
		last->next = alloc(i);
		last = last->next;
	}

	return first;
}

// Verify a linked list.
void checkList(Object *l, size_t count) {
	for (size_t i = 0; i < count; i++) {
		if (!l) {
			printf("Verification failure. The list is too short (only %Iu elements).\n", i);
			return;
		}
		if (l->value != i)
			printf("Verification failure. Expected %Iu, got %Iu\n", i, l->value);
		l = l->next;
	}

	if (l)
		printf("Verification failure. The list is more than %Iu elements.\n", count);
}

// Allocate and verify quite a lot of lists to put pressure on the GC.
void listOps(size_t times) {
	size_t len = 10000;
	for (size_t i = 0; i < times; i++) {
		Object *l = createList(len);
		checkList(l, len);
	}
}

void smallAllocs() {
	const size_t count = 200;
	Object *objs[count];

	for (size_t i = 0; i < count; i++) {
		objs[i] = createList(i+2);
		// The line below causes very slow shutdown times.
		collect();
	}

	for (size_t i = 0; i < count; i++) {
		checkList(objs[i], i+2);
	}
}

void realMain() {
	// If doing something heavy before calling 'smallAllocs', the problem also disappears.
	// listOps(2000);

	smallAllocs();

	printf("Done with small allocations.\n");
	fflush(stdout);

	// This line causes an assertion from the MPS. Something like:
	// trace.c:382: MPS ASSERTION FAILED: trace->condemned > condemnedBefore
	// allocLo(0, 0);

	listOps(2000);

	printf("Committed memory: %Iu kB\n", mps_arena_committed(arena) / 1024);
	// Sleep(10000); // uncomment to see the total memory consumption clearly in Task Manager etc.
}

int main() {
	int cold;

	MPS_ARGS_BEGIN(args) {
		MPS_ARGS_ADD(args, MPS_KEY_ARENA_SIZE, 1024*1024);
		check(mps_arena_create_k(&arena, mps_arena_class_vm(), args), "Failed to create arena.");
	} MPS_ARGS_END(args);

	check(mps_chain_create(&chain, arena, 3, generationParams), "Failed to create a chain.");

	MPS_ARGS_BEGIN(args) {
		MPS_ARGS_ADD(args, MPS_KEY_FMT_ALIGN, wordSize);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_HEADER_SIZE, 0);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_SCAN, &mpsScan);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_SKIP, &mpsSkip);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_FWD, &mpsMakeFwd);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_ISFWD, &mpsIsFwd);
		MPS_ARGS_ADD(args, MPS_KEY_FMT_PAD, &mpsMakePad);
		check(mps_fmt_create_k(&format, arena, args), "Failed to create object format.");
	} MPS_ARGS_END(args);

	MPS_ARGS_BEGIN(args) {
		MPS_ARGS_ADD(args, MPS_KEY_CHAIN, chain);
		MPS_ARGS_ADD(args, MPS_KEY_FORMAT, format);
		check(mps_pool_create_k(&pool, arena, mps_class_amc(), args), "Failed to create AMC pool.");
	} MPS_ARGS_END(args);

	check(mps_ap_create_k(&ap, pool, mps_args_none), "Failed to create allocation point.");

	MPS_ARGS_BEGIN(args) {
		MPS_ARGS_ADD(args, MPS_KEY_CHAIN, chain);
		MPS_ARGS_ADD(args, MPS_KEY_GEN, 1);
		MPS_ARGS_ADD(args, MPS_KEY_FORMAT, format);
		check(mps_pool_create_k(&loPool, arena, mps_class_lo(), args), "Failed to create LO pool.");
	} MPS_ARGS_END(args);

	check(mps_ap_create_k(&loAp, loPool, mps_args_none), "Failed to creat LO allocation point.");

	check(mps_thread_reg(&thread, arena), "Failed to register thread");
	check(mps_root_create_thread(&threadRoot, arena, thread, &cold), "Failed to set up thread root.");

	printf("Initialization done.\n");
	fflush(stdout);

	realMain();

	printf("Shutting down...\n");
	fflush(stdout);

	mps_arena_collect(arena);
	mps_root_destroy(threadRoot);
	mps_thread_dereg(thread);
	mps_ap_destroy(ap);
	mps_pool_destroy(pool);
	mps_ap_destroy(loAp);
	mps_pool_destroy(loPool);
	mps_fmt_destroy(format);
	mps_chain_destroy(chain);
	mps_arena_destroy(arena);

	printf("Done!\n");

	return 0;
}