mycpp

Coverage Report

Created: 2025-06-02 15:20

/home/uke/oil/mycpp/mark_sweep_heap.cc
Line
Count
Source (jump to first uncovered line)
1
#include "mycpp/mark_sweep_heap.h"
2
3
#include <inttypes.h>  // PRId64
4
#include <stdio.h>     // dprintf()
5
#include <stdlib.h>    // getenv()
6
#include <string.h>    // strlen()
7
#include <sys/time.h>  // gettimeofday()
8
#include <time.h>      // clock_gettime(), CLOCK_PROCESS_CPUTIME_ID
9
#include <unistd.h>    // STDERR_FILENO
10
11
#include "_build/detected-cpp-config.h"  // for GC_TIMING
12
#include "mycpp/gc_builtins.h"           // StringToInt()
13
#include "mycpp/gc_slab.h"
14
15
// TODO: Remove this guard when we have separate binaries
16
#if MARK_SWEEP
17
18
18
void MarkSweepHeap::Init() {
19
18
  Init(1000);  // collect at 1000 objects in tests
20
18
}
21
22
19
void MarkSweepHeap::Init(int gc_threshold) {
23
19
  gc_threshold_ = gc_threshold;
24
25
19
  char* e;
26
19
  e = getenv("OILS_GC_THRESHOLD");
27
19
  if (e) {
28
0
    int result;
29
0
    if (StringToInt(e, strlen(e), 10, &result)) {
30
      // Override collection threshold
31
0
      gc_threshold_ = result;
32
0
    }
33
0
  }
34
35
  // only for developers
36
19
  e = getenv("_OILS_GC_VERBOSE");
37
19
  if (e && strcmp(e, "1") == 0) {
38
0
    gc_verbose_ = true;
39
0
  }
40
41
19
  live_objs_.reserve(KiB(10));
42
19
  roots_.reserve(KiB(1));  // prevent resizing in common case
43
19
}
44
45
2
int MarkSweepHeap::MaybeCollect() {
46
  // Maybe collect BEFORE allocation, because the new object won't be rooted
47
  #if GC_ALWAYS
48
  int result = Collect();
49
  #else
50
2
  int result = -1;
51
2
  if (num_live() > gc_threshold_) {
52
0
    result = Collect();
53
0
  }
54
2
  #endif
55
56
2
  num_gc_points_++;  // this is a manual collection point
57
2
  return result;
58
2
}
59
60
  #if defined(BUMP_SMALL)
61
    #include "mycpp/bump_leak_heap.h"
62
63
BumpLeakHeap gBumpLeak;
64
  #endif
65
66
// Allocate and update stats
67
// TODO: Make this interface nicer.
68
2.89k
void* MarkSweepHeap::Allocate(size_t num_bytes, int* obj_id, int* pool_id) {
69
  // log("Allocate %d", num_bytes);
70
2.89k
  #ifndef NO_POOL_ALLOC
71
2.89k
  if (num_bytes <= pool1_.kMaxObjSize) {
72
1.66k
    *pool_id = 1;
73
1.66k
    return pool1_.Allocate(obj_id);
74
1.66k
  }
75
1.22k
  if (num_bytes <= pool2_.kMaxObjSize) {
76
693
    *pool_id = 2;
77
693
    return pool2_.Allocate(obj_id);
78
693
  }
79
535
  *pool_id = 0;  // malloc(), not a pool
80
535
  #endif
81
82
  // Does the pool allocator approximate a bump allocator?  Use pool2_
83
  // threshold of 48 bytes.
84
  // These only work with GC off -- OILS_GC_THRESHOLD=[big]
85
  #ifdef BUMP_SMALL
86
  if (num_bytes <= 48) {
87
    return gBumpLeak.Allocate(num_bytes);
88
  }
89
  #endif
90
91
535
  if (to_free_.empty()) {
92
    // Use higher object IDs
93
535
    *obj_id = greatest_obj_id_;
94
535
    greatest_obj_id_++;
95
96
    // This check is ON in release mode
97
535
    CHECK(greatest_obj_id_ <= kMaxObjId);
98
535
  } else {
99
0
    ObjHeader* dead = to_free_.back();
100
0
    to_free_.pop_back();
101
102
0
    *obj_id = dead->obj_id;  // reuse the dead object's ID
103
104
0
    free(dead);
105
0
  }
106
107
0
  void* result = malloc(num_bytes);
108
535
  DCHECK(result != nullptr);
109
110
0
  live_objs_.push_back(static_cast<ObjHeader*>(result));
111
112
535
  num_live_++;
113
535
  num_allocated_++;
114
535
  bytes_allocated_ += num_bytes;
115
116
535
  return result;
117
1.22k
}
118
119
  #if 0
