Repository navigation
Expand file tree
/
Copy pathscope.cpp
More file actions
517 lines (451 loc) · 17.1 KB
/
Copy pathscope.cpp
File metadata and controls
517 lines (451 loc) · 17.1 KB
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
// Copyright © 2019-2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "scope.h"
#include <nlohmann_json.hpp>
#include <atomic>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <vector>
#include <mutex>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
// The SCOPE trace is a lossless, back-pressured device->host stream.
// `VX_scope_tap` captures into an on-chip BRAM that is now a *ring*: the
// host drains it continuously (every CP launch-wait poll iteration, via
// `vx_scope_drain`) over the bit-serial MMIO sideband; when the ring is
// full the tap pauses capture (it never overflows / overwrites). The
// serial sideband is kept deliberately — it is independent of the membus
// and the CP, so SCOPE survives a wedged command path.
#define SAMPLE_FLUSH_SIZE 100
#define TIMEOUT_TIME (60*60)
#define MAX_DELAY_CYCLES 10000
#define CMD_GET_WIDTH 0
#define CMD_GET_COUNT 1
#define CMD_GET_START 2
#define CMD_GET_DATA 3
#define CMD_SET_START 4
#define CMD_SET_STOP 5
#define CMD_SET_DEPTH 6
#define CHECK_ERR(_expr) \
do { \
int err = _expr; \
if (err == 0) \
break; \
printf("[SCOPE] Error: '%s' returned %d!\n", #_expr, err); \
return err; \
} while (false)
struct tap_signal_t {
uint32_t id;
std::string name;
uint32_t width;
};
struct tap_t {
uint32_t id;
uint32_t width;
uint32_t samples; // total samples drained from the ring
uint32_t cur_sample; // VCD emit cursor, in samples
uint64_t cycle_time;
std::string path;
std::vector<tap_signal_t> signals;
std::vector<uint64_t> words; // raw drained word stream: [delta,data...]*
size_t rpos; // VCD emit cursor, in words
};
static scope_callback_t g_callback;
static vx_device_h g_hdevice = nullptr;
// Atomic so vx_scope_stop can flip the flag without taking
// g_stop_mutex — a wedged drain thread (e.g. AXI-Lite read hung on a
// desynced serial bus) holds the mutex and would cause vx_scope_stop to
// block forever. With the atomic, stop signals the drain to bail first.
static std::atomic<bool> g_running{false};
static std::mutex g_stop_mutex;
static std::vector<tap_t> g_taps;
// Serial-bus sentinel returned by the AFU on the SCP_0/SCP_1 read pair
// when its bounded-stall timeout fires (rvalid_stall held longer than
// the watchdog). The host treats it as "tap timed out; skip this drain
// pass" rather than as data.
static constexpr uint64_t SCOPE_BUS_SENTINEL = 0xDEADDEADDEADDEADull;
using json = nlohmann::json;
static std::vector<std::string> split(const std::string &s, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
static void dump_module(std::ofstream& ofs,
const std::string& name,
std::unordered_map<std::string, std::unordered_set<std::string>>& hierarchy,
std::unordered_map<std::string, tap_t*>& tails,
int indentation) {
std::string indent(indentation, ' ');
ofs << indent << "$scope module " << name << " $end" << std::endl;
auto itt = tails.find(name);
if (itt != tails.end()) {
for (auto& signal : itt->second->signals) {
ofs << indent << " $var wire " << signal.width << " " << signal.id << " " << signal.name << " $end" << std::endl;
}
}
auto ith = hierarchy.find(name);
if (ith != hierarchy.end()) {
for (auto& child : ith->second) {
dump_module(ofs, child, hierarchy, tails, indentation + 1);
}
}
ofs << indent << "$upscope $end" << std::endl;
}
static void dump_header(std::ofstream& ofs, std::vector<tap_t>& taps) {
ofs << "$version Generated by Vortex Scope Analyzer $end" << std::endl;
ofs << "$timescale 1 ns $end" << std::endl;
ofs << "$scope module TOP $end" << std::endl;
ofs << " $var wire 1 0 clk $end" << std::endl;
std::unordered_map<std::string, std::unordered_set<std::string>> hierarchy;
std::unordered_set<std::string> heads;
std::unordered_map<std::string, tap_t*> tails;
// Build hierarchy
for (auto& tap : taps) {
std::vector<std::string> tokens = split(tap.path, '.');
for (size_t i = 1; i < tokens.size(); ++i) {
hierarchy[tokens[i-1]].insert(tokens[i]);
}
auto h = tokens[0];
auto t = tokens[tokens.size()-1];
heads.insert(h);
tails[t] = &tap;
}
// Dump module hierarchy
for (auto& head : heads) {
dump_module(ofs, head, hierarchy, tails, 1);
}
ofs << "$upscope $end" << std::endl;
ofs << "enddefinitions $end" << std::endl;
}
// return the earliest tap that has data to dump
static tap_t* find_earliest_tap(std::vector<tap_t>& taps) {
tap_t* earliest = nullptr;
for (auto& tap : taps) {
if (tap.samples == 0)
continue;
if (tap.cur_sample == tap.samples)
continue;
if (earliest != nullptr) {
if (tap.cycle_time < earliest->cycle_time)
earliest = &tap;
} else {
earliest = &tap;
}
}
return earliest;
}
static uint64_t advance_clock(std::ofstream& ofs, uint64_t cur_time, uint64_t next_time) {
uint64_t delta = next_time - cur_time;
if (delta > MAX_DELAY_CYCLES) {
ofs << '#' << (cur_time * 2 + 0) << std::endl;
ofs << "bx 0" << std::endl;
ofs << '#' << (cur_time * 2 + 1) << std::endl;
ofs << "bx 0" << std::endl;
cur_time = next_time - MAX_DELAY_CYCLES;
}
while (cur_time < next_time) {
ofs << '#' << (cur_time * 2 + 0) << std::endl;
ofs << "b0 0" << std::endl;
ofs << '#' << (cur_time * 2 + 1) << std::endl;
ofs << "b1 0" << std::endl;
++cur_time;
}
return cur_time;
}
// load the scope manifest (tap topology) from the JSON file
static int load_taps(std::vector<tap_t>& taps) {
const char* json_path = getenv("SCOPE_JSON_PATH");
if (nullptr == json_path) {
std::cerr << "[SCOPE] Error: SCOPE_JSON_PATH is not set" << std::endl;
return -1;
}
std::ifstream ifs(json_path);
if (!ifs) {
std::cerr << "[SCOPE] Error: cannot open scope manifest file: " << json_path << std::endl;
return -1;
}
auto json_obj = json::parse(ifs);
if (json_obj.is_null()) {
std::cerr << "[SCOPE] Error: invalid scope manifest file: " << json_path << std::endl;
return -1;
}
uint32_t signal_id = 1;
for (auto& tap : json_obj["taps"]) {
tap_t _tap;
_tap.id = tap["id"].get<uint32_t>();
_tap.width = tap["width"].get<uint32_t>();
_tap.path = tap["path"].get<std::string>();
_tap.samples = 0;
_tap.cur_sample = 0;
_tap.cycle_time = 0;
_tap.rpos = 0;
for (auto& signal : tap["signals"]) {
auto name = signal[0].get<std::string>();
auto width = signal[1].get<uint32_t>();
_tap.signals.push_back({signal_id, name, width});
++signal_id;
}
taps.emplace_back(std::move(_tap));
}
return 0;
}
// issue one serial register command and read back its 64-bit response
//
// Returns:
// 0 on success
// 1 on serial-bus timeout (AFU returned SCOPE_BUS_SENTINEL on the
// read) — soft error; caller should skip this tap-this-pass rather
// than treat as fatal.
// -1 on hard backend error.
static int read_reg(vx_device_h hdevice, uint64_t wr_value, uint64_t* rd_value) {
CHECK_ERR(g_callback.registerWrite(hdevice, wr_value));
CHECK_ERR(g_callback.registerRead(hdevice, rd_value));
if (*rd_value == SCOPE_BUS_SENTINEL)
return 1; // bus-stall watchdog tripped; tap likely desynced
return 0;
}
// Drain every sample currently buffered in one tap's ring. The hardware
// reports the live occupancy via CMD_GET_COUNT; each sample is a leading
// delta word followed by ceil(width/64) data words.
//
// Occupancy can never exceed the on-chip ring depth, which is small
// (<= 64Ki). A larger value means the bit-serial bus desynced; fail
// loudly rather than spin reading a bogus sample count.
#define SCOPE_MAX_OCCUPANCY (64u * 1024u)
static int drain_tap(tap_t& tap, vx_device_h hdevice) {
uint64_t count = 0;
int rr = read_reg(hdevice, (uint64_t(tap.id) << 3) | CMD_GET_COUNT, &count);
if (rr > 0) {
// Serial-bus watchdog fired — soft error. Drop this drain pass; let
// the next drain retry. Matches CoreSight ITM behaviour on a hung
// trace bus: report and continue, do not propagate as fatal.
std::cerr << "[SCOPE] tap #" << tap.id << " serial-bus timeout (skip)"
<< std::endl;
return 0;
}
if (rr < 0) return rr;
if (count == 0)
return 0;
if (count > SCOPE_MAX_OCCUPANCY) {
std::cerr << "[SCOPE] Error: tap #" << tap.id << " bogus occupancy="
<< count << " (serial bus desync)" << std::endl;
return -1;
}
uint32_t data_blocks = (tap.width + 63) / 64;
uint64_t words_per_sample = 1 + data_blocks;
uint64_t cmd_data = (uint64_t(tap.id) << 3) | CMD_GET_DATA;
uint64_t total_words = count * words_per_sample;
size_t rollback = tap.words.size(); // snapshot for partial-drain rollback
tap.words.reserve(rollback + total_words);
for (uint64_t i = 0; i < total_words; ++i) {
uint64_t word = 0;
int rr_d = read_reg(hdevice, cmd_data, &word);
if (rr_d > 0) {
// Mid-burst bus timeout. Rollback the partial words because (a)
// the AFU watchdog fired, so we cannot tell which of the queued
// samples actually drained from the on-chip ring; and (b) keeping
// a partial sample would desync the VCD writer's per-sample word
// accounting forever. Accept the lost samples — far better than
// silently corrupted trace data.
std::cerr << "[SCOPE] tap #" << tap.id << " serial-bus timeout mid-drain "
"(dropped " << (count - (i / words_per_sample))
<< " samples)" << std::endl;
tap.words.resize(rollback);
return 0;
}
if (rr_d < 0) return rr_d;
tap.words.push_back(word);
}
tap.samples += (uint32_t)count;
return 0;
}
static int dump_tap(std::ofstream& ofs, tap_t* tap) {
uint32_t signal_offset = 0;
uint32_t sample_offset = 0;
uint64_t word;
std::vector<char> signal_data(tap->width + 1);
auto signal_it = tap->signals.rbegin();
uint32_t signal_width = signal_it->width;
do {
// read data word from the drained buffer
if (tap->rpos >= tap->words.size()) {
std::cerr << "[SCOPE] Error: tap #" << tap->id << " trace underrun" << std::endl;
return -1;
}
word = tap->words[tap->rpos++];
do {
uint32_t word_offset = sample_offset % 64;
signal_data[signal_width - signal_offset - 1] = ((word >> word_offset) & 0x1) ? '1' : '0';
++signal_offset;
++sample_offset;
if (signal_offset == signal_width) {
signal_data[signal_width] = 0; // string null termination
ofs << 'b' << signal_data.data() << ' ' << signal_it->id << std::endl;
if (sample_offset == tap->width) {
// end-of-sample
++tap->cur_sample;
if (tap->cur_sample != tap->samples) {
// next delta word
if (tap->rpos >= tap->words.size()) {
std::cerr << "[SCOPE] Error: tap #" << tap->id << " trace underrun" << std::endl;
return -1;
}
word = tap->words[tap->rpos++];
tap->cycle_time += 1 + word;
if (0 == (tap->cur_sample % SAMPLE_FLUSH_SIZE)) {
ofs << std::flush;
std::cout << std::dec << "[SCOPE] flush tap #" << tap->id << ": "<< tap->cur_sample << "/" << tap->samples << " samples, next_time=" << tap->cycle_time << std::endl;
}
}
break;
}
signal_offset = 0;
++signal_it;
signal_width = signal_it->width;
}
} while ((sample_offset % 64) != 0);
} while (sample_offset != tap->width);
return 0;
}
int vx_scope_start(scope_callback_t* callback, vx_device_h hdevice, uint64_t start_time, uint64_t stop_time) {
if (nullptr == hdevice || nullptr == callback)
return -1;
g_callback = *callback;
g_hdevice = hdevice;
g_taps.clear();
CHECK_ERR(load_taps(g_taps));
// Validate the scope manifest against the hardware.
for (auto& tap : g_taps) {
uint64_t dev_width = 0;
CHECK_ERR(read_reg(hdevice, (uint64_t(tap.id) << 3) | CMD_GET_WIDTH, &dev_width));
if (tap.width != dev_width) {
std::cerr << "[SCOPE] Error: invalid tap #" << tap.id << " width, actual=" << dev_width << ", expected=" << tap.width << std::endl;
return 1;
}
}
if (stop_time != uint64_t(-1)) {
std::cout << "[SCOPE] stop time: " << std::dec << stop_time << "s" << std::endl;
for (auto& tap : g_taps) {
uint64_t cmd_stop = (stop_time << 11) | (uint64_t(tap.id) << 3) | CMD_SET_STOP;
CHECK_ERR(g_callback.registerWrite(hdevice, cmd_stop));
}
}
if (start_time != uint64_t(-1)) {
std::cout << "[SCOPE] start time: " << std::dec << start_time << "s" << std::endl;
for (auto& tap : g_taps) {
uint64_t cmd_start = (start_time << 11) | (uint64_t(tap.id) << 3) | CMD_SET_START;
CHECK_ERR(g_callback.registerWrite(hdevice, cmd_start));
}
}
g_running.store(true, std::memory_order_release);
// create auto-stop thread
uint32_t timeout_time = TIMEOUT_TIME;
const char* env_timeout = std::getenv("SCOPE_TIMEOUT");
if (env_timeout != nullptr) {
std::stringstream ss(env_timeout);
uint32_t env_value;
if (ss >> env_value) {
timeout_time = env_value;
std::cout << "[SCOPE] timeout time=" << env_value << std::endl;
}
}
std::thread([hdevice, timeout_time]() {
std::this_thread::sleep_for(std::chrono::seconds(timeout_time));
std::cout << "[SCOPE] auto-stop timeout!" << std::endl;
vx_scope_stop(hdevice);
}).detach();
return 0;
}
// Drain the tap rings into host buffers. Called continuously by the host
// (the CP launch-wait poll loop). Checks g_running cooperatively so a
// stop request takes effect within a single tap of latency, even if the
// drain loop is mid-burst.
int vx_scope_drain(void) {
// Fast-path bail BEFORE taking the mutex: vx_scope_stop sets g_running
// to false unlocked, so a wedged drain holder can't block stop.
if (!g_running.load(std::memory_order_acquire))
return 0;
std::lock_guard<std::mutex> lock(g_stop_mutex);
if (!g_running.load(std::memory_order_acquire))
return 0; // raced with stop while waiting on mutex
for (auto& tap : g_taps) {
if (!g_running.load(std::memory_order_acquire))
return 0; // stop signal mid-pass — bail
CHECK_ERR(drain_tap(tap, g_hdevice));
}
return 0;
}
int vx_scope_stop(vx_device_h hdevice) {
if (nullptr == hdevice)
return -1;
// Flag stop BEFORE taking the mutex so vx_scope_drain bails out of any
// in-flight pass — including ones wedged inside read_reg on a stuck
// AXI-Lite read. Without this, a hung drain would hold g_stop_mutex
// and vx_scope_stop would block forever joining the hang.
if (!g_running.exchange(false, std::memory_order_acq_rel))
return 0; // already stopped
std::lock_guard<std::mutex> lock(g_stop_mutex);
std::cout << "[SCOPE] stop recording..." << std::endl;
// stop the taps
for (auto& tap : g_taps) {
uint64_t cmd_stop = (uint64_t(0) << 11) | (uint64_t(tap.id) << 3) | CMD_SET_STOP;
CHECK_ERR(g_callback.registerWrite(hdevice, cmd_stop));
}
// Final drain of whatever is still buffered in the rings.
for (auto& tap : g_taps) {
CHECK_ERR(drain_tap(tap, hdevice));
}
std::cout << "[SCOPE] load trace info..." << std::endl;
for (auto& tap : g_taps) {
if (tap.samples == 0)
continue;
// get start timestamp
uint64_t start = 0;
CHECK_ERR(read_reg(hdevice, (uint64_t(tap.id) << 3) | CMD_GET_START, &start));
// First drained word is the leading delta.
uint64_t delta = tap.words[0];
tap.rpos = 1;
tap.cycle_time = 1 + start + delta;
std::cout << std::dec << "[SCOPE] tap #" << tap.id
<< ": width=" << tap.width
<< ", num_samples=" << tap.samples
<< ", start_time=" << tap.cycle_time
<< ", path=" << tap.path << std::endl;
}
std::cout << "[SCOPE] dump header..." << std::endl;
const char* vcd_file = std::getenv("VCD_FILE");
std::ofstream ofs(vcd_file ? vcd_file : "trace.vcd");
dump_header(ofs, g_taps);
std::cout << "[SCOPE] dump taps..." << std::endl;
uint64_t cur_time = 0;
auto tap = find_earliest_tap(g_taps);
if (tap != nullptr) {
do {
cur_time = advance_clock(ofs, cur_time, tap->cycle_time);
CHECK_ERR(dump_tap(ofs, tap));
tap = find_earliest_tap(g_taps);
} while (tap != nullptr);
advance_clock(ofs, cur_time, cur_time + 1);
}
std::cout << "[SCOPE] trace dump done! - " << (cur_time/2) << " cycles" << std::endl;
return 0;
}