heap-stats.sl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%% Ordinary LISP part of the heap statistics gathering package, HEAP-STATS.
  3. %%% Load this file to get the package.
  4. %%% The top-level function is collect-stats. See its description.
  5. %%%
  6. %%% Author: Cris Perdue
  7. %%% December 1982
  8. %%% Documented and cleaned up a litte, January 1983
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. (compiletime (load if))
  11. (load h-stats-1 get-heap-bounds)
  12. %%% An object that holds a complete set of statistics for the heap
  13. %%% at some moment in time. When one of these is created, the
  14. %%% instance variable "template" must be initialized, and the
  15. %%% template must be a "histogram template" as discussed below.
  16. %%% Maintainer note: the code that actually gathers statistics assumes
  17. %%% that the heap-stats object is a vector (or evector) with a header,
  18. %%% 2 items of data allocated by the objects package, then the data shown
  19. %%% here, in order.
  20. (defflavor heap-stats
  21. (template
  22. string-count
  23. string-space
  24. vector-count
  25. vector-space
  26. wordvec-count
  27. wordvec-space
  28. (pairs 0)
  29. (strings 0)
  30. (halfwords 0)
  31. (wordvecs 0)
  32. (vectors 0))
  33. ()
  34. (initable-instance-variables template)
  35. gettable-instance-variables)
  36. (defmethod (heap-stats init) (init-plist)
  37. (if (not (vectorp template)) then
  38. (error 0 "The TEMPLATE of a HEAP-STATS object must be initialized."))
  39. (let ((s (+ (size template) 1)))
  40. (setf string-count (make-vector s 0))
  41. (setf string-space (make-vector s 0))
  42. (setf vector-count (make-vector s 0))
  43. (setf vector-space (make-vector s 0))
  44. (setf wordvec-count (make-vector s 0))
  45. (setf wordvec-space (make-vector s 0))))
  46. (global '(old-!%reclaim stats-channel))
  47. %%% This method prints statistics on a particular snapshot of the heap
  48. %%% onto the given channel.
  49. (defmethod (heap-stats print-stats) (channel)
  50. (channelprintf
  51. channel
  52. "%w pairs, %w strings, %w vectors, %w wordvecs, %w halfwordvecs%n%n"
  53. pairs strings vectors wordvecs halfwords)
  54. (for (in table (list string-count vector-count))
  55. (in spacetable (list string-space vector-space))
  56. (in title '("STRINGS" "VECTORS"))
  57. (do
  58. (channelprintf channel "%w%n%n" title)
  59. (print-histo template table spacetable channel)
  60. (channelterpri channel)
  61. (channelterpri channel))))
  62. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  63. %%% Internal functions
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65. %%% Prints a single histogram onto the given channel. Arguments
  66. %%% are the template from which the histogram was generated, a
  67. %%% corresponding table with a count of the number of objects of
  68. %%% each size range, and another corresponding table with the
  69. %%% total space occupied by the objects within each size range.
  70. (defun print-histo (template table spacetable channel)
  71. (channelprintf channel
  72. "Size <= n%tHow many%tStorage items used%n" 12 24)
  73. (channelprintf channel
  74. "------------------------------------------%n")
  75. (for (from i 0 (size template))
  76. (do (channelprintf channel
  77. "%w%t%w%t%w%n" (indx template i) 12
  78. (indx table i) 24 (indx spacetable i))))
  79. (channelprintf channel
  80. "> %w%t%w%t%w%n"
  81. (indx template (size template)) 12
  82. (indx table (+ (size template) 1)) 24
  83. (indx spacetable (+ (size template) 1))))
  84. (fluid '(before-stats after-stats print-stats? stdtemplate))
  85. %%% This function initializes the collecting of statistics and
  86. %%% printing them to a file. The name of the file is the
  87. %%% argument to collect-stats. NIL rather than a string for the file
  88. %%% name turns statistics collection off. In statistics collection mode
  89. %%% statistics are gathered just before and after each garbage collection.
  90. (defun collect-stats (file)
  91. (if (and file (not old-!%reclaim)) then
  92. (if (not (and (eq (object-type before-stats) 'heap-stats)
  93. (eq (object-type after-stats) 'heap-stats))) then
  94. (printf "Caution: before- and after-stats are not both bound.%n"))
  95. (setq old-!%reclaim (cdr (getd '!%reclaim)))
  96. (setq stats-channel (open file 'output))
  97. (putd '!%reclaim
  98. 'expr
  99. '(lambda ()
  100. (heapstats before-stats)
  101. (apply old-!%reclaim nil)
  102. (heapstats after-stats)
  103. (channelprintf stats-channel "BEFORE RECLAIMING%n%n")
  104. (=> before-stats print-stats stats-channel)
  105. (channelterpri stats-channel)
  106. (channelprintf stats-channel "AFTER RECLAIMING%n%n")
  107. (=> after-stats print-stats stats-channel)))
  108. elseif (and (not file) old-!%reclaim) then
  109. (close stats-channel)
  110. (putd '!%reclaim 'expr old-!%reclaim)
  111. (setq old-!%reclaim nil)
  112. elseif old-!%reclaim then
  113. (printf "Statistics collecting is apparently already turned on.%n")
  114. else
  115. (printf "Statistics collecting is apparently already off.%n")
  116. (printf "Trying to close the channel anyway.%n")
  117. (close stats-channel)))
  118. %%% This is initialized here to be a reasonable histogram template for
  119. %%% statistics on heap usage. A histogram template is a vector of
  120. %%% integers that define the buckets to be used in collecting the
  121. %%% histogram data. All values less than or equal to template[0]
  122. %%% go into data[0]. Of those values that do not go into data[0],
  123. %%% all less than or equal to template[1] go into data[1], etc..
  124. %%% The vector of data must have at least one more element that
  125. %%% the template does. All values greater than the last value in
  126. %%% the template go into the following element of the data vector.
  127. (setq StdTemplate
  128. (make-vector 27 0))
  129. (for (from i 0 16)
  130. (do (setf (indx StdTemplate i) i)))
  131. (for (from i 17 27)
  132. (for k 32 (* k 2))
  133. (do (setf (indx StdTemplate i) k)))
  134. (setq before-stats (make-instance 'heap-stats 'template StdTemplate))
  135. (setq after-stats (make-instance 'heap-stats 'template StdTemplate))