1 /*
  2  * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "cds/archiveBuilder.hpp"
 27 #include "cds/archiveHeapLoader.inline.hpp"
 28 #include "cds/archiveUtils.hpp"
 29 #include "cds/cdsConfig.hpp"
 30 #include "cds/classListParser.hpp"
 31 #include "cds/classListWriter.hpp"
 32 #include "cds/dynamicArchive.hpp"
 33 #include "cds/filemap.hpp"
 34 #include "cds/heapShared.hpp"
 35 #include "cds/metaspaceShared.hpp"
 36 #include "classfile/classLoader.hpp"
 37 #include "classfile/systemDictionaryShared.hpp"
 38 #include "classfile/vmClasses.hpp"
 39 #include "interpreter/bootstrapInfo.hpp"
 40 #include "memory/metaspaceUtils.hpp"
 41 #include "memory/resourceArea.hpp"
 42 #include "oops/compressedOops.inline.hpp"
 43 #include "oops/klass.inline.hpp"
 44 #include "runtime/arguments.hpp"
 45 #include "utilities/bitMap.inline.hpp"
 46 #include "utilities/debug.hpp"
 47 #include "utilities/formatBuffer.hpp"
 48 #include "utilities/globalDefinitions.hpp"
 49 #include "utilities/spinYield.hpp"
 50 
 51 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
 52 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
 53 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;
 54 CHeapBitMap* ArchivePtrMarker::_cc_ptrmap = nullptr;
 55 VirtualSpace* ArchivePtrMarker::_vs;
 56 
 57 bool ArchivePtrMarker::_compacted;
 58 
 59 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
 60   assert(_ptrmap == nullptr, "initialize only once");
 61   assert(_rw_ptrmap == nullptr, "initialize only once");
 62   assert(_ro_ptrmap == nullptr, "initialize only once");
 63   assert(_cc_ptrmap == nullptr, "initialize only once");
 64   _vs = vs;
 65   _compacted = false;
 66   _ptrmap = ptrmap;
 67 
 68   // Use this as initial guesstimate. We should need less space in the
 69   // archive, but if we're wrong the bitmap will be expanded automatically.
 70   size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
 71   // But set it smaller in debug builds so we always test the expansion code.
 72   // (Default archive is about 12MB).
 73   DEBUG_ONLY(estimated_archive_size = 6 * M);
 74 
 75   // We need one bit per pointer in the archive.
 76   _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
 77 }
 78 
 79 void ArchivePtrMarker::initialize_rw_ro_cc_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* cc_ptrmap) {
 80   address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
 81   address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
 82   address* cc_bottom = (address*)ArchiveBuilder::current()->cc_region()->base();
 83 
 84   _rw_ptrmap = rw_ptrmap;
 85   _ro_ptrmap = ro_ptrmap;
 86   _cc_ptrmap = cc_ptrmap;
 87 
 88   size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
 89   size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
 90   size_t cc_size = ArchiveBuilder::current()->cc_region()->used() / sizeof(address);
 91   // ro_start is the first bit in _ptrmap that covers the pointer that would sit at ro_bottom.
 92   // E.g., if rw_bottom = (address*)100
 93   //          ro_bottom = (address*)116
 94   //       then for 64-bit platform:
 95   //          ro_start = ro_bottom - rw_bottom = (116 - 100) / sizeof(address) = 2;
 96   size_t ro_start = ro_bottom - rw_bottom;
 97   size_t cc_start = cc_bottom - rw_bottom;
 98 
 99   // Note: ptrmap is big enough only to cover the last pointer in cc_region or ro_region.
100   // See ArchivePtrMarker::compact()
101   if (ro_start + ro_size >_ptrmap->size()) {
102     ro_size = _ptrmap->size() - ro_start; // ro is smaller than we thought
103     cc_size = 0;                          // cc is empty
104   } else if (cc_size != 0 && cc_start + cc_size > _ptrmap->size()) {
105     cc_size = _ptrmap->size() - cc_start; // ro is smaller than we thought
106   }
107 
108   assert(rw_size < _ptrmap->size(), "sanity");
109   assert(ro_size < _ptrmap->size(), "sanity");
110   assert(cc_size < _ptrmap->size(), "sanity");
111   assert(rw_size + ro_size + cc_size <= _ptrmap->size(), "sanity");
112 
113   _rw_ptrmap->initialize(rw_size);
114   _ro_ptrmap->initialize(ro_size);
115   _cc_ptrmap->initialize(cc_size);
116 
117   for (size_t i = 0; i < rw_size; i++) {
118     _rw_ptrmap->at_put(i, _ptrmap->at(i));
119   }
120   for (size_t i = 0; i < ro_size; i++) {
121     _ro_ptrmap->at_put(i, _ptrmap->at(ro_start + i));
122   }
123   for (size_t i = 0; i < cc_size; i++) {
124     _cc_ptrmap->at_put(i, _ptrmap->at(cc_start + i));
125   }
126 }
127 
128 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
129   assert(_ptrmap != nullptr, "not initialized");
130   assert(!_compacted, "cannot mark anymore");
131 
132   if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
133     address value = *ptr_loc;
134     // We don't want any pointer that points to very bottom of the archive, otherwise when
135     // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
136     // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
137     // of the archive.
138     assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
139 
140     if (value != nullptr) {
141       assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
142       size_t idx = ptr_loc - ptr_base();
143       if (_ptrmap->size() <= idx) {
144         _ptrmap->resize((idx + 1) * 2);
145       }
146       assert(idx < _ptrmap->size(), "must be");
147       _ptrmap->set_bit(idx);
148       //tty->print_cr("Marking pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx);
149     }
150   }
151 }
152 
153 void ArchivePtrMarker::clear_pointer(address* ptr_loc) {
154   assert(_ptrmap != nullptr, "not initialized");
155   assert(!_compacted, "cannot clear anymore");
156 
157   assert(ptr_base() <= ptr_loc && ptr_loc < ptr_end(), "must be");
158   assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
159   size_t idx = ptr_loc - ptr_base();
160   assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked");
161   _ptrmap->clear_bit(idx);
162   //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx);
163 }
164 
165 class ArchivePtrBitmapCleaner: public BitMapClosure {
166   CHeapBitMap* _ptrmap;
167   address* _ptr_base;
168   address  _relocatable_base;
169   address  _relocatable_end;
170   size_t   _max_non_null_offset;
171 
172 public:
173   ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) :
174     _ptrmap(ptrmap), _ptr_base(ptr_base),
175     _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {}
176 
177   bool do_bit(size_t offset) {
178     address* ptr_loc = _ptr_base + offset;
179     address  ptr_value = *ptr_loc;
180     if (ptr_value != nullptr) {
181       assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!");
182       if (_max_non_null_offset < offset) {
183         _max_non_null_offset = offset;
184       }
185     } else {
186       _ptrmap->clear_bit(offset);
187       DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT  "] -> null @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset));
188     }
189 
190     return true;
191   }
192 
193   size_t max_non_null_offset() const { return _max_non_null_offset; }
194 };
195 
196 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) {
197   assert(!_compacted, "cannot compact again");
198   ArchivePtrBitmapCleaner cleaner(_ptrmap, ptr_base(), relocatable_base, relocatable_end);
199   _ptrmap->iterate(&cleaner);
200   compact(cleaner.max_non_null_offset());
201 }
202 
203 void ArchivePtrMarker::compact(size_t max_non_null_offset) {
204   assert(!_compacted, "cannot compact again");
205   _ptrmap->resize(max_non_null_offset + 1);
206   _compacted = true;
207 }
208 
209 char* DumpRegion::expand_top_to(char* newtop) {
210   assert(is_allocatable(), "must be initialized and not packed");
211   assert(newtop >= _top, "must not grow backwards");
212   if (newtop > _end) {
213     ArchiveBuilder::current()->report_out_of_space(_name, newtop - _top);
214     ShouldNotReachHere();
215   }
216 
217   commit_to(newtop);
218   _top = newtop;
219 
220   if (_max_delta > 0) {
221     uintx delta = ArchiveBuilder::current()->buffer_to_offset((address)(newtop-1));
222     if (delta > _max_delta) {
223       // This is just a sanity check and should not appear in any real world usage. This
224       // happens only if you allocate more than 2GB of shared objects and would require
225       // millions of shared classes.
226       log_error(cds)("Out of memory in the CDS archive: Please reduce the number of shared classes.");
227       MetaspaceShared::unrecoverable_writing_error();
228     }
229   }
230 
231   return _top;
232 }
233 
234 void DumpRegion::commit_to(char* newtop) {
235   assert(CDSConfig::is_dumping_archive(), "sanity");
236   char* base = _rs->base();
237   size_t need_committed_size = newtop - base;
238   size_t has_committed_size = _vs->committed_size();
239   if (need_committed_size < has_committed_size) {
240     return;
241   }
242 
243   size_t min_bytes = need_committed_size - has_committed_size;
244   size_t preferred_bytes = 1 * M;
245   size_t uncommitted = _vs->reserved_size() - has_committed_size;
246 
247   size_t commit = MAX2(min_bytes, preferred_bytes);
248   commit = MIN2(commit, uncommitted);
249   assert(commit <= uncommitted, "sanity");
250 
251   if (!_vs->expand_by(commit, false)) {
252     log_error(cds)("Failed to expand shared space to " SIZE_FORMAT " bytes",
253                     need_committed_size);
254     MetaspaceShared::unrecoverable_writing_error();
255   }
256 
257   const char* which;
258   if (_rs->base() == (char*)MetaspaceShared::symbol_rs_base()) {
259     which = "symbol";
260   } else {
261     which = "shared";
262   }
263   log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9)  " bytes ending at %p]",
264                  which, commit, _vs->actual_committed_size(), _vs->high());
265 }
266 
267 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) {
268   // Always align to at least minimum alignment
269   alignment = MAX2(SharedSpaceObjectAlignment, alignment);
270   char* p = (char*)align_up(_top, alignment);
271   char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
272   expand_top_to(newtop);
273   memset(p, 0, newtop - p);
274   return p;
275 }
276 
277 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
278   assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
279   intptr_t *p = (intptr_t*)_top;
280   char* newtop = _top + sizeof(intptr_t);
281   expand_top_to(newtop);
282   *p = n;
283   if (need_to_mark) {
284     ArchivePtrMarker::mark_pointer(p);
285   }
286 }
287 
288 void DumpRegion::print(size_t total_bytes) const {
289   char* base = used() > 0 ? ArchiveBuilder::current()->to_requested(_base) : nullptr;
290   log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
291                  _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
292                  p2i(base));
293 }
294 
295 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
296   log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
297                  _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
298   if (strcmp(_name, failing_region) == 0) {
299     log_error(cds)(" required = %d", int(needed_bytes));
300   }
301 }
302 
303 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
304   _rs = rs;
305   _vs = vs;
306   // Start with 0 committed bytes. The memory will be committed as needed.
307   if (!_vs->initialize(*_rs, 0)) {
308     fatal("Unable to allocate memory for shared space");
309   }
310   _base = _top = _rs->base();
311   _end = _rs->end();
312 }
313 
314 void DumpRegion::pack(DumpRegion* next) {
315   if (!is_packed()) {
316     _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
317     _is_packed = true;
318   }
319   if (next != nullptr) {
320     next->_rs = _rs;
321     next->_vs = _vs;
322     next->_base = next->_top = this->_end;
323     next->_end = _rs->end();
324   }
325 }
326 
327 void WriteClosure::do_ptr(void** p) {
328   // Write ptr into the archive; ptr can be:
329   //   (a) null                 -> written as 0
330   //   (b) a "buffered" address -> written as is
331   //   (c) a "source"   address -> convert to "buffered" and write
332   // The common case is (c). E.g., when writing the vmClasses into the archive.
333   // We have (b) only when we don't have a corresponding source object. E.g.,
334   // the archived c++ vtable entries.
335   address ptr = *(address*)p;
336   if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
337     ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
338   }
339   // null pointers do not need to be converted to offsets
340   if (ptr != nullptr) {
341     ptr = (address)ArchiveBuilder::current()->buffer_to_offset(ptr);
342   }
343   _dump_region->append_intptr_t((intptr_t)ptr, false);
344 }
345 
346 void ReadClosure::do_ptr(void** p) {
347   assert(*p == nullptr, "initializing previous initialized pointer.");
348   intptr_t obj = nextPtr();
349   assert(obj >= 0, "sanity.");
350   *p = (obj != 0) ? (void*)(_base_address + obj) : (void*)obj;
351 }
352 
353 void ReadClosure::do_u4(u4* p) {
354   intptr_t obj = nextPtr();
355   *p = (u4)(uintx(obj));
356 }
357 
358 void ReadClosure::do_int(int* p) {
359   intptr_t obj = nextPtr();
360   *p = (int)(intx(obj));
361 }
362 
363 void ReadClosure::do_bool(bool* p) {
364   intptr_t obj = nextPtr();
365   *p = (bool)(uintx(obj));
366 }
367 
368 void ReadClosure::do_tag(int tag) {
369   int old_tag;
370   old_tag = (int)(intptr_t)nextPtr();
371   // do_int(&old_tag);
372   assert(tag == old_tag, "tag doesn't match (%d, expected %d)", old_tag, tag);
373   FileMapInfo::assert_mark(tag == old_tag);
374 }
375 
376 void ArchiveUtils::log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) {
377   if (ClassListWriter::is_enabled()) {
378     if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) {
379       const constantPoolHandle& pool = bootstrap_specifier->pool();
380       if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
381         // Currently lambda proxy classes are supported only for the built-in loaders.
382         ResourceMark rm(THREAD);
383         int pool_index = bootstrap_specifier->bss_index();
384         ClassListWriter w;
385         w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
386         CDSIndyInfo cii;
387         ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
388         GrowableArray<const char*>* indy_items = cii.items();
389         for (int i = 0; i < indy_items->length(); i++) {
390           w.stream()->print(" %s", indy_items->at(i));
391         }
392         w.stream()->cr();
393       }
394     }
395   }
396 }
397 
398 
399 // "boot", "platform", "app" or nullptr
400 const char* ArchiveUtils::builtin_loader_name_or_null(oop loader) {
401   if (loader == nullptr) {
402     return "boot";
403   } else if (loader == SystemDictionary::java_platform_loader()) {
404     return "platform";
405   } else if (loader == SystemDictionary::java_system_loader()) {
406     return "app";
407   } else {
408     return nullptr;
409   }
410 }
411 
412 // "boot", "platform", "app". Asserts if not a built-in-loader
413 const char* ArchiveUtils::builtin_loader_name(oop loader) {
414   const char* name = builtin_loader_name_or_null(loader);
415   assert(name != nullptr, "must be a built-in loader");
416   return name;
417 }
418 
419 bool ArchiveUtils::builtin_loader_from_type(const char* loader_type, oop* value_ret) {
420   if (strcmp(loader_type, "boot") == 0) {
421     *value_ret = nullptr;
422     return true;
423   } else if (strcmp(loader_type, "platform") == 0) {
424     *value_ret = SystemDictionary::java_platform_loader();
425     return true;
426   } else if (strcmp(loader_type, "app") == 0) {
427     *value_ret = SystemDictionary::java_system_loader();
428     return true;
429   } else {
430     DEBUG_ONLY(*value_ret = cast_to_oop((void*)badOopVal));
431     return false;
432   }
433 }
434 
435 oop ArchiveUtils::builtin_loader_from_type(int loader_type) {
436   if (loader_type == ClassLoader::BOOT_LOADER) {
437     return nullptr;
438   } else if (loader_type == ClassLoader::PLATFORM_LOADER)  {
439     return SystemDictionary::java_platform_loader();
440   } else if (loader_type == ClassLoader::APP_LOADER) {
441     return SystemDictionary::java_system_loader();
442   } else {
443     ShouldNotReachHere();
444     return nullptr;
445   }
446 }
447 
448 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
449   if (SystemDictionaryShared::is_excluded_class(src_ik)) {
450     assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
451     return false;
452   }
453   return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
454 }
455 
456 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
457   assert(seg_idx < _count, "In range");
458   return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
459 }
460 
461 int HeapRootSegments::size_in_elems(size_t seg_idx) {
462   assert(seg_idx < _count, "In range");
463   if (seg_idx != _count - 1) {
464     return _max_size_in_elems;
465   } else {
466     // Last slice, leftover
467     return _roots_count % _max_size_in_elems;
468   }
469 }
470 
471 size_t HeapRootSegments::segment_offset(size_t seg_idx) {
472   assert(seg_idx < _count, "In range");
473   return _base_offset + seg_idx * _max_size_in_bytes;
474 }
475 
476 ArchiveWorkers::ArchiveWorkers() :
477         _end_semaphore(0),
478         _num_workers(max_workers()),
479         _started_workers(0),
480         _finish_tokens(0),
481         _state(UNUSED),
482         _task(nullptr) {}
483 
484 ArchiveWorkers::~ArchiveWorkers() {
485   assert(Atomic::load(&_state) != WORKING, "Should not be working");
486 }
487 
488 int ArchiveWorkers::max_workers() {
489   // The pool is used for short-lived bursty tasks. We do not want to spend
490   // too much time creating and waking up threads unnecessarily. Plus, we do
491   // not want to overwhelm large machines. This is why we want to be very
492   // conservative about the number of workers actually needed.
493   return MAX2(0, log2i_graceful(os::active_processor_count()));
494 }
495 
496 bool ArchiveWorkers::is_parallel() {
497   return _num_workers > 0;
498 }
499 
500 void ArchiveWorkers::start_worker_if_needed() {
501   while (true) {
502     int cur = Atomic::load(&_started_workers);
503     if (cur >= _num_workers) {
504       return;
505     }
506     if (Atomic::cmpxchg(&_started_workers, cur, cur + 1, memory_order_relaxed) == cur) {
507       new ArchiveWorkerThread(this);
508       return;
509     }
510   }
511 }
512 
513 void ArchiveWorkers::run_task(ArchiveWorkerTask* task) {
514   assert(Atomic::load(&_state) == UNUSED, "Should be unused yet");
515   assert(Atomic::load(&_task) == nullptr, "Should not have running tasks");
516   Atomic::store(&_state, WORKING);
517 
518   if (is_parallel()) {
519     run_task_multi(task);
520   } else {
521     run_task_single(task);
522   }
523 
524   assert(Atomic::load(&_state) == WORKING, "Should be working");
525   Atomic::store(&_state, SHUTDOWN);
526 }
527 
528 void ArchiveWorkers::run_task_single(ArchiveWorkerTask* task) {
529   // Single thread needs no chunking.
530   task->configure_max_chunks(1);
531 
532   // Execute the task ourselves, as there are no workers.
533   task->work(0, 1);
534 }
535 
536 void ArchiveWorkers::run_task_multi(ArchiveWorkerTask* task) {
537   // Multiple threads can work with multiple chunks.
538   task->configure_max_chunks(_num_workers * CHUNKS_PER_WORKER);
539 
540   // Set up the run and publish the task. Issue one additional finish token
541   // to cover the semaphore shutdown path, see below.
542   Atomic::store(&_finish_tokens, _num_workers + 1);
543   Atomic::release_store(&_task, task);
544 
545   // Kick off pool startup by starting a single worker, and proceed
546   // immediately to executing the task locally.
547   start_worker_if_needed();
548 
549   // Execute the task ourselves, while workers are catching up.
550   // This allows us to hide parts of task handoff latency.
551   task->run();
552 
553   // Done executing task locally, wait for any remaining workers to complete.
554   // Once all workers report, we can proceed to termination. To do this safely,
555   // we need to make sure every worker has left. A spin-wait alone would suffice,
556   // but we do not want to burn cycles on it. A semaphore alone would not be safe,
557   // since workers can still be inside it as we proceed from wait here. So we block
558   // on semaphore first, and then spin-wait for all workers to terminate.
559   _end_semaphore.wait();
560   SpinYield spin;
561   while (Atomic::load(&_finish_tokens) != 0) {
562     spin.wait();
563   }
564 
565   OrderAccess::fence();
566 
567   assert(Atomic::load(&_finish_tokens) == 0, "All tokens are consumed");
568 }
569 
570 void ArchiveWorkers::run_as_worker() {
571   assert(is_parallel(), "Should be in parallel mode");
572 
573   ArchiveWorkerTask* task = Atomic::load_acquire(&_task);
574   task->run();
575 
576   // All work done in threads should be visible to caller.
577   OrderAccess::fence();
578 
579   // Signal the pool the work is complete, and we are exiting.
580   // Worker cannot do anything else with the pool after this.
581   if (Atomic::sub(&_finish_tokens, 1, memory_order_relaxed) == 1) {
582     // Last worker leaving. Notify the pool it can unblock to spin-wait.
583     // Then consume the last token and leave.
584     _end_semaphore.signal();
585     int last = Atomic::sub(&_finish_tokens, 1, memory_order_relaxed);
586     assert(last == 0, "Should be");
587   }
588 }
589 
590 void ArchiveWorkerTask::run() {
591   while (true) {
592     int chunk = Atomic::load(&_chunk);
593     if (chunk >= _max_chunks) {
594       return;
595     }
596     if (Atomic::cmpxchg(&_chunk, chunk, chunk + 1, memory_order_relaxed) == chunk) {
597       assert(0 <= chunk && chunk < _max_chunks, "Sanity");
598       work(chunk, _max_chunks);
599     }
600   }
601 }
602 
603 void ArchiveWorkerTask::configure_max_chunks(int max_chunks) {
604   if (_max_chunks == 0) {
605     _max_chunks = max_chunks;
606   }
607 }
608 
609 ArchiveWorkerThread::ArchiveWorkerThread(ArchiveWorkers* pool) : NamedThread(), _pool(pool) {
610   set_name("ArchiveWorkerThread");
611   if (os::create_thread(this, os::os_thread)) {
612     os::start_thread(this);
613   } else {
614     vm_exit_during_initialization("Unable to create archive worker",
615                                   os::native_thread_creation_failed_msg());
616   }
617 }
618 
619 void ArchiveWorkerThread::run() {
620   // Avalanche startup: each worker starts two others.
621   _pool->start_worker_if_needed();
622   _pool->start_worker_if_needed();
623 
624   // Set ourselves up.
625   os::set_priority(this, NearMaxPriority);
626 
627   // Work.
628   _pool->run_as_worker();
629 }
630 
631 void ArchiveWorkerThread::post_run() {
632   this->NamedThread::post_run();
633   delete this;
634 }