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 jevents: Move json encoding to its own functions

Have dedicated encode functions rather than having them embedded in
MetricGroup. This is to provide some uniformity in the Metric ToXXX
routines.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Thomas Falcon <thomas.falcon@intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
3f31651a b90e94ae

+22 -12
+22 -12
tools/perf/pmu-events/metric.py
··· 484 484 def ToMetricGroupDescriptions(self, root: bool = True) -> Dict[str, str]: 485 485 return {} 486 486 487 - class _MetricJsonEncoder(json.JSONEncoder): 488 - """Special handling for Metric objects.""" 489 - 490 - def default(self, o): 491 - if isinstance(o, Metric): 492 - return o.ToPerfJson() 493 - return json.JSONEncoder.default(self, o) 494 - 495 - 496 487 class MetricGroup: 497 488 """A group of metrics. 498 489 ··· 514 523 515 524 return result 516 525 517 - def ToPerfJson(self) -> str: 518 - return json.dumps(sorted(self.Flatten()), indent=2, cls=_MetricJsonEncoder) 526 + def ToPerfJson(self) -> List[Dict[str, str]]: 527 + result = [] 528 + for x in sorted(self.Flatten()): 529 + result.append(x.ToPerfJson()) 530 + return result 519 531 520 532 def ToMetricGroupDescriptions(self, root: bool = True) -> Dict[str, str]: 521 533 result = {self.name: self.description} if self.description else {} ··· 527 533 return result 528 534 529 535 def __str__(self) -> str: 530 - return self.ToPerfJson() 536 + return str(self.ToPerfJson()) 537 + 538 + 539 + def JsonEncodeMetric(x: MetricGroup): 540 + class MetricJsonEncoder(json.JSONEncoder): 541 + """Special handling for Metric objects.""" 542 + 543 + def default(self, o): 544 + if isinstance(o, Metric) or isinstance(o, MetricGroup): 545 + return o.ToPerfJson() 546 + return json.JSONEncoder.default(self, o) 547 + 548 + return json.dumps(x, indent=2, cls=MetricJsonEncoder) 549 + 550 + 551 + def JsonEncodeMetricGroupDescriptions(x: MetricGroup): 552 + return json.dumps(x.ToMetricGroupDescriptions(), indent=2) 531 553 532 554 533 555 class _RewriteIfExpToSelect(ast.NodeTransformer):