Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/jfr/leakprofiler/checkpoint/objectSampleDescription.cpp
41155 views
1
/*
2
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. 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
#include "precompiled.hpp"
26
#include "jvm_io.h"
27
#include "classfile/javaClasses.inline.hpp"
28
#include "classfile/symbolTable.hpp"
29
#include "classfile/vmClasses.hpp"
30
#include "classfile/vmSymbols.hpp"
31
#include "jfr/leakprofiler/checkpoint/objectSampleDescription.hpp"
32
#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
33
#include "oops/oop.inline.hpp"
34
#include "runtime/fieldDescriptor.inline.hpp"
35
#include "runtime/thread.hpp"
36
#include "utilities/ostream.hpp"
37
38
static Symbol* symbol_size = NULL;
39
40
ObjectDescriptionBuilder::ObjectDescriptionBuilder() {
41
reset();
42
}
43
44
void ObjectDescriptionBuilder::write_int(jint value) {
45
char buf[20];
46
jio_snprintf(buf, sizeof(buf), "%d", value);
47
write_text(buf);
48
}
49
50
void ObjectDescriptionBuilder::write_text(const char* text) {
51
if (_index == sizeof(_buffer) - 2) {
52
return;
53
}
54
while (*text != '\0' && _index < sizeof(_buffer) - 2) {
55
_buffer[_index] = *text;
56
_index++;
57
text++;
58
}
59
assert(_index < sizeof(_buffer) - 1, "index should not exceed buffer size");
60
// add ellipsis if we reached end
61
if (_index == sizeof(_buffer) - 2) {
62
_buffer[_index-3] = '.';
63
_buffer[_index-2] = '.';
64
_buffer[_index-1] = '.';
65
}
66
// terminate string
67
_buffer[_index] = '\0';
68
}
69
70
void ObjectDescriptionBuilder::reset() {
71
_index = 0;
72
_buffer[0] = '\0';
73
}
74
75
void ObjectDescriptionBuilder::print_description(outputStream* out) {
76
out->print("%s", (const char*)_buffer);
77
}
78
79
const char* ObjectDescriptionBuilder::description() {
80
if (_buffer[0] == '\0') {
81
return NULL;
82
}
83
const size_t len = strlen(_buffer);
84
char* copy = NEW_RESOURCE_ARRAY(char, len + 1);
85
assert(copy != NULL, "invariant");
86
strncpy(copy, _buffer, len + 1);
87
return copy;
88
}
89
90
ObjectSampleDescription::ObjectSampleDescription(oop object) :
91
_object(object) {
92
}
93
94
void ObjectSampleDescription::ensure_initialized() {
95
if (symbol_size == NULL) {
96
symbol_size = SymbolTable::new_permanent_symbol("size");
97
}
98
}
99
100
void ObjectSampleDescription::print_description(outputStream* out) {
101
write_object_to_buffer();
102
_description.print_description(out);
103
}
104
105
const char* ObjectSampleDescription::description() {
106
write_object_to_buffer();
107
return _description.description();
108
}
109
110
void ObjectSampleDescription::write_text(const char* text) {
111
_description.write_text(text);
112
}
113
114
void ObjectSampleDescription::write_int(jint value) {
115
_description.write_int(value);
116
}
117
118
void ObjectSampleDescription::write_object_to_buffer() {
119
ensure_initialized();
120
_description.reset();
121
write_object_details();
122
}
123
124
void ObjectSampleDescription::write_object_details() {
125
Klass* klass = _object->klass();
126
Symbol* class_name = klass->name();
127
jint size;
128
129
if (_object->is_a(vmClasses::Class_klass())) {
130
write_class_name();
131
return;
132
}
133
134
if (_object->is_a(vmClasses::Thread_klass())) {
135
write_thread_name();
136
return;
137
}
138
139
if (_object->is_a(vmClasses::ThreadGroup_klass())) {
140
write_thread_group_name();
141
return;
142
}
143
144
if (read_int_size(&size)) {
145
write_size(size);
146
return;
147
}
148
}
149
150
void ObjectSampleDescription::write_class_name() {
151
assert(_object->is_a(vmClasses::Class_klass()), "invariant");
152
const Klass* const k = java_lang_Class::as_Klass(_object);
153
if (k == NULL) {
154
// might represent a primitive
155
const Klass* const ak = java_lang_Class::array_klass_acquire(_object);
156
// If ak is NULL, this is most likely a mirror associated with a
157
// jvmti redefine/retransform scratch klass. We can't get any additional
158
// information from it.
159
if (ak != NULL) {
160
write_text(type2name(java_lang_Class::primitive_type(_object)));
161
}
162
return;
163
}
164
165
if (k->is_instance_klass()) {
166
const InstanceKlass* ik = InstanceKlass::cast(k);
167
if (ik->is_hidden()) {
168
return;
169
}
170
const Symbol* name = ik->name();
171
if (name != NULL) {
172
write_text("Class Name: ");
173
write_text(name->as_klass_external_name());
174
}
175
}
176
}
177
178
void ObjectSampleDescription::write_thread_group_name() {
179
assert(_object->is_a(vmClasses::ThreadGroup_klass()), "invariant");
180
const char* tg_name = java_lang_ThreadGroup::name(_object);
181
if (tg_name != NULL) {
182
write_text("Thread Group: ");
183
write_text(tg_name);
184
}
185
}
186
187
void ObjectSampleDescription::write_thread_name() {
188
assert(_object->is_a(vmClasses::Thread_klass()), "invariant");
189
oop name = java_lang_Thread::name(_object);
190
if (name != NULL) {
191
char* p = java_lang_String::as_utf8_string(name);
192
if (p != NULL) {
193
write_text("Thread Name: ");
194
write_text(p);
195
}
196
}
197
}
198
199
void ObjectSampleDescription::write_size(jint size) {
200
if (size >= 0) {
201
write_text("Size: ");
202
write_int(size);
203
}
204
}
205
206
bool ObjectSampleDescription::read_int_size(jint* result_size) {
207
fieldDescriptor fd;
208
Klass* klass = _object->klass();
209
if (klass->is_instance_klass()) {
210
InstanceKlass* ik = InstanceKlass::cast(klass);
211
if (ik->find_field(symbol_size, vmSymbols::int_signature(), false, &fd) != NULL) {
212
jint size = _object->int_field(fd.offset());
213
*result_size = size;
214
return true;
215
}
216
}
217
return false;
218
}
219
220