Skip to content

Commit

Permalink
Merge pull request #1589 from dsouzai/aotDebugCounter
Browse files Browse the repository at this point in the history
Relocatable Debug Counters
  • Loading branch information
mstoodle authored Jul 5, 2018
2 parents 5b1eda6 + 7efa689 commit b5ae0fd
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 2 deletions.
30 changes: 30 additions & 0 deletions runtime/compiler/arm/codegen/J9AheadOfTimeCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,35 @@ uint8_t *J9::ARM::AheadOfTimeCompile::initializeAOTRelocationHeader(TR::Iterated
}
break;

case TR_DebugCounter:
{
TR::DebugCounterBase *counter = (TR::DebugCounterBase *) relocation->getTargetAddress();
if (!counter || !counter->getReloData() || !counter->getName())
comp->failCompilation<TR::CompilationException>("Failed to generate debug counter relo data");

TR::DebugCounterReloData *counterReloData = counter->getReloData();

uintptrj_t offset = (uintptrj_t)fej9->sharedCache()->rememberDebugCounterName(counter->getName());

uint8_t flags = (uint8_t)counterReloData->_seqKind;
TR_ASSERT((flags & RELOCATION_CROSS_PLATFORM_FLAGS_MASK) == 0, "reloFlags bits overlap cross-platform flags bits\n");
*flagsCursor |= (flags & RELOCATION_RELOC_FLAGS_MASK);

*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_callerIndex;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_bytecodeIndex;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = offset;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_delta;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_fidelity;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_staticDelta;
cursor += SIZEPOINTER;
}
break;

}
return cursor;
}
Expand Down Expand Up @@ -717,6 +746,7 @@ uint32_t J9::ARM::AheadOfTimeCompile::_relocationTargetTypeToHeaderSizeMap[TR_Nu
0, // TR_NativeMethodAbsolute = 56,
0, // TR_NativeMethodRelative = 57,
16, // TR_ArbitraryClassAddress = 58,
28 // TR_DebugCounter = 59
};


Expand Down
35 changes: 35 additions & 0 deletions runtime/compiler/codegen/J9AheadOfTimeCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,41 @@ J9::AheadOfTimeCompile::dumpRelocationData()
}
}
break;
case TR_DebugCounter:
cursor ++;
if (is64BitTarget)
{
cursor += 4; // padding
ep1 = cursor; // inlinedSiteIndex
ep2 = cursor+8; // bcIndex
ep3 = cursor+16; // offsetOfNameString
ep4 = cursor+24; // delta
ep5 = cursor+40; // staticDelta
cursor += 48;
self()->traceRelocationOffsets(cursor, offsetSize, endOfCurrentRecord, orderedPair);
if (isVerbose)
{
traceMsg(self()->comp(), "\n Debug Counter: Inlined site index = %d, bcIndex = %d, offsetOfNameString = %p, delta = %d, staticDelta = %d",
*(int64_t *)ep1, *(int32_t *)ep2, *(UDATA *)ep3, *(int32_t *)ep4, *(int32_t *)ep5);
}
}
else
{
ep1 = cursor; // inlinedSiteIndex
ep2 = cursor+4; // bcIndex
ep3 = cursor+8; // offsetOfNameString
ep4 = cursor+12; // delta
ep5 = cursor+20; // staticDelta
cursor += 24;
self()->traceRelocationOffsets(cursor, offsetSize, endOfCurrentRecord, orderedPair);
if (isVerbose)
{
traceMsg(self()->comp(), "\n Debug Counter: Inlined site index = %d, bcIndex = %d, offsetOfNameString = %p, delta = %d, staticDelta = %d",
*(int32_t *)ep1, *(int32_t *)ep2, *(UDATA *)ep3, *(int32_t *)ep4, *(int32_t *)ep5);
}
}
break;

default:
traceMsg(self()->comp(), "Unknown Relocation type = %d\n", kind);
TR_ASSERT(false, "should be unreachable");
Expand Down
33 changes: 33 additions & 0 deletions runtime/compiler/env/J9SharedCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,39 @@ TR_J9SharedCache::rememberClass(J9Class *clazz, bool create)
return chainData;
}

