From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796077222; b=OjFlVdZi/N2g5uaJpTYGLT5l7A2dEdMw9dJCn6YxQkpR65Ev2KKHZrYxqS+bsFCYFQK+ dNB7YHUMlYPOECMGTAsXQad0TWDJKWq9RLHyJ8S1Zu76IWa9CGQHFUmJcGWn8CsKcK8du W0p7yf/nmG8xEeW7f+q6L4q0pw+oCRC+ZX55kjoz45WumndU0TCIGlCGU2VqzY1lwuLb2 nNEi0AgcMHb/z/2/Ln/557PtQKECdEc7134yBkrKD4O9uVH3JZiEeXNDjyFLKpT79kY7J 3ZJu+XsL5dLkXp5cIlDiGe6RLl8ECCZT+OPk5CnBoaZGpnSFgzJ7Im1WiaJ80oQgcMg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796077222; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=nfn8hw4Qh9cCZsx7Kvs8pXs5x/u5Krhb5M6LRIc5A/o=; b=3aPSBMzW6qqiR7nVgyA2/nCc8w1mrwDwqIEh+YT/K0yBDkSWTF3steo8O8RonC5A4OtC hvC7SPMPjA63JTJtKBZjlHRLDlr9HX7MzgNBxBjWNzly9+ai4JGzNhu/bCdwkuK8fXoNh /AC3tFbKQ1as3ioYoD3BbLmn8VNFGGc644YhjD9HP3KKFyraqqBMq8IWbNJb6ld4f5E+r nbRkTwW2ogzace8+kPtMIhIt/miQu5KpThc0tPwfh+YCo+0CGa1GA+0VxzFxjTIBciHiT jNxW5W61Fpj2hHJv+6W+CtS8cebl+44T8QKAtAj/GW7PKUSBn9DN2z1oeLcpKehCKng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KvYirjqL; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A208606A325 for ; Mon, 30 Nov 2026 22:20:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1A208606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KvYirjqL Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-341eff9e381so16050129eec.1 for ; Mon, 30 Nov 2026 14:20:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796077221; x=1796682021; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=nfn8hw4Qh9cCZsx7Kvs8pXs5x/u5Krhb5M6LRIc5A/o=; b=KvYirjqL4ZFEBmT6/HfTNY0sqlxEVZhWQjfVDPZFB8XTg/zL/dXDIrbe2kpj/Nk0s5 yLQhm3JsBSqLdZ20ycAj7egJ9ZhNyN9iIRrDJwvXvBvtOWKI0XjZlfmW/Gl8tUEt4EKc IJMgiTPX5dp+E5AvzewPGFIjEKK5t1/K5XSPzPe7i8LDpz+cinadR7GZR7vUyylW7Wn1 uRhKCcNKN621CrELYSHP6msTjVjg5zF9j/mssT+LFsYgTuO4JJxfQkvyui1L6WneTkHR 6yOrZcQTUuSRI7OUF4Fdthlevj4N9CWQtMrOwvt1HDo6dcpCInCexajW8ktWMtX4T3/f M52A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796077221; x=1796682021; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=nfn8hw4Qh9cCZsx7Kvs8pXs5x/u5Krhb5M6LRIc5A/o=; b=qlGYu/A9subS1gZd6vi5Uky4OlsR6whbn0wxV6O8YhjDilUfscQiHPQ6RC+Xxdy9Uz 2jtTvCreWkbW+3Abe7hD6uD43dVm5YbVtQiUuHb9aqKnfuV8hL7TPfnvMvKnC6KFPwlX v0+7tNq9w1K+bO8S/In4owQghCdFwpkc3NlmTihJ1ztoztt65IjEOUE61q6wmfk8VRQn twhRy6xyFR6pwsIChjcsFdzzfcWFnNVrNXMAVowbOKRzDdpGfzM8khHRP5hec9hM6C2D x3HtcL1CEQPOJLKg2EmkdF8je1CuOOOjszSRIvQ7wxHLrsnCUbToJojttPKG+H5Oz3bC 99ew== X-Forwarded-Encrypted: i=1; AL+hErGlZlbdYwQWVOgxLoHbXr1Vq+/y6vLGSbhurnqcIdI18Z+FHJ233DJ+rcXRdlGjCQG9KhDqlGL5yA3hfWg=@vger.kernel.org X-Gm-Message-State: APuU4Z8soAZ/YuaMDBXHnMpFUliIXvYLbIaPglxSk2b7gAOLO/PaKr00 E2SawodEOMN7++Ay9aqCIrVZN9601ctBpT54GL4LAf+GPRQcwGt8LeDfs4Z/pBccV+GHrQJk4oZ GmLOMOZSgJQ== X-Received: from dldyr3-n2.prod.google.com ([2002:a05:701b:4503:20b0:1a0:8cef:34d5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:290:b0:191:444:425 with SMTP id a92af1059eb24-1b5567c0fbdmr32080862c88.2.1796077220644; Mon, 30 Nov 2026 14:20:20 -0800 (PST) Date: Mon, 30 Nov 2026 14:19:58 -0800 In-Reply-To: <20261130222008.4189072-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130222008.4189072-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130222008.4189072-2-irogers@google.com> Subject: [PATCH v3 01/11] perf jevents: Store raw bytes in BigCString instead of C-escaped strings From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Athira Rajeev Cc: Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" Previously BigCString stored C-escaped strings (with literal '\000', '\n', '\"', '\\') and estimated their compiled byte length via c_len(), which did not handle arbitrary escapes and caused both suffix folding and metricgroups sorting to compare '\000' ('\\' == 0x5c) rather than NUL (0x00). Notably, sorting metricgroup names with '\000' placed 'BadSpec\000' ('S' == 0x53) before 'Bad\000' ('\\' == 0x5c), breaking strcmp() binary search in describe_metricgroup("Bad"). Store raw NUL-delimited UTF-8 bytes directly in BigCString and _metricgroups, compute offsets from len(b) directly, remove c_len(), and escape bytes into C string literals only when emitting C source via bytes_to_c_literal(). Fixes: 66c6e0c10027 ("perf jevents: Add support for metricgroup descriptions") Reviewed-by: James Clark Signed-off-by: Ian Rogers --- tools/perf/pmu-events/jevents.py | 153 +++++++++++++++---------------- 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/tools/perf/pmu-events/jevents.py b/tools/perf/pmu-events/jevents.py index 34325e6d7e05..cbeac3355b16 100755 --- a/tools/perf/pmu-events/jevents.py +++ b/tools/perf/pmu-events/jevents.py @@ -11,7 +11,7 @@ from typing import (Callable, Dict, Optional, Sequence, Set, Tuple) import collections # Global command line arguments. -_args = None +_args: Optional[argparse.Namespace] = None # List of regular event tables. _event_tables: list[str] = [] # List of event tables generated from "/sys" directories. @@ -37,7 +37,7 @@ _pending_metrics_tblname: Optional[str] = None # Global BigCString shared by all structures. _bcs: "BigCString" # Map from the name of a metric group to a description of the group. -_metricgroups: Dict[str, str] = {} +_metricgroups: Dict[bytes, bytes] = {} # Order specific JsonEvent attributes will be visited. _json_event_attributes = [ # cmp_sevent related attributes. @@ -95,26 +95,24 @@ def file_name_to_table_name(prefix: str, parents: Sequence[str], return tblname.replace('-', '_') -def c_len(s: str) -> int: - """Return the length of s a C string +_C_ESCAPE_TABLE = str.maketrans({ + '\\': '\\\\', + '"': '\\"', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', + **{chr(c): f'\\{c:03o}' for c in range(32) if chr(c) not in ('\n', '\r', '\t')}, + chr(127): '\\177', +}) + + +def bytes_to_c_literal(b: bytes) -> str: + """Convert UTF-8 bytes (with NUL terminators) to a C string literal body.""" + return b.decode('utf-8').translate(_C_ESCAPE_TABLE) - This doesn't handle all escape characters properly. It first assumes - all \\ are for escaping, it then adjusts as it will have over counted - \\. The code uses \000 rather than \0 as a terminator as an adjacent - number would be folded into a string of \0 (ie. "\0" + "5" doesn't - equal a terminator followed by the number 5 but the escape of - \05). The code adjusts for \000 but not properly for all octal, hex - or unicode values. - """ - try: - utf = s.encode(encoding='utf-8',errors='strict') - except: - print(f'broken string {s}') - raise - return len(utf) - utf.count(b'\\') + utf.count(b'\\\\') - (utf.count(b'\\000') * 2) class BigCString: - """A class to hold many strings concatenated together. + """A class to hold many byte strings concatenated together. Generating a large number of stand-alone C strings creates a large number of relocations in position independent code. The BigCString @@ -126,12 +124,12 @@ class BigCString: strings are merged. If a longer string ends-with the same value as a shorter string, these entries are also merged. """ - strings: Set[str] + strings: Set[bytes] big_string: Sequence[str] - offsets: Dict[str, int] + offsets: Dict[bytes, int] insert_number: int - insert_point: Dict[str, int] - metrics: Set[str] + insert_point: Dict[bytes, int] + metrics: Set[bytes] def __init__(self): self.strings = set() @@ -139,21 +137,21 @@ class BigCString: self.insert_point = {} self.metrics = set() - def add(self, s: str, is_metric: bool) -> None: - """Called to add to the big string.""" - if s not in self.strings: - self.strings.add(s) - self.insert_point[s] = self.insert_number + def add(self, b: bytes, is_metric: bool) -> None: + """Called to add NUL-terminated UTF-8 bytes to the big string.""" + if b not in self.strings: + self.strings.add(b) + self.insert_point[b] = self.insert_number self.insert_number += 1 if is_metric: - self.metrics.add(s) + self.metrics.add(b) def compute(self) -> None: """Called once all strings are added to compute the string and offsets.""" - folded_strings = {} + folded_strings: Dict[bytes, bytes] = {} # Determine if two strings can be folded, ie. let 1 string use the - # end of another. First reverse all strings and sort them. + # end of another. First reverse all byte strings and sort them. sorted_reversed_strings = sorted([x[::-1] for x in self.strings]) # Strings 'xyz' and 'yz' will now be [ 'zy', 'zyx' ]. Scan forward @@ -161,16 +159,16 @@ class BigCString: # into, in the example rather than using 'yz' we can use'xyz' at # an offset of 1. We record which string can be folded into which # in folded_strings, we don't need to record the offset as it is - # trivially computed from the string lengths. - for pos,s in enumerate(sorted_reversed_strings): + # trivially computed from the byte lengths. + for pos, b in enumerate(sorted_reversed_strings): best_pos = pos for check_pos in range(pos + 1, len(sorted_reversed_strings)): - if sorted_reversed_strings[check_pos].startswith(s): + if sorted_reversed_strings[check_pos].startswith(b): best_pos = check_pos else: break if pos != best_pos: - folded_strings[s[::-1]] = sorted_reversed_strings[best_pos][::-1] + folded_strings[b[::-1]] = sorted_reversed_strings[best_pos][::-1] # Compute reverse mappings for debugging. fold_into_strings = collections.defaultdict(set) @@ -178,36 +176,34 @@ class BigCString: if key != val: fold_into_strings[val].add(key) - # big_string_offset is the current location within the C string + # big_string_offset is the current byte location within the C string # being appended to - comments, etc. don't count. big_string is - # the string contents represented as a list. Strings are immutable - # in Python and so appending to one causes memory issues, while - # lists are mutable. + # the C string literal contents represented as a list. big_string_offset = 0 self.big_string = [] self.offsets = {} - def string_cmp_key(s: str) -> Tuple[bool, int, str]: - return (s in self.metrics, self.insert_point[s], s) + def string_cmp_key(b: bytes) -> Tuple[bool, int, bytes]: + return (b in self.metrics, self.insert_point[b], b) # Emit all strings that aren't folded in a sorted manner. - for s in sorted(self.strings, key=string_cmp_key): - if s not in folded_strings: - self.offsets[s] = big_string_offset + for b in sorted(self.strings, key=string_cmp_key): + if b not in folded_strings: + self.offsets[b] = big_string_offset self.big_string.append(f'/* offset={big_string_offset} */\n"') - self.big_string.append(s) + self.big_string.append(bytes_to_c_literal(b)) self.big_string.append('"') - if s in fold_into_strings: - self.big_string.append(' /* also: ' + ', '.join(fold_into_strings[s]) + ' */') + if b in fold_into_strings: + also = [bytes_to_c_literal(x).replace('*/', r'\*\/') + for x in sorted(fold_into_strings[b])] + self.big_string.append(' /* also: ' + ', '.join(also) + ' */') self.big_string.append('\n') - big_string_offset += c_len(s) - continue + big_string_offset += len(b) # Compute the offsets of the folded strings. - for s in folded_strings.keys(): - assert s not in self.offsets - folded_s = folded_strings[s] - self.offsets[s] = self.offsets[folded_s] + c_len(folded_s) - c_len(s) + for b, folded_b in folded_strings.items(): + assert b not in self.offsets + self.offsets[b] = self.offsets[folded_b] + len(folded_b) - len(b) _bcs = BigCString() @@ -228,8 +224,7 @@ class JsonEvent: """Fix formatting issue for the desc string.""" if s is None: return None - return s.removesuffix('. ').removesuffix('. ').removesuffix('.').replace( - '\n', '\\n').replace('\"', '\\"').replace('\r', '\\r') + return s.removesuffix('. ').removesuffix('. ').removesuffix('.') def convert_aggr_mode(aggr_mode: Optional[str]) -> Optional[str]: """Returns the aggr_mode_class enum value associated with the JSON string.""" @@ -449,21 +444,19 @@ class JsonEvent: s += f'\t{attr} = {value},\n' return s + '}' - def build_c_string(self, is_metric: bool) -> str: + def build_bytes(self, is_metric: bool) -> bytes: + """Encode the event or metric attributes as NUL-delimited UTF-8 bytes.""" s = '' for attr in _json_metric_attributes if is_metric else _json_event_attributes: x = getattr(self, attr) if is_metric and x and attr == 'metric_expr': - # Convert parsed metric expressions into a string. Slashes - # must be doubled in the file. - x = x.ToPerfJson().replace('\\', '\\\\') - if is_metric and x and attr == 'metric_threshold': - x = x.replace('\\', '\\\\') + # Convert parsed metric expressions into a string. + x = x.ToPerfJson() if attr in _json_enum_attributes: s += x if x else '0' else: - s += f'{x}\\000' if x else '\\000' - return s + s += f'{x}\0' if x else '\0' + return s.encode('utf-8') def to_c_string(self, is_metric: bool) -> str: """Representation of the event as a C struct initializer.""" @@ -472,9 +465,10 @@ class JsonEvent: s = s.replace('*/', r'\*\/') return f'\t/* {s} */\n' if len(s) < 80 else f'\t/* {s[0:80]}... */\n' - s = self.build_c_string(is_metric) + b = self.build_bytes(is_metric) + s = bytes_to_c_literal(b) assert _bcs is not None - return f'{make_comment(s)}\t{{ { _bcs.offsets[s] } }},\n' + return f'{make_comment(s)}\t{{ { _bcs.offsets[b] } }},\n' _json_cache: Dict[Tuple[str, str], Sequence[JsonEvent]] = {} @@ -580,11 +574,12 @@ def print_pending_events() -> None: static const struct pmu_table_entry {_pending_events_tblname}[] = {{ """) for (pmu, tbl_pmu) in sorted(pmus): - pmu_name = f"{pmu}\\000" + pmu_name_bytes = (pmu + '\0').encode('utf-8') + pmu_comment = bytes_to_c_literal(pmu_name_bytes).replace('*/', r'\*\/') _args.output_file.write(f"""\t{{ \t\t.entries = {_pending_events_tblname}_{tbl_pmu}, \t\t.num_entries = ARRAY_SIZE({_pending_events_tblname}_{tbl_pmu}), -\t\t.pmu_name = {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +\t\t.pmu_name = {{ {_bcs.offsets[pmu_name_bytes]} /* {pmu_comment} */ }}, \t}}, """) _args.output_file.write('};\n\n') @@ -636,11 +631,12 @@ def print_pending_metrics() -> None: static const struct pmu_table_entry {_pending_metrics_tblname}[] = {{ """) for (pmu, tbl_pmu) in sorted(pmus): - pmu_name = f"{pmu}\\000" + pmu_name_bytes = (pmu + '\0').encode('utf-8') + pmu_comment = bytes_to_c_literal(pmu_name_bytes).replace('*/', r'\*\/') _args.output_file.write(f"""\t{{ \t\t.entries = {_pending_metrics_tblname}_{tbl_pmu}, \t\t.num_entries = ARRAY_SIZE({_pending_metrics_tblname}_{tbl_pmu}), -\t\t.pmu_name = {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +\t\t.pmu_name = {{ {_bcs.offsets[pmu_name_bytes]} /* {pmu_comment} */ }}, \t}}, """) _args.output_file.write('};\n\n') @@ -670,23 +666,22 @@ def preprocess_one_file(parents: Sequence[str], item: os.DirEntry) -> None: metricgroup_descriptions = json.load(f) for mgroup in metricgroup_descriptions: assert len(mgroup) > 1, parents - description = f"{metricgroup_descriptions[mgroup]}\\000" - mgroup = f"{mgroup}\\000" - _bcs.add(mgroup, is_metric=True) + description = (metricgroup_descriptions[mgroup] + '\0').encode('utf-8') + mgroup_bytes = (mgroup + '\0').encode('utf-8') + _bcs.add(mgroup_bytes, is_metric=True) _bcs.add(description, is_metric=True) - _metricgroups[mgroup] = description + _metricgroups[mgroup_bytes] = description return topic = get_topic(item.name) for event in read_json_events(item.path, topic): - pmu_name = f"{event.pmu}\\000" + pmu_name = (event.pmu + '\0').encode('utf-8') if event.name: _bcs.add(pmu_name, is_metric=False) - _bcs.add(event.build_c_string(is_metric=False), is_metric=False) + _bcs.add(event.build_bytes(is_metric=False), is_metric=False) if event.metric_name: - pmu_name = f"{event.pmu}\\000" _bcs.add(pmu_name, is_metric=True) - _bcs.add(event.build_c_string(is_metric=True), is_metric=True) + _bcs.add(event.build_bytes(is_metric=True), is_metric=True) def process_one_file(parents: Sequence[str], item: os.DirEntry) -> None: """Process a JSON file during the main walk.""" @@ -1433,8 +1428,10 @@ static const int metricgroups[][2] = { """) for mgroup in sorted(_metricgroups): description = _metricgroups[mgroup] + mgroup_comment = bytes_to_c_literal(mgroup).replace('*/', r'\*\/') + desc_comment = bytes_to_c_literal(description).replace('*/', r'\*\/') _args.output_file.write( - f'\t{{ {_bcs.offsets[mgroup]}, {_bcs.offsets[description]} }}, /* {mgroup} => {description} */\n' + f'\t{{ {_bcs.offsets[mgroup]}, {_bcs.offsets[description]} }}, /* {mgroup_comment} => {desc_comment} */\n' ) _args.output_file.write(""" }; -- 2.56.1.839.g22a8290db9-goog