Debian Codegraph @

test/concurrency_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2020 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import argparse
17 import os
18 import sys
19 from time import (sleep, time)
20 from random import randrange
21 import math
22 import struct
23 import traceback
24 import argparse
25 import logging
26 from itertools import (chain, repeat)
27
28 from pyocd.core.helpers import ConnectHelper
29 from pyocd.flash.file_programmer import FileProgrammer
30 from pyocd.probe.pydapaccess import DAPAccess
31 from pyocd.utility.conversion import float32_to_u32
32 from pyocd.utility.mask import same
33 from pyocd.utility.compatibility import to_str_safe
34 from pyocd.core.memory_map import MemoryType
35
36 from test_util import (
37 Test,
38 TestResult,
39 get_session_options,
40 get_target_test_params,
41 run_in_parallel,
42 )
43
44 # Test configuration values.
45 TEST_MAX_LENGTH = 1 * 1024 * 1024
46 TEST_THREAD_COUNT = 8
47 TEST_SUBCHUNK_COUNT = 2 # Number of reads/writes per thread.
48
49 def ncycles(iterable, n):
50 return chain.from_iterable(repeat(tuple(iterable), n))
51
52 class ConcurrencyTestResult(TestResult):
53 def __init__(self):
54 super(ConcurrencyTestResult, self).__init__(None, None, None)
55 self.name = "concurrency"
56
57 class ConcurrencyTest(Test):
58 def __init__(self):
59 super(ConcurrencyTest, self).__init__("Concurrency Test", concurrency_test)
60
61 def run(self, board):
62 try:
63 result = self.test_function(board.unique_id)
64 except Exception as e:
65 result = ConcurrencyTestResult()
66 result.passed = False
67 print("Exception %s when testing board %s" % (e, board.unique_id))
68 traceback.print_exc(file=sys.stdout)
69 result.board = board
70 result.test = self
71 return result
72
73 def concurrency_test(board_id):
74 with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session:
75 board = session.board
76 target = session.target
77
78 test_params = get_target_test_params(session)
79 session.probe.set_clock(test_params['test_clock'])
80
81 memory_map = target.get_memory_map()
82 boot_region = memory_map.get_boot_memory()
83 ram_region = memory_map.get_default_region_of_type(MemoryType.RAM)
84
85 test_pass_count = 0
86 test_count = 0
87 result = ConcurrencyTestResult()
88
89 target.reset_and_halt()
90
91 # Prepare TEST_THREAD_COUNT regions of RAM with patterns
92 data_len = min(TEST_MAX_LENGTH, ram_region.length)
93 chunk_len = data_len // TEST_THREAD_COUNT
94 subchunk_len = chunk_len // TEST_SUBCHUNK_COUNT
95
96 chunk_data = []
97 for i in range(TEST_THREAD_COUNT):
98 chunk_data.append([(i + j) % 256 for j in range(chunk_len)])
99
100 def write_chunk_data(core, i):
101 start = ram_region.start + chunk_len * i
102 for j in range(TEST_SUBCHUNK_COUNT):
103 offset = subchunk_len * j
104 addr = start + offset
105 end = addr + subchunk_len - 1
106 print("Writing region %i:%i from %#010x to %#010x via %s" % (i, j, addr, end, core.ap))
107 core.write_memory_block8(addr, chunk_data[i][offset:offset + subchunk_len])
108 print("Finished writing region %i:%i" % (i, j))
109
110 def read_chunk_data(core, i):
111 start = ram_region.start + chunk_len * i
112 for j in range(TEST_SUBCHUNK_COUNT):
113 offset = subchunk_len * j
114 addr = start + offset
115 end = addr + subchunk_len - 1
116 print("Reading region %i:%i from %#010x to %#010x via %s" % (i, j, addr, end, core.ap))
117 data = core.read_memory_block8(addr, subchunk_len)
118 chunk_read_data[i].extend(data)
119 print("Finished reading region %i:%i" % (i, j))
120
121 # Test with a single core/AP.
122 print("\n------ Test 1: Concurrent memory accesses, single core ------")
123
124 core = target.cores[0]
125
126 # Write chunk patterns concurrently.
127 print("Writing %i regions to RAM" % TEST_THREAD_COUNT)
128 run_in_parallel(write_chunk_data, [[core, i] for i in range(TEST_THREAD_COUNT)])
129
130 print("Reading %i regions to RAM" % TEST_THREAD_COUNT)
131 chunk_read_data = [list() for i in range(TEST_THREAD_COUNT)]
132 run_in_parallel(read_chunk_data, [[core, i] for i in range(TEST_THREAD_COUNT)])
133
134 print("Comparing data")
135
136 for i in range(TEST_THREAD_COUNT):
137 test_count += 1
138 if same(chunk_read_data[i], chunk_data[i]):
139 test_pass_count += 1
140 print("Region %i PASSED" % i)
141 else:
142 print("Region %i FAILED" % i)
143
144 # Test with a multiple cores/APs.
145 # Disabled until cores each have their own memory map, the regions accessible to each
146 # core can be identified.
147 if False: # len(target.cores) > 1:
148 print("\n------ Test 2: Concurrent memory accesses, multiple cores ------")
149
150 cycle_count = ((len(target.cores) + TEST_THREAD_COUNT - 1) // TEST_THREAD_COUNT * TEST_THREAD_COUNT)
151 repeat_cores = ncycles(iter(target.cores), cycle_count)
152 thread_args = []
153 for i in range(TEST_THREAD_COUNT):
154 thread_args.append((target.cores[next(repeat_cores)], i))
155
156 # Write chunk patterns concurrently.
157 print("Writing %i regions to RAM" % TEST_THREAD_COUNT)
158 run_in_parallel(write_chunk_data, thread_args)
159
160 print("Reading %i regions to RAM" % TEST_THREAD_COUNT)
161 chunk_read_data = [list() for i in range(TEST_THREAD_COUNT)]
162 run_in_parallel(read_chunk_data, thread_args)
163
164 print("Comparing data")
165
166 for i in range(TEST_THREAD_COUNT):
167 test_count += 1
168 if same(chunk_read_data[i], chunk_data[i]):
169 test_pass_count += 1
170 print("Region %i PASSED" % i)
171 else:
172 print("Region %i FAILED" % i)
173
174 # --- end ---
175 print("\nTest Summary:")
176 print("Pass count %i of %i tests" % (test_pass_count, test_count))
177 if test_pass_count == test_count:
178 print("CONCURRENCY TEST PASSED")
179 else:
180 print("CONCURRENCY TEST FAILED")
181
182 target.reset()
183
184 result.passed = test_count == test_pass_count
185 return result
186
187 if __name__ == "__main__":
188 parser = argparse.ArgumentParser(description='pyOCD concurrency test')
189 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
190 parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
191 args = parser.parse_args()
192 level = logging.DEBUG if args.debug else logging.INFO
193 logging.basicConfig(level=level)
194 DAPAccess.set_args(args.daparg)
195 # Set to debug to print some of the decisions made while flashing
196 session = ConnectHelper.session_with_chosen_probe(**get_session_options())
197 test = ConcurrencyTest()
198 result = [test.run(session.board)]
199