Skip to content

Commit

Permalink
Merge pull request #18424 from tajila/issue4
Browse files Browse the repository at this point in the history
(0.41) Add support for thread local allocation stats
  • Loading branch information
pshipton committed Nov 9, 2023
2 parents 6281176 + dad3c02 commit a803f00
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class ExtendedThreadMXBeanImpl extends ThreadMXBeanImpl implements

/**
* Singleton accessor method.
*
*
* @return the <code>ExtendedThreadMXBeanImpl</code> singleton.
*/
public static ThreadMXBean getInstance() {
Expand Down Expand Up @@ -67,20 +67,37 @@ public ExtendedThreadInfo[] dumpAllExtendedThreads(boolean lockedMonitors, boole
return resultArray;
}

private native static long getThreadAllocatedBytesImpl(long threadID);

/**
* {@inheritDoc}
*/
@Override
public long getThreadAllocatedBytes(long threadId) {
throw new UnsupportedOperationException();
if (isThreadAllocatedMemorySupported()) {
if (isThreadAllocatedMemoryEnabled()) {
return getThreadAllocatedBytesImpl(threadId);
} else {
return -1;
}
} else {
throw new UnsupportedOperationException();
}
}

/**
* {@inheritDoc}
*/
@Override
public long[] getThreadAllocatedBytes(long[] threadIds) {
throw new UnsupportedOperationException();
int length = threadIds.length;
long[] allocatedBytes = new long[length];

for (int i = 0; i < length; i++) {
allocatedBytes[i] = getThreadAllocatedBytes(threadIds[i]);
}

return allocatedBytes;
}

/**
Expand Down Expand Up @@ -135,27 +152,30 @@ public long[] getThreadUserTime(long[] threadIds) {
return result;
}

private boolean isThreadAllocatedMemoryEnabled = true;

/**
* {@inheritDoc}
*/
@Override
public boolean isThreadAllocatedMemorySupported() {
return false;
/* Currently, this capability is always supported. */
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isThreadAllocatedMemoryEnabled() {
throw new UnsupportedOperationException();
return isThreadAllocatedMemoryEnabled && isThreadAllocatedMemorySupported();
}

/**
* {@inheritDoc}
*/
@Override
public void setThreadAllocatedMemoryEnabled(boolean value) {
throw new UnsupportedOperationException();
isThreadAllocatedMemoryEnabled = value;
}
}
1 change: 1 addition & 0 deletions runtime/gc/gctable.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ J9MemoryManagerFunctions MemoryManagerFunctions = {
#endif /* !J9VM_ENV_DATA64 */
#endif /* J9VM_GC_OBJECT_ACCESS_BARRIER */
j9gc_get_bytes_allocated_by_thread,
j9gc_get_cumulative_bytes_allocated_by_thread,
j9mm_iterate_all_ownable_synchronizer_objects,
j9mm_iterate_all_continuation_objects,
ownableSynchronizerObjectCreated,
Expand Down
1 change: 1 addition & 0 deletions runtime/gc_base/gc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ extern BOOLEAN j9gc_stringHashEqualFn (void *leftKey, void *rightKey, void *user

/* modronapi.cpp */
extern J9_CFUNC UDATA j9gc_get_bytes_allocated_by_thread(J9VMThread* vmThread);
extern J9_CFUNC BOOLEAN j9gc_get_cumulative_bytes_allocated_by_thread(J9VMThread *vmThread, UDATA *cumulativeValue);

#ifdef __cplusplus
}
Expand Down
11 changes: 11 additions & 0 deletions runtime/gc_base/modronapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,17 @@ j9gc_get_bytes_allocated_by_thread(J9VMThread *vmThread)
return MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread)->_objectAllocationInterface->getAllocationStats()->bytesAllocated();
}

/**
* @param[in] vmThread the vmThread we are querying about
* @param[out] cumulativeValue pointer to a variable where to store cumulative number of bytes allocated by a thread since the start of VM
* @return false if the value just rolled over or if cumulativeValue pointer is null, otherwise true
*/
BOOLEAN
j9gc_get_cumulative_bytes_allocated_by_thread(J9VMThread *vmThread, UDATA *cumulativeValue)
{
return MM_EnvironmentBase::getEnvironment(vmThread->omrVMThread)->_objectAllocationInterface->getAllocationStats()->bytesAllocatedCumulative(cumulativeValue);
}

/**
* Return information about the total CPU time consumed by GC threads, as well
* as the number of GC threads. The time for the main and worker threads is
Expand Down
1 change: 1 addition & 0 deletions runtime/gc_base/modronapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void j9gc_set_memoryController(J9VMThread *vmThread, j9object_t objectPtr, j9obj
void j9gc_set_allocation_sampling_interval(J9JavaVM *vm, UDATA samplingInterval);
void j9gc_set_allocation_threshold(J9VMThread *vmThread, UDATA low, UDATA high);
UDATA j9gc_get_bytes_allocated_by_thread(J9VMThread *vmThread);
BOOLEAN j9gc_get_cumulative_bytes_allocated_by_thread(J9VMThread *vmThread, UDATA *cumulativeValue);
void j9gc_get_CPU_times(J9JavaVM *javaVM, U_64 *mainCpuMillis, U_64 *workerCpuMillis, U_32 *maxThreads, U_32 *currentThreads);
J9HookInterface** j9gc_get_private_hook_interface(J9JavaVM *javaVM);
/**
Expand Down
Loading

0 comments on commit a803f00

Please sign in to comment.