0N/A/*
1879N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "ci/ciMethod.hpp"
1879N/A#include "compiler/compileLog.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/os.hpp"
0N/A
0N/ACompileLog* CompileLog::_first = NULL;
0N/A
0N/A// ------------------------------------------------------------------
0N/A// CompileLog::CompileLog
0N/ACompileLog::CompileLog(const char* file, FILE* fp, intx thread_id)
0N/A : _context(_context_buffer, sizeof(_context_buffer))
0N/A{
3863N/A initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp));
0N/A _file = file;
0N/A _file_end = 0;
0N/A _thread_id = thread_id;
0N/A
0N/A _identities_limit = 0;
0N/A _identities_capacity = 400;
3863N/A _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);
0N/A
0N/A // link into the global list
0N/A { MutexLocker locker(CompileTaskAlloc_lock);
0N/A _next = _first;
0N/A _first = this;
0N/A }
0N/A}
0N/A
0N/ACompileLog::~CompileLog() {
0N/A delete _out;
0N/A _out = NULL;
3863N/A FREE_C_HEAP_ARRAY(char, _identities, mtCompiler);
0N/A}
0N/A
0N/A
0N/A// Advance kind up to a null or space, return this tail.
0N/A// Make sure kind is null-terminated, not space-terminated.
0N/A// Use the buffer if necessary.
0N/Astatic const char* split_attrs(const char* &kind, char* buffer) {
0N/A const char* attrs = strchr(kind, ' ');
0N/A // Tease apart the first word from the rest:
0N/A if (attrs == NULL) {
0N/A return ""; // no attrs, no split
0N/A } else if (kind == buffer) {
0N/A ((char*) attrs)[-1] = 0;
0N/A return attrs;
0N/A } else {
0N/A // park it in the buffer, so we can put a null on the end
1409N/A assert(!(kind >= buffer && kind < buffer+100), "not obviously in buffer");
0N/A int klen = attrs - kind;
0N/A strncpy(buffer, kind, klen);
0N/A buffer[klen] = 0;
0N/A kind = buffer; // return by reference
0N/A return attrs;
0N/A }
0N/A}
0N/A
0N/A// see_tag, pop_tag: Override the default do-nothing methods on xmlStream.
0N/A// These methods provide a hook for managing the the extra context markup.
0N/Avoid CompileLog::see_tag(const char* tag, bool push) {
0N/A if (_context.size() > 0 && _out != NULL) {
0N/A _out->write(_context.base(), _context.size());
0N/A _context.reset();
0N/A }
0N/A xmlStream::see_tag(tag, push);
0N/A}
0N/Avoid CompileLog::pop_tag(const char* tag) {
0N/A _context.reset(); // toss any context info.
0N/A xmlStream::pop_tag(tag);
0N/A}
0N/A
0N/A
0N/A// ------------------------------------------------------------------
0N/A// CompileLog::identify
0N/Aint CompileLog::identify(ciObject* obj) {
0N/A if (obj == NULL) return 0;
0N/A int id = obj->ident();
0N/A if (id < 0) return id;
0N/A // If it has already been identified, just return the id.
0N/A if (id < _identities_limit && _identities[id] != 0) return id;
0N/A // Lengthen the array, if necessary.
0N/A if (id >= _identities_capacity) {
0N/A int new_cap = _identities_capacity * 2;
0N/A if (new_cap <= id) new_cap = id + 100;
3863N/A _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);
0N/A _identities_capacity = new_cap;
0N/A }
0N/A while (id >= _identities_limit) {
0N/A _identities[_identities_limit++] = 0;
0N/A }
0N/A assert(id < _identities_limit, "oob");
0N/A // Mark this id as processed.
0N/A // (Be sure to do this before any recursive calls to identify.)
0N/A _identities[id] = 1; // mark
0N/A
0N/A // Now, print the object's identity once, in detail.
0N/A if (obj->is_klass()) {
0N/A ciKlass* klass = obj->as_klass();
0N/A begin_elem("klass id='%d'", id);
0N/A name(klass->name());
0N/A if (!klass->is_loaded()) {
0N/A print(" unloaded='1'");
0N/A } else {
0N/A print(" flags='%d'", klass->modifier_flags());
0N/A }
0N/A end_elem();
0N/A } else if (obj->is_method()) {
0N/A ciMethod* method = obj->as_method();
0N/A ciSignature* sig = method->signature();
0N/A // Pre-identify items that we will need!
0N/A identify(sig->return_type());
0N/A for (int i = 0; i < sig->count(); i++) {
0N/A identify(sig->type_at(i));
0N/A }
0N/A begin_elem("method id='%d' holder='%d'",
0N/A id, identify(method->holder()));
0N/A name(method->name());
0N/A print(" return='%d'", identify(sig->return_type()));
0N/A if (sig->count() > 0) {
0N/A print(" arguments='");
0N/A for (int i = 0; i < sig->count(); i++) {
0N/A print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
0N/A }
0N/A print("'");
0N/A }
0N/A if (!method->is_loaded()) {
0N/A print(" unloaded='1'");
0N/A } else {
0N/A print(" flags='%d'", (jchar) method->flags().as_int());
0N/A // output a few metrics
0N/A print(" bytes='%d'", method->code_size());
0N/A method->log_nmethod_identity(this);
0N/A //print(" count='%d'", method->invocation_count());
0N/A //int bec = method->backedge_count();
0N/A //if (bec != 0) print(" backedge_count='%d'", bec);
0N/A print(" iicount='%d'", method->interpreter_invocation_count());
0N/A }
0N/A end_elem();
0N/A } else if (obj->is_symbol()) {
0N/A begin_elem("symbol id='%d'", id);
0N/A name(obj->as_symbol());
0N/A end_elem();
0N/A } else if (obj->is_null_object()) {
0N/A elem("null_object id='%d'", id);
0N/A } else if (obj->is_type()) {
0N/A BasicType type = obj->as_type()->basic_type();
0N/A elem("type id='%d' name='%s'", id, type2name(type));
0N/A } else {
0N/A // Should not happen.
0N/A elem("unknown id='%d'", id);
0N/A }
0N/A return id;
0N/A}
0N/A
0N/Avoid CompileLog::name(ciSymbol* name) {
0N/A if (name == NULL) return;
0N/A print(" name='");
0N/A name->print_symbol_on(text()); // handles quoting conventions
0N/A print("'");
0N/A}
0N/A
0N/A
0N/A// ------------------------------------------------------------------
0N/A// CompileLog::clear_identities
0N/A// Forget which identities have been printed.
0N/Avoid CompileLog::clear_identities() {
0N/A _identities_limit = 0;
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// CompileLog::finish_log_on_error
0N/A//
0N/A// Note: This function is called after fatal error, avoid unnecessary memory
0N/A// or stack allocation, use only async-safe functions. It's possible JVM is
0N/A// only partially initialized.
0N/Avoid CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
0N/A static bool called_exit = false;
0N/A if (called_exit) return;
0N/A called_exit = true;
0N/A
0N/A for (CompileLog* log = _first; log != NULL; log = log->_next) {
0N/A log->flush();
0N/A const char* partial_file = log->file();
0N/A int partial_fd = open(partial_file, O_RDONLY);
0N/A if (partial_fd != -1) {
0N/A // print/print_cr may need to allocate large stack buffer to format
0N/A // strings, here we use snprintf() and print_raw() instead.
0N/A file->print_raw("<compilation_log thread='");
0N/A jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
0N/A file->print_raw(buf);
0N/A file->print_raw_cr("'>");
0N/A
0N/A size_t nr; // number read into buf from partial log
0N/A // Copy data up to the end of the last <event> element:
0N/A julong to_read = log->_file_end;
0N/A while (to_read > 0) {
0N/A if (to_read < (julong)buflen)
0N/A nr = (size_t)to_read;
0N/A else nr = buflen;
0N/A nr = read(partial_fd, buf, (int)nr);
0N/A if (nr <= 0) break;
0N/A to_read -= (julong)nr;
0N/A file->write(buf, nr);
0N/A }
0N/A
0N/A // Copy any remaining data inside a quote:
0N/A bool saw_slop = false;
0N/A int end_cdata = 0; // state machine [0..2] watching for too many "]]"
0N/A while ((nr = read(partial_fd, buf, buflen)) > 0) {
0N/A if (!saw_slop) {
0N/A file->print_raw_cr("<fragment>");
0N/A file->print_raw_cr("<![CDATA[");
0N/A saw_slop = true;
0N/A }
0N/A // The rest of this loop amounts to a simple copy operation:
0N/A // { file->write(buf, nr); }
0N/A // However, it must sometimes output the buffer in parts,
0N/A // in case there is a CDATA quote embedded in the fragment.
0N/A const char* bufp; // pointer into buf
0N/A size_t nw; // number written in each pass of the following loop:
0N/A for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
0N/A // Write up to any problematic CDATA delimiter (usually all of nr).
0N/A for (nw = 0; nw < nr; nw++) {
0N/A // First, scan ahead into the buf, checking the state machine.
0N/A switch (bufp[nw]) {
0N/A case ']':
0N/A if (end_cdata < 2) end_cdata += 1; // saturating counter
0N/A continue; // keep scanning
0N/A case '>':
0N/A if (end_cdata == 2) break; // found CDATA delimiter!
0N/A // else fall through:
0N/A default:
0N/A end_cdata = 0;
0N/A continue; // keep scanning
0N/A }
0N/A // If we get here, nw is pointing at a bad '>'.
0N/A // It is very rare for this to happen.
0N/A // However, this code has been tested by introducing
0N/A // CDATA sequences into the compilation log.
0N/A break;
0N/A }
0N/A // Now nw is the number of characters to write, usually == nr.
0N/A file->write(bufp, nw);
0N/A if (nw < nr) {
0N/A // We are about to go around the loop again.
0N/A // But first, disrupt the ]]> by closing and reopening the quote.
0N/A file->print_raw("]]><![CDATA[");
0N/A end_cdata = 0; // reset state machine
0N/A }
0N/A }
0N/A }
0N/A if (saw_slop) {
0N/A file->print_raw_cr("]]>");
0N/A file->print_raw_cr("</fragment>");
0N/A }
0N/A file->print_raw_cr("</compilation_log>");
0N/A close(partial_fd);
0N/A unlink(partial_file);
0N/A }
0N/A }
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// CompileLog::finish_log
0N/A//
0N/A// Called during normal shutdown. For now, any clean-up needed in normal
0N/A// shutdown is also needed in VM abort, so is covered by finish_log_on_error().
0N/A// Just allocate a buffer and call finish_log_on_error().
0N/Avoid CompileLog::finish_log(outputStream* file) {
0N/A char buf[4 * K];
0N/A finish_log_on_error(file, buf, sizeof(buf));
0N/A}
4027N/A
4027N/A// ------------------------------------------------------------------
4027N/A// CompileLog::inline_success
4027N/A//
4027N/A// Print about successful method inlining.
4027N/Avoid CompileLog::inline_success(const char* reason) {
4027N/A begin_elem("inline_success reason='");
4027N/A text(reason);
4027N/A end_elem("'");
4027N/A}
4027N/A
4027N/A// ------------------------------------------------------------------
4027N/A// CompileLog::inline_fail
4027N/A//
4027N/A// Print about failed method inlining.
4027N/Avoid CompileLog::inline_fail(const char* reason) {
4027N/A begin_elem("inline_fail reason='");
4027N/A text(reason);
4027N/A end_elem("'");
4027N/A}
4027N/A
4027N/A// ------------------------------------------------------------------
4027N/A// CompileLog::set_context
4027N/A//
4027N/A// Set XML tag as an optional marker - it is printed only if
4027N/A// there are other entries after until it is reset.
4027N/Avoid CompileLog::set_context(const char* format, ...) {
4027N/A va_list ap;
4027N/A va_start(ap, format);
4027N/A clear_context();
4027N/A _context.print("<");
4027N/A _context.vprint(format, ap);
4027N/A _context.print_cr("/>");
4027N/A va_end(ap);
4027N/A}
4027N/A
4027N/A// ------------------------------------------------------------------
4027N/A// CompileLog::code_cache_state
4027N/A//
4027N/A// Print code cache state.
4027N/Avoid CompileLog::code_cache_state() {
4027N/A begin_elem("code_cache");
4027N/A CodeCache::log_state(this);
4027N/A end_elem("");
4027N/A}