src/share/vm/memory/dump.cpp

changeset 435
a61af66fc99e
child 548
ba764ed4b6f2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/memory/dump.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,1454 @@
     1.4 +/*
     1.5 + * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +# include "incls/_precompiled.incl"
    1.29 +# include "incls/_dump.cpp.incl"
    1.30 +
    1.31 +
    1.32 +// Closure to set up the fingerprint field for all methods.
    1.33 +
    1.34 +class FingerprintMethodsClosure: public ObjectClosure {
    1.35 +public:
    1.36 +  void do_object(oop obj) {
    1.37 +    if (obj->is_method()) {
    1.38 +      methodOop mobj = (methodOop)obj;
    1.39 +      ResourceMark rm;
    1.40 +      (new Fingerprinter(mobj))->fingerprint();
    1.41 +    }
    1.42 +  }
    1.43 +};
    1.44 +
    1.45 +
    1.46 +
    1.47 +// Closure to set the hash value (String.hash field) in all of the
    1.48 +// String objects in the heap.  Setting the hash value is not required.
    1.49 +// However, setting the value in advance prevents the value from being
    1.50 +// written later, increasing the likelihood that the shared page contain
    1.51 +// the hash can be shared.
    1.52 +//
    1.53 +// NOTE THAT the algorithm in StringTable::hash_string() MUST MATCH the
    1.54 +// algorithm in java.lang.String.hashCode().
    1.55 +
    1.56 +class StringHashCodeClosure: public OopClosure {
    1.57 +private:
    1.58 +  Thread* THREAD;
    1.59 +  int hash_offset;
    1.60 +public:
    1.61 +  StringHashCodeClosure(Thread* t) {
    1.62 +    THREAD = t;
    1.63 +    hash_offset = java_lang_String::hash_offset_in_bytes();
    1.64 +  }
    1.65 +
    1.66 +  void do_oop(oop* pobj) {
    1.67 +    if (pobj != NULL) {
    1.68 +      oop obj = *pobj;
    1.69 +      if (obj->klass() == SystemDictionary::string_klass()) {
    1.70 +
    1.71 +        int hash;
    1.72 +        typeArrayOop value = java_lang_String::value(obj);
    1.73 +        int length = java_lang_String::length(obj);
    1.74 +        if (length == 0) {
    1.75 +          hash = 0;
    1.76 +        } else {
    1.77 +          int offset = java_lang_String::offset(obj);
    1.78 +          jchar* s = value->char_at_addr(offset);
    1.79 +          hash = StringTable::hash_string(s, length);
    1.80 +        }
    1.81 +        obj->int_field_put(hash_offset, hash);
    1.82 +      }
    1.83 +    }
    1.84 +  }
    1.85 +};
    1.86 +
    1.87 +
    1.88 +// Remove data from objects which should not appear in the shared file
    1.89 +// (as it pertains only to the current JVM).
    1.90 +
    1.91 +class RemoveUnshareableInfoClosure : public ObjectClosure {
    1.92 +public:
    1.93 +  void do_object(oop obj) {
    1.94 +    // Zap data from the objects which is pertains only to this JVM.  We
    1.95 +    // want that data recreated in new JVMs when the shared file is used.
    1.96 +    if (obj->is_method()) {
    1.97 +      ((methodOop)obj)->remove_unshareable_info();
    1.98 +    }
    1.99 +    else if (obj->is_klass()) {
   1.100 +      Klass::cast((klassOop)obj)->remove_unshareable_info();
   1.101 +    }
   1.102 +
   1.103 +    // Don't save compiler related special oops (shouldn't be any yet).
   1.104 +    if (obj->is_methodData() || obj->is_compiledICHolder()) {
   1.105 +      ShouldNotReachHere();
   1.106 +    }
   1.107 +  }
   1.108 +};
   1.109 +
   1.110 +
   1.111 +static bool mark_object(oop obj) {
   1.112 +  if (obj != NULL &&
   1.113 +      !obj->is_shared() &&
   1.114 +      !obj->is_forwarded() &&
   1.115 +      !obj->is_gc_marked()) {
   1.116 +    obj->set_mark(markOopDesc::prototype()->set_marked());
   1.117 +    return true;
   1.118 +  }
   1.119 +
   1.120 +  return false;
   1.121 +}
   1.122 +
   1.123 +// Closure:  mark objects closure.
   1.124 +
   1.125 +class MarkObjectsOopClosure : public OopClosure {
   1.126 +public:
   1.127 +  void do_oop(oop* pobj) {
   1.128 +    mark_object(*pobj);
   1.129 +  }
   1.130 +};
   1.131 +
   1.132 +
   1.133 +class MarkObjectsSkippingKlassesOopClosure : public OopClosure {
   1.134 +public:
   1.135 +  void do_oop(oop* pobj) {
   1.136 +    oop obj = *pobj;
   1.137 +    if (obj != NULL &&
   1.138 +        !obj->is_klass()) {
   1.139 +      mark_object(obj);
   1.140 +    }
   1.141 +  }
   1.142 +};
   1.143 +
   1.144 +
   1.145 +static void mark_object_recursive_skipping_klasses(oop obj) {
   1.146 +  mark_object(obj);
   1.147 +  if (obj != NULL) {
   1.148 +    MarkObjectsSkippingKlassesOopClosure mark_all;
   1.149 +    obj->oop_iterate(&mark_all);
   1.150 +  }
   1.151 +}
   1.152 +
   1.153 +
   1.154 +// Closure:  mark common read-only objects, excluding symbols
   1.155 +
   1.156 +class MarkCommonReadOnly : public ObjectClosure {
   1.157 +private:
   1.158 +  MarkObjectsOopClosure mark_all;
   1.159 +public:
   1.160 +  void do_object(oop obj) {
   1.161 +
   1.162 +    // Mark all constMethod objects.
   1.163 +
   1.164 +    if (obj->is_constMethod()) {
   1.165 +      mark_object(obj);
   1.166 +      mark_object(constMethodOop(obj)->stackmap_data());
   1.167 +      // Exception tables are needed by ci code during compilation.
   1.168 +      mark_object(constMethodOop(obj)->exception_table());
   1.169 +    }
   1.170 +
   1.171 +    // Mark objects referenced by klass objects which are read-only.
   1.172 +
   1.173 +    else if (obj->is_klass()) {
   1.174 +      Klass* k = Klass::cast((klassOop)obj);
   1.175 +      mark_object(k->secondary_supers());
   1.176 +
   1.177 +      // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though
   1.178 +      // it is never modified. Otherwise, they will be pre-marked; the
   1.179 +      // GC marking phase will skip them; and by skipping them will fail
   1.180 +      // to mark the methods objects referenced by the array.
   1.181 +
   1.182 +      if (obj->blueprint()->oop_is_instanceKlass()) {
   1.183 +        instanceKlass* ik = instanceKlass::cast((klassOop)obj);
   1.184 +        mark_object(ik->method_ordering());
   1.185 +        mark_object(ik->local_interfaces());
   1.186 +        mark_object(ik->transitive_interfaces());
   1.187 +        mark_object(ik->fields());
   1.188 +
   1.189 +        mark_object(ik->class_annotations());
   1.190 +
   1.191 +        mark_object_recursive_skipping_klasses(ik->fields_annotations());
   1.192 +        mark_object_recursive_skipping_klasses(ik->methods_annotations());
   1.193 +        mark_object_recursive_skipping_klasses(ik->methods_parameter_annotations());
   1.194 +        mark_object_recursive_skipping_klasses(ik->methods_default_annotations());
   1.195 +
   1.196 +        typeArrayOop inner_classes = ik->inner_classes();
   1.197 +        if (inner_classes != NULL) {
   1.198 +          mark_object(inner_classes);
   1.199 +        }
   1.200 +      }
   1.201 +    }
   1.202 +  }
   1.203 +};
   1.204 +
   1.205 +
   1.206 +// Closure:  mark common symbols
   1.207 +
   1.208 +class MarkCommonSymbols : public ObjectClosure {
   1.209 +private:
   1.210 +  MarkObjectsOopClosure mark_all;
   1.211 +public:
   1.212 +  void do_object(oop obj) {
   1.213 +
   1.214 +    // Mark symbols refered to by method objects.
   1.215 +
   1.216 +    if (obj->is_method()) {
   1.217 +      methodOop m = methodOop(obj);
   1.218 +      mark_object(m->name());
   1.219 +      mark_object(m->signature());
   1.220 +    }
   1.221 +
   1.222 +    // Mark symbols referenced by klass objects which are read-only.
   1.223 +
   1.224 +    else if (obj->is_klass()) {
   1.225 +
   1.226 +      if (obj->blueprint()->oop_is_instanceKlass()) {
   1.227 +        instanceKlass* ik = instanceKlass::cast((klassOop)obj);
   1.228 +        mark_object(ik->name());
   1.229 +        mark_object(ik->generic_signature());
   1.230 +        mark_object(ik->source_file_name());
   1.231 +        mark_object(ik->source_debug_extension());
   1.232 +
   1.233 +        typeArrayOop inner_classes = ik->inner_classes();
   1.234 +        if (inner_classes != NULL) {
   1.235 +          int length = inner_classes->length();
   1.236 +          for (int i = 0;
   1.237 +                   i < length;
   1.238 +                   i += instanceKlass::inner_class_next_offset) {
   1.239 +            int ioff = i + instanceKlass::inner_class_inner_name_offset;
   1.240 +            int index = inner_classes->ushort_at(ioff);
   1.241 +            if (index != 0) {
   1.242 +              mark_object(ik->constants()->symbol_at(index));
   1.243 +            }
   1.244 +          }
   1.245 +        }
   1.246 +        ik->field_names_and_sigs_iterate(&mark_all);
   1.247 +      }
   1.248 +    }
   1.249 +
   1.250 +    // Mark symbols referenced by other constantpool entries.
   1.251 +
   1.252 +    if (obj->is_constantPool()) {
   1.253 +      constantPoolOop(obj)->shared_symbols_iterate(&mark_all);
   1.254 +    }
   1.255 +  }
   1.256 +};
   1.257 +
   1.258 +
   1.259 +// Closure:  mark char arrays used by strings
   1.260 +
   1.261 +class MarkStringValues : public ObjectClosure {
   1.262 +private:
   1.263 +  MarkObjectsOopClosure mark_all;
   1.264 +public:
   1.265 +  void do_object(oop obj) {
   1.266 +
   1.267 +    // Character arrays referenced by String objects are read-only.
   1.268 +
   1.269 +    if (java_lang_String::is_instance(obj)) {
   1.270 +      mark_object(java_lang_String::value(obj));
   1.271 +    }
   1.272 +  }
   1.273 +};
   1.274 +
   1.275 +
   1.276 +#ifdef DEBUG
   1.277 +// Closure:  Check for objects left in the heap which have not been moved.
   1.278 +
   1.279 +class CheckRemainingObjects : public ObjectClosure {
   1.280 +private:
   1.281 +  int count;
   1.282 +
   1.283 +public:
   1.284 +  CheckRemainingObjects() {
   1.285 +    count = 0;
   1.286 +  }
   1.287 +
   1.288 +  void do_object(oop obj) {
   1.289 +    if (!obj->is_shared() &&
   1.290 +        !obj->is_forwarded()) {
   1.291 +      ++count;
   1.292 +      if (Verbose) {
   1.293 +        tty->print("Unreferenced object: ");
   1.294 +        obj->print_on(tty);
   1.295 +      }
   1.296 +    }
   1.297 +  }
   1.298 +
   1.299 +  void status() {
   1.300 +    tty->print_cr("%d objects no longer referenced, not shared.", count);
   1.301 +  }
   1.302 +};
   1.303 +#endif
   1.304 +
   1.305 +
   1.306 +// Closure:  Mark remaining objects read-write, except Strings.
   1.307 +
   1.308 +class MarkReadWriteObjects : public ObjectClosure {
   1.309 +private:
   1.310 +  MarkObjectsOopClosure mark_objects;
   1.311 +public:
   1.312 +  void do_object(oop obj) {
   1.313 +
   1.314 +      // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though
   1.315 +      // it is never modified. Otherwise, they will be pre-marked; the
   1.316 +      // GC marking phase will skip them; and by skipping them will fail
   1.317 +      // to mark the methods objects referenced by the array.
   1.318 +
   1.319 +    if (obj->is_klass()) {
   1.320 +      mark_object(obj);
   1.321 +      Klass* k = klassOop(obj)->klass_part();
   1.322 +      mark_object(k->java_mirror());
   1.323 +      if (obj->blueprint()->oop_is_instanceKlass()) {
   1.324 +        instanceKlass* ik = (instanceKlass*)k;
   1.325 +        mark_object(ik->methods());
   1.326 +        mark_object(ik->constants());
   1.327 +      }
   1.328 +      if (obj->blueprint()->oop_is_javaArray()) {
   1.329 +        arrayKlass* ak = (arrayKlass*)k;
   1.330 +        mark_object(ak->component_mirror());
   1.331 +      }
   1.332 +      return;
   1.333 +    }
   1.334 +
   1.335 +    // Mark constantPool tags and the constantPoolCache.
   1.336 +
   1.337 +    else if (obj->is_constantPool()) {
   1.338 +      constantPoolOop pool = constantPoolOop(obj);
   1.339 +      mark_object(pool->cache());
   1.340 +      pool->shared_tags_iterate(&mark_objects);
   1.341 +      return;
   1.342 +    }
   1.343 +
   1.344 +    // Mark all method objects.
   1.345 +
   1.346 +    if (obj->is_method()) {
   1.347 +      mark_object(obj);
   1.348 +    }
   1.349 +  }
   1.350 +};
   1.351 +
   1.352 +
   1.353 +// Closure:  Mark String objects read-write.
   1.354 +
   1.355 +class MarkStringObjects : public ObjectClosure {
   1.356 +private:
   1.357 +  MarkObjectsOopClosure mark_objects;
   1.358 +public:
   1.359 +  void do_object(oop obj) {
   1.360 +
   1.361 +    // Mark String objects referenced by constant pool entries.
   1.362 +
   1.363 +    if (obj->is_constantPool()) {
   1.364 +      constantPoolOop pool = constantPoolOop(obj);
   1.365 +      pool->shared_strings_iterate(&mark_objects);
   1.366 +      return;
   1.367 +    }
   1.368 +  }
   1.369 +};
   1.370 +
   1.371 +
   1.372 +// Move objects matching specified type (ie. lock_bits) to the specified
   1.373 +// space.
   1.374 +
   1.375 +class MoveMarkedObjects : public ObjectClosure {
   1.376 +private:
   1.377 +  OffsetTableContigSpace* _space;
   1.378 +  bool _read_only;
   1.379 +
   1.380 +public:
   1.381 +  MoveMarkedObjects(OffsetTableContigSpace* space, bool read_only) {
   1.382 +    _space = space;
   1.383 +    _read_only = read_only;
   1.384 +  }
   1.385 +
   1.386 +  void do_object(oop obj) {
   1.387 +    if (obj->is_shared()) {
   1.388 +      return;
   1.389 +    }
   1.390 +    if (obj->is_gc_marked() && obj->forwardee() == NULL) {
   1.391 +      int s = obj->size();
   1.392 +      oop sh_obj = (oop)_space->allocate(s);
   1.393 +      if (sh_obj == NULL) {
   1.394 +        if (_read_only) {
   1.395 +          warning("\nThe permanent generation read only space is not large "
   1.396 +                  "enough to \npreload requested classes.  Use "
   1.397 +                  "-XX:SharedReadOnlySize= to increase \nthe initial "
   1.398 +                  "size of the read only space.\n");
   1.399 +        } else {
   1.400 +          warning("\nThe permanent generation read write space is not large "
   1.401 +                  "enough to \npreload requested classes.  Use "
   1.402 +                  "-XX:SharedReadWriteSize= to increase \nthe initial "
   1.403 +                  "size of the read write space.\n");
   1.404 +        }
   1.405 +        exit(2);
   1.406 +      }
   1.407 +      if (PrintSharedSpaces && Verbose && WizardMode) {
   1.408 +        tty->print_cr("\nMoveMarkedObjects: " PTR_FORMAT " -> " PTR_FORMAT " %s", obj, sh_obj,
   1.409 +                      (_read_only ? "ro" : "rw"));
   1.410 +      }
   1.411 +      Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)sh_obj, s);
   1.412 +      obj->forward_to(sh_obj);
   1.413 +      if (_read_only) {
   1.414 +        // Readonly objects: set hash value to self pointer and make gc_marked.
   1.415 +        sh_obj->forward_to(sh_obj);
   1.416 +      } else {
   1.417 +        sh_obj->init_mark();
   1.418 +      }
   1.419 +    }
   1.420 +  }
   1.421 +};
   1.422 +
   1.423 +static void mark_and_move(oop obj, MoveMarkedObjects* move) {
   1.424 +  if (mark_object(obj)) move->do_object(obj);
   1.425 +}
   1.426 +
   1.427 +enum order_policy {
   1.428 +  OP_favor_startup = 0,
   1.429 +  OP_balanced = 1,
   1.430 +  OP_favor_runtime = 2
   1.431 +};
   1.432 +
   1.433 +static void mark_and_move_for_policy(order_policy policy, oop obj, MoveMarkedObjects* move) {
   1.434 +  if (SharedOptimizeColdStartPolicy >= policy) mark_and_move(obj, move);
   1.435 +}
   1.436 +
   1.437 +class MarkAndMoveOrderedReadOnly : public ObjectClosure {
   1.438 +private:
   1.439 +  MoveMarkedObjects *_move_ro;
   1.440 +
   1.441 +public:
   1.442 +  MarkAndMoveOrderedReadOnly(MoveMarkedObjects *move_ro) : _move_ro(move_ro) {}
   1.443 +
   1.444 +  void do_object(oop obj) {
   1.445 +    if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) {
   1.446 +      instanceKlass* ik = instanceKlass::cast((klassOop)obj);
   1.447 +      int i;
   1.448 +
   1.449 +      mark_and_move_for_policy(OP_favor_startup, ik->name(), _move_ro);
   1.450 +
   1.451 +      if (ik->super() != NULL) {
   1.452 +        do_object(ik->super());
   1.453 +      }
   1.454 +
   1.455 +      objArrayOop interfaces = ik->local_interfaces();
   1.456 +      mark_and_move_for_policy(OP_favor_startup, interfaces, _move_ro);
   1.457 +      for(i = 0; i < interfaces->length(); i++) {
   1.458 +        klassOop k = klassOop(interfaces->obj_at(i));
   1.459 +        mark_and_move_for_policy(OP_favor_startup, k->klass_part()->name(), _move_ro);
   1.460 +        do_object(k);
   1.461 +      }
   1.462 +
   1.463 +      objArrayOop methods = ik->methods();
   1.464 +      for(i = 0; i < methods->length(); i++) {
   1.465 +        methodOop m = methodOop(methods->obj_at(i));
   1.466 +        mark_and_move_for_policy(OP_favor_startup, m->constMethod(), _move_ro);
   1.467 +        mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->exception_table(), _move_ro);
   1.468 +        mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->stackmap_data(), _move_ro);
   1.469 +
   1.470 +        // We don't move the name symbolOop here because it may invalidate
   1.471 +        // method ordering, which is dependent on the address of the name
   1.472 +        // symbolOop.  It will get promoted later with the other symbols.
   1.473 +        // Method name is rarely accessed during classloading anyway.
   1.474 +        // mark_and_move_for_policy(OP_balanced, m->name(), _move_ro);
   1.475 +
   1.476 +        mark_and_move_for_policy(OP_favor_startup, m->signature(), _move_ro);
   1.477 +      }
   1.478 +
   1.479 +      mark_and_move_for_policy(OP_favor_startup, ik->transitive_interfaces(), _move_ro);
   1.480 +      mark_and_move_for_policy(OP_favor_startup, ik->fields(), _move_ro);
   1.481 +
   1.482 +      mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(),  _move_ro);
   1.483 +      mark_and_move_for_policy(OP_favor_runtime, ik->method_ordering(),   _move_ro);
   1.484 +      mark_and_move_for_policy(OP_favor_runtime, ik->class_annotations(), _move_ro);
   1.485 +      mark_and_move_for_policy(OP_favor_runtime, ik->fields_annotations(), _move_ro);
   1.486 +      mark_and_move_for_policy(OP_favor_runtime, ik->methods_annotations(), _move_ro);
   1.487 +      mark_and_move_for_policy(OP_favor_runtime, ik->methods_parameter_annotations(), _move_ro);
   1.488 +      mark_and_move_for_policy(OP_favor_runtime, ik->methods_default_annotations(), _move_ro);
   1.489 +      mark_and_move_for_policy(OP_favor_runtime, ik->inner_classes(), _move_ro);
   1.490 +      mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro);
   1.491 +    }
   1.492 +  }
   1.493 +};
   1.494 +
   1.495 +class MarkAndMoveOrderedReadWrite: public ObjectClosure {
   1.496 +private:
   1.497 +  MoveMarkedObjects *_move_rw;
   1.498 +
   1.499 +public:
   1.500 +  MarkAndMoveOrderedReadWrite(MoveMarkedObjects *move_rw) : _move_rw(move_rw) {}
   1.501 +
   1.502 +  void do_object(oop obj) {
   1.503 +    if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) {
   1.504 +      instanceKlass* ik = instanceKlass::cast((klassOop)obj);
   1.505 +      int i;
   1.506 +
   1.507 +      mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop(), _move_rw);
   1.508 +
   1.509 +      if (ik->super() != NULL) {
   1.510 +        do_object(ik->super());
   1.511 +      }
   1.512 +
   1.513 +      objArrayOop interfaces = ik->local_interfaces();
   1.514 +      for(i = 0; i < interfaces->length(); i++) {
   1.515 +        klassOop k = klassOop(interfaces->obj_at(i));
   1.516 +        mark_and_move_for_policy(OP_favor_startup, k, _move_rw);
   1.517 +        do_object(k);
   1.518 +      }
   1.519 +
   1.520 +      objArrayOop methods = ik->methods();
   1.521 +      mark_and_move_for_policy(OP_favor_startup, methods, _move_rw);
   1.522 +      for(i = 0; i < methods->length(); i++) {
   1.523 +        methodOop m = methodOop(methods->obj_at(i));
   1.524 +        mark_and_move_for_policy(OP_favor_startup, m, _move_rw);
   1.525 +        mark_and_move_for_policy(OP_favor_startup, ik->constants(), _move_rw);          // idempotent
   1.526 +        mark_and_move_for_policy(OP_balanced, ik->constants()->cache(), _move_rw); // idempotent
   1.527 +        mark_and_move_for_policy(OP_balanced, ik->constants()->tags(), _move_rw);  // idempotent
   1.528 +      }
   1.529 +
   1.530 +      mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop()->klass(), _move_rw);
   1.531 +      mark_and_move_for_policy(OP_favor_startup, ik->constants()->klass(), _move_rw);
   1.532 +
   1.533 +      // Although Java mirrors are marked in MarkReadWriteObjects,
   1.534 +      // apparently they were never moved into shared spaces since
   1.535 +      // MoveMarkedObjects skips marked instance oops.  This may
   1.536 +      // be a bug in the original implementation or simply the vestige
   1.537 +      // of an abandoned experiment.  Nevertheless we leave a hint
   1.538 +      // here in case this capability is ever correctly implemented.
   1.539 +      //
   1.540 +      // mark_and_move_for_policy(OP_favor_runtime, ik->java_mirror(), _move_rw);
   1.541 +    }
   1.542 +  }
   1.543 +
   1.544 +};
   1.545 +
   1.546 +// Adjust references in oops to refer to shared spaces.
   1.547 +
   1.548 +class ResolveForwardingClosure: public OopClosure {
   1.549 +public:
   1.550 +  void do_oop(oop* p) {
   1.551 +    oop obj = *p;
   1.552 +    if (!obj->is_shared()) {
   1.553 +      if (obj != NULL) {
   1.554 +        oop f = obj->forwardee();
   1.555 +        guarantee(f->is_shared(), "Oop doesn't refer to shared space.");
   1.556 +        *p = f;
   1.557 +      }
   1.558 +    }
   1.559 +  }
   1.560 +};
   1.561 +
   1.562 +
   1.563 +void sort_methods(instanceKlass* ik, TRAPS) {
   1.564 +  klassOop super = ik->super();
   1.565 +  if (super != NULL) {
   1.566 +    sort_methods(instanceKlass::cast(super), THREAD);
   1.567 +  }
   1.568 +
   1.569 +  // The methods array must be ordered by symbolOop address. (See
   1.570 +  // classFileParser.cpp where methods in a class are originally
   1.571 +  // sorted.)  Since objects have just be reordered, this must be
   1.572 +  // corrected.
   1.573 +  methodOopDesc::sort_methods(ik->methods(),
   1.574 +                              ik->methods_annotations(),
   1.575 +                              ik->methods_parameter_annotations(),
   1.576 +                              ik->methods_default_annotations(),
   1.577 +                              true /* idempotent, slow */);
   1.578 +
   1.579 +  // Itable indices are calculated based on methods array order
   1.580 +  // (see klassItable::compute_itable_index()).  Must reinitialize.
   1.581 +  // We assume that since checkconstraints is false, this method
   1.582 +  // cannot throw an exception.  An exception here would be
   1.583 +  // problematic since this is the VMThread, not a JavaThread.
   1.584 +  ik->itable()->initialize_itable(false, THREAD);
   1.585 +}
   1.586 +
   1.587 +// Sort methods if the oop is an instanceKlass.
   1.588 +
   1.589 +class SortMethodsClosure: public ObjectClosure {
   1.590 +private:
   1.591 +  Thread* _thread;
   1.592 +
   1.593 +public:
   1.594 +  SortMethodsClosure(Thread* thread) : _thread(thread) {}
   1.595 +
   1.596 +  void do_object(oop obj) {
   1.597 +    // instanceKlass objects need some adjustment.
   1.598 +    if (obj->blueprint()->oop_is_instanceKlass()) {
   1.599 +      instanceKlass* ik = instanceKlass::cast((klassOop)obj);
   1.600 +
   1.601 +      sort_methods(ik, _thread);
   1.602 +    }
   1.603 +  }
   1.604 +};
   1.605 +
   1.606 +
   1.607 +// Adjust references in oops to refer to shared spaces.
   1.608 +
   1.609 +class PatchOopsClosure: public ObjectClosure {
   1.610 +private:
   1.611 +  Thread* _thread;
   1.612 +  ResolveForwardingClosure resolve;
   1.613 +
   1.614 +public:
   1.615 +  PatchOopsClosure(Thread* thread) : _thread(thread) {}
   1.616 +
   1.617 +  void do_object(oop obj) {
   1.618 +    obj->oop_iterate_header(&resolve);
   1.619 +    obj->oop_iterate(&resolve);
   1.620 +
   1.621 +    assert(obj->klass()->is_shared(), "Klass not pointing into shared space.");
   1.622 +
   1.623 +    // If the object is a Java object or class which might (in the
   1.624 +    // future) contain a reference to a young gen object, add it to the
   1.625 +    // list.
   1.626 +
   1.627 +    if (obj->is_klass() || obj->is_instance()) {
   1.628 +      if (obj->is_klass() ||
   1.629 +          obj->is_a(SystemDictionary::class_klass()) ||
   1.630 +          obj->is_a(SystemDictionary::throwable_klass())) {
   1.631 +        // Do nothing
   1.632 +      }
   1.633 +      else if (obj->is_a(SystemDictionary::string_klass())) {
   1.634 +        // immutable objects.
   1.635 +      } else {
   1.636 +        // someone added an object we hadn't accounted for.
   1.637 +        ShouldNotReachHere();
   1.638 +      }
   1.639 +    }
   1.640 +  }
   1.641 +};
   1.642 +
   1.643 +
   1.644 +// Empty the young and old generations.
   1.645 +
   1.646 +class ClearSpaceClosure : public SpaceClosure {
   1.647 +public:
   1.648 +  void do_space(Space* s) {
   1.649 +    s->clear();
   1.650 +  }
   1.651 +};
   1.652 +
   1.653 +
   1.654 +// Closure for serializing initialization data out to a data area to be
   1.655 +// written to the shared file.
   1.656 +
   1.657 +class WriteClosure : public SerializeOopClosure {
   1.658 +private:
   1.659 +  oop* top;
   1.660 +  char* end;
   1.661 +
   1.662 +  void out_of_space() {
   1.663 +    warning("\nThe shared miscellaneous data space is not large "
   1.664 +            "enough to \npreload requested classes.  Use "
   1.665 +            "-XX:SharedMiscDataSize= to increase \nthe initial "
   1.666 +            "size of the miscellaneous data space.\n");
   1.667 +    exit(2);
   1.668 +  }
   1.669 +
   1.670 +
   1.671 +  inline void check_space() {
   1.672 +    if ((char*)top + sizeof(oop) > end) {
   1.673 +      out_of_space();
   1.674 +    }
   1.675 +  }
   1.676 +
   1.677 +
   1.678 +public:
   1.679 +  WriteClosure(char* md_top, char* md_end) {
   1.680 +    top = (oop*)md_top;
   1.681 +    end = md_end;
   1.682 +  }
   1.683 +
   1.684 +  char* get_top() { return (char*)top; }
   1.685 +
   1.686 +  void do_oop(oop* p) {
   1.687 +    check_space();
   1.688 +    oop obj = *p;
   1.689 +    assert(obj->is_oop_or_null(), "invalid oop");
   1.690 +    assert(obj == NULL || obj->is_shared(),
   1.691 +           "Oop in shared space not pointing into shared space.");
   1.692 +    *top = obj;
   1.693 +    ++top;
   1.694 +  }
   1.695 +
   1.696 +  void do_int(int* p) {
   1.697 +    check_space();
   1.698 +    *top = (oop)(intptr_t)*p;
   1.699 +    ++top;
   1.700 +  }
   1.701 +
   1.702 +  void do_size_t(size_t* p) {
   1.703 +    check_space();
   1.704 +    *top = (oop)(intptr_t)*p;
   1.705 +    ++top;
   1.706 +  }
   1.707 +
   1.708 +  void do_ptr(void** p) {
   1.709 +    check_space();
   1.710 +    *top = (oop)*p;
   1.711 +    ++top;
   1.712 +  }
   1.713 +
   1.714 +  void do_ptr(HeapWord** p) { do_ptr((void **) p); }
   1.715 +
   1.716 +  void do_tag(int tag) {
   1.717 +    check_space();
   1.718 +    *top = (oop)(intptr_t)tag;
   1.719 +    ++top;
   1.720 +  }
   1.721 +
   1.722 +  void do_region(u_char* start, size_t size) {
   1.723 +    if ((char*)top + size > end) {
   1.724 +      out_of_space();
   1.725 +    }
   1.726 +    assert((intptr_t)start % sizeof(oop) == 0, "bad alignment");
   1.727 +    assert(size % sizeof(oop) == 0, "bad size");
   1.728 +    do_tag((int)size);
   1.729 +    while (size > 0) {
   1.730 +      *top = *(oop*)start;
   1.731 +      ++top;
   1.732 +      start += sizeof(oop);
   1.733 +      size -= sizeof(oop);
   1.734 +    }
   1.735 +  }
   1.736 +
   1.737 +  bool reading() const { return false; }
   1.738 +};
   1.739 +
   1.740 +
   1.741 +class ResolveConstantPoolsClosure : public ObjectClosure {
   1.742 +private:
   1.743 +  TRAPS;
   1.744 +public:
   1.745 +  ResolveConstantPoolsClosure(Thread *t) {
   1.746 +    __the_thread__ = t;
   1.747 +  }
   1.748 +  void do_object(oop obj) {
   1.749 +    if (obj->is_constantPool()) {
   1.750 +      constantPoolOop cpool = (constantPoolOop)obj;
   1.751 +      int unresolved = cpool->pre_resolve_shared_klasses(THREAD);
   1.752 +    }
   1.753 +  }
   1.754 +};
   1.755 +
   1.756 +
   1.757 +// Print a summary of the contents of the read/write spaces to help
   1.758 +// identify objects which might be able to be made read-only.  At this
   1.759 +// point, the objects have been written, and we can trash them as
   1.760 +// needed.
   1.761 +
   1.762 +static void print_contents() {
   1.763 +  if (PrintSharedSpaces) {
   1.764 +    GenCollectedHeap* gch = GenCollectedHeap::heap();
   1.765 +    CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
   1.766 +
   1.767 +    // High level summary of the read-only space:
   1.768 +
   1.769 +    ClassifyObjectClosure coc;
   1.770 +    tty->cr(); tty->print_cr("ReadOnly space:");
   1.771 +    gen->ro_space()->object_iterate(&coc);
   1.772 +    coc.print();
   1.773 +
   1.774 +    // High level summary of the read-write space:
   1.775 +
   1.776 +    coc.reset();
   1.777 +    tty->cr(); tty->print_cr("ReadWrite space:");
   1.778 +    gen->rw_space()->object_iterate(&coc);
   1.779 +    coc.print();
   1.780 +
   1.781 +    // Reset counters
   1.782 +
   1.783 +    ClearAllocCountClosure cacc;
   1.784 +    gen->ro_space()->object_iterate(&cacc);
   1.785 +    gen->rw_space()->object_iterate(&cacc);
   1.786 +    coc.reset();
   1.787 +
   1.788 +    // Lower level summary of the read-only space:
   1.789 +
   1.790 +    gen->ro_space()->object_iterate(&coc);
   1.791 +    tty->cr(); tty->print_cr("ReadOnly space:");
   1.792 +    ClassifyInstanceKlassClosure cikc;
   1.793 +    gen->rw_space()->object_iterate(&cikc);
   1.794 +    cikc.print();
   1.795 +
   1.796 +    // Reset counters
   1.797 +
   1.798 +    gen->ro_space()->object_iterate(&cacc);
   1.799 +    gen->rw_space()->object_iterate(&cacc);
   1.800 +    coc.reset();
   1.801 +
   1.802 +    // Lower level summary of the read-write space:
   1.803 +
   1.804 +    gen->rw_space()->object_iterate(&coc);
   1.805 +    cikc.reset();
   1.806 +    tty->cr();  tty->print_cr("ReadWrite space:");
   1.807 +    gen->rw_space()->object_iterate(&cikc);
   1.808 +    cikc.print();
   1.809 +  }
   1.810 +}
   1.811 +
   1.812 +
   1.813 +// Patch C++ vtable pointer in klass oops.
   1.814 +
   1.815 +// Klass objects contain references to c++ vtables in the JVM library.
   1.816 +// Fix them to point to our constructed vtables.  However, don't iterate
   1.817 +// across the space while doing this, as that causes the vtables to be
   1.818 +// patched, undoing our useful work.  Instead, iterate to make a list,
   1.819 +// then use the list to do the fixing.
   1.820 +
   1.821 +class PatchKlassVtables: public ObjectClosure {
   1.822 +private:
   1.823 +  void*         _vtbl_ptr;
   1.824 +  VirtualSpace* _md_vs;
   1.825 +  GrowableArray<klassOop>* _klass_objects;
   1.826 +
   1.827 +public:
   1.828 +
   1.829 +  PatchKlassVtables(void* vtbl_ptr, VirtualSpace* md_vs) {
   1.830 +    _vtbl_ptr = vtbl_ptr;
   1.831 +    _md_vs = md_vs;
   1.832 +    _klass_objects = new GrowableArray<klassOop>();
   1.833 +  }
   1.834 +
   1.835 +
   1.836 +  void do_object(oop obj) {
   1.837 +    if (obj->is_klass()) {
   1.838 +      _klass_objects->append(klassOop(obj));
   1.839 +    }
   1.840 +  }
   1.841 +
   1.842 +
   1.843 +  void patch(void** vtbl_list, int vtbl_list_size) {
   1.844 +    for (int i = 0; i < _klass_objects->length(); ++i) {
   1.845 +      klassOop obj = (klassOop)_klass_objects->at(i);
   1.846 +      Klass* k = obj->klass_part();
   1.847 +      void* v =  *(void**)k;
   1.848 +
   1.849 +      int n;
   1.850 +      for (n = 0; n < vtbl_list_size; ++n) {
   1.851 +        *(void**)k = NULL;
   1.852 +        if (vtbl_list[n] == v) {
   1.853 +          *(void**)k = (void**)_vtbl_ptr +
   1.854 +                                 (n * CompactingPermGenGen::num_virtuals);
   1.855 +          break;
   1.856 +        }
   1.857 +      }
   1.858 +      guarantee(n < vtbl_list_size, "unable to find matching vtbl pointer");
   1.859 +    }
   1.860 +  }
   1.861 +};
   1.862 +
   1.863 +
   1.864 +// Populate the shared space.
   1.865 +
   1.866 +class VM_PopulateDumpSharedSpace: public VM_Operation {
   1.867 +private:
   1.868 +  GrowableArray<oop> *_class_promote_order;
   1.869 +  OffsetTableContigSpace* _ro_space;
   1.870 +  OffsetTableContigSpace* _rw_space;
   1.871 +  VirtualSpace* _md_vs;
   1.872 +  VirtualSpace* _mc_vs;
   1.873 +
   1.874 +public:
   1.875 +  VM_PopulateDumpSharedSpace(GrowableArray<oop> *class_promote_order,
   1.876 +                             OffsetTableContigSpace* ro_space,
   1.877 +                             OffsetTableContigSpace* rw_space,
   1.878 +                             VirtualSpace* md_vs, VirtualSpace* mc_vs) {
   1.879 +    _class_promote_order = class_promote_order;
   1.880 +    _ro_space = ro_space;
   1.881 +    _rw_space = rw_space;
   1.882 +    _md_vs = md_vs;
   1.883 +    _mc_vs = mc_vs;
   1.884 +  }
   1.885 +
   1.886 +  VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
   1.887 +  void doit() {
   1.888 +    Thread* THREAD = VMThread::vm_thread();
   1.889 +    NOT_PRODUCT(SystemDictionary::verify();)
   1.890 +    // The following guarantee is meant to ensure that no loader constraints
   1.891 +    // exist yet, since the constraints table is not shared.  This becomes
   1.892 +    // more important now that we don't re-initialize vtables/itables for
   1.893 +    // shared classes at runtime, where constraints were previously created.
   1.894 +    guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
   1.895 +              "loader constraints are not saved");
   1.896 +    GenCollectedHeap* gch = GenCollectedHeap::heap();
   1.897 +
   1.898 +    // At this point, many classes have been loaded.
   1.899 +
   1.900 +    // Update all the fingerprints in the shared methods.
   1.901 +
   1.902 +    tty->print("Calculating fingerprints ... ");
   1.903 +    FingerprintMethodsClosure fpmc;
   1.904 +    gch->object_iterate(&fpmc);
   1.905 +    tty->print_cr("done. ");
   1.906 +
   1.907 +    // Remove all references outside the heap.
   1.908 +
   1.909 +    tty->print("Removing unshareable information ... ");
   1.910 +    RemoveUnshareableInfoClosure ruic;
   1.911 +    gch->object_iterate(&ruic);
   1.912 +    tty->print_cr("done. ");
   1.913 +
   1.914 +    // Move the objects in three passes.
   1.915 +
   1.916 +    MarkObjectsOopClosure mark_all;
   1.917 +    MarkCommonReadOnly mark_common_ro;
   1.918 +    MarkCommonSymbols mark_common_symbols;
   1.919 +    MarkStringValues mark_string_values;
   1.920 +    MarkReadWriteObjects mark_rw;
   1.921 +    MarkStringObjects mark_strings;
   1.922 +    MoveMarkedObjects move_ro(_ro_space, true);
   1.923 +    MoveMarkedObjects move_rw(_rw_space, false);
   1.924 +
   1.925 +    // The SharedOptimizeColdStart VM option governs the new layout
   1.926 +    // algorithm for promoting classes into the shared archive.
   1.927 +    // The general idea is to minimize cold start time by laying
   1.928 +    // out the objects in the order they are accessed at startup time.
   1.929 +    // By doing this we are trying to eliminate out-of-order accesses
   1.930 +    // in the shared archive.  This benefits cold startup time by making
   1.931 +    // disk reads as sequential as possible during class loading and
   1.932 +    // bootstrapping activities.  There may also be a small secondary
   1.933 +    // effect of better "packing" of more commonly used data on a smaller
   1.934 +    // number of pages, although no direct benefit has been measured from
   1.935 +    // this effect.
   1.936 +    //
   1.937 +    // At the class level of granularity, the promotion order is dictated
   1.938 +    // by the classlist file whose generation is discussed elsewhere.
   1.939 +    //
   1.940 +    // At smaller granularity, optimal ordering was determined by an
   1.941 +    // offline analysis of object access order in the shared archive.
   1.942 +    // The dbx watchpoint facility, combined with SA post-processing,
   1.943 +    // was used to observe common access patterns primarily during
   1.944 +    // classloading.  This information was used to craft the promotion
   1.945 +    // order seen in the following closures.
   1.946 +    //
   1.947 +    // The observed access order is mostly governed by what happens
   1.948 +    // in SystemDictionary::load_shared_class().  NOTE WELL - care
   1.949 +    // should be taken when making changes to this method, because it
   1.950 +    // may invalidate assumptions made about access order!
   1.951 +    //
   1.952 +    // (Ideally, there would be a better way to manage changes to
   1.953 +    //  the access order.  Unfortunately a generic in-VM solution for
   1.954 +    //  dynamically observing access order and optimizing shared
   1.955 +    //  archive layout is pretty difficult.  We go with the static
   1.956 +    //  analysis because the code is fairly mature at this point
   1.957 +    //  and we're betting that the access order won't change much.)
   1.958 +
   1.959 +    MarkAndMoveOrderedReadOnly  mark_and_move_ordered_ro(&move_ro);
   1.960 +    MarkAndMoveOrderedReadWrite mark_and_move_ordered_rw(&move_rw);
   1.961 +
   1.962 +    // Phase 1a: move commonly used read-only objects to the read-only space.
   1.963 +
   1.964 +    if (SharedOptimizeColdStart) {
   1.965 +      tty->print("Moving pre-ordered read-only objects to shared space at " PTR_FORMAT " ... ",
   1.966 +                 _ro_space->top());
   1.967 +      for (int i = 0; i < _class_promote_order->length(); i++) {
   1.968 +        oop obj = _class_promote_order->at(i);
   1.969 +        mark_and_move_ordered_ro.do_object(obj);
   1.970 +      }
   1.971 +      tty->print_cr("done. ");
   1.972 +    }
   1.973 +
   1.974 +    tty->print("Moving read-only objects to shared space at " PTR_FORMAT " ... ",
   1.975 +               _ro_space->top());
   1.976 +    gch->object_iterate(&mark_common_ro);
   1.977 +    gch->object_iterate(&move_ro);
   1.978 +    tty->print_cr("done. ");
   1.979 +
   1.980 +    // Phase 1b: move commonly used symbols to the read-only space.
   1.981 +
   1.982 +    tty->print("Moving common symbols to shared space at " PTR_FORMAT " ... ",
   1.983 +               _ro_space->top());
   1.984 +    gch->object_iterate(&mark_common_symbols);
   1.985 +    gch->object_iterate(&move_ro);
   1.986 +    tty->print_cr("done. ");
   1.987 +
   1.988 +    // Phase 1c: move remaining symbols to the read-only space
   1.989 +    // (e.g. String initializers).
   1.990 +
   1.991 +    tty->print("Moving remaining symbols to shared space at " PTR_FORMAT " ... ",
   1.992 +               _ro_space->top());
   1.993 +    vmSymbols::oops_do(&mark_all, true);
   1.994 +    gch->object_iterate(&move_ro);
   1.995 +    tty->print_cr("done. ");
   1.996 +
   1.997 +    // Phase 1d: move String character arrays to the read-only space.
   1.998 +
   1.999 +    tty->print("Moving string char arrays to shared space at " PTR_FORMAT " ... ",
  1.1000 +               _ro_space->top());
  1.1001 +    gch->object_iterate(&mark_string_values);
  1.1002 +    gch->object_iterate(&move_ro);
  1.1003 +    tty->print_cr("done. ");
  1.1004 +
  1.1005 +    // Phase 2: move all remaining symbols to the read-only space.  The
  1.1006 +    // remaining symbols are assumed to be string initializers no longer
  1.1007 +    // referenced.
  1.1008 +
  1.1009 +    void* extra_symbols = _ro_space->top();
  1.1010 +    tty->print("Moving additional symbols to shared space at " PTR_FORMAT " ... ",
  1.1011 +               _ro_space->top());
  1.1012 +    SymbolTable::oops_do(&mark_all);
  1.1013 +    gch->object_iterate(&move_ro);
  1.1014 +    tty->print_cr("done. ");
  1.1015 +    tty->print_cr("Read-only space ends at " PTR_FORMAT ", %d bytes.",
  1.1016 +                  _ro_space->top(), _ro_space->used());
  1.1017 +
  1.1018 +    // Phase 3: move read-write objects to the read-write space, except
  1.1019 +    // Strings.
  1.1020 +
  1.1021 +    if (SharedOptimizeColdStart) {
  1.1022 +      tty->print("Moving pre-ordered read-write objects to shared space at " PTR_FORMAT " ... ",
  1.1023 +                 _rw_space->top());
  1.1024 +      for (int i = 0; i < _class_promote_order->length(); i++) {
  1.1025 +        oop obj = _class_promote_order->at(i);
  1.1026 +        mark_and_move_ordered_rw.do_object(obj);
  1.1027 +      }
  1.1028 +      tty->print_cr("done. ");
  1.1029 +    }
  1.1030 +    tty->print("Moving read-write objects to shared space at " PTR_FORMAT " ... ",
  1.1031 +               _rw_space->top());
  1.1032 +    Universe::oops_do(&mark_all, true);
  1.1033 +    SystemDictionary::oops_do(&mark_all);
  1.1034 +    oop tmp = Universe::arithmetic_exception_instance();
  1.1035 +    mark_object(java_lang_Throwable::message(tmp));
  1.1036 +    gch->object_iterate(&mark_rw);
  1.1037 +    gch->object_iterate(&move_rw);
  1.1038 +    tty->print_cr("done. ");
  1.1039 +
  1.1040 +    // Phase 4: move String objects to the read-write space.
  1.1041 +
  1.1042 +    tty->print("Moving String objects to shared space at " PTR_FORMAT " ... ",
  1.1043 +               _rw_space->top());
  1.1044 +    StringTable::oops_do(&mark_all);
  1.1045 +    gch->object_iterate(&mark_strings);
  1.1046 +    gch->object_iterate(&move_rw);
  1.1047 +    tty->print_cr("done. ");
  1.1048 +    tty->print_cr("Read-write space ends at " PTR_FORMAT ", %d bytes.",
  1.1049 +                  _rw_space->top(), _rw_space->used());
  1.1050 +
  1.1051 +#ifdef DEBUG
  1.1052 +    // Check: scan for objects which were not moved.
  1.1053 +
  1.1054 +    CheckRemainingObjects check_objects;
  1.1055 +    gch->object_iterate(&check_objects);
  1.1056 +    check_objects.status();
  1.1057 +#endif
  1.1058 +
  1.1059 +    // Resolve forwarding in objects and saved C++ structures
  1.1060 +    tty->print("Updating references to shared objects ... ");
  1.1061 +    ResolveForwardingClosure resolve;
  1.1062 +    Universe::oops_do(&resolve);
  1.1063 +    SystemDictionary::oops_do(&resolve);
  1.1064 +    StringTable::oops_do(&resolve);
  1.1065 +    SymbolTable::oops_do(&resolve);
  1.1066 +    vmSymbols::oops_do(&resolve);
  1.1067 +
  1.1068 +    // Set up the share data and shared code segments.
  1.1069 +
  1.1070 +    char* md_top = _md_vs->low();
  1.1071 +    char* md_end = _md_vs->high();
  1.1072 +    char* mc_top = _mc_vs->low();
  1.1073 +    char* mc_end = _mc_vs->high();
  1.1074 +
  1.1075 +    // Reserve space for the list of klassOops whose vtables are used
  1.1076 +    // for patching others as needed.
  1.1077 +
  1.1078 +    void** vtbl_list = (void**)md_top;
  1.1079 +    int vtbl_list_size = CompactingPermGenGen::vtbl_list_size;
  1.1080 +    Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size);
  1.1081 +
  1.1082 +    md_top += vtbl_list_size * sizeof(void*);
  1.1083 +    void* vtable = md_top;
  1.1084 +
  1.1085 +    // Reserve space for a new dummy vtable for klass objects in the
  1.1086 +    // heap.  Generate self-patching vtable entries.
  1.1087 +
  1.1088 +    CompactingPermGenGen::generate_vtable_methods(vtbl_list,
  1.1089 +                                                  &vtable,
  1.1090 +                                                  &md_top, md_end,
  1.1091 +                                                  &mc_top, mc_end);
  1.1092 +
  1.1093 +    // Fix (forward) all of the references in these shared objects (which
  1.1094 +    // are required to point ONLY to objects in the shared spaces).
  1.1095 +    // Also, create a list of all objects which might later contain a
  1.1096 +    // reference to a younger generation object.
  1.1097 +
  1.1098 +    CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
  1.1099 +    PatchOopsClosure patch(THREAD);
  1.1100 +    gen->ro_space()->object_iterate(&patch);
  1.1101 +    gen->rw_space()->object_iterate(&patch);
  1.1102 +
  1.1103 +    // Previously method sorting was done concurrently with forwarding
  1.1104 +    // pointer resolution in the shared spaces.  This imposed an ordering
  1.1105 +    // restriction in that methods were required to be promoted/patched
  1.1106 +    // before their holder classes.  (Because constant pool pointers in
  1.1107 +    // methodKlasses are required to be resolved before their holder class
  1.1108 +    // is visited for sorting, otherwise methods are sorted by incorrect,
  1.1109 +    // pre-forwarding addresses.)
  1.1110 +    //
  1.1111 +    // Now, we reorder methods as a separate step after ALL forwarding
  1.1112 +    // pointer resolution, so that methods can be promoted in any order
  1.1113 +    // with respect to their holder classes.
  1.1114 +
  1.1115 +    SortMethodsClosure sort(THREAD);
  1.1116 +    gen->ro_space()->object_iterate(&sort);
  1.1117 +    gen->rw_space()->object_iterate(&sort);
  1.1118 +    tty->print_cr("done. ");
  1.1119 +    tty->cr();
  1.1120 +
  1.1121 +    // Reorder the system dictionary.  (Moving the symbols opps affects
  1.1122 +    // how the hash table indices are calculated.)
  1.1123 +
  1.1124 +    SystemDictionary::reorder_dictionary();
  1.1125 +
  1.1126 +    // Empty the non-shared heap (because most of the objects were
  1.1127 +    // copied out, and the remainder cannot be considered valid oops).
  1.1128 +
  1.1129 +    ClearSpaceClosure csc;
  1.1130 +    for (int i = 0; i < gch->n_gens(); ++i) {
  1.1131 +      gch->get_gen(i)->space_iterate(&csc);
  1.1132 +    }
  1.1133 +    csc.do_space(gen->the_space());
  1.1134 +    NOT_PRODUCT(SystemDictionary::verify();)
  1.1135 +
  1.1136 +    // Copy the String table, the symbol table, and the system
  1.1137 +    // dictionary to the shared space in usable form.  Copy the hastable
  1.1138 +    // buckets first [read-write], then copy the linked lists of entries
  1.1139 +    // [read-only].
  1.1140 +
  1.1141 +    SymbolTable::reverse(extra_symbols);
  1.1142 +    NOT_PRODUCT(SymbolTable::verify());
  1.1143 +    SymbolTable::copy_buckets(&md_top, md_end);
  1.1144 +
  1.1145 +    StringTable::reverse();
  1.1146 +    NOT_PRODUCT(StringTable::verify());
  1.1147 +    StringTable::copy_buckets(&md_top, md_end);
  1.1148 +
  1.1149 +    SystemDictionary::reverse();
  1.1150 +    SystemDictionary::copy_buckets(&md_top, md_end);
  1.1151 +
  1.1152 +    ClassLoader::verify();
  1.1153 +    ClassLoader::copy_package_info_buckets(&md_top, md_end);
  1.1154 +    ClassLoader::verify();
  1.1155 +
  1.1156 +    SymbolTable::copy_table(&md_top, md_end);
  1.1157 +    StringTable::copy_table(&md_top, md_end);
  1.1158 +    SystemDictionary::copy_table(&md_top, md_end);
  1.1159 +    ClassLoader::verify();
  1.1160 +    ClassLoader::copy_package_info_table(&md_top, md_end);
  1.1161 +    ClassLoader::verify();
  1.1162 +
  1.1163 +    // Print debug data.
  1.1164 +
  1.1165 +    if (PrintSharedSpaces) {
  1.1166 +      const char* fmt = "%s space: " PTR_FORMAT " out of " PTR_FORMAT " bytes allocated at " PTR_FORMAT ".";
  1.1167 +      tty->print_cr(fmt, "ro", _ro_space->used(), _ro_space->capacity(),
  1.1168 +                    _ro_space->bottom());
  1.1169 +      tty->print_cr(fmt, "rw", _rw_space->used(), _rw_space->capacity(),
  1.1170 +                    _rw_space->bottom());
  1.1171 +    }
  1.1172 +
  1.1173 +    // Write the oop data to the output array.
  1.1174 +
  1.1175 +    WriteClosure wc(md_top, md_end);
  1.1176 +    CompactingPermGenGen::serialize_oops(&wc);
  1.1177 +    md_top = wc.get_top();
  1.1178 +
  1.1179 +    // Update the vtable pointers in all of the Klass objects in the
  1.1180 +    // heap. They should point to newly generated vtable.
  1.1181 +
  1.1182 +    PatchKlassVtables pkvt(vtable, _md_vs);
  1.1183 +    _rw_space->object_iterate(&pkvt);
  1.1184 +    pkvt.patch(vtbl_list, vtbl_list_size);
  1.1185 +
  1.1186 +    char* saved_vtbl = (char*)malloc(vtbl_list_size * sizeof(void*));
  1.1187 +    memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*));
  1.1188 +    memset(vtbl_list, 0, vtbl_list_size * sizeof(void*));
  1.1189 +
  1.1190 +    // Create and write the archive file that maps the shared spaces.
  1.1191 +
  1.1192 +    FileMapInfo* mapinfo = new FileMapInfo();
  1.1193 +    mapinfo->populate_header(gch->gen_policy()->max_alignment());
  1.1194 +
  1.1195 +    // Pass 1 - update file offsets in header.
  1.1196 +    mapinfo->write_header();
  1.1197 +    mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true);
  1.1198 +    _ro_space->set_saved_mark();
  1.1199 +    mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false);
  1.1200 +    _rw_space->set_saved_mark();
  1.1201 +    mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(),
  1.1202 +                          md_top - _md_vs->low(), SharedMiscDataSize,
  1.1203 +                          false, false);
  1.1204 +    mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(),
  1.1205 +                          mc_top - _mc_vs->low(), SharedMiscCodeSize,
  1.1206 +                          true, true);
  1.1207 +
  1.1208 +    // Pass 2 - write data.
  1.1209 +    mapinfo->open_for_write();
  1.1210 +    mapinfo->write_header();
  1.1211 +    mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true);
  1.1212 +    mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false);
  1.1213 +    mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(),
  1.1214 +                          md_top - _md_vs->low(), SharedMiscDataSize,
  1.1215 +                          false, false);
  1.1216 +    mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(),
  1.1217 +                          mc_top - _mc_vs->low(), SharedMiscCodeSize,
  1.1218 +                          true, true);
  1.1219 +    mapinfo->close();
  1.1220 +
  1.1221 +    // Summarize heap.
  1.1222 +    memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*));
  1.1223 +    print_contents();
  1.1224 +  }
  1.1225 +}; // class VM_PopulateDumpSharedSpace
  1.1226 +
  1.1227 +
  1.1228 +// Populate the shared spaces and dump to a file.
  1.1229 +
  1.1230 +jint CompactingPermGenGen::dump_shared(GrowableArray<oop>* class_promote_order, TRAPS) {
  1.1231 +  GenCollectedHeap* gch = GenCollectedHeap::heap();
  1.1232 +
  1.1233 +  // Calculate hash values for all of the (interned) strings to avoid
  1.1234 +  // writes to shared pages in the future.
  1.1235 +
  1.1236 +  tty->print("Calculating hash values for String objects .. ");
  1.1237 +  StringHashCodeClosure shcc(THREAD);
  1.1238 +  StringTable::oops_do(&shcc);
  1.1239 +  tty->print_cr("done. ");
  1.1240 +
  1.1241 +  CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
  1.1242 +  VM_PopulateDumpSharedSpace op(class_promote_order,
  1.1243 +                                gen->ro_space(), gen->rw_space(),
  1.1244 +                                gen->md_space(), gen->mc_space());
  1.1245 +  VMThread::execute(&op);
  1.1246 +  return JNI_OK;
  1.1247 +}
  1.1248 +
  1.1249 +
  1.1250 +class LinkClassesClosure : public ObjectClosure {
  1.1251 + private:
  1.1252 +  Thread* THREAD;
  1.1253 +
  1.1254 + public:
  1.1255 +  LinkClassesClosure(Thread* thread) : THREAD(thread) {}
  1.1256 +
  1.1257 +  void do_object(oop obj) {
  1.1258 +    if (obj->is_klass()) {
  1.1259 +      Klass* k = Klass::cast((klassOop) obj);
  1.1260 +      if (k->oop_is_instance()) {
  1.1261 +        instanceKlass* ik = (instanceKlass*) k;
  1.1262 +        // Link the class to cause the bytecodes to be rewritten and the
  1.1263 +        // cpcache to be created.
  1.1264 +        if (ik->get_init_state() < instanceKlass::linked) {
  1.1265 +          ik->link_class(THREAD);
  1.1266 +          guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting");
  1.1267 +        }
  1.1268 +
  1.1269 +        // Create String objects from string initializer symbols.
  1.1270 +        ik->constants()->resolve_string_constants(THREAD);
  1.1271 +        guarantee(!HAS_PENDING_EXCEPTION, "exception resolving string constants");
  1.1272 +      }
  1.1273 +    }
  1.1274 +  }
  1.1275 +};
  1.1276 +
  1.1277 +
  1.1278 +// Support for a simple checksum of the contents of the class list
  1.1279 +// file to prevent trivial tampering. The algorithm matches that in
  1.1280 +// the MakeClassList program used by the J2SE build process.
  1.1281 +#define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe))
  1.1282 +static jlong
  1.1283 +jsum(jlong start, const char *buf, const int len)
  1.1284 +{
  1.1285 +    jlong h = start;
  1.1286 +    char *p = (char *)buf, *e = p + len;
  1.1287 +    while (p < e) {
  1.1288 +        char c = *p++;
  1.1289 +        if (c <= ' ') {
  1.1290 +            /* Skip spaces and control characters */
  1.1291 +            continue;
  1.1292 +        }
  1.1293 +        h = 31 * h + c;
  1.1294 +    }
  1.1295 +    return h;
  1.1296 +}
  1.1297 +
  1.1298 +
  1.1299 +
  1.1300 +
  1.1301 +
  1.1302 +// Preload classes from a list, populate the shared spaces and dump to a
  1.1303 +// file.
  1.1304 +
  1.1305 +void GenCollectedHeap::preload_and_dump(TRAPS) {
  1.1306 +  TraceTime timer("Dump Shared Spaces", TraceStartupTime);
  1.1307 +  ResourceMark rm;
  1.1308 +
  1.1309 +  // Preload classes to be shared.
  1.1310 +  // Should use some hpi:: method rather than fopen() here. aB.
  1.1311 +  // Construct the path to the class list (in jre/lib)
  1.1312 +  // Walk up two directories from the location of the VM and
  1.1313 +  // optionally tack on "lib" (depending on platform)
  1.1314 +  char class_list_path[JVM_MAXPATHLEN];
  1.1315 +  os::jvm_path(class_list_path, sizeof(class_list_path));
  1.1316 +  for (int i = 0; i < 3; i++) {
  1.1317 +    char *end = strrchr(class_list_path, *os::file_separator());
  1.1318 +    if (end != NULL) *end = '\0';
  1.1319 +  }
  1.1320 +  int class_list_path_len = (int)strlen(class_list_path);
  1.1321 +  if (class_list_path_len >= 3) {
  1.1322 +    if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) {
  1.1323 +      strcat(class_list_path, os::file_separator());
  1.1324 +      strcat(class_list_path, "lib");
  1.1325 +    }
  1.1326 +  }
  1.1327 +  strcat(class_list_path, os::file_separator());
  1.1328 +  strcat(class_list_path, "classlist");
  1.1329 +
  1.1330 +  FILE* file = fopen(class_list_path, "r");
  1.1331 +  if (file != NULL) {
  1.1332 +    jlong computed_jsum  = JSUM_SEED;
  1.1333 +    jlong file_jsum      = 0;
  1.1334 +
  1.1335 +    char class_name[256];
  1.1336 +    int class_count = 0;
  1.1337 +    GenCollectedHeap* gch = GenCollectedHeap::heap();
  1.1338 +    gch->_preloading_shared_classes = true;
  1.1339 +    GrowableArray<oop>* class_promote_order = new GrowableArray<oop>();
  1.1340 +
  1.1341 +    // Preload (and intern) strings which will be used later.
  1.1342 +
  1.1343 +    StringTable::intern("main", THREAD);
  1.1344 +    StringTable::intern("([Ljava/lang/String;)V", THREAD);
  1.1345 +    StringTable::intern("Ljava/lang/Class;", THREAD);
  1.1346 +
  1.1347 +    StringTable::intern("I", THREAD);   // Needed for StringBuffer persistence?
  1.1348 +    StringTable::intern("Z", THREAD);   // Needed for StringBuffer persistence?
  1.1349 +
  1.1350 +    // sun.io.Converters
  1.1351 +    static const char obj_array_sig[] = "[[Ljava/lang/Object;";
  1.1352 +    SymbolTable::lookup(obj_array_sig, (int)strlen(obj_array_sig), THREAD);
  1.1353 +
  1.1354 +    // java.util.HashMap
  1.1355 +    static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
  1.1356 +    SymbolTable::lookup(map_entry_array_sig, (int)strlen(map_entry_array_sig),
  1.1357 +                        THREAD);
  1.1358 +
  1.1359 +    tty->print("Loading classes to share ... ");
  1.1360 +    while ((fgets(class_name, sizeof class_name, file)) != NULL) {
  1.1361 +      if (*class_name == '#') {
  1.1362 +        jint fsh, fsl;
  1.1363 +        if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
  1.1364 +          file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
  1.1365 +        }
  1.1366 +
  1.1367 +        continue;
  1.1368 +      }
  1.1369 +      // Remove trailing newline
  1.1370 +      size_t name_len = strlen(class_name);
  1.1371 +      class_name[name_len-1] = '\0';
  1.1372 +
  1.1373 +      computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1);
  1.1374 +
  1.1375 +      // Got a class name - load it.
  1.1376 +      symbolHandle class_name_symbol = oopFactory::new_symbol(class_name,
  1.1377 +                                                              THREAD);
  1.1378 +      guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
  1.1379 +      klassOop klass = SystemDictionary::resolve_or_null(class_name_symbol,
  1.1380 +                                                         THREAD);
  1.1381 +      guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class.");
  1.1382 +      if (klass != NULL) {
  1.1383 +        if (PrintSharedSpaces) {
  1.1384 +          tty->print_cr("Shared spaces preloaded: %s", class_name);
  1.1385 +        }
  1.1386 +
  1.1387 +
  1.1388 +        instanceKlass* ik = instanceKlass::cast(klass);
  1.1389 +
  1.1390 +        // Should be class load order as per -XX:+TraceClassLoadingPreorder
  1.1391 +        class_promote_order->append(ik->as_klassOop());
  1.1392 +
  1.1393 +        // Link the class to cause the bytecodes to be rewritten and the
  1.1394 +        // cpcache to be created. The linking is done as soon as classes
  1.1395 +        // are loaded in order that the related data structures (klass,
  1.1396 +        // cpCache, Sting constants) are located together.
  1.1397 +
  1.1398 +        if (ik->get_init_state() < instanceKlass::linked) {
  1.1399 +          ik->link_class(THREAD);
  1.1400 +          guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
  1.1401 +        }
  1.1402 +
  1.1403 +        // Create String objects from string initializer symbols.
  1.1404 +
  1.1405 +        ik->constants()->resolve_string_constants(THREAD);
  1.1406 +
  1.1407 +        class_count++;
  1.1408 +      } else {
  1.1409 +        if (PrintSharedSpaces) {
  1.1410 +          tty->cr();
  1.1411 +          tty->print_cr(" Preload failed: %s", class_name);
  1.1412 +        }
  1.1413 +      }
  1.1414 +      file_jsum = 0; // Checksum must be on last line of file
  1.1415 +    }
  1.1416 +    if (computed_jsum != file_jsum) {
  1.1417 +      tty->cr();
  1.1418 +      tty->print_cr("Preload failed: checksum of class list was incorrect.");
  1.1419 +      exit(1);
  1.1420 +    }
  1.1421 +
  1.1422 +    tty->print_cr("done. ");
  1.1423 +
  1.1424 +    if (PrintSharedSpaces) {
  1.1425 +      tty->print_cr("Shared spaces: preloaded %d classes", class_count);
  1.1426 +    }
  1.1427 +
  1.1428 +    // Rewrite and unlink classes.
  1.1429 +    tty->print("Rewriting and unlinking classes ... ");
  1.1430 +    // Make heap parsable
  1.1431 +    ensure_parsability(false); // arg is actually don't care
  1.1432 +
  1.1433 +    // Link any classes which got missed.  (It's not quite clear why
  1.1434 +    // they got missed.)  This iteration would be unsafe if we weren't
  1.1435 +    // single-threaded at this point; however we can't do it on the VM
  1.1436 +    // thread because it requires object allocation.
  1.1437 +    LinkClassesClosure lcc(Thread::current());
  1.1438 +    object_iterate(&lcc);
  1.1439 +    tty->print_cr("done. ");
  1.1440 +
  1.1441 +    // Create and dump the shared spaces.
  1.1442 +    jint err = CompactingPermGenGen::dump_shared(class_promote_order, THREAD);
  1.1443 +    if (err != JNI_OK) {
  1.1444 +      fatal("Dumping shared spaces failed.");
  1.1445 +    }
  1.1446 +
  1.1447 +  } else {
  1.1448 +    char errmsg[JVM_MAXPATHLEN];
  1.1449 +    hpi::lasterror(errmsg, JVM_MAXPATHLEN);
  1.1450 +    tty->print_cr("Loading classlist failed: %s", errmsg);
  1.1451 +    exit(1);
  1.1452 +  }
  1.1453 +
  1.1454 +  // Since various initialization steps have been undone by this process,
  1.1455 +  // it is not reasonable to continue running a java process.
  1.1456 +  exit(0);
  1.1457 +}

mercurial