Skip to content

Commit

Permalink
Merge pull request #19741 from tajila/jfr
Browse files Browse the repository at this point in the history
JFR Writer part 2
  • Loading branch information
gacholio authored Jul 9, 2024
2 parents 69c0709 + 4be3c05 commit 4aa1e64
Show file tree
Hide file tree
Showing 12 changed files with 3,573 additions and 38 deletions.
23 changes: 23 additions & 0 deletions runtime/oti/vm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,29 @@ internalExceptionDescribe(J9VMThread *vmThread);
UDATA
iterateStackTrace(J9VMThread * vmThread, j9object_t* exception, UDATA (*callback) (J9VMThread * vmThread, void * userData, UDATA bytecodeOffset, J9ROMClass * romClass, J9ROMMethod * romMethod, J9UTF8 * fileName, UDATA lineNumber, J9ClassLoader* classLoader, J9Class* ramClass), void * userData, UDATA pruneConstructors, UDATA skipHiddenFrames);

/**
* @brief
* @param vmThread
* @param exception
* @param vmThread
* @param userData
* @param bytecodeOffset
* @param romClass
* @param romMethod
* @param fileName
* @param lineNumber
* @param classLoader
* @param ramClass)
* @param userData
* @param pruneConstructors
* @param skipHiddenFrames
* @param sizeOfWalkstateCache
* @param exceptionIsJavaObject
* @return UDATA
*/
UDATA
iterateStackTraceImpl(J9VMThread * vmThread, j9object_t* exception, UDATA (*callback) (J9VMThread * vmThread, void * userData, UDATA bytecodeOffset, J9ROMClass * romClass, J9ROMMethod * romMethod, J9UTF8 * fileName, UDATA lineNumber, J9ClassLoader* classLoader, J9Class* ramClass), void * userData, UDATA pruneConstructors, UDATA skipHiddenFrames, UDATA sizeOfWalkstateCache, BOOLEAN exceptionIsJavaObject);


/* ---------------- exceptionsupport.c ---------------- */

Expand Down
344 changes: 344 additions & 0 deletions runtime/vm/BufferWriter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
/*******************************************************************************
* Copyright IBM Corp. and others 2024
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/

#if !defined(BUFFERWRITER_HPP_)
#define BUFFERWRITER_HPP_

#include "j9cfg.h"
#include "j9.h"

class VM_BufferWriter {
/*
* Data members
*/
private:
U_8* _buffer;
U_8* _cursor;
UDATA _size;

U_8* _maxCursor;

#if defined(J9VM_ENV_LITTLE_ENDIAN)
static const bool _isLE = true;
#else
static const bool _isLE = false;
#endif

protected:

public:

/*
* Function members
*/
private:
static VMINLINE U_16
byteSwap(U_16 val)
{
U_16 swapped = ((val & 0xFF00) >> 8);
swapped |= ((val & 0x00FF) << 8);

return swapped;
}

static VMINLINE U_32
byteSwap(U_32 val)
{
U_32 swapped = ((val & 0xFF000000) >> 24);
swapped |= ((val & 0x00FF0000) >> 8);
swapped |= ((val & 0x0000FF00) << 8);
swapped |= ((val & 0x000000FF) << 24);

return swapped;
}

static VMINLINE U_64
byteSwap(U_64 val)
{
U_64 swapped = ((val & 0xFF00000000000000) >> 56);
swapped |= ((val & 0x00FF000000000000) >> 40);
swapped |= ((val & 0x0000FF0000000000) >> 24);
swapped |= ((val & 0x000000FF00000000) >> 8);
swapped |= ((val & 0x00000000FF000000) << 8);
swapped |= ((val & 0x0000000000FF0000) << 24);
swapped |= ((val & 0x000000000000FF00) << 40);
swapped |= ((val & 0x00000000000000FF) << 56);

return swapped;
}

protected:

public:

VM_BufferWriter(U_8 *buffer, UDATA size)
: _buffer(buffer)
, _cursor(buffer)
, _size(size)
{
_maxCursor = NULL;
}

U_64
getFileOffset(U_8* bufferOffset, U_8* from)
{
return (U_64)((UDATA)bufferOffset - (UDATA)from);
}

U_64
getFileOffsetFromStart(U_8* bufferOffset)
{
return getFileOffset(bufferOffset, _buffer);
}

U_8*
getBufferStart()
{
return _buffer;
}

