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