Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2562155iog; Sun, 26 Jun 2022 20:13:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v+rxe8yZpNXd2JJl+P1ZhjkZxV09wFrKjKk07tc/KfD+6+cA9a7bC27IEUXn+33/WxXQJN X-Received: by 2002:a05:6402:43cd:b0:435:d76d:f9a7 with SMTP id p13-20020a05640243cd00b00435d76df9a7mr13989404edc.119.1656299626420; Sun, 26 Jun 2022 20:13:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656299626; cv=none; d=google.com; s=arc-20160816; b=O2mDrWuqfZlKDUzlLVr+JGZm4JTb8zwDmraeR+2TZrg1d39AmsVK80v2PrQCpj0yqt 4sxjF924kRQRv7i6b0fxi41sQkRWzkUyF5oLB/Z/ikYg2Nd6zQ+IMpc3AxmsM5XyPN6r aTq2zPAOimfNINXjLMl8TbrlJw/wj/YniCmG1GPC3M/kfPI7TUwanNtWadkaWEHs7/Y+ aVZLA0Sk6nEfQv885cj3h+wZPoFFjuIxHfpgdU7ZxjTpw5x2n40YHsGHE9OayNFpd3tX D3MbIKpJrER3VxLTJjJz0jVhW/b14jv5GngQUF7q0tuXtrste8Pob0mI5WjzvwaR/kWJ JAxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=9MIm30Bob/pL/qKTJcTuKIBq0frLtOFlhUm3XFP4a4U=; b=ahkdf33cI33Jop08lNxQE4aiPdOakoY2KsMgzYR1ONBbw9YBRRYOtx72+WlNi3jZtC n/raRTq1ER0zlPMFlMsVg4cpnXxQLWeiS+mlRocsF9ajgON1+OHWhn70RyNb0kmUBy3O dUeXQeLuoNH27mIQ6PaAexCnVfTfcINL58+piLKqoZegndfDcWVAjoRaNoiJdDYCJNud ZnKhjuuFUziLVosKtkdtyXAEt5vhHtk0iSJbic4on6+glIbjeC8MRPmzgtsLua3j91Oi GqLNrkoQX2CZwdXV9F7fsLJHM9MW8fKBLWOJvuF7AM5z9K7K4jCNygsGqs9N8XFlYJxH sLQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=SOY6128Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ga3-20020a1709070c0300b00722faf491a0si12248785ejc.686.2022.06.26.20.13.22; Sun, 26 Jun 2022 20:13:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=SOY6128Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232245AbiF0C6S (ORCPT + 99 others); Sun, 26 Jun 2022 22:58:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232473AbiF0C6K (ORCPT ); Sun, 26 Jun 2022 22:58:10 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1035238B1 for ; Sun, 26 Jun 2022 19:58:05 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31ba792c793so17270817b3.20 for ; Sun, 26 Jun 2022 19:58:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=9MIm30Bob/pL/qKTJcTuKIBq0frLtOFlhUm3XFP4a4U=; b=SOY6128Q0pTmsD0FvMVkV/NY0Al+vtIU9Pz8Y2+M8VvTPWE+/HAr9v+EDrEhkk/+x6 rxq12VCb3gLHM2mR2/nNJanyDI2L7J5xPIEgKa0AWiJ3wPSwVKaAYq4GBgW5TQLyNkzm xJsSHo8BVyQLUsLjbpmfSzJxXZ6jMACToG8x9avELIHjbdt4fp+qRKu9IiYCopQ5Id/0 QQRmTgTx2PDNn51mp2lxMzqgkvNhruuhcxNqQ8wUvIp4MJb9rmBSmULvLq2d059Mb1nC icuNf/5aN/rAsCvUNTkAvE10uzZJkqU6fIwPeD9Y0LeY0F5SDPLB44dPhsVul1MyfHpN ygGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=9MIm30Bob/pL/qKTJcTuKIBq0frLtOFlhUm3XFP4a4U=; b=rBwKBfbNDcfOjUC/NAAEFXun0B9/pslJ5mPGQj5zq0azZXLRNDpQ9tlBPUu3SEPkYb rWA1nTRcDUkTNuOny4NQugegDEPn32vlX+84D2RSeAD424byjZZnoiXdxjL6vdMTu5vH q7TpOwWEq/+5CQoD0C/YzUHiMiVZbKGfmE4fs5gTbTevv0dxOrUdQk3JC4VDpZyqb5DU VOH7Vy8Uz1DrCZk+Mw+g5F6MNYPyNaebFR7POWHdvuaVNIOmP6jpMMtc5FX9ohFp1VBD KFBtd/1cQb0X7tp5wKQe5xbaR0mwuwDNO0GP/mZ9QrxvyOcrk0xI05qLDsUOewCF9Sdv tMDw== X-Gm-Message-State: AJIora9h3CnDmK/6qWm3X6jtnLV2mrdXt7JFX2xDWaH+rFlQZLLC++tZ u4wLQG5WF9LqpoqGcNSXUqXjC0cQTUM3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:646b:8b65:8f13:1c18]) (user=irogers job=sendgmr) by 2002:a0d:eace:0:b0:317:87ac:b3a8 with SMTP id t197-20020a0deace000000b0031787acb3a8mr12856301ywe.126.1656298684225; Sun, 26 Jun 2022 19:58:04 -0700 (PDT) Date: Sun, 26 Jun 2022 19:57:42 -0700 In-Reply-To: <20220627025744.106527-1-irogers@google.com> Message-Id: <20220627025744.106527-3-irogers@google.com> Mime-Version: 1.0 References: <20220627025744.106527-1-irogers@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v6 2/4] perf jevents: Add python converter script From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , John Garry , Kan Liang , Andi Kleen , Zhengjun Xing , Felix Fietkau , Qi Liu , Like Xu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Nick Forrington , Kajol Jain , James Clark , Andrew Kilroy , "Paul A . Clarke" , Will Deacon , Mathieu Poirier , ananth.narayan@amd.com, ravi.bangoria@amd.com, santosh.shukla@amd.com, sandipan.das@amd.com, Caleb Biggers , Perry Taylor , Kshipra Bopardikar Cc: Stephane Eranian , Ian Rogers , Thomas Richter , Ian Rogers Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ian Rogers jevents.c is large, has a dependency on an old forked version of jsmn, and is challenging to work upon. A lot of jevents.c's complexity comes from needing to write json and csv parsing from first principles. In contrast python has this functionality in standard libraries and is already a build pre-requisite for tools like asciidoc (that builds all of the perf man pages). Introduce jevents.py that produces identical output to jevents.c. Add a test that runs both converter tools and validates there are no output differences. The test can be invoked with a phony build target like: make -C tools/perf jevents-py-test The python code deliberately tries to replicate the behavior of jevents.c so that the output matches and transitioning tools shouldn't introduce regressions. In some cases the code isn't as elegant as hoped, but fixing this can be done as follow up. Tested-by: Zhengjun Xing Tested-by: Thomas Richter Signed-off-by: Ian Rogers --- tools/perf/Makefile.perf | 6 + tools/perf/pmu-events/jevents-test.sh | 33 +++ tools/perf/pmu-events/jevents.py | 409 ++++++++++++++++++++++++++ 3 files changed, 448 insertions(+) create mode 100755 tools/perf/pmu-events/jevents-test.sh create mode 100755 tools/perf/pmu-events/jevents.py diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 8f738e11356d..1e29c8936f71 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -669,6 +669,12 @@ $(JEVENTS_IN): FORCE $(JEVENTS): $(JEVENTS_IN) $(QUIET_LINK)$(HOSTCC) $(JEVENTS_IN) -o $@ +JEVENTS_PY := pmu-events/jevents.py +JEVENTS_PY_TEST := pmu-events/jevents-test.sh +.PHONY: jevents-py-test +jevents-py-test: $(JEVENTS) + $(Q)$(call echo-cmd,gen)$(JEVENTS_PY_TEST) $(JEVENTS) $(JEVENTS_PY) pmu-events/arch + $(PMU_EVENTS_IN): $(JEVENTS) FORCE $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=pmu-events obj=pmu-events diff --git a/tools/perf/pmu-events/jevents-test.sh b/tools/perf/pmu-events/jevents-test.sh new file mode 100755 index 000000000000..9ae852292576 --- /dev/null +++ b/tools/perf/pmu-events/jevents-test.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) +# Validate that the legacy jevents and jevents.py produce identical output. +set -e + +JEVENTS="$1" +JEVENTS_PY="$2" +ARCH_PATH="$3" +JEVENTS_C_GENERATED=$(mktemp /tmp/jevents_c.XXXXX.c) +JEVENTS_PY_GENERATED=$(mktemp /tmp/jevents_py.XXXXX.c) + +cleanup() { + rm "$JEVENTS_C_GENERATED" "$JEVENTS_PY_GENERATED" + trap - exit term int +} +trap cleanup exit term int + +for path in "$ARCH_PATH"/* +do + arch=$(basename $path) + if [ "$arch" = "test" ] + then + continue + fi + echo "Checking architecture: $arch" + echo "Generating using jevents.c" + "$JEVENTS" "$arch" "$ARCH_PATH" "$JEVENTS_C_GENERATED" + echo "Generating using jevents.py" + "$JEVENTS_PY" "$arch" "$ARCH_PATH" "$JEVENTS_PY_GENERATED" + echo "Diffing" + diff -u "$JEVENTS_C_GENERATED" "$JEVENTS_PY_GENERATED" +done +cleanup diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py new file mode 100755 index 000000000000..83e0dcbeac9a --- /dev/null +++ b/tools/perf/pmu-events/jevents.py @@ -0,0 +1,409 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) +"""Convert directories of JSON events to C code.""" +import argparse +import csv +import json +import os +import sys +from typing import Callable +from typing import Sequence + +# Global command line arguments. +_args = None +# List of event tables generated from "/sys" directories. +_sys_event_tables = [] +# Map from an event name to an architecture standard +# JsonEvent. Architecture standard events are in json files in the top +# f'{_args.starting_dir}/{_args.arch}' directory. +_arch_std_events = {} +# Track whether an events table is currently being defined and needs closing. +_close_table = False + + +def removesuffix(s: str, suffix: str) -> str: + """Remove the suffix from a string + + The removesuffix function is added to str in Python 3.9. We aim for 3.6 + compatibility and so provide our own function here. + """ + return s[0:-len(suffix)] if s.endswith(suffix) else s + + +def file_name_to_table_name(parents: Sequence[str], dirname: str) -> str: + """Generate a C table name from directory names.""" + tblname = 'pme' + for p in parents: + tblname += '_' + p + tblname += '_' + dirname + return tblname.replace('-', '_') + + +class JsonEvent: + """Representation of an event loaded from a json file dictionary.""" + + def __init__(self, jd: dict): + """Constructor passed the dictionary of parsed json values.""" + + def llx(x: int) -> str: + """Convert an int to a string similar to a printf modifier of %#llx.""" + return '0' if x == 0 else hex(x) + + def fixdesc(s: str) -> str: + """Fix formatting issue for the desc string.""" + if s is None: + return None + return removesuffix(removesuffix(removesuffix(s, '. '), + '. '), '.').replace('\n', '\\n').replace( + '\"', '\\"').replace('\r', '\\r') + + def convert_aggr_mode(aggr_mode: str) -> str: + """Returns the aggr_mode_class enum value associated with the JSON string.""" + if not aggr_mode: + return None + aggr_mode_to_enum = { + 'PerChip': '1', + 'PerCore': '2', + } + return aggr_mode_to_enum[aggr_mode] + + def lookup_msr(num: str) -> str: + """Converts the msr number, or first in a list to the appropriate event field.""" + if not num: + return None + msrmap = { + 0x3F6: 'ldlat=', + 0x1A6: 'offcore_rsp=', + 0x1A7: 'offcore_rsp=', + 0x3F7: 'frontend=', + } + return msrmap[int(num.split(',', 1)[0], 0)] + + def real_event(name: str, event: str) -> str: + """Convert well known event names to an event string otherwise use the event argument.""" + fixed = { + 'inst_retired.any': 'event=0xc0,period=2000003', + 'inst_retired.any_p': 'event=0xc0,period=2000003', + 'cpu_clk_unhalted.ref': 'event=0x0,umask=0x03,period=2000003', + 'cpu_clk_unhalted.thread': 'event=0x3c,period=2000003', + 'cpu_clk_unhalted.core': 'event=0x3c,period=2000003', + 'cpu_clk_unhalted.thread_any': 'event=0x3c,any=1,period=2000003', + } + if not name: + return None + if name.lower() in fixed: + return fixed[name.lower()] + return event + + def unit_to_pmu(unit: str) -> str: + """Convert a JSON Unit to Linux PMU name.""" + if not unit: + return None + # Comment brought over from jevents.c: + # it's not realistic to keep adding these, we need something more scalable ... + table = { + 'CBO': 'uncore_cbox', + 'QPI LL': 'uncore_qpi', + 'SBO': 'uncore_sbox', + 'iMPH-U': 'uncore_arb', + 'CPU-M-CF': 'cpum_cf', + 'CPU-M-SF': 'cpum_sf', + 'UPI LL': 'uncore_upi', + 'hisi_sicl,cpa': 'hisi_sicl,cpa', + 'hisi_sccl,ddrc': 'hisi_sccl,ddrc', + 'hisi_sccl,hha': 'hisi_sccl,hha', + 'hisi_sccl,l3c': 'hisi_sccl,l3c', + 'imx8_ddr': 'imx8_ddr', + 'L3PMC': 'amd_l3', + 'DFPMC': 'amd_df', + 'cpu_core': 'cpu_core', + 'cpu_atom': 'cpu_atom', + } + return table[unit] if unit in table else f'uncore_{unit.lower()}' + + eventcode = 0 + if 'EventCode' in jd: + eventcode = int(jd['EventCode'].split(',', 1)[0], 0) + if 'ExtSel' in jd: + eventcode |= int(jd['ExtSel']) << 8 + configcode = int(jd['ConfigCode'], 0) if 'ConfigCode' in jd else None + self.name = jd['EventName'].lower() if 'EventName' in jd else None + self.compat = jd.get('Compat') + self.desc = fixdesc(jd.get('BriefDescription')) + self.long_desc = fixdesc(jd.get('PublicDescription')) + precise = jd.get('PEBS') + msr = lookup_msr(jd.get('MSRIndex')) + msrval = jd.get('MSRValue') + extra_desc = '' + if 'Data_LA' in jd: + extra_desc += ' Supports address when precise' + if 'Errata' in jd: + extra_desc += '.' + if 'Errata' in jd: + extra_desc += ' Spec update: ' + jd['Errata'] + self.pmu = unit_to_pmu(jd.get('Unit')) + filter = jd.get('Filter') + self.unit = jd.get('ScaleUnit') + self.perpkg = jd.get('PerPkg') + self.aggr_mode = convert_aggr_mode(jd.get('AggregationMode')) + self.deprecated = jd.get('Deprecated') + self.metric_name = jd.get('MetricName') + self.metric_group = jd.get('MetricGroup') + self.metric_constraint = jd.get('MetricConstraint') + self.metric_expr = jd.get('MetricExpr') + if self.metric_expr: + self.metric_expr = self.metric_expr.replace('\\', '\\\\') + arch_std = jd.get('ArchStdEvent') + if precise and self.desc and not '(Precise Event)' in self.desc: + extra_desc += ' (Must be precise)' if precise == '2' else (' (Precise ' + 'event)') + event = f'config={llx(configcode)}' if configcode is not None else f'event={llx(eventcode)}' + event_fields = [ + ('AnyThread', 'any='), + ('PortMask', 'ch_mask='), + ('CounterMask', 'cmask='), + ('EdgeDetect', 'edge='), + ('FCMask', 'fc_mask='), + ('Invert', 'inv='), + ('SampleAfterValue', 'period='), + ('UMask', 'umask='), + ] + for key, value in event_fields: + if key in jd and jd[key] != '0': + event += ',' + value + jd[key] + if filter: + event += f',{filter}' + if msr: + event += f',{msr}{msrval}' + if self.desc and extra_desc: + self.desc += extra_desc + if self.long_desc and extra_desc: + self.long_desc += extra_desc + if self.pmu: + if self.desc and not self.desc.endswith('. '): + self.desc += '. ' + self.desc = (self.desc if self.desc else '') + ('Unit: ' + self.pmu + ' ') + if arch_std and arch_std.lower() in _arch_std_events: + event = _arch_std_events[arch_std.lower()].event + # Copy from the architecture standard event to self for undefined fields. + for attr, value in _arch_std_events[arch_std.lower()].__dict__.items(): + if hasattr(self, attr) and not getattr(self, attr): + setattr(self, attr, value) + + self.event = real_event(self.name, event) + + def __repr__(self) -> str: + """String representation primarily for debugging.""" + s = '{\n' + for attr, value in self.__dict__.items(): + if value: + s += f'\t{attr} = {value},\n' + return s + '}' + + def to_c_string(self, topic_local: str) -> str: + """Representation of the event as a C struct initializer.""" + + def attr_string(attr: str, value: str) -> str: + return '\t.%s = \"%s\",\n' % (attr, value) + + def str_if_present(self, attr: str) -> str: + if not getattr(self, attr): + return '' + return attr_string(attr, getattr(self, attr)) + + s = '{\n' + for attr in ['name', 'event']: + s += str_if_present(self, attr) + if self.desc is not None: + s += attr_string('desc', self.desc) + else: + s += attr_string('desc', '(null)') + s += str_if_present(self, 'compat') + s += f'\t.topic = "{topic_local}",\n' + for attr in [ + 'long_desc', 'pmu', 'unit', 'perpkg', 'aggr_mode', 'metric_expr', + 'metric_name', 'metric_group', 'deprecated', 'metric_constraint' + ]: + s += str_if_present(self, attr) + s += '},\n' + return s + + +def read_json_events(path: str) -> Sequence[JsonEvent]: + """Read json events from the specified file.""" + return json.load(open(path), object_hook=lambda d: JsonEvent(d)) + + +def preprocess_arch_std_files(archpath: str) -> None: + """Read in all architecture standard events.""" + global _arch_std_events + for item in os.scandir(archpath): + if item.is_file() and item.name.endswith('.json'): + for event in read_json_events(item.path): + if event.name: + _arch_std_events[event.name.lower()] = event + + +def print_events_table_prefix(tblname: str) -> None: + """Called when a new events table is started.""" + global _close_table + if _close_table: + raise IOError('Printing table prefix but last table has no suffix') + _args.output_file.write(f'static const struct pmu_event {tblname}[] = {{\n') + _close_table = True + + +def print_events_table_entries(item: os.DirEntry, topic: str) -> None: + """Create contents of an events table.""" + if not _close_table: + raise IOError('Table entries missing prefix') + for event in read_json_events(item.path): + _args.output_file.write(event.to_c_string(topic)) + + +def print_events_table_suffix() -> None: + """Optionally close events table.""" + global _close_table + if _close_table: + _args.output_file.write("""{ +\t.name = 0, +\t.event = 0, +\t.desc = 0, +}, +}; +""") + _close_table = False + + +def process_one_file(parents: Sequence[str], item: os.DirEntry) -> None: + """Process a JSON file during the main walk.""" + global _sys_event_tables + + def get_topic(topic: str) -> str: + return removesuffix(topic, '.json').replace('-', ' ') + + def is_leaf_dir(path: str) -> bool: + for item in os.scandir(path): + if item.is_dir(): + return False + return True + + # model directory, reset topic + if item.is_dir() and is_leaf_dir(item.path): + print_events_table_suffix() + + tblname = file_name_to_table_name(parents, item.name) + if item.name == 'sys': + _sys_event_tables.append(tblname) + print_events_table_prefix(tblname) + return + + # base dir or too deep + level = len(parents) + if level == 0 or level > 4: + return + + # Ignore other directories. If the file name does not have a .json + # extension, ignore it. It could be a readme.txt for instance. + if not item.is_file() or not item.name.endswith('.json'): + return + + print_events_table_entries(item, get_topic(item.name)) + + +def print_mapping_table() -> None: + """Read the mapfile and generate the struct from cpuid string to event table.""" + with open(f'{_args.starting_dir}/{_args.arch}/mapfile.csv') as csvfile: + table = csv.reader(csvfile) + _args.output_file.write( + 'const struct pmu_events_map pmu_events_map[] = {\n') + first = True + for row in table: + # Skip the first row or any row beginning with #. + if not first and len(row) > 0 and not row[0].startswith('#'): + tblname = file_name_to_table_name([], row[2].replace('/', '_')) + _args.output_file.write("""{ +\t.cpuid = \"%s\", +\t.version = \"%s\", +\t.type = \"%s\", +\t.table = %s +}, +""" % (row[0].replace('\\', '\\\\'), row[1], row[3], tblname)) + first = False + + _args.output_file.write("""{ +\t.cpuid = "testcpu", +\t.version = "v1", +\t.type = "core", +\t.table = pme_test_soc_cpu, +}, +{ +\t.cpuid = 0, +\t.version = 0, +\t.type = 0, +\t.table = 0, +}, +}; +""") + + +def print_system_mapping_table() -> None: + """C struct mapping table array for tables from /sys directories.""" + _args.output_file.write( + '\nconst struct pmu_sys_events pmu_sys_event_tables[] = {\n') + for tblname in _sys_event_tables: + _args.output_file.write(f"""\t{{ +\t\t.table = {tblname}, +\t\t.name = \"{tblname}\", +\t}}, +""") + _args.output_file.write("""\t{ +\t\t.table = 0 +\t}, +}; +""") + + +def main() -> None: + global _args + + def dir_path(path: str) -> str: + """Validate path is a directory for argparse.""" + if os.path.isdir(path): + return path + raise argparse.ArgumentTypeError(f'\'{path}\' is not a valid directory') + + def ftw(path: str, parents: Sequence[str], + action: Callable[[Sequence[str], os.DirEntry], None]) -> None: + """Replicate the directory/file walking behavior of C's file tree walk.""" + for item in os.scandir(path): + action(parents, item) + if item.is_dir(): + ftw(item.path, parents + [item.name], action) + + ap = argparse.ArgumentParser() + ap.add_argument('arch', help='Architecture name like x86') + ap.add_argument( + 'starting_dir', + type=dir_path, + help='Root of tree containing architecture directories containing json files' + ) + ap.add_argument( + 'output_file', type=argparse.FileType('w'), nargs='?', default=sys.stdout) + _args = ap.parse_args() + + _args.output_file.write("#include \"pmu-events/pmu-events.h\"\n") + for path in [_args.arch, 'test']: + arch_path = f'{_args.starting_dir}/{path}' + if not os.path.isdir(arch_path): + raise IOError(f'Missing architecture directory in \'{arch_path}\'') + preprocess_arch_std_files(arch_path) + ftw(arch_path, [], process_one_file) + print_events_table_suffix() + + print_mapping_table() + print_system_mapping_table() + + +if __name__ == '__main__': + main() -- 2.37.0.rc0.161.g10f37bed90-goog