winIDEA SDK
Loading...
Searching...
No Matches
profiler_data.py
# This script is licensed under BSD License, see file LICENSE.txt, or search for `License` in the SDK online help.
#
# (c) TASKING Germany GmbH, 2023
"""
This script reads data recorded by profiler and prints it to stdout.
It demonstrates usage of class CProfilerData2 and related classes.
"""
from __future__ import print_function
import sys
import isystem.connect as ic
def printTimes(stats, timeType):
print(' total: ', stats.getTotalTime(timeType))
print(' min: ', stats.getMinTime(timeType))
print(' minStart:', stats.getMinStartTime(timeType))
print(' minEnd: ', stats.getMinEndTime(timeType))
print(' max: ', stats.getMaxTime(timeType))
print(' maxStart:', stats.getMaxStartTime(timeType))
print(' maxEnd: ', stats.getMaxEndTime(timeType))
print(' average: ', stats.getAverageTime(timeType))
def printStatistics(stats):
print(' Statistics:')
print(' Id: ', hex(stats.getAreaId()))
print(' Handle: ', stats.getHandle())
print(' Name: ', stats.getAreaName())
print(' Value: ', stats.getAreaValue())
print(' Num hits: ', stats.getNumHits())
print(' Net times: ')
printTimes(stats, ic.CProfilerStatistics2.ENetTimes)
print(' Gross times:')
printTimes(stats, ic.CProfilerStatistics2.EGrossTimes)
print(' Call times:')
printTimes(stats, ic.CProfilerStatistics2.ECallTimes)
print(' Period:')
printTimes(stats, ic.CProfilerStatistics2.EPeriodTimes)
print(' Outside times:')
printTimes(stats, ic.CProfilerStatistics2.EOutsideTimes)
print(' Value statistics:')
print(' Average:', stats.getValueAverage())
print(' Min:', stats.getValueMin())
print(' Max:', stats.getValueMax())
print(' TimeOfMin:', stats.getValueTimeOfMin())
print(' TimeOfMax:', stats.getValueTimeOfMax())
def printArea(area):
print('Area:')
print(' Id: ', hex(area.getAreaId()))
print(' Handle: ', area.getHandle())
print(' Path: ', area.getPath())
print(' Name: ', area.getAreaName())
print(' Value: ', area.getValue())
print(' Value type:', area.getValueType())
print(' Line num.: ', area.getLineNumber())
print(' Parent h.: ', area.getParentHandle())
print(' Parent a.: ', area.getParentAreaName())
def printAreas(profilerData, areaType):
areaIterator = profilerData.getAreaIterator(areaType)
while areaIterator.hasNext():
area = areaIterator.next()
printArea(area)
if (profilerData.hasStatisticsForArea(area.getAreaId())):
stats = profilerData.getStatistics(area.getAreaId())
printStatistics(stats)
else:
print('No statistic for area ', hex(area.getAreaId()), 'found.')
def uhex(val, bits):
return hex(val if val >= 0 else (1 << bits) + val)
def printTimeEvent(timeEvent, valueType):
print('TimeEvent: ')
print(' handle: ', hex(timeEvent.getHandle()))
print(' evType: ', timeEvent.getEventType())
if valueType == 'F32':
print(' value F: ', timeEvent.getFloatValue())
elif valueType == 'I32' or valueType == 'I64':
print(' value I: ', uhex(timeEvent.getValue(), 64))
elif valueType == 'S32' or valueType == 'S64':
print(' value S: ', timeEvent.getSignedValue())
else:
raise Exception('Unknown area value type: ', valueType)
print(' time: ', timeEvent.getTime())
print(' eventSrc:', timeEvent.getEventSource())
def iterateEvents(timeIter, profilerData):
counter = 0
while timeIter.hasNext():
timeEvent = timeIter.next()
area = profilerData.getArea(timeEvent.getHandle())
printTimeEvent(timeEvent, area.getValueType())
counter += 1
if counter > 30: # otherwise we'd have to wait to long
break
if __name__ == '__main__':
profilerExportFile = '../../targetProjects/profilerSample-1.xml'
if len(sys.argv) > 1:
profilerExportFile = sys.argv[1]
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
# always check for parser warnings
warnings = profilerData.getParserWarnings()
if warnings:
print('WARNING(S): ', warnings)
print('\nProfiler session time:', profilerData.getTotalSessionTimeNs())
print('\n--------------- Function areas ---------------\n')
printAreas(profilerData, ic.CProfilerArea2.EFunctions)
print('\n--------------- Variable areas ---------------\n')
printAreas(profilerData, ic.CProfilerArea2.EVariables)
print('\n--------------- State Variable areas ---------------\n')
printAreas(profilerData, ic.CProfilerArea2.EStateVariables)
print('\n--------------- Inspector areas ---------------\n')
printAreas(profilerData, ic.CProfilerArea2.EInspector)
print('\n--------------- State Filter areas ---------------\n')
printAreas(profilerData, ic.CProfilerArea2.EStateInspector)
print('\n\n\n--------------- All time-line Events ---------------\n')
timeIter = profilerData.getTimelineIterator()
iterateEvents(timeIter, profilerData)
print('\n\n\nExamples of getArea() method, if we know area Id:')
area = profilerData.getArea(0)
printArea(area)
area = profilerData.getArea(0x20000000)
printArea(area)
area = profilerData.getArea(0x30000001)
printArea(area)
# This is much more common situation
print('\n\n\nExamples of getArea() method, if we know area name:')
area = profilerData.getArea(ic.CProfilerArea2.EFunctions, 'fibonacci')
printArea(area)
area = profilerData.getArea(ic.CProfilerArea2.EVariables, 'main_loop_counter')
printArea(area)
# print(area info for one state
area = profilerData.getArea(ic.CProfilerArea2.EStateVariables, 'E_ONE')
printArea(area)
area = profilerData.getArea(ic.CProfilerArea2.EStateVariables, 'g_enumA', 0)
printArea(area)
contexts = ic.StrVector()
profilerData.getContexts(contexts)
print('\n\nAvailable contexts:\n', end=' ')
for ctx in contexts:
print(ctx)
print('\n\n\nExamples of getStatistics() methods:')
# make a check if statistics exist
if profilerData.hasStatisticsForArea(ic.CProfilerArea2.EFunctions, 'fibonacci'):
stats = profilerData.getStatistics(ic.CProfilerArea2.EFunctions, 'fibonacci')
printStatistics(stats)
else:
print('No statistics for function fibonacci found.')
# make a check if statistics exist
if profilerData.hasStatisticsForFunction(0, 'Neutral'):
stats = profilerData.getStatisticsForFunction(0, 'Neutral')
printStatistics(stats)
else:
print('No statistics for function with handle 0 and context Neutral found.')
# make a check if statistics exist
if (profilerData.hasStatisticsForFunction(ic.CProfilerArea2.EFunctions,
'fibonacci', 'Neutral')):
stats = profilerData.getStatisticsForFunction(ic.CProfilerArea2.EFunctions,
'fibonacci', 'Neutral')
printStatistics(stats)
else:
print('No statistics for function named fibonacci with context Neutral found')
stats = profilerData.getStatistics(ic.CProfilerArea2.EVariables, 'main_loop_counter')
printStatistics(stats)
stats = profilerData.getStatistics(ic.CProfilerArea2.EVariables, 'g_enumA')
printStatistics(stats)
# make a check if statistics exist
if profilerData.hasStatisticsForStateArea(ic.CProfilerArea2.EStateVariables, 'g_enumA', 2):
stats = profilerData.getStatistics(ic.CProfilerArea2.EStateVariables, 'g_enumA', 2)
printStatistics(stats)
else:
print('No statistics for state area g_enumA[2] found.')
# print all events
print('\n\n\nAll time-line Events:\n')
timeIter = profilerData.getTimelineIterator()
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
# Print all Write events. Since the time event iterator works on file,
# we have to restart parsing by creating a new object.
print('\n\n\nWrite Events:\n')
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite)
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
# Print all Enter events for function 'fibonacci'. Since the time
# event iterator works on file, we have to restart parsing by creating
# a new object.
print("\n\n\n'Enter' Events for function 'fibonacci':\n")
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
typeSimpleArea = profilerData.getArea(ic.CProfilerArea2.EFunctions, 'fibonacci')
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvAny,
typeSimpleArea.getHandle())
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
# Print all Enter events for variable 'main_loop_counter'. Since the time
# event iterator works on file, we have to restart parsing by creating
# a new object.
print("\n\n\n'Write' Events for variable 'main_loop_counter':\n")
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
iCounterArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'main_loop_counter')
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
iCounterArea.getHandle())
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
# Print all Enter events for variable 'g_enumA', value 3. Since the time
# event iterator works on file, we have to restart parsing by creating
# a new object.
print("\n\n\n'Write' Events for variable 'g_enumA', value 3:\n")
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
stateArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
stateArea.getHandle(),
3)
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
# Print all events for variable 'g_enumA', all values. Since the time
# event iterator works on file, we have to restart parsing by creating
# a new object.
print("\n\n\nWrite Events for variable 'g_enumA', all values:\n")
profilerData = ic.CProfilerData2.createInstance(profilerExportFile)
varArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
varArea.getHandle())
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
print("\n\n\nParse data from binary export:")
print("Write Events for variable 'g_enumA', all values:\n")
profilerData = ic.CProfilerData2.createInstance('../../targetProjects/profilerSample-bin.xml', True)
varArea = profilerData.getArea(ic.CProfilerArea2.EVariables, 'g_enumA')
timeIter = profilerData.getTimelineIterator(ic.CProfilerTimeEvent.EEvWrite,
varArea.getHandle())
iterateEvents(timeIter, profilerData)
profilerData.closeParser() # releases memory and closes XML file
print('Done!')
# This script is licensed under BSD License, see file LICENSE.txt.