Skip to content

Commit ad62756

Browse files
authored
Merge pull request #33 from com-github-javabdd/31-capture-maximum-used-memory-statistic
#31 Add max memory statistics.
2 parents b14bd93 + f241b3c commit ad62756

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

src/main/java/com/github/javabdd/BDDFactory.java

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,67 @@ public MaxUsedBddNodesStats getMaxUsedBddNodesStats() {
15461546
return maxusedbddnodesstats;
15471547
}
15481548

1549+
/**
1550+
* Stores statistics about the maximum memory usage. The data is obtained through best effort, and may not be
1551+
* entirely accurate.
1552+
*/
1553+
public static class MaxMemoryStats {
1554+
protected boolean enabled = false;
1555+
1556+
protected long maxMemoryBytes;
1557+
1558+
protected MaxMemoryStats() {
1559+
}
1560+
1561+
void copyFrom(MaxMemoryStats that) {
1562+
this.maxMemoryBytes = that.maxMemoryBytes;
1563+
}
1564+
1565+
public void enableMeasurements() {
1566+
enabled = true;
1567+
}
1568+
1569+
public void disableMeasurements() {
1570+
enabled = false;
1571+
}
1572+
1573+
public void resetMeasurements() {
1574+
maxMemoryBytes = 0;
1575+
}
1576+
1577+
public void newMeasurement() {
1578+
long newMemoryBytes = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
1579+
maxMemoryBytes = Math.max(newMemoryBytes, maxMemoryBytes);
1580+
}
1581+
1582+
public long getMaxMemoryBytes() {
1583+
return maxMemoryBytes;
1584+
}
1585+
1586+
@Override
1587+
public String toString() {
1588+
StringBuilder sb = new StringBuilder();
1589+
sb.append("Max memory: ");
1590+
sb.append(maxMemoryBytes);
1591+
sb.append(" bytes");
1592+
return sb.toString();
1593+
}
1594+
}
1595+
1596+
/**
1597+
* Singleton object for maximum memory usage statistics.
1598+
*/
1599+
protected MaxMemoryStats maxmemorystats = new MaxMemoryStats();
1600+
1601+
/**
1602+
* Return the current maximum memory usage statistics for this BDD factory.
1603+
*
1604+
* @return maximum memory usage statistics
1605+
*/
1606+
public MaxMemoryStats getMaxMemoryStats() {
1607+
return maxmemorystats;
1608+
}
1609+
15491610
// TODO: bdd_sizeprobe_hook
15501611
// TODO: bdd_reorder_probe
15511612

@@ -2098,6 +2159,17 @@ public static interface MaxUsedBddNodesStatsCallback {
20982159
public void maxUsedBddNodes(MaxUsedBddNodesStats stats);
20992160
}
21002161

