-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathechallenge.c
258 lines (231 loc) · 5.08 KB
/
echallenge.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
/*
* echallenge.c -- Joe Armstrong's Erlang challenge (v2):
* Create a ring of N processes
* Send M simple messages around the ring
* Increase N until out of resources
*
* Copyright 2009 Dale Schumacher. ALL RIGHTS RESERVED.
*/
static char _Program[] = "EChallenge";
static char _Version[] = "2009-09-04";
static char _Copyright[] = "Copyright 2009 Dale Schumacher";
#include <getopt.h>
#include "abe.h"
#include "dbug.h"
DBUG_UNIT("echallenge");
static CONS* timing_actor;
BEH_DECL(timing_beh)
{
char* s;
DBUG_ENTER("timing_beh");
s = cons_to_str(NOW);
DBUG_PRINT("", ("[timestamp] %s", s));
fprintf(stderr, "[timestamp] %s\n", s);
DBUG_RETURN;
}
/**
countdown_ring_0_beh(first) = \m.[
IF $m = 0 [
BECOME \_.[]
] ELSE [
SEND dec(m) TO first
]
]
**/
BEH_DECL(countdown_ring_0_beh)
{
CONS* m = WHAT;
CONS* first = MINE;
int m_int = MK_INT(m);
DBUG_ENTER("countdown_ring_0_beh");
if (m_int <= 0) {
BECOME(sink_beh, NIL);
SEND(timing_actor, NIL);
} else {
SEND(first, NUMBER(m_int - 1));
}
DBUG_RETURN;
}
/**
countdown_ring_beh(next) = \m.[
SEND m TO next
]
**/
BEH_DECL(countdown_ring_beh)
{
CONS* m = WHAT;
CONS* next = MINE;
DBUG_ENTER("countdown_ring_beh");
SEND(next, m);
DBUG_RETURN;
}
/**
countdown_builder_beh(n) = \(first, m).[
IF $n = 0 [
BECOME countdown_ring_0_beh(first)
SEND m TO first # start message passing phase
] ELSE [
CREATE next WITH countdown_builder_beh(dec(n))
BECOME countdown_ring_beh(next)
SEND (first, m) TO next
]
]
**/
BEH_DECL(countdown_builder_beh)
{
CONS* n;
CONS* first;
CONS* m;
int n_int;
/*
CONS* n = MINE;
CONS* first = car(WHAT);
CONS* m = cdr(WHAT);
int n_int = MK_INT(n);
*/
DBUG_ENTER("countdown_builder_beh");
n = MINE;
assert(numberp(n));
assert(consp(WHAT));
first = car(WHAT);
assert(actorp(first));
m = cdr(WHAT);
assert(numberp(m));
n_int = MK_INT(n);
DBUG_PRINT("", ("n=%d m=%d", n_int, MK_INT(m)));
if (n_int <= 0) {
BECOME(countdown_ring_0_beh, first);
SEND(timing_actor, NIL);
SEND(first, m); /* NOTE: disable this line for process creation only */
} else {
CONS* next;
next = ACTOR(countdown_builder_beh, NUMBER(n_int - 1));
BECOME(countdown_ring_beh, next);
SEND(next, WHAT);
}
DBUG_RETURN;
}
/**
ring_link_beh(next) = \first.[
SEND first TO next
]
**/
BEH_DECL(ring_link_beh)
{
CONS* next = MINE;
CONS* first = WHAT;
DBUG_ENTER("countdown_ring_beh");
SEND(next, first);
DBUG_RETURN;
}
/**
ring_seed_beh(n) = \first.[
CREATE next WITH ring_seed_beh(inc(n))
BECOME ring_link_beh(next)
SEND first TO first
]
**/
BEH_DECL(ring_seed_beh)
{
static int thresh = 1 << 10;
CONS* n = MINE;
CONS* first = WHAT;
CONS* next;
int n_int = MK_INT(n);
DBUG_ENTER("ring_seed_beh");
if (n_int >= thresh) {
thresh <<= 1;
TRACE(fprintf(stderr, "%d:", n_int));
SEND(timing_actor, NIL);
}
next = ACTOR(ring_seed_beh, NUMBER(n_int + 1));
BECOME(ring_link_beh, next);
SEND(first, first);
DBUG_RETURN;
}
/**
IF $n = 0 [
CREATE ring WITH ring_seed_beh(0)
SEND ring TO ring
] ELSE [
CREATE countdown WITH countdown_builder_beh(n)
SEND (countdown, m) TO countdown
]
**/
void
erlang_challenge(int n, int m)
{
CONFIG* cfg;
int cnt;
DBUG_ENTER("erlang_challenge");
cfg = new_configuration(10);
timing_actor = CFG_ACTOR(cfg, timing_beh, NIL);
if (n <= 0) {
CONS* ring;
TRACE(printf("Seeding unbounded growth message ring.\n"));
ring = CFG_ACTOR(cfg, ring_seed_beh, NUMBER(0));
CFG_SEND(cfg, ring, ring);
} else {
CONS* countdown;
TRACE(printf("Sending %d messages around a ring of %d processes.\n", m, n));
countdown = CFG_ACTOR(cfg, countdown_builder_beh, NUMBER(n));
CFG_SEND(cfg, countdown, cons(countdown, NUMBER(m)));
}
CFG_SEND(cfg, timing_actor, NIL);
for (cnt = 1; cnt < 1000000; ++cnt) {
int max = 1000000;
int rem;
rem = run_configuration(cfg, max);
/* TRACE(printf("+ %d messages...\n", (max - rem))); */
TRACE(fputc('.', stderr));
if (cfg->q_count <= 0) {
/* queue empty */
break;
}
}
TRACE(printf("< %dM messages delivered.\n", cnt));
report_actor_usage(cfg);
DBUG_RETURN;
}
void
report_cons_stats()
{
report_atom_usage();
report_cons_usage();
}
void
usage(void)
{
fprintf(stderr, "\
usage: %s [-n processes] [-m messages] [-# dbug]\n",
_Program);
exit(EXIT_FAILURE);
}
void
banner(void)
{
printf("%s v%s -- %s\n", _Program, _Version, _Copyright);
}
int
main(int argc, char** argv)
{
int c;
int n = 0;
int m = 100;
DBUG_ENTER("main");
DBUG_PROCESS(argv[0]);
while ((c = getopt(argc, argv, "n:m:#:V")) != EOF) {
switch(c) {
case 'n': n = atoi(optarg); break;
case 'm': m = atoi(optarg); break;
case '#': DBUG_PUSH(optarg); break;
case 'V': banner(); exit(EXIT_SUCCESS);
case '?': usage();
default: usage();
}
}
banner();
erlang_challenge(n, m); /* this test involves running the dispatch loop */
report_cons_stats();
DBUG_RETURN (exit(EXIT_SUCCESS), 0);
}