src/share/vm/compiler/compileLog.cpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 3900
d2a62e0f25eb
child 4111
9191895df19d
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

duke@435 1 /*
coleenp@4037 2 * Copyright (c) 2002, 2012, 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
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 {
zgu@3900 40 initialize(new(ResourceObj::C_HEAP, mtCompiler) 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;
zgu@3900 47 _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler);
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;
zgu@3900 59 FREE_C_HEAP_ARRAY(char, _identities, mtCompiler);
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
coleenp@4037 102 int CompileLog::identify(ciBaseObject* 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;
zgu@3900 112 _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler);
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.
coleenp@4037 124 if (obj->is_metadata()) {
coleenp@4037 125 ciMetadata* mobj = obj->as_metadata();
coleenp@4037 126 if (mobj->is_klass()) {
coleenp@4037 127 ciKlass* klass = mobj->as_klass();
duke@435 128 begin_elem("klass id='%d'", id);
duke@435 129 name(klass->name());
duke@435 130 if (!klass->is_loaded()) {
duke@435 131 print(" unloaded='1'");
duke@435 132 } else {
duke@435 133 print(" flags='%d'", klass->modifier_flags());
duke@435 134 }
duke@435 135 end_elem();
coleenp@4037 136 } else if (mobj->is_method()) {
coleenp@4037 137 ciMethod* method = mobj->as_method();
duke@435 138 ciSignature* sig = method->signature();
duke@435 139 // Pre-identify items that we will need!
duke@435 140 identify(sig->return_type());
duke@435 141 for (int i = 0; i < sig->count(); i++) {
duke@435 142 identify(sig->type_at(i));
duke@435 143 }
duke@435 144 begin_elem("method id='%d' holder='%d'",
duke@435 145 id, identify(method->holder()));
duke@435 146 name(method->name());
duke@435 147 print(" return='%d'", identify(sig->return_type()));
duke@435 148 if (sig->count() > 0) {
duke@435 149 print(" arguments='");
duke@435 150 for (int i = 0; i < sig->count(); i++) {
duke@435 151 print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
duke@435 152 }
duke@435 153 print("'");
duke@435 154 }
duke@435 155 if (!method->is_loaded()) {
duke@435 156 print(" unloaded='1'");
duke@435 157 } else {
duke@435 158 print(" flags='%d'", (jchar) method->flags().as_int());
duke@435 159 // output a few metrics
duke@435 160 print(" bytes='%d'", method->code_size());
duke@435 161 method->log_nmethod_identity(this);
duke@435 162 //print(" count='%d'", method->invocation_count());
duke@435 163 //int bec = method->backedge_count();
duke@435 164 //if (bec != 0) print(" backedge_count='%d'", bec);
duke@435 165 print(" iicount='%d'", method->interpreter_invocation_count());
duke@435 166 }
duke@435 167 end_elem();
coleenp@4037 168 } else if (mobj->is_type()) {
coleenp@4037 169 BasicType type = mobj->as_type()->basic_type();
coleenp@4037 170 elem("type id='%d' name='%s'", id, type2name(type));
coleenp@4037 171 } else {
coleenp@4037 172 // Should not happen.
coleenp@4037 173 elem("unknown id='%d'", id);
coleenp@4037 174 ShouldNotReachHere();
coleenp@4037 175 }
duke@435 176 } else if (obj->is_symbol()) {
duke@435 177 begin_elem("symbol id='%d'", id);
duke@435 178 name(obj->as_symbol());
duke@435 179 end_elem();
duke@435 180 } else {
duke@435 181 // Should not happen.
duke@435 182 elem("unknown id='%d'", id);
duke@435 183 }
duke@435 184 return id;
duke@435 185 }
duke@435 186
duke@435 187 void CompileLog::name(ciSymbol* name) {
duke@435 188 if (name == NULL) return;
duke@435 189 print(" name='");
duke@435 190 name->print_symbol_on(text()); // handles quoting conventions
duke@435 191 print("'");
duke@435 192 }
duke@435 193
duke@435 194
duke@435 195 // ------------------------------------------------------------------
duke@435 196 // CompileLog::clear_identities
duke@435 197 // Forget which identities have been printed.
duke@435 198 void CompileLog::clear_identities() {
duke@435 199 _identities_limit = 0;
duke@435 200 }
duke@435 201
duke@435 202 // ------------------------------------------------------------------
duke@435 203 // CompileLog::finish_log_on_error
duke@435 204 //
duke@435 205 // Note: This function is called after fatal error, avoid unnecessary memory
duke@435 206 // or stack allocation, use only async-safe functions. It's possible JVM is
duke@435 207 // only partially initialized.
duke@435 208 void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
duke@435 209 static bool called_exit = false;
duke@435 210 if (called_exit) return;
duke@435 211 called_exit = true;
duke@435 212
duke@435 213 for (CompileLog* log = _first; log != NULL; log = log->_next) {
duke@435 214 log->flush();
duke@435 215 const char* partial_file = log->file();
duke@435 216 int partial_fd = open(partial_file, O_RDONLY);
duke@435 217 if (partial_fd != -1) {
duke@435 218 // print/print_cr may need to allocate large stack buffer to format
duke@435 219 // strings, here we use snprintf() and print_raw() instead.
duke@435 220 file->print_raw("<compilation_log thread='");
duke@435 221 jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
duke@435 222 file->print_raw(buf);
duke@435 223 file->print_raw_cr("'>");
duke@435 224
duke@435 225 size_t nr; // number read into buf from partial log
duke@435 226 // Copy data up to the end of the last <event> element:
duke@435 227 julong to_read = log->_file_end;
duke@435 228 while (to_read > 0) {
duke@435 229 if (to_read < (julong)buflen)
duke@435 230 nr = (size_t)to_read;
duke@435 231 else nr = buflen;
duke@435 232 nr = read(partial_fd, buf, (int)nr);
duke@435 233 if (nr <= 0) break;
duke@435 234 to_read -= (julong)nr;
duke@435 235 file->write(buf, nr);
duke@435 236 }
duke@435 237
duke@435 238 // Copy any remaining data inside a quote:
duke@435 239 bool saw_slop = false;
duke@435 240 int end_cdata = 0; // state machine [0..2] watching for too many "]]"
duke@435 241 while ((nr = read(partial_fd, buf, buflen)) > 0) {
duke@435 242 if (!saw_slop) {
duke@435 243 file->print_raw_cr("<fragment>");
duke@435 244 file->print_raw_cr("<![CDATA[");
duke@435 245 saw_slop = true;
duke@435 246 }
duke@435 247 // The rest of this loop amounts to a simple copy operation:
duke@435 248 // { file->write(buf, nr); }
duke@435 249 // However, it must sometimes output the buffer in parts,
duke@435 250 // in case there is a CDATA quote embedded in the fragment.
duke@435 251 const char* bufp; // pointer into buf
duke@435 252 size_t nw; // number written in each pass of the following loop:
duke@435 253 for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
duke@435 254 // Write up to any problematic CDATA delimiter (usually all of nr).
duke@435 255 for (nw = 0; nw < nr; nw++) {
duke@435 256 // First, scan ahead into the buf, checking the state machine.
duke@435 257 switch (bufp[nw]) {
duke@435 258 case ']':
duke@435 259 if (end_cdata < 2) end_cdata += 1; // saturating counter
duke@435 260 continue; // keep scanning
duke@435 261 case '>':
duke@435 262 if (end_cdata == 2) break; // found CDATA delimiter!
duke@435 263 // else fall through:
duke@435 264 default:
duke@435 265 end_cdata = 0;
duke@435 266 continue; // keep scanning
duke@435 267 }
duke@435 268 // If we get here, nw is pointing at a bad '>'.
duke@435 269 // It is very rare for this to happen.
duke@435 270 // However, this code has been tested by introducing
duke@435 271 // CDATA sequences into the compilation log.
duke@435 272 break;
duke@435 273 }
duke@435 274 // Now nw is the number of characters to write, usually == nr.
duke@435 275 file->write(bufp, nw);
duke@435 276 if (nw < nr) {
duke@435 277 // We are about to go around the loop again.
duke@435 278 // But first, disrupt the ]]> by closing and reopening the quote.
duke@435 279 file->print_raw("]]><![CDATA[");
duke@435 280 end_cdata = 0; // reset state machine
duke@435 281 }
duke@435 282 }
duke@435 283 }
duke@435 284 if (saw_slop) {
duke@435 285 file->print_raw_cr("]]>");
duke@435 286 file->print_raw_cr("</fragment>");
duke@435 287 }
duke@435 288 file->print_raw_cr("</compilation_log>");
duke@435 289 close(partial_fd);
duke@435 290 unlink(partial_file);
duke@435 291 }
duke@435 292 }
duke@435 293 }
duke@435 294
duke@435 295 // ------------------------------------------------------------------
duke@435 296 // CompileLog::finish_log
duke@435 297 //
duke@435 298 // Called during normal shutdown. For now, any clean-up needed in normal
duke@435 299 // shutdown is also needed in VM abort, so is covered by finish_log_on_error().
duke@435 300 // Just allocate a buffer and call finish_log_on_error().
duke@435 301 void CompileLog::finish_log(outputStream* file) {
duke@435 302 char buf[4 * K];
duke@435 303 finish_log_on_error(file, buf, sizeof(buf));
duke@435 304 }

mercurial