UDATA
TR_J9SharedCache::rememberDebugCounterName(const char *name)
{
TR_J9VMBase *fej9 = (TR_J9VMBase *)(fe());
J9VMThread *vmThread = fej9->getCurrentVMThread();

J9SharedDataDescriptor dataDescriptor;
dataDescriptor.address = (U_8*)name;
dataDescriptor.length = (strlen(name) + 1); // +1 for the \0 terminator
dataDescriptor.type = J9SHR_DATA_TYPE_JITHINT;
dataDescriptor.flags = J9SHRDATA_NOT_INDEXED;

const U_8 *data = sharedCacheConfig()->storeSharedData(vmThread,
(const char*)NULL,
0,
&dataDescriptor);

UDATA offset = data ? (UDATA)offsetInSharedCacheFromPointer((void *)data) : (UDATA)-1;

//printf("\nrememberDebugCounterName: Tried to store %s (%p), data=%p, offset=%p\n", name, name, data, offset);

return offset;
}

const char *
TR_J9SharedCache::getDebugCounterName(UDATA offset)
{
const char *name = (offset != (UDATA)-1) ? (const char *)pointerFromOffsetInSharedCache((void *)offset) : NULL;

//printf("\ngetDebugCounterName: Tried to find %p, name=%s (%p)\n", offset, (name ? name : ""), name);

return name;
}

uint32_t
TR_J9SharedCache::numInterfacesImplemented(J9Class *clazz)
Expand Down
3 changes: 3 additions & 0 deletions runtime/compiler/env/J9SharedCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class TR_J9SharedCache : public TR_SharedCache

UDATA *rememberClass(J9Class *clazz, bool create=true);

UDATA rememberDebugCounterName(const char *name);
const char *getDebugCounterName(UDATA offset);

bool classMatchesCachedVersion(J9Class *clazz, UDATA *chainData=NULL);
bool classMatchesCachedVersion(TR_OpaqueClassBlock *classPtr, UDATA *chainData=NULL)
{
Expand Down
32 changes: 32 additions & 0 deletions runtime/compiler/p/codegen/J9AheadOfTimeCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "il/Node_inlines.hpp"
#include "il/SymbolReference.hpp"
#include "il/symbol/LabelSymbol.hpp"
#include "ras/DebugCounter.hpp"

J9::Power::AheadOfTimeCompile::AheadOfTimeCompile(TR::CodeGenerator *cg) :
J9::AheadOfTimeCompile(_relocationTargetTypeToHeaderSizeMap, cg->comp()),
Expand Down Expand Up @@ -801,6 +802,35 @@ uint8_t *J9::Power::AheadOfTimeCompile::initializeAOTRelocationHeader(TR::Iterat
}
break;

case TR_DebugCounter:
{
TR::DebugCounterBase *counter = (TR::DebugCounterBase *) relocation->getTargetAddress();
if (!counter || !counter->getReloData() || !counter->getName())
comp->failCompilation<TR::CompilationException>("Failed to generate debug counter relo data");

TR::DebugCounterReloData *counterReloData = counter->getReloData();

uintptrj_t offset = (uintptrj_t)fej9->sharedCache()->rememberDebugCounterName(counter->getName());

uint8_t flags = (uint8_t)counterReloData->_seqKind;
TR_ASSERT((flags & RELOCATION_CROSS_PLATFORM_FLAGS_MASK) == 0, "reloFlags bits overlap cross-platform flags bits\n");
*flagsCursor |= (flags & RELOCATION_RELOC_FLAGS_MASK);

*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_callerIndex;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_bytecodeIndex;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = offset;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_delta;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_fidelity;
cursor += SIZEPOINTER;
*(uintptrj_t *)cursor = (uintptrj_t)counterReloData->_staticDelta;
cursor += SIZEPOINTER;
}
break;

}
return cursor;
}
Expand Down Expand Up @@ -867,6 +897,7 @@ uint32_t J9::Power::AheadOfTimeCompile::_relocationTargetTypeToHeaderSizeMap[TR_
0, // TR_NativeMethodAbsolute = 56,
0, // TR_NativeMethodRelative = 57,
32, // TR_ArbitraryClassAddress = 58,
56, // TR_DebugCounter = 59
};
#else
uint32_t J9::Power::AheadOfTimeCompile::_relocationTargetTypeToHeaderSizeMap[TR_NumExternalRelocationKinds] =
Expand Down Expand Up @@ -930,6 +961,7 @@ uint32_t J9::Power::AheadOfTimeCompile::_relocationTargetTypeToHeaderSizeMap[TR_
0, // TR_NativeMethodAbsolute = 56,
0, // TR_NativeMethodRelative = 57,
16, // TR_ArbitraryClassAddress = 58,
28 // TR_DebugCounter = 59
};

