Path: blob/master/src/hotspot/share/services/heapDumperCompression.hpp
41144 views
/*1* Copyright (c) 2020 SAP SE. 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_SERVICES_HEAPDUMPERCOMPRESSION_HPP25#define SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP2627#include "memory/allocation.hpp"282930// Interface for a compression implementation.31class AbstractCompressor : public CHeapObj<mtInternal> {32public:33virtual ~AbstractCompressor() { }3435// Initializes the compressor. Returns a static error message in case of an error.36// Otherwise initializes the needed out and tmp size for the given block size.37virtual char const* init(size_t block_size, size_t* needed_out_size,38size_t* needed_tmp_size) = 0;3940// Does the actual compression. Returns NULL on success and a static error41// message otherwise. Sets the 'compressed_size'.42virtual char const* compress(char* in, size_t in_size, char* out, size_t out_size,43char* tmp, size_t tmp_size, size_t* compressed_size) = 0;44};4546// Interface for a writer implementation.47class AbstractWriter : public CHeapObj<mtInternal> {48public:49virtual ~AbstractWriter() { }5051// Opens the writer. Returns NULL on success and a static error message otherwise.52virtual char const* open_writer() = 0;5354// Does the write. Returns NULL on success and a static error message otherwise.55virtual char const* write_buf(char* buf, ssize_t size) = 0;56};575859// A writer for a file.60class FileWriter : public AbstractWriter {61private:62char const* _path;63int _fd;6465public:66FileWriter(char const* path) : _path(path), _fd(-1) { }6768~FileWriter();6970// Opens the writer. Returns NULL on success and a static error message otherwise.71virtual char const* open_writer();7273// Does the write. Returns NULL on success and a static error message otherwise.74virtual char const* write_buf(char* buf, ssize_t size);75};767778// A compressor using the gzip format.79class GZipCompressor : public AbstractCompressor {80private:81int _level;82size_t _block_size;83bool _is_first;8485void* load_gzip_func(char const* name);8687public:88GZipCompressor(int level) : _level(level), _block_size(0), _is_first(false) {89}9091virtual char const* init(size_t block_size, size_t* needed_out_size,92size_t* needed_tmp_size);9394virtual char const* compress(char* in, size_t in_size, char* out, size_t out_size,95char* tmp, size_t tmp_size, size_t* compressed_size);96};979899// The data needed to write a single buffer (and compress it optionally).100struct WriteWork {101// The id of the work.102int64_t _id;103104// The input buffer where the raw data is105char* _in;106size_t _in_used;107size_t _in_max;108109// The output buffer where the compressed data is. Is NULL when compression is disabled.110char* _out;111size_t _out_used;112size_t _out_max;113114// The temporary space needed for compression. Is NULL when compression is disabled.115char* _tmp;116size_t _tmp_max;117118// Used to link WriteWorks into lists.119WriteWork* _next;120WriteWork* _prev;121};122123// A list for works.124class WorkList {125private:126WriteWork _head;127128void insert(WriteWork* before, WriteWork* work);129WriteWork* remove(WriteWork* work);130131public:132WorkList();133134// Return true if the list is empty.135bool is_empty() { return _head._next == &_head; }136137// Adds to the beginning of the list.138void add_first(WriteWork* work) { insert(&_head, work); }139140// Adds to the end of the list.141void add_last(WriteWork* work) { insert(_head._prev, work); }142143// Adds so the ids are ordered.144void add_by_id(WriteWork* work);145146// Returns the first element.147WriteWork* first() { return is_empty() ? NULL : _head._next; }148149// Returns the last element.150WriteWork* last() { return is_empty() ? NULL : _head._prev; }151152// Removes the first element. Returns NULL if empty.153WriteWork* remove_first() { return remove(first()); }154155// Removes the last element. Returns NULL if empty.156WriteWork* remove_last() { return remove(first()); }157};158159160class Monitor;161162// This class is used by the DumpWriter class. It supplies the DumpWriter with163// chunks of memory to write the heap dump data into. When the DumpWriter needs a164// new memory chunk, it calls get_new_buffer(), which commits the old chunk used165// and returns a new chunk. The old chunk is then added to a queue to be compressed166// and then written in the background.167class CompressionBackend : StackObj {168bool _active;169char const * _err;170171int _nr_of_threads;172int _works_created;173bool _work_creation_failed;174175int64_t _id_to_write;176int64_t _next_id;177178size_t _in_size;179size_t _max_waste;180size_t _out_size;181size_t _tmp_size;182183size_t _written;184185AbstractWriter* const _writer;186AbstractCompressor* const _compressor;187188Monitor* const _lock;189190WriteWork* _current;191WorkList _to_compress;192WorkList _unused;193WorkList _finished;194195void set_error(char const* new_error);196197WriteWork* allocate_work(size_t in_size, size_t out_size, size_t tmp_size);198void free_work(WriteWork* work);199void free_work_list(WorkList* list);200201void do_foreground_work();202WriteWork* get_work();203void do_compress(WriteWork* work);204void finish_work(WriteWork* work);205206public:207// compressor can be NULL if no compression is used.208// Takes ownership of the writer and compressor.209// block_size is the buffer size of a WriteWork.210// max_waste is the maximum number of bytes to leave211// empty in the buffer when it is written.212CompressionBackend(AbstractWriter* writer, AbstractCompressor* compressor,213size_t block_size, size_t max_waste);214215~CompressionBackend();216217size_t get_written() const { return _written; }218219char const* error() const { return _err; }220221// Commits the old buffer (using the value in *used) and sets up a new one.222void get_new_buffer(char** buffer, size_t* used, size_t* max);223224// The entry point for a worker thread.225void thread_loop();226227// Shuts down the backend, releasing all threads.228void deactivate();229};230231232#endif // SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP233234235