Module: Rperf::Meta

Defined in:
lib/rperf/meta.rb

Constant Summary collapse

FORMAT_VERSION =
1
TOP_METHODS_LIMIT =
50
READ_CHUNK =

--- meta/summary prefix reader ---

64 * 1024
READ_LIMIT =
8 * 1024 * 1024
DQUOTE =

Byte codes used by the scanner. Byte-wise scanning is safe in UTF-8: continuation bytes are >= 0x80 and never collide with ASCII syntax.

0x22
BSLASH =
0x5c
LBRACE =
0x7b
RBRACE =
0x7d
LBRACKET =
0x5b
RBRACKET =
0x5d
COMMA =
0x2c
COLON =
0x3a

Class Method Summary collapse

Class Method Details

.build_meta(data) ⇒ Object

Build the meta hash for a profile about to be written. Git info comes from RPERF_META_GIT (set by the CLI, which collects it before exec so a chdir in the profiled app cannot point at the wrong repository); when unset (direct API usage) it is collected here. RPERF_META_GIT="null" means "already checked, not a repository".



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rperf/meta.rb', line 88

def build_meta(data)
  meta = {
    format_version: FORMAT_VERSION,
    created_at: Time.now.utc.iso8601,
    ruby_version: RUBY_VERSION,
    rperf_version: Rperf::VERSION,
    mode: (data[:mode] || :cpu).to_s,
  }
  hostname = safe_hostname
  meta[:hostname] = hostname if hostname
  git = git_from_env_or_collect
  meta[:git] = git if git
  labels = labels_from_env
  meta[:labels] = labels if labels && !labels.empty?
  meta
end

.build_summary(data) ⇒ Object

Build the summary hash from profile data (as returned by Rperf.stop). Fields whose source data is missing are omitted.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rperf/meta.rb', line 144

def build_summary(data)
  s = {}
  s[:total_ms] = (data[:duration_ns] / 1e6).round(1) if data[:duration_ns]
  if data[:user_ns] || data[:sys_ns]
    s[:cpu_ms] = (((data[:user_ns] || 0) + (data[:sys_ns] || 0)) / 1e6).round(1)
  end
  if (gc = data[:gc_stats])
    s[:gc_count_minor] = gc[:minor_count] if gc[:minor_count]
    s[:gc_count_major] = gc[:major_count] if gc[:major_count]
    s[:gc_ms] = gc[:time_ms].to_f.round(1) if gc[:time_ms]
    s[:allocated_objects] = gc[:allocated_objects] if gc[:allocated_objects]
    s[:freed_objects] = gc[:freed_objects] if gc[:freed_objects]
  end
  s[:maxrss_mb] = data[:maxrss_mb] if data[:maxrss_mb]
  s[:samples] = data[:sampling_count] if data[:sampling_count]
  s[:top_methods] = top_methods(data)
  s
end

.collect_git(dir = Dir.pwd) ⇒ Object

Collect git information for the profiled working directory. GitHub Actions environment variables take priority over git commands (CI checkouts may be detached or shallow). Returns a Hash with sha/branch/subject/committed_at/dirty, or nil when not in a git repository or git is unavailable.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rperf/meta.rb', line 29

def collect_git(dir = Dir.pwd)
  gh_sha = ENV["GITHUB_SHA"]
  # Validate the sha shape: the value is passed to git as a positional
  # argument, and a crafted value starting with "-" would be parsed as
  # a git option
  if gh_sha && gh_sha.match?(/\A\h{7,64}\z/)
    git = { sha: gh_sha, dirty: false }
    branch = ENV["GITHUB_HEAD_REF"]
    branch = ENV["GITHUB_REF_NAME"] if branch.nil? || branch.empty?
    git[:branch] = branch if branch && !branch.empty?
    # Enrich from the local checkout when possible (may fail on shallow clones)
    subject = git_capture(dir, "log", "-1", "--format=%s", gh_sha)
    committed_at = git_capture(dir, "log", "-1", "--format=%cI", gh_sha)
    git[:subject] = subject if subject && !subject.empty?
    git[:committed_at] = committed_at if committed_at && !committed_at.empty?
    return git
  end

  sha = git_capture(dir, "rev-parse", "HEAD")
  return nil if sha.nil? || sha.empty?

  git = { sha: sha }
  branch = git_capture(dir, "rev-parse", "--abbrev-ref", "HEAD")
  git[:branch] = branch if branch && !branch.empty? && branch != "HEAD"
  subject = git_capture(dir, "log", "-1", "--format=%s")
  git[:subject] = subject if subject && !subject.empty?
  committed_at = git_capture(dir, "log", "-1", "--format=%cI")
  git[:committed_at] = committed_at if committed_at && !committed_at.empty?
  status = git_capture(dir, "status", "--porcelain")
  git[:dirty] = !status.empty? if status
  git
