Skip to content

Commit ec76029

Browse files
committed
Temp - add cache info WIP
1 parent a0aaf40 commit ec76029

File tree

5 files changed

+147
-3
lines changed

5 files changed

+147
-3
lines changed

src/main/java/sc/fiji/bdvpg/cache/AbstractGlobalCache.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,34 @@ else if (data instanceof IntAccess) {
149149

150150
public abstract <V> void touch(GlobalCacheKey key, V value);
151151

152+
/**
153+
* Statistics for cached data for a specific source and timepoint.
154+
*/
155+
public static class CacheStats {
156+
public final long numberOfCells;
157+
public final long sizeInBytes;
158+
159+
public CacheStats(long numberOfCells, long sizeInBytes) {
160+
this.numberOfCells = numberOfCells;
161+
this.sizeInBytes = sizeInBytes;
162+
}
163+
164+
public String getSizeInMB() {
165+
return String.format("%.2f", sizeInBytes / (1024.0 * 1024.0));
166+
}
167+
168+
public String getSizeInKB() {
169+
return String.format("%.2f", sizeInBytes / 1024.0);
170+
}
171+
}
172+
173+
/**
174+
* Get cache statistics for a specific source and timepoint.
175+
*
176+
* @param source the source object
177+
* @param timepoint the timepoint (-1 for all timepoints)
178+
* @return cache statistics
179+
*/
180+
abstract public CacheStats getCacheStats(Object source, int setupId, int timepoint);
181+
152182
}

src/main/java/sc/fiji/bdvpg/cache/BoundedLinkedHashMapGlobalCache.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929

3030
package sc.fiji.bdvpg.cache;
3131

32+
import bdv.img.cache.VolatileGlobalCellCache;
3233
import org.slf4j.Logger;
3334
import org.slf4j.LoggerFactory;
3435

3536
import java.lang.ref.SoftReference;
37+
import java.lang.reflect.Field;
3638
import java.util.HashMap;
3739
import java.util.LinkedHashMap;
3840
import java.util.Map;
@@ -193,4 +195,56 @@ public String toString() {
193195
" %)";
194196
}
195197

198+
@Override
199+
public CacheStats getCacheStats(Object source, int setupid, int timepoint) {
200+
long totalSize = 0;
201+
long cellCount = 0;
202+
try {
203+
204+
Field tpField = VolatileGlobalCellCache.Key.class.getDeclaredField("timepoint");
205+
tpField.setAccessible(true);
206+
Field setupField = VolatileGlobalCellCache.Key.class.getDeclaredField("setup");
207+
setupField.setAccessible(true);
208+
209+
synchronized (cache) {
210+
for (Map.Entry<GlobalCacheKey, SoftReference<Object>> entry : cache.entrySet()) {
211+
GlobalCacheKey key = entry.getKey();
212+
VolatileGlobalCellCache.Key innerKey = null;
213+
if (key.key.get() instanceof VolatileGlobalCellCache.Key) {
214+
innerKey = (VolatileGlobalCellCache.Key) key.key.get();
215+
}
216+
if (innerKey == null) continue;
217+
int tp = (int) tpField.get(innerKey);
218+
int setupId = (int) setupField.get(innerKey);
219+
220+
if (timepoint == -1) {
221+
// Match source for any timepoint
222+
if ((key.getSource() == source) && (setupid == setupId)) {
223+
Long cost = cache.cost.get(key);
224+
if (cost != null) {
225+
totalSize += cost;
226+
cellCount++;
227+
}
228+
}
229+
} else {
230+
// Match source and specific timepoint (any level)
231+
if ((key.getSource() == source) && (tp == timepoint) && (setupid == setupId)) {
232+
Long cost = cache.cost.get(key);
233+
if (cost != null) {
234+
totalSize += cost;
235+
cellCount++;
236+
}
237+
}
238+
}
239+
240+
}
241+
return new CacheStats(cellCount, totalSize);
242+
}
243+
244+
} catch (Exception e) {
245+
e.printStackTrace();
246+
return new CacheStats(0,0);
247+
}
248+
}
249+
196250
}

src/main/java/sc/fiji/bdvpg/cache/CaffeineGlobalCache.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,33 @@ public String toString() {
129129
(int) (100.0 * (double) totalBytes / (double) maxCacheSize) + " %)";
130130
}
131131

