|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# A small demo to visualize COW-related minor page fault trends. |
| 4 | +# - Allocates page-sized strings in an array. |
| 5 | +# - Forks a child that mutates one byte of a different string each second. |
| 6 | +# - Parent samples Process::Metrics and prints minor/major fault deltas and unique memory growth. |
| 7 | + |
| 8 | +require "process/metrics" |
| 9 | + |
| 10 | +PAGE_SIZE = Integer(ENV.fetch("PAGE_SIZE", "4096")) # bytes |
| 11 | +PAGES = Integer(ENV.fetch("PAGES", "128")) # number of page-sized strings |
| 12 | +DURATION = Integer(ENV.fetch("DURATION", "30")) # seconds to run/monitor |
| 13 | + |
| 14 | +# Allocate an array of page-sized mutable strings: |
| 15 | +array = Array.new(PAGES) {"\x00" * PAGE_SIZE} |
| 16 | + |
| 17 | +child_pid = fork do |
| 18 | + $0 = "cow-child" |
| 19 | + i = 0 |
| 20 | + start = Time.now |
| 21 | + while (Time.now - start) < DURATION |
| 22 | + idx = i % PAGES |
| 23 | + s = array[idx] |
| 24 | + # Mutate a single byte; this should trigger a COW on the underlying page on first write: |
| 25 | + s.setbyte(0, (s.getbyte(0) + 1) & 0xFF) |
| 26 | + i += 1 |
| 27 | + sleep 1 |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +puts "Monitoring child PID=#{child_pid} for #{DURATION}s (PAGE_SIZE=#{PAGE_SIZE}, PAGES=#{PAGES})" |
| 32 | + |
| 33 | +last_minor = nil |
| 34 | +last_major = nil |
| 35 | +last_unique = nil |
| 36 | + |
| 37 | +DURATION.times do |t| |
| 38 | + procs = Process::Metrics::General.capture(pid: child_pid) |
| 39 | + proc = procs[child_pid] |
| 40 | + unless proc |
| 41 | + puts "[%2ds] child process is no longer listed" % t |
| 42 | + break |
| 43 | + end |
| 44 | + mem = proc.memory |
| 45 | + unless mem |
| 46 | + puts "[%2ds] detailed memory metrics not available on this platform" % t |
| 47 | + break |
| 48 | + end |
| 49 | + |
| 50 | + minor = mem.minor_faults.to_i |
| 51 | + major = mem.major_faults.to_i |
| 52 | + unique = mem.unique_size.to_i # kB |
| 53 | + |
| 54 | + delta_minor = last_minor ? (minor - last_minor) : 0 |
| 55 | + delta_major = last_major ? (major - last_major) : 0 |
| 56 | + delta_unique = last_unique ? (unique - last_unique) : 0 |
| 57 | + |
| 58 | + puts "[%2ds] minor: %d (+%d), major: %d (+%d), unique_kB: %d (+%d)" % [t, minor, delta_minor, major, delta_major, unique, delta_unique] |
| 59 | + |
| 60 | + last_minor = minor |
| 61 | + last_major = major |
| 62 | + last_unique = unique |
| 63 | + |
| 64 | + sleep 1 |
| 65 | +end |
| 66 | + |
| 67 | +begin |
| 68 | + Process.kill(:TERM, child_pid) |
| 69 | +rescue Errno::ESRCH |
| 70 | + # already exited |
| 71 | +end |
| 72 | + |
| 73 | +begin |
| 74 | + _, status = Process.wait2(child_pid) |
| 75 | + if status |
| 76 | + if status.signaled? |
| 77 | + puts "Child terminated by signal #{status.termsig}" |
| 78 | + else |
| 79 | + puts "Child exited with status #{status.exitstatus}" |
| 80 | + end |
| 81 | + end |
| 82 | +rescue Errno::ECHILD |
| 83 | + # already reaped |
| 84 | +end |
0 commit comments