1""" NNAPI systrace parser: naming conventions and translations """
2
3
4# Application phases
5PHASE_OVERALL = "PO"      # Overall program, e.g., one benchmark case
6PHASE_WARMUP = "PWU"      # Executions done for warmup
7PHASE_BENCHMARK = "PBM"   # Executions done to benchmark after warmup
8# Main phases
9PHASE_INITIALIZATION = "PI" # Initialization - not related to a model
10PHASE_PREPARATION = "PP"  # Model construction
11PHASE_COMPILATION = "PC"  # Model compilation
12PHASE_EXECUTION = "PE"    # Executing the model
13PHASE_TERMINATION = "PT"  # Tearing down
14PHASE_UNSPECIFIED = "PU"  # Helper code called from multiple phases
15# Subphases of execution
16PHASE_INPUTS_AND_OUTPUTS = "PIO"  # Setting inputs/outputs and allocating buffers
17PHASE_TRANSFORMATION = "PTR"      # Transforming data for computation
18PHASE_COMPUTATION = "PCO"         # Computing operations' outputs
19PHASE_RESULTS = "PR"              # Reading out results
20# Layers
21LAYER_APPLICATION = "LA"
22LAYER_RUNTIME = "LR"
23LAYER_IPC = "LI"
24LAYER_DRIVER = "LD"
25LAYER_CPU = "LC"
26LAYER_OTHER = "LO"
27LAYER_UTILITY = "LU"              # Code used from multiple layers
28LAYER_IGNORE = "LX"               # Don't count time
29# Markers
30MARKER_SWITCH = "[SW]"
31MARKER_SUBTRACT = "[SUB]"
32
33layers = [LAYER_APPLICATION, LAYER_RUNTIME, LAYER_IPC, LAYER_DRIVER, LAYER_CPU]
34phases = [PHASE_INITIALIZATION, PHASE_PREPARATION, PHASE_COMPILATION,
35          PHASE_EXECUTION, PHASE_TERMINATION]
36subphases = dict(PE=[PHASE_INPUTS_AND_OUTPUTS, PHASE_TRANSFORMATION,
37                     PHASE_COMPUTATION, PHASE_RESULTS],
38                 PO=([PHASE_WARMUP, PHASE_BENCHMARK] + phases),
39                 PWU=[PHASE_EXECUTION],
40                 PBM=[PHASE_EXECUTION])
41names = { PHASE_INITIALIZATION : "Initialization", PHASE_PREPARATION : "Preparation",
42          PHASE_COMPILATION : "Compilation", PHASE_INPUTS_AND_OUTPUTS: "I/O",
43          PHASE_EXECUTION: "Execution", PHASE_RESULTS: "Results",
44          PHASE_WARMUP: "Warmup", PHASE_BENCHMARK: "Benchmark",
45          PHASE_TERMINATION: "Termination",
46          LAYER_APPLICATION : "Application", LAYER_RUNTIME: "Runtime",
47          LAYER_IPC: "IPC", LAYER_DRIVER: "Driver", LAYER_CPU: "CPU",
48          "total": "Total", "NONE": "" }
49layer_order = { LAYER_APPLICATION: [LAYER_RUNTIME],
50                LAYER_RUNTIME: [LAYER_IPC, LAYER_CPU],
51                LAYER_IPC: [LAYER_DRIVER] }
52
53
54def make_tag(layer, phase):
55  return "_".join([layer, phase])
56
57def translate_hidl_mark_to_nn_and_tag(mark):
58  layer = None
59  if "::client" in mark:
60    if "notify" in mark:
61      # Call "up" into runtime from IPC. Ignore for now to not double-count
62      layer = LAYER_IGNORE
63    else:
64      # Call "down" into IPC
65      layer = LAYER_IPC
66  elif "::server" in mark:
67    if "notify" in mark:
68      # Call "up" into IPC. Ignore for now to not double-count
69      layer = LAYER_IGNORE
70    else:
71      # Call "down" in to driver
72      layer = LAYER_DRIVER
73  elif "::passthrough" in mark:
74    # In process hidl call, treating it as IPC
75    layer = LAYER_IPC
76  elif ("getCapabilities" in mark) or ("getSupportedOperations" in mark):
77    layer = LAYER_DRIVER
78  elif "HIDL" not in mark:
79    layer = LAYER_IGNORE
80  assert layer, mark
81
82  phase = PHASE_INITIALIZATION
83  if "IAllocator" in mark:
84    # Used in both preparation and execution phases
85    phase = "PU"
86  elif ("getCapabilities" in mark):
87    phase = PHASE_INITIALIZATION
88  elif ("prepareModel" in mark) or ("getSupportedOperations" in mark):
89    phase = PHASE_COMPILATION
90  elif ("IPreparedModelCallback") in mark:
91    phase = PHASE_COMPILATION
92  elif ("execute" in mark) or ("IExecutionCallback" in mark):
93    phase = PHASE_EXECUTION
94
95  return "NN_" + make_tag(layer, phase)
96
97def get_function_name_from_mark(mark):
98  function = mark.split("|")[2]
99  if "[NN_" in function:
100    function = function.replace(MARKER_SUBTRACT, "")
101    function = function.replace(MARKER_SWITCH, "")
102    function = function.split("]")[1]
103    function = function.replace("[", "")
104    function = function.replace("]", "")
105  return function
106