2162+
/** Maximum memory usage statistics callback. */
2163+
@FunctionalInterface
2164+
public static interface MaxMemoryStatsCallback {
2165+
/**
2166+
* Maximum memory usage statistics callback.
2167+
*
2168+
* @param stats The statistics.
2169+
*/
2170+
public void maxMemory(MaxMemoryStats stats);
2171+
}
2172+
21012173
/** Continuously BDD nodes usage and BDD operations statistics callback. */
21022174
@FunctionalInterface
21032175
public static interface ContinuousStatsCallback {
@@ -2124,12 +2196,12 @@ public static interface ContinuousStatsCallback {
21242196
/** The registered operator cache statistics callbacks, or {@code null} if none registered. */
21252197
protected List<CacheStatsCallback> cacheCallbacks = null;
21262198

2127-
/**
2128-
* The registered maximum BDD nodes usage statistics callback statistics callbacks, or {@code null} if none
2129-
* registered.
2130-
*/
2199+
/** The registered maximum BDD nodes usage statistics callbacks, or {@code null} if none registered. */
21312200
protected List<MaxUsedBddNodesStatsCallback> maxUsedBddNodesCallbacks = null;
21322201

2202+
/** The registered maximum memory usage statistics callbacks, or {@code null} if none registered. */
2203+
protected List<MaxMemoryStatsCallback> maxMemoryCallbacks = null;
2204+
21332205
/**
21342206
* The registered continuously BDD nodes usage and BDD operations statistics callbacks, or {@code null} if none
21352207
* registered.
@@ -2196,6 +2268,18 @@ public void registerMaxUsedBddNodesStatsCallback(MaxUsedBddNodesStatsCallback ca
21962268
maxUsedBddNodesCallbacks.add(callback);
21972269
}
21982270

2271+
/**
2272+
* Register a maximum memory usage statistics callback.
2273+
*
2274+
* @param callback The callback to register.
2275+
*/
2276+
public void registerMaxMemoryStatsCallback(MaxMemoryStatsCallback callback) {
2277+
if (maxMemoryCallbacks == null) {
2278+
maxMemoryCallbacks = new LinkedList<>();
2279+
}
2280+
maxMemoryCallbacks.add(callback);
2281+
}
2282+
21992283
/**
22002284
* Register a continuously BDD nodes usage and BDD operations statistics callback.
22012285
*
@@ -2313,6 +2397,27 @@ public void unregisterMaxUsedBddNodesStatsCallback(MaxUsedBddNodesStatsCallback
23132397
throw new IllegalArgumentException();
23142398
}
23152399

2400+
/**
2401+
* Unregister a maximum memory usage statistics callback.
2402+
*
2403+
* @param callback The callback to unregister.
2404+
* @throws IllegalArgumentException If callback is not registered.
2405+
*/
2406+
public void unregisterMaxMemoryStatsCallback(MaxMemoryStatsCallback callback) {
2407+
if (maxMemoryCallbacks != null) {
2408+
for (Iterator<MaxMemoryStatsCallback> iter = maxMemoryCallbacks.iterator(); iter.hasNext();) {
2409+
if (iter.next() == callback) {
2410+
iter.remove();
2411+
if (maxMemoryCallbacks.isEmpty()) {
2412+
maxMemoryCallbacks = null;
2413+
}
2414+
return;
2415+
}
2416+
}
2417+
}
2418+
throw new IllegalArgumentException();
2419+
}
2420+
23162421
/**
23172422
* Unregister a continuously BDD nodes usage and BDD operations statistics callback.
23182423
*
@@ -2379,6 +2484,15 @@ public boolean hasMaxUsedBddNodesStatsCallback() {
23792484
return maxUsedBddNodesCallbacks != null;
23802485
}
23812486

2487+
/**
2488+
* Returns whether this BDD factory has a registered maximum memory usage statistics callback.
2489+
*
2490+
* @return {@code true} if such a callback is registered, {@code false} otherwise.
2491+
*/
2492+
public boolean hasMaxMemoryStatsCallback() {
2493+
return maxMemoryCallbacks != null;
2494+
}
2495+
23822496
/**
23832497
* Returns whether this BDD factory has a registered continuously BDD nodes usage and BDD operations statistics
23842498
* callback.
@@ -2447,6 +2561,15 @@ public void invokeMaxUsedBddNodesStatsCallbacks() {
24472561
}
24482562
}
24492563

2564+
/** Invoke all registered maximum memory usage statistics callbacks. */
2565+
public void invokeMaxMemoryStatsCallbacks() {
2566+
if (maxMemoryCallbacks != null) {
2567+
for (MaxMemoryStatsCallback callback: maxMemoryCallbacks) {
2568+
callback.maxMemory(maxmemorystats);
2569+
}
2570+
}
2571+
}
2572+
24502573
/**
24512574
* Invoke all registered continuously BDD nodes usage and BDD operations statistics callbacks.
24522575
*
@@ -2529,6 +2652,15 @@ public static void defaultMaxUsedBddNodesStatsCallback(MaxUsedBddNodesStats stat
25292652
System.out.println(stats.toString());
25302653
}
25312654

2655+
/**
2656+
* Default maximum memory usage statistics callback.
2657+
*
2658+
* @param stats The statistics.
2659+
*/
2660+
public static void defaultMaxMemoryStatsCallback(MaxMemoryStats stats) {
2661+
System.out.println(stats.toString());
2662+
}
2663+
25322664
/**
25332665
* Default continuously BDD nodes usage and BDD operations statistics callback.
25342666
*

src/main/java/com/github/javabdd/JFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ public JFactory cloneFactory() {
579579
INSTANCE.verbose = this.verbose;
580580
INSTANCE.cachestats.copyFrom(this.cachestats);
581581
INSTANCE.maxusedbddnodesstats.copyFrom(this.maxusedbddnodesstats);
582+
INSTANCE.maxmemorystats.copyFrom(this.maxmemorystats);
582583

583584
INSTANCE.bddrunning = this.bddrunning;
584585
INSTANCE.bdderrorcond = this.bdderrorcond;
@@ -6010,6 +6011,11 @@ void bdd_add_perf_stats() {
60106011
// be taken from the cache. Thus it approximates time.
60116012
invokeContinuousStatsCallbacks(usedBddNodes, cachestats.opMiss);
60126013
}
6014+
6015+
if (maxmemorystats.enabled || hasMaxMemoryStatsCallback()) {
6016+
maxmemorystats.newMeasurement();
6017+
invokeMaxMemoryStatsCallbacks();
6018+
}
60136019
}
60146020

60156021
void bdd_done() {

0 commit comments

Comments
 (0)