简体   繁体   English

ghc运行时如何支持概要分析?

[英]How is the ghc runtime support for profiling implemented?

I did not find much documentation in the commentery. 我在评论中找不到太多文档。 Are there any good blog posts or similarly on this? 是否有任何好的博客文章或类似的文章?

The best source for information on the profiling framework might still be the original paper by Patrick Sansom and Simon Peyton Jones. 关于概要分析框架的最佳信息来源仍可能是Patrick Sansom和Simon Peyton Jones的原始论文 Additional details can be found in Sansom's PhD thesis as well as the later paper adding a formal specification. 其他细节可以在Sansom的博士学位论文以及后来添加正式规范的论文中找到。 Simon Marlow also spoke about a few recent changes in the GHC Status Update at Haskell Implementors' Workshop 2011 . 西蒙·马洛(Simon Marlow)在2011年Haskell实施者研讨会上还谈到了GHC状态更新中的一些最新变化。

The idea behind cost-centre profiling is to annotate the expression tree with "cost centre" nodes, so for example with -auto-all the program will have annotations like follows: 成本中心配置文件背后的想法是用“成本中心”节点注释表达式树,因此,例如,使用-auto-all程序将具有如下注释:

fib n = {-# SCC foo #-} (case n of
                           0 -> 0
                           1 -> 1
                           n -> fib (n-1) + fib (n-2))

At runtime when entering fib , the program would look at the current "cost centre stack" and add "foo" to the top. 在运行时,输入fib ,程序将查看当前的“成本中心堆栈”,并将“ foo”添加到顶部。 This would be reversed once the evaluation exits the scope of the SCC annotation again. 一旦评估再次退出SCC注释的范围,这将被撤销。 A bit of magic ensures that if, say, n happens to be a lazy value and the program needs to execute its code, the cost centre appropriate for that code is restored where necessary. 魔确保的比特,如果,比如说, n恰好是一个懒惰值并且该程序需要执行它的代码,成本中心适合于代码被恢复在必要。

This infrastructure is then used for both time as well as space profiling: 然后,此基础结构既用于时间分析,又用于空间分析:

  1. A timer will check the cost-centre stack periodically. 计时器将定期检查成本中心堆栈。 Every time a certain cost-centre stack is found, this counts as a "tick". 每次找到某个成本中心堆栈时,这都被视为“滴答”。 In the end, the RTS will estimate the amount of time per cost-centre stack from the count of its ticks, giving you a time profile. 最后,RTS将根据其滴答声的计数来估计每个成本中心堆栈的时间量,从而为您提供时间概况。

  2. Every time an object is allocated, the program saves back a pointer to the cost-centre stack that was current at that point in time. 每次分配对象时,程序都会保存指向该时间点当前成本中心堆栈的指针。 This enables the garbage collector to provide a statistic of how many bytes were resident, broken down by allocation site. 这使垃圾收集器可以提供驻留的字节数的统计信息,并按分配站点进行细分。

As requested in the comment, a few words on optimization: For obvious reasons the framework can not allow optimizations that move non-constant costs from one cost centre to the other, forcing the optimizer to be quite pessimistic at times. 正如评论中所要求的,关于优化的几句话:由于显而易见的原因,该框架不允许进行将非恒定成本从一个成本中心转移到另一个成本中心的优化,这迫使优化器有时会非常悲观。 For example, in the above example the current release GHC will not be able to unbox the return value, meaning that each recursive call do an unnecessary heap-allocation. 例如,在上面的示例中,当前版本的GHC将无法对返回值进行拆箱,这意味着每个递归调用都会进行不必要的堆分配。

As a rule of thumb, one should not count on any code transformations happening across a SCC annotation. 根据经验,不应指望 SCC注释发生的任何代码转换。 When in doubt, it is better to annotate a function sufficiently high in the call-stack, so the performance-critical bits do not get annotated at all. 如有疑问,最好在调用堆栈中对足够高的函数进行注释,因此,对性能至关重要的位根本不会进行注释。

You might find this paper by Jones, Marlow & Singh useful, depending on what you want to accomplish. 您可能会发现Jones,Marlow和Singh撰写的这篇论文很有用,取决于您要完成的工作。 It includes practices for profiling GHC programs in a parallel context and contains some case studies that you might find useful. 它包括在并行上下文中对GHC程序进行概要分析的实践,并包含一些可能有用的案例研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM