src/share/vm/compiler/compileLog.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "ci/ciMethod.hpp"
aoqi@0 27 #include "compiler/compileLog.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "oops/method.hpp"
aoqi@0 30 #include "runtime/mutexLocker.hpp"
aoqi@0 31 #include "runtime/os.hpp"
aoqi@0 32
aoqi@0 33 CompileLog* CompileLog::_first = NULL;
aoqi@0 34
aoqi@0 35 // ------------------------------------------------------------------
aoqi@0 36 // CompileLog::CompileLog
aoqi@0 37 CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id)
aoqi@0 38 : _context(_context_buffer, sizeof(_context_buffer))
aoqi@0 39 {
aoqi@0 40 initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp, true));
aoqi@0 41 _file_end = 0;
aoqi@0 42 _thread_id = thread_id;
aoqi@0 43
aoqi@0 44 _identities_limit = 0;
aoqi@0 45 _identities_capacity = 400;
aoqi@0 46 _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);
aoqi@0 47 _file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler);
aoqi@0 48 strcpy((char*)_file, file_name);
aoqi@0 49
aoqi@0 50 // link into the global list
aoqi@0 51 { MutexLocker locker(CompileTaskAlloc_lock);
aoqi@0 52 _next = _first;
aoqi@0 53 _first = this;
aoqi@0 54 }
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 CompileLog::~CompileLog() {
aoqi@0 58 delete _out;
aoqi@0 59 _out = NULL;
aoqi@0 60 FREE_C_HEAP_ARRAY(char, _identities, mtCompiler);
aoqi@0 61 FREE_C_HEAP_ARRAY(char, _file, mtCompiler);
aoqi@0 62 }
aoqi@0 63
aoqi@0 64
aoqi@0 65 // see_tag, pop_tag: Override the default do-nothing methods on xmlStream.
aoqi@0 66 // These methods provide a hook for managing the the extra context markup.
aoqi@0 67 void CompileLog::see_tag(const char* tag, bool push) {
aoqi@0 68 if (_context.size() > 0 && _out != NULL) {
aoqi@0 69 _out->write(_context.base(), _context.size());
aoqi@0 70 _context.reset();
aoqi@0 71 }
aoqi@0 72 xmlStream::see_tag(tag, push);
aoqi@0 73 }
aoqi@0 74 void CompileLog::pop_tag(const char* tag) {
aoqi@0 75 _context.reset(); // toss any context info.
aoqi@0 76 xmlStream::pop_tag(tag);
aoqi@0 77 }
aoqi@0 78
aoqi@0 79
aoqi@0 80 // ------------------------------------------------------------------
aoqi@0 81 // CompileLog::identify
aoqi@0 82 int CompileLog::identify(ciBaseObject* obj) {
aoqi@0 83 if (obj == NULL) return 0;
aoqi@0 84 int id = obj->ident();
aoqi@0 85 if (id < 0) return id;
aoqi@0 86 // If it has already been identified, just return the id.
aoqi@0 87 if (id < _identities_limit && _identities[id] != 0) return id;
aoqi@0 88 // Lengthen the array, if necessary.
aoqi@0 89 if (id >= _identities_capacity) {
aoqi@0 90 int new_cap = _identities_capacity * 2;
aoqi@0 91 if (new_cap <= id) new_cap = id + 100;
aoqi@0 92 _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);
aoqi@0 93 _identities_capacity = new_cap;
aoqi@0 94 }
aoqi@0 95 while (id >= _identities_limit) {
aoqi@0 96 _identities[_identities_limit++] = 0;
aoqi@0 97 }
aoqi@0 98 assert(id < _identities_limit, "oob");
aoqi@0 99 // Mark this id as processed.
aoqi@0 100 // (Be sure to do this before any recursive calls to identify.)
aoqi@0 101 _identities[id] = 1; // mark
aoqi@0 102
aoqi@0 103 // Now, print the object's identity once, in detail.
aoqi@0 104 if (obj->is_metadata()) {
aoqi@0 105 ciMetadata* mobj = obj->as_metadata();
aoqi@0 106 if (mobj->is_klass()) {
aoqi@0 107 ciKlass* klass = mobj->as_klass();
aoqi@0 108 begin_elem("klass id='%d'", id);
aoqi@0 109 name(klass->name());
aoqi@0 110 if (!klass->is_loaded()) {
aoqi@0 111 print(" unloaded='1'");
aoqi@0 112 } else {
aoqi@0 113 print(" flags='%d'", klass->modifier_flags());
aoqi@0 114 }
aoqi@0 115 end_elem();
aoqi@0 116 } else if (mobj->is_method()) {
aoqi@0 117 ciMethod* method = mobj->as_method();
aoqi@0 118 ciSignature* sig = method->signature();
aoqi@0 119 // Pre-identify items that we will need!
aoqi@0 120 identify(sig->return_type());
aoqi@0 121 for (int i = 0; i < sig->count(); i++) {
aoqi@0 122 identify(sig->type_at(i));
aoqi@0 123 }
aoqi@0 124 begin_elem("method id='%d' holder='%d'",
aoqi@0 125 id, identify(method->holder()));
aoqi@0 126 name(method->name());
aoqi@0 127 print(" return='%d'", identify(sig->return_type()));
aoqi@0 128 if (sig->count() > 0) {
aoqi@0 129 print(" arguments='");
aoqi@0 130 for (int i = 0; i < sig->count(); i++) {
aoqi@0 131 print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
aoqi@0 132 }
aoqi@0 133 print("'");
aoqi@0 134 }
aoqi@0 135 if (!method->is_loaded()) {
aoqi@0 136 print(" unloaded='1'");
aoqi@0 137 } else {
aoqi@0 138 print(" flags='%d'", (jchar) method->flags().as_int());
aoqi@0 139 // output a few metrics
aoqi@0 140 print(" bytes='%d'", method->code_size());
aoqi@0 141 method->log_nmethod_identity(this);
aoqi@0 142 //print(" count='%d'", method->invocation_count());
aoqi@0 143 //int bec = method->backedge_count();
aoqi@0 144 //if (bec != 0) print(" backedge_count='%d'", bec);
aoqi@0 145 print(" iicount='%d'", method->interpreter_invocation_count());
aoqi@0 146 }
aoqi@0 147 end_elem();
aoqi@0 148 } else if (mobj->is_type()) {
aoqi@0 149 BasicType type = mobj->as_type()->basic_type();
aoqi@0 150 elem("type id='%d' name='%s'", id, type2name(type));
aoqi@0 151 } else {
aoqi@0 152 // Should not happen.
aoqi@0 153 elem("unknown id='%d'", id);
aoqi@0 154 ShouldNotReachHere();
aoqi@0 155 }
aoqi@0 156 } else if (obj->is_symbol()) {
aoqi@0 157 begin_elem("symbol id='%d'", id);
aoqi@0 158 name(obj->as_symbol());
aoqi@0 159 end_elem();
aoqi@0 160 } else {
aoqi@0 161 // Should not happen.
aoqi@0 162 elem("unknown id='%d'", id);
aoqi@0 163 }
aoqi@0 164 return id;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 void CompileLog::name(ciSymbol* name) {
aoqi@0 168 if (name == NULL) return;
aoqi@0 169 print(" name='");
aoqi@0 170 name->print_symbol_on(text()); // handles quoting conventions
aoqi@0 171 print("'");
aoqi@0 172 }
aoqi@0 173
aoqi@0 174
aoqi@0 175 // ------------------------------------------------------------------
aoqi@0 176 // CompileLog::clear_identities
aoqi@0 177 // Forget which identities have been printed.
aoqi@0 178 void CompileLog::clear_identities() {
aoqi@0 179 _identities_limit = 0;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 // ------------------------------------------------------------------
aoqi@0 183 // CompileLog::finish_log_on_error
aoqi@0 184 //
aoqi@0 185 // Note: This function is called after fatal error, avoid unnecessary memory
aoqi@0 186 // or stack allocation, use only async-safe functions. It's possible JVM is
aoqi@0 187 // only partially initialized.
aoqi@0 188 void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
aoqi@0 189 static bool called_exit = false;
aoqi@0 190 if (called_exit) return;
aoqi@0 191 called_exit = true;
aoqi@0 192
aoqi@0 193 CompileLog* log = _first;
aoqi@0 194 while (log != NULL) {
aoqi@0 195 log->flush();
aoqi@0 196 const char* partial_file = log->file();
aoqi@0 197 int partial_fd = open(partial_file, O_RDONLY);
aoqi@0 198 if (partial_fd != -1) {
aoqi@0 199 // print/print_cr may need to allocate large stack buffer to format
aoqi@0 200 // strings, here we use snprintf() and print_raw() instead.
aoqi@0 201 file->print_raw("<compilation_log thread='");
aoqi@0 202 jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
aoqi@0 203 file->print_raw(buf);
aoqi@0 204 file->print_raw_cr("'>");
aoqi@0 205
aoqi@0 206 size_t nr; // number read into buf from partial log
aoqi@0 207 // Copy data up to the end of the last <event> element:
aoqi@0 208 julong to_read = log->_file_end;
aoqi@0 209 while (to_read > 0) {
aoqi@0 210 if (to_read < (julong)buflen)
aoqi@0 211 nr = (size_t)to_read;
aoqi@0 212 else nr = buflen;
aoqi@0 213 nr = read(partial_fd, buf, (int)nr);
aoqi@0 214 if (nr <= 0) break;
aoqi@0 215 to_read -= (julong)nr;
aoqi@0 216 file->write(buf, nr);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Copy any remaining data inside a quote:
aoqi@0 220 bool saw_slop = false;
aoqi@0 221 int end_cdata = 0; // state machine [0..2] watching for too many "]]"
aoqi@0 222 while ((nr = read(partial_fd, buf, buflen)) > 0) {
aoqi@0 223 if (!saw_slop) {
aoqi@0 224 file->print_raw_cr("<fragment>");
aoqi@0 225 file->print_raw_cr("<![CDATA[");
aoqi@0 226 saw_slop = true;
aoqi@0 227 }
aoqi@0 228 // The rest of this loop amounts to a simple copy operation:
aoqi@0 229 // { file->write(buf, nr); }
aoqi@0 230 // However, it must sometimes output the buffer in parts,
aoqi@0 231 // in case there is a CDATA quote embedded in the fragment.
aoqi@0 232 const char* bufp; // pointer into buf
aoqi@0 233 size_t nw; // number written in each pass of the following loop:
aoqi@0 234 for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
aoqi@0 235 // Write up to any problematic CDATA delimiter (usually all of nr).
aoqi@0 236 for (nw = 0; nw < nr; nw++) {
aoqi@0 237 // First, scan ahead into the buf, checking the state machine.
aoqi@0 238 switch (bufp[nw]) {
aoqi@0 239 case ']':
aoqi@0 240 if (end_cdata < 2) end_cdata += 1; // saturating counter
aoqi@0 241 continue; // keep scanning
aoqi@0 242 case '>':
aoqi@0 243 if (end_cdata == 2) break; // found CDATA delimiter!
aoqi@0 244 // else fall through:
aoqi@0 245 default:
aoqi@0 246 end_cdata = 0;
aoqi@0 247 continue; // keep scanning
aoqi@0 248 }
aoqi@0 249 // If we get here, nw is pointing at a bad '>'.
aoqi@0 250 // It is very rare for this to happen.
aoqi@0 251 // However, this code has been tested by introducing
aoqi@0 252 // CDATA sequences into the compilation log.
aoqi@0 253 break;
aoqi@0 254 }
aoqi@0 255 // Now nw is the number of characters to write, usually == nr.
aoqi@0 256 file->write(bufp, nw);
aoqi@0 257 if (nw < nr) {
aoqi@0 258 // We are about to go around the loop again.
aoqi@0 259 // But first, disrupt the ]]> by closing and reopening the quote.
aoqi@0 260 file->print_raw("]]><![CDATA[");
aoqi@0 261 end_cdata = 0; // reset state machine
aoqi@0 262 }
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265 if (saw_slop) {
aoqi@0 266 file->print_raw_cr("]]>");
aoqi@0 267 file->print_raw_cr("</fragment>");
aoqi@0 268 }
aoqi@0 269 file->print_raw_cr("</compilation_log>");
aoqi@0 270 close(partial_fd);
aoqi@0 271 unlink(partial_file);
aoqi@0 272 }
aoqi@0 273 CompileLog* next_log = log->_next;
aoqi@0 274 delete log;
aoqi@0 275 log = next_log;
aoqi@0 276 }
aoqi@0 277 _first = NULL;
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 // ------------------------------------------------------------------
aoqi@0 281 // CompileLog::finish_log
aoqi@0 282 //
aoqi@0 283 // Called during normal shutdown. For now, any clean-up needed in normal
aoqi@0 284 // shutdown is also needed in VM abort, so is covered by finish_log_on_error().
aoqi@0 285 // Just allocate a buffer and call finish_log_on_error().
aoqi@0 286 void CompileLog::finish_log(outputStream* file) {
aoqi@0 287 char buf[4 * K];
aoqi@0 288 finish_log_on_error(file, buf, sizeof(buf));
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 // ------------------------------------------------------------------
aoqi@0 292 // CompileLog::inline_success
aoqi@0 293 //
aoqi@0 294 // Print about successful method inlining.
aoqi@0 295 void CompileLog::inline_success(const char* reason) {
aoqi@0 296 begin_elem("inline_success reason='");
aoqi@0 297 text("%s", reason);
aoqi@0 298 end_elem("'");
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 // ------------------------------------------------------------------
aoqi@0 302 // CompileLog::inline_fail
aoqi@0 303 //
aoqi@0 304 // Print about failed method inlining.
aoqi@0 305 void CompileLog::inline_fail(const char* reason) {
aoqi@0 306 begin_elem("inline_fail reason='");
aoqi@0 307 text("%s", reason);
aoqi@0 308 end_elem("'");
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 // ------------------------------------------------------------------
aoqi@0 312 // CompileLog::set_context
aoqi@0 313 //
aoqi@0 314 // Set XML tag as an optional marker - it is printed only if
aoqi@0 315 // there are other entries after until it is reset.
aoqi@0 316 void CompileLog::set_context(const char* format, ...) {
aoqi@0 317 va_list ap;
aoqi@0 318 va_start(ap, format);
aoqi@0 319 clear_context();
aoqi@0 320 _context.print("<");
aoqi@0 321 _context.vprint(format, ap);
aoqi@0 322 _context.print_cr("/>");
aoqi@0 323 va_end(ap);
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 // ------------------------------------------------------------------
aoqi@0 327 // CompileLog::code_cache_state
aoqi@0 328 //
aoqi@0 329 // Print code cache state.
aoqi@0 330 void CompileLog::code_cache_state() {
aoqi@0 331 begin_elem("code_cache");
aoqi@0 332 CodeCache::log_state(this);
aoqi@0 333 end_elem("%s", "");
aoqi@0 334 }

mercurial