120
void* MarkSweepHeap::Reallocate(void* p, size_t num_bytes) {
121
  FAIL(kNotImplemented);
122
  // This causes a double-free in the GC!
123
  // return realloc(p, num_bytes);
124
}
125
  #endif
126
127
// "Leaf" for marking / TraceChildren
128
//
129
// - Abort if nullptr
130
// - Find the header (get rid of this when remove ObjHeader member)
131
// - Tag::{Opaque,FixedSized,Scanned} have their mark bits set
132
// - Tag::{FixedSize,Scanned} are also pushed on the gray stack
133
134
65
void MarkSweepHeap::MaybeMarkAndPush(RawObject* obj) {
135
65
  ObjHeader* header = ObjHeader::FromObject(obj);
136
65
  if (header->heap_tag == HeapTag::Global) {  // don't mark or push
137
4
    return;
138
4
  }
139
140
61
  int obj_id = header->obj_id;
141
61
  #ifndef NO_POOL_ALLOC
142
61
  if (header->pool_id == 1) {
143
56
    if (pool1_.IsMarked(obj_id)) {
144
14
      return;
145
14
    }
146
42
    pool1_.Mark(obj_id);
147
42
  } else if (header->pool_id == 2) {
148
5
    if (pool2_.IsMarked(obj_id)) {
149
0
      return;
150
0
    }
151
5
    pool2_.Mark(obj_id);
152
5
  } else
153
0
  #endif
154
0
  {
155
0
    if (mark_set_.IsMarked(obj_id)) {
156
0
      return;
157
0
    }
158
0
    mark_set_.Mark(obj_id);
159
0
  }
160
161
47
  switch (header->heap_tag) {
162
16
  case HeapTag::Opaque:  // e.g. strings have no children
163
16
    break;
164
165
4
  case HeapTag::Scanned:  // these 2 types have children
166
31
  case HeapTag::FixedSize:
167
31
    gray_stack_.push_back(header);  // Push the header, not the object!
168
31
    break;
169
170
0
  default:
171
0
    FAIL(kShouldNotGetHere);
172
47
  }
173
47
}
174
175
39
void MarkSweepHeap::TraceChildren() {
176
70
  while (!gray_stack_.empty()) {
177
31
    ObjHeader* header = gray_stack_.back();
178
31
    gray_stack_.pop_back();
179
180
31
    switch (header->heap_tag) {
181
27
    case HeapTag::FixedSize: {
182
27
      auto fixed = reinterpret_cast<LayoutFixed*>(header->ObjectAddress());
183
27
      int mask = FIELD_MASK(*header);
184
185
675
      for (int i = 0; i < kFieldMaskBits; ++i) {
186
648
        if (mask & (1 << i)) {
187
21
          RawObject* child = fixed->children_[i];
188
21
          if (child) {
189
18
            MaybeMarkAndPush(child);
190
18
          }
191
21
        }
192
648
      }
193
27
      break;
194
0
    }
195
196
4
    case HeapTag::Scanned: {
197
4
      auto slab = reinterpret_cast<Slab<RawObject*>*>(header->ObjectAddress());
198
199
4
      int n = NUM_POINTERS(*header);
200
24
      for (int i = 0; i < n; ++i) {
201
20
        RawObject* child = slab->items_[i];
202
20
        if (child) {
203
5
          MaybeMarkAndPush(child);
204
5
        }
205
20
      }
206
4
      break;
207
0
    }
208
0
    default:
209
      // Only FixedSize and Scanned are pushed
210
0
      FAIL(kShouldNotGetHere);
211
31
    }
212
31
  }
213
39
}
214
215
39
void MarkSweepHeap::Sweep() {
216
39
  #ifndef NO_POOL_ALLOC
217
39
  pool1_.Sweep();
218
39
  pool2_.Sweep();
219
39
  #endif
220
221
39
  int last_live_index = 0;
222
39
  int num_objs = live_objs_.size();
223
574
  for (int i = 0; i < num_objs; ++i) {
224
535
    ObjHeader* obj = live_objs_[i];
225
535
    DCHECK(obj);  // malloc() shouldn't have returned nullptr
226
227
0
    bool is_live = mark_set_.IsMarked(obj->obj_id);
228
229
    // Compact live_objs_ and populate to_free_.  Note: doing the reverse could
230
    // be more efficient when many objects are dead.
231
535
    if (is_live) {
232
0
      live_objs_[last_live_index++] = obj;
233
535
    } else {
234
535
      to_free_.push_back(obj);
235
      // free(obj);
236
535
      num_live_--;
237
535
    }
238
535
  }
239
39
  live_objs_.resize(last_live_index);  // remove dangling objects
240
241
39
  num_collections_++;
242
39
  max_survived_ = std::max(max_survived_, num_live());
243
39
}
244
245
39
int MarkSweepHeap::Collect() {
246
39
  #ifdef GC_TIMING
247
39
  struct timespec start, end;
248
39
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) < 0) {
249
0
    FAIL("clock_gettime failed");
250
0
  }