UDATA
getSize()
{
return getMaxCursor() - _buffer;
}

void
writeU8(U_8 val)
{
*_cursor = val;
_cursor += sizeof(U_8);
}

void
writeU16(U_16 val)
{
U_16 newVal = val;
if (_isLE) {
newVal = byteSwap(val);
}
*(U_16*)_cursor = newVal;
_cursor += sizeof(U_16);
}

void
writeU32(U_32 val)
{
U_32 newVal = val;
if (_isLE) {
newVal = byteSwap(val);
}
*(U_32*)_cursor = newVal;
_cursor += sizeof(U_32);
}

void
writeU64(U_64 val)
{
U_64 newVal = val;
if (_isLE) {
newVal = byteSwap(val);
}
*(U_64*)_cursor = newVal;
_cursor += sizeof(U_64);
}

void
writeData(U_8 *data, UDATA size)
{
memcpy(_cursor, data, size);
_cursor += size;
}

U_8*
getAndIncCursor(UDATA size)
{
U_8 *old = _cursor;
_cursor += size;

return old;
}

U_8*
getCursor()
{
return _cursor;
}
U_8*
getMaxCursor()
{
if ((UDATA)_cursor > (UDATA)_maxCursor) {
_maxCursor = _cursor;
}
return _maxCursor;
}

void
setCursor(U_8* cursor)
{
getMaxCursor();
_cursor = cursor;
}

void
writeLEB128(U_64 val)
{
U_64 newVal = val;
if (!_isLE) {
newVal = byteSwap(val);
}
do {
U_8 byte = newVal & 0x7F;
newVal >>= 7;

if (newVal > 0) {
byte |= 0x80;
}
writeU8(byte);
} while(newVal > 0);
}

void
writeLEB128PaddedU64(U_8* cursor, U_64 val)
{
U_8* old = _cursor;
_cursor = cursor;
writeLEB128PaddedU64(val);
_cursor = old;
}

void
writeLEB128PaddedU64(U_64 val)
{
U_64 newVal = val;
if (!_isLE) {
newVal = byteSwap(val);
}
writeU8((newVal & 0x7F) | 0x80);
writeU8(((newVal >> 7) & 0x7F) | 0x80);
writeU8(((newVal >> 14) & 0x7F) | 0x80);
writeU8(((newVal >> 21) & 0x7F) | 0x80);
writeU8(((newVal >> 28) & 0x7F) | 0x80);
writeU8(((newVal >> 35) & 0x7F) | 0x80);
writeU8(((newVal >> 42) & 0x7F) | 0x80);
writeU8(((newVal >> 49) & 0x7F));
}

void
writeLEB128PaddedU32(U_8* cursor, U_32 val)
{
U_8* old = _cursor;
_cursor = cursor;
writeLEB128PaddedU32(val);
_cursor = old;
}

void
writeLEB128PaddedU32(U_32 val)
{
U_64 newVal = val;
if (!_isLE) {
newVal = byteSwap(val);
}
writeU8((newVal & 0x7F) | 0x80);
writeU8(((newVal >> 7) & 0x7F) | 0x80);
writeU8(((newVal >> 14) & 0x7F) | 0x80);
writeU8(((newVal >> 21) & 0x7F));
}

static U_32
convertFromLEB128ToU32(U_8* start)
{
U_32 val = 0;

val = *start & 0x7F;

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (*start & 0X7F) << 7;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (*start & 0X7F) << 14;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (*start & 0X7F) << 21;
}
if (!_isLE) {
val = byteSwap(val);
}
return val;
}

static U_64
convertFromLEB128ToU64(U_8* start)
{
U_64 val = 0;

val = *start & 0x7F;

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 7;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 14;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 21;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 28;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 35;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 42;
}

if (J9_ARE_ALL_BITS_SET(*start, 0x80)) {
start++;
val |= (U_64)(*start & 0X7F) << 59;
}
if (!_isLE) {
val = byteSwap(val);
}
return val;
}

};

#endif /* BUFFERWRITER_HPP_ */
2 changes: 2 additions & 0 deletions runtime/vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ endif()
if(J9VM_OPT_JFR)
list(APPEND main_sources
jfr.cpp
JFRConstantPoolTypes.cpp
JFRChunkWriter.cpp
)
endif()

Expand Down
Loading

0 comments on commit 4aa1e64

Please sign in to comment.