Skip to content

Commit 821e49d

Browse files
authored
Merge pull request #49 from IndTechSpprt/improved-profiling
improved profiling
2 parents 52d4920 + eee2fa6 commit 821e49d

File tree

4 files changed

+110
-21
lines changed

4 files changed

+110
-21
lines changed

MCU_code/PlatformIO_code/shared/menu/menu.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ void display_menu() {
109109
Serial.println("\tc - Start Logging Coordinator data (Restarting logger will append records to existing log)");
110110
Serial.println("\td - Dump Log");
111111
Serial.println("\th - Menu");
112+
#ifdef PROFILING
113+
Serial.println("\tr - RAM Statistics from previous run");
114+
Serial.println("\tt - Timing Statistics from previous run");
115+
#endif
112116
Serial.println();
113117
}
114118

@@ -149,6 +153,40 @@ void menu_handler(void) {
149153
case '\r':
150154
case '\n':
151155
case 'h': display_menu(); break;
156+
#ifdef PROFILING
157+
case 'r': {
158+
Serial.println("RAM Usage:");
159+
for (uint i = 0; i < MAX_RAM_USAGE_SAMPLES; i++) {
160+
Serial.print(ram_usage[i]);
161+
Serial.print(", ");
162+
}
163+
Serial.println("");
164+
Serial.println("RAM Usage by layer:");
165+
for (uint i = 0; i < 53; i++) {
166+
Serial.print(ram_usage_by_layer[i]);
167+
Serial.print(", ");
168+
}
169+
Serial.println("");
170+
} break;
171+
case 't': {
172+
Serial.print("Inference Time (ms): ");
173+
Serial.println(inference_time);
174+
Serial.print("Of which, wait time (ms):");
175+
Serial.println(wait_total);
176+
Serial.println("Layer Wise Inference Time (ms)");
177+
for (uint i = 0; i < 53; i++) {
178+
Serial.print(inference_time_layer_wise[i]);
179+
Serial.print(", ");
180+
}
181+
Serial.println("");
182+
Serial.println("Layer Wise Wait Time (ms)");
183+
for (uint i = 0; i < 53; i++) {
184+
Serial.print(wait_layer_wise[i]);
185+
Serial.print(", ");
186+
}
187+
Serial.println("");
188+
} break;
189+
#endif
152190
}
153191
while (Serial.read() != -1); // remove rest of characters.
154192
}

MCU_code/PlatformIO_code/shared/menu/menu.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ void menu_handler(void);
1414

1515
extern WriteTypes type; //Current write type
1616

17+
#ifdef PROFILING
18+
extern unsigned int inference_time_layer_wise[53];
19+
extern unsigned int wait_layer_wise[53];
20+
extern unsigned int inference_time;
21+
extern unsigned int wait_total;
22+
#define MAX_RAM_USAGE_SAMPLES 1200
23+
extern volatile unsigned int ram_usage[MAX_RAM_USAGE_SAMPLES];
24+
extern unsigned ram_usage_by_layer[53];
25+
#endif
26+
1727
#endif

MCU_code/PlatformIO_code/worker_code/src/worker_code.ino

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ extern unsigned long _heap_end;
99
extern char* __brkval;
1010

1111
#define SAMPLE_PERIOD_MICRO_S 100000
12-
#define MAX_RAM_USAGE_SAMPLES 2000
1312

14-
volatile uint ram_usage[MAX_RAM_USAGE_SAMPLES];
15-
volatile uint samples = 0;
13+
volatile unsigned ram_usage[MAX_RAM_USAGE_SAMPLES];
14+
unsigned int ram_usage_by_layer[53] = {0};
15+
volatile unsigned samples = 0;
1616

1717
/// @brief Determine how much RAM is being used at the current instant (Currently only for HEAP)
1818
void saveRAMUsage() {
@@ -22,8 +22,13 @@ void saveRAMUsage() {
2222
}
2323
}
2424

25+
uint inference_time_layer_wise[53] = {0};
26+
uint wait_layer_wise[53] = {0};
27+
uint inference_time = 0;
28+
29+
uint wait_total = 0;
30+
2531
IntervalTimer ramUsageTimer; //Interval timer to keep track of RAM usage
26-
bool first_run = true; //bool to keep track of whether this is the first run or not
2732
#endif
2833

