Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/services/heapDumperCompression.hpp
41144 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
int _fd;
65
66
public:
67
FileWriter(char const* path) : _path(path), _fd(-1) { }
68
69
~FileWriter();
70
71
// Opens the writer. Returns NULL on success and a static error message otherwise.
72
virtual char const* open_writer();
73
74
// Does the write. Returns NULL on success and a static error message otherwise.
75
virtual char const* write_buf(char* buf, ssize_t size);
76
};
77
78
79
// A compressor using the gzip format.
80
class GZipCompressor : public AbstractCompressor {
81
private:
82
int _level;
83
size_t _block_size;
84
bool _is_first;
85
86
void* load_gzip_func(char const* name);
87
88
public:
89
GZipCompressor(int level) : _level(level), _block_size(0), _is_first(false) {
90
}
91
92
virtual char const* init(size_t block_size, size_t* needed_out_size,
93
size_t* needed_tmp_size);
94
95
virtual char const* compress(char* in, size_t in_size, char* out, size_t out_size,
96
char* tmp, size_t tmp_size, size_t* compressed_size);
97
};
98
99
100
// The data needed to write a single buffer (and compress it optionally).
101
struct WriteWork {
102
// The id of the work.
103
int64_t _id;
104
105
// The input buffer where the raw data is
106
char* _in;
107
size_t _in_used;
108
size_t _in_max;
109
110
// The output buffer where the compressed data is. Is NULL when compression is disabled.
111
char* _out;
112
size_t _out_used;
113
size_t _out_max;
114
115
// The temporary space needed for compression. Is NULL when compression is disabled.
116
char* _tmp;
117
size_t _tmp_max;
118
119
// Used to link WriteWorks into lists.
120
WriteWork* _next;
121
WriteWork* _prev;
122
};
123
124
// A list for works.
125
class WorkList {
126
private:
127
WriteWork _head;
128
129
void insert(WriteWork* before, WriteWork* work);
130
WriteWork* remove(WriteWork* work);
131
132
public:
133
WorkList();
134
135
// Return true if the list is empty.
136
bool is_empty() { return _head._next == &_head; }
137
138
// Adds to the beginning of the list.
139
void add_first(WriteWork* work) { insert(&_head, work); }
140
141
// Adds to the end of the list.
142
void add_last(WriteWork* work) { insert(_head._prev, work); }
143
144
// Adds so the ids are ordered.
145
void add_by_id(WriteWork* work);
146
147
// Returns the first element.
148
WriteWork* first() { return is_empty() ? NULL : _head._next; }
149
150
// Returns the last element.
151
WriteWork* last() { return is_empty() ? NULL : _head._prev; }
152
153
// Removes the first element. Returns NULL if empty.
154
WriteWork* remove_first() { return remove(first()); }
155
156
// Removes the last element. Returns NULL if empty.
157
WriteWork* remove_last() { return remove(first()); }
158
};
159
160
161
class Monitor;
162
163
// This class is used by the DumpWriter class. It supplies the DumpWriter with
164
// chunks of memory to write the heap dump data into. When the DumpWriter needs a
165
// new memory chunk, it calls get_new_buffer(), which commits the old chunk used
166
// and returns a new chunk. The old chunk is then added to a queue to be compressed
167
// and then written in the background.
168
class CompressionBackend : StackObj {
169
bool _active;
170
char const * _err;
171
172
int _nr_of_threads;
173
int _works_created;
174
bool _work_creation_failed;
175
176
int64_t _id_to_write;
177
int64_t _next_id;
178
179
size_t _in_size;
180
size_t _max_waste;
181
size_t _out_size;
182
size_t _tmp_size;
183
184
size_t _written;
185
186
AbstractWriter* const _writer;
187
AbstractCompressor* const _compressor;
188
189
Monitor* const _lock;
190
191
WriteWork* _current;
192
WorkList _to_compress;
193
WorkList _unused;
194
WorkList _finished;
195
196
void set_error(char const* new_error);
197
198
WriteWork* allocate_work(size_t in_size, size_t out_size, size_t tmp_size);
199
void free_work(WriteWork* work);
200
void free_work_list(WorkList* list);
201
202
void do_foreground_work();
203
WriteWork* get_work();
204
void do_compress(WriteWork* work);
205
void finish_work(WriteWork* work);
206
207
public:
208
// compressor can be NULL if no compression is used.
209
// Takes ownership of the writer and compressor.
210
// block_size is the buffer size of a WriteWork.
211
// max_waste is the maximum number of bytes to leave
212
// empty in the buffer when it is written.
213
CompressionBackend(AbstractWriter* writer, AbstractCompressor* compressor,
214
size_t block_size, size_t max_waste);
215
216
~CompressionBackend();
217
218
size_t get_written() const { return _written; }
219
220
char const* error() const { return _err; }
221
222
// Commits the old buffer (using the value in *used) and sets up a new one.
223
void get_new_buffer(char** buffer, size_t* used, size_t* max);
224
225
// The entry point for a worker thread.
226
void thread_loop();
227
228
// Shuts down the backend, releasing all threads.
229
void deactivate();
230
};
231
232
233
#endif // SHARE_SERVICES_HEAPDUMPERCOMPRESSION_HPP
234
235