0.0
No commit activity in last 3 years
No release in over 3 years
Read the CPU's performance counters perf_event_open(2)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

PerfCounters

Experimental, do not use in production, could blow up your Rubies.

What

Read the CPU's performance counters from Ruby using perf_event_open(2).

Installation

$ gem install perf_counters

Usage

require 'perf_counters'
events = [
  Event::INSTRUCTIONS,
  Event::CPU_CYCLES,
  Event::CACHE_MISSES,
]
perf = PerfCounters::Measurement.new(
  exclude_kernel: true,
  events: events,
)
perf.start
# do something here
perf.stop
=> {:instructions=>3276, :cpu_cycles=>18651, :cache_misses=>24}

Sample Rack middleware in a Rails app

class PerformanceCountersRackMiddleware
  def initialize app
    @app = app
  end

  def call env
    events = [
      Event::INSTRUCTIONS,
      Event::CPU_CYCLES,
      Event::CACHE_MISSES,
      Event::CONTEXT_SWITCHES,
      Event::BUS_CYCLES,
      Event::PAGE_FAULTS_MIN,
    ]
    perf_data = PerfCounters.measure(exclude_kernel: true, events: events) do
      @status, @headers, @response = @app.call(env)
    end

    Rails.logger.info "#{perf_data}"

    [@status, @headers, @response]
  end
end

For 2 request it outputs something like:

{:instructions=>93930165, :cpu_cycles=>69263494, :cache_misses=>101678, :context_switches=>0, :bus_cycles=>2663999, :page_faults_min=>216}
[...]
Completed 200 OK in 71ms (Views: 54.7ms)


{:instructions=>93984918, :cpu_cycles=>74644389, :cache_misses=>116050, :context_switches=>0, :bus_cycles=>2870920, :page_faults_min=>246}
[...]
Completed 200 OK in 50ms (Views: 38.2ms)

Contributing

Requirements

A modern Linux machine (unfortunately, most VMs don't virtualize perf counters) with perf installed.

Install

$ bin/setup

Compile

$ rake compile

Run tests

$ rake [test]