Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/services/heapDumperCompression.hpp
64440 views
1
/*
2
* Copyright (c) 2020 SAP SE. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#ifndef SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP
26
#define SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP
27
28
#include "memory/allocation.hpp"
29
30
31
// Interface for a compression implementation.
32
class AbstractCompressor : public CHeapObj<mtInternal> {
33
public:
34
virtual ~AbstractCompressor() { }
35
36
// Initializes the compressor. Returns a static error message in case of an error.
37
// Otherwise initializes the needed out and tmp size for the given block size.
38
virtual char const* init(size_t block_size, size_t* needed_out_size,
39
size_t* needed_tmp_size) = 0;
40
41
// Does the actual compression. Returns NULL on success and a static error
42
// message otherwise. Sets the 'compressed_size'.
43
virtual char const* compress(char* in, size_t in_size, char* out, size_t out_size,
44
char* tmp, size_t tmp_size, size_t* compressed_size) = 0;
45
};
46
47
// Interface for a writer implementation.
48
class AbstractWriter : public CHeapObj<mtInternal> {
49
public:
50
virtual ~AbstractWriter() { }
51
52
// Opens the writer. Returns NULL on success and a static error message otherwise.
53
virtual char const* open_writer() = 0;
54
55
// Does the write. Returns NULL on success and a static error message otherwise.
56
virtual char const* write_buf(char* buf, ssize_t size) = 0;
57
};
58
59
60
// A writer for a file.
61
class FileWriter : public AbstractWriter {
62
private:
63
char const* _path;
64
bool _overwrite;
65
int _fd;
66
67
public:
68
FileWriter(char const* path, bool overwrite) : _path(path), _overwrite(overwrite), _fd(-1) { }
69
70
~FileWriter();
71
72
// Opens the writer. Returns NULL on success and a static error message otherwise.
73
virtual char const* open_writer();
74
75
// Does the write. Returns NULL on success and a static error message otherwise.
76
virtual char const* write_buf(char* buf, ssize_t size);
77
};
78
79
80
// A compressor using the gzip format.
81
class GZipCompressor : public AbstractCompressor {
82
private:
83
int _level;
84
size_t _block_size;
85
bool _is_first;
86
87
void* load_gzip_func(char const* name);
88
89
public:
90
GZipCompressor(int level) : _level(level), _block_size(0), _is_first(false) {
91
}
92
93
virtual char const* init(size_t block_size, size_t* needed_out_size,
94
size_t* needed_tmp_size);
95
96
virtual char const* compress(char* in, size_t in_size, char* out, size_t out_size,
97
char* tmp, size_t tmp_size, size_t* compressed_size);
98
};
99
100
101
// The data needed to write a single buffer (and compress it optionally).
102
struct WriteWork {
103
// The id of the work.
104
int64_t _id;
105
106
// The input buffer where the raw data is
107
char* _in;
108
size_t _in_used;
109
size_t _in_max;
110
111
// The output buffer where the compressed data is. Is NULL when compression is disabled.
112
char* _out;
113
size_t _out_used;
114
size_t _out_max;
115
116
// The temporary space needed for compression. Is NULL when compression is disabled.
117
char* _tmp;
118
size_t _tmp_max;
119
120
// Used to link WriteWorks into lists.
121
WriteWork* _next;
122
WriteWork* _prev;
123
};
124
125
// A list for works.
126
class WorkList {
127
private:
128
WriteWork _head;
129
130
void insert(WriteWork* before, WriteWork* work);
131
WriteWork* remove(WriteWork* work);
132
133
public:
134
WorkList();
135
136
// Return true if the list is empty.
137
bool is_empty() { return _head._next == &_head; }
138
139
// Adds to the beginning of the list.
140
void add_first(WriteWork* work) { insert(&_head, work); }
141
142
// Adds to the end of the list.
143
void add_last(WriteWork* work) { insert(_head._prev, work); }
144
145
// Adds so the ids are ordered.
146
void add_by_id(WriteWork* work);
147
148
// Returns the first element.
149
WriteWork* first() { return is_empty() ? NULL : _head._next; }
150
151
// Returns the last element.
152
WriteWork* last() { return is_empty() ? NULL : _head._prev; }
153
154
// Removes the first element. Returns NULL if empty.
155
WriteWork* remove_first() { return remove(first()); }
156
157
// Removes the last element. Returns NULL if empty.
158
WriteWork* remove_last() { return remove(first()); }
159
};
160
161
162
class Monitor;
163
164
// This class is used by the DumpWriter class. It supplies the DumpWriter with
165
// chunks of memory to write the heap dump data into. When the DumpWriter needs a
166
// new memory chunk, it calls get_new_buffer(), which commits the old chunk used
167
// and returns a new chunk. The old chunk is then added to a queue to be compressed
168
// and then written in the background.
169
class CompressionBackend : StackObj {
170
bool _active;
171
char const * _err;
172
173
int _nr_of_threads;
174
int _works_created;
175
bool _work_creation_failed;
176
177
int64_t _id_to_write;
178
int64_t _next_id;
179
180
size_t _in_size;
181
size_t _max_waste;
182
size_t _out_size;
183
size_t _tmp_size;
184
185
size_t _written;
186
187
AbstractWriter* const _writer;
188
AbstractCompressor* const _compressor;
189
190
Monitor* const _lock;
191
192
WriteWork* _current;
193
WorkList _to_compress;
194
WorkList _unused;
195
WorkList _finished;
196
197
void set_error(char const* new_error);
198
199
WriteWork* allocate_work(size_t in_size, size_t out_size, size_t tmp_size);
200
void free_work(WriteWork* work);
201
void free_work_list(WorkList* list);
202
203
void do_foreground_work();
204
WriteWork* get_work();
205
void do_compress(WriteWork* work);
206
void finish_work(WriteWork* work);
207
208
public:
209
// compressor can be NULL if no compression is used.
210
// Takes ownership of the writer and compressor.
211
// block_size is the buffer size of a WriteWork.
212
// max_waste is the maximum number of bytes to leave
213
// empty in the buffer when it is written.
214
CompressionBackend(AbstractWriter* writer, AbstractCompressor* compressor,
215
size_t block_size, size_t max_waste);
216
217
~CompressionBackend();
218
219
size_t get_written() const { return _written; }
220
221
char const* error() const { return _err; }
222
223
// Commits the old buffer (using the value in *used) and sets up a new one.
224
void get_new_buffer(char** buffer, size_t* used, size_t* max);
225
226
// The entry point for a worker thread.
227
void thread_loop();
228
229
// Shuts down the backend, releasing all threads.
230
void deactivate();
231
};
232
233
234
#endif // SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP
235
236