forked from bxparks/AceRoutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryBenchmark.ino
530 lines (441 loc) · 12.9 KB
/
MemoryBenchmark.ino
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* A program which attempts to calculate how much flash and static memory are
* used by the AceRoutine library. Set the FEATURE macro to various integer to
* compile different parts of the AceButton library.
*/
#include <Arduino.h>
// DO NOT MODIFY THIS LINE. This will be overwritten by collect.sh on each
// iteration, incrementing from 0 to N. The Arduino IDE will compile the
// program, then the script will extract the flash and static memory usage
// numbers printed out by the Arduino compiler. The numbers will be printed on
// the STDOUT, which then can be saved to a file specific for a particular
// hardware platform, e.g. "nano.txt" or "esp8266.txt".
#define FEATURE 0
// List of features to gather memory statistics.
#define FEATURE_BASELINE 0
#define FEATURE_ONE_DELAY_FUNCTION 1
#define FEATURE_TWO_DELAY_FUNCTIONS 2
#define FEATURE_ONE_COROUTINE 3
#define FEATURE_TWO_COROUTINES 4
#define FEATURE_ONE_COROUTINE_MICROS 5
#define FEATURE_TWO_COROUTINES_MICROS 6
#define FEATURE_ONE_COROUTINE_SECONDS 7
#define FEATURE_TWO_COROUTINES_SECONDS 8
#define FEATURE_SCHEDULER_ONE_COROUTINE 9
#define FEATURE_SCHEDULER_TWO_COROUTINES 10
#define FEATURE_SCHEDULER_ONE_COROUTINE_MICROS 11
#define FEATURE_SCHEDULER_TWO_COROUTINES_MICROS 12
#define FEATURE_SCHEDULER_ONE_COROUTINE_SECONDS 13
#define FEATURE_SCHEDULER_TWO_COROUTINES_SECONDS 14
#define FEATURE_SCHEDULER_SETUP_ONE_COROUTINE 15
#define FEATURE_SCHEDULER_SETUP_TWO_COROUTINES 16
#define FEATURE_SCHEDULER_MANUAL_SETUP_ONE_COROUTINE 17
#define FEATURE_SCHEDULER_MANUAL_SETUP_TWO_COROUTINES 18
#define FEATURE_BLINK_FUNCTION 19
#define FEATURE_BLINK_COROUTINE 20
#if FEATURE != FEATURE_BASELINE
#include <AceRoutine.h>
using namespace ace_routine;
#endif
// A volatile integer to prevent the compiler from optimizing away the entire
// program.
volatile int disableCompilerOptimization = 0;
#if FEATURE == FEATURE_ONE_DELAY_FUNCTION
// Hand-rolled alternative to using a COROUTINE() that executes every 10
// milliseconds.
void delayFunction() {
static uint16_t prevMillis;
uint16_t nowMillis = millis();
if ((uint16_t) nowMillis - prevMillis >= 10) {
prevMillis = nowMillis;
disableCompilerOptimization = 1;
}
}
#elif FEATURE == FEATURE_TWO_DELAY_FUNCTIONS
// Hand-rolled alternative to using a COROUTINE() that executes every 10
// milliseconds.
void delayFunctionA() {
static uint16_t prevMillis;
uint16_t nowMillis = millis();
if ((uint16_t) nowMillis - prevMillis >= 10) {
prevMillis = nowMillis;
disableCompilerOptimization = 1;
}
}
void delayFunctionB() {
static uint16_t prevMillis;
uint16_t nowMillis = millis();
if ((uint16_t) nowMillis - prevMillis >= 10) {
prevMillis = nowMillis;
disableCompilerOptimization = 1;
}
}
#elif FEATURE == FEATURE_ONE_COROUTINE
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
#elif FEATURE == FEATURE_TWO_COROUTINES
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
COROUTINE(b) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
#elif FEATURE == FEATURE_ONE_COROUTINE_MICROS
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
#elif FEATURE == FEATURE_TWO_COROUTINES_MICROS
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
COROUTINE(b) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
#elif FEATURE == FEATURE_ONE_COROUTINE_SECONDS
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
#elif FEATURE == FEATURE_TWO_COROUTINES_SECONDS
COROUTINE(a) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
COROUTINE(b) {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE
class MyCoroutine : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
};
MyCoroutine a;
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES
class MyCoroutineA : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
};
class MyCoroutineB : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
};
MyCoroutineA a;
MyCoroutineB b;
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE_MICROS
class MyCoroutine : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
};
MyCoroutine a;
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES_MICROS
class MyCoroutineA : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
};
class MyCoroutineB : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_MICROS(10);
}
}
};
MyCoroutineA a;
MyCoroutineB b;
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE_SECONDS
class MyCoroutine : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
};
MyCoroutine a;
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES_SECONDS
class MyCoroutineA : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
};
class MyCoroutineB : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
};
MyCoroutineA a;
MyCoroutineB b;
#elif FEATURE == FEATURE_SCHEDULER_SETUP_ONE_COROUTINE
class MyCoroutine : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
MyCoroutine a;
#elif FEATURE == FEATURE_SCHEDULER_SETUP_TWO_COROUTINES
class MyCoroutineA : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
class MyCoroutineB : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
MyCoroutineA a;
MyCoroutineB b;
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_ONE_COROUTINE
class MyCoroutine : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
MyCoroutine a;
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_TWO_COROUTINES
class MyCoroutineA : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
class MyCoroutineB : public Coroutine {
public:
int runCoroutine() override {
COROUTINE_LOOP() {
disableCompilerOptimization = 1;
COROUTINE_DELAY_SECONDS(10);
}
}
void setupCoroutine() override {
disableCompilerOptimization = 1;
}
};
MyCoroutineA a;
MyCoroutineB b;
#elif FEATURE == FEATURE_BLINK_FUNCTION
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Asymmetric blink, same functionality as COROUTINE(blink) below, but using
// a delay function and a finite state machine. The logic is a lot harder to
// follow than a coroutine.
void blink() {
static uint16_t prevMillis;
static uint8_t blinkState;
const uint8_t kBlinkStateLow = 0;
const uint8_t kBlinkStateHigh = 1;
if (blinkState == kBlinkStateHigh) {
uint16_t nowMillis = millis();
if ((uint16_t) (nowMillis - prevMillis) >= 100) {
prevMillis = nowMillis;
digitalWrite(LED_BUILTIN, LOW);
blinkState = kBlinkStateLow;
}
} else {
uint16_t nowMillis = millis();
if ((uint16_t) (nowMillis - prevMillis) >= 500) {
prevMillis = nowMillis;
digitalWrite(LED_BUILTIN, HIGH);
blinkState = kBlinkStateHigh;
}
}
}
#elif FEATURE == FEATURE_BLINK_COROUTINE
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
// Asymmetric blink, same functionality as blink() function above. HIGH is
// different than LOW.
COROUTINE(blink) {
COROUTINE_LOOP() {
digitalWrite(LED_BUILTIN, HIGH);
COROUTINE_DELAY(100);
digitalWrite(LED_BUILTIN, LOW);
COROUTINE_DELAY(500);
}
}
#endif
// TeensyDuino seems to pull in malloc() and free() when a class with virtual
// functions is used polymorphically. This causes the memory consumption of
// FEATURE_BASELINE (which normally has no classes defined, so does not include
// malloc() and free()) to be artificially small which throws off the memory
// consumption calculations for all subsequent features. Let's define a
// throw-away class and call its method for all FEATURES, including BASELINE.
#if defined(TEENSYDUINO)
class FooClass {
public:
virtual void doit() {
disableCompilerOptimization = 0;
}
};
FooClass* foo;
#endif
void setup() {
delay(1000);
#if defined(TEENSYDUINO)
// Force Teensy to bring in malloc(), free() and other things for virtual
// dispatch.
foo = new FooClass();
#endif
#if FEATURE >= FEATURE_SCHEDULER_ONE_COROUTINE \
&& FEATURE <= FEATURE_SCHEDULER_MANUAL_SETUP_TWO_COROUTINES
CoroutineScheduler::setup();
#if FEATURE == FEATURE_SCHEDULER_SETUP_ONE_COROUTINE \
|| FEATURE == FEATURE_SCHEDULER_SETUP_TWO_COROUTINES
CoroutineScheduler::setupCoroutines();
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_ONE_COROUTINE
a.setupCoroutine();
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_TWO_COROUTINES
a.setupCoroutine();
b.setupCoroutine();
#endif
#endif
}
void loop() {
#if defined(TEENSYDUINO)
foo->doit();
#endif
#if FEATURE == FEATURE_BASELINE
disableCompilerOptimization = 1;
#elif FEATURE == FEATURE_ONE_DELAY_FUNCTION
delayFunction();
#elif FEATURE == FEATURE_TWO_DELAY_FUNCTIONS
delayFunctionA();
delayFunctionB();
#elif FEATURE == FEATURE_ONE_COROUTINE
a.runCoroutine();
#elif FEATURE == FEATURE_TWO_COROUTINES
a.runCoroutine();
b.runCoroutine();
#elif FEATURE == FEATURE_ONE_COROUTINE_MICROS
a.runCoroutine();
#elif FEATURE == FEATURE_TWO_COROUTINES_MICROS
a.runCoroutine();
b.runCoroutine();
#elif FEATURE == FEATURE_ONE_COROUTINE_SECONDS
a.runCoroutine();
#elif FEATURE == FEATURE_TWO_COROUTINES_SECONDS
a.runCoroutine();
b.runCoroutine();
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE_MICROS
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES_MICROS
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_ONE_COROUTINE_SECONDS
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_TWO_COROUTINES_SECONDS
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_SETUP_ONE_COROUTINE
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_SETUP_TWO_COROUTINES
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_ONE_COROUTINE
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_SCHEDULER_MANUAL_SETUP_TWO_COROUTINES
CoroutineScheduler::loop();
#elif FEATURE == FEATURE_BLINK_COROUTINE
blink.runCoroutine();
#elif FEATURE == FEATURE_BLINK_FUNCTION
blink();
#endif
}