end

.finalize_scan(found) ⇒ Object



281
282
283
# File 'lib/rperf/meta.rb', line 281

def finalize_scan(found)
  found.empty? ? nil : { meta: found[:meta], summary: found[:summary] }
end

.git_capture(dir, *args) ⇒ Object

Run a git command, returning stripped stdout or nil on failure (no git binary, not a repository, etc.).



64
65
66
67
68
69
# File 'lib/rperf/meta.rb', line 64

def git_capture(dir, *args)
  out = IO.popen(["git", "-C", dir, *args], err: File::NULL, &:read)
  $?.success? ? out.strip : nil
rescue SystemCallError
  nil
end

.git_from_env_or_collectObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rperf/meta.rb', line 105

def git_from_env_or_collect
  if ENV.key?("RPERF_META_GIT")
    v = ENV["RPERF_META_GIT"].to_s
    return nil if v.empty? || v == "null"
    begin
      require "json"
      JSON.parse(v, symbolize_names: true)
    rescue JSON::ParserError
      nil
    end
  else
    # Memoized (array wraps a legitimate nil): periodic viewer snapshots
    # must not spawn git subprocesses on every take_snapshot!
    @collect_git_memo ||= [collect_git]
    @collect_git_memo[0]
  end
end

.labels_from_envObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rperf/meta.rb', line 123

def labels_from_env
  v = ENV["RPERF_META_LABELS"]
  return nil unless v
  begin
    require "json"
    labels = JSON.parse(v)
    labels.is_a?(Hash) ? labels : nil
  rescue JSON::ParserError
    nil
  end
end

.read(path) ⇒ Object

Read meta/summary from a .json(.gz) profile without loading the body. Returns { meta: Hash|nil, summary: Hash|nil } or nil for files without leading meta/summary keys (old format) and unreadable/corrupt files.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rperf/meta.rb', line 189

def read(path)
  File.open(path, "rb") do |f|
    magic = f.read(2)
    f.rewind
    io = (magic == "\x1f\x8b".b) ? Zlib::GzipReader.new(f) : f
    begin
      buf = "".b
      loop do
        chunk = io.read(READ_CHUNK)
        buf << chunk if chunk
        result = scan_prefix(buf)
        return result unless result == :incomplete
        return nil if chunk.nil? || buf.bytesize > READ_LIMIT
      end
    ensure
      # Free the inflate zstream now — directory listings open many files
      # and the buffers would otherwise linger until GC
      io.close if io.is_a?(Zlib::GzipReader)
    end
  end
rescue Zlib::Error, SystemCallError, JSON::ParserError
  # Zlib::Error covers GzipFile::Error (truncated) and also DataError /
  # BufError (valid gzip header, corrupt deflate body) — one corrupt
  # snapshot must not break listing an entire directory
  nil
end

.safe_hostnameObject



135
136
137
138
139
140
# File 'lib/rperf/meta.rb', line 135

def safe_hostname
  require "socket"
  Socket.gethostname
rescue StandardError
  nil
end

.scan_prefix(buf) ⇒ Object

Scan the head of a JSON object for top-level "meta" / "summary" keys. rperf writes them first, so scanning stops at the first other key — large sample arrays are never traversed. Returns { meta:, summary: }, nil (old format / malformed), or :incomplete (need more input).



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rperf/meta.rb', line 232

