DIY Ruby CPU 分析——Part I

【编者按】原文做者 Emil Soman,Rubyist,除此以外居然同时也是艺术家,吉他手,Garden City RubyConf 组织者。本文是DIY Ruby CPU Profiling 的第一部分,由 OneAPM 工程师编译整理。html

alt

在 Codemancers,咱们正在建设 Rbkit——一个针对 Ruby 语言的——拥有新炫酷功能的代码分析器。我目前正在实现一个嵌在 rbkit gem 里的 CPU 分析器,这将有助 rbkit UI 重建分析 Ruby 进程调用图,并在屏幕上得出有用的可视化展现。在这个过程当中,我学到了许多新东西,很乐意在本系列的博客文章中与您分享。ruby

咱们打算一步一步从基础开始,专门为 Ruby 编写一个初级的 CPU 分析器!在完成时咱们将学到:服务器

  • 什么是 CPU 分析
  • 分析模式——工具和采样
  • CPU Time 和 Wall Time ——它们分别是什么意思,如何测量?
  • 写一个简单的 C 扩展并用于 Ruby 中
  • Ruby Tracepoints——调用和返回
  • C 语言中的信号处理
  • 用一个信号暂停 Ruby 进程并用调用堆查看
  • 用分析数据进行一些有用但笨拙的试验工具

    Part I. 介绍 CPU 分析

    经过对你的程序进行 CPU 分析,能够发现相较于 CPU 使用率,你的程序是多么宝贵。为了分析程序,你须要使用一个分析工具并按照下列步骤操做:性能

  • 开始 CPU 剖析
  • 执行你想要分析的代码
  • 中止 CPU 剖析并获得剖析结果
  • 分析结果
  • 经过分析剖析结果,你会发现使整个程序缓慢的瓶颈。code

分析模式

CPU 剖析能够分为如下两种方法:htm

1. 工具

在这种模式下,分析工具利用一些 hooks,由解释器提供或者插入程序中,来了解调用图并测量在调用图中每一个方法的执行时间。举个例子,看一下下面这段 Ruby 代码:进程

def main
3.times do
find_many_square_roots
find_many_squares
end
end


def find_many_square_roots
5000.times{|i| Math.sqrt(i)}
end

def find_many_squares
5000.times{|i| i**2 }
end

main

我已经插入了一些内容,来帮助了解若是 Ruby 解释器给了咱们方法的调用和返回的 hooks,它们如何执行:get

def main
# method call hook gets executed
3.times do
find_many_square_roots
find_many_squares
end
# method end hook gets executed
end

def find_many_square_roots
# method call hook gets executed
5000.times{|i| Math.sqrt(i)}
# method end hook gets executed
end

def find_many_squares
# method call hook gets executed
5000.times{|i| i**2 }
# method end hook gets executed
end

main

如今,若是咱们可以打印出当前时间和这些 hooks 内部当前方法的名称,会获得看起来像这种形式的输出结果:博客

sec:00 usec:201007  called      main
sec:00 usec:201108  called      find_many_square_roots
sec:00 usec:692123  returned    find_many_square_roots
sec:00 usec:692178  called      find_many_squares
sec:00 usec:846540  returned    find_many_squares
sec:00 usec:846594  called      find_many_square_roots
sec:01 usec:336166  returned    find_many_square_roots
sec:01 usec:336215  called      find_many_squares
sec:01 usec:484880  returned    find_many_squares
sec:01 usec:484945  called      find_many_square_roots
sec:01 usec:959254  returned    find_many_square_roots
sec:01 usec:959315  called      find_many_squares
sec:02 usec:106474  returned    find_many_squares
sec:02 usec:106526  returned    main

正如你所看到的,此输出能够告诉咱们在每一种方法里面花了多长时间。同时也告诉咱们,每个方法调用的次数。这大概就解释了性能分析工具是如何工做的。

优势:

高精度
咱们获得了方法调用数
易于实施

缺点:

每一个被分析的方法执行 hooks 时的额外开销

2. 采样

在采样模式下,分析器每隔 x 时间单元打断一次程序,并查看调用堆并记录它的信息(被称为“样品”)。一旦该程序完成运行,分析器收集全部样品并找出每一个方法出如今全部样品中的次数。
很难想象?让咱们来看看一样的例子代码,看看若是咱们使用采样分析器,输出结果会有怎样的不一样。
采样分析器的输出结果以下:

Call stack at 0.5sec: main/find_many_square_roots
Call stack at 1.0sec: main/find_many_square_roots
Call stack at 1.5sec: main/find_many_square_roots
Call stack at 2.0sec: main/find_many_squares

在这个例子中,程序每 0.5 秒被中断一次而且调用堆栈被记录。所以,经过这个程序执行的过程咱们获得了 4 个样品,find_many_square_roots 记录于 3 个样品中, find_many_squares 只存在于一个样品中。从本次采样中,咱们获得 find_many_square_roots 占用了 75% CPU,与此同时 find_many_squares 只占用了 25% 的 CPU 。这就大概解释了分析器是怎么样工做的。

优势:

与工具分析相比开销可忽略不计
很容易找到缓慢/长时间运行的方法

缺点:

不擅长测量短期运行的方法
咱们没有获得方法调用数
很难本身写出采样分析器

归纳

咱们只是调查了 CPU 分析的含义和两种经常使用的 CPU 分析方法。在第 2 部分,咱们将探讨对描述 CPU 使用状况的 2 个单位进行测量—— CPU Time 和 Wall Time。咱们也会亲手写一些代码来获取进行测量。感谢您的阅读!

OneAPM for Ruby 可以深刻到全部 Ruby 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。 想阅读更多技术文章,请访问 OneAPM 官方博客

相关文章
相关标签/搜索