2
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "ci/ciMethod.hpp"
27
#include "code/codeCache.hpp"
28
#include "compiler/compileLog.hpp"
30
#include "memory/allocation.inline.hpp"
31
#include "oops/method.hpp"
32
#include "runtime/mutexLocker.hpp"
33
#include "runtime/os.hpp"
35
CompileLog* CompileLog::_first = nullptr;
37
// ------------------------------------------------------------------
38
// CompileLog::CompileLog
39
CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id)
40
: _context(_context_buffer, sizeof(_context_buffer))
42
initialize(new(mtCompiler) fileStream(fp, true));
44
_thread_id = thread_id;
46
_identities_limit = 0;
47
_identities_capacity = 400;
48
_identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);
49
_file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler);
50
strcpy((char*)_file, file_name);
52
// link into the global list
53
{ MutexLocker locker(CompileTaskAlloc_lock);
59
CompileLog::~CompileLog() {
60
delete _out; // Close fd in fileStream::~fileStream()
62
// Remove partial file after merging in CompileLog::finish_log_on_error
64
FREE_C_HEAP_ARRAY(char, _identities);
65
FREE_C_HEAP_ARRAY(char, _file);
69
// see_tag, pop_tag: Override the default do-nothing methods on xmlStream.
70
// These methods provide a hook for managing the extra context markup.
71
void CompileLog::see_tag(const char* tag, bool push) {
72
if (_context.size() > 0 && _out != nullptr) {
73
_out->write(_context.base(), _context.size());
76
xmlStream::see_tag(tag, push);
78
void CompileLog::pop_tag(const char* tag) {
79
_context.reset(); // toss any context info.
80
xmlStream::pop_tag(tag);
84
// ------------------------------------------------------------------
85
// CompileLog::identify
86
int CompileLog::identify(ciBaseObject* obj) {
87
if (obj == nullptr) return 0;
88
int id = obj->ident();
89
if (id < 0) return id;
90
// If it has already been identified, just return the id.
91
if (id < _identities_limit && _identities[id] != 0) return id;
92
// Lengthen the array, if necessary.
93
if (id >= _identities_capacity) {
94
int new_cap = _identities_capacity * 2;
95
if (new_cap <= id) new_cap = id + 100;
96
_identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);
97
_identities_capacity = new_cap;
99
while (id >= _identities_limit) {
100
_identities[_identities_limit++] = 0;
102
assert(id < _identities_limit, "oob");
103
// Mark this id as processed.
104
// (Be sure to do this before any recursive calls to identify.)
105
_identities[id] = 1; // mark
107
// Now, print the object's identity once, in detail.
108
if (obj->is_metadata()) {
109
ciMetadata* mobj = obj->as_metadata();
110
if (mobj->is_klass()) {
111
ciKlass* klass = mobj->as_klass();
112
begin_elem("klass id='%d'", id);
114
if (!klass->is_loaded()) {
115
print(" unloaded='1'");
117
print(" flags='%d'", klass->modifier_flags());
120
} else if (mobj->is_method()) {
121
ciMethod* method = mobj->as_method();
122
ciSignature* sig = method->signature();
123
// Pre-identify items that we will need!
124
identify(sig->return_type());
125
for (int i = 0; i < sig->count(); i++) {
126
identify(sig->type_at(i));
128
begin_elem("method id='%d' holder='%d'",
129
id, identify(method->holder()));
130
name(method->name());
131
print(" return='%d'", identify(sig->return_type()));
132
if (sig->count() > 0) {
133
print(" arguments='");
134
for (int i = 0; i < sig->count(); i++) {
135
print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
139
if (!method->is_loaded()) {
140
print(" unloaded='1'");
142
print(" flags='%d'", (jchar) method->flags().as_int());
143
// output a few metrics
144
print(" bytes='%d'", method->code_size());
145
method->log_nmethod_identity(this);
146
//print(" count='%d'", method->invocation_count());
147
//int bec = method->backedge_count();
148
//if (bec != 0) print(" backedge_count='%d'", bec);
149
print(" iicount='%d'", method->interpreter_invocation_count());
152
} else if (mobj->is_type()) {
153
BasicType type = mobj->as_type()->basic_type();
154
elem("type id='%d' name='%s'", id, type2name(type));
156
// Should not happen.
157
elem("unknown id='%d'", id);
158
ShouldNotReachHere();
160
} else if (obj->is_symbol()) {
161
begin_elem("symbol id='%d'", id);
162
name(obj->as_symbol());
165
// Should not happen.
166
elem("unknown id='%d'", id);
171
void CompileLog::name(ciSymbol* name) {
172
if (name == nullptr) return;
174
name->print_symbol_on(text()); // handles quoting conventions
178
void CompileLog::name(ciKlass* k) {
180
if (!k->is_loaded()) {
181
text()->print("%s", k->name()->as_klass_external_name());
183
text()->print("%s", k->external_name());
188
// ------------------------------------------------------------------
189
// CompileLog::clear_identities
190
// Forget which identities have been printed.
191
void CompileLog::clear_identities() {
192
_identities_limit = 0;
195
// ------------------------------------------------------------------
196
// CompileLog::finish_log_on_error
198
// Note: This function is called after fatal error, avoid unnecessary memory
199
// or stack allocation, use only async-safe functions. It's possible JVM is
200
// only partially initialized.
201
void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
202
static bool called_exit = false;
203
if (called_exit) return;
206
CompileLog* log = _first;
207
while (log != nullptr) {
209
const char* partial_file = log->file();
210
int partial_fd = open(partial_file, O_RDONLY);
211
if (partial_fd != -1) {
212
// print/print_cr may need to allocate large stack buffer to format
213
// strings, here we use snprintf() and print_raw() instead.
214
file->print_raw("<compilation_log thread='");
215
jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
216
file->print_raw(buf);
217
file->print_raw_cr("'>");
219
size_t nr; // number read into buf from partial log
220
// In case of unsuccessful completion, read returns -1.
222
// Copy data up to the end of the last <event> element:
223
julong to_read = log->_file_end;
224
while (to_read > 0) {
225
if (to_read < (julong)buflen)
226
nr = (size_t)to_read;
228
bytes_read = ::read(partial_fd, buf, (int)nr);
229
if (bytes_read <= 0) break;
231
to_read -= (julong)nr;
232
file->write(buf, nr);
235
// Copy any remaining data inside a quote:
236
bool saw_slop = false;
237
int end_cdata = 0; // state machine [0..2] watching for too many "]]"
238
while ((bytes_read = ::read(partial_fd, buf, buflen-1)) > 0) {
240
buf[buflen-1] = '\0';
242
file->print_raw_cr("<fragment>");
243
file->print_raw_cr("<![CDATA[");
246
// The rest of this loop amounts to a simple copy operation:
247
// { file->write(buf, nr); }
248
// However, it must sometimes output the buffer in parts,
249
// in case there is a CDATA quote embedded in the fragment.
250
const char* bufp; // pointer into buf
251
size_t nw; // number written in each pass of the following loop:
252
for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
253
// Write up to any problematic CDATA delimiter (usually all of nr).
254
for (nw = 0; nw < nr; nw++) {
255
// First, scan ahead into the buf, checking the state machine.
258
if (end_cdata < 2) end_cdata += 1; // saturating counter
259
continue; // keep scanning
261
if (end_cdata == 2) break; // found CDATA delimiter!
262
// else fall through:
265
continue; // keep scanning
267
// If we get here, nw is pointing at a bad '>'.
268
// It is very rare for this to happen.
269
// However, this code has been tested by introducing
270
// CDATA sequences into the compilation log.
273
// Now nw is the number of characters to write, usually == nr.
274
file->write(bufp, nw);
276
// We are about to go around the loop again.
277
// But first, disrupt the ]]> by closing and reopening the quote.
278
file->print_raw("]]><![CDATA[");
279
end_cdata = 0; // reset state machine
284
file->print_raw_cr("]]>");
285
file->print_raw_cr("</fragment>");
287
file->print_raw_cr("</compilation_log>");
290
CompileLog* next_log = log->_next;
291
delete log; // Removes partial file
297
// ------------------------------------------------------------------
298
// CompileLog::finish_log
300
// Called during normal shutdown. For now, any clean-up needed in normal
301
// shutdown is also needed in VM abort, so is covered by finish_log_on_error().
302
// Just allocate a buffer and call finish_log_on_error().
303
void CompileLog::finish_log(outputStream* file) {
305
finish_log_on_error(file, buf, sizeof(buf));
308
// ------------------------------------------------------------------
309
// CompileLog::inline_success
311
// Print about successful method inlining.
312
void CompileLog::inline_success(const char* reason) {
313
begin_elem("inline_success reason='");
318
// ------------------------------------------------------------------
319
// CompileLog::inline_fail
321
// Print about failed method inlining.
322
void CompileLog::inline_fail(const char* reason) {
323
begin_elem("inline_fail reason='");
328
// ------------------------------------------------------------------
329
// CompileLog::set_context
331
// Set XML tag as an optional marker - it is printed only if
332
// there are other entries after until it is reset.
333
void CompileLog::set_context(const char* format, ...) {
335
va_start(ap, format);
338
_context.vprint(format, ap);
339
_context.print_cr("/>");
343
// ------------------------------------------------------------------
344
// CompileLog::code_cache_state
346
// Print code cache state.
347
void CompileLog::code_cache_state() {
348
begin_elem("code_cache");
349
CodeCache::log_state(this);