-
Notifications
You must be signed in to change notification settings - Fork 4
/
challenge.c
171 lines (155 loc) · 3.62 KB
/
challenge.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
/*
* challenge.c -- Joe Armstrong's Erlang challenge:
* Create a ring of M processes
* Send N simple messages around the ring
* Increase M until out of resources
*
* Copyright 2008 Dale Schumacher. ALL RIGHTS RESERVED.
*/
static char _Program[] = "Challenge";
static char _Version[] = "2008-05-12";
static char _Copyright[] = "Copyright 2008 Dale Schumacher";
#include <getopt.h>
#include "abe.h"
#include "dbug.h"
DBUG_UNIT("challenge");
#if 0
#define M_CNT (-1) /* sending -1 will trigger a countdown based on the highest numbered process */
#else
#define M_CNT (100) /* with a large (1M) number of processes, we want to send fewer messages */
#endif
/**
countdown_ring:
BEHAVIOR {n:$n, a:$a}
$m -> [
IF preceeds($m, 0) [
SEND $a $n
] ELIF preceeds(0, $n) [
SEND $a $m
] ELIF preceeds(0, $m) [
SEND $a sum($m, -1)
]
]
DONE
**/
BEH_DECL(countdown_ring)
{
CONS* m = WHAT;
CONS* state = MINE;
CONS* n = map_get(state, ATOM("n"));
CONS* a = map_get(state, ATOM("a"));
DBUG_ENTER("countdown_ring");
DBUG_PRINT("", ("self=%p", SELF));
DBUG_PRINT("", ("m=%d n=%d a=%p", MK_INT(m), MK_INT(n), a));
if (MK_INT(m) < 0) {
SEND(a, n);
} else if (0 < MK_INT(n)) {
SEND(a, m);
} else if (0 < MK_INT(m)) {
SEND(a, NUMBER(MK_INT(m) - 1));
}
DBUG_RETURN;
}
/**
challenge:
BEHAVIOR {n:$n}
$m -> [
IF preceeds(0, $n) [
a: ACTOR $challenge {n:sum($n, -1)}
BECOME $countdown_ring {n:$n, a:$a}
SEND $a $m
] ELSE [
BECOME $countdown_ring {n:$n, a:$m}
SEND $m -1
]
]
DONE
**/
BEH_DECL(challenge)
{
CONS* msg = WHAT;
CONS* state = MINE;
CONS* n = map_get(state, ATOM("n"));
CONS* a = map_get(state, ATOM("a"));
DBUG_ENTER("challenge");
DBUG_PRINT("", ("self=%p", SELF));
DBUG_PRINT("", ("msg=%p n=%d a=%p", msg, MK_INT(n), a));
if (0 < MK_INT(n)) {
a = ACTOR(challenge, map_put(NIL, ATOM("n"), NUMBER(MK_INT(n) - 1)));
BECOME(countdown_ring, map_put(state, ATOM("a"), a));
SEND(a, msg);
} else {
state = NIL;
state = map_put(state, ATOM("a"), msg);
state = map_put(state, ATOM("n"), n);
BECOME(countdown_ring, state);
SEND(msg, NUMBER(M_CNT)); /* disable this line for process creation only */
}
DBUG_RETURN;
}
/**
a: ACTOR $challenge {n:$counter}
SEND $a $a
**/
void
test_erlang_challenge(int counter)
{
CONFIG* cfg;
CONS* actor;
int n;
int m;
DBUG_ENTER("test_erlang_challenge");
cfg = new_configuration(10);
actor = CFG_ACTOR(cfg, challenge, map_put(NIL, ATOM("n"), NUMBER(counter)));
CFG_SEND(cfg, actor, actor);
for (m = 1; m < 1000000; ++m) {
n = run_configuration(cfg, 1000000);
if (cfg->q_count <= 0) {
/* queue empty */
break;
}
}
TRACE(printf("~%dM messages delivered.\n", m));
report_actor_usage(cfg);
DBUG_RETURN;
}
void
report_cons_stats()
{
report_atom_usage();
report_cons_usage();
}
void
usage(void)
{
fprintf(stderr, "\
usage: %s [-n count] [-# 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 counter = 1000;
DBUG_ENTER("main");
DBUG_PROCESS(argv[0]);
while ((c = getopt(argc, argv, "n:#:V")) != EOF) {
switch(c) {
case 'n': counter = atoi(optarg); break;
case '#': DBUG_PUSH(optarg); break;
case 'V': banner(); exit(EXIT_SUCCESS);
case '?': usage();
default: usage();
}
}
banner();
test_erlang_challenge(counter); /* this test involves running the dispatch loop */
report_cons_stats();
DBUG_RETURN (exit(EXIT_SUCCESS), 0);
}