gpt4 book ai didi

emacs - 分析 Emacs Lisp 行为不当的技巧?

转载 作者:行者123 更新时间:2023-12-03 06:37:47 36 4
gpt4 key购买 nike

我经常定制 Emacs。最近,我在 .emacs 配置中添加了一些东西,偶尔会将我的 CPU 固定在 100%,但我真的不知道它是什么。

如果我按 C-g 多次,最终我会在迷你缓冲区下方收到一条消息,询问我是否要自动保存文件,然后是否要完全中止 emacs。如果我一直说“不”并一直按 C-g,最终我可以恢复正常运行 emacs。大约一个小时后,它会再次发生。

我可以继续像现在这样,注释掉我最近添加的各种内容,重新启动 emacs,试图缩小罪魁祸首的范围,但进展缓慢。

有没有办法可以直接分析 emacs 来找出哪个 lisp 函数占用了 CPU?

最佳答案

debug-on-quit 设置为 t 以便您可以了解 Emacs 正在做什么的建议是一个很好的建议。您可以将其视为使用单个样本进行采样分析的一种形式:通常单个样本就足够了。

<小时/>

更新:从版本 24.3 开始,Emacs 包含两个 分析器。 profiler.el 中有一个(新)采样分析器,elp.el 中有一个(旧)仪器分析器。

采样分析器是 documented here 。使用起来非常简单:

To begin profiling, type M-x profiler-start. You can choose to profile by processor usage, memory usage, or both. After doing some work, type M-x profiler-report to display a summary buffer for each resource that you chose to profile. When you have finished profiling, type M-x profiler-stop.

以下是使用 Perforce/Emacs integrationcpu+mem 探查器 session 的一些示例输出我坚持认为。我扩展了最上面的函数 (progn) 以查找 where the CPU time and memory use is coming from .

Function                                            Bytes    %
- progn 26,715,850 29%
- let 26,715,850 29%
- while 26,715,850 29%
- let 26,715,850 29%
- cond 26,715,850 29%
- insert 26,715,850 29%
+ c-after-change 26,713,770 29%
+ p4-file-revision-annotate-links 2,080 0%
+ let 20,431,797 22%
+ call-interactively 12,767,261 14%
+ save-current-buffer 10,005,836 11%
+ while 8,337,166 9%
+ p4-annotate-internal 5,964,974 6%
+ p4-annotate 2,821,034 3%
+ let* 2,089,810 2%

你可以看到罪魁祸首是c-after-change,所以看起来我可以通过locally binding inhibit-modification-hooks to t around this code节省大量CPU时间和内存。 .

<小时/>

您还可以使用 Emacs Lisp Profiler。这是相当缺乏记录的:您必须阅读 elp.el 中的注释以了解详细信息,但基本上您运行 elp-instrument-package 来打开分析对于具有给定前缀的所有函数,然后使用 elp-results 查看结果。

下面是输入 M-x elp-instrument-package RET c- RET、对 4,000 行 C 进行字体化,然后运行 ​​elp-results(并使用 elp-sort-by-function 按调用计数排序):

Function Name                  Call Count  Elapsed Time  Average Time
============================= ========== ============ ============
c-skip-comments-and-strings 107 0.0 0.0
c-valid-offset 78 0.0 0.0
c-set-offset 68 0.031 0.0004558823
c-end-of-macro 52 0.0 0.0
c-neutralize-CPP-line 52 0.0 0.0
c-font-lock-invalid-string 20 0.0 0.0
c-set-style-1 19 0.031 0.0016315789
...

在您的特定情况下,分析器不会立即提供帮助,因为您不知道哪个包有问题。但如果您可以猜测(或使用 debug-on-quit 来确定),那么探查器可以帮助您详细诊断问题。

关于emacs - 分析 Emacs Lisp 行为不当的技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/568150/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com