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