Module: Rperf::PProf

Defined in:
lib/rperf.rb

Overview

Hand-written protobuf encoder for pprof profile format. Only runs once at stop time, so performance is not critical.

Samples from C are: [[[path_str, label_str], …], weight] This encoder builds its own string table for pprof output.

Class Method Summary collapse

Class Method Details

.build_tables(merged) ⇒ Object

Assign sequential ids to unique frames. rperf emits exactly one Location and one Function per frame, sharing the same id, so a single table serves both.



1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/rperf.rb', line 1439

def build_tables(merged)
  frame_ids = {}
  next_id = 1

  merged.each do |(frames, _thread_seq, _label_set_id), _weight|
    frames.each do |frame|
      unless frame_ids.key?(frame)
        frame_ids[frame] = next_id
        next_id += 1
      end
    end
  end

  frame_ids
end

.encode(data) ⇒ Object



1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
# File 'lib/rperf.rb', line 1297

def encode(data)
  samples_raw = data[:aggregated_samples] || []
  frequency = data[:frequency]
  interval_ns = (frequency && frequency > 0) ? 1_000_000_000 / frequency : 0
  mode = (data[:mode] || :cpu).to_sym

  # Build string table: index 0 must be ""
  string_table = [""]
  string_index = { "" => 0 }

  intern = ->(s) {
    string_index[s] ||= begin
      idx = string_table.size
      string_table << s
      idx
    end
  }

  # Convert string frames to index frames and merge identical stacks per thread/label
  merged = Hash.new(0)
  thread_seq_key = nil  # interned lazily — only when a sample carries thread_seq
  label_sets = data[:label_sets]  # Array of Hash (may be nil)
  samples_raw.each do |frames, weight, thread_seq, label_set_id|
    key = [frames.map { |path, label| [intern.(path), intern.(label)] }, thread_seq || 0, label_set_id || 0]
    merged[key] += weight
  end
  merged = merged.to_a

  # Intern label set keys/values for pprof labels
  label_key_indices = {}  # String key → string_table index
  if label_sets
    label_sets.each do |ls|
      ls.each do |k, v|
        sk = k.to_s
        label_key_indices[sk] ||= intern.(sk)
        intern.(v.to_s)  # ensure value is interned
      end
    end
  end

  # Build the frame → id table (locations and functions are 1:1)
  frame_ids = build_tables(merged)

  # Intern type label and unit
  type_label = mode == :wall ? "wall" : "cpu"
  type_idx = intern.(type_label)
  ns_idx = intern.("nanoseconds")

  # Encode Profile message
  buf = "".b

  # field 1: sample_type (repeated ValueType)
  buf << encode_message(1, encode_value_type(type_idx, ns_idx))

  # field 2: sample (repeated Sample) with thread_seq + user labels
  merged.each do |(frames, thread_seq, label_set_id), weight|
    sample_buf = "".b
    loc_ids = frames.map { |f| frame_ids[f] }
    sample_buf << encode_packed_uint64(1, loc_ids)
    sample_buf << encode_packed_int64(2, [weight])
    if thread_seq && thread_seq > 0
      label_buf = "".b
      thread_seq_key ||= intern.("thread_seq")
      label_buf << encode_int64(1, thread_seq_key)  # key
      label_buf << encode_int64(3, thread_seq)       # num
      sample_buf << encode_message(3, label_buf)
    end
    if label_sets && label_set_id && label_set_id > 0
      ls = label_sets[label_set_id]
      if ls
        ls.each do |k, v|
          label_buf = "".b
          label_buf << encode_int64(1, label_key_indices[k.to_s])  # key
          label_buf << encode_int64(2, string_index[v.to_s])       # str
          sample_buf << encode_message(3, label_buf)
        end
      end
    end
    buf << encode_message(2, sample_buf)
  end

  # field 4: location (repeated Location) — Line points at the same id
  frame_ids.each do |_frame, id|
    loc_buf = "".b
    loc_buf << encode_uint64(1, id)
    line_buf = "".b
    line_buf << encode_uint64(1, id)
    loc_buf << encode_message(4, line_buf)
    buf << encode_message(4, loc_buf)
  end

  # field 5: function (repeated Function)
  frame_ids.each do |frame, func_id|
    func_buf = "".b
    func_buf << encode_uint64(1, func_id)
    func_buf << encode_int64(2, frame[1])    # name (label_idx)
    func_buf << encode_int64(4, frame[0])    # filename (path_idx)
    buf << encode_message(5, func_buf)
  end

  # Intern comment and doc_url strings before encoding string_table
  comment_indices = [
    intern.("rperf #{Rperf::VERSION}"),
    intern.("mode: #{mode}"),
    intern.("frequency: #{frequency}Hz"),
    intern.("ruby: #{RUBY_DESCRIPTION}"),
  ]
  doc_url_idx = intern.("https://ko1.github.io/rperf/docs/help.html")

  # field 6: string_table (repeated string)
  string_table.each do |s|
    buf << encode_bytes(6, s.encode("UTF-8", invalid: :replace, undef: :replace))
  end

  # field 9: time_nanos (int64)
  if data[:start_time_ns]
    buf << encode_int64(9, data[:start_time_ns])
  end

  # field 10: duration_nanos (int64)
  if data[:duration_ns]
    buf << encode_int64(10, data[:duration_ns])
  end

  # field 11: period_type (ValueType)
  buf << encode_message(11, encode_value_type(type_idx, ns_idx))

  # field 12: period (int64)
  buf << encode_int64(12, interval_ns)

  # field 13: comment (repeated int64 = string_table index)
  comment_indices.each { |idx| buf << encode_int64(13, idx) }

  # field 15: doc_url (int64 = string_table index)
  buf << encode_int64(15, doc_url_idx)

  buf
