branch: elpa/slime
commit 6e8f9193308e77a3830df037cf73cb0ac278d8a0
Author: Stas Boukarev <stass...@gmail.com>
Commit: Stas Boukarev <stass...@gmail.com>

    sprof: change sorting column.
---
 contrib/slime-sprof.el   | 46 +++++++++++++++++++++++++++++++++-------------
 contrib/swank-sprof.lisp | 11 ++++++-----
 2 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/contrib/slime-sprof.el b/contrib/slime-sprof.el
index 327c811435..884e5a5bcc 100644
--- a/contrib/slime-sprof.el
+++ b/contrib/slime-sprof.el
@@ -21,6 +21,8 @@
 (defvar slime-sprof-exclude-swank nil
   "*Display swank functions in the report.")
 
+(defvar slime-sprof-sort 'cumul)
+
 (define-derived-mode slime-sprof-browser-mode fundamental-mode
   "slprof"
   "Mode for browsing profiler data\
@@ -61,22 +63,40 @@
 
 (defun slime-sprof-format (graph)
   (with-current-buffer (slime-buffer-name :sprof)
-    (let ((inhibit-read-only t))
-      (erase-buffer)
-      (insert (format "%4s %-54s %6s %6s %6s\n"
-                      "Rank"
-                      "Name"
-                      "Self%"
-                      "Cumul%"
-                      "Total%"))
-      (dolist (data graph)
-        (slime-sprof-browser-insert-line data 54))))
-  (forward-line 2))
+    (let ((line (line-number-at-pos))
+          (point (point)))
+      (save-excursion
+       (let ((inhibit-read-only t))
+         (erase-buffer)
+         (insert (format "%4s %-56s"
+                         "Rank"
+                         "Name"))
+         (loop for (label name) in '(("Self%" self) ("Cumul%" cumul))
+               do
+               (insert-text-button
+                label
+                'button t
+                'sort name
+                'action 'slime-sprof-sort)
+               (insert " "))
+         (insert "Total%\n")
+         (dolist (data graph)
+           (slime-sprof-browser-insert-line data 54))))
+      (if (= line 1)
+          (goto-char point)
+          (goto-line 2)))))
+
+(defun slime-sprof-sort (arg)
+  (let* ((pos (if (markerp arg) arg (point)))
+         (sort (get-text-property pos 'sort)))
+    (setf slime-sprof-sort sort)
+    (slime-sprof-update)))
 
 (cl-defun slime-sprof-update (&optional (exclude-swank 
slime-sprof-exclude-swank))
   (slime-eval-async `(swank:swank-sprof-get-call-graph
-                      :exclude-swank ,exclude-swank)
-    'slime-sprof-format))
+                      :exclude-swank ,exclude-swank
+                      :sort ',slime-sprof-sort)
+                    'slime-sprof-format))
 
 (defalias 'slime-sprof-browser 'slime-sprof-report)
 
diff --git a/contrib/swank-sprof.lisp b/contrib/swank-sprof.lisp
index 8c9fede966..79b724b3cd 100644
--- a/contrib/swank-sprof.lisp
+++ b/contrib/swank-sprof.lisp
@@ -60,13 +60,14 @@
                                 :test #'eq))))
                nodes)))
 
-(defun serialize-call-graph (&key exclude-swank)
+(defun serialize-call-graph (&key exclude-swank (sort 'cumul))
   (let ((nodes (sb-sprof::call-graph-flat-nodes *call-graph*)))
     (when exclude-swank
       (setf nodes (filter-swank-nodes nodes)))
     (setf nodes (sort (copy-list nodes) #'>
-                      ;; :key #'sb-sprof::node-count)))
-                      :key #'sb-sprof::node-accrued-count))
+                      :key (ecase sort
+                             (swank-io-package::self #'sb-sprof::node-count)
+                             (swank-io-package::cumul 
#'sb-sprof::node-accrued-count))))
     (setf *number-nodes* (make-hash-table))
     (setf *node-numbers* (make-hash-table))
     (loop for node in nodes
@@ -83,9 +84,9 @@
                       (return (append list
                                       `((nil "Elsewhere" ,rest nil nil)))))))))
 
-(defslimefun swank-sprof-get-call-graph (&key exclude-swank)
+(defslimefun swank-sprof-get-call-graph (&key exclude-swank sort)
   (when (setf *call-graph* (sb-sprof:report :type nil))
-    (serialize-call-graph :exclude-swank exclude-swank)))
+    (serialize-call-graph :exclude-swank exclude-swank :sort sort)))
 
 (defslimefun swank-sprof-expand-node (index)
   (let* ((node (gethash index *number-nodes*)))

Reply via email to