1 8 9 package com.sleepycat.je.cleaner; 10 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.dbi.EnvironmentImpl; 16 import com.sleepycat.je.dbi.MemoryBudget; 17 import com.sleepycat.je.log.LogEntryType; 18 import com.sleepycat.je.utilint.DbLsn; 19 20 30 public class UtilizationTracker { 31 32 private EnvironmentImpl env; 33 private Cleaner cleaner; 34 private List files; 35 private long activeFile; 36 private TrackedFileSummary[] snapshot; 37 private long bytesSinceActivate; 38 39 43 public UtilizationTracker(EnvironmentImpl env) 44 throws DatabaseException { 45 46 this(env, env.getCleaner()); 47 } 48 49 53 UtilizationTracker(EnvironmentImpl env, Cleaner cleaner) 54 throws DatabaseException { 55 56 assert cleaner != null; 57 this.env = env; 58 this.cleaner = cleaner; 59 files = new ArrayList (); 60 snapshot = new TrackedFileSummary[0]; 61 activeFile = -1; 62 } 63 64 public EnvironmentImpl getEnvironment() { 65 return env; 66 } 67 68 77 public long evictMemory() 78 throws DatabaseException { 79 80 81 if (!cleaner.trackDetail) { 82 return 0; 83 } 84 85 90 if (!env.isOpen()) { 91 return 0; 92 } 93 94 MemoryBudget mb = env.getMemoryBudget(); 95 long totalEvicted = 0; 96 long totalBytes = 0; 97 int largestBytes = 0; 98 TrackedFileSummary bestFile = null; 99 100 104 TrackedFileSummary[] a = snapshot; 105 for (int i = 0; i < a.length; i += 1) { 106 107 TrackedFileSummary tfs = a[i]; 108 int mem = tfs.getMemorySize(); 109 totalBytes += mem; 110 111 if (mem > largestBytes && tfs.getAllowFlush()) { 112 largestBytes = mem; 113 bestFile = tfs; 114 } 115 } 116 117 if (bestFile != null && totalBytes > mb.getTrackerBudget()) { 118 env.getUtilizationProfile().flushFileSummary(bestFile); 119 totalEvicted += largestBytes; 120 } 121 return totalEvicted; 122 } 123 124 127 public void activateCleaner() { 128 env.getCleaner().wakeup(); 129 bytesSinceActivate = 0; 130 } 131 132 145 public TrackedFileSummary[] getTrackedFiles() { 146 return snapshot; 147 } 148 149 154 public TrackedFileSummary getTrackedFile(long fileNum) { 155 156 160 TrackedFileSummary[] a = snapshot; 161 for (int i = 0; i < a.length; i += 1) { 162 if (a[i].getFileNumber() == fileNum) { 163 return a[i]; 164 } 165 } 166 return null; 167 } 168 169 175 public boolean countNewLogEntry(long lsn, LogEntryType type, int size) { 176 177 TrackedFileSummary file = getFile(DbLsn.getFileNumber(lsn)); 178 file.totalCount += 1; 179 file.totalSize += size; 180 if (type.isNodeType()) { 181 if (inArray(type, LogEntryType.IN_TYPES)) { 182 file.totalINCount += 1; 183 file.totalINSize += size; 184 } else { 185 file.totalLNCount += 1; 186 file.totalLNSize += size; 187 } 188 } 189 bytesSinceActivate += size; 190 return (bytesSinceActivate >= env.getCleaner().cleanerBytesInterval); 191 } 192 193 202 public void countObsoleteNode(long lsn, LogEntryType type, int size) { 203 204 TrackedFileSummary file = getFile(DbLsn.getFileNumber(lsn)); 205 206 countOneNode(file, type, size); 207 208 file.trackObsolete(DbLsn.getFileOffset(lsn)); 209 } 210 211 220 public void countObsoleteNodeInexact(long lsn, 221 LogEntryType type, 222 int size) { 223 224 TrackedFileSummary file = getFile(DbLsn.getFileNumber(lsn)); 225 226 countOneNode(file, type, size); 227 } 228 229 232 private void countOneNode(TrackedFileSummary file, 233 LogEntryType type, 234 int size) { 235 236 if (type == null || type.isNodeType()) { 237 if (type == null || !inArray(type, LogEntryType.IN_TYPES)) { 238 file.obsoleteLNCount += 1; 239 240 if (size > 0) { 241 file.obsoleteLNSize += size; 242 file.obsoleteLNSizeCounted += 1; 243 } 244 } else { 245 file.obsoleteINCount += 1; 246 247 assert size == 0; 248 } 249 } 250 } 251 252 257 public void addSummary(long fileNumber, TrackedFileSummary other) { 258 259 TrackedFileSummary file = getFile(fileNumber); 260 file.addTrackedSummary(other); 261 } 262 263 267 public TrackedFileSummary getUnflushableTrackedSummary(long fileNum) 268 throws DatabaseException { 269 270 TrackedFileSummary file = getFile(fileNum); 271 file.setAllowFlush(false); 272 return file; 273 } 274 275 281 private TrackedFileSummary getFile(long fileNum) { 282 283 if (activeFile < fileNum) { 284 activeFile = fileNum; 285 } 286 int size = files.size(); 287 for (int i = 0; i < size; i += 1) { 288 TrackedFileSummary file = (TrackedFileSummary) files.get(i); 289 if (file.getFileNumber() == fileNum) { 290 return file; 291 } 292 } 293 294 298 TrackedFileSummary file = new TrackedFileSummary 299 (this, fileNum, cleaner.trackDetail); 300 301 files.add(file); 302 takeSnapshot(); 303 return file; 304 } 305 306 314 void resetFile(TrackedFileSummary file) { 315 316 if (file.getFileNumber() < activeFile && file.getAllowFlush()) { 317 files.remove(file); 318 takeSnapshot(); 319 } 320 } 321 322 327 private void takeSnapshot() { 328 332 TrackedFileSummary[] a = new TrackedFileSummary[files.size()]; 333 files.toArray(a); 334 snapshot = a; 335 } 336 337 340 private boolean inArray(Object o, Object [] a) { 341 342 for (int i = 0; i < a.length; i += 1) { 343 if (a[i] == o) { 344 return true; 345 } 346 } 347 return false; 348 } 349 } 350 | Popular Tags |