Skip to content

Commit 72b5023

Browse files
authored
Debug Improvements (#938)
1 parent 7d0ad72 commit 72b5023

File tree

9 files changed

+87
-23
lines changed

9 files changed

+87
-23
lines changed

src/main/java/software/amazon/awssdk/crt/CrtResource.java

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ public CrtResource() {
124124
* @param resource The resource to add a reference to
125125
*/
126126
public void addReferenceTo(CrtResource resource) {
127+
if (debugNativeObjects) {
128+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
129+
String.format("%s(%d) is adding a reference to %s(%d)",
130+
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
131+
}
127132
resource.addRef();
128133
synchronized(this) {
129134
referencedResources.add(resource);
130135
}
131-
132-
if (debugNativeObjects) {
133-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) is adding a reference to instance of class %s(%d)", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
134-
}
135136
}
136137

137138
/**
@@ -146,9 +147,13 @@ public void removeReferenceTo(CrtResource resource) {
146147

147148
if (debugNativeObjects) {
148149
if (removed) {
149-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) is removing a reference to instance of class %s(%d)", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
150+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
151+
String.format("%s(%d) is removing a reference to %s(%d)",
152+
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
150153
} else {
151-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) erroneously tried to remove a reference to instance of class %s(%d) that it was not referencing", this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
154+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
155+
String.format("%s(%d) erroneously tried to remove a reference to %s(%d) that it was not referencing",
156+
this.getClass().getCanonicalName(), id, resource.getClass().getCanonicalName(), resource.id));
152157
}
153158
}
154159

@@ -209,7 +214,8 @@ protected void acquireNativeHandle(long handle) {
209214
*/
210215
private void release() {
211216
if (debugNativeObjects) {
212-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Releasing class %s(%d)", this.getClass().getCanonicalName(), id));
217+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
218+
String.format("Releasing class %s(%d)", this.getClass().getCanonicalName(), id));
213219

214220
synchronized(CrtResource.class) {
215221
CRT_RESOURCES.remove(id);
@@ -237,7 +243,25 @@ public long getNativeHandle() {
237243
* Increments the reference count to this resource.
238244
*/
239245
public void addRef() {
240-
refCount.incrementAndGet();
246+
int updatedRefs = refCount.incrementAndGet();
247+
if (debugNativeObjects) {
248+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
249+
String.format("%s(%d) is adding a reference. refCount is now %d",
250+
this.getClass().getCanonicalName(), id, updatedRefs));
251+
}
252+
}
253+
254+
/**
255+
* Increments the reference count to this resource with a description.
256+
* @param desc Descrption string of why the reference is being incremented.
257+
*/
258+
public void addRef(String desc) {
259+
int updatedRefs = refCount.incrementAndGet();
260+
if (debugNativeObjects) {
261+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
262+
String.format("%s(%d) is adding a reference for: (%s). RefCount is now %d",
263+
this.getClass().getCanonicalName(), id, desc, updatedRefs));
264+
}
241265
}
242266

243267
/**
@@ -274,7 +298,11 @@ public boolean isNull() {
274298

275299
@Override
276300
public void close() {
277-
decRef();
301+
decRef("close() called");
302+
}
303+
304+
public void close(String desc) {
305+
decRef(desc);
278306
}
279307

280308
/**
@@ -286,12 +314,37 @@ public void decRef(CrtResource decRefInstigator) {
286314
int remainingRefs = refCount.decrementAndGet();
287315

288316
if (debugNativeObjects) {
289-
if (decRefInstigator != null) {
290-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format(
291-
"DecRef instance of class %s(%d) called by %s(%d). %d remaining refs", this.getClass().getCanonicalName(), id,
292-
decRefInstigator.getClass().getCanonicalName(), decRefInstigator.id, remainingRefs));
317+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format(
318+
"DecRef of %s(%d) called by %s(%d). %d remaining refs", this.getClass().getCanonicalName(), id,
319+
decRefInstigator.getClass().getCanonicalName(), decRefInstigator.id, remainingRefs));
320+
}
321+
322+
if (remainingRefs != 0) {
323+
return;
324+
}
325+
326+
release();
327+
328+
if (canReleaseReferencesImmediately()) {
329+
releaseReferences();
330+
}
331+
}
332+
333+
/**
334+
* Decrements the reference count to this resource with a description.
335+
* @param desc Descrption string of why the reference is being decremented.
336+
*/
337+
public void decRef(String desc) {
338+
int remainingRefs = refCount.decrementAndGet();
339+
340+
if (debugNativeObjects) {
341+
if (desc != null) {
342+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
343+
String.format("DecRef on %s(%d) for: (%s). %d remaining refs",
344+
this.getClass().getCanonicalName(), id, desc, remainingRefs));
293345
} else {
294-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("DecRef instance of class %s(%d) via self.close(). %d remaining refs",
346+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
347+
String.format("Defref on %s(%d). %d remaining refs",
295348
this.getClass().getCanonicalName(), id, remainingRefs));
296349
}
297350
}
@@ -306,11 +359,12 @@ public void decRef(CrtResource decRefInstigator) {
306359
releaseReferences();
307360
}
308361
}
362+
309363
/**
310364
* Decrements the reference count to this resource.
311365
*/
312366
public void decRef() {
313-
decRef(null);
367+
decRef((String)null);
314368
}
315369

316370
/**
@@ -320,7 +374,9 @@ public void decRef() {
320374
*/
321375
protected void releaseReferences() {
322376
if (debugNativeObjects) {
323-
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource, String.format("Instance of class %s(%d) closing all referenced objects", this.getClass().getCanonicalName(), id));
377+
Log.log(ResourceLogLevel, Log.LogSubject.JavaCrtResource,
378+
String.format("%s(%d) closing all referenced objects",
379+
this.getClass().getCanonicalName(), id));
324380
}
325381

326382
synchronized(this) {

src/main/java/software/amazon/awssdk/crt/eventstream/ClientConnectionContinuationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public CompletableFuture<Void> getContinuationClosedFuture() {
6767
@Override
6868
public void close() {
6969
if (continuation != null) {
70-
continuation.decRef();
70+
continuation.decRef("close() called");
7171
continuation = null;
7272
}
7373
}

src/main/java/software/amazon/awssdk/crt/eventstream/ClientConnectionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void close() {
9393
if (clientConnection.isOpen()) {
9494
clientConnection.closeConnection(0);
9595
}
96-
clientConnection.decRef();
96+
clientConnection.decRef("close() called");
9797
clientConnection = null;
9898
}
9999
}

src/main/java/software/amazon/awssdk/crt/eventstream/ServerConnectionContinuationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public CompletableFuture<Void> getContinuationClosedFuture() {
6868
@Override
6969
public void close() {
7070
if (continuation != null) {
71-
continuation.decRef();
71+
continuation.decRef("close() called");
7272
continuation = null;
7373
}
7474
}

src/main/java/software/amazon/awssdk/crt/eventstream/ServerConnectionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void close() {
7676
if (connection.isConnectionOpen()) {
7777
connection.closeConnection(0);
7878
}
79-
connection.decRef();
79+
connection.decRef("close() called");
8080
connection = null;
8181
}
8282
}

src/main/java/software/amazon/awssdk/crt/io/ClientBootstrap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void onShutdownComplete() {
9090
public static void closeStaticDefault() {
9191
synchronized (ClientBootstrap.class) {
9292
if (staticDefaultClientBootstrap != null) {
93-
staticDefaultClientBootstrap.close();
93+
staticDefaultClientBootstrap.close("closeStaticDefault");
9494
}
9595
staticDefaultClientBootstrap = null;
9696
}

src/main/java/software/amazon/awssdk/crt/io/EventLoopGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static void setStaticDefaultNumThreads(int numThreads) {
9595
public static void closeStaticDefault() {
9696
synchronized (EventLoopGroup.class) {
9797
if (staticDefaultEventLoopGroup != null) {
98-
staticDefaultEventLoopGroup.close();
98+
staticDefaultEventLoopGroup.close("closeStaticDefault");
9999
}
100100
staticDefaultEventLoopGroup = null;
101101
}

src/main/java/software/amazon/awssdk/crt/io/HostResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void setStaticDefaultMaxEntries(int maxEntries) {
7373
public static void closeStaticDefault() {
7474
synchronized (HostResolver.class) {
7575
if (staticDefaultResolver != null) {
76-
staticDefaultResolver.close();
76+
staticDefaultResolver.close("closeStaticDefault");
7777
}
7878
staticDefaultResolver = null;
7979
}

src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
2323
import org.junit.BeforeClass;
2424
import org.junit.After;
2525
import org.junit.Assume;
26+
import org.junit.Rule;
27+
import org.junit.rules.TestName;
28+
2629

2730
import java.util.Optional;
2831

2932
public class CrtTestFixture {
3033

3134
private CrtTestContext context;
3235

36+
@Rule
37+
public TestName testName = new TestName();
38+
3339
public final CrtTestContext getContext() {
3440
return context;
3541
}
@@ -230,6 +236,7 @@ public static void setupOnce() {
230236
/* The setup function will be run before every test */
231237
@Before
232238
public void setup() {
239+
System.out.println("[TEST START] " + testName.getMethodName());
233240
Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup begin");
234241

235242
// TODO this CrtTestContext should be removed as we are using System Properties
@@ -258,6 +265,7 @@ public void tearDown() {
258265
}
259266
}
260267
Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture tearDown end");
268+
System.out.println("[TEST END] " + testName.getMethodName());
261269
}
262270

263271
protected TlsContext createTlsContextOptions(byte[] trustStore) {

0 commit comments

Comments
 (0)