251
0
  #endif
252
253
0
  int num_roots = roots_.size();
254
39
  int num_globals = global_roots_.size();
255
256
39
  if (gc_verbose_) {
257
0
    log("");
258
0
    log("%2d. GC with %d roots (%d global) and %d live objects",
259
0
        num_collections_, num_roots + num_globals, num_globals, num_live());
260
0
  }
261
262
  // Resize it
263
39
  mark_set_.ReInit(greatest_obj_id_);
264
39
  #ifndef NO_POOL_ALLOC
265
39
  pool1_.PrepareForGc();
266
39
  pool2_.PrepareForGc();
267
39
  #endif
268
269
  // Mark roots.
270
  // Note: It might be nice to get rid of double pointers
271
106
  for (int i = 0; i < num_roots; ++i) {
272
67
    RawObject* root = *(roots_[i]);
273
67
    if (root) {
274
42
      MaybeMarkAndPush(root);
275
42
    }
276
67
  }
277
278
39
  for (int i = 0; i < num_globals; ++i) {
279
0
    RawObject* root = global_roots_[i];
280
0
    if (root) {
281
0
      MaybeMarkAndPush(root);
282
0
    }
283
0
  }
284
285
  // Traverse object graph.
286
39
  TraceChildren();
287
288
39
  Sweep();
289
290
39
  if (gc_verbose_) {
291
0
    log("    %d live after sweep", num_live());
292
0
  }
293
294
  // We know how many are live.  If the number of objects is close to the
295
  // threshold (above 75%), then set the threshold to 2 times the number of
296
  // live objects.  This is an ad hoc policy that removes observed "thrashing"
297
  // -- being at 99% of the threshold and doing FUTILE mark and sweep.
298
299
39
  int water_mark = (gc_threshold_ * 3) / 4;
300
39
  if (num_live() > water_mark) {
301
0
    gc_threshold_ = num_live() * 2;
302
0
    num_growths_++;
303
0
    if (gc_verbose_) {
304
0
      log("    exceeded %d live objects; gc_threshold set to %d", water_mark,
305
0
          gc_threshold_);
306
0
    }
307
0
  }
308
309
39
  #ifdef GC_TIMING
310
39
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0) {
311
0
    FAIL("clock_gettime failed");
312
0
  }
313
314
0
  double start_secs = start.tv_sec + start.tv_nsec / 1e9;
315
39
  double end_secs = end.tv_sec + end.tv_nsec / 1e9;
316
39
  double gc_millis = (end_secs - start_secs) * 1000.0;
317
318
39
  if (gc_verbose_) {
319
0
    log("    %.1f ms GC", gc_millis);
320
0
  }
321
322
39
  total_gc_millis_ += gc_millis;
323
39
  if (gc_millis > max_gc_millis_) {
324
14
    max_gc_millis_ = gc_millis;
325
14
  }
326
39
  #endif
327
328
39
  return num_live();  // for unit tests only
