-
Notifications
You must be signed in to change notification settings - Fork 588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BufferPoolMXBean to track allocated/deallocated pointers #413
Conversation
It doesn't look like that API is available on Android. Would it be possible to split this out of Pointer, into the |
Oh, OK, yes it makes sense to have it decoupled from |
Yeah, you can still refer to that class in the tools directory, that's fine. |
8154274
to
af46e9f
Compare
Looking good, thanks! Could you elaborate on the need for an atomic? I've been using just As for the name of the class, we can get rid of the "s", that is And the check for the system property should go in |
@netbrain I think this MXBean would fix bytedeco/javacv#1337, what do you think? |
The
So either you need to use a
OK, applied suggestions. |
Yeah, that synchronized block is already there. What am I missing? |
Ops, sorry somehow I got wrong here, thinking the write was not inside a I checked again the code and maybe I have only one doubt : you basically use |
Right, I never had any issues with reading the value. If it's OK, let's remove the atomic. Thanks! |
I'm not able to push changes to your repository, but if you could apply this patch, that looks good to merge. Thanks! diff --git a/src/main/java/org/bytedeco/javacpp/Pointer.java b/src/main/java/org/bytedeco/javacpp/Pointer.java
index f892e63..a17d8af 100644
--- a/src/main/java/org/bytedeco/javacpp/Pointer.java
+++ b/src/main/java/org/bytedeco/javacpp/Pointer.java
@@ -32,7 +32,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
import org.bytedeco.javacpp.annotation.Name;
import org.bytedeco.javacpp.tools.Generator;
import org.bytedeco.javacpp.tools.PointerBufferPoolMXBean;
@@ -286,7 +285,7 @@ public class Pointer implements AutoCloseable {
Deallocator deallocator;
static volatile long totalBytes = 0;
- static AtomicLong totalCount = new AtomicLong();
+ static volatile long totalCount = 0;
long bytes;
AtomicInteger count;
@@ -300,7 +299,7 @@ public class Pointer implements AutoCloseable {
next.prev = head = this;
}
totalBytes += bytes;
- totalCount.incrementAndGet();
+ totalCount++;
}
}
@@ -319,7 +318,7 @@ public class Pointer implements AutoCloseable {
}
prev = next = this;
totalBytes -= bytes;
- totalCount.decrementAndGet();
+ totalCount--;
}
}
@@ -538,7 +537,7 @@ public class Pointer implements AutoCloseable {
/** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */
public static long totalCount() {
- return DeallocatorReference.totalCount.get();
+ return DeallocatorReference.totalCount;
}
/** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */ |
Oh, you meant to remove the But one important thing I was actually thinking: it won't be right either using |
That's just for the MXBean, right? We can easily pass a reference to DeallocatorReference.class on register() and have a synchronized block inside there. |
FWIW, I don't think consumers of BufferPoolMXBean expect these values to be synchronized. Calls to |
…property org.bytedeco.javacpp.mxbean in Pointer
…y are both updated at the same time inside a synchronized block. Also created thread-safe increment/decrement methods as they are used in the tests.
4153c34
to
0868fd0
Compare
I think we just have to make the library right: as Just pushed: now only |
Adding getters with synchronized methods is not going to do anything else than make everything slower. That's why volatile exists! We can use them to make things faster! So please put it back like it was. |
…r values are changed together in a synchronized block, when reading their values might not be in sync. This has been done to favor speed over correctness.
83a76d2
to
5ad905b
Compare
Thanks! Like I said, using volatile instead of 2 separate independent synchronized blocks in 2 separate independent method calls that can occur at any undetermined time across multiple threads is going to give the same result. Neither can provide 2 values that are in sync, but the synchronized blocks are a whole lot slower. |
I'm seeing load errors on Android after this change:
|
Those are just warnings though, right? Would you know the "proper" way to make sure Android doesn't try to initialize optional classes like that? |
Maybe we should be using reflection to call |
This PR adds a
BufferMXBean
to allow tracking of allocation/deallocation of pointers by JavaCPP.Note: I'm using
AtomicLong
for the counter asvolatile
is not enough to ensure exclusive access when incrementing the value using++
. ThetotalBytes
field should also be fixed either synchronizing onDeallocatorThread.class
when accessing it or useAtomicLong
as well.