Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf scripts python: Add classes and conversion functions

This commit introduces new classes and conversion functions to
facilitate the representation of Gecko profile information. The new
classes Frame, Stack, Sample, and Thread are added to handle specific
components of the profile data, also link to the origin docs has been
commented out.

Additionally, Inside the Thread class _to_json_dict() method has been
created that converts the current thread data into the corresponding
format expected by the GeckoThread JSON schema, as per the Gecko
profile format specification.

Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/ab7b40bd32df7101a6f8b4a3aa41570b63b831ac.1689961706.git.anupnewsmail@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Anup Sharma and committed by
Arnaldo Carvalho de Melo
5aacd7f0 0a02e44c

+132 -1
+132 -1
tools/perf/scripts/python/gecko.py
··· 11 11 12 12 import os 13 13 import sys 14 - from typing import Dict 14 + from dataclasses import dataclass, field 15 + from typing import List, Dict, Optional, NamedTuple, Set, Tuple, Any 15 16 16 17 # Add the Perf-Trace-Util library to the Python path 17 18 sys.path.append(os.environ['PERF_EXEC_PATH'] + \ ··· 21 20 from perf_trace_context import * 22 21 from Core import * 23 22 23 + StringID = int 24 + StackID = int 25 + FrameID = int 26 + CategoryID = int 27 + Milliseconds = float 28 + 24 29 # start_time is intialiazed only once for the all event traces. 25 30 start_time = None 31 + 32 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156 33 + class Frame(NamedTuple): 34 + string_id: StringID 35 + relevantForJS: bool 36 + innerWindowID: int 37 + implementation: None 38 + optimizations: None 39 + line: None 40 + column: None 41 + category: CategoryID 42 + subcategory: int 43 + 44 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L216 45 + class Stack(NamedTuple): 46 + prefix_id: Optional[StackID] 47 + frame_id: FrameID 48 + 49 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L90 50 + class Sample(NamedTuple): 51 + stack_id: Optional[StackID] 52 + time_ms: Milliseconds 53 + responsiveness: int 54 + 55 + @dataclass 56 + class Thread: 57 + """A builder for a profile of the thread. 58 + 59 + Attributes: 60 + comm: Thread command-line (name). 61 + pid: process ID of containing process. 62 + tid: thread ID. 63 + samples: Timeline of profile samples. 64 + frameTable: interned stack frame ID -> stack frame. 65 + stringTable: interned string ID -> string. 66 + stringMap: interned string -> string ID. 67 + stackTable: interned stack ID -> stack. 68 + stackMap: (stack prefix ID, leaf stack frame ID) -> interned Stack ID. 69 + frameMap: Stack Frame string -> interned Frame ID. 70 + comm: str 71 + pid: int 72 + tid: int 73 + samples: List[Sample] = field(default_factory=list) 74 + frameTable: List[Frame] = field(default_factory=list) 75 + stringTable: List[str] = field(default_factory=list) 76 + stringMap: Dict[str, int] = field(default_factory=dict) 77 + stackTable: List[Stack] = field(default_factory=list) 78 + stackMap: Dict[Tuple[Optional[int], int], int] = field(default_factory=dict) 79 + frameMap: Dict[str, int] = field(default_factory=dict) 80 + """ 81 + comm: str 82 + pid: int 83 + tid: int 84 + samples: List[Sample] = field(default_factory=list) 85 + frameTable: List[Frame] = field(default_factory=list) 86 + stringTable: List[str] = field(default_factory=list) 87 + stringMap: Dict[str, int] = field(default_factory=dict) 88 + stackTable: List[Stack] = field(default_factory=list) 89 + stackMap: Dict[Tuple[Optional[int], int], int] = field(default_factory=dict) 90 + frameMap: Dict[str, int] = field(default_factory=dict) 91 + 92 + def _to_json_dict(self) -> Dict: 93 + """Converts current Thread to GeckoThread JSON format.""" 94 + # Gecko profile format is row-oriented data as List[List], 95 + # And a schema for interpreting each index. 96 + # Schema: 97 + # https://github.com/firefox-devtools/profiler/blob/main/docs-developer/gecko-profile-format.md 98 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L230 99 + return { 100 + "tid": self.tid, 101 + "pid": self.pid, 102 + "name": self.comm, 103 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L51 104 + "markers": { 105 + "schema": { 106 + "name": 0, 107 + "startTime": 1, 108 + "endTime": 2, 109 + "phase": 3, 110 + "category": 4, 111 + "data": 5, 112 + }, 113 + "data": [], 114 + }, 115 + 116 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L90 117 + "samples": { 118 + "schema": { 119 + "stack": 0, 120 + "time": 1, 121 + "responsiveness": 2, 122 + }, 123 + "data": self.samples 124 + }, 125 + 126 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156 127 + "frameTable": { 128 + "schema": { 129 + "location": 0, 130 + "relevantForJS": 1, 131 + "innerWindowID": 2, 132 + "implementation": 3, 133 + "optimizations": 4, 134 + "line": 5, 135 + "column": 6, 136 + "category": 7, 137 + "subcategory": 8, 138 + }, 139 + "data": self.frameTable, 140 + }, 141 + 142 + # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L216 143 + "stackTable": { 144 + "schema": { 145 + "prefix": 0, 146 + "frame": 1, 147 + }, 148 + "data": self.stackTable, 149 + }, 150 + "stringTable": self.stringTable, 151 + "registerTime": 0, 152 + "unregisterTime": None, 153 + "processType": "default", 154 + } 26 155 27 156 # Uses perf script python interface to parse each 28 157 # event and store the data in the thread builder.