Ruby DIY Ruby CPU 分析——Part I

oneapm · 2015年09月25日 · 最后由 adamshen 回复于 2015年09月26日 · 1980 次阅读

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

alt

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

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

  • 什么是 CPU 分析
  • 分析模式——工具和采样
  • CPU Time 和 Wall Time ——它们分别是什么意思,如何测量?
  • 写一个简单的 C 扩展并用于 Ruby 中
  • Ruby Tracepoints——调用和返回
  • C 语言中的信号处理
  • 用一个信号暂停 Ruby 进程并用调用堆查看
  • 用分析数据进行一些有用但笨拙的试验 ###Part I. 介绍 CPU 分析 通过对你的程序进行 CPU 分析,可以发现相较于 CPU 使用率,你的程序是多么宝贵。为了分析程序,你需要使用一个分析工具并按照下列步骤操作:

  • 开始 CPU 剖析

  • 执行你想要分析的代码

  • 停止 CPU 剖析并得到剖析结果

  • 分析结果

  • 通过分析剖析结果,你会发现使整个程序缓慢的瓶颈。

分析模式

CPU 剖析可以分为以下两种方法:

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,它们如何执行:

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 官方博客

rbkit 听起来像是又一个神器啊。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号