src/share/vm/compiler/compileLog.cpp

Wed, 22 Jan 2014 17:42:23 -0800

author
kvn
date
Wed, 22 Jan 2014 17:42:23 -0800
changeset 6503
a9becfeecd1b
parent 6198
55fb97c4c58d
child 6680
78bbf4d43a14
permissions
-rw-r--r--

Merge

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

mercurial