Skip to content

Commit 79753b9

Browse files
committed
JFR PhysicalMemory event fix-up
In V2, respond to emit event request immediately instead of deferring to chunk generation. This allows multiple such events to be generated in one chunk. Related: #24194 Signed-off-by: Gengchen Tuo <gengchen.tuo@ibm.com>
1 parent 7d59d0f commit 79753b9

6 files changed

Lines changed: 115 additions & 20 deletions

File tree

runtime/oti/j9nonbuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,12 @@ typedef struct J9JFRThreadAllocationStatistics {
590590
U_64 allocated;
591591
} J9JFRThreadAllocationStatistics;
592592

593+
typedef struct J9JFRPhysicalMemory {
594+
J9JFR_EVENT_COMMON_FIELDS
595+
U_64 totalSize;
596+
U_64 usedSize;
597+
} J9JFRPhysicalMemory;
598+
593599
#endif /* defined(J9VM_OPT_JFR) */
594600

595601
/* @ddr_namespace: map_to_type=J9CfrError */

runtime/vm/BufferWriter.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,13 @@ class VM_BufferWriter {
349349
{
350350
OMRPORT_ACCESS_FROM_J9PORT(_portLibrary);
351351
va_list args;
352+
va_list args_copy;
352353
va_start(args, format);
354+
va_copy(args_copy, args);
353355
/* Minus 1 because omrstr_vprintf accounts for null terminator if buffer is null. */
354356
uintptr_t totalLength = omrstr_vprintf(NULL, 0, format, args) - 1;
355357
if (checkBounds(totalLength)) {
356-
omrstr_vprintf((char *)_cursor, _bufferEnd - _cursor, format, args);
358+
omrstr_vprintf((char *)_cursor, _bufferEnd - _cursor, format, args_copy);
357359
_cursor += totalLength;
358360
}
359361
va_end(args);

runtime/vm/JFRChunkWriter.hpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class VM_JFRChunkWriter {
218218
static constexpr int MONITOR_ENTER_EVENT_SIZE = sizeof(U_32) + (3 * LEB128_64_SIZE) + (5 * LEB128_32_SIZE);
219219
static constexpr int THREAD_PARK_EVENT_SIZE = (9 * sizeof(U_64)) + sizeof(U_32);
220220
static constexpr int JVM_INFORMATION_EVENT_SIZE = 3000;
221-
static constexpr int PHYSICAL_MEMORY_EVENT_SIZE = (4 * sizeof(U_64)) + sizeof(U_32);
221+
static constexpr int PHYSICAL_MEMORY_EVENT_SIZE = LEB128_32_SIZE + (4 * LEB128_64_SIZE);
222222
static constexpr int VIRTUALIZATION_INFORMATION_EVENT_SIZE = 50;
223223
static constexpr int CPU_INFORMATION_EVENT_SIZE = 600;
224224
static constexpr int OS_INFORMATION_EVENT_SIZE = 100;
@@ -479,6 +479,8 @@ class VM_JFRChunkWriter {
479479

480480
pool_do(_constantPoolTypes.getGarbageCollectionTable(), &writeGarbageCollectionEvent, _bufferWriter);
481481

482+
pool_do(_constantPoolTypes.getPhysicalMemoryTable(), &writePhysicalMemoryEvent, _bufferWriter);
483+
482484
pool_do(_constantPoolTypes.getGCHeapSummaryTable(), &writeGCHeapSummaryEvent, _bufferWriter);
483485

484486
pool_do(_constantPoolTypes.getNetworkUtilizationTable(), &writeNetworkUtilizationEvent, this);
@@ -544,10 +546,6 @@ class VM_JFRChunkWriter {
544546
if (_constantPoolTypes.shouldWriteYoungGenerationConfigurationEvent()) {
545547
writeYoungGenerationConfigurationEvent();
546548
}
547-
548-
if (_constantPoolTypes.shouldWritePhysicalMemory()) {
549-
writePhysicalMemoryEvent();
550-
}
551549
}
552550

553551
writeJFRHeader();
@@ -875,6 +873,32 @@ class VM_JFRChunkWriter {
875873
writeEventSize(_bufferWriter, dataStart);
876874
}
877875

876+
static void
877+
writePhysicalMemoryEvent(void *anElement, void *userData)
878+
{
879+
PhysicalMemoryEntry *entry = (PhysicalMemoryEntry *)anElement;
880+
VM_BufferWriter *_bufferWriter = (VM_BufferWriter *)userData;
881+
882+
/* reserve size field */
883+
U_8 *dataStart = reserveEventSize(_bufferWriter);
884+
885+
/* write event type */
886+
_bufferWriter->writeLEB128(PhysicalMemoryID);
887+
888+
/* write start time */
889+
_bufferWriter->writeLEB128(entry->ticks);
890+
891+
/* write total size */
892+
_bufferWriter->writeLEB128(entry->totalSize);
893+
894+
/* write used size */
895+
_bufferWriter->writeLEB128(entry->usedSize);
896+
897+
/* write size */
898+
writeEventSize(_bufferWriter, dataStart);
899+
}
900+
901+
878902
void
879903
writeJFRChunkToFile()
880904
{
@@ -1047,7 +1071,7 @@ class VM_JFRChunkWriter {
10471071

10481072
requiredBufferSize += OS_INFORMATION_EVENT_SIZE;
10491073

1050-
requiredBufferSize += PHYSICAL_MEMORY_EVENT_SIZE;
1074+
requiredBufferSize += (_constantPoolTypes.getPhysicalMemoryCount() * PHYSICAL_MEMORY_EVENT_SIZE);
10511075

10521076
requiredBufferSize += VIRTUALIZATION_INFORMATION_EVENT_SIZE;
10531077

runtime/vm/JFRConstantPoolTypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,28 @@ VM_JFRConstantPoolTypes::addThreadAllocationStatistics(J9JFRThreadAllocationStat
15611561
entry->threadIndex = threadAlocationData->currentThreadTID;
15621562
entry->allocated = threadAlocationData->allocated;
15631563

1564+
_threadAllocationStatisticsCount += 1;
1565+
1566+
done:
1567+
return;
1568+
}
1569+
1570+
void
1571+
VM_JFRConstantPoolTypes::addPhysicalMemoryEntry(J9JFRPhysicalMemory *physicalMemoryData)
1572+
{
1573+
PhysicalMemoryEntry *entry = (PhysicalMemoryEntry *)pool_newElement(_physicalMemoryTable);
1574+
1575+
if (NULL == entry) {
1576+
_buildResult = OutOfMemory;
1577+
goto done;
1578+
}
1579+
1580+
entry->ticks = physicalMemoryData->startTicks;
1581+
entry->totalSize = physicalMemoryData->totalSize;
1582+
entry->usedSize = physicalMemoryData->usedSize;
1583+
1584+
_physicalMemoryCount += 1;
1585+
15641586
done:
15651587
return;
15661588
}

runtime/vm/JFRConstantPoolTypes.hpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ struct ThreadDumpEntry {
467467
UDATA resultLength;
468468
};
469469

470+
struct PhysicalMemoryEntry {
471+
I_64 ticks;
472+
U_64 totalSize;
473+
U_64 usedSize;
474+
};
475+
470476
struct JFRConstantEvents {
471477
JVMInformationEntry JVMInfoEntry;
472478
CPUInformationEntry CPUInfoEntry;
@@ -563,6 +569,8 @@ class VM_JFRConstantPoolTypes {
563569
UDATA _threadDumpCount;
564570
J9Pool *_threadAllocationStatisticsTable;
565571
UDATA _threadAllocationStatisticsCount;
572+
J9Pool *_physicalMemoryTable;
573+
UDATA _physicalMemoryCount;
566574

567575
/* Periodic events. */
568576
bool _shouldWriteJVMInformation;
@@ -573,7 +581,6 @@ class VM_JFRConstantPoolTypes {
573581
bool _shouldWriteInitialEnvironmentVariableEvents;
574582
bool _shouldWritewriteGCHeapConfigurationEvent;
575583
bool _shouldWriteYoungGenerationConfigurationEvent;
576-
bool _shouldWritePhysicalMemory;
577584
bool _shouldWriteSystemProcess;
578585
bool _shouldWriteNativeLibrary;
579586
bool _shouldWriteModuleRequire;
@@ -881,6 +888,9 @@ class VM_JFRConstantPoolTypes {
881888
void addGCHeapSummaryEntry(J9JFRGCHeapSummary *gcHeapSummaryData);
882889

883890
void addNetworkUtilizationEntry(J9JFRNetworkUtilization *networkUtilizationData);
891+
892+
void addPhysicalMemoryEntry(J9JFRPhysicalMemory *physicalMemoryData);
893+
884894
void addDataLossEntry(J9JFRDataLoss *dataLossData);
885895

886896
void addThreadDumpEntry(J9JFRThreadDump *threadDumpData);
@@ -1274,6 +1284,16 @@ class VM_JFRConstantPoolTypes {
12741284
return _stackFrameCount;
12751285
}
12761286

1287+
UDATA getPhysicalMemoryCount()
1288+
{
1289+
return _physicalMemoryCount;
1290+
}
1291+
1292+
J9Pool *getPhysicalMemoryTable()
1293+
{
1294+
return _physicalMemoryTable;
1295+
}
1296+
12771297
bool shouldWriteJVMInformation()
12781298
{
12791299
return _shouldWriteJVMInformation;
@@ -1314,11 +1334,6 @@ class VM_JFRConstantPoolTypes {
13141334
return _shouldWriteYoungGenerationConfigurationEvent;
13151335
}
13161336

1317-
bool shouldWritePhysicalMemory()
1318-
{
1319-
return _shouldWritePhysicalMemory;
1320-
}
1321-
13221337
bool shouldWriteSystemProcess()
13231338
{
13241339
return _shouldWriteSystemProcess;
@@ -1448,7 +1463,7 @@ class VM_JFRConstantPoolTypes {
14481463
_shouldWriteYoungGenerationConfigurationEvent = true;
14491464
break;
14501465
case J9JFR_EVENT_TYPE_PHYSICAL_MEMORY:
1451-
_shouldWritePhysicalMemory = true;
1466+
addPhysicalMemoryEntry((J9JFRPhysicalMemory *)event);
14521467
break;
14531468
case J9JFR_EVENT_TYPE_SYSTEM_PROCESS:
14541469
_shouldWriteSystemProcess = true;
@@ -2232,6 +2247,8 @@ class VM_JFRConstantPoolTypes {
22322247
, _threadDumpCount(0)
22332248
, _threadAllocationStatisticsTable(NULL)
22342249
, _threadAllocationStatisticsCount(0)
2250+
, _physicalMemoryTable(NULL)
2251+
, _physicalMemoryCount(0)
22352252
, _shouldWriteJVMInformation(false)
22362253
, _shouldWriteCPUInformationEvent(false)
22372254
, _shouldWriteVirtualizationInformationEvent(false)
@@ -2240,7 +2257,6 @@ class VM_JFRConstantPoolTypes {
22402257
, _shouldWriteInitialEnvironmentVariableEvents(false)
22412258
, _shouldWritewriteGCHeapConfigurationEvent(false)
22422259
, _shouldWriteYoungGenerationConfigurationEvent(false)
2243-
, _shouldWritePhysicalMemory(false)
22442260
, _shouldWriteSystemProcess(false)
22452261
, _shouldWriteNativeLibrary(false)
22462262
, _shouldWriteModuleRequire(false)
@@ -2406,6 +2422,13 @@ class VM_JFRConstantPoolTypes {
24062422
goto done;
24072423
}
24082424

2425+
_physicalMemoryTable = pool_new(sizeof(PhysicalMemoryEntry), 0, sizeof(U_64), 0, J9_GET_CALLSITE(), OMRMEM_CATEGORY_VM, POOL_FOR_PORT(privatePortLibrary));
2426+
if (NULL == _physicalMemoryTable) {
2427+
_buildResult = OutOfMemory;
2428+
goto done;
2429+
}
2430+
2431+
24092432
_systemProcessTable = pool_new(sizeof(SystemProcessEntry), 0, sizeof(U_64), 0, J9_GET_CALLSITE(), OMRMEM_CATEGORY_VM, POOL_FOR_PORT(privatePortLibrary));
24102433
if (NULL == _systemProcessTable) {
24112434
_buildResult = OutOfMemory;
@@ -2585,6 +2608,7 @@ class VM_JFRConstantPoolTypes {
25852608
pool_kill(_dataLossTable);
25862609
pool_kill(_threadDumpTable);
25872610
pool_kill(_threadAllocationStatisticsTable);
2611+
pool_kill(_physicalMemoryTable);
25882612
freeNetworkInterfaceNames();
25892613
j9mem_free_memory(_globalStringTable);
25902614
}

runtime/vm/jfr.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,16 @@ jfrEventSize(J9JFREvent *jfrEvent)
197197
case J9JFR_EVENT_TYPE_INITIAL_ENVIRONMENT_VARIABLE:
198198
case J9JFR_EVENT_TYPE_GC_HEAP_CONFIGURATION:
199199
case J9JFR_EVENT_TYPE_YOUNG_GENERATION_CONFIGURATION:
200-
case J9JFR_EVENT_TYPE_PHYSICAL_MEMORY:
201200
case J9JFR_EVENT_TYPE_SYSTEM_PROCESS:
202201
case J9JFR_EVENT_TYPE_MODULE_REQUIRE:
203202
case J9JFR_EVENT_TYPE_MODULE_EXPORT:
204203
case J9JFR_EVENT_TYPE_CLASS_LOADER_STATISTICS:
205204
case J9JFR_EVENT_TYPE_NATIVE_LIBRARY:
206205
size = sizeof(J9JFREvent);
207206
break;
207+
case J9JFR_EVENT_TYPE_PHYSICAL_MEMORY:
208+
size = sizeof(J9JFRPhysicalMemory);
209+
break;
208210
default:
209211
Assert_VM_unreachable();
210212
break;
@@ -1621,6 +1623,24 @@ jfrThreadStatistics(J9VMThread *currentThread)
16211623

16221624
}
16231625

1626+
static void
1627+
jfrPhysicalMemory(J9VMThread *currentThread)
1628+
{
1629+
PORT_ACCESS_FROM_VMC(currentThread);
1630+
1631+
J9MemoryInfo memInfo = {0};
1632+
I_32 rc = j9sysinfo_get_memory_info(&memInfo);
1633+
1634+
if (0 == rc) {
1635+
J9JFRPhysicalMemory *jfrEvent = (J9JFRPhysicalMemory *)reserveBuffer(currentThread, currentThread, sizeof(J9JFRPhysicalMemory));
1636+
if (NULL != jfrEvent) {
1637+
initializeEventFields(currentThread, currentThread, (J9JFREvent *)jfrEvent, J9JFR_EVENT_TYPE_PHYSICAL_MEMORY);
1638+
jfrEvent->totalSize = memInfo.totalPhysical;
1639+
jfrEvent->usedSize = memInfo.totalPhysical - memInfo.availPhysical;
1640+
}
1641+
}
1642+
}
1643+
16241644
static int J9THREAD_PROC
16251645
jfrSamplingThreadProc(void *entryArg)
16261646
{
@@ -2352,10 +2372,7 @@ JfrPeriodicEventSet::requestClassLoaderStatistics(J9VMThread *currentThread)
23522372
void
23532373
JfrPeriodicEventSet::requestPhysicalMemory(J9VMThread *currentThread)
23542374
{
2355-
J9JFREvent *jfrEvent = (J9JFREvent *)reserveBuffer(currentThread, currentThread, sizeof(J9JFREvent));
2356-
if (NULL != jfrEvent) {
2357-
initializeEventFields(currentThread, currentThread, jfrEvent, J9JFR_EVENT_TYPE_PHYSICAL_MEMORY);
2358-
}
2375+
jfrPhysicalMemory(currentThread);
23592376
}
23602377

23612378
void

0 commit comments

Comments
 (0)