Path: blob/master/runtime/compiler/net/Message.cpp
6000 views
/*******************************************************************************1/*******************************************************************************2* Copyright (c) 2020, 2022 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/2223#include "net/Message.hpp"24#include "infra/Assert.hpp"25#include "env/VerboseLog.hpp"2627namespace JITServer28{29const char* const Message::DataDescriptor::_descriptorNames[] = {30"INT32",31"INT64",32"UINT32",33"UINT64",34"BOOL",35"STRING",36"OBJECT",37"ENUM",38"VECTOR",39"SIMPLE_VECTOR",40"EMPTY_VECTOR",41"TUPLE",42"INVALID"43};4445uint32_t46Message::addData(const DataDescriptor &desc, const void *dataStart, bool needs64BitAlignment)47{48// Write the descriptor itself49uint32_t descOffset = _buffer.writeValue(desc);5051// If the data following the descriptor needs to be 64-bit aligned,52// add some initial padding in the outgoing buffer and write the53// offset to the real payload into the descriptor54uint8_t initialPadding = 0;55if (needs64BitAlignment && !_buffer.is64BitAligned())56{57initialPadding = _buffer.alignCurrentPositionOn64Bit();58TR_ASSERT(initialPadding != 0, "Initial padding must be non zero because we checked alignment");59DataDescriptor *serializedDescriptor = _buffer.getValueAtOffset<DataDescriptor>(descOffset);60serializedDescriptor->addInitialPadding(initialPadding);61}6263// Write the real data and possibly some padding at the end64_buffer.writeData(dataStart, desc.getPayloadSize(), desc.getPaddingSize());65_descriptorOffsets.push_back(descOffset);66return desc.getTotalSize() + initialPadding;67}6869void70Message::deserialize()71{72// Assume that buffer is populated with data that defines a valid message73// Reconstruct the message by setting metadata and pointers to descriptors74// Note that the size of the entire message buffer has already been stripped75//76_buffer.readValue<MetaData>(); // This only advances curPtr in the MessageBuffer7778uint32_t numDataPoints = getMetaData()->_numDataPoints;7980_descriptorOffsets.reserve(numDataPoints);81// TODO: do I need to clear the vector of _descriptorOffsets just in case?82for (uint32_t i = 0; i < numDataPoints; ++i)83{84uint32_t descOffset = _buffer.readValue<DataDescriptor>(); // Read the descriptor itself85_descriptorOffsets.push_back(descOffset);8687// skip the data segment, which is processed in getArgs88_buffer.readData(getLastDescriptor()->getTotalSize());89}90}9192uint32_t93Message::DataDescriptor::print(uint32_t nestingLevel)94{95uint32_t numDescriptorsPrinted = 1;96TR_VerboseLog::write(TR_Vlog_JITServer, "");97for (uint32_t i = 0; i < nestingLevel; ++i)98TR_VerboseLog::write("\t");99TR_VerboseLog::writeLine("DataDescriptor[%p]: type=%d(%6s) payload_size=%u dataOffset=%u, padding=%u",100this, getDataType(), _descriptorNames[getDataType()], getPayloadSize(), getDataOffset(), getPaddingSize());101if (!isPrimitive())102{103TR_VerboseLog::writeLine(TR_Vlog_JITServer, "DataDescriptor[%p]: nested data begin", this);104DataDescriptor *curDesc = static_cast<DataDescriptor *>(getDataStart());105while ((char *) curDesc->getDataStart() + curDesc->getTotalSize() - (char *) getDataStart() <= getTotalSize())106{107numDescriptorsPrinted += curDesc->print(nestingLevel + 1);108curDesc = curDesc->getNextDescriptor();109}110TR_VerboseLog::writeLine(TR_Vlog_JITServer, "DataDescriptor[%p] nested data end", this);111}112113return numDescriptorsPrinted;114}115116void117Message::print()118{119const MetaData *metaData = getMetaData();120TR_VerboseLog::CriticalSection vlogLock;121TR_VerboseLog::writeLine(TR_Vlog_JITServer, "Message: type=%d numDataPoints=%u version=%lu numDescriptors=%lu\n",122metaData->_type, metaData->_numDataPoints, metaData->_version, _descriptorOffsets.size());123uint32_t numDescriptorsPrinted = 0;124for (uint32_t i = 0; i < _descriptorOffsets.size(); i += numDescriptorsPrinted)125{126DataDescriptor* desc = getDescriptor(i);127numDescriptorsPrinted = desc->print(0);128}129}130};131132133