Path: blob/master/src/hotspot/share/compiler/compileLog.cpp
40930 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. 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#include "precompiled.hpp"25#include "jvm.h"26#include "ci/ciMethod.hpp"27#include "code/codeCache.hpp"28#include "compiler/compileLog.hpp"29#include "memory/allocation.inline.hpp"30#include "oops/method.hpp"31#include "runtime/mutexLocker.hpp"32#include "runtime/os.hpp"3334CompileLog* CompileLog::_first = NULL;3536// ------------------------------------------------------------------37// CompileLog::CompileLog38CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id)39: _context(_context_buffer, sizeof(_context_buffer))40{41initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp, true));42_file_end = 0;43_thread_id = thread_id;4445_identities_limit = 0;46_identities_capacity = 400;47_identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);48_file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler);49strcpy((char*)_file, file_name);5051// link into the global list52{ MutexLocker locker(CompileTaskAlloc_lock);53_next = _first;54_first = this;55}56}5758CompileLog::~CompileLog() {59delete _out; // Close fd in fileStream::~fileStream()60_out = NULL;61// Remove partial file after merging in CompileLog::finish_log_on_error62unlink(_file);63FREE_C_HEAP_ARRAY(char, _identities);64FREE_C_HEAP_ARRAY(char, _file);65}666768// see_tag, pop_tag: Override the default do-nothing methods on xmlStream.69// These methods provide a hook for managing the extra context markup.70void CompileLog::see_tag(const char* tag, bool push) {71if (_context.size() > 0 && _out != NULL) {72_out->write(_context.base(), _context.size());73_context.reset();74}75xmlStream::see_tag(tag, push);76}77void CompileLog::pop_tag(const char* tag) {78_context.reset(); // toss any context info.79xmlStream::pop_tag(tag);80}818283// ------------------------------------------------------------------84// CompileLog::identify85int CompileLog::identify(ciBaseObject* obj) {86if (obj == NULL) return 0;87int id = obj->ident();88if (id < 0) return id;89// If it has already been identified, just return the id.90if (id < _identities_limit && _identities[id] != 0) return id;91// Lengthen the array, if necessary.92if (id >= _identities_capacity) {93int new_cap = _identities_capacity * 2;94if (new_cap <= id) new_cap = id + 100;95_identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);96_identities_capacity = new_cap;97}98while (id >= _identities_limit) {99_identities[_identities_limit++] = 0;100}101assert(id < _identities_limit, "oob");102// Mark this id as processed.103// (Be sure to do this before any recursive calls to identify.)104_identities[id] = 1; // mark105106// Now, print the object's identity once, in detail.107if (obj->is_metadata()) {108ciMetadata* mobj = obj->as_metadata();109if (mobj->is_klass()) {110ciKlass* klass = mobj->as_klass();111begin_elem("klass id='%d'", id);112name(klass);113if (!klass->is_loaded()) {114print(" unloaded='1'");115} else {116print(" flags='%d'", klass->modifier_flags());117}118end_elem();119} else if (mobj->is_method()) {120ciMethod* method = mobj->as_method();121ciSignature* sig = method->signature();122// Pre-identify items that we will need!123identify(sig->return_type());124for (int i = 0; i < sig->count(); i++) {125identify(sig->type_at(i));126}127begin_elem("method id='%d' holder='%d'",128id, identify(method->holder()));129name(method->name());130print(" return='%d'", identify(sig->return_type()));131if (sig->count() > 0) {132print(" arguments='");133for (int i = 0; i < sig->count(); i++) {134print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));135}136print("'");137}138if (!method->is_loaded()) {139print(" unloaded='1'");140} else {141print(" flags='%d'", (jchar) method->flags().as_int());142// output a few metrics143print(" bytes='%d'", method->code_size());144method->log_nmethod_identity(this);145//print(" count='%d'", method->invocation_count());146//int bec = method->backedge_count();147//if (bec != 0) print(" backedge_count='%d'", bec);148print(" iicount='%d'", method->interpreter_invocation_count());149}150end_elem();151} else if (mobj->is_type()) {152BasicType type = mobj->as_type()->basic_type();153elem("type id='%d' name='%s'", id, type2name(type));154} else {155// Should not happen.156elem("unknown id='%d'", id);157ShouldNotReachHere();158}159} else if (obj->is_symbol()) {160begin_elem("symbol id='%d'", id);161name(obj->as_symbol());162end_elem();163} else {164// Should not happen.165elem("unknown id='%d'", id);166}167return id;168}169170void CompileLog::name(ciSymbol* name) {171if (name == NULL) return;172print(" name='");173name->print_symbol_on(text()); // handles quoting conventions174print("'");175}176177void CompileLog::name(ciKlass* k) {178print(" name='");179if (!k->is_loaded()) {180text()->print("%s", k->name()->as_klass_external_name());181} else {182text()->print("%s", k->external_name());183}184print("'");185}186187// ------------------------------------------------------------------188// CompileLog::clear_identities189// Forget which identities have been printed.190void CompileLog::clear_identities() {191_identities_limit = 0;192}193194// ------------------------------------------------------------------195// CompileLog::finish_log_on_error196//197// Note: This function is called after fatal error, avoid unnecessary memory198// or stack allocation, use only async-safe functions. It's possible JVM is199// only partially initialized.200void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {201static bool called_exit = false;202if (called_exit) return;203called_exit = true;204205CompileLog* log = _first;206while (log != NULL) {207log->flush();208const char* partial_file = log->file();209int partial_fd = open(partial_file, O_RDONLY);210if (partial_fd != -1) {211// print/print_cr may need to allocate large stack buffer to format212// strings, here we use snprintf() and print_raw() instead.213file->print_raw("<compilation_log thread='");214jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());215file->print_raw(buf);216file->print_raw_cr("'>");217218size_t nr; // number read into buf from partial log219// In case of unsuccessful completion, read returns -1.220ssize_t bytes_read;221// Copy data up to the end of the last <event> element:222julong to_read = log->_file_end;223while (to_read > 0) {224if (to_read < (julong)buflen)225nr = (size_t)to_read;226else nr = buflen;227bytes_read = read(partial_fd, buf, (int)nr);228if (bytes_read <= 0) break;229nr = bytes_read;230to_read -= (julong)nr;231file->write(buf, nr);232}233234// Copy any remaining data inside a quote:235bool saw_slop = false;236int end_cdata = 0; // state machine [0..2] watching for too many "]]"237while ((bytes_read = read(partial_fd, buf, buflen-1)) > 0) {238nr = bytes_read;239buf[buflen-1] = '\0';240if (!saw_slop) {241file->print_raw_cr("<fragment>");242file->print_raw_cr("<![CDATA[");243saw_slop = true;244}245// The rest of this loop amounts to a simple copy operation:246// { file->write(buf, nr); }247// However, it must sometimes output the buffer in parts,248// in case there is a CDATA quote embedded in the fragment.249const char* bufp; // pointer into buf250size_t nw; // number written in each pass of the following loop:251for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {252// Write up to any problematic CDATA delimiter (usually all of nr).253for (nw = 0; nw < nr; nw++) {254// First, scan ahead into the buf, checking the state machine.255switch (bufp[nw]) {256case ']':257if (end_cdata < 2) end_cdata += 1; // saturating counter258continue; // keep scanning259case '>':260if (end_cdata == 2) break; // found CDATA delimiter!261// else fall through:262default:263end_cdata = 0;264continue; // keep scanning265}266// If we get here, nw is pointing at a bad '>'.267// It is very rare for this to happen.268// However, this code has been tested by introducing269// CDATA sequences into the compilation log.270break;271}272// Now nw is the number of characters to write, usually == nr.273file->write(bufp, nw);274if (nw < nr) {275// We are about to go around the loop again.276// But first, disrupt the ]]> by closing and reopening the quote.277file->print_raw("]]><![CDATA[");278end_cdata = 0; // reset state machine279}280}281}282if (saw_slop) {283file->print_raw_cr("]]>");284file->print_raw_cr("</fragment>");285}286file->print_raw_cr("</compilation_log>");287close(partial_fd);288}289CompileLog* next_log = log->_next;290delete log; // Removes partial file291log = next_log;292}293_first = NULL;294}295296// ------------------------------------------------------------------297// CompileLog::finish_log298//299// Called during normal shutdown. For now, any clean-up needed in normal300// shutdown is also needed in VM abort, so is covered by finish_log_on_error().301// Just allocate a buffer and call finish_log_on_error().302void CompileLog::finish_log(outputStream* file) {303char buf[4 * K];304finish_log_on_error(file, buf, sizeof(buf));305}306307// ------------------------------------------------------------------308// CompileLog::inline_success309//310// Print about successful method inlining.311void CompileLog::inline_success(const char* reason) {312begin_elem("inline_success reason='");313text("%s", reason);314end_elem("'");315}316317// ------------------------------------------------------------------318// CompileLog::inline_fail319//320// Print about failed method inlining.321void CompileLog::inline_fail(const char* reason) {322begin_elem("inline_fail reason='");323text("%s", reason);324end_elem("'");325}326327// ------------------------------------------------------------------328// CompileLog::set_context329//330// Set XML tag as an optional marker - it is printed only if331// there are other entries after until it is reset.332void CompileLog::set_context(const char* format, ...) {333va_list ap;334va_start(ap, format);335clear_context();336_context.print("<");337_context.vprint(format, ap);338_context.print_cr("/>");339va_end(ap);340}341342// ------------------------------------------------------------------343// CompileLog::code_cache_state344//345// Print code cache state.346void CompileLog::code_cache_state() {347begin_elem("code_cache");348CodeCache::log_state(this);349end_elem("%s", "");350}351352353