329
39
}
330
331
0
void MarkSweepHeap::PrintShortStats() {
332
  // TODO: should use feature detection of dprintf
333
0
  #ifndef OILS_WIN32
334
0
    #ifndef NO_POOL_ALLOC
335
0
  int fd = 2;
336
0
  dprintf(fd, "  num allocated    = %10d\n",
337
0
          num_allocated_ + pool1_.num_allocated() + pool2_.num_allocated());
338
0
  dprintf(
339
0
      fd, "bytes allocated    = %10" PRId64 "\n",
340
0
      bytes_allocated_ + pool1_.bytes_allocated() + pool2_.bytes_allocated());
341
0
    #endif
342
0
  #endif
343
0
}
344
345
3
void MarkSweepHeap::PrintStats(int fd) {
346
  // TODO: should use feature detection of dprintf
347
3
  #ifndef OILS_WIN32
348
3
  dprintf(fd, "  num live         = %10d\n", num_live());
349
  // max survived_ can be less than num_live(), because leave off the last GC
350
3
  dprintf(fd, "  max survived     = %10d\n", max_survived_);
351
3
  dprintf(fd, "\n");
352
353
3
    #ifndef NO_POOL_ALLOC
354
3
  dprintf(fd, "  num allocated    = %10d\n",
355
3
          num_allocated_ + pool1_.num_allocated() + pool2_.num_allocated());
356
3
  dprintf(fd, "  num in heap      = %10d\n", num_allocated_);
357
    #else
358
  dprintf(fd, "  num allocated    = %10d\n", num_allocated_);
359
    #endif
360
361
3
    #ifndef NO_POOL_ALLOC
362
3
  dprintf(fd, "  num in pool 1    = %10d\n", pool1_.num_allocated());
363
3
  dprintf(fd, "  num in pool 2    = %10d\n", pool2_.num_allocated());
364
3
  dprintf(
365
3
      fd, "bytes allocated    = %10" PRId64 "\n",
366
3
      bytes_allocated_ + pool1_.bytes_allocated() + pool2_.bytes_allocated());
367
    #else
368
  dprintf(fd, "bytes allocated    = %10" PRId64 "\n", bytes_allocated_);
369
    #endif
370
371
3
  dprintf(fd, "\n");
372
3
  dprintf(fd, "  num gc points    = %10d\n", num_gc_points_);
373
3
  dprintf(fd, "  num collections  = %10d\n", num_collections_);
374
3
  dprintf(fd, "\n");
375
3
  dprintf(fd, "   gc threshold    = %10d\n", gc_threshold_);
376
3
  dprintf(fd, "  num growths      = %10d\n", num_growths_);
377
3
  dprintf(fd, "\n");
378
3
  dprintf(fd, "  max gc millis    = %10.1f\n", max_gc_millis_);
379
3
  dprintf(fd, "total gc millis    = %10.1f\n", total_gc_millis_);
380
3
  dprintf(fd, "\n");
381
3
  dprintf(fd, "roots capacity     = %10d\n",
382
3
          static_cast<int>(roots_.capacity()));
383
3
  dprintf(fd, " objs capacity     = %10d\n",
384
3
          static_cast<int>(live_objs_.capacity()));
385
3
  #endif
386
3
}
387
388
// Cleanup at the end of main() to remain ASAN-safe
389
12
void MarkSweepHeap::MaybePrintStats() {
390
12
  int stats_fd = -1;
391
12
  char* e = getenv("OILS_GC_STATS");
392
12
  if (e && strlen(e)) {  // env var set and non-empty
393
0
    stats_fd = STDERR_FILENO;
394
12
  } else {
395
    // A raw file descriptor lets benchmarks extract stats even if the script
396
    // writes to stdout and stderr.  Shells can't use open() without potential
397
    // conflicts.
398
399
12
    e = getenv("OILS_GC_STATS_FD");
400
12
    if (e && strlen(e)) {
401
      // Try setting 'stats_fd'.  If there's an error, it will be unchanged, and
402
      // we don't PrintStats();
403
0
      StringToInt(e, strlen(e), 10, &stats_fd);
404
0
    }
405
12
  }
406
407
12
  if (stats_fd != -1) {
408
0
    PrintStats(stats_fd);
409
0
  }
410
12
}
411
412
11
void MarkSweepHeap::FreeEverything() {
413
11
  roots_.clear();
414
11
  global_roots_.clear();
415
416
11
  Collect();
417
418
  // Collect() told us what to free()
419
535
  for (auto obj : to_free_) {
420
535
    free(obj);
421
535
  }
422
11
  #ifndef NO_POOL_ALLOC
423
11
  pool1_.Free();
424
11
  pool2_.Free();
425
11
  #endif
426
11
}
427
428
11
void MarkSweepHeap::CleanProcessExit() {
429
11
  MaybePrintStats();
430
431
11
  char* e = getenv("OILS_GC_ON_EXIT");
432
  // collect by default; OILS_GC_ON_EXIT=0 overrides
433
11
  if (e && strcmp(e, "0") == 0) {
434
0
    ;
435
11
  } else {
436
11
    FreeEverything();
437
11
  }
438
11
}
439
440
// for the main binary
441
1
void MarkSweepHeap::ProcessExit() {
442
1
  MaybePrintStats();
443
444
  #ifdef CLEAN_PROCESS_EXIT
445
  FreeEverything();
446
  #else
447
1
  char* e = getenv("OILS_GC_ON_EXIT");
448
  // don't collect by default; OILS_GC_ON_EXIT=1 overrides
449
1
  if (e && strcmp(e, "1") == 0) {
450
0
    FreeEverything();
451
0
  }
452
1
  #endif
453
1
}
454
455
MarkSweepHeap gHeap;
456
457
#endif  // MARK_SWEEP