BlockMap.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* BlockMap.java -- Container for information on GC maintained memory blocks.
  2. Copyright (C) 2007 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. package gnu.gcj.tools.gc_analyze;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.Map;
  12. import java.util.TreeMap;
  13. class BlockMap
  14. {
  15. static final int HBLKSIZE = 4096;
  16. class SizeKind implements Comparable<SizeKind>
  17. {
  18. int size;
  19. int kind;
  20. public SizeKind(int size, int kind)
  21. {
  22. this.size = size;
  23. this.kind = kind;
  24. }
  25. public int compareTo(SizeKind b)
  26. {
  27. if (this.size != b.size)
  28. return this.size - b.size;
  29. return this.kind - b.kind;
  30. }
  31. }
  32. class PtrMarks
  33. {
  34. long ptr;
  35. int marks;
  36. public PtrMarks(long ptr, int marks)
  37. {
  38. this.ptr = ptr;
  39. this.marks = marks;
  40. }
  41. }
  42. private TreeMap<SizeKind, ArrayList<PtrMarks>> map =
  43. new TreeMap<SizeKind, ArrayList<PtrMarks>>();
  44. public BlockMap(BufferedReader reader) throws IOException
  45. {
  46. for (;;)
  47. {
  48. String s = reader.readLine();
  49. if (s == null)
  50. break;
  51. if (s.charAt(0) == '#')
  52. continue;
  53. if (s.indexOf("Begin block map") >= 0)
  54. {
  55. for (;;)
  56. {
  57. s = reader.readLine();
  58. if (s.charAt(0) == '#')
  59. continue;
  60. if (s.indexOf("End block map") >= 0)
  61. return;
  62. String[] items = s.split(",");
  63. long ptr = 0;
  64. int kind = 0, size = 0, marks = 0;
  65. for (int i=0; i<items.length; i++)
  66. {
  67. String[] x = items[i].split(" ");
  68. String last = x[x.length - 1];
  69. switch (i)
  70. {
  71. case 0:
  72. ptr = MemoryMap.parseHexLong(last.substring(2));
  73. break;
  74. case 1:
  75. kind = Integer.parseInt(last);
  76. break;
  77. case 2:
  78. size = Integer.parseInt(last);
  79. break;
  80. case 3:
  81. marks = Integer.parseInt(last);
  82. break;
  83. }
  84. }
  85. SizeKind sk = new SizeKind(size, kind);
  86. ArrayList<PtrMarks> m = map.get(sk);
  87. if (m == null)
  88. {
  89. m = new ArrayList<PtrMarks>();
  90. map.put(sk, m);
  91. }
  92. PtrMarks pm = new PtrMarks(ptr, marks);
  93. m.add(pm);
  94. } // inner loop
  95. } // started inner loop
  96. } // outer loop - finding begin
  97. } // memoryMap
  98. public void dump()
  99. {
  100. System.out.println();
  101. System.out.println();
  102. System.out.println("*** Used Blocks ***\n");
  103. System.out.println();
  104. System.out.println(" Size Kind Blocks Used Free Wasted");
  105. System.out.println("------- ------------- ------- ---------- ---------- -------");
  106. int total_blocks = 0, total_used = 0, total_free = 0, total_wasted = 0;
  107. for (Map.Entry<SizeKind, ArrayList<PtrMarks>> me : map.entrySet())
  108. {
  109. SizeKind sk = me.getKey();
  110. System.out.println(MemoryAnalyze.format(sk.size, 7) + " "
  111. + MemoryAnalyze.kindToName(sk.kind));
  112. int sub_blocks = 0, sub_used = 0, sub_free = 0, sub_wasted = 0;
  113. int sub_count = 0;
  114. ArrayList<PtrMarks> v = me.getValue();
  115. for (PtrMarks pm : v)
  116. {
  117. int bytes = sk.size;
  118. int blocks = (sk.size + HBLKSIZE - 1) / HBLKSIZE;
  119. int used;
  120. int free;
  121. int wasted;
  122. if (bytes < HBLKSIZE)
  123. {
  124. used = bytes * pm.marks;
  125. free = bytes * (HBLKSIZE / bytes - pm.marks);
  126. wasted = HBLKSIZE - HBLKSIZE / bytes * bytes;
  127. }
  128. else
  129. {
  130. if (pm.marks != 0)
  131. {
  132. used = bytes;
  133. free = 0;
  134. wasted = (bytes + HBLKSIZE - 1)
  135. / HBLKSIZE * HBLKSIZE - used;
  136. }
  137. else
  138. {
  139. used = 0;
  140. free = bytes;
  141. wasted = 0;
  142. }
  143. }
  144. StringBuilder sb = new StringBuilder();
  145. sb.append(" ");
  146. sb.append(MemoryAnalyze.format(blocks, 5));
  147. sb.append(" ");
  148. sb.append(MemoryAnalyze.format(used, 9));
  149. sb.append(" ");
  150. sb.append(MemoryAnalyze.format(free, 9));
  151. sb.append(" ");
  152. sb.append(MemoryAnalyze.format(wasted, 9));
  153. System.out.println(sb);
  154. sub_blocks += blocks;
  155. sub_used += used;
  156. sub_free += free;
  157. sub_wasted += wasted;
  158. sub_count++;
  159. total_blocks += blocks;
  160. total_used += used;
  161. total_free += free;
  162. total_wasted += wasted;
  163. } // blocks with size/kind
  164. if (sub_count > 1)
  165. {
  166. System.out.println(
  167. " ------- ---------- ---------- -------");
  168. StringBuilder sb = new StringBuilder();
  169. sb.append(" ");
  170. sb.append(MemoryAnalyze.format(sub_blocks, 5));
  171. sb.append(" ");
  172. sb.append(MemoryAnalyze.format(sub_used, 9));
  173. sb.append(" ");
  174. sb.append(MemoryAnalyze.format(sub_free, 9));
  175. sb.append(" ");
  176. sb.append(MemoryAnalyze.format(sub_wasted, 9));
  177. System.out.println(sb);
  178. }
  179. } // size/kind
  180. System.out.println("------- ------------- ------- ---------- ---------- -------");
  181. StringBuilder sb = new StringBuilder();
  182. sb.append(" ");
  183. sb.append(MemoryAnalyze.format(total_blocks, 5));
  184. sb.append(" ");
  185. sb.append(MemoryAnalyze.format(total_used, 9));
  186. sb.append(" ");
  187. sb.append(MemoryAnalyze.format(total_free, 9));
  188. sb.append(" ");
  189. sb.append(MemoryAnalyze.format(total_wasted, 9));
  190. System.out.println(sb);
  191. System.out.println("Total bytes = "
  192. + MemoryAnalyze.format(total_blocks * HBLKSIZE, 10));
  193. }
  194. }