apetushkov@9858: /* apetushkov@9858: * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #ifndef SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP apetushkov@9858: #define SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP apetushkov@9858: apetushkov@9858: #include "jfr/writers/jfrStreamWriterHost.hpp" apetushkov@9858: #include "runtime/os.hpp" apetushkov@9858: apetushkov@9858: template apetushkov@9858: StreamWriterHost::StreamWriterHost(typename Adapter::StorageType* storage, Thread* thread) : apetushkov@9858: MemoryWriterHost(storage, thread), _stream_pos(0), _fd(invalid_fd) { apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: StreamWriterHost::StreamWriterHost(typename Adapter::StorageType* storage, size_t size) : apetushkov@9858: MemoryWriterHost(storage, size), _stream_pos(0), _fd(invalid_fd) { apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: StreamWriterHost::StreamWriterHost(Thread* thread) : apetushkov@9858: MemoryWriterHost(thread), _stream_pos(0), _fd(invalid_fd) { apetushkov@9858: } apetushkov@9858: apetushkov@9858: template bulasevich@9947: inline int64_t StreamWriterHost::current_stream_position() const { apetushkov@9858: return this->used_offset() + _stream_pos; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline bool StreamWriterHost::accommodate(size_t used, size_t requested) { apetushkov@9858: if (used > 0) { apetushkov@9858: this->flush(used); apetushkov@9858: } apetushkov@9858: assert(this->used_size() == 0, "invariant"); apetushkov@9858: if (this->available_size() >= requested) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: return StorageHost::accommodate(0, requested); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline void StreamWriterHost::bytes(void* dest, const void* buf, size_t len) { apetushkov@9858: if (len > this->available_size()) { apetushkov@9858: this->write_unbuffered(buf, len); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: MemoryWriterHost::bytes(dest, buf, len); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline void StreamWriterHost::flush(size_t size) { apetushkov@9858: assert(size > 0, "invariant"); apetushkov@9858: assert(this->is_valid(), "invariant"); bulasevich@9947: _stream_pos += os::write(_fd, this->start_pos(), (unsigned int)size); apetushkov@9858: StorageHost::reset(); apetushkov@9858: assert(0 == this->used_offset(), "invariant"); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline bool StreamWriterHost::has_valid_fd() const { apetushkov@9858: return invalid_fd != _fd; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template bulasevich@9947: inline int64_t StreamWriterHost::current_offset() const { apetushkov@9858: return current_stream_position(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template bulasevich@9947: void StreamWriterHost::seek(int64_t offset) { apetushkov@9858: this->flush(); apetushkov@9858: assert(0 == this->used_offset(), "can only seek from beginning"); apetushkov@9858: _stream_pos = os::seek_to_file_offset(_fd, offset); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: void StreamWriterHost::flush() { apetushkov@9858: if (this->is_valid()) { apetushkov@9858: const size_t used = this->used_size(); apetushkov@9858: if (used > 0) { apetushkov@9858: this->flush(used); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: void StreamWriterHost::write_unbuffered(const void* buf, size_t len) { apetushkov@9858: this->flush(); apetushkov@9858: assert(0 == this->used_offset(), "can only seek from beginning"); apetushkov@9858: while (len > 0) { bulasevich@9947: const unsigned int n = MIN2((unsigned int)len, (unsigned int)INT_MAX); apetushkov@9858: _stream_pos += os::write(_fd, buf, n); apetushkov@9858: len -= n; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline bool StreamWriterHost::is_valid() const { apetushkov@9858: return has_valid_fd(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline void StreamWriterHost::close_fd() { apetushkov@9858: assert(this->has_valid_fd(), "closing invalid fd!"); apetushkov@9858: os::close(_fd); apetushkov@9858: _fd = invalid_fd; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: inline void StreamWriterHost::reset(fio_fd fd) { apetushkov@9858: assert(!this->has_valid_fd(), "invariant"); apetushkov@9858: _fd = fd; apetushkov@9858: _stream_pos = 0; apetushkov@9858: this->hard_reset(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: #endif // SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP