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