def scan_prefix(buf)
  require "json" # lazy: see the note at the top of this file
  n = buf.bytesize
  i = skip_ws(buf, 0, n)
  return :incomplete if i >= n
  return nil unless buf.getbyte(i) == LBRACE
  i += 1
  found = {}

  loop do
    i = skip_ws(buf, i, n)
    return :incomplete if i >= n
    return finalize_scan(found) if buf.getbyte(i) == RBRACE
    return nil unless buf.getbyte(i) == DQUOTE

    key_start = i
    i = scan_string(buf, i, n)
    return :incomplete unless i
    key = buf.byteslice(key_start + 1, i - key_start - 2)

    # First key that is not meta/summary ends the scan (old format or body)
    return finalize_scan(found) unless key == "meta" || key == "summary"

    i = skip_ws(buf, i, n)
    return :incomplete if i >= n
    return nil unless buf.getbyte(i) == COLON
    i += 1
    i = skip_ws(buf, i, n)
    return :incomplete if i >= n

    vstart = i
    i = scan_value(buf, i, n)
    return :incomplete unless i
    fragment = buf.byteslice(vstart, i - vstart).force_encoding(Encoding::UTF_8)
    found[key.to_sym] = JSON.parse(fragment, symbolize_names: true)
    return finalize_scan(found) if found.key?(:meta) && found.key?(:summary)

    i = skip_ws(buf, i, n)
    return :incomplete if i >= n
    case buf.getbyte(i)
    when COMMA then i += 1
    when RBRACE then return finalize_scan(found)
    else return nil
    end
  end
rescue JSON::ParserError
  nil
end

.scan_string(buf, i, n) ⇒ Object

Scan a JSON string starting at the opening quote. Returns the index just past the closing quote, or nil if truncated.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rperf/meta.rb', line 296

def scan_string(buf, i, n)
  j = i + 1
  while j < n
    b = buf.getbyte(j)
    if b == BSLASH
      j += 2
    elsif b == DQUOTE
      return j + 1
    else
      j += 1
    end
  end
  nil
end

.scan_value(buf, i, n) ⇒ Object

Scan a JSON value (string, container, or scalar) starting at i. Returns the index just past the value, or nil if truncated.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/rperf/meta.rb', line 313

def scan_value(buf, i, n)
  case buf.getbyte(i)
  when DQUOTE
    scan_string(buf, i, n)
  when LBRACE, LBRACKET
    depth = 0
    j = i
    while j < n
      b = buf.getbyte(j)
      if b == DQUOTE
        j = scan_string(buf, j, n)
        return nil unless j
      elsif b == LBRACE || b == LBRACKET
        depth += 1
        j += 1
      elsif b == RBRACE || b == RBRACKET
        depth -= 1
        j += 1
        return j if depth == 0
      else
        j += 1
      end
    end
    nil
  else
    # scalar: number, true, false, null
    j = i
    while j < n
      b = buf.getbyte(j)
      break if b == COMMA || b == RBRACE || b == RBRACKET ||
               b == 0x20 || b == 0x09 || b == 0x0a || b == 0x0d
      j += 1
    end
    j < n ? j : nil
  end
end

.skip_ws(buf, i, n) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/rperf/meta.rb', line 285

def skip_ws(buf, i, n)
  while i < n
    b = buf.getbyte(i)
    break unless b == 0x20 || b == 0x09 || b == 0x0a || b == 0x0d
    i += 1
  end
  i
end

.snapshot_filename(git, time: Time.now.utc, pid: Process.pid) ⇒ Object

File name used by rperf record --snapshot-dir. In a git repository: rperf--.json.gz Outside: rperf-nogit--.json.gz



74
75
76
77
78
79
80
81
# File 'lib/rperf/meta.rb', line 74

def snapshot_filename(git, time: Time.now.utc, pid: Process.pid)
  ts = time.utc.strftime("%Y%m%dT%H%M%SZ")
  if git && git[:sha]
    "rperf-#{git[:sha][0, 7]}-#{ts}.json.gz"
  else
    "rperf-nogit-#{ts}-#{pid}.json.gz"
  end
end

.top_methods(data, limit: TOP_METHODS_LIMIT) ⇒ Object

Top methods by self time, merged by method name (shares the by-name fold with Table so report/summary numbers can never diverge).



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rperf/meta.rb', line 165

def top_methods(data, limit: TOP_METHODS_LIMIT)
  samples = data[:aggregated_samples]
  return [] if !samples || samples.empty?

  flat_by_name, cum_by_name, total = Table.flat_cum_by_name(data)
  return [] if total <= 0

  flat_by_name.sort_by { |_, w| -w }.first(limit).map do |name, w|
    {
      name: name,
      self_pct: (w * 100.0 / total).round(1),
      total_pct: (cum_by_name[name] * 100.0 / total).round(1),
    }
  end
end