2934
WriteTypes type = Stop; //Current write type
@@ -34,9 +39,6 @@ bool overflow_flag = false;
3439
int rec_count = 0;
3540
int ino_count = 0;
3641
void setup() {
37-
#ifdef PROFILING
38-
ramUsageTimer.begin(saveRAMUsage,SAMPLE_PERIOD_MICRO_S);//Save RAM usage at 1 ms intervals
39-
#endif
4042
setup_filesys();
4143
{
4244
setup_communication();
@@ -48,9 +50,15 @@ void setup() {
4850
read_line_by_line(LINES_FILENAME, lines);
4951
}
5052
#ifdef PROFILING
51-
int inference_start = millis();
53+
uint inference_start = millis();
54+
ramUsageTimer.begin(saveRAMUsage,SAMPLE_PERIOD_MICRO_S);//Save RAM usage at 1 ms intervals
5255
#endif
5356
for (int j = 0; j < 53; j++) {
57+
#ifdef PROFILING
58+
uint layer_start = millis();
59+
uint wait_layer = 0;
60+
uint wait_phase_begin = 0;
61+
#endif
5462
Serial.print("Current layer: ");
5563
Serial.println(j);
5664
if(j < 52){
@@ -64,9 +72,15 @@ void setup() {
6472
Serial.println("input is nullptr!");
6573
}
6674
}
75+
#ifdef PROFILING
76+
wait_phase_begin = millis();
77+
#endif
6778
while(rec_count != input_length[j]){
6879
check_and_receive(rec_count,input_distribution);
6980
}
81+
#ifdef PROFILING
82+
wait_layer += millis() - wait_phase_begin;
83+
#endif
7084
Serial.println("finished...");
7185
rec_count = 0;
7286
}
@@ -92,6 +106,9 @@ void setup() {
92106
}
93107
distributed_computation(first_line, input_distribution, result, overflow, input_length[j]);
94108
handle_residual(result,result_length[j],j,residual_connection,zps,scales);
109+
#ifdef PROFILING
110+
ram_usage_by_layer[j] = 524288 - ((char*)&_heap_end - __brkval);
111+
#endif
95112
if(input_distribution != nullptr) delete[] input_distribution;
96113
}
97114
if (overflow_flag) {
@@ -100,7 +117,13 @@ void setup() {
100117
}
101118
input_distribution = new byte[input_length[j + 1]];
102119
Serial.println("waiting for permission...");
120+
#ifdef PROFILING
121+
wait_phase_begin = millis();
122+
#endif
103123
wait_for_permission(rec_count,input_distribution);
124+
#ifdef PROFILING
125+
wait_layer += millis() - wait_phase_begin;
126+
#endif
104127
Serial.println("premission granted, sending results...");
105128
if (j < 51) {
106129
char to_send[MESSAGE_SIZE];
@@ -155,7 +178,13 @@ void setup() {
155178
}
156179
//check regularly to avoid clogging
157180
if(rec_count < input_length[j + 1]) {
181+
#ifdef PROFILING
182+
wait_phase_begin = millis();
183+
#endif
158184
check_and_receive( rec_count, input_distribution);
185+
#ifdef PROFILING
186+
wait_layer += millis() - wait_phase_begin;
187+
#endif
159188
}
160189
}
161190
//send the rest of the data
@@ -168,6 +197,12 @@ void setup() {
168197
if (overflow_flag) dataFile.close();
169198
to_send[1] = Complete; //signal the end
170199
send_message_to_coordinator(to_send);
200+
#ifdef PROFILING
201+
unsigned int temp_usage = 524288 - ((char*)&_heap_end - __brkval);
202+
if (temp_usage > ram_usage_by_layer[j]) {
203+
ram_usage_by_layer[j] = temp_usage;
204+
}
205+
#endif
171206
}
172207
else if(j == 51){
173208
char to_send[MESSAGE_SIZE];
@@ -190,6 +225,9 @@ void setup() {
190225
}
191226
to_send[1] = Complete;
192227
send_message_to_coordinator(to_send);
228+
#ifdef PROFILING
229+
ram_usage_by_layer[j] = 524288 - ((char*)&_heap_end - __brkval);
230+
#endif
193231
}
194232
///////////////////////////
195233
}
@@ -202,7 +240,13 @@ void setup() {
202240
Serial.println(rec_count);
203241
Serial.println("not enough inputs, receiving...");
204242
while(rec_count != input_length[j]){
243+
#ifdef PROFILING
244+
wait_phase_begin = millis();
245+
#endif
205246
check_and_receive(rec_count,input_distribution);
247+
#ifdef PROFILING
248+
wait_layer += millis() - wait_phase_begin;
249+
#endif
206250
}
207251
Serial.println("finished...");
208252
rec_count = 0;
@@ -230,25 +274,22 @@ void setup() {
230274
results[1] = Inference_Results;
231275
write_length(results, res_count);
232276
client.write(results, MESSAGE_SIZE);
277+
#ifdef PROFILING
278+
ram_usage_by_layer[j] = 524288 - ((char*)&_heap_end - __brkval);
279+
#endif
233280
}
281+
#ifdef PROFILING
282+
inference_time_layer_wise[j] = (millis() - layer_start);
283+
wait_layer_wise[j] = wait_layer;
284+
wait_total += wait_layer;
285+
#endif
234286
}
235287
#ifdef PROFILING
236-
Serial.print("Inference took ");
237-
Serial.print(((float) (millis() - inference_start)) / 1000.0 );
238-
Serial.println("s");
239-
ramUsageTimer.end();
288+
inference_time = millis() - inference_start;
240289
#endif
241290
}
242291
void loop() {
243292
if (Serial.available()) {
244-
#ifdef PROFILING
245-
if (first_run) {
246-
for (uint i = 0; i < MAX_RAM_USAGE_SAMPLES; i++) {
247-
Serial.print(ram_usage[i]);
248-
Serial.print(", ");
249-
}
250-
}
251-
#endif
252293
menu_handler();
253294
}
254295
// sendUDPMessage("1 to 2", ip2, localPort);

MCU_code/testbed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"mcu_id": "0", "ip_end": "124", "mac_end": "235"}, {"mcu_id": "1", "ip_end": "123", "mac_end": "236"}, {"mcu_id": "2", "ip_end": "122", "mac_end": "237"}]
1+
[{"id": "0", "ip_end": "124", "mac_end": "235"}, {"id": "1", "ip_end": "123", "mac_end": "236"}, {"id": "2", "ip_end": "122", "mac_end": "237"}, {"id": "3", "ip_end": "121", "mac_end": "238"}]

0 commit comments

Comments
 (0)