elp.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;;; elp.el --- Emacs Lisp Profiler
  2. ;; Copyright (C) 1994-1995, 1997-1998, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Barry A. Warsaw
  5. ;; Maintainer: FSF
  6. ;; Created: 26-Feb-1994
  7. ;; Keywords: debugging lisp tools
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; If you want to profile a bunch of functions, set elp-function-list
  22. ;; to the list of symbols, then do a M-x elp-instrument-list. This
  23. ;; hacks those functions so that profiling information is recorded
  24. ;; whenever they are called. To print out the current results, use
  25. ;; M-x elp-results. If you want output to go to standard-output
  26. ;; instead of a separate buffer, setq elp-use-standard-output to
  27. ;; non-nil. With elp-reset-after-results set to non-nil, profiling
  28. ;; information will be reset whenever the results are displayed. You
  29. ;; can also reset all profiling info at any time with M-x
  30. ;; elp-reset-all.
  31. ;;
  32. ;; You can also instrument all functions in a package, provided that
  33. ;; the package follows the GNU coding standard of a common textual
  34. ;; prefix. Use M-x elp-instrument-package for this.
  35. ;;
  36. ;; If you want to sort the results, set elp-sort-by-function to some
  37. ;; predicate function. The three most obvious choices are predefined:
  38. ;; elp-sort-by-call-count, elp-sort-by-average-time, and
  39. ;; elp-sort-by-total-time. Also, you can prune from the output, all
  40. ;; functions that have been called fewer than a given number of times
  41. ;; by setting elp-report-limit.
  42. ;;
  43. ;; Elp can instrument byte-compiled functions just as easily as
  44. ;; interpreted functions, but it cannot instrument macros. However,
  45. ;; when you redefine a function (e.g. with eval-defun), you'll need to
  46. ;; re-instrument it with M-x elp-instrument-function. This will also
  47. ;; reset profiling information for that function. Elp can handle
  48. ;; interactive functions (i.e. commands), but of course any time spent
  49. ;; idling for user prompts will show up in the timing results.
  50. ;;
  51. ;; You can also designate a `master' function. Profiling times will
  52. ;; be gathered for instrumented functions only during execution of
  53. ;; this master function. Thus, if you have some defuns like:
  54. ;;
  55. ;; (defun foo () (do-something-time-intensive))
  56. ;; (defun bar () (foo))
  57. ;; (defun baz () (bar) (foo))
  58. ;;
  59. ;; and you want to find out the amount of time spent in bar and foo,
  60. ;; but only during execution of bar, make bar the master. The call of
  61. ;; foo from baz will not add to foo's total timing sums. Use M-x
  62. ;; elp-set-master and M-x elp-unset-master to utilize this feature.
  63. ;; Only one master function can be set at a time.
  64. ;; You can restore any function's original function definition with
  65. ;; elp-restore-function. The other instrument, restore, and reset
  66. ;; functions are provided for symmetry.
  67. ;; Here is a list of variable you can use to customize elp:
  68. ;; elp-function-list
  69. ;; elp-reset-after-results
  70. ;; elp-sort-by-function
  71. ;; elp-report-limit
  72. ;;
  73. ;; Here is a list of the interactive commands you can use:
  74. ;; elp-instrument-function
  75. ;; elp-restore-function
  76. ;; elp-instrument-list
  77. ;; elp-restore-list
  78. ;; elp-instrument-package
  79. ;; elp-restore-all
  80. ;; elp-reset-function
  81. ;; elp-reset-list
  82. ;; elp-reset-all
  83. ;; elp-set-master
  84. ;; elp-unset-master
  85. ;; elp-results
  86. ;; Note that there are plenty of factors that could make the times
  87. ;; reported unreliable, including the accuracy and granularity of your
  88. ;; system clock, and the overhead spent in lisp calculating and
  89. ;; recording the intervals. I figure the latter is pretty constant,
  90. ;; so while the times may not be entirely accurate, I think they'll
  91. ;; give you a good feel for the relative amount of work spent in the
  92. ;; various lisp routines you are profiling. Note further that times
  93. ;; are calculated using wall-clock time, so other system load will
  94. ;; affect accuracy too.
  95. ;;; Background:
  96. ;; This program was inspired by the only two existing Emacs Lisp
  97. ;; profilers that I'm aware of, Boaz Ben-Zvi's profile.el, and Root
  98. ;; Boy Jim's profiler.el. Both were written for Emacs 18 and both were
  99. ;; pretty good first shots at profiling, but I found that they didn't
  100. ;; provide the functionality or interface that I wanted, so I wrote
  101. ;; this. I've tested elp in XEmacs 19 and Emacs 19. There's no point
  102. ;; in even trying to make this work with Emacs 18.
  103. ;; Unlike previous profilers, elp uses Emacs 19's built-in function
  104. ;; current-time to return interval times. This obviates the need for
  105. ;; both an external C program and Emacs processes to communicate with
  106. ;; such a program, and thus simplifies the package as a whole.
  107. ;; TBD:
  108. ;; Make this act like a real profiler, so that it records time spent
  109. ;; in all branches of execution.
  110. ;;; Code:
  111. ;; start of user configuration variables
  112. ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  113. (defgroup elp nil
  114. "Emacs Lisp Profiler."
  115. :group 'lisp)
  116. (defcustom elp-function-list nil
  117. "List of functions to profile.
  118. Used by the command `elp-instrument-list'."
  119. :type '(repeat function)
  120. :group 'elp)
  121. (defcustom elp-reset-after-results t
  122. "Non-nil means reset all profiling info after results are displayed.
  123. Results are displayed with the `elp-results' command."
  124. :type 'boolean
  125. :group 'elp)
  126. (defcustom elp-sort-by-function 'elp-sort-by-total-time
  127. "Non-nil specifies ELP results sorting function.
  128. These functions are currently available:
  129. elp-sort-by-call-count -- sort by the highest call count
  130. elp-sort-by-total-time -- sort by the highest total time
  131. elp-sort-by-average-time -- sort by the highest average times
  132. You can write your own sort function. It should adhere to the
  133. interface specified by the PREDICATE argument for `sort'.
  134. Each \"element of LIST\" is really a 4 element vector where element 0 is
  135. the call count, element 1 is the total time spent in the function,
  136. element 2 is the average time spent in the function, and element 3 is
  137. the symbol's name string."
  138. :type 'function
  139. :group 'elp)
  140. (defcustom elp-report-limit 1
  141. "Prevent some functions from being displayed in the results buffer.
  142. If a number, no function that has been called fewer than that number
  143. of times will be displayed in the output buffer. If nil, all
  144. functions will be displayed."
  145. :type '(choice integer
  146. (const :tag "Show All" nil))
  147. :group 'elp)
  148. (defcustom elp-use-standard-output nil
  149. "If non-nil, output to `standard-output' instead of a buffer."
  150. :type 'boolean
  151. :group 'elp)
  152. (defcustom elp-recycle-buffers-p t
  153. "If nil, don't recycle the `elp-results-buffer'.
  154. In other words, a new unique buffer is create every time you run
  155. \\[elp-results]."
  156. :type 'boolean
  157. :group 'elp)
  158. ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  159. ;; end of user configuration variables
  160. (defvar elp-results-buffer "*ELP Profiling Results*"
  161. "Buffer name for outputting profiling results.")
  162. (defconst elp-timer-info-property 'elp-info
  163. "ELP information property name.")
  164. (defvar elp-all-instrumented-list nil
  165. "List of all functions currently being instrumented.")
  166. (defvar elp-record-p t
  167. "Controls whether functions should record times or not.
  168. This variable is set by the master function.")
  169. (defvar elp-master nil
  170. "Master function symbol.")
  171. (defvar elp-not-profilable
  172. ;; First, the functions used inside each instrumented function:
  173. '(elp-wrapper called-interactively-p
  174. ;; Then the functions used by the above functions. I used
  175. ;; (delq nil (mapcar (lambda (x) (and (symbolp x) (fboundp x) x))
  176. ;; (aref (symbol-function 'elp-wrapper) 2)))
  177. ;; to help me find this list.
  178. error call-interactively apply current-time
  179. ;; Andreas Politz reports problems profiling these (Bug#4233):
  180. + byte-code-function-p functionp byte-code subrp
  181. indirect-function fboundp)
  182. "List of functions that cannot be profiled.
  183. Those functions are used internally by the profiling code and profiling
  184. them would thus lead to infinite recursion.")
  185. (defun elp-profilable-p (fun)
  186. (and (symbolp fun)
  187. (fboundp fun)
  188. (not (or (memq fun elp-not-profilable)
  189. (keymapp fun)
  190. (memq (car-safe (symbol-function fun)) '(autoload macro))
  191. (condition-case nil
  192. (when (subrp (indirect-function fun))
  193. (eq 'unevalled
  194. (cdr (subr-arity (indirect-function fun)))))
  195. (error nil))))))
  196. ;;;###autoload
  197. (defun elp-instrument-function (funsym)
  198. "Instrument FUNSYM for profiling.
  199. FUNSYM must be a symbol of a defined function."
  200. (interactive "aFunction to instrument: ")
  201. ;; restore the function. this is necessary to avoid infinite
  202. ;; recursion of already instrumented functions (i.e. elp-wrapper
  203. ;; calling elp-wrapper ad infinitum). it is better to simply
  204. ;; restore the function than to throw an error. this will work
  205. ;; properly in the face of eval-defun because if the function was
  206. ;; redefined, only the timer info will be nil'd out since
  207. ;; elp-restore-function is smart enough not to trash the new
  208. ;; definition.
  209. (elp-restore-function funsym)
  210. (let* ((funguts (symbol-function funsym))
  211. (infovec (vector 0 0 funguts))
  212. (newguts '(lambda (&rest args))))
  213. ;; we cannot profile macros
  214. (and (eq (car-safe funguts) 'macro)
  215. (error "ELP cannot profile macro: %s" funsym))
  216. ;; TBD: at some point it might be better to load the autoloaded
  217. ;; function instead of throwing an error. if we do this, then we
  218. ;; probably want elp-instrument-package to be updated with the
  219. ;; newly loaded list of functions. i'm not sure it's smart to do
  220. ;; the autoload here, since that could have side effects, and
  221. ;; elp-instrument-function is similar (in my mind) to defun-ish
  222. ;; type functionality (i.e. it shouldn't execute the function).
  223. (and (eq (car-safe funguts) 'autoload)
  224. (error "ELP cannot profile autoloaded function: %s" funsym))
  225. ;; We cannot profile functions used internally during profiling.
  226. (unless (elp-profilable-p funsym)
  227. (error "ELP cannot profile the function: %s" funsym))
  228. ;; put rest of newguts together
  229. (if (commandp funsym)
  230. (setq newguts (append newguts '((interactive)))))
  231. (setq newguts (append newguts `((elp-wrapper
  232. (quote ,funsym)
  233. ,(when (commandp funsym)
  234. '(called-interactively-p 'any))
  235. args))))
  236. ;; to record profiling times, we set the symbol's function
  237. ;; definition so that it runs the elp-wrapper function with the
  238. ;; function symbol as an argument. We place the old function
  239. ;; definition on the info vector.
  240. ;;
  241. ;; The info vector data structure is a 3 element vector. The 0th
  242. ;; element is the call-count, i.e. the total number of times this
  243. ;; function has been entered. This value is bumped up on entry to
  244. ;; the function so that non-local exists are still recorded. TBD:
  245. ;; I haven't tested non-local exits at all, so no guarantees.
  246. ;;
  247. ;; The 1st element is the total amount of time in seconds that has
  248. ;; been spent inside this function. This number is added to on
  249. ;; function exit.
  250. ;;
  251. ;; The 2nd element is the old function definition list. This gets
  252. ;; funcall'd in between start/end time retrievals. I believe that
  253. ;; this lets us profile even byte-compiled functions.
  254. ;; put the info vector on the property list
  255. (put funsym elp-timer-info-property infovec)
  256. ;; Set the symbol's new profiling function definition to run
  257. ;; elp-wrapper.
  258. (let ((advice-info (get funsym 'ad-advice-info)))
  259. (if advice-info
  260. (progn
  261. ;; If function is advised, don't let Advice change
  262. ;; its definition from under us during the `fset'.
  263. (put funsym 'ad-advice-info nil)
  264. (fset funsym newguts)
  265. (put funsym 'ad-advice-info advice-info))
  266. (fset funsym newguts)))
  267. ;; add this function to the instrumentation list
  268. (unless (memq funsym elp-all-instrumented-list)
  269. (push funsym elp-all-instrumented-list))))
  270. (defun elp-restore-function (funsym)
  271. "Restore an instrumented function to its original definition.
  272. Argument FUNSYM is the symbol of a defined function."
  273. (interactive "aFunction to restore: ")
  274. (let ((info (get funsym elp-timer-info-property)))
  275. ;; delete the function from the all instrumented list
  276. (setq elp-all-instrumented-list
  277. (delq funsym elp-all-instrumented-list))
  278. ;; if the function was the master, reset the master
  279. (if (eq funsym elp-master)
  280. (setq elp-master nil
  281. elp-record-p t))
  282. ;; zap the properties
  283. (put funsym elp-timer-info-property nil)
  284. ;; restore the original function definition, but if the function
  285. ;; wasn't instrumented do nothing. we do this after the above
  286. ;; because its possible the function got un-instrumented due to
  287. ;; circumstances beyond our control. Also, check to make sure
  288. ;; that the current function symbol points to elp-wrapper. If
  289. ;; not, then the user probably did an eval-defun, or loaded a
  290. ;; byte-compiled version, while the function was instrumented and
  291. ;; we don't want to destroy the new definition. can it ever be
  292. ;; the case that a lisp function can be compiled instrumented?
  293. (and info
  294. (functionp funsym)
  295. (not (byte-code-function-p (symbol-function funsym)))
  296. (assq 'elp-wrapper (symbol-function funsym))
  297. (fset funsym (aref info 2)))))
  298. ;;;###autoload
  299. (defun elp-instrument-list (&optional list)
  300. "Instrument, for profiling, all functions in `elp-function-list'.
  301. Use optional LIST if provided instead.
  302. If called interactively, read LIST using the minibuffer."
  303. (interactive "PList of functions to instrument: ")
  304. (unless (listp list)
  305. (signal 'wrong-type-argument (list 'listp list)))
  306. (let ((list (or list elp-function-list)))
  307. (mapcar 'elp-instrument-function list)))
  308. ;;;###autoload
  309. (defun elp-instrument-package (prefix)
  310. "Instrument for profiling, all functions which start with PREFIX.
  311. For example, to instrument all ELP functions, do the following:
  312. \\[elp-instrument-package] RET elp- RET"
  313. (interactive
  314. (list (completing-read "Prefix of package to instrument: "
  315. obarray 'elp-profilable-p)))
  316. (if (zerop (length prefix))
  317. (error "Instrumenting all Emacs functions would render Emacs unusable"))
  318. (elp-instrument-list
  319. (mapcar
  320. 'intern
  321. (all-completions prefix obarray 'elp-profilable-p))))
  322. (defun elp-restore-list (&optional list)
  323. "Restore the original definitions for all functions in `elp-function-list'.
  324. Use optional LIST if provided instead."
  325. (interactive "PList of functions to restore: ")
  326. (let ((list (or list elp-function-list)))
  327. (mapcar 'elp-restore-function list)))
  328. (defun elp-restore-all ()
  329. "Restore the original definitions of all functions being profiled."
  330. (interactive)
  331. (elp-restore-list elp-all-instrumented-list))
  332. (defun elp-reset-function (funsym)
  333. "Reset the profiling information for FUNSYM."
  334. (interactive "aFunction to reset: ")
  335. (let ((info (get funsym elp-timer-info-property)))
  336. (or info
  337. (error "%s is not instrumented for profiling" funsym))
  338. (aset info 0 0) ;reset call counter
  339. (aset info 1 0.0) ;reset total time
  340. ;; don't muck with aref 2 as that is the old symbol definition
  341. ))
  342. (defun elp-reset-list (&optional list)
  343. "Reset the profiling information for all functions in `elp-function-list'.
  344. Use optional LIST if provided instead."
  345. (interactive "PList of functions to reset: ")
  346. (let ((list (or list elp-function-list)))
  347. (mapcar 'elp-reset-function list)))
  348. (defun elp-reset-all ()
  349. "Reset the profiling information for all functions being profiled."
  350. (interactive)
  351. (elp-reset-list elp-all-instrumented-list))
  352. (defun elp-set-master (funsym)
  353. "Set the master function for profiling."
  354. (interactive "aMaster function: ")
  355. ;; when there's a master function, recording is turned off by
  356. ;; default
  357. (setq elp-master funsym
  358. elp-record-p nil)
  359. ;; make sure master function is instrumented
  360. (or (memq funsym elp-all-instrumented-list)
  361. (elp-instrument-function funsym)))
  362. (defun elp-unset-master ()
  363. "Unset the master function."
  364. (interactive)
  365. ;; when there's no master function, recording is turned on by default.
  366. (setq elp-master nil
  367. elp-record-p t))
  368. (defsubst elp-elapsed-time (start end)
  369. (float-time (time-subtract end start)))
  370. (defun elp-wrapper (funsym interactive-p args)
  371. "This function has been instrumented for profiling by the ELP.
  372. ELP is the Emacs Lisp Profiler. To restore the function to its
  373. original definition, use \\[elp-restore-function] or \\[elp-restore-all]."
  374. ;; turn on recording if this is the master function
  375. (if (and elp-master
  376. (eq funsym elp-master))
  377. (setq elp-record-p t))
  378. ;; get info vector and original function symbol
  379. (let* ((info (get funsym elp-timer-info-property))
  380. (func (aref info 2))
  381. result)
  382. (or func
  383. (error "%s is not instrumented for profiling" funsym))
  384. (if (not elp-record-p)
  385. ;; when not recording, just call the original function symbol
  386. ;; and return the results.
  387. (setq result
  388. (if interactive-p
  389. (call-interactively func)
  390. (apply func args)))
  391. ;; we are recording times
  392. (let (enter-time exit-time)
  393. ;; increment the call-counter
  394. (aset info 0 (1+ (aref info 0)))
  395. ;; now call the old symbol function, checking to see if it
  396. ;; should be called interactively. make sure we return the
  397. ;; correct value
  398. (if interactive-p
  399. (setq enter-time (current-time)
  400. result (call-interactively func)
  401. exit-time (current-time))
  402. (setq enter-time (current-time)
  403. result (apply func args)
  404. exit-time (current-time)))
  405. ;; calculate total time in function
  406. (aset info 1 (+ (aref info 1) (elp-elapsed-time enter-time exit-time)))
  407. ))
  408. ;; turn off recording if this is the master function
  409. (if (and elp-master
  410. (eq funsym elp-master))
  411. (setq elp-record-p nil))
  412. result))
  413. ;; shut the byte-compiler up
  414. (defvar elp-field-len nil)
  415. (defvar elp-cc-len nil)
  416. (defvar elp-at-len nil)
  417. (defvar elp-et-len nil)
  418. (defun elp-sort-by-call-count (vec1 vec2)
  419. ;; sort by highest call count. See `sort'.
  420. (>= (aref vec1 0) (aref vec2 0)))
  421. (defun elp-sort-by-total-time (vec1 vec2)
  422. ;; sort by highest total time spent in function. See `sort'.
  423. (>= (aref vec1 1) (aref vec2 1)))
  424. (defun elp-sort-by-average-time (vec1 vec2)
  425. ;; sort by highest average time spent in function. See `sort'.
  426. (>= (aref vec1 2) (aref vec2 2)))
  427. (defsubst elp-pack-number (number width)
  428. ;; pack the NUMBER string into WIDTH characters, watching out for
  429. ;; very small or large numbers
  430. (if (<= (length number) width)
  431. number
  432. ;; check for very large or small numbers
  433. (if (string-match "^\\(.*\\)\\(e[+-].*\\)$" number)
  434. (concat (substring
  435. (match-string 1 number)
  436. 0
  437. (- width (match-end 2) (- (match-beginning 2)) 3))
  438. "..."
  439. (match-string 2 number))
  440. (substring number 0 width))))
  441. (defun elp-output-result (resultvec)
  442. ;; output the RESULTVEC into the results buffer. RESULTVEC is a 4 or
  443. ;; more element vector where aref 0 is the call count, aref 1 is the
  444. ;; total time spent in the function, aref 2 is the average time
  445. ;; spent in the function, and aref 3 is the symbol's string
  446. ;; name. All other elements in the vector are ignored.
  447. (let* ((cc (aref resultvec 0))
  448. (tt (aref resultvec 1))
  449. (at (aref resultvec 2))
  450. (symname (aref resultvec 3))
  451. callcnt totaltime avetime)
  452. (setq callcnt (number-to-string cc)
  453. totaltime (number-to-string tt)
  454. avetime (number-to-string at))
  455. ;; possibly prune the results
  456. (if (and elp-report-limit
  457. (numberp elp-report-limit)
  458. (< cc elp-report-limit))
  459. nil
  460. (elp-output-insert-symname symname)
  461. (insert-char 32 (+ elp-field-len (- (length symname)) 2))
  462. ;; print stuff out, formatting it nicely
  463. (insert callcnt)
  464. (insert-char 32 (+ elp-cc-len (- (length callcnt)) 2))
  465. (let ((ttstr (elp-pack-number totaltime elp-et-len))
  466. (atstr (elp-pack-number avetime elp-at-len)))
  467. (insert ttstr)
  468. (insert-char 32 (+ elp-et-len (- (length ttstr)) 2))
  469. (insert atstr))
  470. (insert "\n"))))
  471. (defvar elp-results-symname-map
  472. (let ((map (make-sparse-keymap)))
  473. (define-key map [mouse-2] 'elp-results-jump-to-definition)
  474. (define-key map [follow-link] 'mouse-face)
  475. (define-key map "\C-m" 'elp-results-jump-to-definition)
  476. map)
  477. "Keymap used on the function name column." )
  478. (defun elp-results-jump-to-definition (&optional event)
  479. "Jump to the definition of the function under the point."
  480. (interactive (list last-nonmenu-event))
  481. (if event (posn-set-point (event-end event)))
  482. (find-function (get-text-property (point) 'elp-symname)))
  483. (defun elp-output-insert-symname (symname)
  484. ;; Insert SYMNAME with text properties.
  485. (insert (propertize symname
  486. 'elp-symname (intern symname)
  487. 'keymap elp-results-symname-map
  488. 'mouse-face 'highlight
  489. 'face 'link
  490. 'help-echo "mouse-2 or RET jumps to definition")))
  491. ;;;###autoload
  492. (defun elp-results ()
  493. "Display current profiling results.
  494. If `elp-reset-after-results' is non-nil, then current profiling
  495. information for all instrumented functions is reset after results are
  496. displayed."
  497. (interactive)
  498. (let ((curbuf (current-buffer))
  499. (resultsbuf (if elp-recycle-buffers-p
  500. (get-buffer-create elp-results-buffer)
  501. (generate-new-buffer elp-results-buffer))))
  502. (set-buffer resultsbuf)
  503. (erase-buffer)
  504. ;; get the length of the longest function name being profiled
  505. (let* ((longest 0)
  506. (title "Function Name")
  507. (titlelen (length title))
  508. (elp-field-len titlelen)
  509. (cc-header "Call Count")
  510. (elp-cc-len (length cc-header))
  511. (et-header "Elapsed Time")
  512. (elp-et-len (length et-header))
  513. (at-header "Average Time")
  514. (elp-at-len (length at-header))
  515. (resvec
  516. (mapcar
  517. (function
  518. (lambda (funsym)
  519. (let* ((info (get funsym elp-timer-info-property))
  520. (symname (format "%s" funsym))
  521. (cc (aref info 0))
  522. (tt (aref info 1)))
  523. (if (not info)
  524. (insert "No profiling information found for: "
  525. symname)
  526. (setq longest (max longest (length symname)))
  527. (vector cc tt (if (zerop cc)
  528. 0.0 ;avoid arithmetic div-by-zero errors
  529. (/ (float tt) (float cc)))
  530. symname)))))
  531. elp-all-instrumented-list))
  532. ) ; end let*
  533. ;; If printing to stdout, insert the header so it will print.
  534. ;; Otherwise use header-line-format.
  535. (setq elp-field-len (max titlelen longest))
  536. (if (or elp-use-standard-output noninteractive)
  537. (progn
  538. (insert title)
  539. (if (> longest titlelen)
  540. (progn
  541. (insert-char 32 (- longest titlelen))))
  542. (insert " " cc-header " " et-header " " at-header "\n")
  543. (insert-char ?= elp-field-len)
  544. (insert " ")
  545. (insert-char ?= elp-cc-len)
  546. (insert " ")
  547. (insert-char ?= elp-et-len)
  548. (insert " ")
  549. (insert-char ?= elp-at-len)
  550. (insert "\n"))
  551. (let ((column 0))
  552. (setq header-line-format
  553. (mapconcat
  554. (lambda (title)
  555. (prog1
  556. (concat
  557. (propertize " "
  558. 'display (list 'space :align-to column)
  559. 'face 'fixed-pitch)
  560. title)
  561. (setq column (+ column 2
  562. (if (= column 0)
  563. elp-field-len
  564. (length title))))))
  565. (list title cc-header et-header at-header) ""))))
  566. ;; if sorting is enabled, then sort the results list. in either
  567. ;; case, call elp-output-result to output the result in the
  568. ;; buffer
  569. (if elp-sort-by-function
  570. (setq resvec (sort resvec elp-sort-by-function)))
  571. (mapc 'elp-output-result resvec))
  572. ;; now pop up results buffer
  573. (set-buffer curbuf)
  574. (pop-to-buffer resultsbuf)
  575. ;; copy results to standard-output?
  576. (if (or elp-use-standard-output noninteractive)
  577. (princ (buffer-substring (point-min) (point-max)))
  578. (goto-char (point-min)))
  579. ;; reset profiling info if desired
  580. (and elp-reset-after-results
  581. (elp-reset-all))))
  582. (defun elp-unload-function ()
  583. "Unload the Emacs Lisp Profiler."
  584. (elp-restore-all)
  585. ;; continue standard unloading
  586. nil)
  587. (provide 'elp)
  588. ;;; elp.el ends here