]> code.delx.au - gnu-emacs/blobdiff - lisp/profiler.el
Merge profiler branch
[gnu-emacs] / lisp / profiler.el
index 00ee99a61324cede442763f17146737ad0e8e75b..5fc745732625dad38dbfdf0db6b4604df90606a6 100644 (file)
@@ -20,7 +20,7 @@
 
 ;;; Commentary:
 
-;; 
+;;
 
 ;;; Code:
 
@@ -218,6 +218,10 @@ be same type."
     (profiler-calltree-walk child function)))
 
 (defun profiler-calltree-build-1 (tree log &optional reverse)
+  ;; FIXME: Do a better job of reconstructing a complete call-tree
+  ;; when the backtraces have been truncated.  Ideally, we should be
+  ;; able to reduce profiler-max-stack-depth to 3 or 4 and still
+  ;; get a meaningful call-tree.
   (maphash
    (lambda (backtrace count)
      (when (vectorp backtrace)
@@ -237,6 +241,7 @@ be same type."
 
 (defun profiler-calltree-compute-percentages (tree)
   (let ((total-count 0))
+    ;; FIXME: the memory profiler's total wraps around all too easily!
     (dolist (child (profiler-calltree-children tree))
       (cl-incf total-count (profiler-calltree-count child)))
     (unless (zerop total-count)
@@ -573,55 +578,50 @@ MODE can be one of `cpu', `mem', or `cpu+mem'.
 If MODE is `cpu' or `cpu+mem', time-based profiler will be started.
 Also, if MODE is `mem' or `cpu+mem', then memory profiler will be started."
   (interactive
-   (list (intern (completing-read "Mode (default cpu): "
-                                  '("cpu" "mem" "cpu+mem")
-                                 nil t nil nil "cpu"))))
+   (list (if (not (fboundp 'profiler-cpu-start)) 'mem
+           (intern (completing-read "Mode (default cpu): "
+                                    '("cpu" "mem" "cpu+mem")
+                                    nil t nil nil "cpu")))))
   (cl-ecase mode
     (cpu
-     (sample-profiler-start profiler-sample-interval)
+     (profiler-cpu-start profiler-sample-interval)
      (message "CPU profiler started"))
     (mem
-     (memory-profiler-start)
+     (profiler-memory-start)
      (message "Memory profiler started"))
     (cpu+mem
-     (sample-profiler-start profiler-sample-interval)
-     (memory-profiler-start)
+     (profiler-cpu-start profiler-sample-interval)
+     (profiler-memory-start)
      (message "CPU and memory profiler started"))))
 
 (defun profiler-stop ()
   "Stop started profilers.  Profiler logs will be kept."
   (interactive)
-  (cond
-   ((and (sample-profiler-running-p)
-        (memory-profiler-running-p))
-    (sample-profiler-stop)
-    (memory-profiler-stop)
-    (message "CPU and memory profiler stopped"))
-   ((sample-profiler-running-p)
-    (sample-profiler-stop)
-    (message "CPU profiler stopped"))
-   ((memory-profiler-running-p)
-    (memory-profiler-stop)
-    (message "Memory profiler stopped"))
-   (t
-    (error "No profilers started"))))
+  (let ((cpu (if (fboundp 'profiler-cpu-stop) (profiler-cpu-stop)))
+        (mem (profiler-memory-stop)))
+    (message "%s profiler stopped"
+             (cond ((and mem cpu) "CPU and memory")
+                   (mem "Memory")
+                   (cpu "CPU")
+                   (t "No")))))
 
 (defun profiler-reset ()
   "Reset profiler log."
   (interactive)
-  (ignore (sample-profiler-log))
-  (ignore (memory-profiler-log))
+  (when (fboundp 'profiler-cpu-log)
+    (ignore (profiler-cpu-log)))
+  (ignore (profiler-memory-log))
   t)
 
 (defun profiler--report-cpu ()
-  (let ((log (sample-profiler-log)))
+  (let ((log (if (fboundp 'profiler-cpu-log) (profiler-cpu-log))))
     (when log
       (puthash 'type 'cpu log)
       (puthash 'timestamp (current-time) log)
       (profiler-report-log log))))
 
 (defun profiler--report-memory ()
-  (let ((log (memory-profiler-log)))
+  (let ((log (profiler-memory-log)))
     (when log
       (puthash 'type 'memory log)
       (puthash 'timestamp (current-time) log)
@@ -643,23 +643,23 @@ Also, if MODE is `mem' or `cpu+mem', then memory profiler will be started."
 \f
 ;;; Profiling helpers
 
-(cl-defmacro with-sample-profiling ((&key interval) &rest body)
-  `(unwind-protect
-       (progn
-         (ignore (sample-profiler-log))
-         (sample-profiler-start ,interval)
-         ,@body)
-     (sample-profiler-stop)
-     (profiler--report-cpu)))
-
-(defmacro with-memory-profiling (&rest body)
-  `(unwind-protect
-       (progn
-         (ignore (memory-profiler-log))
-         (memory-profiler-start)
-         ,@body)
-     (memory-profiler-stop)
-     (profiler--report-memory)))
+;; (cl-defmacro with-sample-profiling ((&key interval) &rest body)
+;;   `(unwind-protect
+;;        (progn
+;;          (ignore (profiler-cpu-log))
+;;          (profiler-cpu-start ,interval)
+;;          ,@body)
+;;      (profiler-cpu-stop)
+;;      (profiler--report-cpu)))
+
+;; (defmacro with-memory-profiling (&rest body)
+;;   `(unwind-protect
+;;        (progn
+;;          (ignore (profiler-memory-log))
+;;          (profiler-memory-start)
+;;          ,@body)
+;;      (profiler-memory-stop)
+;;      (profiler--report-memory)))
 
 (provide 'profiler)
 ;;; profiler.el ends here