src/share/vm/ci/ciReplay.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
equal deleted inserted replaced
-1:000000000000 0:f90c822e73f8
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 *
22 */
23
24 #include "precompiled.hpp"
25 #include "ci/ciMethodData.hpp"
26 #include "ci/ciReplay.hpp"
27 #include "ci/ciSymbol.hpp"
28 #include "ci/ciKlass.hpp"
29 #include "ci/ciUtilities.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/oopFactory.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "utilities/copy.hpp"
35 #include "utilities/macros.hpp"
36
37 #ifndef PRODUCT
38
39 // ciReplay
40
41 typedef struct _ciMethodDataRecord {
42 const char* _klass_name;
43 const char* _method_name;
44 const char* _signature;
45
46 int _state;
47 int _current_mileage;
48
49 intptr_t* _data;
50 char* _orig_data;
51 jobject* _oops_handles;
52 int* _oops_offsets;
53 int _data_length;
54 int _orig_data_length;
55 int _oops_length;
56 } ciMethodDataRecord;
57
58 typedef struct _ciMethodRecord {
59 const char* _klass_name;
60 const char* _method_name;
61 const char* _signature;
62
63 int _instructions_size;
64 int _interpreter_invocation_count;
65 int _interpreter_throwout_count;
66 int _invocation_counter;
67 int _backedge_counter;
68 } ciMethodRecord;
69
70 typedef struct _ciInlineRecord {
71 const char* _klass_name;
72 const char* _method_name;
73 const char* _signature;
74
75 int _inline_depth;
76 int _inline_bci;
77 } ciInlineRecord;
78
79 class CompileReplay;
80 static CompileReplay* replay_state;
81
82 class CompileReplay : public StackObj {
83 private:
84 FILE* _stream;
85 Thread* _thread;
86 Handle _protection_domain;
87 Handle _loader;
88
89 GrowableArray<ciMethodRecord*> _ci_method_records;
90 GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
91
92 // Use pointer because we may need to return inline records
93 // without destroying them.
94 GrowableArray<ciInlineRecord*>* _ci_inline_records;
95
96 const char* _error_message;
97
98 char* _bufptr;
99 char* _buffer;
100 int _buffer_length;
101 int _buffer_pos;
102
103 // "compile" data
104 ciKlass* _iklass;
105 Method* _imethod;
106 int _entry_bci;
107 int _comp_level;
108
109 public:
110 CompileReplay(const char* filename, TRAPS) {
111 _thread = THREAD;
112 _loader = Handle(_thread, SystemDictionary::java_system_loader());
113 _protection_domain = Handle();
114
115 _stream = fopen(filename, "rt");
116 if (_stream == NULL) {
117 fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
118 }
119
120 _ci_inline_records = NULL;
121 _error_message = NULL;
122
123 _buffer_length = 32;
124 _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
125 _bufptr = _buffer;
126 _buffer_pos = 0;
127
128 _imethod = NULL;
129 _iklass = NULL;
130 _entry_bci = 0;
131 _comp_level = 0;
132
133 test();
134 }
135
136 ~CompileReplay() {
137 if (_stream != NULL) fclose(_stream);
138 }
139
140 void test() {
141 strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
142 _bufptr = _buffer;
143 assert(parse_int("test") == 1, "what");
144 assert(parse_int("test") == 2, "what");
145 assert(strcmp(parse_string(), "foo") == 0, "what");
146 assert(parse_int("test") == 4, "what");
147 assert(strcmp(parse_string(), "bar") == 0, "what");
148 assert(parse_intptr_t("test") == 9, "what");
149 assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
150 }
151
152 bool had_error() {
153 return _error_message != NULL || _thread->has_pending_exception();
154 }
155
156 bool can_replay() {
157 return !(_stream == NULL || had_error());
158 }
159
160 void report_error(const char* msg) {
161 _error_message = msg;
162 // Restore the _buffer contents for error reporting
163 for (int i = 0; i < _buffer_pos; i++) {
164 if (_buffer[i] == '\0') _buffer[i] = ' ';
165 }
166 }
167
168 int parse_int(const char* label) {
169 if (had_error()) {
170 return 0;
171 }
172
173 int v = 0;
174 int read;
175 if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
176 report_error(label);
177 } else {
178 _bufptr += read;
179 }
180 return v;
181 }
182
183 intptr_t parse_intptr_t(const char* label) {
184 if (had_error()) {
185 return 0;
186 }
187
188 intptr_t v = 0;
189 int read;
190 if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
191 report_error(label);
192 } else {
193 _bufptr += read;
194 }
195 return v;
196 }
197
198 void skip_ws() {
199 // Skip any leading whitespace
200 while (*_bufptr == ' ' || *_bufptr == '\t') {
201 _bufptr++;
202 }
203 }
204
205
206 char* scan_and_terminate(char delim) {
207 char* str = _bufptr;
208 while (*_bufptr != delim && *_bufptr != '\0') {
209 _bufptr++;
210 }
211 if (*_bufptr != '\0') {
212 *_bufptr++ = '\0';
213 }
214 if (_bufptr == str) {
215 // nothing here
216 return NULL;
217 }
218 return str;
219 }
220
221 char* parse_string() {
222 if (had_error()) return NULL;
223
224 skip_ws();
225 return scan_and_terminate(' ');
226 }
227
228 char* parse_quoted_string() {
229 if (had_error()) return NULL;
230
231 skip_ws();
232
233 if (*_bufptr == '"') {
234 _bufptr++;
235 return scan_and_terminate('"');
236 } else {
237 return scan_and_terminate(' ');
238 }
239 }
240
241 const char* parse_escaped_string() {
242 char* result = parse_quoted_string();
243 if (result != NULL) {
244 unescape_string(result);
245 }
246 return result;
247 }
248
249 // Look for the tag 'tag' followed by an
250 bool parse_tag_and_count(const char* tag, int& length) {
251 const char* t = parse_string();
252 if (t == NULL) {
253 return false;
254 }
255
256 if (strcmp(tag, t) != 0) {
257 report_error(tag);
258 return false;
259 }
260 length = parse_int("parse_tag_and_count");
261 return !had_error();
262 }
263
264 // Parse a sequence of raw data encoded as bytes and return the
265 // resulting data.
266 char* parse_data(const char* tag, int& length) {
267 if (!parse_tag_and_count(tag, length)) {
268 return NULL;
269 }
270
271 char * result = NEW_RESOURCE_ARRAY(char, length);
272 for (int i = 0; i < length; i++) {
273 int val = parse_int("data");
274 result[i] = val;
275 }
276 return result;
277 }
278
279 // Parse a standard chunk of data emitted as:
280 // 'tag' <length> # # ...
281 // Where each # is an intptr_t item
282 intptr_t* parse_intptr_data(const char* tag, int& length) {
283 if (!parse_tag_and_count(tag, length)) {
284 return NULL;
285 }
286
287 intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
288 for (int i = 0; i < length; i++) {
289 skip_ws();
290 intptr_t val = parse_intptr_t("data");
291 result[i] = val;
292 }
293 return result;
294 }
295
296 // Parse a possibly quoted version of a symbol into a symbolOop
297 Symbol* parse_symbol(TRAPS) {
298 const char* str = parse_escaped_string();
299 if (str != NULL) {
300 Symbol* sym = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
301 return sym;
302 }
303 return NULL;
304 }
305
306 // Parse a valid klass name and look it up
307 Klass* parse_klass(TRAPS) {
308 const char* str = parse_escaped_string();
309 Symbol* klass_name = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
310 if (klass_name != NULL) {
311 Klass* k = NULL;
312 if (_iklass != NULL) {
313 k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
314 } else {
315 k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
316 }
317 if (HAS_PENDING_EXCEPTION) {
318 oop throwable = PENDING_EXCEPTION;
319 java_lang_Throwable::print(throwable, tty);
320 tty->cr();
321 report_error(str);
322 return NULL;
323 }
324 return k;
325 }
326 return NULL;
327 }
328
329 // Lookup a klass
330 Klass* resolve_klass(const char* klass, TRAPS) {
331 Symbol* klass_name = SymbolTable::lookup(klass, (int)strlen(klass), CHECK_NULL);
332 return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, CHECK_NULL);
333 }
334
335 // Parse the standard tuple of <klass> <name> <signature>
336 Method* parse_method(TRAPS) {
337 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
338 Symbol* method_name = parse_symbol(CHECK_NULL);
339 Symbol* method_signature = parse_symbol(CHECK_NULL);
340 Method* m = k->find_method(method_name, method_signature);
341 if (m == NULL) {
342 report_error("Can't find method");
343 }
344 return m;
345 }
346
347 int get_line(int c) {
348 while(c != EOF) {
349 if (_buffer_pos + 1 >= _buffer_length) {
350 int new_length = _buffer_length * 2;
351 // Next call will throw error in case of OOM.
352 _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
353 _buffer_length = new_length;
354 }
355 if (c == '\n') {
356 c = getc(_stream); // get next char
357 break;
358 } else if (c == '\r') {
359 // skip LF
360 } else {
361 _buffer[_buffer_pos++] = c;
362 }
363 c = getc(_stream);
364 }
365 // null terminate it, reset the pointer
366 _buffer[_buffer_pos] = '\0'; // NL or EOF
367 _buffer_pos = 0;
368 _bufptr = _buffer;
369 return c;
370 }
371
372 // Process each line of the replay file executing each command until
373 // the file ends.
374 void process(TRAPS) {
375 int line_no = 1;
376 int c = getc(_stream);
377 while(c != EOF) {
378 c = get_line(c);
379 process_command(THREAD);
380 if (had_error()) {
381 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
382 if (ReplayIgnoreInitErrors) {
383 CLEAR_PENDING_EXCEPTION;
384 _error_message = NULL;
385 } else {
386 return;
387 }
388 }
389 line_no++;
390 }
391 }
392
393 void process_command(TRAPS) {
394 char* cmd = parse_string();
395 if (cmd == NULL) {
396 return;
397 }
398 if (strcmp("#", cmd) == 0) {
399 // ignore
400 } else if (strcmp("compile", cmd) == 0) {
401 process_compile(CHECK);
402 } else if (strcmp("ciMethod", cmd) == 0) {
403 process_ciMethod(CHECK);
404 } else if (strcmp("ciMethodData", cmd) == 0) {
405 process_ciMethodData(CHECK);
406 } else if (strcmp("staticfield", cmd) == 0) {
407 process_staticfield(CHECK);
408 } else if (strcmp("ciInstanceKlass", cmd) == 0) {
409 process_ciInstanceKlass(CHECK);
410 } else if (strcmp("instanceKlass", cmd) == 0) {
411 process_instanceKlass(CHECK);
412 #if INCLUDE_JVMTI
413 } else if (strcmp("JvmtiExport", cmd) == 0) {
414 process_JvmtiExport(CHECK);
415 #endif // INCLUDE_JVMTI
416 } else {
417 report_error("unknown command");
418 }
419 }
420
421 // validation of comp_level
422 bool is_valid_comp_level(int comp_level) {
423 const int msg_len = 256;
424 char* msg = NULL;
425 if (!is_compile(comp_level)) {
426 msg = NEW_RESOURCE_ARRAY(char, msg_len);
427 jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
428 } else if (!TieredCompilation && (comp_level != CompLevel_highest_tier)) {
429 msg = NEW_RESOURCE_ARRAY(char, msg_len);
430 switch (comp_level) {
431 case CompLevel_simple:
432 jio_snprintf(msg, msg_len, "compilation level %d requires Client VM or TieredCompilation", comp_level);
433 break;
434 case CompLevel_full_optimization:
435 jio_snprintf(msg, msg_len, "compilation level %d requires Server VM", comp_level);
436 break;
437 default:
438 jio_snprintf(msg, msg_len, "compilation level %d requires TieredCompilation", comp_level);
439 }
440 }
441 if (msg != NULL) {
442 report_error(msg);
443 return false;
444 }
445 return true;
446 }
447
448 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
449 void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
450 _imethod = m;
451 _iklass = imethod->holder();
452 _entry_bci = entry_bci;
453 _comp_level = comp_level;
454 int line_no = 1;
455 int c = getc(_stream);
456 while(c != EOF) {
457 c = get_line(c);
458 // Expecting only lines with "compile" command in inline replay file.
459 char* cmd = parse_string();
460 if (cmd == NULL || strcmp("compile", cmd) != 0) {
461 return NULL;
462 }
463 process_compile(CHECK_NULL);
464 if (had_error()) {
465 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
466 tty->print_cr("%s", _buffer);
467 return NULL;
468 }
469 if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) {
470 // Found inlining record for the requested method.
471 return _ci_inline_records;
472 }
473 line_no++;
474 }
475 return NULL;
476 }
477
478 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
479 void process_compile(TRAPS) {
480 Method* method = parse_method(CHECK);
481 if (had_error()) return;
482 int entry_bci = parse_int("entry_bci");
483 const char* comp_level_label = "comp_level";
484 int comp_level = parse_int(comp_level_label);
485 // old version w/o comp_level
486 if (had_error() && (error_message() == comp_level_label)) {
487 comp_level = CompLevel_full_optimization;
488 }
489 if (!is_valid_comp_level(comp_level)) {
490 return;
491 }
492 if (_imethod != NULL) {
493 // Replay Inlining
494 if (entry_bci != _entry_bci || comp_level != _comp_level) {
495 return;
496 }
497 const char* iklass_name = _imethod->method_holder()->name()->as_utf8();
498 const char* imethod_name = _imethod->name()->as_utf8();
499 const char* isignature = _imethod->signature()->as_utf8();
500 const char* klass_name = method->method_holder()->name()->as_utf8();
501 const char* method_name = method->name()->as_utf8();
502 const char* signature = method->signature()->as_utf8();
503 if (strcmp(iklass_name, klass_name) != 0 ||
504 strcmp(imethod_name, method_name) != 0 ||
505 strcmp(isignature, signature) != 0) {
506 return;
507 }
508 }
509 int inline_count = 0;
510 if (parse_tag_and_count("inline", inline_count)) {
511 // Record inlining data
512 _ci_inline_records = new GrowableArray<ciInlineRecord*>();
513 for (int i = 0; i < inline_count; i++) {
514 int depth = parse_int("inline_depth");
515 int bci = parse_int("inline_bci");
516 if (had_error()) {
517 break;
518 }
519 Method* inl_method = parse_method(CHECK);
520 if (had_error()) {
521 break;
522 }
523 new_ciInlineRecord(inl_method, bci, depth);
524 }
525 }
526 if (_imethod != NULL) {
527 return; // Replay Inlining
528 }
529 Klass* k = method->method_holder();
530 ((InstanceKlass*)k)->initialize(THREAD);
531 if (HAS_PENDING_EXCEPTION) {
532 oop throwable = PENDING_EXCEPTION;
533 java_lang_Throwable::print(throwable, tty);
534 tty->cr();
535 if (ReplayIgnoreInitErrors) {
536 CLEAR_PENDING_EXCEPTION;
537 ((InstanceKlass*)k)->set_init_state(InstanceKlass::fully_initialized);
538 } else {
539 return;
540 }
541 }
542 // Make sure the existence of a prior compile doesn't stop this one
543 nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
544 if (nm != NULL) {
545 nm->make_not_entrant();
546 }
547 replay_state = this;
548 CompileBroker::compile_method(method, entry_bci, comp_level,
549 methodHandle(), 0, "replay", THREAD);
550 replay_state = NULL;
551 reset();
552 }
553
554 // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
555 //
556 //
557 void process_ciMethod(TRAPS) {
558 Method* method = parse_method(CHECK);
559 if (had_error()) return;
560 ciMethodRecord* rec = new_ciMethod(method);
561 rec->_invocation_counter = parse_int("invocation_counter");
562 rec->_backedge_counter = parse_int("backedge_counter");
563 rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
564 rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
565 rec->_instructions_size = parse_int("instructions_size");
566 }
567
568 // ciMethodData <klass> <name> <signature> <state> <current mileage> orig <length> # # ... data <length> # # ... oops <length>
569 void process_ciMethodData(TRAPS) {
570 Method* method = parse_method(CHECK);
571 if (had_error()) return;
572 /* just copied from Method, to build interpret data*/
573 if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
574 return;
575 }
576 // To be properly initialized, some profiling in the MDO needs the
577 // method to be rewritten (number of arguments at a call for
578 // instance)
579 method->method_holder()->link_class(CHECK);
580 // methodOopDesc::build_interpreter_method_data(method, CHECK);
581 {
582 // Grab a lock here to prevent multiple
583 // MethodData*s from being created.
584 MutexLocker ml(MethodData_lock, THREAD);
585 if (method->method_data() == NULL) {
586 ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
587 MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
588 method->set_method_data(method_data);
589 }
590 }
591
592 // collect and record all the needed information for later
593 ciMethodDataRecord* rec = new_ciMethodData(method);
594 rec->_state = parse_int("state");
595 rec->_current_mileage = parse_int("current_mileage");
596
597 rec->_orig_data = parse_data("orig", rec->_orig_data_length);
598 if (rec->_orig_data == NULL) {
599 return;
600 }
601 rec->_data = parse_intptr_data("data", rec->_data_length);
602 if (rec->_data == NULL) {
603 return;
604 }
605 if (!parse_tag_and_count("oops", rec->_oops_length)) {
606 return;
607 }
608 rec->_oops_handles = NEW_RESOURCE_ARRAY(jobject, rec->_oops_length);
609 rec->_oops_offsets = NEW_RESOURCE_ARRAY(int, rec->_oops_length);
610 for (int i = 0; i < rec->_oops_length; i++) {
611 int offset = parse_int("offset");
612 if (had_error()) {
613 return;
614 }
615 Klass* k = parse_klass(CHECK);
616 rec->_oops_offsets[i] = offset;
617 KlassHandle *kh = NEW_C_HEAP_OBJ(KlassHandle, mtCompiler);
618 ::new ((void*)kh) KlassHandle(THREAD, k);
619 rec->_oops_handles[i] = (jobject)kh;
620 }
621 }
622
623 // instanceKlass <name>
624 //
625 // Loads and initializes the klass 'name'. This can be used to
626 // create particular class loading environments
627 void process_instanceKlass(TRAPS) {
628 // just load the referenced class
629 Klass* k = parse_klass(CHECK);
630 }
631
632 // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag # # # ...
633 //
634 // Load the klass 'name' and link or initialize it. Verify that the
635 // constant pool is the same length as 'length' and make sure the
636 // constant pool tags are in the same state.
637 void process_ciInstanceKlass(TRAPS) {
638 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
639 int is_linked = parse_int("is_linked");
640 int is_initialized = parse_int("is_initialized");
641 int length = parse_int("length");
642 if (is_initialized) {
643 k->initialize(THREAD);
644 if (HAS_PENDING_EXCEPTION) {
645 oop throwable = PENDING_EXCEPTION;
646 java_lang_Throwable::print(throwable, tty);
647 tty->cr();
648 if (ReplayIgnoreInitErrors) {
649 CLEAR_PENDING_EXCEPTION;
650 k->set_init_state(InstanceKlass::fully_initialized);
651 } else {
652 return;
653 }
654 }
655 } else if (is_linked) {
656 k->link_class(CHECK);
657 }
658 ConstantPool* cp = k->constants();
659 if (length != cp->length()) {
660 report_error("constant pool length mismatch: wrong class files?");
661 return;
662 }
663
664 int parsed_two_word = 0;
665 for (int i = 1; i < length; i++) {
666 int tag = parse_int("tag");
667 if (had_error()) {
668 return;
669 }
670 switch (cp->tag_at(i).value()) {
671 case JVM_CONSTANT_UnresolvedClass: {
672 if (tag == JVM_CONSTANT_Class) {
673 tty->print_cr("Resolving klass %s at %d", cp->unresolved_klass_at(i)->as_utf8(), i);
674 Klass* k = cp->klass_at(i, CHECK);
675 }
676 break;
677 }
678 case JVM_CONSTANT_Long:
679 case JVM_CONSTANT_Double:
680 parsed_two_word = i + 1;
681
682 case JVM_CONSTANT_ClassIndex:
683 case JVM_CONSTANT_StringIndex:
684 case JVM_CONSTANT_String:
685 case JVM_CONSTANT_UnresolvedClassInError:
686 case JVM_CONSTANT_Fieldref:
687 case JVM_CONSTANT_Methodref:
688 case JVM_CONSTANT_InterfaceMethodref:
689 case JVM_CONSTANT_NameAndType:
690 case JVM_CONSTANT_Utf8:
691 case JVM_CONSTANT_Integer:
692 case JVM_CONSTANT_Float:
693 case JVM_CONSTANT_MethodHandle:
694 case JVM_CONSTANT_MethodType:
695 case JVM_CONSTANT_InvokeDynamic:
696 if (tag != cp->tag_at(i).value()) {
697 report_error("tag mismatch: wrong class files?");
698 return;
699 }
700 break;
701
702 case JVM_CONSTANT_Class:
703 if (tag == JVM_CONSTANT_Class) {
704 } else if (tag == JVM_CONSTANT_UnresolvedClass) {
705 tty->print_cr("Warning: entry was unresolved in the replay data");
706 } else {
707 report_error("Unexpected tag");
708 return;
709 }
710 break;
711
712 case 0:
713 if (parsed_two_word == i) continue;
714
715 default:
716 fatal(err_msg_res("Unexpected tag: %d", cp->tag_at(i).value()));
717 break;
718 }
719
720 }
721 }
722
723 // Initialize a class and fill in the value for a static field.
724 // This is useful when the compile was dependent on the value of
725 // static fields but it's impossible to properly rerun the static
726 // initiailizer.
727 void process_staticfield(TRAPS) {
728 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
729
730 if (ReplaySuppressInitializers == 0 ||
731 ReplaySuppressInitializers == 2 && k->class_loader() == NULL) {
732 return;
733 }
734
735 assert(k->is_initialized(), "must be");
736
737 const char* field_name = parse_escaped_string();;
738 const char* field_signature = parse_string();
739 fieldDescriptor fd;
740 Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
741 Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
742 if (!k->find_local_field(name, sig, &fd) ||
743 !fd.is_static() ||
744 fd.has_initial_value()) {
745 report_error(field_name);
746 return;
747 }
748
749 oop java_mirror = k->java_mirror();
750 if (field_signature[0] == '[') {
751 int length = parse_int("array length");
752 oop value = NULL;
753
754 if (field_signature[1] == '[') {
755 // multi dimensional array
756 ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK);
757 int rank = 0;
758 while (field_signature[rank] == '[') {
759 rank++;
760 }
761 int* dims = NEW_RESOURCE_ARRAY(int, rank);
762 dims[0] = length;
763 for (int i = 1; i < rank; i++) {
764 dims[i] = 1; // These aren't relevant to the compiler
765 }
766 value = kelem->multi_allocate(rank, dims, CHECK);
767 } else {
768 if (strcmp(field_signature, "[B") == 0) {
769 value = oopFactory::new_byteArray(length, CHECK);
770 } else if (strcmp(field_signature, "[Z") == 0) {
771 value = oopFactory::new_boolArray(length, CHECK);
772 } else if (strcmp(field_signature, "[C") == 0) {
773 value = oopFactory::new_charArray(length, CHECK);
774 } else if (strcmp(field_signature, "[S") == 0) {
775 value = oopFactory::new_shortArray(length, CHECK);
776 } else if (strcmp(field_signature, "[F") == 0) {
777 value = oopFactory::new_singleArray(length, CHECK);
778 } else if (strcmp(field_signature, "[D") == 0) {
779 value = oopFactory::new_doubleArray(length, CHECK);
780 } else if (strcmp(field_signature, "[I") == 0) {
781 value = oopFactory::new_intArray(length, CHECK);
782 } else if (strcmp(field_signature, "[J") == 0) {
783 value = oopFactory::new_longArray(length, CHECK);
784 } else if (field_signature[0] == '[' && field_signature[1] == 'L') {
785 KlassHandle kelem = resolve_klass(field_signature + 1, CHECK);
786 value = oopFactory::new_objArray(kelem(), length, CHECK);
787 } else {
788 report_error("unhandled array staticfield");
789 }
790 }
791 java_mirror->obj_field_put(fd.offset(), value);
792 } else {
793 const char* string_value = parse_escaped_string();
794 if (strcmp(field_signature, "I") == 0) {
795 int value = atoi(string_value);
796 java_mirror->int_field_put(fd.offset(), value);
797 } else if (strcmp(field_signature, "B") == 0) {
798 int value = atoi(string_value);
799 java_mirror->byte_field_put(fd.offset(), value);
800 } else if (strcmp(field_signature, "C") == 0) {
801 int value = atoi(string_value);
802 java_mirror->char_field_put(fd.offset(), value);
803 } else if (strcmp(field_signature, "S") == 0) {
804 int value = atoi(string_value);
805 java_mirror->short_field_put(fd.offset(), value);
806 } else if (strcmp(field_signature, "Z") == 0) {
807 int value = atol(string_value);
808 java_mirror->bool_field_put(fd.offset(), value);
809 } else if (strcmp(field_signature, "J") == 0) {
810 jlong value;
811 if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
812 fprintf(stderr, "Error parsing long: %s\n", string_value);
813 return;
814 }
815 java_mirror->long_field_put(fd.offset(), value);
816 } else if (strcmp(field_signature, "F") == 0) {
817 float value = atof(string_value);
818 java_mirror->float_field_put(fd.offset(), value);
819 } else if (strcmp(field_signature, "D") == 0) {
820 double value = atof(string_value);
821 java_mirror->double_field_put(fd.offset(), value);
822 } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
823 Handle value = java_lang_String::create_from_str(string_value, CHECK);
824 java_mirror->obj_field_put(fd.offset(), value());
825 } else if (field_signature[0] == 'L') {
826 Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
827 KlassHandle kelem = resolve_klass(field_signature, CHECK);
828 oop value = ((InstanceKlass*)kelem())->allocate_instance(CHECK);
829 java_mirror->obj_field_put(fd.offset(), value);
830 } else {
831 report_error("unhandled staticfield");
832 }
833 }
834 }
835
836 #if INCLUDE_JVMTI
837 void process_JvmtiExport(TRAPS) {
838 const char* field = parse_string();
839 bool value = parse_int("JvmtiExport flag") != 0;
840 if (strcmp(field, "can_access_local_variables") == 0) {
841 JvmtiExport::set_can_access_local_variables(value);
842 } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
843 JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
844 } else if (strcmp(field, "can_post_on_exceptions") == 0) {
845 JvmtiExport::set_can_post_on_exceptions(value);
846 } else {
847 report_error("Unrecognized JvmtiExport directive");
848 }
849 }
850 #endif // INCLUDE_JVMTI
851
852 // Create and initialize a record for a ciMethod
853 ciMethodRecord* new_ciMethod(Method* method) {
854 ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
855 rec->_klass_name = method->method_holder()->name()->as_utf8();
856 rec->_method_name = method->name()->as_utf8();
857 rec->_signature = method->signature()->as_utf8();
858 _ci_method_records.append(rec);
859 return rec;
860 }
861
862 // Lookup data for a ciMethod
863 ciMethodRecord* find_ciMethodRecord(Method* method) {
864 const char* klass_name = method->method_holder()->name()->as_utf8();
865 const char* method_name = method->name()->as_utf8();
866 const char* signature = method->signature()->as_utf8();
867 for (int i = 0; i < _ci_method_records.length(); i++) {
868 ciMethodRecord* rec = _ci_method_records.at(i);
869 if (strcmp(rec->_klass_name, klass_name) == 0 &&
870 strcmp(rec->_method_name, method_name) == 0 &&
871 strcmp(rec->_signature, signature) == 0) {
872 return rec;
873 }
874 }
875 return NULL;
876 }
877
878 // Create and initialize a record for a ciMethodData
879 ciMethodDataRecord* new_ciMethodData(Method* method) {
880 ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
881 rec->_klass_name = method->method_holder()->name()->as_utf8();
882 rec->_method_name = method->name()->as_utf8();
883 rec->_signature = method->signature()->as_utf8();
884 _ci_method_data_records.append(rec);
885 return rec;
886 }
887
888 // Lookup data for a ciMethodData
889 ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
890 const char* klass_name = method->method_holder()->name()->as_utf8();
891 const char* method_name = method->name()->as_utf8();
892 const char* signature = method->signature()->as_utf8();
893 for (int i = 0; i < _ci_method_data_records.length(); i++) {
894 ciMethodDataRecord* rec = _ci_method_data_records.at(i);
895 if (strcmp(rec->_klass_name, klass_name) == 0 &&
896 strcmp(rec->_method_name, method_name) == 0 &&
897 strcmp(rec->_signature, signature) == 0) {
898 return rec;
899 }
900 }
901 return NULL;
902 }
903
904 // Create and initialize a record for a ciInlineRecord
905 ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
906 ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
907 rec->_klass_name = method->method_holder()->name()->as_utf8();
908 rec->_method_name = method->name()->as_utf8();
909 rec->_signature = method->signature()->as_utf8();
910 rec->_inline_bci = bci;
911 rec->_inline_depth = depth;
912 _ci_inline_records->append(rec);
913 return rec;
914 }
915
916 // Lookup inlining data for a ciMethod
917 ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
918 if (_ci_inline_records != NULL) {
919 return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
920 }
921 return NULL;
922 }
923
924 static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>* records,
925 Method* method, int bci, int depth) {
926 if (records != NULL) {
927 const char* klass_name = method->method_holder()->name()->as_utf8();
928 const char* method_name = method->name()->as_utf8();
929 const char* signature = method->signature()->as_utf8();
930 for (int i = 0; i < records->length(); i++) {
931 ciInlineRecord* rec = records->at(i);
932 if ((rec->_inline_bci == bci) &&
933 (rec->_inline_depth == depth) &&
934 (strcmp(rec->_klass_name, klass_name) == 0) &&
935 (strcmp(rec->_method_name, method_name) == 0) &&
936 (strcmp(rec->_signature, signature) == 0)) {
937 return rec;
938 }
939 }
940 }
941 return NULL;
942 }
943
944 const char* error_message() {
945 return _error_message;
946 }
947
948 void reset() {
949 _error_message = NULL;
950 _ci_method_records.clear();
951 _ci_method_data_records.clear();
952 }
953
954 // Take an ascii string contain \u#### escapes and convert it to utf8
955 // in place.
956 static void unescape_string(char* value) {
957 char* from = value;
958 char* to = value;
959 while (*from != '\0') {
960 if (*from != '\\') {
961 *from++ = *to++;
962 } else {
963 switch (from[1]) {
964 case 'u': {
965 from += 2;
966 jchar value=0;
967 for (int i=0; i<4; i++) {
968 char c = *from++;
969 switch (c) {
970 case '0': case '1': case '2': case '3': case '4':
971 case '5': case '6': case '7': case '8': case '9':
972 value = (value << 4) + c - '0';
973 break;
974 case 'a': case 'b': case 'c':
975 case 'd': case 'e': case 'f':
976 value = (value << 4) + 10 + c - 'a';
977 break;
978 case 'A': case 'B': case 'C':
979 case 'D': case 'E': case 'F':
980 value = (value << 4) + 10 + c - 'A';
981 break;
982 default:
983 ShouldNotReachHere();
984 }
985 }
986 UNICODE::convert_to_utf8(&value, 1, to);
987 to++;
988 break;
989 }
990 case 't': *to++ = '\t'; from += 2; break;
991 case 'n': *to++ = '\n'; from += 2; break;
992 case 'r': *to++ = '\r'; from += 2; break;
993 case 'f': *to++ = '\f'; from += 2; break;
994 default:
995 ShouldNotReachHere();
996 }
997 }
998 }
999 *from = *to;
1000 }
1001 };
1002
1003 void ciReplay::replay(TRAPS) {
1004 int exit_code = replay_impl(THREAD);
1005
1006 Threads::destroy_vm();
1007
1008 vm_exit(exit_code);
1009 }
1010
1011 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1012 if (FLAG_IS_DEFAULT(InlineDataFile)) {
1013 tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1014 return NULL;
1015 }
1016
1017 VM_ENTRY_MARK;
1018 // Load and parse the replay data
1019 CompileReplay rp(InlineDataFile, THREAD);
1020 if (!rp.can_replay()) {
1021 tty->print_cr("ciReplay: !rp.can_replay()");
1022 return NULL;
1023 }
1024 void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1025 if (HAS_PENDING_EXCEPTION) {
1026 oop throwable = PENDING_EXCEPTION;
1027 CLEAR_PENDING_EXCEPTION;
1028 java_lang_Throwable::print(throwable, tty);
1029 tty->cr();
1030 java_lang_Throwable::print_stack_trace(throwable, tty);
1031 tty->cr();
1032 return NULL;
1033 }
1034
1035 if (rp.had_error()) {
1036 tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1037 return NULL;
1038 }
1039 return data;
1040 }
1041
1042 int ciReplay::replay_impl(TRAPS) {
1043 HandleMark hm;
1044 ResourceMark rm;
1045 // Make sure we don't run with background compilation
1046 BackgroundCompilation = false;
1047
1048 if (ReplaySuppressInitializers > 2) {
1049 // ReplaySuppressInitializers > 2 means that we want to allow
1050 // normal VM bootstrap but once we get into the replay itself
1051 // don't allow any intializers to be run.
1052 ReplaySuppressInitializers = 1;
1053 }
1054
1055 if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1056 tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1057 return 1;
1058 }
1059
1060 // Load and parse the replay data
1061 CompileReplay rp(ReplayDataFile, THREAD);
1062 int exit_code = 0;
1063 if (rp.can_replay()) {
1064 rp.process(THREAD);
1065 } else {
1066 exit_code = 1;
1067 return exit_code;
1068 }
1069
1070 if (HAS_PENDING_EXCEPTION) {
1071 oop throwable = PENDING_EXCEPTION;
1072 CLEAR_PENDING_EXCEPTION;
1073 java_lang_Throwable::print(throwable, tty);
1074 tty->cr();
1075 java_lang_Throwable::print_stack_trace(throwable, tty);
1076 tty->cr();
1077 exit_code = 2;
1078 }
1079
1080 if (rp.had_error()) {
1081 tty->print_cr("Failed on %s", rp.error_message());
1082 exit_code = 1;
1083 }
1084 return exit_code;
1085 }
1086
1087 void ciReplay::initialize(ciMethodData* m) {
1088 if (replay_state == NULL) {
1089 return;
1090 }
1091
1092 ASSERT_IN_VM;
1093 ResourceMark rm;
1094
1095 Method* method = m->get_MethodData()->method();
1096 ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1097 if (rec == NULL) {
1098 // This indicates some mismatch with the original environment and
1099 // the replay environment though it's not always enough to
1100 // interfere with reproducing a bug
1101 tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1102 method->print_name(tty);
1103 tty->cr();
1104 } else {
1105 m->_state = rec->_state;
1106 m->_current_mileage = rec->_current_mileage;
1107 if (rec->_data_length != 0) {
1108 assert(m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1109
1110 // Write the correct ciObjects back into the profile data
1111 ciEnv* env = ciEnv::current();
1112 for (int i = 0; i < rec->_oops_length; i++) {
1113 KlassHandle *h = (KlassHandle *)rec->_oops_handles[i];
1114 *(ciMetadata**)(rec->_data + rec->_oops_offsets[i]) =
1115 env->get_metadata((*h)());
1116 }
1117 // Copy the updated profile data into place as intptr_ts
1118 #ifdef _LP64
1119 Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1120 #else
1121 Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1122 #endif
1123 }
1124
1125 // copy in the original header
1126 Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1127 }
1128 }
1129
1130
1131 bool ciReplay::should_not_inline(ciMethod* method) {
1132 if (replay_state == NULL) {
1133 return false;
1134 }
1135 VM_ENTRY_MARK;
1136 // ciMethod without a record shouldn't be inlined.
1137 return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1138 }
1139
1140 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1141 if (data != NULL) {
1142 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1143 VM_ENTRY_MARK;
1144 // Inline record are ordered by bci and depth.
1145 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1146 } else if (replay_state != NULL) {
1147 VM_ENTRY_MARK;
1148 // Inline record are ordered by bci and depth.
1149 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1150 }
1151 return false;
1152 }
1153
1154 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1155 if (data != NULL) {
1156 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1157 VM_ENTRY_MARK;
1158 // Inline record are ordered by bci and depth.
1159 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1160 } else if (replay_state != NULL) {
1161 VM_ENTRY_MARK;
1162 // Inline record are ordered by bci and depth.
1163 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1164 }
1165 return false;
1166 }
1167
1168 void ciReplay::initialize(ciMethod* m) {
1169 if (replay_state == NULL) {
1170 return;
1171 }
1172
1173 ASSERT_IN_VM;
1174 ResourceMark rm;
1175
1176 Method* method = m->get_Method();
1177 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1178 if (rec == NULL) {
1179 // This indicates some mismatch with the original environment and
1180 // the replay environment though it's not always enough to
1181 // interfere with reproducing a bug
1182 tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1183 method->print_name(tty);
1184 tty->cr();
1185 } else {
1186 EXCEPTION_CONTEXT;
1187 // m->_instructions_size = rec->_instructions_size;
1188 m->_instructions_size = -1;
1189 m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1190 m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1191 MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1192 guarantee(mcs != NULL, "method counters allocation failed");
1193 mcs->invocation_counter()->_counter = rec->_invocation_counter;
1194 mcs->backedge_counter()->_counter = rec->_backedge_counter;
1195 }
1196 }
1197
1198 bool ciReplay::is_loaded(Method* method) {
1199 if (replay_state == NULL) {
1200 return true;
1201 }
1202
1203 ASSERT_IN_VM;
1204 ResourceMark rm;
1205
1206 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1207 return rec != NULL;
1208 }
1209 #endif // PRODUCT

mercurial