2618
|
1 ;;; diagnose.el --- routines for debugging problems in XEmacs
|
787
|
2
|
|
3 ;; Copyright (C) 2002 Ben Wing.
|
|
4
|
|
5 ;; Maintainer: XEmacs Development Team
|
|
6 ;; Keywords: dumped
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: Not in FSF.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This file is dumped with XEmacs.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33
|
|
34 (defun show-memory-usage ()
|
|
35 "Show statistics about memory usage of various sorts in XEmacs."
|
|
36 (interactive)
|
|
37 (garbage-collect)
|
|
38 (flet ((show-foo-stats (objtypename objlist memfun)
|
|
39 (let* ((hash (make-hash-table))
|
|
40 (first t)
|
|
41 types fmt
|
|
42 (objnamelen 25)
|
|
43 (linelen objnamelen)
|
|
44 (totaltotal 0))
|
|
45 (dolist (obj objlist)
|
|
46 (let ((total 0)
|
|
47 (stats (funcall memfun obj)))
|
|
48 (loop for (type . num) in stats while type do
|
|
49 (puthash type (+ num (or (gethash type hash) 0)) hash)
|
|
50 (incf total num)
|
|
51 (if first (push type types)))
|
|
52 (incf totaltotal total)
|
|
53 (when first
|
|
54 (setq types (nreverse types))
|
|
55 (setq fmt (concat
|
|
56 (format "%%-%ds" objnamelen)
|
|
57 (mapconcat
|
|
58 #'(lambda (type)
|
|
59 (let ((fieldlen
|
|
60 (max 8 (+ 2 (length
|
|
61 (symbol-name type))))))
|
|
62 (incf linelen fieldlen)
|
|
63 (format "%%%ds" fieldlen)))
|
|
64 types "")
|
2618
|
65 (progn (incf linelen 9) "%9s\n")))
|
787
|
66 (princ "\n")
|
|
67 (princ (apply 'format fmt objtypename
|
|
68 (append types (list 'total))))
|
|
69 (princ (make-string linelen ?-))
|
|
70 (princ "\n"))
|
|
71 (let ((objname (format "%s" obj)))
|
|
72 (princ (apply 'format fmt (substring objname 0
|
|
73 (min (length objname)
|
|
74 (1- objnamelen)))
|
|
75 (nconc (mapcar #'(lambda (type)
|
|
76 (cdr (assq type stats)))
|
|
77 types)
|
|
78 (list total)))))
|
|
79 (setq first nil)))
|
|
80 (princ "\n")
|
|
81 (princ (apply 'format fmt "total"
|
|
82 (nconc (mapcar #'(lambda (type)
|
|
83 (gethash type hash))
|
|
84 types)
|
|
85 (list totaltotal))))
|
|
86 totaltotal)))
|
|
87
|
2618
|
88 (let ((grandtotal 0)
|
|
89 (buffer "*memory usage*")
|
|
90 begin)
|
|
91 (with-output-to-temp-buffer buffer
|
|
92 (save-excursion
|
|
93 (set-buffer buffer)
|
|
94 (when-fboundp 'charset-list
|
|
95 (setq begin (point))
|
|
96 (incf grandtotal
|
|
97 (show-foo-stats 'charset (charset-list)
|
|
98 #'charset-memory-usage))
|
3066
|
99 (when-fboundp 'sort-numeric-fields
|
|
100 (sort-numeric-fields -1
|
|
101 (save-excursion
|
|
102 (goto-char begin)
|
|
103 (forward-line 2)
|
|
104 (point))
|
|
105 (save-excursion
|
|
106 (forward-line -2)
|
|
107 (point))))
|
2618
|
108 (princ "\n"))
|
|
109 (setq begin (point))
|
|
110 (incf grandtotal
|
|
111 (show-foo-stats 'buffer (buffer-list) #'buffer-memory-usage))
|
3066
|
112 (when-fboundp 'sort-numeric-fields
|
|
113 (sort-numeric-fields -1
|
|
114 (save-excursion
|
|
115 (goto-char begin)
|
|
116 (forward-line 3)
|
|
117 (point))
|
|
118 (save-excursion
|
|
119 (forward-line -2)
|
|
120 (point))))
|
2618
|
121 (princ "\n")
|
|
122 (setq begin (point))
|
787
|
123 (incf grandtotal
|
2618
|
124 (show-foo-stats 'window (mapcan #'(lambda (fr)
|
|
125 (window-list fr t))
|
|
126 (frame-list))
|
|
127 #'window-memory-usage))
|
|
128 (sort-numeric-fields -1
|
|
129 (save-excursion
|
|
130 (goto-char begin)
|
|
131 (forward-line 3)
|
|
132 (point))
|
|
133 (save-excursion
|
|
134 (forward-line -2)
|
|
135 (point)))
|
787
|
136 (princ "\n")
|
2618
|
137 (let ((total 0)
|
|
138 (fmt "%-30s%10s\n"))
|
|
139 (setq begin (point))
|
|
140 (princ (format fmt "object" "storage"))
|
|
141 (princ (make-string 40 ?-))
|
|
142 (princ "\n")
|
|
143 (map-plist #'(lambda (stat num)
|
2775
|
144 (when (string-match
|
3092
|
145 "\\(.*\\)-storage\\$"
|
2775
|
146 (symbol-name stat))
|
2618
|
147 (incf total num)
|
|
148 (princ (format fmt
|
|
149 (match-string 1 (symbol-name stat))
|
|
150 num)))
|
|
151 (when (eq stat 'long-strings-total-length)
|
|
152 (incf total num)
|
|
153 (princ (format fmt stat num))))
|
|
154 (sixth (garbage-collect)))
|
|
155 (princ "\n")
|
|
156 (princ (format fmt "total" total))
|
|
157 (incf grandtotal total))
|
|
158 (sort-numeric-fields -1
|
|
159 (save-excursion
|
|
160 (goto-char begin)
|
|
161 (forward-line 2)
|
|
162 (point))
|
|
163 (save-excursion
|
|
164 (forward-line -2)
|
|
165 (point)))
|
787
|
166
|
2618
|
167 (princ (format "\n\ngrand total: %s\n" grandtotal)))
|
787
|
168 grandtotal))))
|
2720
|
169
|
|
170
|
3041
|
171 (defun show-object-memory-usage-stats ()
|
|
172 "Show statistics about object memeory usage in XEmacs."
|
2775
|
173 (interactive)
|
|
174 (garbage-collect)
|
3041
|
175 (let ((buffer "*object memory usage statistics*")
|
|
176 (plist (object-memory-usage-stats))
|
2775
|
177 (fmt "%-30s%10s%10s\n")
|
|
178 (grandtotal 0)
|
|
179 begin)
|
|
180 (flet ((show-stats (match-string)
|
|
181 (princ (format fmt "object" "count" "storage"))
|
|
182 (princ (make-string 50 ?-))
|
|
183 (princ "\n")
|
|
184 (let ((total-use 0)
|
|
185 (total-use-overhead 0)
|
|
186 (total-count 0))
|
|
187 (map-plist
|
|
188 #'(lambda (stat num)
|
|
189 (when (string-match match-string
|
|
190 (symbol-name stat))
|
|
191 (let ((storage-use num)
|
|
192 (storage-use-overhead
|
|
193 (plist-get
|
|
194 plist
|
|
195 (intern (concat (match-string 1 (symbol-name stat))
|
|
196 "-storage-including-overhead"))))
|
|
197 (storage-count
|
|
198 (or (plist-get
|
|
199 plist
|
|
200 (intern
|
|
201 (concat (match-string 1 (symbol-name stat))
|
|
202 "s-used")))
|
|
203 (plist-get
|
|
204 plist
|
|
205 (intern
|
|
206 (concat (match-string 1 (symbol-name stat))
|
|
207 "es-used")))
|
|
208 (plist-get
|
|
209 plist
|
|
210 (intern
|
|
211 (concat (match-string 1 (symbol-name stat))
|
|
212 "-used"))))))
|
|
213 (incf total-use storage-use)
|
|
214 (incf total-use-overhead (if storage-use-overhead
|
|
215 storage-use-overhead
|
|
216 storage-use))
|
|
217 (incf total-count storage-count)
|
|
218 (princ (format fmt
|
|
219 (match-string 1 (symbol-name stat))
|
|
220 storage-count storage-use)))))
|
|
221 plist)
|
|
222 (princ "\n")
|
|
223 (princ (format fmt "total"
|
|
224 total-count total-use-overhead))
|
|
225 (incf grandtotal total-use-overhead)
|
|
226 (sort-numeric-fields -1
|
|
227 (save-excursion
|
|
228 (goto-char begin)
|
|
229 (forward-line 2)
|
|
230 (point))
|
|
231 (save-excursion
|
|
232 (forward-line -2)
|
|
233 (point))))))
|
|
234 (with-output-to-temp-buffer buffer
|
|
235 (save-excursion
|
|
236 (set-buffer buffer)
|
|
237 (setq begin (point))
|
3041
|
238 (princ "Allocated with lisp allocator:\n")
|
2775
|
239 (show-stats "\\(.*\\)-storage$")
|
|
240 (princ (format "\n\ngrand total: %s\n" grandtotal)))
|
|
241 grandtotal))))
|
|
242
|
|
243
|
2720
|
244 (defun show-mc-alloc-memory-usage ()
|
|
245 "Show statistics about memory usage of the new allocator."
|
|
246 (interactive)
|
|
247 (garbage-collect)
|
|
248 (let* ((stats (mc-alloc-memory-usage))
|
|
249 (page-size (first stats))
|
|
250 (heap-sects (second stats))
|
|
251 (used-plhs (third stats))
|
3092
|
252 (free-plhs (fourth stats))
|
|
253 (globals (fifth stats))
|
|
254 (mc-malloced-bytes (sixth stats)))
|
2720
|
255 (with-output-to-temp-buffer "*memory usage*"
|
|
256 (flet ((print-used-plhs (text plhs)
|
|
257 (let ((sum-n-pages 0)
|
|
258 (sum-used-n-cells 0)
|
|
259 (sum-used-space 0)
|
|
260 (sum-used-total 0)
|
|
261 (sum-total-n-cells 0)
|
|
262 (sum-total-space 0)
|
|
263 (sum-total-total 0)
|
|
264 (fmt "%7s%7s|%7s%9s%9s%4s|%7s%9s%9s%4s|%4s\n"))
|
|
265 (princ (format "%-14s|%-29s|%-29s|\n"
|
|
266 text
|
|
267 " currently in use"
|
|
268 " total available"))
|
|
269 (princ (format fmt "cell-sz" "#pages"
|
|
270 "#cells" "space" "total" "% "
|
|
271 "#cells" "space" "total" "% " "% "))
|
|
272 (princ (make-string 79 ?-))
|
|
273 (princ "\n")
|
|
274 (while plhs
|
|
275 (let* ((elem (car plhs))
|
|
276 (cell-size (first elem))
|
|
277 (n-pages (second elem))
|
|
278 (used-n-cells (third elem))
|
|
279 (used-space (fourth elem))
|
|
280 (used-total (if (zerop cell-size)
|
|
281 (sixth elem)
|
|
282 (* cell-size used-n-cells)))
|
|
283 (used-eff (floor (if (not (zerop used-total))
|
|
284 (* (/ (* used-space 1.0)
|
|
285 (* used-total 1.0))
|
|
286 100.0)
|
|
287 0)))
|
|
288 (total-n-cells (fifth elem))
|
|
289 (total-space (if (zerop cell-size)
|
|
290 used-space
|
|
291 (* cell-size total-n-cells)))
|
|
292 (total-total (sixth elem))
|
|
293 (total-eff (floor (if (not (zerop total-total))
|
|
294 (* (/ (* total-space 1.0)
|
|
295 (* total-total 1.0))
|
|
296 100.0)
|
|
297 0)))
|
|
298 (eff (floor (if (not (zerop total-total))
|
|
299 (* (/ (* used-space 1.0)
|
|
300 (* total-total 1.0))
|
|
301 100.0)
|
|
302 0))))
|
|
303 (princ (format fmt
|
|
304 cell-size n-pages used-n-cells used-space
|
|
305 used-total used-eff total-n-cells
|
|
306 total-space total-total total-eff eff))
|
|
307 (incf sum-n-pages n-pages)
|
|
308 (incf sum-used-n-cells used-n-cells)
|
|
309 (incf sum-used-space used-space)
|
|
310 (incf sum-used-total used-total)
|
|
311 (incf sum-total-n-cells total-n-cells)
|
|
312 (incf sum-total-space total-space)
|
|
313 (incf sum-total-total total-total))
|
|
314 (setq plhs (cdr plhs)))
|
|
315 (let ((avg-used-eff (floor (if (not (zerop sum-used-total))
|
|
316 (* (/ (* sum-used-space 1.0)
|
|
317 (* sum-used-total 1.0))
|
|
318 100.0)
|
|
319 0)))
|
|
320 (avg-total-eff (floor (if (not (zerop sum-total-total))
|
|
321 (* (/ (* sum-total-space 1.0)
|
|
322 (* sum-total-total 1.0))
|
|
323 100.0)
|
|
324 0)))
|
|
325 (avg-eff (floor (if (not (zerop sum-total-total))
|
|
326 (* (/ (* sum-used-space 1.0)
|
|
327 (* sum-total-total 1.0))
|
|
328 100.0)
|
|
329 0))))
|
|
330 (princ (format fmt "sum " sum-n-pages sum-used-n-cells
|
|
331 sum-used-space sum-used-total avg-used-eff
|
|
332 sum-total-n-cells sum-total-space
|
|
333 sum-total-total avg-total-eff avg-eff))
|
|
334 (princ "\n"))))
|
|
335
|
|
336
|
|
337 (print-free-plhs (text plhs)
|
|
338 (let ((sum-n-pages 0)
|
|
339 (sum-n-sects 0)
|
|
340 (sum-space 0)
|
|
341 (sum-total 0)
|
|
342 (fmt "%6s%10s |%7s%10s\n"))
|
|
343 (princ (format "%s\n" text))
|
|
344 (princ (format fmt "#pages" "space" "#sects" "total"))
|
|
345 (princ (make-string 35 ?-))
|
|
346 (princ "\n")
|
|
347 (while plhs
|
|
348 (let* ((elem (car plhs))
|
|
349 (n-pages (first elem))
|
|
350 (n-sects (second elem))
|
|
351 (space (* n-pages page-size))
|
|
352 (total (* n-sects space)))
|
|
353 (princ (format fmt n-pages space n-sects total))
|
|
354 (incf sum-n-pages n-pages)
|
|
355 (incf sum-n-sects n-sects)
|
|
356 (incf sum-space space)
|
|
357 (incf sum-total total))
|
|
358 (setq plhs (cdr plhs)))
|
|
359 (princ (make-string 35 ?=))
|
|
360 (princ "\n")
|
|
361 (princ (format fmt sum-n-pages sum-space
|
|
362 sum-n-sects sum-total))
|
|
363 (princ "\n"))))
|
|
364
|
|
365 (princ (format "%-12s%10s\n\n" "PAGE_SIZE" page-size))
|
|
366
|
|
367 (print-used-plhs "USED HEAP" used-plhs)
|
|
368 (princ "\n\n")
|
|
369
|
|
370 (print-free-plhs "FREE HEAP" free-plhs)
|
|
371 (princ "\n\n")
|
|
372
|
|
373 (let ((fmt "%-30s%10s\n"))
|
|
374 (princ (format fmt "heap sections" ""))
|
|
375 (princ (make-string 40 ?-))
|
|
376 (princ "\n")
|
|
377 (princ (format fmt "number of heap sects"
|
|
378 (first heap-sects)))
|
|
379 (princ (format fmt "used size" (second heap-sects)))
|
|
380 (princ (make-string 40 ?-))
|
|
381 (princ "\n")
|
|
382 (princ (format fmt "real size" (third heap-sects)))
|
|
383 (princ (format fmt "global allocator structs" globals))
|
|
384 (princ (make-string 40 ?-))
|
|
385 (princ "\n")
|
|
386 (princ (format fmt "real size + structs"
|
|
387 (+ (third heap-sects) globals)))
|
|
388 (princ "\n")
|
|
389 (princ (make-string 40 ?=))
|
|
390 (princ "\n")
|
|
391 (princ (format fmt "grand total" mc-malloced-bytes)))
|
|
392
|
|
393 (+ mc-malloced-bytes)))))
|
3092
|
394
|
|
395
|
|
396 (defun show-gc-stats ()
|
|
397 "Show statistics about garbage collection cycles."
|
|
398 (interactive)
|
|
399 (let ((buffer "*garbage collection statistics*")
|
|
400 (plist (gc-stats))
|
|
401 (fmt "%-9s %10s %10s %10s %10s %10s\n"))
|
|
402 (flet ((plist-get-stat (category field)
|
|
403 (or (plist-get plist (intern (concat category field)))
|
|
404 "-"))
|
|
405 (show-stats (category)
|
|
406 (princ (format fmt category
|
|
407 (plist-get-stat category "-total")
|
|
408 (plist-get-stat category "-in-last-gc")
|
|
409 (plist-get-stat category "-in-this-gc")
|
|
410 (plist-get-stat category "-in-last-cycle")
|
|
411 (plist-get-stat category "-in-this-cycle")))))
|
|
412 (with-output-to-temp-buffer buffer
|
|
413 (save-excursion
|
|
414 (set-buffer buffer)
|
|
415 (princ (format "%s %s\n" "Current phase" (plist-get plist 'phase)))
|
|
416 (princ (make-string 64 ?-))
|
|
417 (princ "\n")
|
|
418 (princ (format fmt "stat" "total" "last-gc" "this-gc"
|
|
419 "last-cycle" "this-cylce"))
|
|
420 (princ (make-string 64 ?-))
|
|
421 (princ "\n")
|
|
422 (show-stats "n-gc")
|
|
423 (show-stats "n-cycles")
|
|
424 (show-stats "enqueued")
|
|
425 (show-stats "dequeued")
|
|
426 (show-stats "repushed")
|
|
427 (show-stats "enqueued2")
|
|
428 (show-stats "dequeued2")
|
|
429 (show-stats "finalized")
|
|
430 (show-stats "freed")
|
|
431 (princ (make-string 64 ?-))
|
|
432 (princ "\n")
|
|
433 (princ (format fmt "explicitly"
|
|
434 "freed:"
|
|
435 (plist-get-stat "explicitly" "-freed")
|
|
436 "tried:"
|
|
437 (plist-get-stat "explicitly" "-tried-freed")
|
|
438 "")))
|
|
439
|
|
440 (plist-get plist 'n-gc-total)))))
|