src/share/vm/compiler/compileLog.cpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 1907
c18cbe5936b8
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

duke@435 1 /*
stefank@2314 2 * Copyright (c) 2002, 2010, 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"
stefank@2314 29 #include "oops/methodOop.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
duke@435 37 CompileLog::CompileLog(const char* file, FILE* fp, intx thread_id)
duke@435 38 : _context(_context_buffer, sizeof(_context_buffer))
duke@435 39 {
duke@435 40 initialize(new(ResourceObj::C_HEAP) fileStream(fp));
duke@435 41 _file = file;
duke@435 42 _file_end = 0;
duke@435 43 _thread_id = thread_id;
duke@435 44
duke@435 45 _identities_limit = 0;
duke@435 46 _identities_capacity = 400;
duke@435 47 _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity);
duke@435 48
duke@435 49 // link into the global list
duke@435 50 { MutexLocker locker(CompileTaskAlloc_lock);
duke@435 51 _next = _first;
duke@435 52 _first = this;
duke@435 53 }
duke@435 54 }
duke@435 55
duke@435 56 CompileLog::~CompileLog() {
duke@435 57 delete _out;
duke@435 58 _out = NULL;
duke@435 59 FREE_C_HEAP_ARRAY(char, _identities);
duke@435 60 }
duke@435 61
duke@435 62
duke@435 63 // Advance kind up to a null or space, return this tail.
duke@435 64 // Make sure kind is null-terminated, not space-terminated.
duke@435 65 // Use the buffer if necessary.
duke@435 66 static const char* split_attrs(const char* &kind, char* buffer) {
duke@435 67 const char* attrs = strchr(kind, ' ');
duke@435 68 // Tease apart the first word from the rest:
duke@435 69 if (attrs == NULL) {
duke@435 70 return ""; // no attrs, no split
duke@435 71 } else if (kind == buffer) {
duke@435 72 ((char*) attrs)[-1] = 0;
duke@435 73 return attrs;
duke@435 74 } else {
duke@435 75 // park it in the buffer, so we can put a null on the end
jcoomes@1844 76 assert(!(kind >= buffer && kind < buffer+100), "not obviously in buffer");
duke@435 77 int klen = attrs - kind;
duke@435 78 strncpy(buffer, kind, klen);
duke@435 79 buffer[klen] = 0;
duke@435 80 kind = buffer; // return by reference
duke@435 81 return attrs;
duke@435 82 }
duke@435 83 }
duke@435 84
duke@435 85 // see_tag, pop_tag: Override the default do-nothing methods on xmlStream.
duke@435 86 // These methods provide a hook for managing the the extra context markup.
duke@435 87 void CompileLog::see_tag(const char* tag, bool push) {
duke@435 88 if (_context.size() > 0 && _out != NULL) {
duke@435 89 _out->write(_context.base(), _context.size());
duke@435 90 _context.reset();
duke@435 91 }
duke@435 92 xmlStream::see_tag(tag, push);
duke@435 93 }
duke@435 94 void CompileLog::pop_tag(const char* tag) {
duke@435 95 _context.reset(); // toss any context info.
duke@435 96 xmlStream::pop_tag(tag);
duke@435 97 }
duke@435 98
duke@435 99
duke@435 100 // ------------------------------------------------------------------
duke@435 101 // CompileLog::identify
duke@435 102 int CompileLog::identify(ciObject* obj) {
duke@435 103 if (obj == NULL) return 0;
duke@435 104 int id = obj->ident();
duke@435 105 if (id < 0) return id;
duke@435 106 // If it has already been identified, just return the id.
duke@435 107 if (id < _identities_limit && _identities[id] != 0) return id;
duke@435 108 // Lengthen the array, if necessary.
duke@435 109 if (id >= _identities_capacity) {
duke@435 110 int new_cap = _identities_capacity * 2;
duke@435 111 if (new_cap <= id) new_cap = id + 100;
duke@435 112 _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap);
duke@435 113 _identities_capacity = new_cap;
duke@435 114 }
duke@435 115 while (id >= _identities_limit) {
duke@435 116 _identities[_identities_limit++] = 0;
duke@435 117 }
duke@435 118 assert(id < _identities_limit, "oob");
duke@435 119 // Mark this id as processed.
duke@435 120 // (Be sure to do this before any recursive calls to identify.)
duke@435 121 _identities[id] = 1; // mark
duke@435 122
duke@435 123 // Now, print the object's identity once, in detail.
duke@435 124 if (obj->is_klass()) {
duke@435 125 ciKlass* klass = obj->as_klass();
duke@435 126 begin_elem("klass id='%d'", id);
duke@435 127 name(klass->name());
duke@435 128 if (!klass->is_loaded()) {
duke@435 129 print(" unloaded='1'");
duke@435 130 } else {
duke@435 131 print(" flags='%d'", klass->modifier_flags());
duke@435 132 }
duke@435 133 end_elem();
duke@435 134 } else if (obj->is_method()) {
duke@435 135 ciMethod* method = obj->as_method();
duke@435 136 ciSignature* sig = method->signature();
duke@435 137 // Pre-identify items that we will need!
duke@435 138 identify(sig->return_type());
duke@435 139 for (int i = 0; i < sig->count(); i++) {
duke@435 140 identify(sig->type_at(i));
duke@435 141 }
duke@435 142 begin_elem("method id='%d' holder='%d'",
duke@435 143 id, identify(method->holder()));
duke@435 144 name(method->name());
duke@435 145 print(" return='%d'", identify(sig->return_type()));
duke@435 146 if (sig->count() > 0) {
duke@435 147 print(" arguments='");
duke@435 148 for (int i = 0; i < sig->count(); i++) {
duke@435 149 print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
duke@435 150 }
duke@435 151 print("'");
duke@435 152 }
duke@435 153 if (!method->is_loaded()) {
duke@435 154 print(" unloaded='1'");
duke@435 155 } else {
duke@435 156 print(" flags='%d'", (jchar) method->flags().as_int());
duke@435 157 // output a few metrics
duke@435 158 print(" bytes='%d'", method->code_size());
duke@435 159 method->log_nmethod_identity(this);
duke@435 160 //print(" count='%d'", method->invocation_count());
duke@435 161 //int bec = method->backedge_count();
duke@435 162 //if (bec != 0) print(" backedge_count='%d'", bec);
duke@435 163 print(" iicount='%d'", method->interpreter_invocation_count());
duke@435 164 }
duke@435 165 end_elem();
duke@435 166 } else if (obj->is_symbol()) {
duke@435 167 begin_elem("symbol id='%d'", id);
duke@435 168 name(obj->as_symbol());
duke@435 169 end_elem();
duke@435 170 } else if (obj->is_null_object()) {
duke@435 171 elem("null_object id='%d'", id);
duke@435 172 } else if (obj->is_type()) {
duke@435 173 BasicType type = obj->as_type()->basic_type();
duke@435 174 elem("type id='%d' name='%s'", id, type2name(type));
duke@435 175 } else {
duke@435 176 // Should not happen.
duke@435 177 elem("unknown id='%d'", id);
duke@435 178 }
duke@435 179 return id;
duke@435 180 }
duke@435 181
duke@435 182 void CompileLog::name(ciSymbol* name) {
duke@435 183 if (name == NULL) return;
duke@435 184 print(" name='");
duke@435 185 name->print_symbol_on(text()); // handles quoting conventions
duke@435 186 print("'");
duke@435 187 }
duke@435 188
duke@435 189
duke@435 190 // ------------------------------------------------------------------
duke@435 191 // CompileLog::clear_identities
duke@435 192 // Forget which identities have been printed.
duke@435 193 void CompileLog::clear_identities() {
duke@435 194 _identities_limit = 0;
duke@435 195 }
duke@435 196
duke@435 197 // ------------------------------------------------------------------
duke@435 198 // CompileLog::finish_log_on_error
duke@435 199 //
duke@435 200 // Note: This function is called after fatal error, avoid unnecessary memory
duke@435 201 // or stack allocation, use only async-safe functions. It's possible JVM is
duke@435 202 // only partially initialized.
duke@435 203 void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
duke@435 204 static bool called_exit = false;
duke@435 205 if (called_exit) return;
duke@435 206 called_exit = true;
duke@435 207
duke@435 208 for (CompileLog* log = _first; log != NULL; log = log->_next) {
duke@435 209 log->flush();
duke@435 210 const char* partial_file = log->file();
duke@435 211 int partial_fd = open(partial_file, O_RDONLY);
duke@435 212 if (partial_fd != -1) {
duke@435 213 // print/print_cr may need to allocate large stack buffer to format
duke@435 214 // strings, here we use snprintf() and print_raw() instead.
duke@435 215 file->print_raw("<compilation_log thread='");
duke@435 216 jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
duke@435 217 file->print_raw(buf);
duke@435 218 file->print_raw_cr("'>");
duke@435 219
duke@435 220 size_t nr; // number read into buf from partial log
duke@435 221 // Copy data up to the end of the last <event> element:
duke@435 222 julong to_read = log->_file_end;
duke@435 223 while (to_read > 0) {
duke@435 224 if (to_read < (julong)buflen)
duke@435 225 nr = (size_t)to_read;
duke@435 226 else nr = buflen;
duke@435 227 nr = read(partial_fd, buf, (int)nr);
duke@435 228 if (nr <= 0) break;
duke@435 229 to_read -= (julong)nr;
duke@435 230 file->write(buf, nr);
duke@435 231 }
duke@435 232
duke@435 233 // Copy any remaining data inside a quote:
duke@435 234 bool saw_slop = false;
duke@435 235 int end_cdata = 0; // state machine [0..2] watching for too many "]]"
duke@435 236 while ((nr = read(partial_fd, buf, buflen)) > 0) {
duke@435 237 if (!saw_slop) {
duke@435 238 file->print_raw_cr("<fragment>");
duke@435 239 file->print_raw_cr("<![CDATA[");
duke@435 240 saw_slop = true;
duke@435 241 }
duke@435 242 // The rest of this loop amounts to a simple copy operation:
duke@435 243 // { file->write(buf, nr); }
duke@435 244 // However, it must sometimes output the buffer in parts,
duke@435 245 // in case there is a CDATA quote embedded in the fragment.
duke@435 246 const char* bufp; // pointer into buf
duke@435 247 size_t nw; // number written in each pass of the following loop:
duke@435 248 for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
duke@435 249 // Write up to any problematic CDATA delimiter (usually all of nr).
duke@435 250 for (nw = 0; nw < nr; nw++) {
duke@435 251 // First, scan ahead into the buf, checking the state machine.
duke@435 252 switch (bufp[nw]) {
duke@435 253 case ']':
duke@435 254 if (end_cdata < 2) end_cdata += 1; // saturating counter
duke@435 255 continue; // keep scanning
duke@435 256 case '>':
duke@435 257 if (end_cdata == 2) break; // found CDATA delimiter!
duke@435 258 // else fall through:
duke@435 259 default:
duke@435 260 end_cdata = 0;
duke@435 261 continue; // keep scanning
duke@435 262 }
duke@435 263 // If we get here, nw is pointing at a bad '>'.
duke@435 264 // It is very rare for this to happen.
duke@435 265 // However, this code has been tested by introducing
duke@435 266 // CDATA sequences into the compilation log.
duke@435 267 break;
duke@435 268 }
duke@435 269 // Now nw is the number of characters to write, usually == nr.
duke@435 270 file->write(bufp, nw);
duke@435 271 if (nw < nr) {
duke@435 272 // We are about to go around the loop again.
duke@435 273 // But first, disrupt the ]]> by closing and reopening the quote.
duke@435 274 file->print_raw("]]><![CDATA[");
duke@435 275 end_cdata = 0; // reset state machine
duke@435 276 }
duke@435 277 }
duke@435 278 }
duke@435 279 if (saw_slop) {
duke@435 280 file->print_raw_cr("]]>");
duke@435 281 file->print_raw_cr("</fragment>");
duke@435 282 }
duke@435 283 file->print_raw_cr("</compilation_log>");
duke@435 284 close(partial_fd);
duke@435 285 unlink(partial_file);
duke@435 286 }
duke@435 287 }
duke@435 288 }
duke@435 289
duke@435 290 // ------------------------------------------------------------------
duke@435 291 // CompileLog::finish_log
duke@435 292 //
duke@435 293 // Called during normal shutdown. For now, any clean-up needed in normal
duke@435 294 // shutdown is also needed in VM abort, so is covered by finish_log_on_error().
duke@435 295 // Just allocate a buffer and call finish_log_on_error().
duke@435 296 void CompileLog::finish_log(outputStream* file) {
duke@435 297 char buf[4 * K];
duke@435 298 finish_log_on_error(file, buf, sizeof(buf));
duke@435 299 }

mercurial