Path: blob/master/src/hotspot/share/jfr/writers/jfrStreamWriterHost.inline.hpp
41149 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/writers/jfrStreamWriterHost.hpp"2829#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);79guarantee(num_written > 0, "Nothing got written, or os::write() failed");80_stream_pos += num_written;81len -= num_written;82buf += num_written;83}84}8586template <typename Adapter, typename AP>87inline void StreamWriterHost<Adapter, AP>::flush(size_t size) {88assert(size > 0, "invariant");89assert(this->is_valid(), "invariant");90this->write_bytes(this->start_pos(), (intptr_t)size);91StorageHost<Adapter, AP>::reset();92assert(0 == this->used_offset(), "invariant");93}9495template <typename Adapter, typename AP>96inline bool StreamWriterHost<Adapter, AP>::has_valid_fd() const {97return invalid_fd != _fd;98}99100template <typename Adapter, typename AP>101inline int64_t StreamWriterHost<Adapter, AP>::current_offset() const {102return current_stream_position();103}104105template <typename Adapter, typename AP>106void StreamWriterHost<Adapter, AP>::seek(int64_t offset) {107this->flush();108assert(0 == this->used_offset(), "can only seek from beginning");109_stream_pos = os::seek_to_file_offset(_fd, offset);110}111112template <typename Adapter, typename AP>113void StreamWriterHost<Adapter, AP>::flush() {114if (this->is_valid()) {115const size_t used = this->used_size();116if (used > 0) {117this->flush(used);118}119}120}121122template <typename Adapter, typename AP>123void StreamWriterHost<Adapter, AP>::write_unbuffered(const void* buf, intptr_t len) {124this->flush();125assert(0 == this->used_offset(), "can only seek from beginning");126this->write_bytes((const u1*)buf, len);127}128129template <typename Adapter, typename AP>130inline bool StreamWriterHost<Adapter, AP>::is_valid() const {131return has_valid_fd();132}133134template <typename Adapter, typename AP>135inline void StreamWriterHost<Adapter, AP>::close_fd() {136assert(this->has_valid_fd(), "closing invalid fd!");137os::close(_fd);138_fd = invalid_fd;139}140141template <typename Adapter, typename AP>142inline void StreamWriterHost<Adapter, AP>::reset(fio_fd fd) {143assert(!this->has_valid_fd(), "invariant");144_fd = fd;145_stream_pos = 0;146this->hard_reset();147}148149#endif // SHARE_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP150151152