hiperf_txt_parser 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,13 +3,15 @@ import type { PerfData } from "./types.js";
3
3
  * 将 PerfData 导出为与 sample/perf_data.txt 相同格式的文本(含缩进与每行前缀)
4
4
  */
5
5
  export declare function formatPerfDataToText(data: PerfData): string;
6
- /** 导出用 JSON 的单项:issuce 固定为 "unknow",call_chain 为 frames 逐行拼接 */
7
- export interface RecordSampleJsonExportItem {
6
+ /**
7
+ * 导出用 JSON 单项:固定含 `issuce`、`call_chain`;若存在 `traceFieldDict`,其键值对与二者**同级**平铺。
8
+ */
9
+ export type RecordSampleJsonExportItem = {
8
10
  issuce: "unknow";
9
11
  call_chain: string;
10
- }
12
+ } & Record<string, string>;
11
13
  /**
12
- * 将 PerfData 转为导出用 JSON 结构:顶层为数组,每项为 { issuce: "unknow", call_chain: "" },
13
- * call_chain 由对应 record sample 的 callchainFrames.frames 逐行拼接得到,无则为空字符串。
14
+ * 将 PerfData 转为导出用 JSON 结构:顶层为数组。
15
+ * call_chain callchainFrames.frames 拼接;有 traceFieldDict 时各字段平铺在对象顶层(与 call_chain 同级)。
14
16
  */
15
17
  export declare function formatPerfDataToJson(data: PerfData): RecordSampleJsonExportItem[];
@@ -19,7 +19,18 @@ function serializeOneSample(sample) {
19
19
  lines.push(` ${addr}`);
20
20
  }
21
21
  }
22
- if (sample.raw) {
22
+ if (sample.traceFieldDict && Object.keys(sample.traceFieldDict).length > 0 && sample.raw) {
23
+ lines.push(` raw size=${sample.raw.size}`);
24
+ for (const [k, v] of Object.entries(sample.traceFieldDict)) {
25
+ lines.push(` ${k}: ${v}`);
26
+ }
27
+ const n = Object.keys(sample.traceFieldDict).length;
28
+ for (let i = n; i < sample.raw.lines.length; i++) {
29
+ const { hex, short } = sample.raw.lines[i];
30
+ lines.push(short ? ` ${hex} (${short})` : ` ${hex}`);
31
+ }
32
+ }
33
+ else if (sample.raw) {
23
34
  lines.push(` raw size=${sample.raw.size}`);
24
35
  for (const { hex, short } of sample.raw.lines) {
25
36
  lines.push(short ? ` ${hex} (${short})` : ` ${hex}`);
@@ -47,12 +58,20 @@ export function formatPerfDataToText(data) {
47
58
  return data.recordSamples.map(serializeOneSample).join("\n\n");
48
59
  }
49
60
  /**
50
- * 将 PerfData 转为导出用 JSON 结构:顶层为数组,每项为 { issuce: "unknow", call_chain: "" },
51
- * call_chain 由对应 record sample 的 callchainFrames.frames 逐行拼接得到,无则为空字符串。
61
+ * 将 PerfData 转为导出用 JSON 结构:顶层为数组。
62
+ * call_chain callchainFrames.frames 拼接;有 traceFieldDict 时各字段平铺在对象顶层(与 call_chain 同级)。
52
63
  */
53
64
  export function formatPerfDataToJson(data) {
54
- return data.recordSamples.map((s) => ({
55
- issuce: "unknow",
56
- call_chain: s.callchainFrames ? s.callchainFrames.frames.join("\n") : "",
57
- }));
65
+ return data.recordSamples.map((s) => {
66
+ const item = {
67
+ issuce: "unknow",
68
+ call_chain: s.callchainFrames ? s.callchainFrames.frames.join("\n") : "",
69
+ };
70
+ if (s.traceFieldDict) {
71
+ for (const [k, v] of Object.entries(s.traceFieldDict)) {
72
+ item[k] = v;
73
+ }
74
+ }
75
+ return item;
76
+ });
58
77
  }
@@ -88,6 +88,7 @@ export declare function decodeRawByRegistry(raw: Uint8Array, registry: TracePars
88
88
  *
89
89
  * 规则:
90
90
  * - 若找到 common_type 对应解析器:将 print fmt 渲染结果写入 `sample.raw.lines`(一行)。
91
+ * - fieldDict:写入 `traceFieldDict`,`raw.lines` 为每行一条 `key: value`;`raw.size` 为行数(含可选 common 行)。
91
92
  * - 若找不到解析器:放弃解析,并将 `common_type` 写入 `sample.raw.lines`(一行)。
92
93
  */
93
94
  export declare function decodePerfRawData(perfData: PerfData, registry: TraceParserRegistry, options?: DecodePerfRawDataOptions): PerfData;
@@ -441,6 +441,7 @@ export function decodeRawByRegistry(raw, registry, options = {}) {
441
441
  *
442
442
  * 规则:
443
443
  * - 若找到 common_type 对应解析器:将 print fmt 渲染结果写入 `sample.raw.lines`(一行)。
444
+ * - fieldDict:写入 `traceFieldDict`,`raw.lines` 为每行一条 `key: value`;`raw.size` 为行数(含可选 common 行)。
444
445
  * - 若找不到解析器:放弃解析,并将 `common_type` 写入 `sample.raw.lines`(一行)。
445
446
  */
446
447
  export function decodePerfRawData(perfData, registry, options = {}) {
@@ -461,8 +462,30 @@ export function decodePerfRawData(perfData, registry, options = {}) {
461
462
  .map(([k, v]) => `${k}:${typeof v === "bigint" ? v.toString(10) : v}`)
462
463
  .join(" ")
463
464
  : "";
465
+ const dict = decoded.renderedFieldDict;
466
+ const useFieldDict = options.tracePrintMode === "fieldDict" &&
467
+ dict !== undefined &&
468
+ Object.keys(dict).length > 0;
469
+ if (useFieldDict) {
470
+ const kvLines = Object.entries(dict).map(([k, v]) => ({
471
+ hex: `${k}: ${v}`,
472
+ }));
473
+ const lines = options.keepCommonFields && commonLine.length > 0
474
+ ? [...kvLines, { hex: commonLine }]
475
+ : kvLines;
476
+ return {
477
+ ...sample,
478
+ traceFieldDict: dict,
479
+ raw: {
480
+ ...sample.raw,
481
+ size: lines.length,
482
+ lines,
483
+ },
484
+ };
485
+ }
464
486
  return {
465
487
  ...sample,
488
+ traceFieldDict: undefined,
466
489
  raw: {
467
490
  ...sample.raw,
468
491
  lines: options.keepCommonFields && commonLine.length > 0
package/dist/types.d.ts CHANGED
@@ -39,6 +39,8 @@ export interface RecordSample {
39
39
  period: number;
40
40
  callchain?: CallchainEntry;
41
41
  raw?: RawEntry;
42
+ /** tracePrintMode 为 fieldDict 且解码成功时:print 各参数键值(用于导出 txt/json) */
43
+ traceFieldDict?: Record<string, string>;
42
44
  server?: ServerEntry;
43
45
  callchainFrames?: CallchainFramesEntry;
44
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hiperf_txt_parser",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Parse perf data.txt and output structured TypeScript data",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",