winIDEA SDK
profiler_data.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5"""
6This script reads data recorded by profiler and prints it to stdout.
7It demonstrates usage of class CProfilerData2 and related classes.
8"""
9
10from __future__ import print_function
11
12import sys
13import isystem.connect as ic
14
15
16def printTimes(stats, timeType):
17 print(' total: ', stats.getTotalTime(timeType))
18 print(' min: ', stats.getMinTime(timeType))
19 print(' minStart:', stats.getMinStartTime(timeType))
20 print(' minEnd: ', stats.getMinEndTime(timeType))
21 print(' max: ', stats.getMaxTime(timeType))
22 print(' maxStart:', stats.getMaxStartTime(timeType))
23 print(' maxEnd: ', stats.getMaxEndTime(timeType))
24 print(' average: ', stats.getAverageTime(timeType))
25
26
27def printStatistics(stats):
28 print(' Statistics:')
29 print(' Id: ', hex(stats.getAreaId()))
30 print(' Handle: ', stats.getHandle())
31 print(' Name: ', stats.getAreaName())
32 print(' Value: ', stats.getAreaValue())
33 print(' Num hits: ', stats.getNumHits())
34 print(' Net times: ')
35 printTimes(stats, ic.CProfilerStatistics2.ENetTimes)
36 print(' Gross times:')
37 printTimes(stats, ic.CProfilerStatistics2.EGrossTimes)
38 print(' Call times:')
39 printTimes(stats, ic.CProfilerStatistics2.ECallTimes)
40 print(' Period:')
41 printTimes(stats, ic.CProfilerStatistics2.EPeriodTimes)
42 print(' Outside times:')
43 printTimes(stats, ic.CProfilerStatistics2.EOutsideTimes)
44 print(' Value statistics:')
45 print(' Average:', stats.getValueAverage())
46 print(' Min:', stats.getValueMin())
47 print(' Max:', stats.getValueMax())
48 print(' TimeOfMin:', stats.getValueTimeOfMin())
49 print(' TimeOfMax:', stats.getValueTimeOfMax())
50
51
52
53def printArea(area):
54 print('Area:')
55 print(' Id: ', hex(area.getAreaId()))
56 print(' Handle: ', area.getHandle())
57 print(' Path: ', area.getPath())
58 print(' Name: ', area.getAreaName())
59 print(' Value: ', area.getValue())
60 print(' Value type:', area.getValueType())
61 print(' Line num.: ', area.getLineNumber())
62 print(' Parent h.: ', area.getParentHandle())
63 print(' Parent a.: ', area.getParentAreaName())
64
65
66def printAreas(profilerData, areaType):
67
68 areaIterator = profilerData.getAreaIterator(areaType)
69
70 while areaIterator.hasNext():
71 area = areaIterator.next()
72 printArea(area)
73
74 if (profilerData.hasStatisticsForArea(area.getAreaId())):
75 stats = profilerData.getStatistics(area.getAreaId())
76 printStatistics(stats)
77 else:
78 print('No statistic for area ', hex(area.getAreaId()), 'found.')
79
80def uhex(val, bits):
81 return hex(val if val >= 0 else (1 << bits) + val)
82
83
84def printTimeEvent(timeEvent, valueType):
85 print('TimeEvent: ')
86 print(' handle: ', hex(timeEvent.getHandle()))
87 print(' evType: ', timeEvent.getEventType())
88
89 if valueType == 'F32':
90 print(' value F: ', timeEvent.getFloatValue())
91 elif valueType == 'I32' or valueType == 'I64':
92 print(' value I: ', uhex(timeEvent.getValue(), 64))
93 elif valueType == 'S32' or valueType == 'S64':
94 print(' value S: ', timeEvent.getSignedValue())
95 else:
96 raise Exception('Unknown area value type: ', valueType)
97
98 print(' time: ', timeEvent.getTime())
99 print(' eventSrc:', timeEvent.getEventSource())
100
101
102def iterateEvents(timeIter, profilerData):
103 counter = 0
104 while timeIter.hasNext():
105 timeEvent = timeIter.next()
106 area = profilerData.getArea(timeEvent.getHandle())
107 printTimeEvent(timeEvent, area.getValueType())
108 counter += 1
109 if counter > 30: # otherwise we'd have to wait to long
110 break
111
112profilerExportFile = '../../targetProjects/profilerSample-1.xml'
113
114if len(sys.argv) > 1:
115 profilerExportFile = sys.argv[1]
116
117profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
118
119# always check for parser warnings
120warnings = profilerData.getParserWarnings()
121if warnings:
122 print('WARNING(S): ', warnings)
123
124print('\nProfiler session time:', profilerData.getTotalSessionTimeNs())
125print('\n--------------- Function areas ---------------\n')
126printAreas(profilerData, ic.CProfilerArea2.EFunctions)
127
128print('\n--------------- Variable areas ---------------\n')
129printAreas(profilerData, ic.CProfilerArea2.EVariables)
130
131print('\n--------------- State Variable areas ---------------\n')
132printAreas(profilerData, ic.CProfilerArea2.EStateVariables)
133
134print('\n--------------- Inspector areas ---------------\n')
135printAreas(profilerData, ic.CProfilerArea2.EInspector)
136
137print('\n--------------- State Filter areas ---------------\n')
138printAreas(profilerData, ic.CProfilerArea2.EStateInspector)
139
140print('\n\n\n--------------- All time-line Events ---------------\n')
141timeIter = profilerData.getTimelineIterator()
142iterateEvents(timeIter, profilerData)
143
144print('\n\n\nExamples of getArea() method, if we know area Id:')
145area = profilerData.getArea(0)
146printArea(area)
147area = profilerData.getArea(0x20000000)
148printArea(area)
149area = profilerData.getArea(0x30000001)
150printArea(area)
151
152# This is much more common situation
153print('\n\n\nExamples of getArea() method, if we know area name:')
154area = profilerData.getArea(ic.CProfilerArea2.EFunctions, 'fibonacci')
155printArea(area)
156area = profilerData.getArea(ic.CProfilerArea2.EVariables, 'main_loop_counter')
157printArea(area)
158# print(area info for one state
159area = profilerData.getArea(ic.CProfilerArea2.EStateVariables, 'E_ONE')
160printArea(area)
161
162area = profilerData.getArea(ic.CProfilerArea2.EStateVariables, 'g_enumA', 0)
163printArea(area)
164
165contexts = ic.StrVector()
166profilerData.getContexts(contexts)
167print('\n\nAvailable contexts:\n', end=' ')
168for ctx in contexts:
169 print(ctx)
170
171print('\n\n\nExamples of getStatistics() methods:')
172
173# make a check if statistics exist
174if profilerData.hasStatisticsForArea(ic.CProfilerArea2.EFunctions, 'fibonacci'):
175 stats = profilerData.getStatistics(ic.CProfilerArea2.EFunctions, 'fibonacci')
176 printStatistics(stats)
177else:
178 print('No statistics for function fibonacci found.')
179
180# make a check if statistics exist
181if profilerData.hasStatisticsForFunction(0, 'Neutral'):
182 stats = profilerData.getStatisticsForFunction(0, 'Neutral')
183 printStatistics(stats)
184else:
185 print('No statistics for function with handle 0 and context Neutral found.')
186
187# make a check if statistics exist
188if (profilerData.hasStatisticsForFunction(ic.CProfilerArea2.EFunctions,
189 'fibonacci', 'Neutral')):
190 stats = profilerData.getStatisticsForFunction(ic.CProfilerArea2.EFunctions,
191 'fibonacci', 'Neutral')
192 printStatistics(stats)
193else:
194 print('No statistics for function named fibonacci with context Neutral found')
195
196stats = profilerData.getStatistics(ic.CProfilerArea2.EVariables, 'main_loop_counter')
197printStatistics(stats)
198
199stats = profilerData.getStatistics(ic.CProfilerArea2.EVariables, 'g_enumA')
200printStatistics(stats)
201
202# make a check if statistics exist
203if profilerData.hasStatisticsForStateArea(ic.CProfilerArea2.EStateVariables, 'g_enumA', 2):
204 stats = profilerData.getStatistics(ic.CProfilerArea2.EStateVariables, 'g_enumA', 2)
205 printStatistics(stats)
206else:
207 print('No statistics for state area g_enumA[2] found.')
208
209
210# print all events
211print('\n\n\nAll time-line Events:\n')
212timeIter = profilerData.getTimelineIterator()
213iterateEvents(timeIter, profilerData)
214profilerData.closeParser() # releases memory and closes XML file
215
216
217# Print all Write events. Since the time event iterator works on file,
218# we have to restart parsing by creating a new object.
219print('\n\n\nWrite Events:\n')
220profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
221timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite)
222iterateEvents(timeIter, profilerData)
223profilerData.closeParser() # releases memory and closes XML file
224
225
226# Print all Enter events for function 'fibonacci'. Since the time
227# event iterator works on file, we have to restart parsing by creating
228# a new object.
229print("\n\n\n'Enter' Events for function 'fibonacci':\n")
230profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
231typeSimpleArea = profilerData.getArea(ic.CProfilerArea2.EFunctions, 'fibonacci')
232timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvAny,
233 typeSimpleArea.getHandle())
234iterateEvents(timeIter, profilerData)
235
236profilerData.closeParser() # releases memory and closes XML file
237
238
239# Print all Enter events for variable 'main_loop_counter'. Since the time
240# event iterator works on file, we have to restart parsing by creating
241# a new object.
242print("\n\n\n'Write' Events for variable 'main_loop_counter':\n")
243profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
244iCounterArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'main_loop_counter')
245timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
246 iCounterArea.getHandle())
247iterateEvents(timeIter, profilerData)
248
249profilerData.closeParser() # releases memory and closes XML file
250
251
252# Print all Enter events for variable 'g_enumA', value 3. Since the time
253# event iterator works on file, we have to restart parsing by creating
254# a new object.
255print("\n\n\n'Write' Events for variable 'g_enumA', value 3:\n")
256profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
257stateArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
258timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
259 stateArea.getHandle(),
260 3)
261iterateEvents(timeIter, profilerData)
262
263profilerData.closeParser() # releases memory and closes XML file
264
265
266# Print all events for variable 'g_enumA', all values. Since the time
267# event iterator works on file, we have to restart parsing by creating
268# a new object.
269print("\n\n\nWrite Events for variable 'g_enumA', all values:\n")
270profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
271varArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
272timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
273 varArea.getHandle())
274iterateEvents(timeIter, profilerData)
275
276profilerData.closeParser() # releases memory and closes XML file
277
278
279print("\n\n\nParse data from binary export:")
280print("Write Events for variable 'g_enumA', all values:\n")
281profilerData = ic.CProfilerData2.createInstance('../../targetProjects/profilerSample-bin.xml', True)
282varArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
283timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
284 varArea.getHandle())
285iterateEvents(timeIter, profilerData)
286
287profilerData.closeParser() # releases memory and closes XML file
288print('Done!')