blob: 5a4101c0968a8e163b2f78ec5bf0036bc24f7021 [file] [log] [blame]
Kristofer Jonssone56b6e42022-09-29 11:52:22 +02001#
2# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the License); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an AS IS BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from .types import *
20from sys import stderr
21
22class OutputInterface:
23 def flush(self):
24 ...
25
26 def writeEventRecord(self, data: bytearray):
27 ...
28
29class OutputBinary(OutputInterface):
30 def __init__(self, fname):
31 self.file = open(fname, 'wb')
32
33 def flush(self):
34 self.file.flush()
35
36 def writeEventRecord(self, data: bytearray):
37 self.file.write(data)
38
39class OutputJson(OutputInterface):
40 def __init__(self, fname):
41 self.file = open(fname, 'w')
42
43 self.count = 0
44 self.nextId = 0
45 self.timestamp = 0
46 self.event = []
47
48 def flush(self):
49 self.file.flush()
50
51 def writeEventRecord(self, data: bytearray):
52 record = EventRecord_t(data)
53
54 if record.first():
55 # Drop messages that don't originate from Ethos-U
56 if record.component() != EventRecord_t.ETHOSU_CID:
57 return
58
59 self.nextId = 0
60 self.timestamp = record.timestamp
61 self.eventConfig = []
62 self.eventCount = []
63
64 messageIndex = record.message()
65
66 if self.nextId != messageIndex or self.timestamp != record.timestamp:
67 stderr.write(f'Expected record id {self.nextId} and timestamp {self.timestamp} but got {messageIndex} and {record.timestamp}. count={self.count}, locked={record.locked()}, valid={record.valid()}\n')
68 stderr.write(f'record={record}\n')
69 return
70
71 self.nextId = messageIndex + 1
72
73 if messageIndex == 0:
74 self.cycleCount = record.val2 << 32 | record.val1
75 elif messageIndex == 1:
76 self.qread = record.val1
77 self.status = record.val2
78 else:
79 self.eventConfig.append(record.val1)
80 self.eventCount.append(record.val2)
81
82 if record.last():
83 self.file.write(f'{{ "timestamp": {self.timestamp}, "qread": {self.qread}, "status": {self.status}, "cycleCount": {self.cycleCount}, "eventConfig": [ {", ".join(map(str, self.eventConfig))} ], "eventCount": [ {", ".join(map(str, self.eventCount))} ] }}\n')