end

.encode_bytes(field, data) ⇒ Object



1481
1482
1483
1484
# File 'lib/rperf.rb', line 1481

def encode_bytes(field, data)
  data = data.b if data.respond_to?(:b)
  encode_varint((field << 3) | 2) + encode_varint(data.bytesize) + data
end

.encode_int64(field, value) ⇒ Object



1477
1478
1479
# File 'lib/rperf.rb', line 1477

def encode_int64(field, value)
  encode_varint((field << 3) | 0) + encode_varint(value < 0 ? value + (1 << 64) : value)
end

.encode_message(field, data) ⇒ Object



1486
1487
1488
# File 'lib/rperf.rb', line 1486

def encode_message(field, data)
  encode_bytes(field, data)
end

.encode_packed_int64(field, values) ⇒ Object



1499
1500
1501
1502
# File 'lib/rperf.rb', line 1499

def encode_packed_int64(field, values)
  inner = values.map { |v| encode_varint(v < 0 ? v + (1 << 64) : v) }.join
  encode_bytes(field, inner)
end

.encode_packed_uint64(field, values) ⇒ Object



1494
1495
1496
1497
# File 'lib/rperf.rb', line 1494

def encode_packed_uint64(field, values)
  inner = values.map { |v| encode_varint(v) }.join
  encode_bytes(field, inner)
end

.encode_uint64(field, value) ⇒ Object



1473
1474
1475
# File 'lib/rperf.rb', line 1473

def encode_uint64(field, value)
  encode_varint((field << 3) | 0) + encode_varint(value)
end

.encode_value_type(type_idx, unit_idx) ⇒ Object



1490
1491
1492
# File 'lib/rperf.rb', line 1490

def encode_value_type(type_idx, unit_idx)
  encode_int64(1, type_idx) + encode_int64(2, unit_idx)
end

.encode_varint(value) ⇒ Object

— Protobuf encoding helpers —



1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
# File 'lib/rperf.rb', line 1457

def encode_varint(value)
  value = value & 0xFFFFFFFF_FFFFFFFF if value < 0
  buf = "".b
  loop do
    byte = value & 0x7F
    value >>= 7
    if value > 0
      buf << (byte | 0x80).chr
    else
      buf << byte.chr
      break
    end
  end
  buf
end