forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 11
/
pas_thread_local_cache.c
1232 lines (954 loc) · 46.8 KB
/
pas_thread_local_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "pas_config.h"
#if LIBPAS_ENABLED
#include "pas_thread_local_cache.h"
#include "pas_all_heap_configs.h"
#include "pas_bitvector.h"
#include "pas_committed_pages_vector.h"
#include "pas_debug_heap.h"
#include "pas_heap_lock.h"
#include "pas_large_utility_free_heap.h"
#include "pas_log.h"
#include "pas_monotonic_time.h"
#include "pas_scavenger.h"
#include "pas_segregated_size_directory_inlines.h"
#include "pas_segregated_page.h"
#include "pas_thread_local_cache_layout.h"
#include "pas_thread_local_cache_node.h"
#include "pas_thread_suspend_lock.h"
#include <unistd.h>
#if PAS_OS(DARWIN)
#include <mach/thread_act.h>
#endif
#if PAS_PLATFORM(PLAYSTATION)
#include <pthread_np.h>
#endif
PAS_BEGIN_EXTERN_C;
#if PAS_HAVE_THREAD_KEYWORD
__thread void* pas_thread_local_cache_pointer = NULL;
#endif
pas_fast_tls pas_thread_local_cache_fast_tls = PAS_FAST_TLS_INITIALIZER;
size_t pas_thread_local_cache_size_for_allocator_index_capacity(unsigned allocator_index_capacity)
{
size_t result;
result = PAS_OFFSETOF(pas_thread_local_cache, local_allocators)
+ 8 * allocator_index_capacity;
return pas_round_up_to_power_of_2(result, pas_page_malloc_alignment());
}
static size_t num_pages(unsigned allocator_index_capacity)
{
return pas_thread_local_cache_size_for_allocator_index_capacity(allocator_index_capacity)
>> pas_page_malloc_alignment_shift();
}
static void compute_page_indices(pas_allocator_index begin_allocator_index,
pas_allocator_index end_allocator_index,
uintptr_t* begin_page_index,
uintptr_t* end_page_index)
{
size_t page_size_shift;
page_size_shift = pas_page_malloc_alignment_shift();
*begin_page_index = pas_thread_local_cache_offset_of_allocator(begin_allocator_index) >> page_size_shift;
*end_page_index =
((pas_thread_local_cache_offset_of_allocator(end_allocator_index) - 1) >> page_size_shift) + 1;
}
static size_t pages_committed_size(unsigned allocator_index_capacity)
{
return PAS_BITVECTOR_NUM_BYTES(num_pages(allocator_index_capacity));
}
static void deallocate(pas_thread_local_cache* thread_local_cache)
{
char* begin;
size_t size;
pas_large_utility_free_heap_deallocate(
thread_local_cache->should_stop_bitvector,
PAS_BITVECTOR_NUM_BYTES(thread_local_cache->allocator_index_capacity));
pas_large_utility_free_heap_deallocate(
thread_local_cache->pages_committed,
pages_committed_size(thread_local_cache->allocator_index_capacity));
begin = (char*)thread_local_cache;
size = pas_thread_local_cache_size_for_allocator_index_capacity(
thread_local_cache->allocator_index_capacity);
/* If we're doing symmetric decommit, then we need to commit the memory for the TLC now. */
pas_page_malloc_commit_without_mprotect(begin, size, pas_may_mmap);
pas_large_utility_free_heap_deallocate(begin, size);
}
static void destroy(pas_thread_local_cache* thread_local_cache, pas_lock_hold_mode heap_lock_hold_mode)
{
static const bool verbose = false;
pas_heap_lock_lock_conditionally(heap_lock_hold_mode);
if (verbose)
pas_log("[%d] Destroying TLC %p\n", getpid(), thread_local_cache);
pas_thread_local_cache_shrink(thread_local_cache, pas_lock_is_held);
pas_thread_local_cache_node_deallocate(thread_local_cache->node);
deallocate(thread_local_cache);
pas_heap_lock_unlock_conditionally(heap_lock_hold_mode);
}
static void destructor(void* arg)
{
static const bool verbose = false;
pas_thread_local_cache* thread_local_cache;
thread_local_cache = (pas_thread_local_cache*)arg;
if (verbose)
pas_log("[%d] Destructor call for TLS %p\n", getpid(), thread_local_cache);
#if !PAS_OS(DARWIN)
/* If pthread_self_is_exiting_np does not exist, we set PAS_THREAD_LOCAL_CACHE_DESTROYED in the TLS so that
subsequent calls of pas_thread_local_cache_try_get() can detect whether TLS is destroyed. Since
PAS_THREAD_LOCAL_CACHE_DESTROYED is a non-null value, pthread will call this destructor again (up to
PTHREAD_DESTRUCTOR_ITERATIONS times). Each time it does, it will clear the TLS entry. Hence, we need to re-set
PAS_THREAD_LOCAL_CACHE_DESTROYED in the TLS each time to continue to indicate that destroy() has already been called once. */
pas_thread_local_cache_set_impl((pas_thread_local_cache*)PAS_THREAD_LOCAL_CACHE_DESTROYED);
PAS_ASSERT(!pas_thread_local_cache_can_set());
#endif
if (((uintptr_t)thread_local_cache) != PAS_THREAD_LOCAL_CACHE_DESTROYED)
destroy(thread_local_cache, pas_lock_is_not_held);
else {
if (verbose)
pas_log("[%d] Repeated destructor call for TLS %p\n", getpid(), thread_local_cache);
}
}
static pas_thread_local_cache* allocate_cache(unsigned allocator_index_capacity)
{
static const bool verbose = false;
size_t size;
pas_thread_local_cache* result;
size_t index;
PAS_ASSERT(allocator_index_capacity >= PAS_LOCAL_ALLOCATOR_UNSELECTED_NUM_INDICES);
size = pas_thread_local_cache_size_for_allocator_index_capacity(allocator_index_capacity);
if (verbose)
printf("Cache size: %zu\n", size);
result = (pas_thread_local_cache*)pas_large_utility_free_heap_allocate_with_alignment(
size, pas_alignment_create_traditional(pas_page_malloc_alignment()), "pas_thread_local_cache");
pas_zero_memory(result, size);
result->should_stop_bitvector = (unsigned int*)pas_large_utility_free_heap_allocate(
PAS_BITVECTOR_NUM_BYTES(allocator_index_capacity),
"pas_thread_local_cache/should_stop_bitvector");
pas_zero_memory(result->should_stop_bitvector, PAS_BITVECTOR_NUM_BYTES(allocator_index_capacity));
PAS_ASSERT(pages_committed_size(allocator_index_capacity));
result->pages_committed = (unsigned*)pas_large_utility_free_heap_allocate(
pages_committed_size(allocator_index_capacity),
"pas_thread_local_cache/pages_committed");
pas_zero_memory(result->pages_committed, pages_committed_size(allocator_index_capacity));
for (index = num_pages(allocator_index_capacity); index--;)
pas_bitvector_set(result->pages_committed, index, true);
result->allocator_index_capacity = allocator_index_capacity;
return result;
}
static void dump_thread_diagnostics(pthread_t thread)
{
uint64_t thread_id;
char thread_name[256];
int getname_result;
PAS_UNUSED_PARAM(thread_id);
#if PAS_OS(DARWIN)
if (!pthread_threadid_np(thread, &thread_id))
pas_log("[%d] thread %p has id %llu\n", getpid(), thread, thread_id);
else
pas_log("[%d] thread %p does not have id\n", getpid(), thread);
#endif
#if PAS_PLATFORM(PLAYSTATION)
getname_result = pthread_get_name_np(thread, thread_name);
#else
getname_result = pthread_getname_np(thread, thread_name, sizeof(thread_name));
#endif
if (!getname_result)
pas_log("[%d] thread %p has name %s\n", getpid(), (void*)thread, thread_name);
else
pas_log("[%d] thread %p does not have name\n", getpid(), (void*)thread);
}
pas_thread_local_cache* pas_thread_local_cache_create(void)
{
static const bool verbose = false;
pas_thread_local_cache* thread_local_cache;
unsigned allocator_index_upper_bound;
pas_thread_local_cache_layout_node layout_node;
allocator_index_upper_bound = pas_thread_local_cache_layout_next_allocator_index;
thread_local_cache = allocate_cache(allocator_index_upper_bound);
thread_local_cache->node = pas_thread_local_cache_node_allocate();
if (verbose) {
pas_log("[%d] TLC %p created with thread %p\n", getpid(), thread_local_cache, (void*)pthread_self());
dump_thread_diagnostics(pthread_self());
}
thread_local_cache->thread = pthread_self();
pas_compiler_fence();
thread_local_cache->allocator_index_upper_bound = allocator_index_upper_bound;
pas_local_allocator_construct_unselected(
(pas_local_allocator*)pas_thread_local_cache_get_local_allocator_direct_unchecked(
thread_local_cache, PAS_LOCAL_ALLOCATOR_UNSELECTED_INDEX));
for (PAS_THREAD_LOCAL_CACHE_LAYOUT_EACH_ALLOCATOR(layout_node))
pas_thread_local_cache_layout_node_commit_and_construct(layout_node, thread_local_cache);
pas_compiler_fence();
thread_local_cache->node->cache = thread_local_cache;
pas_thread_local_cache_set(thread_local_cache);
return thread_local_cache;
}
void pas_thread_local_cache_destroy(pas_lock_hold_mode heap_lock_hold_mode)
{
static const bool verbose = false;
pas_thread_local_cache* thread_local_cache;
thread_local_cache = pas_thread_local_cache_try_get();
if (!thread_local_cache)
return;
pas_thread_local_cache_set(NULL);
if (verbose)
pas_log("[%d] TLC %p getting destroyed\n", getpid(), thread_local_cache);
destroy(thread_local_cache, heap_lock_hold_mode);
}
pas_thread_local_cache* pas_thread_local_cache_get_slow(const pas_heap_config* config,
pas_lock_hold_mode heap_lock_hold_mode)
{
pas_thread_local_cache* thread_local_cache;
pas_heap_lock_lock_conditionally(heap_lock_hold_mode);
PAS_FAST_TLS_CONSTRUCT_IF_NECESSARY(
PAS_THREAD_LOCAL_KEY, &pas_thread_local_cache_fast_tls, destructor);
/* This is allowed to set up a particular TLC layout that every TLC has to obey. So, it has to
happen before any TLCs are allocated. */
pas_heap_config_activate(config);
PAS_ASSERT(!pas_thread_local_cache_try_get());
thread_local_cache = pas_thread_local_cache_create();
pas_heap_lock_unlock_conditionally(heap_lock_hold_mode);
return thread_local_cache;
}
pas_local_allocator_result pas_thread_local_cache_get_local_allocator_slow(
pas_thread_local_cache* thread_local_cache,
unsigned desired_allocator_index,
pas_lock_hold_mode heap_lock_hold_mode)
{
static const bool verbose = false;
pas_thread_local_cache* new_thread_local_cache;
pas_thread_local_cache_layout_node layout_node;
unsigned old_index_upper_bound;
unsigned desired_index_upper_bound;
old_index_upper_bound = thread_local_cache->allocator_index_upper_bound;
PAS_ASSERT(desired_allocator_index);
PAS_ASSERT(desired_allocator_index >= old_index_upper_bound);
PAS_ASSERT(desired_allocator_index < (pas_allocator_index)UINT_MAX);
/* Rather than copy the deallocation log let's just flush it. */
pas_thread_local_cache_flush_deallocation_log(thread_local_cache, heap_lock_hold_mode);
pas_heap_lock_lock_conditionally(heap_lock_hold_mode);
pas_lock_lock(&thread_local_cache->node->scavenger_lock);
PAS_ASSERT(desired_allocator_index < pas_thread_local_cache_layout_next_allocator_index);
desired_index_upper_bound = pas_thread_local_cache_layout_next_allocator_index;
if (desired_index_upper_bound <= thread_local_cache->allocator_index_capacity)
new_thread_local_cache = thread_local_cache;
else {
unsigned index_capacity;
index_capacity = PAS_MAX(thread_local_cache->allocator_index_capacity << 1,
desired_index_upper_bound);
new_thread_local_cache = allocate_cache(index_capacity);
if (verbose)
printf("[%d] Reallocating TLC %p -> %p\n", getpid(), thread_local_cache, new_thread_local_cache);
new_thread_local_cache->node = thread_local_cache->node;
new_thread_local_cache->thread = thread_local_cache->thread;
new_thread_local_cache->allocator_index_upper_bound =
thread_local_cache->allocator_index_upper_bound;
pas_local_allocator_construct_unselected(
(pas_local_allocator*)pas_thread_local_cache_get_local_allocator_direct_unchecked(
new_thread_local_cache, PAS_LOCAL_ALLOCATOR_UNSELECTED_INDEX));
for (PAS_THREAD_LOCAL_CACHE_LAYOUT_EACH_ALLOCATOR(layout_node)) {
pas_allocator_index allocator_index;
allocator_index = pas_thread_local_cache_layout_node_get_allocator_index_generic(layout_node);
if (allocator_index >= old_index_upper_bound)
break;
pas_thread_local_cache_layout_node_move(
layout_node, new_thread_local_cache, thread_local_cache);
}
memcpy(new_thread_local_cache->should_stop_bitvector,
thread_local_cache->should_stop_bitvector,
PAS_BITVECTOR_NUM_BYTES(thread_local_cache->allocator_index_upper_bound));
pas_compiler_fence();
new_thread_local_cache->node->cache = new_thread_local_cache;
}
for (PAS_THREAD_LOCAL_CACHE_LAYOUT_EACH_ALLOCATOR(layout_node)) {
pas_allocator_index allocator_index;
allocator_index = pas_thread_local_cache_layout_node_get_allocator_index_generic(layout_node);
if (allocator_index < old_index_upper_bound)
continue;
if (allocator_index >= desired_index_upper_bound)
break;
pas_thread_local_cache_layout_node_commit_and_construct(layout_node, new_thread_local_cache);
}
pas_compiler_fence();
new_thread_local_cache->allocator_index_upper_bound = desired_index_upper_bound;
if (thread_local_cache != new_thread_local_cache)
deallocate(thread_local_cache);
pas_lock_unlock(&thread_local_cache->node->scavenger_lock);
pas_heap_lock_unlock_conditionally(heap_lock_hold_mode);
if (thread_local_cache != new_thread_local_cache)
pas_thread_local_cache_set(new_thread_local_cache);
PAS_ASSERT(desired_allocator_index < new_thread_local_cache->allocator_index_upper_bound);
return pas_local_allocator_result_create_success(
pas_thread_local_cache_get_local_allocator_direct(new_thread_local_cache,
desired_allocator_index));
}
bool pas_thread_local_cache_is_committed(pas_thread_local_cache* thread_local_cache,
pas_allocator_index begin_allocator_index,
pas_allocator_index end_allocator_index)
{
uintptr_t begin_page_index;
uintptr_t end_page_index;
uintptr_t page_index;
compute_page_indices(begin_allocator_index, end_allocator_index, &begin_page_index, &end_page_index);
for (page_index = begin_page_index; page_index < end_page_index; ++page_index) {
if (!pas_bitvector_get(thread_local_cache->pages_committed, page_index))
return false;
}
return true;
}
pas_decommit_exclusion_range pas_thread_local_cache_compute_decommit_exclusion_range(
pas_thread_local_cache* thread_local_cache,
pas_allocator_index begin_allocator_index,
pas_allocator_index end_allocator_index)
{
uintptr_t begin_page_index;
uintptr_t end_page_index;
uintptr_t page_index;
pas_range allocator_range;
pas_decommit_exclusion_range result;
size_t page_size_shift;
allocator_range = pas_range_create(pas_thread_local_cache_offset_of_allocator(begin_allocator_index),
pas_thread_local_cache_offset_of_allocator(end_allocator_index));
result.start_of_possible_decommit = allocator_range.begin;
result.end_of_possible_decommit = allocator_range.end;
compute_page_indices(begin_allocator_index, end_allocator_index, &begin_page_index, &end_page_index);
page_size_shift = pas_page_malloc_alignment_shift();
for (page_index = begin_page_index; page_index < end_page_index; ++page_index) {
if (!pas_bitvector_get(thread_local_cache->pages_committed, page_index)) {
result.start_of_possible_decommit = PAS_MAX(
result.start_of_possible_decommit,
PAS_MIN(
allocator_range.end,
(page_index + 1) << page_size_shift));
result.end_of_possible_decommit = PAS_MIN(
result.end_of_possible_decommit,
PAS_MAX(
allocator_range.begin,
page_index << page_size_shift));
}
}
PAS_ASSERT(!pas_decommit_exclusion_range_is_empty(result));
return result;
}
void pas_thread_local_cache_ensure_committed(pas_thread_local_cache* thread_local_cache,
pas_allocator_index begin_allocator_index,
pas_allocator_index end_allocator_index)
{
uintptr_t begin_page_index;
uintptr_t end_page_index;
uintptr_t page_index;
compute_page_indices(begin_allocator_index, end_allocator_index, &begin_page_index, &end_page_index);
for (page_index = begin_page_index; page_index < end_page_index; ++page_index) {
if (pas_bitvector_get(thread_local_cache->pages_committed, page_index))
continue;
pas_lock_assert_held(&thread_local_cache->node->scavenger_lock);
/* Don't attempt to do fancy things with spans for commit, since we're no longer really
optimizing for symmetric commit anyway. */
pas_page_malloc_commit_without_mprotect(
(char*)thread_local_cache + (page_index << pas_page_malloc_alignment_shift()),
pas_page_malloc_alignment(),
pas_may_mmap);
pas_bitvector_set(thread_local_cache->pages_committed, page_index, true);
}
}
pas_local_allocator_result
pas_thread_local_cache_get_local_allocator_if_can_set_cache_for_possibly_uninitialized_index_slow(
unsigned allocator_index,
const pas_heap_config* heap_config)
{
if (!pas_thread_local_cache_can_set() || pas_debug_heap_is_enabled(heap_config->kind))
return pas_local_allocator_result_create_failure();
return pas_thread_local_cache_get_local_allocator_for_possibly_uninitialized_index(
pas_thread_local_cache_get(heap_config),
allocator_index,
pas_lock_is_not_held);
}
pas_allocator_index pas_thread_local_cache_allocator_index_for_allocator(pas_thread_local_cache* cache,
void* allocator)
{
uintptr_t cache_address;
uintptr_t allocator_address;
uintptr_t begin_allocators;
uintptr_t end_allocators;
uintptr_t offset;
uintptr_t index;
cache_address = (uintptr_t)cache;
allocator_address = (uintptr_t)allocator;
begin_allocators = cache_address + PAS_OFFSETOF(pas_thread_local_cache, local_allocators);
end_allocators = begin_allocators + sizeof(uint64_t) * cache->allocator_index_upper_bound;
PAS_ASSERT(allocator_address >= begin_allocators);
PAS_ASSERT(allocator_address < end_allocators);
offset = allocator_address - begin_allocators;
PAS_ASSERT(pas_is_aligned(offset, sizeof(uint64_t)));
index = offset / sizeof(uint64_t);
PAS_ASSERT(index < cache->allocator_index_upper_bound);
PAS_ASSERT((pas_allocator_index)index == index);
return (pas_allocator_index)index;
}
void pas_thread_local_cache_stop_local_allocators(pas_thread_local_cache* thread_local_cache,
pas_lock_hold_mode heap_lock_hold_mode)
{
pas_thread_local_cache_layout_node layout_node;
/* We have to hold the heap lock to do things to our local allocators unless we also set the
is_in_use bit. */
pas_heap_lock_lock_conditionally(heap_lock_hold_mode);
pas_lock_lock(&thread_local_cache->node->scavenger_lock);
for (PAS_THREAD_LOCAL_CACHE_LAYOUT_EACH_ALLOCATOR(layout_node)) {
pas_allocator_index allocator_index;
allocator_index = pas_thread_local_cache_layout_node_get_allocator_index_generic(layout_node);
if (allocator_index >= thread_local_cache->allocator_index_upper_bound)
break;
if (!pas_thread_local_cache_layout_node_is_committed(layout_node, thread_local_cache))
continue;
pas_thread_local_cache_layout_node_stop(
layout_node, thread_local_cache, pas_lock_lock_mode_lock, pas_lock_is_held);
}
pas_lock_unlock(&thread_local_cache->node->scavenger_lock);
pas_heap_lock_unlock_conditionally(heap_lock_hold_mode);
}
typedef struct {
pas_thread_local_cache* thread_local_cache;
pas_local_allocator* requesting_allocator;
pas_lock_hold_mode heap_lock_hold_mode;
unsigned* should_stop_bitvector;
} stop_local_allocators_if_necessary_data;
static unsigned stop_local_allocators_if_necessary_set_bit_source(
size_t word_index,
void* arg)
{
stop_local_allocators_if_necessary_data* data;
data = (stop_local_allocators_if_necessary_data*)arg;
return data->should_stop_bitvector[word_index];
}
static bool stop_local_allocators_if_necessary_set_bit_callback(
pas_found_bit_index index,
void* arg)
{
static const bool verbose = false;
stop_local_allocators_if_necessary_data* data;
pas_allocator_index allocator_index;
pas_local_allocator_scavenger_data* scavenger_data;
data = (stop_local_allocators_if_necessary_data*)arg;
allocator_index = (pas_allocator_index)index.index;
PAS_ASSERT(allocator_index == index.index);
PAS_TESTING_ASSERT(pas_bitvector_get(data->should_stop_bitvector, allocator_index));
pas_bitvector_set(data->should_stop_bitvector, allocator_index, false);
scavenger_data = (pas_local_allocator_scavenger_data*)pas_thread_local_cache_get_local_allocator_direct(
data->thread_local_cache, allocator_index);
if ((pas_local_allocator*)scavenger_data == data->requesting_allocator)
return true;
if (!scavenger_data->should_stop_count)
return true;
if (verbose)
pas_log("Stopping allocator %p because pas_thread_local_cache_stop_local_allocators_if_necessary\n", scavenger_data);
pas_local_allocator_scavenger_data_stop(
scavenger_data, pas_lock_lock_mode_lock, data->heap_lock_hold_mode);
return true;
}
void pas_thread_local_cache_stop_local_allocators_if_necessary(
pas_thread_local_cache* thread_local_cache,
pas_local_allocator* requesting_allocator,
pas_lock_hold_mode heap_lock_hold_mode)
{
stop_local_allocators_if_necessary_data data;
if (!thread_local_cache->should_stop_some)
return;
data.thread_local_cache = thread_local_cache;
data.requesting_allocator = requesting_allocator;
data.heap_lock_hold_mode = heap_lock_hold_mode;
data.should_stop_bitvector = thread_local_cache->should_stop_bitvector;
pas_bitvector_for_each_set_bit(
stop_local_allocators_if_necessary_set_bit_source,
0,
PAS_BITVECTOR_NUM_WORDS(thread_local_cache->allocator_index_upper_bound),
stop_local_allocators_if_necessary_set_bit_callback,
&data);
thread_local_cache->should_stop_some = false;
}
static PAS_ALWAYS_INLINE void
process_deallocation_log_with_config(pas_thread_local_cache* cache,
pas_segregated_deallocation_mode deallocation_mode,
pas_segregated_page_config page_config,
pas_segregated_page_role role,
size_t* index,
uintptr_t encoded_begin,
pas_lock** held_lock)
{
static const bool verbose = false;
pas_lock* last_held_lock;
if (!pas_segregated_deallocation_logging_mode_does_logging(
pas_segregated_page_config_logging_mode_for_role(page_config, role))) {
pas_panic("Deallocation logging is disabled for %s/%s, but here we are.\n",
pas_segregated_page_config_kind_get_string(page_config.kind),
pas_segregated_page_role_get_string(role));
}
last_held_lock = NULL;
for (;;) {
uintptr_t begin;
begin = encoded_begin >> PAS_SEGREGATED_PAGE_CONFIG_KIND_AND_ROLE_NUM_BITS;
switch (page_config.kind) {
case pas_segregated_page_config_kind_null:
PAS_ASSERT(!begin);
break;
case pas_segregated_page_config_kind_pas_utility_small:
PAS_ASSERT(!"Should not be reached");
break;
default:
last_held_lock = *held_lock;
pas_segregated_page_deallocate(begin, held_lock, deallocation_mode, cache, page_config, role);
if (verbose && *held_lock != last_held_lock && last_held_lock)
pas_log("Switched lock from %p to %p.\n", last_held_lock, *held_lock);
pas_compiler_fence();
cache->deallocation_log[*index] = 0;
break;
}
if (!*index)
return;
encoded_begin = cache->deallocation_log[--*index];
if (PAS_UNLIKELY((encoded_begin & PAS_SEGREGATED_PAGE_CONFIG_KIND_AND_ROLE_MASK)
!= pas_segregated_page_config_kind_and_role_create(page_config.kind, role))) {
++*index;
return;
}
}
}
static PAS_ALWAYS_INLINE void flush_deallocation_log_without_resetting(
pas_thread_local_cache* thread_local_cache,
pas_segregated_deallocation_mode deallocation_mode)
{
size_t index;
pas_lock* held_lock;
pas_lock_testing_assert_held(&thread_local_cache->node->scavenger_lock);
index = thread_local_cache->deallocation_log_index;
held_lock = NULL;
while (index) {
uintptr_t encoded_begin;
encoded_begin = thread_local_cache->deallocation_log[--index];
switch ((pas_segregated_page_config_kind_and_role)
(encoded_begin & PAS_SEGREGATED_PAGE_CONFIG_KIND_AND_ROLE_MASK)) {
#define PAS_DEFINE_SEGREGATED_PAGE_CONFIG_KIND(name, value) \
case pas_segregated_page_config_kind_ ## name ## _and_shared_role: \
process_deallocation_log_with_config( \
thread_local_cache, deallocation_mode, value, pas_segregated_page_shared_role, &index, \
encoded_begin, &held_lock); \
break; \
case pas_segregated_page_config_kind_ ## name ## _and_exclusive_role: \
process_deallocation_log_with_config( \
thread_local_cache, deallocation_mode, value, pas_segregated_page_exclusive_role, &index, \
encoded_begin, &held_lock); \
break;
#include "pas_segregated_page_config_kind.def"
#undef PAS_DEFINE_SEGREGATED_PAGE_CONFIG_KIND
}
}
pas_lock_switch(&held_lock, NULL);
}
static void flush_deallocation_log_for_scavenger(pas_thread_local_cache* thread_local_cache)
{
flush_deallocation_log_without_resetting(thread_local_cache, pas_segregated_deallocation_direct_mode);
}
static PAS_ALWAYS_INLINE void flush_deallocation_log_with_mode(
pas_thread_local_cache* thread_local_cache,
pas_lock_hold_mode heap_lock_hold_mode,
pas_segregated_deallocation_mode deallocation_mode)
{
if (!thread_local_cache)
return;
pas_lock_lock(&thread_local_cache->node->scavenger_lock);
switch (deallocation_mode) {
case pas_segregated_deallocation_direct_mode:
/* If we're doing a direct flush then try to share code with flush_deallocation_log_for_scavenger,
which does the same thing we would do. */
flush_deallocation_log_for_scavenger(thread_local_cache);
break;
case pas_segregated_deallocation_to_view_cache_mode:
flush_deallocation_log_without_resetting(
thread_local_cache, pas_segregated_deallocation_to_view_cache_mode);
break;
}
thread_local_cache->deallocation_log_index = 0;
thread_local_cache->num_logged_bytes = 0;
/* Dirtying the deallocation log is a signal to the scavenger that we are actively operating on
this log. */
thread_local_cache->deallocation_log_dirty = true;
pas_lock_unlock(&thread_local_cache->node->scavenger_lock);
if (heap_lock_hold_mode == pas_lock_is_not_held)
pas_scavenger_notify_eligibility_if_needed();
}
void pas_thread_local_cache_flush_deallocation_log(pas_thread_local_cache* thread_local_cache,
pas_lock_hold_mode heap_lock_hold_mode)
{
flush_deallocation_log_with_mode(
thread_local_cache, heap_lock_hold_mode, pas_segregated_deallocation_to_view_cache_mode);
}
void pas_thread_local_cache_flush_deallocation_log_direct(pas_thread_local_cache* thread_local_cache,
pas_lock_hold_mode heap_lock_hold_mode)
{
flush_deallocation_log_with_mode(
thread_local_cache, heap_lock_hold_mode, pas_segregated_deallocation_direct_mode);
}
typedef struct scavenger_thread_suspend_data {
bool did_suspend;
bool is_scavenger_itself;
#if PAS_OS(DARWIN)
mach_port_t mach_thread;
#endif
} scavenger_thread_suspend_data;
static scavenger_thread_suspend_data scavenger_thread_suspend_data_create()
{
scavenger_thread_suspend_data thread_suspend_data;
thread_suspend_data.did_suspend = false;
thread_suspend_data.is_scavenger_itself = false;
return thread_suspend_data;
}
#if PAS_OS(DARWIN)
static void suspend(pas_thread_local_cache* cache, scavenger_thread_suspend_data* thread_suspend_data)
{
static const bool verbose = false;
pthread_t thread;
kern_return_t result;
if (thread_suspend_data->did_suspend)
return;
thread_suspend_data->did_suspend = true;
if (verbose)
printf("Suspending TLC %p with thread %p.\n", cache, cache->thread);
thread = cache->thread;
PAS_ASSERT(thread);
thread_suspend_data->is_scavenger_itself = thread == pthread_self();
if (thread_suspend_data->is_scavenger_itself)
return;
thread_suspend_data->mach_thread = pthread_mach_thread_np(thread);
result = thread_suspend(thread_suspend_data->mach_thread);
/* Fun fact: it's impossible for us to try to suspend a thread that has exited, since
thread exit for any thread with a TLC needs to grab the heap lock and we hold the
heap lock. */
if (result != KERN_SUCCESS) {
pas_log("[%d] Failed to suspend pthread %p (mach thread %d) associated with TLC %p: %d\n",
getpid(), thread, thread_suspend_data->mach_thread, cache, result);
dump_thread_diagnostics(thread);
PAS_ASSERT(result == KERN_SUCCESS);
}
}
static void resume(pas_thread_local_cache* cache, scavenger_thread_suspend_data* thread_suspend_data)
{
static const bool verbose = false;
kern_return_t result;
if (verbose)
pas_log("Resuming TLC %p with thread %p.\n", cache, cache->thread);
if (thread_suspend_data->is_scavenger_itself)
return;
result = thread_resume(thread_suspend_data->mach_thread);
PAS_ASSERT(result == KERN_SUCCESS);
}
#endif
static void decommit_allocator_range(pas_thread_local_cache* cache,
pas_thread_local_cache_layout_segment* begin_segment,
uintptr_t begin_node_index,
uintptr_t start_of_possible_decommit,
uintptr_t end_of_possible_decommit,
pas_thread_local_cache_layout_segment* end_segment,
uintptr_t end_node_index)
{
#if PAS_OS(DARWIN)
static const bool verbose = false;
uintptr_t begin_page_index;
uintptr_t end_page_index;
pas_thread_local_cache_layout_node inner_node;
uintptr_t page_index;
size_t page_size;
size_t page_size_shift;
pas_range decommit_range;
pas_heap_lock_assert_held();
pas_lock_assert_held(&cache->node->scavenger_lock);
page_size = pas_page_malloc_alignment();
page_size_shift = pas_page_malloc_alignment_shift();
begin_page_index = pas_round_up_to_power_of_2(
start_of_possible_decommit, page_size) >> page_size_shift;
end_page_index = end_of_possible_decommit >> page_size_shift;
if (begin_page_index >= end_page_index)
return;
decommit_range = pas_range_create(begin_page_index << page_size_shift,
end_page_index << page_size_shift);
if (begin_segment) {
pas_thread_local_cache_layout_segment* segment = begin_segment;
uintptr_t node_index = begin_node_index;
for (inner_node = pas_thread_local_cache_layout_segment_get_node(segment, node_index); !(segment == end_segment && node_index == end_node_index); inner_node = pas_thread_local_cache_layout_segment_next_node(&segment, &node_index))
pas_thread_local_cache_layout_node_prepare_to_decommit(inner_node, cache, decommit_range);
}
for (page_index = begin_page_index; page_index < end_page_index; ++page_index) {
PAS_ASSERT(pas_bitvector_get(cache->pages_committed, page_index));
pas_bitvector_set(cache->pages_committed, page_index, false);
}
if (verbose)
pas_log("Decommitting %p...%p\n", (void*)decommit_range.begin, (void*)decommit_range.end);
pas_page_malloc_decommit_without_mprotect(
(char*)cache + decommit_range.begin, pas_range_size(decommit_range), pas_may_mmap);
if (verbose) {
pas_log("Num committed pages in the range we just decommitted: %zu\n",
pas_count_committed_pages(
(char*)cache + decommit_range.begin,
pas_range_size(decommit_range),
&pas_large_utility_free_heap_allocation_config));
}
#else /* PAS_OS(DARWIN) -> so !PAS_OS(DARWIN) */
PAS_UNUSED_PARAM(cache);
PAS_UNUSED_PARAM(begin_segment);
PAS_UNUSED_PARAM(begin_node_index);
PAS_UNUSED_PARAM(start_of_possible_decommit);
PAS_UNUSED_PARAM(end_of_possible_decommit);
PAS_UNUSED_PARAM(end_segment);
PAS_UNUSED_PARAM(end_node_index);
#endif /* PAS_OS(DARWIN) -> so end of !PAS_OS(DARWIN) */
}
static void stop_allocator(pas_thread_local_cache* cache,
pas_allocator_scavenge_action allocator_action,
pas_allocator_index allocator_index,
pas_local_allocator_scavenger_data* scavenger_data,
bool* result,
scavenger_thread_suspend_data* thread_suspend_data)
{
static const bool verbose = false;
if (allocator_action == pas_allocator_scavenge_request_stop_action) {
uint8_t should_stop_count;
if (verbose)
pas_log("Doing the request thingy.\n");
if (scavenger_data->dirty) {
if (verbose)
pas_log("It was dirty.\n");
scavenger_data->dirty = false;
*result = true;
return;
}
/* Bail out if it's no longer active. */
if (pas_local_allocator_scavenger_data_is_stopped(scavenger_data)) {
if (verbose)
pas_log("It was empty.\n");
return;
}
should_stop_count = scavenger_data->should_stop_count;
if (should_stop_count < pas_local_allocator_should_stop_count_for_suspend) {
scavenger_data->should_stop_count = ++should_stop_count;
PAS_ASSERT(should_stop_count
<= pas_local_allocator_should_stop_count_for_suspend);
pas_bitvector_set(cache->should_stop_bitvector, allocator_index, true);
cache->should_stop_some = true;
*result = true;
if (verbose)
pas_log("Told it to stop.\n");
return;
}
PAS_ASSERT(should_stop_count
== pas_local_allocator_should_stop_count_for_suspend);
} else {
if (verbose)
pas_log("Doing actual stop.\n");
PAS_ASSERT(allocator_action == pas_allocator_scavenge_force_stop_action);
}
if (pas_local_allocator_scavenger_data_is_stopped(scavenger_data)) {
if (verbose)
pas_log("It was empty.\n");