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: Implement add sample function and thread processing

The intern_stack function is responsible for retrieving
or creating a stack_id based on the provided frame_id and prefix_id.
It first generates a key using the frame_id and prefix_id values.
If the stack corresponding to the key is found in the stackMap,
it is returned. Otherwise, a new stack is created by appending
the prefix_id and frame_id to the stackTable. The key
and the index of the newly created stack are added to the
stackMap for future reference.

The _intern_frame function is responsible for retrieving or
creating a frame_id based on the provided frame string. If the frame_id
corresponding to the frameString is found in the frameMap, it is
returned. Otherwise, a new frame is created by appending relevant
information to the frameTable and adding the frameString to the string_id
through _intern_string.

The _intern_string function will gets a matching string, or saves the new
string and returns a String ID.

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

authored by

Anup Sharma and committed by
Arnaldo Carvalho de Melo
258dfd41 833daec7

+54
+54
tools/perf/scripts/python/gecko.py
··· 13 13 import sys 14 14 import json 15 15 import argparse 16 + from functools import reduce 16 17 from dataclasses import dataclass, field 17 18 from typing import List, Dict, Optional, NamedTuple, Set, Tuple, Any 18 19 ··· 39 38 40 39 # The product name is used by the profiler UI to show the Operating system and Processor. 41 40 PRODUCT = os.popen('uname -op').read().strip() 41 + 42 + # The category index is used by the profiler UI to show the color of the flame graph. 43 + USER_CATEGORY_INDEX = 0 44 + KERNEL_CATEGORY_INDEX = 1 42 45 43 46 # https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156 44 47 class Frame(NamedTuple): ··· 103 98 stackTable: List[Stack] = field(default_factory=list) 104 99 stackMap: Dict[Tuple[Optional[int], int], int] = field(default_factory=dict) 105 100 frameMap: Dict[str, int] = field(default_factory=dict) 101 + 102 + def _intern_stack(self, frame_id: int, prefix_id: Optional[int]) -> int: 103 + """Gets a matching stack, or saves the new stack. Returns a Stack ID.""" 104 + key = f"{frame_id}" if prefix_id is None else f"{frame_id},{prefix_id}" 105 + # key = (prefix_id, frame_id) 106 + stack_id = self.stackMap.get(key) 107 + if stack_id is None: 108 + # return stack_id 109 + stack_id = len(self.stackTable) 110 + self.stackTable.append(Stack(prefix_id=prefix_id, frame_id=frame_id)) 111 + self.stackMap[key] = stack_id 112 + return stack_id 113 + 114 + def _intern_string(self, string: str) -> int: 115 + """Gets a matching string, or saves the new string. Returns a String ID.""" 116 + string_id = self.stringMap.get(string) 117 + if string_id is not None: 118 + return string_id 119 + string_id = len(self.stringTable) 120 + self.stringTable.append(string) 121 + self.stringMap[string] = string_id 122 + return string_id 123 + 124 + def _intern_frame(self, frame_str: str) -> int: 125 + """Gets a matching stack frame, or saves the new frame. Returns a Frame ID.""" 126 + frame_id = self.frameMap.get(frame_str) 127 + if frame_id is not None: 128 + return frame_id 129 + frame_id = len(self.frameTable) 130 + self.frameMap[frame_str] = frame_id 131 + string_id = self._intern_string(frame_str) 132 + 133 + symbol_name_to_category = KERNEL_CATEGORY_INDEX if frame_str.find('kallsyms') != -1 \ 134 + or frame_str.find('/vmlinux') != -1 \ 135 + or frame_str.endswith('.ko)') \ 136 + else USER_CATEGORY_INDEX 137 + 138 + self.frameTable.append(Frame( 139 + string_id=string_id, 140 + relevantForJS=False, 141 + innerWindowID=0, 142 + implementation=None, 143 + optimizations=None, 144 + line=None, 145 + column=None, 146 + category=symbol_name_to_category, 147 + subcategory=None, 148 + )) 149 + return frame_id 106 150 107 151 def _to_json_dict(self) -> Dict: 108 152 """Converts current Thread to GeckoThread JSON format."""