#endif
Expand Down
1 change: 1 addition & 0 deletions runtime/compiler/p/runtime/PPCRelocationTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ TR_PPC32RelocationTarget::isOrderedPairRelocation(TR_RelocationRecord *reloRecor
case TR_RamMethodSequence:
case TR_BodyInfoAddressLoad:
case TR_DataAddress:
case TR_DebugCounter:
return true;
}

Expand Down
138 changes: 138 additions & 0 deletions runtime/compiler/runtime/RelocationRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ struct TR_RelocationRecordEmitClassBinaryTemplate : public TR_RelocationRecordWi
#endif
};

struct TR_RelocationRecordDebugCounterBinaryTemplate : public TR_RelocationRecordWithInlinedSiteIndexBinaryTemplate
{
UDATA _bcIndex;
UDATA _offsetOfNameString;
UDATA _delta;
UDATA _fidelity;
UDATA _staticDelta;
};


// TR_RelocationRecordGroup

Expand Down Expand Up @@ -452,6 +461,9 @@ TR_RelocationRecord::create(TR_RelocationRecord *storage, TR_RelocationRuntime *
case TR_EmitClass:
reloRecord = new (storage) TR_RelocationRecordEmitClass(reloRuntime, record);
break;
case TR_DebugCounter:
reloRecord = new (storage) TR_RelocationRecordDebugCounter(reloRuntime, record);
break;
default:
// TODO: error condition
printf("Unexpected relo record: %d\n", reloType);fflush(stdout);
Expand Down Expand Up @@ -3395,3 +3407,129 @@ TR_RelocationRecordEmitClass::applyRelocation(TR_RelocationRuntime *reloRuntime,
reloRuntime->addClazzRecord(reloLocation, reloPrivateData->_bcIndex, reloPrivateData->_method);
return 0;
}


char *
TR_RelocationRecordDebugCounter::name()
{
return "TR_RelocationRecordDebugCounter";
}

int32_t
TR_RelocationRecordDebugCounter::bytesInHeaderAndPayload()
{
return sizeof(TR_RelocationRecordDebugCounterBinaryTemplate);
}

void
TR_RelocationRecordDebugCounter::preparePrivateData(TR_RelocationRuntime *reloRuntime, TR_RelocationTarget *reloTarget)
{
TR_RelocationRecordDebugCounterPrivateData *reloPrivateData = &(privateData()->debugCounter);

IDATA callerIndex = (IDATA)inlinedSiteIndex(reloTarget);
if (callerIndex != -1)
{
reloPrivateData->_method = getInlinedSiteMethod(reloRuntime, callerIndex);
}
else
{
reloPrivateData->_method = NULL;
}

reloPrivateData->_bcIndex = reloTarget->loadSigned32b((uint8_t *) &(((TR_RelocationRecordDebugCounterBinaryTemplate *)_record)->_bcIndex));
reloPrivateData->_delta = reloTarget->loadSigned32b((uint8_t *) &(((TR_RelocationRecordDebugCounterBinaryTemplate *)_record)->_delta));
reloPrivateData->_fidelity = reloTarget->loadUnsigned8b((uint8_t *) &(((TR_RelocationRecordDebugCounterBinaryTemplate *)_record)->_fidelity));
reloPrivateData->_staticDelta = reloTarget->loadSigned32b((uint8_t *) &(((TR_RelocationRecordDebugCounterBinaryTemplate *)_record)->_staticDelta));

UDATA offset = (UDATA)reloTarget->loadPointer((uint8_t *) &(((TR_RelocationRecordDebugCounterBinaryTemplate *)_record)->_offsetOfNameString));
reloPrivateData->_name = reloRuntime->fej9()->sharedCache()->getDebugCounterName(offset);
}

int32_t
TR_RelocationRecordDebugCounter::applyRelocation(TR_RelocationRuntime *reloRuntime, TR_RelocationTarget *reloTarget, uint8_t *reloLocation)
{
TR::DebugCounterBase *counter = findOrCreateCounter(reloRuntime);
if (counter == NULL)
{
/*
* We don't have to return -1 here and fail the relocation. We can always just allocate some memory
* and patch the update location to that. However, given that it's likely that the developer wishes
* to have debug counters run, it's probably better to fail the relocation.
*
*/
return -1;
}

// Update Counter Location
reloTarget->storeAddressSequence((uint8_t *)counter->getBumpCountAddress(), reloLocation, reloFlags(reloTarget));

return 0;
}

int32_t
TR_RelocationRecordDebugCounter::applyRelocation(TR_RelocationRuntime *reloRuntime, TR_RelocationTarget *reloTarget, uint8_t *reloLocationHigh, uint8_t *reloLocationLow)
{
TR::DebugCounterBase *counter = findOrCreateCounter(reloRuntime);
if (counter == NULL)
{
/*
* We don't have to return -1 here and fail the relocation. We can always just allocate some memory
* and patch the update location to that. However, given that it's likely that the developer wishes
* to have debug counters run, it's probably better to fail the relocation.
*
*/
return -1;
}

// Update Counter Location
reloTarget->storeAddress((uint8_t *)counter->getBumpCountAddress(), reloLocationHigh, reloLocationLow, reloFlags(reloTarget));

return 0;
}

TR::DebugCounterBase *
TR_RelocationRecordDebugCounter::findOrCreateCounter(TR_RelocationRuntime *reloRuntime)
{
TR::DebugCounterBase *counter = NULL;
TR_RelocationRecordDebugCounterPrivateData *reloPrivateData = &(privateData()->debugCounter);
TR::Compilation *comp = reloRuntime->comp();
bool isAggregateCounter = reloPrivateData->_delta == 0 ? false : true;

if (reloPrivateData->_name == NULL ||
(isAggregateCounter && reloPrivateData->_method == (TR_OpaqueMethodBlock *)-1))
{
return NULL;
}

// Find or Create Debug Counter
if (isAggregateCounter)
{
counter = comp->getPersistentInfo()->getDynamicCounters()->findAggregation(reloPrivateData->_name, strlen(reloPrivateData->_name));
if (!counter)
{
TR::DebugCounterAggregation *aggregatedCounters = comp->getPersistentInfo()->getDynamicCounters()->createAggregation(comp, reloPrivateData->_name);
if (aggregatedCounters)
{
aggregatedCounters->aggregateStandardCounters(comp,
reloPrivateData->_method,
reloPrivateData->_bcIndex,
reloPrivateData->_name,
reloPrivateData->_delta,
reloPrivateData->_fidelity,
reloPrivateData->_staticDelta);
if (!aggregatedCounters->hasAnyCounters())
return NULL;
}
counter = aggregatedCounters;
}
}
else
{
counter = TR::DebugCounter::getDebugCounter(comp,
reloPrivateData->_name,
reloPrivateData->_fidelity,
reloPrivateData->_staticDelta);
}

return counter;
}
Loading

0 comments on commit b5ae0fd

Please sign in to comment.