18import isystem.connect
as ic
25 isPyLabInstalled =
True
26except ImportError
as ex:
27 isPyLabInstalled =
False
32 This function initializes the target. Customize it according to
36 debugCtrl = ic.CDebugFacade(cmgr)
39 debugCtrl.runUntilFunction("main")
40 debugCtrl.waitUntilStopped()
47def recordWatchExpressions(debugCtrl,
49 recordingTimeInSeconds,
55 This function evaluates watch expressions and returns list of results. The
56 amount of data that can be recorded
is limited by the amount of system
61 debugCtrl - iSYSTEM
's debugCtrl controller, which provides access to target
63 watches - list of strings with watch expressions,
for example
64 [
'main_loop_counter,h',
'g_arrayInt[0]']
66 recordingTime - how long to record the expressions,
in
67 seconds. Recording can also be terminated by
70 samplingInterval - defines how much time should
pass between samples
72 isPrintToStdOut -
if True, each row
is printed to stdout during recording
75 List of rows, where each row
is a list containing time stamp
and recorded
76 values
in the same order
as watch expressions were specified. Example
for
83 startTime = time.time()
84 endTime = startTime + recordingTimeInSeconds
87 print([
'Time'] + watches)
92 while (time.time() < endTime):
94 currentTime = time.time() - startTime
97 for expression
in watches:
99 value = debugCtrl.evaluate(ic.IConnectDebug.fRealTime, expression)
103 row.append(value.getResult())
109 recordedData.append(row)
112 nextSamplingTime = startTime + samplingInterval * sampleCounter
113 sleepTime = nextSamplingTime - time.time()
115 time.sleep(sleepTime)
120def recordWatchExpressionsToCSV(debugCtrl,
122 recordingTimeInSeconds,
128 This function evaluates watch expressions and writes them to CSV file.
129 If isRecordToMemory ==
False, the amount of data that can be recorded
is
130 limited by the free disk size.
134 debugCtrl - iSYSTEM
's debugCtrl controller, which provides access to target
136 watches - list of strings with watch expressions,
for example
137 [
'main_loop_counter,h',
'g_arrayInt[0]']
139 recordingTime - how long to record the expressions,
in
140 seconds. Recording can also be terminated by
143 samplingInterval - defines how much time should
pass between samples
145 fileName - name of the output CSV file.
147 isRecordToMemory -
if True, then data
is also stored into memory
148 array
and returned
as function
return value. Be
149 aware of memory usage
in this case. If false,
150 an empty list
is returned.
152 isPrintToStdOut -
if True, each row
is printed to stdout during recording
155 List of rows, where each row
is a list containing time stamp
and recorded
156 values
in the same order
as watch expressions were specified. Example
for
159 [0.1, 24, -525, 1.78]
162 If isRecordToMemory ==
False, an empty list
is returned.
165 startTime = time.time()
166 endTime = startTime + recordingTimeInSeconds
170 with open(fileName,
'w')
as csvFile:
175 csvWriter = csv.writer(csvFile)
177 header = [
'Time'] + watches
179 csvWriter.writerow(header)
182 while (time.time() < endTime):
184 currentTime = time.time() - startTime
187 for expression
in watches:
189 print(
'expression: ', expression)
190 value = debugCtrl.evaluate(ic.IConnectDebug.fRealTime, expression)
194 row.append(value.getResult())
196 csvWriter.writerow(row)
202 recordedData.append(row)
205 nextSamplingTime = startTime + samplingInterval * sampleCounter
206 sleepTime = nextSamplingTime - time.time()
208 time.sleep(sleepTime)
216 recordingTimeInSeconds,
217 samplingIntervalInSeconds):
219 This function runs batch access and returns recorded data
as a list of
220 SBatchAccessItemResult structures.
221 The amount of data that can be recorded
is limited by the amount of system
226 cmgr - iSYSTEM
's connection manager, which provides connection to target
227 debugCtrl - iSYSTEM's debugCtrl controller, which provides access to target
229 variables - list of strings with variables,
for example
230 [
'main_loop_counter',
'g_baseStruct.i_base']
232 recordingTime - how long to record the expressions,
in
235 samplingInterval - defines how much time should
pass between samples
238 A list of SBatchAccessItemResult structures, one structure per variable
242 numItems = len(variables)
243 numRuns = int(recordingTimeInSeconds / samplingIntervalInSeconds)
248 header = ic.SBatchAccessHeader()
249 header.m_dwFlags = (ic.SBatchAccessHeader.flRealTime |
250 ic.SBatchAccessHeader.flWantTimeStamp)
251 header.m_dwNumItems = numItems
252 header.m_dwNumRuns = numRuns
253 header.m_qwStartAtTime = 0
254 header.m_qwRunInterval = int(samplingIntervalInSeconds * 1000000)
256 ba_items = ic.VectorBatchAccessItem()
259 for var
in variables:
260 item = ic.SBatchAccessItem()
262 varInfo = dbgCtrl.getSymbolInfo(ic.IConnectDebug.fRealTime, var)
265 item.m_byFlags = ic.SBatchAccessItem.flRead
266 item.m_bySize = varInfo.getSizeMAUs()
267 item.m_byMemArea = varInfo.getMemArea()
268 item.m_aAddress = varInfo.getAddress()
269 ba_items.push_back(item)
270 itemSizes.append(varInfo.getSizeMAUs())
273 ba_results = ic.VectorBatchAccessResult(numItems * numRuns)
274 dataCtrl = ic.CDataController(cmgr)
277 dataCtrl.batchAccess(0, header, ba_items, ba_results)
279 return ba_results, itemSizes
282def bigEndian2Int(numBytes, cArray):
283 """ Converts big-endian seq. of bytes to integer. """
286 for i
in range(numBytes):
288 value |= ic.CDataController.getByte(cArray, i)
293def batchAccessResultToCSV(fileName, ba_results, itemSizes, variables):
295 Writes results of batchAccess() to CSV file. Uses big endian conversion.
298 fileName - then name of the output file
299 ba_results - results returned by function recordBatch()
300 itemSizes - results returned by function recordBatch()
303 numItems = len(itemSizes)
304 numRuns = int(ba_results.size() / numItems)
306 firstAccess = ba_results[0].m_qwTimeStamp
308 with open(fileName,
'w')
as csvFile:
310 csvWriter = csv.writer(csvFile)
313 csvWriter.writerow([
'Time'] + variables)
315 for runIdx
in range(numRuns):
317 row = [(ba_results[runIdx * numItems].m_qwTimeStamp - firstAccess)/1000000.]
319 for varIdx
in range(numItems):
320 result = ba_results[runIdx * numItems + varIdx]
322 if (result.m_byResult == ic.SBatchAccessItemResult.resOK):
323 value = bigEndian2Int(itemSizes[varIdx], result.m_abyData)
326 elif (result.m_byResult == ic.SBatchAccessItemResult.resAccess):
327 row.append(
'Can not access. Check access flags in header!')
328 elif (result.m_byResult == ic.SBatchAccessItemResult.resTimeout):
329 row.append(
'Timeout! Probably the sampling rate is invalid!')
331 row.append(
'Invalid error code: ' + str(result.m_byResult))
333 csvWriter.writerow(row)
336def batchAccessResultToList(ba_results, itemSizes):
338 Converts results of batchAccess() to Python list. Uses big endian conversion.
341 fileName - then name of the output file
342 ba_results - results returned by function recordBatch()
343 itemSizes - results returned by function recordBatch()
346 numItems = len(itemSizes)
347 numRuns = int(ba_results.size() / numItems)
349 firstAccess = ba_results[0].m_qwTimeStamp
352 for runIdx
in range(numRuns):
354 row = [(ba_results[runIdx * numItems].m_qwTimeStamp - firstAccess)/1000000.]
356 for varIdx
in range(numItems):
357 result = ba_results[runIdx * numItems + varIdx]
359 if (result.m_byResult == ic.SBatchAccessItemResult.resOK):
360 value = bigEndian2Int(itemSizes[varIdx], result.m_abyData)
363 elif (result.m_byResult == ic.SBatchAccessItemResult.resAccess):
364 row.append(
'Can not access. Check access flags in header!')
365 elif (result.m_byResult == ic.SBatchAccessItemResult.resTimeout):
366 row.append(
'Timeout! Probably the sampling rate is invalid!')
368 row.append(
'Invalid error code: ' + str(result.m_byResult))
370 recordedData.append(row)
375def writeDatatoCSV(fileName, data, expressions):
376 with open(fileName,
'w', neline=
'')
as csvFile:
377 csvWriter = csv.writer(csvFile)
379 csvWriter.writerow([
'Time'] + expressions)
382 csvWriter.writerow(row)
386 cmgr = ic.ConnectionMgr()
387 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
389 debugCtrl = initTarget(cmgr)
390 ideCtrl = ic.CIDEController(cmgr)
393 ideCtrl.setOption(
'/IDE/Debug.Symbols.Format.ANSI',
True)
399 watches = [
'main_loop_counter,h',
'g_intArray1[0],d',
'g_complexStruct.m_struct.i_base,d']
400 recordingTimeInSeconds = 5
401 samplingIntervalInSeconds = 0.2
402 filePrefix =
'watches'
403 isRecordToMemory =
True
404 isPrintToStdOut =
True
405 print(
'Recording watch expressions:')
406 watchResults = recordWatchExpressionsToCSV(debugCtrl,
408 recordingTimeInSeconds,
409 samplingIntervalInSeconds,
416 variables = [
'main_loop_counter',
'g_intArray1[0]',
'g_complexStruct.m_struct.i_base']
418 debugCtrl.modify(ic.IConnectDebug.fRealTime,
'main_loop_counter',
'0')
419 samplingIntervalInSeconds = 0.5
421 print(
'Recording with batch access...')
422 batchResults, itemSizes = recordBatch(cmgr,
425 recordingTimeInSeconds,
426 samplingIntervalInSeconds)
428 batchAccessResultToCSV(filePrefix +
'.csv', batchResults,
429 itemSizes, variables)
431 batchData = batchAccessResultToList(batchResults, itemSizes)
434 data = pl.array(batchData)
437 pl.plot(times, data[:, 1])
438 pl.ylabel(
'main_loop_counter')
441 pl.plot(times, data[:, 2],
'g')
442 pl.ylabel(
'g_intArray1[0]')
445 pl.plot(times, data[:, 3],
'r')
446 pl.ylabel(
'g_complexStruct.m_struct.i_base')
454if __name__ ==
'__main__':