Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/compiler/compileLog.cpp
32285 views
/*1* Copyright (c) 2002, 2014, 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 "ci/ciMethod.hpp"26#include "compiler/compileLog.hpp"27#include "memory/allocation.inline.hpp"28#include "oops/method.hpp"29#include "runtime/mutexLocker.hpp"30#include "runtime/os.hpp"3132CompileLog* CompileLog::_first = NULL;3334// ------------------------------------------------------------------35// CompileLog::CompileLog36CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id)37: _context(_context_buffer, sizeof(_context_buffer))38{39initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp, true));40_file_end = 0;41_thread_id = thread_id;4243_identities_limit = 0;44_identities_capacity = 400;45_identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);46_file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler);47strcpy((char*)_file, file_name);4849// link into the global list50{ MutexLocker locker(CompileTaskAlloc_lock);51_next = _first;52_first = this;53}54}5556CompileLog::~CompileLog() {57delete _out; // Close fd in fileStream::~fileStream()58_out = NULL;59// Remove partial file after merging in CompileLog::finish_log_on_error60unlink(_file);61FREE_C_HEAP_ARRAY(char, _identities, mtCompiler);62FREE_C_HEAP_ARRAY(char, _file, mtCompiler);63}646566// see_tag, pop_tag: Override the default do-nothing methods on xmlStream.67// These methods provide a hook for managing the the extra context markup.68void CompileLog::see_tag(const char* tag, bool push) {69if (_context.size() > 0 && _out != NULL) {70_out->write(_context.base(), _context.size());71_context.reset();72}73xmlStream::see_tag(tag, push);74}75void CompileLog::pop_tag(const char* tag) {76_context.reset(); // toss any context info.77xmlStream::pop_tag(tag);78}798081// ------------------------------------------------------------------82// CompileLog::identify83int CompileLog::identify(ciBaseObject* obj) {84if (obj == NULL) return 0;85int id = obj->ident();86if (id < 0) return id;87// If it has already been identified, just return the id.88if (id < _identities_limit && _identities[id] != 0) return id;89// Lengthen the array, if necessary.90if (id >= _identities_capacity) {91int new_cap = _identities_capacity * 2;92if (new_cap <= id) new_cap = id + 100;93_identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);94_identities_capacity = new_cap;95}96while (id >= _identities_limit) {97_identities[_identities_limit++] = 0;98}99assert(id < _identities_limit, "oob");100// Mark this id as processed.101// (Be sure to do this before any recursive calls to identify.)102_identities[id] = 1; // mark103104// Now, print the object's identity once, in detail.105if (obj->is_metadata()) {106ciMetadata* mobj = obj->as_metadata();107if (mobj->is_klass()) {108ciKlass* klass = mobj->as_klass();109begin_elem("klass id='%d'", id);110name(klass->name());111if (!klass->is_loaded()) {112print(" unloaded='1'");113} else {114print(" flags='%d'", klass->modifier_flags());115}116end_elem();117} else if (mobj->is_method()) {118ciMethod* method = mobj->as_method();119ciSignature* sig = method->signature();120// Pre-identify items that we will need!121identify(sig->return_type());122for (int i = 0; i < sig->count(); i++) {123identify(sig->type_at(i));124}125begin_elem("method id='%d' holder='%d'",126id, identify(method->holder()));127name(method->name());128print(" return='%d'", identify(sig->return_type()));129if (sig->count() > 0) {130print(" arguments='");131for (int i = 0; i < sig->count(); i++) {132print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));133}134print("'");135}136if (!method->is_loaded()) {137print(" unloaded='1'");138} else {139print(" flags='%d'", (jchar) method->flags().as_int());140// output a few metrics141print(" bytes='%d'", method->code_size());142method->log_nmethod_identity(this);143//print(" count='%d'", method->invocation_count());144//int bec = method->backedge_count();145//if (bec != 0) print(" backedge_count='%d'", bec);146print(" iicount='%d'", method->interpreter_invocation_count());147}148end_elem();149} else if (mobj->is_type()) {150BasicType type = mobj->as_type()->basic_type();151elem("type id='%d' name='%s'", id, type2name(type));152} else {153// Should not happen.154elem("unknown id='%d'", id);155ShouldNotReachHere();156}157} else if (obj->is_symbol()) {158begin_elem("symbol id='%d'", id);159name(obj->as_symbol());160end_elem();161} else {162// Should not happen.163elem("unknown id='%d'", id);164}165return id;166}167168void CompileLog::name(ciSymbol* name) {169if (name == NULL) return;170print(" name='");171name->print_symbol_on(text()); // handles quoting conventions172print("'");173}174175176// ------------------------------------------------------------------177// CompileLog::clear_identities178// Forget which identities have been printed.179void CompileLog::clear_identities() {180_identities_limit = 0;181}182183// ------------------------------------------------------------------184// CompileLog::finish_log_on_error185//186// Note: This function is called after fatal error, avoid unnecessary memory187// or stack allocation, use only async-safe functions. It's possible JVM is188// only partially initialized.189void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {190static bool called_exit = false;191if (called_exit) return;192called_exit = true;193194CompileLog* log = _first;195while (log != NULL) {196log->flush();197const char* partial_file = log->file();198int partial_fd = open(partial_file, O_RDONLY);199if (partial_fd != -1) {200// print/print_cr may need to allocate large stack buffer to format201// strings, here we use snprintf() and print_raw() instead.202file->print_raw("<compilation_log thread='");203jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());204file->print_raw(buf);205file->print_raw_cr("'>");206207size_t nr; // number read into buf from partial log208// Copy data up to the end of the last <event> element:209julong to_read = log->_file_end;210while (to_read > 0) {211if (to_read < (julong)buflen)212nr = (size_t)to_read;213else nr = buflen;214nr = read(partial_fd, buf, (int)nr);215if (nr <= 0) break;216to_read -= (julong)nr;217file->write(buf, nr);218}219220// Copy any remaining data inside a quote:221bool saw_slop = false;222int end_cdata = 0; // state machine [0..2] watching for too many "]]"223while ((nr = read(partial_fd, buf, buflen)) > 0) {224if (!saw_slop) {225file->print_raw_cr("<fragment>");226file->print_raw_cr("<![CDATA[");227saw_slop = true;228}229// The rest of this loop amounts to a simple copy operation:230// { file->write(buf, nr); }231// However, it must sometimes output the buffer in parts,232// in case there is a CDATA quote embedded in the fragment.233const char* bufp; // pointer into buf234size_t nw; // number written in each pass of the following loop:235for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {236// Write up to any problematic CDATA delimiter (usually all of nr).237for (nw = 0; nw < nr; nw++) {238// First, scan ahead into the buf, checking the state machine.239switch (bufp[nw]) {240case ']':241if (end_cdata < 2) end_cdata += 1; // saturating counter242continue; // keep scanning243case '>':244if (end_cdata == 2) break; // found CDATA delimiter!245// else fall through:246default:247end_cdata = 0;248continue; // keep scanning249}250// If we get here, nw is pointing at a bad '>'.251// It is very rare for this to happen.252// However, this code has been tested by introducing253// CDATA sequences into the compilation log.254break;255}256// Now nw is the number of characters to write, usually == nr.257file->write(bufp, nw);258if (nw < nr) {259// We are about to go around the loop again.260// But first, disrupt the ]]> by closing and reopening the quote.261file->print_raw("]]><![CDATA[");262end_cdata = 0; // reset state machine263}264}265}266if (saw_slop) {267file->print_raw_cr("]]>");268file->print_raw_cr("</fragment>");269}270file->print_raw_cr("</compilation_log>");271close(partial_fd);272}273CompileLog* next_log = log->_next;274delete log; // Removes partial file275log = next_log;276}277_first = NULL;278}279280// ------------------------------------------------------------------281// CompileLog::finish_log282//283// Called during normal shutdown. For now, any clean-up needed in normal284// shutdown is also needed in VM abort, so is covered by finish_log_on_error().285// Just allocate a buffer and call finish_log_on_error().286void CompileLog::finish_log(outputStream* file) {287char buf[4 * K];288finish_log_on_error(file, buf, sizeof(buf));289}290291// ------------------------------------------------------------------292// CompileLog::inline_success293//294// Print about successful method inlining.295void CompileLog::inline_success(const char* reason) {296begin_elem("inline_success reason='");297text("%s", reason);298end_elem("'");299}300301// ------------------------------------------------------------------302// CompileLog::inline_fail303//304// Print about failed method inlining.305void CompileLog::inline_fail(const char* reason) {306begin_elem("inline_fail reason='");307text("%s", reason);308end_elem("'");309}310311// ------------------------------------------------------------------312// CompileLog::set_context313//314// Set XML tag as an optional marker - it is printed only if315// there are other entries after until it is reset.316void CompileLog::set_context(const char* format, ...) {317va_list ap;318va_start(ap, format);319clear_context();320_context.print("<");321_context.vprint(format, ap);322_context.print_cr("/>");323va_end(ap);324}325326// ------------------------------------------------------------------327// CompileLog::code_cache_state328//329// Print code cache state.330void CompileLog::code_cache_state() {331begin_elem("code_cache");332CodeCache::log_state(this);333end_elem("%s", "");334}335336337