hiperf_txt_parser 1.0.9 → 1.0.10

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.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { parsePerfData, filterByTgid } from "./parser.js";
2
2
  export { formatPerfDataToText, formatPerfDataToJson } from "./serializer.js";
3
3
  export { toBackTraceStack, toBackTraceStacks } from "./backtrace.js";
4
- export { parseTraceFormat, parseCommonFieldsFromRaw, parseAllFieldsFromRaw, rawHexLinesToBuffer, buildTraceParserRegistry, decodeRawByRegistry, decodePerfRawData, } from "./traceFormat.js";
5
- export type { ParsedTraceFormat, TraceFormatField, TraceParserRegistry, DecodedRawSample, } from "./traceFormat.js";
4
+ export { parseTraceFormat, parseCommonFieldsFromRaw, parseAllFieldsFromRaw, rawHexLinesToBuffer, bufferToRawHexLines, buildTraceParserRegistry, decodeRawByRegistry, decodePerfRawData, } from "./traceFormat.js";
5
+ export type { ParsedTraceFormat, TraceFormatField, TraceParserRegistry, DecodedRawSample, DecodePerfRawDataOptions, Endian, } from "./traceFormat.js";
6
6
  export type { PerfData, RecordSample } from "./types.js";
7
7
  export type { RecordSampleJsonExportItem } from "./serializer.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { parsePerfData, filterByTgid } from "./parser.js";
2
2
  export { formatPerfDataToText, formatPerfDataToJson } from "./serializer.js";
3
3
  export { toBackTraceStack, toBackTraceStacks } from "./backtrace.js";
4
- export { parseTraceFormat, parseCommonFieldsFromRaw, parseAllFieldsFromRaw, rawHexLinesToBuffer, buildTraceParserRegistry, decodeRawByRegistry, decodePerfRawData, } from "./traceFormat.js";
4
+ export { parseTraceFormat, parseCommonFieldsFromRaw, parseAllFieldsFromRaw, rawHexLinesToBuffer, bufferToRawHexLines, buildTraceParserRegistry, decodeRawByRegistry, decodePerfRawData, } from "./traceFormat.js";
@@ -30,6 +30,11 @@ export interface DecodedRawSample {
30
30
  renderedText?: string;
31
31
  skipped: boolean;
32
32
  }
33
+ export interface DecodePerfRawDataOptions {
34
+ /** 是否在替换后的 raw 内容中保留 common_* 信息 */
35
+ keepCommonFields?: boolean;
36
+ }
37
+ export type Endian = "le" | "be";
33
38
  /**
34
39
  * 解析 sample/trace_format 风格的文本,得到字段列表
35
40
  */
@@ -43,11 +48,21 @@ export declare function parseCommonFieldsFromRaw(raw: Uint8Array, format: Parsed
43
48
  */
44
49
  export declare function parseAllFieldsFromRaw(raw: Uint8Array, format: ParsedTraceFormat): Record<string, number | bigint | string | Array<number | bigint>>;
45
50
  /**
46
- * 将 perf 文本里 raw 段的 hex 行拼成连续字节(小端:每行一个数值,宽度由 hex 位数决定,4→2 字节,8→4 字节,16→8 字节)
51
+ * 将 perf 文本里 raw 段的 hex 行拼成连续字节(小端)。
52
+ *
53
+ * 规则:每行视为一个整数 token(如 0x12345678),按该 token 的字节宽度转成 LE 字节序。
54
+ * 例如:0x12345678 => [0x78, 0x56, 0x34, 0x12]
47
55
  */
48
56
  export declare function rawHexLinesToBuffer(lines: Array<{
49
57
  hex: string;
50
- }>): Uint8Array;
58
+ }>, endian?: Endian): Uint8Array;
59
+ /**
60
+ * 将字节缓冲按小端编码为 raw hex 行。
61
+ * 默认每行 4 字节(即 8 hex digits)。
62
+ */
63
+ export declare function bufferToRawHexLines(raw: Uint8Array, bytesPerLine?: number, endian?: Endian): Array<{
64
+ hex: string;
65
+ }>;
51
66
  /**
52
67
  * 从多个 trace_format 文本构建解析器集合(按 event ID 索引)
53
68
  */
