Skip to content

Commit 105d04e

Browse files
committed
better logging for CrtResource refCount tracking
1 parent 26b4c28 commit 105d04e

1 file changed

Lines changed: 72 additions & 16 deletions

File tree

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) {

0 commit comments

Comments
 (0)