1 8 9 package com.sleepycat.je.util; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.PrintStream ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.SortedMap ; 18 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.DbInternal; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.JEVersion; 24 import com.sleepycat.je.cleaner.FileSummary; 25 import com.sleepycat.je.cleaner.UtilizationProfile; 26 import com.sleepycat.je.dbi.EnvironmentImpl; 27 import com.sleepycat.je.log.UtilizationFileReader; 28 import com.sleepycat.je.utilint.CmdUtil; 29 30 public class DbSpace { 31 32 private static final String USAGE = 33 "usage: " + CmdUtil.getJavaCommand(DbSpace.class) + "\n" + 34 " -h <dir> # environment home directory\n" + 35 " [-q] # quiet, print grand totals only\n" + 36 " [-u] # sort by utilization\n" + 37 " [-d] # dump file summary details\n" + 38 " [-r] # recalculate utilization (reads entire log)\n" + 39 " [-V] # print JE version number"; 40 41 public static void main(String argv[]) 42 throws DatabaseException { 43 44 DbSpace space = new DbSpace(); 45 space.parseArgs(argv); 46 47 EnvironmentConfig envConfig = new EnvironmentConfig(); 48 envConfig.setReadOnly(true); 49 Environment env = new Environment(space.envHome, envConfig); 50 space.envImpl = DbInternal.envGetEnvironmentImpl(env); 51 52 try { 53 space.print(System.out); 54 System.exit(0); 55 } catch (Throwable e) { 56 e.printStackTrace(System.err); 57 System.exit(1); 58 } finally { 59 try { 60 env.close(); 61 } catch (Throwable e) { 62 e.printStackTrace(System.err); 63 System.exit(1); 64 } 65 } 66 } 67 68 private File envHome = null; 69 private EnvironmentImpl envImpl; 70 private boolean quiet = false; 71 private boolean sorted = false; 72 private boolean details = false; 73 private boolean recalc = false; 74 75 private DbSpace() { 76 } 77 78 public DbSpace(Environment env, 79 boolean quiet, 80 boolean details, 81 boolean sorted) { 82 this(DbInternal.envGetEnvironmentImpl(env), quiet, details, sorted); 83 } 84 85 public DbSpace(EnvironmentImpl envImpl, 86 boolean quiet, 87 boolean details, 88 boolean sorted) { 89 this.envImpl = envImpl; 90 this.quiet = quiet; 91 this.details = details; 92 this.sorted = sorted; 93 } 94 95 private void printUsage(String msg) { 96 if (msg != null) { 97 System.err.println(msg); 98 } 99 System.err.println(USAGE); 100 System.exit(-1); 101 } 102 103 private void parseArgs(String argv[]) { 104 105 int argc = 0; 106 int nArgs = argv.length; 107 108 if (nArgs == 0) { 109 printUsage(null); 110 System.exit(0); 111 } 112 113 while (argc < nArgs) { 114 String thisArg = argv[argc++]; 115 if (thisArg.equals("-q")) { 116 quiet = true; 117 } else if (thisArg.equals("-u")) { 118 sorted = true; 119 } else if (thisArg.equals("-d")) { 120 details = true; 121 } else if (thisArg.equals("-r")) { 122 recalc = true; 123 } else if (thisArg.equals("-V")) { 124 System.out.println(JEVersion.CURRENT_VERSION); 125 System.exit(0); 126 } else if (thisArg.equals("-h")) { 127 if (argc < nArgs) { 128 envHome = new File (argv[argc++]); 129 } else { 130 printUsage("-h requires an argument"); 131 } 132 } 133 } 134 135 if (envHome == null) { 136 printUsage("-h is a required argument"); 137 } 138 } 139 140 public void print(PrintStream out) 141 throws IOException , DatabaseException { 142 143 UtilizationProfile profile = envImpl.getUtilizationProfile(); 144 SortedMap map = profile.getFileSummaryMap(false); 145 Map recalcMap = UtilizationFileReader.calcFileSummaryMap(envImpl); 146 int fileIndex = 0; 147 148 Summary totals = new Summary(); 149 Summary[] summaries = null; 150 if (!quiet) { 151 summaries = new Summary[map.size()]; 152 } 153 154 Iterator iter = map.entrySet().iterator(); 155 while (iter.hasNext()) { 156 Map.Entry entry = (Map.Entry ) iter.next(); 157 Long fileNum = (Long ) entry.getKey(); 158 FileSummary fs = (FileSummary) entry.getValue(); 159 FileSummary recalcFs = null; 160 if (recalcMap != null) { 161 recalcFs = (FileSummary) recalcMap.get(fileNum); 162 } 163 Summary summary = new Summary(fileNum, fs, recalcFs); 164 if (summaries != null) { 165 summaries[fileIndex] = summary; 166 } 167 if (details) { 168 out.println 169 ("File 0x" + Long.toHexString(fileNum.longValue()) + 170 ": " + fs); 171 if (recalcMap != null) { 172 out.println 173 ("Recalculated File 0x" + 174 Long.toHexString(fileNum.longValue()) + 175 ": " + recalcFs); 176 } 177 } 178 totals.add(summary); 179 fileIndex += 1; 180 } 181 182 if (details) { 183 out.println(); 184 } 185 out.println(recalc ? Summary.RECALC_HEADER : Summary.HEADER); 186 187 if (summaries != null) { 188 if (sorted) { 189 Arrays.sort(summaries); 190 } 191 for (int i = 0; i < summaries.length; i += 1) { 192 summaries[i].print(out, recalc); 193 } 194 } 195 196 totals.print(out, recalc); 197 } 198 199 private static class Summary implements Comparable { 200 201 static final String HEADER = " File Size (KB) % Used\n" + 202 "-------- --------- ------"; 203 207 static final String RECALC_HEADER = 208 " File Size (KB) % Used % Used (recalculated)\n" + 209 "-------- --------- ------ ------"; 210 214 Long fileNum; 215 long totalSize; 216 long obsoleteSize; 217 long recalcObsoleteSize; 218 219 Summary() {} 220 221 Summary(Long fileNum, FileSummary summary, FileSummary recalcSummary) 222 throws DatabaseException { 223 224 this.fileNum = fileNum; 225 totalSize = summary.totalSize; 226 obsoleteSize = summary.getObsoleteSize(); 227 if (recalcSummary != null) { 228 recalcObsoleteSize = recalcSummary.getObsoleteSize(); 229 } 230 } 231 232 public int compareTo(Object other) { 233 Summary o = (Summary) other; 234 return utilization() - o.utilization(); 235 } 236 237 public boolean equals(Object o) { 238 if (o == null) { 239 return false; 240 } 241 242 if (o instanceof Summary) { 243 return utilization() == ((Summary) o).utilization(); 244 } else { 245 return false; 246 } 247 } 248 249 void add(Summary o) { 250 totalSize += o.totalSize; 251 obsoleteSize += o.obsoleteSize; 252 recalcObsoleteSize += o.recalcObsoleteSize; 253 } 254 255 void print(PrintStream out, boolean recalc) { 256 if (fileNum != null) { 257 pad(out, Long.toHexString(fileNum.longValue()), 8, '0'); 258 } else { 259 out.print(" TOTALS "); 260 } 261 int kb = (int) (totalSize / 1024); 262 out.print(" "); 263 pad(out, Integer.toString(kb), 9, ' '); 264 out.print(" "); 265 pad(out, Integer.toString(utilization()), 3, ' '); 266 if (recalc) { 267 out.print(" "); 268 pad(out, Integer.toString(recalcUtilization()), 3, ' '); 269 } 270 out.println(); 271 } 272 273 int utilization() { 274 return UtilizationProfile.utilization(obsoleteSize, totalSize); 275 } 276 277 int recalcUtilization() { 278 return UtilizationProfile.utilization 279 (recalcObsoleteSize, totalSize); 280 } 281 282 private void pad(PrintStream out, String val, int digits, 283 char padChar) { 284 int padSize = digits - val.length(); 285 for (int i = 0; i < padSize; i += 1) { 286 out.print(padChar); 287 } 288 out.print(val); 289 } 290 } 291 } 292 | Popular Tags |