/
memory-manager.cpp
1485 lines (1327 loc) · 47.4 KB
/
memory-manager.cpp
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2015 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/base/memory-manager.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <sys/mman.h>
#include <unistd.h>
#include "hphp/runtime/base/builtin-functions.h"
#include "hphp/runtime/base/exceptions.h"
#include "hphp/runtime/base/memory-profile.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/base/stack-logger.h"
#include "hphp/runtime/base/surprise-flags.h"
#include "hphp/runtime/base/sweepable.h"
#include "hphp/runtime/base/thread-info.h"
#include "hphp/runtime/base/heap-graph.h"
#include "hphp/runtime/server/http-server.h"
#include "hphp/util/alloc.h"
#include "hphp/util/logger.h"
#include "hphp/util/process.h"
#include "hphp/util/trace.h"
#include <folly/Random.h>
#include <folly/ScopeGuard.h>
#include "hphp/runtime/base/memory-manager-defs.h"
namespace HPHP {
const unsigned kInvalidSweepIndex = 0xffffffff;
TRACE_SET_MOD(mm);
//////////////////////////////////////////////////////////////////////
std::atomic<MemoryManager::ReqProfContext*>
MemoryManager::s_trigger{nullptr};
// generate mmap flags for contiguous heap
uint32_t getRequestHeapFlags() {
struct stat buf;
// check if MAP_UNITIALIZED is supported
auto mapUninitializedSupported =
(stat("/sys/kernel/debug/fb_map_uninitialized", &buf) == 0);
auto mmapFlags = MAP_NORESERVE | MAP_ANON | MAP_PRIVATE;
/* Check whether mmap(2) supports the MAP_UNINITIALIZED flag. */
if (mapUninitializedSupported) {
mmapFlags |= MAP_UNINITIALIZED;
}
return mmapFlags;
}
static auto s_mmapFlags = getRequestHeapFlags();
#ifdef USE_JEMALLOC
bool MemoryManager::s_statsEnabled = false;
size_t MemoryManager::s_cactiveLimitCeiling = 0;
static size_t threadAllocatedpMib[2];
static size_t threadDeallocatedpMib[2];
static size_t statsCactiveMib[2];
static pthread_once_t threadStatsOnce = PTHREAD_ONCE_INIT;
void MemoryManager::threadStatsInit() {
if (!mallctlnametomib) return;
size_t miblen = sizeof(threadAllocatedpMib) / sizeof(size_t);
if (mallctlnametomib("thread.allocatedp", threadAllocatedpMib, &miblen)) {
return;
}
miblen = sizeof(threadDeallocatedpMib) / sizeof(size_t);
if (mallctlnametomib("thread.deallocatedp", threadDeallocatedpMib, &miblen)) {
return;
}
miblen = sizeof(statsCactiveMib) / sizeof(size_t);
if (mallctlnametomib("stats.cactive", statsCactiveMib, &miblen)) {
return;
}
MemoryManager::s_statsEnabled = true;
// In threadStats() we wish to solve for cactiveLimit in:
//
// footprint + cactiveLimit + headRoom == MemTotal
//
// However, headRoom comes from RuntimeOption::ServerMemoryHeadRoom, which
// isn't initialized until after the code here runs. Therefore, compute
// s_cactiveLimitCeiling here in order to amortize the cost of introspecting
// footprint and MemTotal.
//
// cactiveLimit == (MemTotal - footprint) - headRoom
//
// cactiveLimit == s_cactiveLimitCeiling - headRoom
// where
// s_cactiveLimitCeiling == MemTotal - footprint
size_t footprint = Process::GetCodeFootprint(Process::GetProcessId());
size_t MemTotal = 0;
#ifndef __APPLE__
size_t pageSize = size_t(sysconf(_SC_PAGESIZE));
MemTotal = size_t(sysconf(_SC_PHYS_PAGES)) * pageSize;
#else
int mib[2] = { CTL_HW, HW_MEMSIZE };
u_int namelen = sizeof(mib) / sizeof(mib[0]);
size_t len = sizeof(MemTotal);
sysctl(mib, namelen, &MemTotal, &len, nullptr, 0);
#endif
if (MemTotal > footprint) {
MemoryManager::s_cactiveLimitCeiling = MemTotal - footprint;
}
}
inline
void MemoryManager::threadStats(uint64_t*& allocated, uint64_t*& deallocated,
size_t*& cactive, size_t& cactiveLimit) {
pthread_once(&threadStatsOnce, threadStatsInit);
if (!MemoryManager::s_statsEnabled) return;
size_t len = sizeof(allocated);
if (mallctlbymib(threadAllocatedpMib,
sizeof(threadAllocatedpMib) / sizeof(size_t),
&allocated, &len, nullptr, 0)) {
not_reached();
}
len = sizeof(deallocated);
if (mallctlbymib(threadDeallocatedpMib,
sizeof(threadDeallocatedpMib) / sizeof(size_t),
&deallocated, &len, nullptr, 0)) {
not_reached();
}
len = sizeof(cactive);
if (mallctlbymib(statsCactiveMib,
sizeof(statsCactiveMib) / sizeof(size_t),
&cactive, &len, nullptr, 0)) {
not_reached();
}
int64_t headRoom = RuntimeOption::ServerMemoryHeadRoom;
// Compute cactiveLimit based on s_cactiveLimitCeiling, as computed in
// threadStatsInit().
if (headRoom != 0 && headRoom < MemoryManager::s_cactiveLimitCeiling) {
cactiveLimit = MemoryManager::s_cactiveLimitCeiling - headRoom;
} else {
cactiveLimit = std::numeric_limits<size_t>::max();
}
}
#endif
static void* MemoryManagerInit() {
// We store the free list pointers right at the start of each
// object (overlapping whatever it's first word holds), and we also clobber
// _count as a free-object flag when the object is deallocated. This
// assert just makes sure they don't overflow.
assert(FAST_REFCOUNT_OFFSET + sizeof(int) <=
MemoryManager::smallSizeClass(1));
MemoryManager::TlsWrapper tls;
return (void*)tls.getNoCheck;
}
void* MemoryManager::TlsInitSetup = MemoryManagerInit();
void MemoryManager::Create(void* storage) {
new (storage) MemoryManager();
}
void MemoryManager::Delete(MemoryManager* mm) {
mm->~MemoryManager();
}
void MemoryManager::OnThreadExit(MemoryManager* mm) {
mm->~MemoryManager();
}
MemoryManager::MemoryManager() {
#ifdef USE_JEMALLOC
threadStats(m_allocated, m_deallocated, m_cactive, m_cactiveLimit);
#endif
resetStatsImpl(true);
m_stats.maxBytes = std::numeric_limits<int64_t>::max();
// make the circular-lists empty.
m_strings.next = m_strings.prev = &m_strings;
m_bypassSlabAlloc = RuntimeOption::DisableSmallAllocator;
}
void MemoryManager::addExceptionRoot(ExtendedException* exn) {
m_exceptionRoots.push_back(exn);
// The key is the index into the exception root list (biased by 1).
exn->m_key.m_index = m_exceptionRoots.size();
}
void MemoryManager::removeExceptionRoot(ExtendedException* exn) {
// Swap the exception being removed with the last exception in the list (which
// might be the same one). This lets us remove from the vector in constant
// time.
assert(exn->m_key.m_index > 0 &&
exn->m_key.m_index <= m_exceptionRoots.size());
auto& removed = m_exceptionRoots[exn->m_key.m_index-1];
assert(removed == exn);
auto& back = m_exceptionRoots.back();
assert(back->m_key.m_index == m_exceptionRoots.size());
back->m_key = removed->m_key;
removed->m_key.m_index = 0;
removed = back;
m_exceptionRoots.pop_back();
}
void MemoryManager::dropRootMaps() {
m_objectRoots = nullptr;
m_resourceRoots = nullptr;
m_exceptionRoots = std::vector<ExtendedException*>();
}
void MemoryManager::deleteRootMaps() {
if (m_objectRoots) {
req::destroy_raw(m_objectRoots);
m_objectRoots = nullptr;
}
if (m_resourceRoots) {
req::destroy_raw(m_resourceRoots);
m_resourceRoots = nullptr;
}
m_exceptionRoots = std::vector<ExtendedException*>();
}
void MemoryManager::resetRuntimeOptions() {
if (debug) {
deleteRootMaps();
checkHeap("resetRuntimeOptions");
// check that every allocation in heap has been freed before reset
iterate([&](Header* h) {
assert(h->kind() == HeaderKind::Free);
});
}
MemoryManager::TlsWrapper::destroy(); // ~MemoryManager()
MemoryManager::TlsWrapper::getCheck(); // new MemeoryManager()
}
void MemoryManager::resetStatsImpl(bool isInternalCall) {
#ifdef USE_JEMALLOC
FTRACE(1, "resetStatsImpl({}) pre:\n", isInternalCall);
FTRACE(1, "usage: {}\nalloc: {}\npeak usage: {}\npeak alloc: {}\n",
m_stats.usage, m_stats.alloc, m_stats.peakUsage, m_stats.peakAlloc);
FTRACE(1, "total alloc: {}\nje alloc: {}\nje dealloc: {}\n",
m_stats.totalAlloc, m_prevAllocated, m_prevDeallocated);
FTRACE(1, "je debt: {}\n\n", m_stats.jemallocDebt);
#else
FTRACE(1, "resetStatsImpl({}) pre:\n"
"usage: {}\nalloc: {}\npeak usage: {}\npeak alloc: {}\n\n",
isInternalCall,
m_stats.usage, m_stats.alloc, m_stats.peakUsage, m_stats.peakAlloc);
#endif
if (isInternalCall) {
m_statsIntervalActive = false;
m_stats.usage = 0;
m_stats.alloc = 0;
m_stats.peakUsage = 0;
m_stats.peakAlloc = 0;
m_stats.totalAlloc = 0;
m_stats.peakIntervalUsage = 0;
m_stats.peakIntervalAlloc = 0;
#ifdef USE_JEMALLOC
m_enableStatsSync = false;
} else if (!m_enableStatsSync) {
#else
} else {
#endif
// This is only set by the jemalloc stats sync which we don't enable until
// after this has been called.
assert(m_stats.totalAlloc == 0);
#ifdef USE_JEMALLOC
assert(m_stats.jemallocDebt >= m_stats.alloc);
#endif
// The effect of this call is simply to ignore anything we've done *outside*
// the MemoryManager allocator after we initialized to avoid attributing
// shared structure initialization that happens during hphp_thread_init()
// to this session.
// We don't want to clear the other values because we do already have some
// small-sized allocator usage and live slabs and wiping now will result in
// negative values when we try to reconcile our accounting with jemalloc.
#ifdef USE_JEMALLOC
// Anything that was definitively allocated by the MemoryManager allocator
// should be counted in this number even if we're otherwise zeroing out
// the count for each thread.
m_stats.totalAlloc = s_statsEnabled ? m_stats.jemallocDebt : 0;
m_enableStatsSync = s_statsEnabled;
#else
m_stats.totalAlloc = 0;
#endif
}
#ifdef USE_JEMALLOC
if (s_statsEnabled) {
m_stats.jemallocDebt = 0;
m_prevDeallocated = *m_deallocated;
m_prevAllocated = *m_allocated;
}
#endif
#ifdef USE_JEMALLOC
FTRACE(1, "resetStatsImpl({}) post:\n", isInternalCall);
FTRACE(1, "usage: {}\nalloc: {}\npeak usage: {}\npeak alloc: {}\n",
m_stats.usage, m_stats.alloc, m_stats.peakUsage, m_stats.peakAlloc);
FTRACE(1, "total alloc: {}\nje alloc: {}\nje dealloc: {}\n",
m_stats.totalAlloc, m_prevAllocated, m_prevDeallocated);
FTRACE(1, "je debt: {}\n\n", m_stats.jemallocDebt);
#else
FTRACE(1, "resetStatsImpl({}) post:\n"
"usage: {}\nalloc: {}\npeak usage: {}\npeak alloc: {}\n\n",
isInternalCall,
m_stats.usage, m_stats.alloc, m_stats.peakUsage, m_stats.peakAlloc);
#endif
}
void MemoryManager::refreshStatsHelperExceeded() {
setSurpriseFlag(MemExceededFlag);
m_couldOOM = false;
if (RuntimeOption::LogNativeStackOnOOM) {
log_native_stack("Exceeded memory limit");
}
}
void MemoryManager::setMemThresholdCallback(size_t threshold) {
m_memThresholdCallbackPeakUsage = threshold;
}
#ifdef USE_JEMALLOC
void MemoryManager::refreshStatsHelperStop() {
HttpServer::Server->stop();
// Increase the limit to the maximum possible value, so that this method
// won't be called again.
m_cactiveLimit = std::numeric_limits<size_t>::max();
}
#endif
/*
* Refresh stats to reflect directly malloc()ed memory, and determine
* whether the request memory limit has been exceeded.
*
* The stats parameter allows the updates to be applied to either
* m_stats as in refreshStats() or to a separate MemoryUsageStats
* struct as in getStatsSafe().
*
* The template variable live controls whether or not MemoryManager
* member variables are updated and whether or not to call helper
* methods in response to memory anomalies.
*/
template<bool live>
void MemoryManager::refreshStatsImpl(MemoryUsageStats& stats) {
#ifdef USE_JEMALLOC
// Incrementally incorporate the difference between the previous and current
// deltas into the memory usage statistic. For reference, the total
// malloced memory usage could be calculated as such, if delta0 were
// recorded in resetStatsImpl():
//
// int64 musage = delta - delta0;
//
// Note however, the slab allocator adds to m_stats.jemallocDebt
// when it calls malloc(), so that this function can avoid
// double-counting the malloced memory. Thus musage in the example
// code may well substantially exceed m_stats.usage.
if (m_enableStatsSync) {
uint64_t jeDeallocated = *m_deallocated;
uint64_t jeAllocated = *m_allocated;
// We can't currently handle wrapping so make sure this isn't happening.
assert(jeAllocated >= 0 &&
jeAllocated <= std::numeric_limits<int64_t>::max());
assert(jeDeallocated >= 0 &&
jeDeallocated <= std::numeric_limits<int64_t>::max());
// This is the delta between the current and the previous jemalloc reading.
int64_t jeMMDeltaAllocated =
int64_t(jeAllocated) - int64_t(m_prevAllocated);
FTRACE(1, "Before stats sync:\n");
FTRACE(1, "je alloc:\ncurrent: {}\nprevious: {}\ndelta with MM: {}\n",
jeAllocated, m_prevAllocated, jeAllocated - m_prevAllocated);
FTRACE(1, "je dealloc:\ncurrent: {}\nprevious: {}\ndelta with MM: {}\n",
jeDeallocated, m_prevDeallocated, jeDeallocated - m_prevDeallocated);
FTRACE(1, "usage: {}\ntotal (je) alloc: {}\nje debt: {}\n",
stats.usage, stats.totalAlloc, stats.jemallocDebt);
if (!contiguous_heap) {
// Since these deltas potentially include memory allocated from another
// thread but deallocated on this one, it is possible for these nubmers to
// go negative.
int64_t jeDeltaAllocated =
int64_t(jeAllocated) - int64_t(jeDeallocated);
int64_t mmDeltaAllocated =
int64_t(m_prevAllocated) - int64_t(m_prevDeallocated);
FTRACE(1, "je delta:\ncurrent: {}\nprevious: {}\n",
jeDeltaAllocated, mmDeltaAllocated);
// Subtract the old jemalloc adjustment (delta0) and add the current one
// (delta) to arrive at the new combined usage number.
stats.usage += jeDeltaAllocated - mmDeltaAllocated;
// Remove the "debt" accrued from allocating the slabs so we don't double
// count the slab-based allocations.
stats.usage -= stats.jemallocDebt;
}
stats.jemallocDebt = 0;
// We need to do the calculation instead of just setting it to jeAllocated
// because of the MaskAlloc capability.
stats.totalAlloc += jeMMDeltaAllocated;
if (live) {
m_prevAllocated = jeAllocated;
m_prevDeallocated = jeDeallocated;
}
FTRACE(1, "After stats sync:\n");
FTRACE(1, "usage: {}\ntotal (je) alloc: {}\n\n",
stats.usage, stats.totalAlloc);
}
#endif
assert(stats.maxBytes > 0);
if (live && stats.usage > stats.maxBytes && m_couldOOM) {
refreshStatsHelperExceeded();
}
if (stats.usage > stats.peakUsage) {
// Check whether the process's active memory limit has been exceeded, and
// if so, stop the server.
//
// Only check whether the total memory limit was exceeded if this request
// is at a new high water mark. This check could be performed regardless
// of this request's current memory usage (because other request threads
// could be to blame for the increased memory usage), but doing so would
// measurably increase computation for little benefit.
#ifdef USE_JEMALLOC
// (*m_cactive) consistency is achieved via atomic operations. The fact
// that we do not use an atomic operation here means that we could get a
// stale read, but in practice that poses no problems for how we are
// using the value.
if (live && s_statsEnabled && *m_cactive > m_cactiveLimit) {
refreshStatsHelperStop();
}
#endif
if (live &&
stats.usage > m_memThresholdCallbackPeakUsage &&
stats.peakUsage <= m_memThresholdCallbackPeakUsage) {
setSurpriseFlag(MemThresholdFlag);
}
stats.peakUsage = stats.usage;
}
if (live && m_statsIntervalActive) {
if (stats.usage > stats.peakIntervalUsage) {
stats.peakIntervalUsage = stats.usage;
}
if (stats.alloc > stats.peakIntervalAlloc) {
stats.peakIntervalAlloc = stats.alloc;
}
}
}
template void MemoryManager::refreshStatsImpl<true>(MemoryUsageStats& stats);
template void MemoryManager::refreshStatsImpl<false>(MemoryUsageStats& stats);
void MemoryManager::sweep() {
// running a gc-cycle at end of request exposes bugs, but otherwise is
// somewhat pointless since we're about to free the heap en-masse.
if (debug) collect("before MM::sweep");
assert(!sweeping());
m_sweeping = true;
DEBUG_ONLY size_t num_sweepables = 0, num_natives = 0;
// iterate until both sweep lists are empty. Entries can be added or
// removed from either list during sweeping.
do {
while (!m_sweepables.empty()) {
num_sweepables++;
auto obj = m_sweepables.next();
obj->unregister();
obj->sweep();
}
while (!m_natives.empty()) {
num_natives++;
assert(m_natives.back()->sweep_index == m_natives.size() - 1);
auto node = m_natives.back();
m_natives.pop_back();
auto obj = Native::obj(node);
auto ndi = obj->getVMClass()->getNativeDataInfo();
ndi->sweep(obj);
// trash the native data but leave the header and object parsable
assert(memset(node+1, kSmallFreeFill, node->obj_offset - sizeof(*node)));
}
} while (!m_sweepables.empty());
DEBUG_ONLY auto napcs = m_apc_arrays.size();
FTRACE(1, "sweep: sweepable {} native {} apc array {}\n",
num_sweepables,
num_natives,
napcs);
// decref apc arrays referenced by this request. This must happen here
// (instead of in resetAllocator), because the sweep routine may use
// g_context.
while (!m_apc_arrays.empty()) {
auto a = m_apc_arrays.back();
m_apc_arrays.pop_back();
a->sweep();
if (debug) a->m_sweep_index = kInvalidSweepIndex;
}
if (debug) checkHeap("after MM::sweep");
}
void MemoryManager::resetAllocator() {
assert(m_natives.empty() && m_sweepables.empty() && m_sweeping);
// decref apc strings referenced by this request
DEBUG_ONLY auto nstrings = StringData::sweepAll();
// cleanup root maps
dropRootMaps();
// free the heap
m_heap.reset();
// zero out freelists
for (auto& i : m_freelists) i.head = nullptr;
m_front = m_limit = 0;
m_needInitFree = false;
m_sweeping = false;
m_exiting = false;
resetStatsImpl(true);
FTRACE(1, "reset: strings {}\n", nstrings);
}
void MemoryManager::flush() {
always_assert(empty());
m_heap.flush();
m_apc_arrays = std::vector<APCLocalArray*>();
m_natives = std::vector<NativeNode*>();
m_exceptionRoots = std::vector<ExtendedException*>();
}
/*
* req::malloc & friends implementation notes
*
* There are three kinds of allocations:
*
* a) Big allocations. (size >= kMaxSmallSize)
*
* In this case we behave as a wrapper around the normal libc
* malloc/free. We insert a BigNode header at the front of the
* allocation in order to find these at sweep time (end of
* request) so we can give them back to libc.
*
* b) Size-tracked small allocations.
*
* This is used for the generic case, for callers who can't tell
* us the size of the allocation at free time.
*
* In this situation, we put a SmallNode header at the front of
* the block that tells us the size for when we need to free it
* later. We differentiate this from a BigNode using the size
* field in either structure (they overlap at the same address).
*
* c) Size-untracked small allocation
*
* Many callers have an easy time telling you how big the object
* was when they need to free it. In this case we can avoid the
* SmallNode, which saves us some memory and also let's us give
* out 16-byte aligned pointers easily.
*
* We know when we have one of these because it has to be freed
* through a different entry point. (E.g. MM().freeSmallSize() or
* MM().freeBigSize().)
*
* When small blocks are freed (case b and c), they're placed in the
* appropriate size-segregated freelist. Large blocks are immediately
* passed back to libc via free.
*
* There are currently two kinds of freelist entries: entries where
* there is already a valid SmallNode on the list (case b), and
* entries where there isn't (case c). The reason for this is that
* that way, when allocating for case b, you don't need to store the
* SmallNode size again. Much of the heap is going through case b at
* the time of this writing, so it is a measurable regression to try
* to just combine the free lists, but presumably we can move more to
* case c and combine the lists eventually.
*/
inline void* MemoryManager::malloc(size_t nbytes) {
auto const nbytes_padded = nbytes + sizeof(SmallNode);
if (LIKELY(nbytes_padded) <= kMaxSmallSize) {
auto const ptr = static_cast<SmallNode*>(mallocSmallSize(nbytes_padded));
ptr->padbytes = nbytes_padded;
ptr->hdr.kind = HeaderKind::SmallMalloc;
return ptr + 1;
}
return mallocBig(nbytes);
}
union MallocNode {
BigNode big;
SmallNode small;
};
static_assert(sizeof(SmallNode) == sizeof(BigNode), "");
inline void MemoryManager::free(void* ptr) {
assert(ptr != 0);
auto const n = static_cast<MallocNode*>(ptr) - 1;
auto const padbytes = n->small.padbytes;
if (LIKELY(padbytes <= kMaxSmallSize)) {
return freeSmallSize(&n->small, n->small.padbytes);
}
m_heap.freeBig(ptr);
}
inline void* MemoryManager::realloc(void* ptr, size_t nbytes) {
FTRACE(3, "MemoryManager::realloc: {} to {}\n", ptr, nbytes);
assert(nbytes > 0);
auto const n = static_cast<MallocNode*>(ptr) - 1;
if (LIKELY(n->small.padbytes <= kMaxSmallSize)) {
void* newmem = req::malloc(nbytes);
auto const copySize = std::min(
n->small.padbytes - sizeof(SmallNode),
nbytes
);
newmem = memcpy(newmem, ptr, copySize);
req::free(ptr);
return newmem;
}
// Ok, it's a big allocation.
if (debug) eagerGCCheck();
auto block = m_heap.resizeBig(ptr, nbytes);
refreshStats();
return block.ptr;
}
const char* header_names[] = {
"PackedArray", "StructArray", "MixedArray", "EmptyArray", "ApcArray",
"GlobalsArray", "ProxyArray", "String", "Resource", "Ref",
"Object", "WaitHandle", "ResumableObj", "AwaitAllWH",
"Vector", "Map", "Set", "Pair", "ImmVector", "ImmMap", "ImmSet",
"ResumableFrame", "NativeData", "SmallMalloc", "BigMalloc", "BigObj",
"Free", "Hole"
};
static_assert(sizeof(header_names)/sizeof(*header_names) == NumHeaderKinds, "");
// initialize a Hole header in the unused memory between m_front and m_limit
void MemoryManager::initHole(void* ptr, uint32_t size) {
auto hdr = static_cast<FreeNode*>(ptr);
hdr->hdr.kind = HeaderKind::Hole;
hdr->size() = size;
}
void MemoryManager::initHole() {
if ((char*)m_front < (char*)m_limit) {
initHole(m_front, (char*)m_limit - (char*)m_front);
}
}
// initialize the FreeNode header on all freelist entries.
void MemoryManager::initFree() {
initHole();
for (auto i = 0; i < kNumSmallSizes; i++) {
for (auto n = m_freelists[i].head; n; n = n->next) {
n->hdr.init(HeaderKind::Free, smallIndex2Size(i));
}
}
m_needInitFree = false;
}
// turn free blocks into holes, leave freelists empty.
void MemoryManager::quarantine() {
for (auto i = 0; i < kNumSmallSizes; i++) {
auto size = smallIndex2Size(i);
while (auto n = m_freelists[i].maybePop()) {
memset(n, 0x8a, size);
static_cast<FreeNode*>(n)->hdr.init(HeaderKind::Hole, size);
}
}
}
// test iterating objects in slabs
void MemoryManager::checkHeap(const char* phase) {
size_t bytes=0;
std::vector<Header*> hdrs;
std::unordered_set<FreeNode*> free_blocks;
std::unordered_set<APCLocalArray*> apc_arrays;
std::unordered_set<StringData*> apc_strings;
size_t counts[NumHeaderKinds];
for (unsigned i=0; i < NumHeaderKinds; i++) counts[i] = 0;
forEachHeader([&](Header* h) {
hdrs.push_back(&*h);
bytes += h->size();
counts[(int)h->kind()]++;
switch (h->kind()) {
case HeaderKind::Free:
free_blocks.insert(&h->free_);
break;
case HeaderKind::Apc:
if (h->apc_.m_sweep_index != kInvalidSweepIndex) {
apc_arrays.insert(&h->apc_);
}
break;
case HeaderKind::String:
if (h->str_.isProxy()) apc_strings.insert(&h->str_);
break;
case HeaderKind::Packed:
case HeaderKind::Struct:
case HeaderKind::Mixed:
case HeaderKind::Empty:
case HeaderKind::Globals:
case HeaderKind::Proxy:
case HeaderKind::Object:
case HeaderKind::WaitHandle:
case HeaderKind::ResumableObj:
case HeaderKind::AwaitAllWH:
case HeaderKind::Vector:
case HeaderKind::Map:
case HeaderKind::Set:
case HeaderKind::Pair:
case HeaderKind::ImmVector:
case HeaderKind::ImmMap:
case HeaderKind::ImmSet:
case HeaderKind::Resource:
case HeaderKind::Ref:
case HeaderKind::ResumableFrame:
case HeaderKind::NativeData:
case HeaderKind::SmallMalloc:
case HeaderKind::BigMalloc:
break;
case HeaderKind::BigObj:
case HeaderKind::Hole:
assert(false && "forEachHeader skips these kinds");
break;
}
});
// check the free lists
for (auto i = 0; i < kNumSmallSizes; i++) {
for (auto n = m_freelists[i].head; n; n = n->next) {
assert(free_blocks.find(n) != free_blocks.end());
free_blocks.erase(n);
}
}
assert(free_blocks.empty());
// check the apc array list
assert(apc_arrays.size() == m_apc_arrays.size());
for (auto a : m_apc_arrays) {
assert(apc_arrays.find(a) != apc_arrays.end());
apc_arrays.erase(a);
}
assert(apc_arrays.empty());
// check the apc string list
for (StringDataNode *next, *n = m_strings.next; n != &m_strings; n = next) {
next = n->next;
auto const s = StringData::node2str(n);
assert(s->isProxy());
assert(apc_strings.find(s) != apc_strings.end());
apc_strings.erase(s);
}
assert(apc_strings.empty());
// heap check is done. If we are not exiting, check pointers using HeapGraph
if (Trace::moduleEnabled(Trace::heapreport)) {
auto g = makeHeapGraph();
if (!exiting()) checkPointers(g, phase);
if (Trace::moduleEnabled(Trace::heapreport, 2)) {
printHeapReport(g, phase);
}
}
}
/*
* Store slab tail bytes (if any) in freelists.
*/
inline void MemoryManager::storeTail(void* tail, uint32_t tailBytes) {
void* rem = tail;
for (uint32_t remBytes = tailBytes; remBytes > 0;) {
uint32_t fragBytes = remBytes;
assert(fragBytes >= kSmallSizeAlign);
assert((fragBytes & kSmallSizeAlignMask) == 0);
unsigned fragInd = smallSize2Index(fragBytes + 1) - 1;
uint32_t fragUsable = smallIndex2Size(fragInd);
void* frag = (void*)(uintptr_t(rem) + remBytes - fragUsable);
FTRACE(4, "MemoryManager::storeTail({}, {}): rem={}, remBytes={}, "
"frag={}, fragBytes={}, fragUsable={}, fragInd={}\n", tail,
(void*)uintptr_t(tailBytes), rem, (void*)uintptr_t(remBytes),
frag, (void*)uintptr_t(fragBytes), (void*)uintptr_t(fragUsable),
fragInd);
m_freelists[fragInd].push(frag, fragUsable);
remBytes -= fragUsable;
}
}
/*
* Create nSplit contiguous regions and store them in the appropriate freelist.
*/
inline void MemoryManager::splitTail(void* tail, uint32_t tailBytes,
unsigned nSplit, uint32_t splitUsable,
unsigned splitInd) {
assert(tailBytes >= kSmallSizeAlign);
assert((tailBytes & kSmallSizeAlignMask) == 0);
assert((splitUsable & kSmallSizeAlignMask) == 0);
assert(nSplit * splitUsable <= tailBytes);
for (uint32_t i = nSplit; i--;) {
void* split = (void*)(uintptr_t(tail) + i * splitUsable);
FTRACE(4, "MemoryManager::splitTail(tail={}, tailBytes={}, tailPast={}): "
"split={}, splitUsable={}, splitInd={}\n", tail,
(void*)uintptr_t(tailBytes), (void*)(uintptr_t(tail) + tailBytes),
split, splitUsable, splitInd);
m_freelists[splitInd].push(split, splitUsable);
}
void* rem = (void*)(uintptr_t(tail) + nSplit * splitUsable);
assert(tailBytes >= nSplit * splitUsable);
uint32_t remBytes = tailBytes - nSplit * splitUsable;
assert(uintptr_t(rem) + remBytes == uintptr_t(tail) + tailBytes);
storeTail(rem, remBytes);
}
/*
* Get a new slab, then allocate nbytes from it and install it in our
* slab list. Return the newly allocated nbytes-sized block.
*/
NEVER_INLINE void* MemoryManager::newSlab(uint32_t nbytes) {
if (UNLIKELY(m_stats.usage > m_stats.maxBytes)) {
refreshStats();
}
storeTail(m_front, (char*)m_limit - (char*)m_front);
if (debug && RuntimeOption::EvalCheckHeapOnAlloc && !g_context.isNull()) {
setSurpriseFlag(PendingGCFlag); // defer heap check until safepoint
}
auto slab = m_heap.allocSlab(kSlabSize);
assert((uintptr_t(slab.ptr) & kSmallSizeAlignMask) == 0);
m_stats.borrow(slab.size);
m_stats.alloc += slab.size;
if (m_stats.alloc > m_stats.peakAlloc) {
m_stats.peakAlloc = m_stats.alloc;
}
m_front = (void*)(uintptr_t(slab.ptr) + nbytes);
m_limit = (void*)(uintptr_t(slab.ptr) + slab.size);
FTRACE(3, "newSlab: adding slab at {} to limit {}\n", slab.ptr, m_limit);
return slab.ptr;
}
/*
* Allocate `bytes' from the current slab, aligned to kSmallSizeAlign.
*/
inline void* MemoryManager::slabAlloc(uint32_t bytes, unsigned index) {
FTRACE(3, "slabAlloc({}, {}): m_front={}, m_limit={}\n", bytes, index,
m_front, m_limit);
uint32_t nbytes = smallIndex2Size(index);
assert(bytes <= nbytes);
assert(nbytes <= kSlabSize);
assert((nbytes & kSmallSizeAlignMask) == 0);
assert((uintptr_t(m_front) & kSmallSizeAlignMask) == 0);
if (UNLIKELY(m_bypassSlabAlloc)) {
// Stats correction; mallocBigSize() pulls stats from jemalloc.
m_stats.usage -= bytes;
return mallocBigSize<false>(nbytes).ptr;
}
void* ptr = m_front;
{
void* next = (void*)(uintptr_t(ptr) + nbytes);
if (uintptr_t(next) <= uintptr_t(m_limit)) {
m_front = next;
} else {
ptr = newSlab(nbytes);
}
}
// Preallocate more of the same in order to amortize entry into this method.
unsigned nSplit = kNContigTab[index] - 1;
uintptr_t avail = uintptr_t(m_limit) - uintptr_t(m_front);
if (UNLIKELY(nSplit * nbytes > avail)) {
nSplit = avail / nbytes; // Expensive division.
}
if (nSplit > 0) {
void* tail = m_front;
uint32_t tailBytes = nSplit * nbytes;
m_front = (void*)(uintptr_t(m_front) + tailBytes);
splitTail(tail, tailBytes, nSplit, nbytes, index);
}
FTRACE(4, "slabAlloc({}, {}) --> ptr={}, m_front={}, m_limit={}\n", bytes,
index, ptr, m_front, m_limit);
return ptr;
}
void* MemoryManager::mallocSmallSizeSlow(uint32_t bytes, unsigned index) {
size_t nbytes = smallIndex2Size(index);
unsigned nContig = kNContigTab[index];
size_t contigMin = nContig * nbytes;
unsigned contigInd = smallSize2Index(contigMin);
for (unsigned i = contigInd; i < kNumSmallSizes; ++i) {
FTRACE(4, "MemoryManager::mallocSmallSizeSlow({}-->{}, {}): contigMin={}, "
"contigInd={}, try i={}\n", bytes, nbytes, index, contigMin,
contigInd, i);
void* p = m_freelists[i].maybePop();
if (p != nullptr) {
FTRACE(4, "MemoryManager::mallocSmallSizeSlow({}-->{}, {}): "
"contigMin={}, contigInd={}, use i={}, size={}, p={}\n", bytes,
nbytes, index, contigMin, contigInd, i, smallIndex2Size(i),
p);
// Split tail into preallocations and store them back into freelists.
uint32_t availBytes = smallIndex2Size(i);
uint32_t tailBytes = availBytes - nbytes;
if (tailBytes > 0) {
void* tail = (void*)(uintptr_t(p) + nbytes);
splitTail(tail, tailBytes, nContig - 1, nbytes, index);
}
return p;
}
}
// No available free list items; carve new space from the current slab.
return slabAlloc(bytes, index);
}
inline void MemoryManager::updateBigStats() {
// If we are using jemalloc, it is keeping track of allocations outside of
// the slabs and the usage so we should force this after an allocation that
// was too large for one of the existing slabs. When we're not using jemalloc
// this check won't do anything so avoid the extra overhead.
if (use_jemalloc || UNLIKELY(m_stats.usage > m_stats.maxBytes)) {
refreshStats();
}
}
NEVER_INLINE
void* MemoryManager::mallocBig(size_t nbytes) {
assert(nbytes > 0);
auto block = m_heap.allocBig(nbytes, HeaderKind::BigMalloc);
updateBigStats();
return block.ptr;
}
template NEVER_INLINE
MemBlock MemoryManager::mallocBigSize<true>(size_t);
template NEVER_INLINE
MemBlock MemoryManager::mallocBigSize<false>(size_t);
template<bool callerSavesActualSize> NEVER_INLINE
MemBlock MemoryManager::mallocBigSize(size_t bytes) {
if (debug) eagerGCCheck();
auto block = m_heap.allocBig(bytes, HeaderKind::BigObj);
auto szOut = block.size;
#ifdef USE_JEMALLOC
// NB: We don't report the SweepNode size in the stats.
auto const delta = callerSavesActualSize ? szOut : bytes;
m_stats.usage += int64_t(delta);
// Adjust jemalloc otherwise we'll double count the direct allocation.
m_stats.borrow(delta);
#else
m_stats.usage += bytes;
#endif
updateBigStats();
auto ptrOut = block.ptr;
FTRACE(3, "mallocBigSize: {} ({} requested, {} usable)\n",
ptrOut, bytes, szOut);
return {ptrOut, szOut};
}
NEVER_INLINE
void* MemoryManager::callocBig(size_t totalbytes) {
if (debug) eagerGCCheck();
assert(totalbytes > 0);
auto block = m_heap.callocBig(totalbytes);
updateBigStats();
return block.ptr;
}
// req::malloc api entry points, with support for malloc/free corner cases.
namespace req {
void* malloc(size_t nbytes) {
auto const size = std::max(nbytes, size_t(1));
return MM().malloc(size);
}
void* calloc(size_t count, size_t nbytes) {
auto const totalBytes = std::max<size_t>(count * nbytes, 1);
if (totalBytes <= kMaxSmallSize) {