@@ -67,4 +82,4 @@ export declare function decodeRawByRegistry(raw: Uint8Array, registry: TracePars
67
82
  * - 若找到 common_type 对应解析器:将 print fmt 渲染结果写入 `sample.raw.lines`(一行)。
68
83
  * - 若找不到解析器:放弃解析,并将 `common_type` 写入 `sample.raw.lines`(一行)。
69
84
  */
70
- export declare function decodePerfRawData(perfData: PerfData, registry: TraceParserRegistry): PerfData;
85
+ export declare function decodePerfRawData(perfData: PerfData, registry: TraceParserRegistry, options?: DecodePerfRawDataOptions): PerfData;
@@ -203,25 +203,60 @@ export function parseAllFieldsFromRaw(raw, format) {
203
203
  return out;
204
204
  }
205
205
  /**
206
- * 将 perf 文本里 raw 段的 hex 行拼成连续字节(小端:每行一个数值,宽度由 hex 位数决定,4→2 字节,8→4 字节,16→8 字节)
206
+ * 将 perf 文本里 raw 段的 hex 行拼成连续字节(小端)。
207
+ *
208
+ * 规则:每行视为一个整数 token(如 0x12345678),按该 token 的字节宽度转成 LE 字节序。
209
+ * 例如:0x12345678 => [0x78, 0x56, 0x34, 0x12]
207
210
  */
208
- export function rawHexLinesToBuffer(lines) {
211
+ export function rawHexLinesToBuffer(lines, endian = "le") {
209
212
  const chunks = [];
210
213
  for (const { hex } of lines) {
211
- const s = hex.replace(/^0x/i, "").trim();
214
+ let s = hex.replace(/^0x/i, "").trim();
212
215
  if (!s)
213
216
  continue;
214
- const byteLen = Math.ceil(s.length / 2);
215
- const width = byteLen <= 2 ? 2 : byteLen <= 4 ? 4 : 8;
217
+ if (s.length % 2 === 1)
218
+ s = "0" + s;
219
+ const byteLen = s.length / 2;
216
220
  let value = BigInt("0x" + s);
217
- const mask = (1n << BigInt(width * 8)) - 1n;
218
- value &= mask;
219
- for (let i = 0; i < width; i++) {
220
- chunks.push(Number((value >> BigInt(8 * i)) & 0xffn));
221
+ if (endian === "le") {
222
+ for (let i = 0; i < byteLen; i++) {
223
+ chunks.push(Number((value >> BigInt(8 * i)) & 0xffn));
224
+ }
225
+ }
226
+ else {
227
+ for (let i = byteLen - 1; i >= 0; i--) {
228
+ chunks.push(Number((value >> BigInt(8 * i)) & 0xffn));
229
+ }
221
230
  }
222
231
  }
223
232
  return Uint8Array.from(chunks);
224
233
  }
234
+ /**
235
+ * 将字节缓冲按小端编码为 raw hex 行。
236
+ * 默认每行 4 字节(即 8 hex digits)。
237
+ */
238
+ export function bufferToRawHexLines(raw, bytesPerLine = 4, endian = "le") {
239
+ if (bytesPerLine <= 0)
240
+ return [];
241
+ const out = [];
242
+ for (let i = 0; i < raw.length; i += bytesPerLine) {
243
+ const end = Math.min(i + bytesPerLine, raw.length);
244
+ let value = 0n;
245
+ if (endian === "le") {
246
+ for (let j = 0; j < end - i; j++) {
247
+ value |= BigInt(raw[i + j]) << BigInt(8 * j);
248
+ }
249
+ }
250
+ else {
251
+ for (let j = 0; j < end - i; j++) {
252
+ value = (value << 8n) | BigInt(raw[i + j]);
253
+ }
254
+ }
255
+ const width = (end - i) * 2;
256
+ out.push({ hex: "0x" + value.toString(16).padStart(width, "0") });
257
+ }
258
+ return out;
259
+ }
225
260
  /**
226
261
  * 从多个 trace_format 文本构建解析器集合(按 event ID 索引)
227
262
  */
@@ -352,22 +387,29 @@ export function decodeRawByRegistry(raw, registry) {
352
387
  * - 若找到 common_type 对应解析器:将 print fmt 渲染结果写入 `sample.raw.lines`(一行)。
353
388
  * - 若找不到解析器:放弃解析,并将 `common_type` 写入 `sample.raw.lines`(一行)。
354
389
  */
355
- export function decodePerfRawData(perfData, registry) {
390
+ export function decodePerfRawData(perfData, registry, options = {}) {
356
391
  return {
357
392
  recordSamples: perfData.recordSamples.map((sample) => {
358
393
  if (!sample.raw || sample.raw.lines.length === 0)
359
394
  return sample;
360
395
  const raw = rawHexLinesToBuffer(sample.raw.lines);
361
396
  const decoded = decodeRawByRegistry(raw, registry);
362
- const replacement = decoded.renderedText ??
397
+ const base = decoded.renderedText ??
363
398
  (decoded.commonType !== undefined ? String(decoded.commonType) : "");
364
- if (!replacement)
399
+ if (!base)
365
400
  return sample;
401
+ const commonLine = options.keepCommonFields
402
+ ? Object.entries(decoded.commonFields)
403
+ .map(([k, v]) => `${k}:${typeof v === "bigint" ? v.toString(10) : v}`)
404
+ .join(" ")
405
+ : "";
366
406
  return {
367
407
  ...sample,
368
408
  raw: {
369
409
  ...sample.raw,
370
- lines: [{ hex: replacement }],
410
+ lines: options.keepCommonFields && commonLine.length > 0
411
+ ? [{ hex: base }, { hex: commonLine }]
412
+ : [{ hex: base }],
371
413
  },
372
414
  };
373
415
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hiperf_txt_parser",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Parse perf data.txt and output structured TypeScript data",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",