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 stack has been created for storing func and dso from the callchain.
The sample has been added to a specific thread. It first checks if the
thread exists in the Thread class. Then it call _add_sample function
which is responsible for appending a new entry to the samples list.

Also callchain parsing and storing part is implemented. Moreover removed
the comment from thread.

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/5a112be85ccdcdcd611e343f6a7a7482d01f6299.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
2d889c6a 258dfd41

+50 -2
+50 -2
tools/perf/scripts/python/gecko.py
··· 40 40 # The product name is used by the profiler UI to show the Operating system and Processor. 41 41 PRODUCT = os.popen('uname -op').read().strip() 42 42 43 + # Here key = tid, value = Thread 44 + tid_to_thread = dict() 45 + 43 46 # The category index is used by the profiler UI to show the color of the flame graph. 44 47 USER_CATEGORY_INDEX = 0 45 48 KERNEL_CATEGORY_INDEX = 1 ··· 156 153 )) 157 154 return frame_id 158 155 156 + def _add_sample(self, comm: str, stack: List[str], time_ms: Milliseconds) -> None: 157 + """Add a timestamped stack trace sample to the thread builder. 158 + Args: 159 + comm: command-line (name) of the thread at this sample 160 + stack: sampled stack frames. Root first, leaf last. 161 + time_ms: timestamp of sample in milliseconds. 162 + """ 163 + # Ihreads may not set their names right after they are created. 164 + # Instead, they might do it later. In such situations, to use the latest name they have set. 165 + if self.comm != comm: 166 + self.comm = comm 167 + 168 + prefix_stack_id = reduce(lambda prefix_id, frame: self._intern_stack 169 + (self._intern_frame(frame), prefix_id), stack, None) 170 + if prefix_stack_id is not None: 171 + self.samples.append(Sample(stack_id=prefix_stack_id, 172 + time_ms=time_ms, 173 + responsiveness=0)) 174 + 159 175 def _to_json_dict(self) -> Dict: 160 176 """Converts current Thread to GeckoThread JSON format.""" 161 177 # Gecko profile format is row-oriented data as List[List], ··· 253 231 if not start_time: 254 232 start_time = time_stamp 255 233 234 + # Parse and append the callchain of the current sample into a stack. 235 + stack = [] 236 + if param_dict['callchain']: 237 + for call in param_dict['callchain']: 238 + if 'sym' not in call: 239 + continue 240 + stack.append(f'{call["sym"]["name"]} (in {call["dso"]})') 241 + if len(stack) != 0: 242 + # Reverse the stack, as root come first and the leaf at the end. 243 + stack = stack[::-1] 244 + 245 + # During perf record if -g is not used, the callchain is not available. 246 + # In that case, the symbol and dso are available in the event parameters. 247 + else: 248 + func = param_dict['symbol'] if 'symbol' in param_dict else '[unknown]' 249 + dso = param_dict['dso'] if 'dso' in param_dict else '[unknown]' 250 + stack.append(f'{func} (in {dso})') 251 + 252 + # Add sample to the specific thread. 253 + thread = tid_to_thread.get(tid) 254 + if thread is None: 255 + thread = Thread(comm=comm, pid=pid, tid=tid) 256 + tid_to_thread[tid] = thread 257 + thread._add_sample(comm=comm, stack=stack, time_ms=time_stamp) 258 + 256 259 # Trace_end runs at the end and will be used to aggregate 257 260 # the data into the final json object and print it out to stdout. 258 261 def trace_end() -> None: 262 + threads = [thread._to_json_dict() for thread in tid_to_thread.values()] 263 + 259 264 # Schema: https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L305 260 265 gecko_profile_with_meta = { 261 266 "meta": { ··· 301 252 "markerSchema": [], 302 253 }, 303 254 "libs": [], 304 - # threads will be implemented in later commits. 305 - # "threads": threads, 255 + "threads": threads, 306 256 "processes": [], 307 257 "pausedRanges": [], 308 258 }