tools/perf/pmu-events/metric.py

Source file repositories/reference/linux-study-clean/tools/perf/pmu-events/metric.py

File Facts

System
Linux kernel
Corpus path
tools/perf/pmu-events/metric.py
Extension
.py
Size
26261 bytes
Lines
808
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
"""Parse or generate representations of perf metrics."""
import ast
import decimal
import json
import os
import re
from enum import Enum
from typing import Dict, List, Optional, Set, Tuple, Union

all_pmus = set()
all_events = set()
experimental_events = set()
all_events_all_models = set()

def LoadEvents(directory: str) -> None:
  """Populate a global set of all known events for the purpose of validating Event names"""
  global all_pmus
  global all_events
  global experimental_events
  global all_events_all_models
  all_events = {
      "context\\-switches",
      "cpu\\-cycles",
      "cycles",
      "duration_time",
      "instructions",
      "l2_itlb_misses",
  }
  for file in os.listdir(os.fsencode(directory)):
    filename = os.fsdecode(file)
    if filename.endswith(".json"):
      try:
        for x in json.load(open(f"{directory}/{filename}")):
          if "Unit" in x:
            all_pmus.add(x["Unit"])
          if "EventName" in x:
            all_events.add(x["EventName"])
            if "Experimental" in x and x["Experimental"] == "1":
              experimental_events.add(x["EventName"])
          elif "ArchStdEvent" in x:
            all_events.add(x["ArchStdEvent"])
      except json.decoder.JSONDecodeError:
        # The generated directory may be the same as the input, which
        # causes partial json files. Ignore errors.
        pass
  all_events_all_models = all_events.copy()
  for root, dirs, files in os.walk(directory + ".."):
    for filename in files:
      if filename.endswith(".json"):
        try:
          for x in json.load(open(f"{root}/{filename}")):
            if "EventName" in x:
              all_events_all_models.add(x["EventName"])
            elif "ArchStdEvent" in x:
              all_events_all_models.add(x["ArchStdEvent"])
        except json.decoder.JSONDecodeError:
          # The generated directory may be the same as the input, which
          # causes partial json files. Ignore errors.
          pass


def CheckPmu(name: str) -> bool:
  return name in all_pmus


def CheckEvent(name: str) -> bool:
  """Check the event name exists in the set of all loaded events"""
  global all_events
  if len(all_events) == 0:

Annotation

Implementation Notes