IORunner.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: IORunner.cpp
  4. * Description: IORunner
  5. *
  6. * Runner implementation for runner running on device
  7. * under test
  8. *
  9. * $Date: 20. June 2019
  10. * $Revision: V1.0.0
  11. *
  12. * Target Processor: Cortex-M cores
  13. * -------------------------------------------------------------------- */
  14. /*
  15. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  16. *
  17. * SPDX-License-Identifier: Apache-2.0
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the License); you may
  20. * not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  27. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. */
  31. #include "Test.h"
  32. #include <string>
  33. #include <cstddef>
  34. #include <cstdlib>
  35. #include <cstdio>
  36. #include "IORunner.h"
  37. #include "Error.h"
  38. #include "Timing.h"
  39. #include "arm_math_types.h"
  40. #include "Calibrate.h"
  41. #ifdef CORTEXA
  42. #define CALIBNB 1
  43. #else
  44. #define CALIBNB 20
  45. #endif
  46. using namespace std;
  47. namespace Client
  48. {
  49. IORunner::IORunner(IO *io,PatternMgr *mgr, Testing::RunningMode runningMode):m_io(io), m_mgr(mgr)
  50. {
  51. volatile Testing::cycles_t current;
  52. this->m_runningMode = runningMode;
  53. // Set running mode on PatternMgr.
  54. if (runningMode == Testing::kDumpOnly)
  55. {
  56. mgr->setDumpMode();
  57. }
  58. if (runningMode == Testing::kTestAndDump)
  59. {
  60. mgr->setTestAndDumpMode();
  61. }
  62. initCycleMeasurement();
  63. /*
  64. For calibration :
  65. Calibration means, in this context, removing the overhad of calling
  66. a C++ function pointer from the cycle measurements.
  67. */
  68. Calibrate c((Testing::testID_t)0);
  69. Client::Suite *s=(Client::Suite *)&c;
  70. Client::test t = (Client::test)&Calibrate::empty;
  71. calibration = 0;
  72. /*
  73. EXTBENCH is set when benchmarking is done through external traces
  74. instead of using internal counters.
  75. Currently the post-processing scripts are only supporting traces generated from
  76. fast models.
  77. */
  78. #ifdef EXTBENCH
  79. startSection();
  80. #endif
  81. /*
  82. For calibration, we measure the time it takes to call 20 times an empty benchmark and compute
  83. the average.
  84. (20 is an arbitrary value.)
  85. This overhead is removed from benchmarks in the Runner..
  86. Calibration is removed from the python script when external trace is used for the cycles.
  87. Indeed, in that case the calibration value can only be measured by parsing the trace.
  88. Otherwise, the calibration is measured below.
  89. */
  90. /*
  91. We want to ensure that the calibration of the overhead of the
  92. measurement is the same here and when we do the measurement later.
  93. So to ensure the conditions are always the same, the instruction cache
  94. and branch predictor are flushed.
  95. */
  96. #ifdef CORTEXA
  97. __set_BPIALL(0);
  98. __DSB();
  99. __ISB();
  100. __set_ICIALLU(0);
  101. __DSB();
  102. __ISB();
  103. #endif
  104. /*
  105. We always call the empty function once to ensure it is in the cache
  106. because it is how the measurement is done.
  107. */
  108. if (!m_mgr->HasMemError())
  109. {
  110. (s->*t)();
  111. }
  112. /*
  113. We measure the cycles required for a measurement,
  114. The cycleMeasurement starts, getCycles and cycleMeasurementStop
  115. should not be in the cache.
  116. So, for the overhead we always have the value corresponding to
  117. the code not in cache.
  118. While for the code itself we have the value for the code in cache.
  119. */
  120. for(int i=0;i < CALIBNB;i++)
  121. {
  122. cycleMeasurementStart();
  123. if (!m_mgr->HasMemError())
  124. {
  125. (s->*t)();
  126. }
  127. #ifndef EXTBENCH
  128. current = getCycles();
  129. #endif
  130. calibration += current;
  131. cycleMeasurementStop();
  132. }
  133. #ifdef EXTBENCH
  134. stopSection();
  135. #endif
  136. #ifndef EXTBENCH
  137. calibration=calibration / CALIBNB;
  138. #endif
  139. }
  140. // Testing.
  141. // When false we are in dump mode and the failed assertion are ignored
  142. // (But exception is taken so assert should be at end of the test and not in the
  143. // middle )
  144. IORunner::IORunner(IO *io,PatternMgr *mgr):m_io(io), m_mgr(mgr)
  145. {
  146. this->m_runningMode = Testing::kTestOnly;
  147. }
  148. IORunner::~IORunner()
  149. {
  150. }
  151. /** Read driver data to control execution of a suite
  152. */
  153. Testing::TestStatus IORunner::run(Suite *s)
  154. {
  155. Testing::TestStatus finalResult = Testing::kTestPassed;
  156. int nbTests = s->getNbTests();
  157. int failedTests=0;
  158. Testing::errorID_t error=0;
  159. unsigned long line = 0;
  160. char details[200];
  161. volatile Testing::cycles_t cycles=0;
  162. Testing::nbParameters_t nbParams;
  163. // Read node identification (suite)
  164. m_io->ReadIdentification();
  165. // Read suite nb of parameters
  166. nbParams = m_io->ReadNbParameters();
  167. // Read list of patterns
  168. m_io->ReadPatternList();
  169. // Read list of output
  170. m_io->ReadOutputList();
  171. // Read list of parameters
  172. m_io->ReadParameterList(nbParams);
  173. // Iterate on tests
  174. for(int i=1; i <= nbTests; i++)
  175. {
  176. test t = s->getTest(i);
  177. Testing::TestStatus result = Testing::kTestPassed;
  178. error = UNKNOWN_ERROR;
  179. line = 0;
  180. cycles = 0;
  181. details[0]='\0';
  182. Testing::param_t *paramData=NULL;
  183. Testing::nbParameterEntries_t entries=0;
  184. std::vector<Testing::param_t> params(nbParams);
  185. bool canExecute=true;
  186. unsigned long dataIndex=0;
  187. Testing::ParameterKind paramKind;
  188. // Read test identification (test ID)
  189. m_io->ReadTestIdentification();
  190. if (m_io->hasParam())
  191. {
  192. Testing::PatternID_t paramID=m_io->getParamID();
  193. paramData = m_io->ImportParams(paramID,entries,paramKind);
  194. dataIndex = 0;
  195. }
  196. while(canExecute)
  197. {
  198. canExecute = false;
  199. if (m_io->hasParam() && paramData)
  200. {
  201. // Load new params
  202. for(unsigned long j=0; j < nbParams ; j++)
  203. {
  204. params[j] = paramData[nbParams*dataIndex+j];
  205. }
  206. // Update condition for new execution
  207. dataIndex += 1;
  208. canExecute = dataIndex < entries;
  209. }
  210. // Execute test
  211. try {
  212. // Prepare memory for test
  213. // setUp will generally load patterns
  214. // and do specific initialization for the tests
  215. s->setUp(m_io->CurrentTestID(),params,m_mgr);
  216. // Run the test once to force the code to be in cache.
  217. // By default it is disabled in the suite.
  218. #ifdef CORTEXA
  219. __set_BPIALL(0);
  220. __DSB();
  221. __ISB();
  222. __set_ICIALLU(0);
  223. __DSB();
  224. __ISB();
  225. #endif
  226. if (s->isForcedInCache())
  227. {
  228. if (!m_mgr->HasMemError())
  229. {
  230. (s->*t)();
  231. }
  232. }
  233. // Run the test
  234. cycleMeasurementStart();
  235. #ifdef EXTBENCH
  236. startSection();
  237. #endif
  238. if (!m_mgr->HasMemError())
  239. {
  240. (s->*t)();
  241. }
  242. #ifdef EXTBENCH
  243. stopSection();
  244. #endif
  245. #ifndef EXTBENCH
  246. cycles=getCycles();
  247. cycles=cycles-calibration;
  248. #endif
  249. cycleMeasurementStop();
  250. }
  251. catch(Error &ex)
  252. {
  253. cycleMeasurementStop();
  254. // In dump only mode we ignore the tests
  255. // since the reference patterns are not loaded
  256. // so tests will fail.
  257. if (this->m_runningMode != Testing::kDumpOnly)
  258. {
  259. error = ex.errorID;
  260. line = ex.lineNumber;
  261. strcpy(details,ex.details);
  262. result=Testing::kTestFailed;
  263. }
  264. }
  265. catch (...) {
  266. cycleMeasurementStop();
  267. // In dump only mode we ignore the tests
  268. // since the reference patterns are not loaded
  269. // so tests will fail.
  270. if (this->m_runningMode != Testing::kDumpOnly)
  271. {
  272. result = Testing::kTestFailed;
  273. error = UNKNOWN_ERROR;
  274. line = 0;
  275. }
  276. }
  277. try {
  278. // Clean memory after this test
  279. // May dump output and do specific cleaning for a test
  280. s->tearDown(m_io->CurrentTestID(),m_mgr);
  281. }
  282. catch(...)
  283. {
  284. }
  285. if (m_mgr->HasMemError())
  286. {
  287. /* We keep the current error if set.
  288. */
  289. if (result == Testing::kTestPassed)
  290. {
  291. result = Testing::kTestFailed;
  292. error = MEMORY_ALLOCATION_ERROR;
  293. line = 0;
  294. }
  295. }
  296. // Free all memory of memory manager so that next test
  297. // is starting in a clean and controlled tests
  298. m_mgr->freeAll();
  299. // Dump test status to output
  300. m_io->DispStatus(result,error,line,cycles);
  301. m_io->DispErrorDetails(details);
  302. m_io->DumpParams(params);
  303. }
  304. if (paramData)
  305. {
  306. if (paramKind == Testing::kDynamicBuffer)
  307. {
  308. free(paramData);
  309. }
  310. paramData = NULL;
  311. }
  312. if (result == Testing::kTestFailed)
  313. {
  314. failedTests ++;
  315. finalResult = Testing::kTestFailed;
  316. }
  317. }
  318. // Signal end of group processing to output
  319. m_io->EndGroup();
  320. return(finalResult);
  321. }
  322. /** Read driver data to control execution of a group
  323. */
  324. Testing::TestStatus IORunner::run(Group *g)
  325. {
  326. int nbTests = g->getNbContainer();
  327. int failedTests=0;
  328. // Read Node identification
  329. m_io->ReadIdentification();
  330. Testing::TestStatus finalResult = Testing::kTestPassed;
  331. // Iterate on group elements
  332. for(int i=1; i <= nbTests; i++)
  333. {
  334. TestContainer *c = g->getContainer(i);
  335. if (c != NULL)
  336. {
  337. // Execute runner for this group
  338. Testing::TestStatus result = c->accept(this);
  339. if (result == Testing::kTestFailed)
  340. {
  341. failedTests ++;
  342. finalResult = Testing::kTestFailed;
  343. }
  344. }
  345. }
  346. // Signal to output that processing of this group has finished.
  347. m_io->EndGroup();
  348. return(finalResult);
  349. }
  350. }