Path: blob/master/src/hotspot/share/jfr/writers/jfrStreamWriterHost.inline.hpp
64442 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP25#define SHARE_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP2627#include "jfr/jni/jfrJavaSupport.hpp"28#include "jfr/writers/jfrStreamWriterHost.hpp"29#include "runtime/os.hpp"3031template <typename Adapter, typename AP>32StreamWriterHost<Adapter, AP>::StreamWriterHost(typename Adapter::StorageType* storage, Thread* thread) :33MemoryWriterHost<Adapter, AP>(storage, thread), _stream_pos(0), _fd(invalid_fd) {34}3536template <typename Adapter, typename AP>37StreamWriterHost<Adapter, AP>::StreamWriterHost(typename Adapter::StorageType* storage, size_t size) :38MemoryWriterHost<Adapter, AP>(storage, size), _stream_pos(0), _fd(invalid_fd) {39}4041template <typename Adapter, typename AP>42StreamWriterHost<Adapter, AP>::StreamWriterHost(Thread* thread) :43MemoryWriterHost<Adapter, AP>(thread), _stream_pos(0), _fd(invalid_fd) {44}4546template <typename Adapter, typename AP>47inline int64_t StreamWriterHost<Adapter, AP>::current_stream_position() const {48return this->used_offset() + _stream_pos;49}5051template <typename Adapter, typename AP>52inline bool StreamWriterHost<Adapter, AP>::accommodate(size_t used, size_t requested) {53if (used > 0) {54this->flush(used);55}56assert(this->used_size() == 0, "invariant");57if (this->available_size() >= requested) {58return true;59}60return StorageHost<Adapter, AP>::accommodate(0, requested);61}6263template <typename Adapter, typename AP>64inline void StreamWriterHost<Adapter, AP>::write_bytes(void* dest, const void* buf, intptr_t len) {65assert(len >= 0, "invariant");66if (len > (intptr_t)this->available_size()) {67this->write_unbuffered(buf, len);68return;69}70MemoryWriterHost<Adapter, AP>::write_bytes(dest, buf, len);71}7273template <typename Adapter, typename AP>74inline void StreamWriterHost<Adapter, AP>::write_bytes(const u1* buf, intptr_t len) {75assert(len >= 0, "invariant");76while (len > 0) {77const unsigned int nBytes = len > INT_MAX ? INT_MAX : (unsigned int)len;78const ssize_t num_written = (ssize_t)os::write(_fd, buf, nBytes);79if (errno == ENOSPC) {80JfrJavaSupport::abort("Failed to write to jfr stream because no space left on device", false);81}82guarantee(num_written > 0, "Nothing got written, or os::write() failed");83_stream_pos += num_written;84len -= num_written;85buf += num_written;86}87}8889template <typename Adapter, typename AP>90inline void StreamWriterHost<Adapter, AP>::flush(size_t size) {91assert(size > 0, "invariant");92assert(this->is_valid(), "invariant");93this->write_bytes(this->start_pos(), (intptr_t)size);94StorageHost<Adapter, AP>::reset();95assert(0 == this->used_offset(), "invariant");96}9798template <typename Adapter, typename AP>99inline bool StreamWriterHost<Adapter, AP>::has_valid_fd() const {100return invalid_fd != _fd;101}102103template <typename Adapter, typename AP>104inline int64_t StreamWriterHost<Adapter, AP>::current_offset() const {105return current_stream_position();106}107108template <typename Adapter, typename AP>109void StreamWriterHost<Adapter, AP>::seek(int64_t offset) {110this->flush();111assert(0 == this->used_offset(), "can only seek from beginning");112_stream_pos = os::seek_to_file_offset(_fd, offset);113}114115template <typename Adapter, typename AP>116void StreamWriterHost<Adapter, AP>::flush() {117if (this->is_valid()) {118const size_t used = this->used_size();119if (used > 0) {120this->flush(used);121}122}123}124125template <typename Adapter, typename AP>126void StreamWriterHost<Adapter, AP>::write_unbuffered(const void* buf, intptr_t len) {127this->flush();128assert(0 == this->used_offset(), "can only seek from beginning");129this->write_bytes((const u1*)buf, len);130}131132template <typename Adapter, typename AP>133inline bool StreamWriterHost<Adapter, AP>::is_valid() const {134return has_valid_fd();135}136137template <typename Adapter, typename AP>138inline void StreamWriterHost<Adapter, AP>::close_fd() {139assert(this->has_valid_fd(), "closing invalid fd!");140os::close(_fd);141_fd = invalid_fd;142}143144template <typename Adapter, typename AP>145inline void StreamWriterHost<Adapter, AP>::reset(fio_fd fd) {146assert(!this->has_valid_fd(), "invariant");147_fd = fd;148_stream_pos = 0;149this->hard_reset();150}151152#endif // SHARE_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP153154155