132+
@Override
133+
public CacheStats getCacheStats(Object source, int setupid, int timepoint) {
134+
long totalSize = 0;
135+
long cellCount = 0;
136+
137+
for (GlobalCacheKey key : cache.asMap().keySet()) {
138+
if (timepoint == -1) {
139+
// Match source for any timepoint
140+
if (key.source.get() == source) {
141+
Object value = cache.getIfPresent(key);
142+
if (value != null) {
143+
totalSize += getWeight(value);
144+
cellCount++;
145+
}
146+
}
147+
} else { // Match source and specific timepoint (any level)
148+
if (key.getSource() == source && key.getTimepoint() == timepoint) {
149+
Object value = cache.getIfPresent(key);
150+
if (value != null) {
151+
totalSize += getWeight(value);
152+
cellCount++;
153+
}
154+
}
155+
}
156+
}
157+
158+
return new CacheStats(cellCount, totalSize);
159+
}
160+
132161
}

src/main/java/sc/fiji/bdvpg/cache/GlobalCacheKey.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
public class GlobalCacheKey {
3535

36-
private final WeakReference<Object> source;
36+
protected final WeakReference<Object> source;
3737

3838
private final int timepoint;
3939

4040
private final int level;
4141

42-
public final WeakReference<Object> key;
42+
protected final WeakReference<Object> key;
4343

4444
public GlobalCacheKey(final Object source, final int timepoint,
4545
final int level, final Object key)
@@ -86,4 +86,16 @@ public boolean equals(final Object other) {
8686
public int hashCode() {
8787
return hashcode;
8888
}
89+
90+
public int getTimepoint() {
91+
return timepoint;
92+
}
93+
94+
public int getLevel() {
95+
return level;
96+
}
97+
98+
public Object getSource() {
99+
return source.get();
100+
}
89101
}

src/main/java/sc/fiji/bdvpg/scijava/services/ui/SourceAndConverterInspector.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import net.imglib2.realtransform.RealTransform;
5050
import org.slf4j.Logger;
5151
import org.slf4j.LoggerFactory;
52+
import sc.fiji.bdvpg.cache.AbstractGlobalCache;
5253
import sc.fiji.bdvpg.scijava.services.SourceAndConverterService;
5354
import sc.fiji.bdvpg.services.ISourceAndConverterService;
5455
import sc.fiji.bdvpg.services.SourceAndConverterServices;
@@ -586,7 +587,7 @@ private static void appendAttributesInfo(DefaultMutableTreeNode parent,
586587
private static void appendViewRegistrationsInfo(DefaultMutableTreeNode parent, AbstractSpimData<?> asd, int setupId, SourceAndConverter<?> sac) {
587588

588589
ViewRegistrations vrs = asd.getViewRegistrations();
589-
DefaultMutableTreeNode registrationsNode = new DefaultMutableTreeNode("View Transforms");
590+
DefaultMutableTreeNode registrationsNode = new DefaultMutableTreeNode("Data");
590591
parent.add(registrationsNode);
591592

592593
// Get the source to query dimensions
@@ -670,6 +671,24 @@ public String toString() {
670671
} catch (Exception e) {
671672
logger.debug("Could not get dimension info for timepoint " + tp.getId() + ": " + e.getMessage());
672673
}
674+
675+
// Add cache statistics for this timepoint
676+
try {
677+
AbstractGlobalCache globalCache = SourceAndConverterServices
678+
.getSourceAndConverterService().getCache();
679+
if (globalCache != null) {
680+
// Get the root source object for cache lookup
681+
//Source<?> rootSource = SourceAndConverterHelper.getRootSource(sac.getSpimSource());
682+
AbstractGlobalCache.CacheStats stats = globalCache.getCacheStats(asd, setupId, tp.getId());
683+
//if (stats.numberOfCells > 0) {
684+
String cacheInfo = "Cache: " + stats.numberOfCells + " cells, " +
685+
stats.getSizeInMB() + " MB";
686+
tpNode.add(new DefaultMutableTreeNode(cacheInfo));
687+
//}
688+
}
689+
} catch (Exception e) {
690+
logger.debug("Could not get cache stats for timepoint " + tp.getId() + ": " + e.getMessage());
691+
}
673692
}
674693
}
675694
}

0 commit comments

Comments
 (0)