File: collector.c

package info (click to toggle)
euslisp 9.31%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,448 kB
  • sloc: ansic: 41,610; lisp: 3,339; makefile: 286; sh: 238; asm: 138; python: 53
file content (1245 lines) | stat: -rw-r--r-- 31,682 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
/* 
 * 2003-
 * collector.c : R.Hanai
 * parallel root scanning, concurrent snapshot garbage collector 
 * with return barrier
 *
 * memos
 * BUGS: 
 * FIXED: copyobj in leo.c
 * TODO:
 *       memory barrier instructions
 *       heap expansion function
 *       write-barriers (copyobj() etc.)
 *       memory management (BIBOP/Lazy Buddy/etc.)
 *       polling / how to scan stacks of suspending threads
 *       mutexes => real-time locks
 *       make thr_self() faster => caching ids.
 *       scan large objects incrementally.
 *       
 *       mark stack overflow
 *       parallel marking (scalability)
 *
 *       <problematic functions>
 *       bindspecial: increment stack collectively
 */

#include <sys/times.h>
#include "eus.h"
#include <sys/param.h>
#include "time.h"
#include "rgc_utils.h"
#include "xccmem.h"


#ifdef __ART_LINUX
#include <linux/art_task.h>
#endif

#if Linux
char *minmemory=(char *)1000000;
#endif
extern pointer K_DISPOSE;
#define MAXDISPOSE 256
static pointer dispose[MAXDISPOSE];
static int gcmerge, dispose_count;

extern struct {
  char using;
  mutex_t *lock;
  thread_t tid;
} thread_table[]; /* defined in "mthread_posix.c" */

struct _gc_data gc_data;
barrier_t startup_barrier;

#define GCDEBUG
//#undef GCDEBUG
#ifdef GCDEBUG
static int white_cells, black_cells, free_cells;
#endif

#ifdef __PROFILE_GC
static int gctime = 0;
int allocd_words = 0;
#endif

static void do_scan_roots();
static void init_sync_data();

#define gcpush(v, off) \
{ \
  lgcsp->addr = (pointer)v; \
  lgcsp->offset = off; \
  lgcsp++; \
}

static pnewgcstack(oldsp)
     register ms_entry *oldsp;
{
  register ms_entry *oldstack, *stk, *newstack, *newgcsp;
  long top, oldsize, newsize;

  oldstack=stk=collector_stack;
  oldsize=collector_stacklimit-oldstack;
  newsize=oldsize*2;
  top=oldsp-collector_stack;
  //  newgcsp=newstack=(pointer *)malloc(newsize * sizeof(pointer)+16);
  newgcsp=newstack=(ms_entry *)malloc(newsize * sizeof(ms_entry)+16);
  fprintf(stderr, "\n\x1b[1;31m;; extending pgcstack 0x%x[%d] --> 0x%x[%d] top=%x\x1b[0m\n",
	  oldstack, oldsize, newstack, newsize, top);
  while (stk<oldsp) *newgcsp++= *stk++;
  collector_stack=newstack;
  collector_stacklimit= &(collector_stack[newsize-10]);
  collector_sp = &(collector_stack[top]);
  cfree(oldstack);
}

static call_disposers()
{ int i;
 context *ctx=current_ctx;
 pointer p,a,curclass;
 /*if (debug) fprintf(stderr, ";; disposal call=%d\n", dispose_count);*/
 for (i=0; i<dispose_count; i++) {
   p=dispose[i];
   p->nodispose=0;
   a=(pointer)findmethod(ctx,K_DISPOSE,classof(p), &curclass); 
   if (debug) fprintf(stderr, ";; (send %x :dispose)\n", p);
   if (a!=NIL) csend(ctx,p,K_DISPOSE,0);
 }}

static struct _marking_state {
  int is_checking_pstack;
  int cur_mut_num;
} marking_state;

struct _sweeping_state sweeping_state;

static inline void go_on_to_sweep_phase()
{
  numunion nu;
  DPRINT2("mark->sweep: free rate = %lf", (double)freeheap / totalheap);
  gcmerge = totalheap * min(1.0, fltval(speval(GCMARGIN)))
    * max(0.1, fltval(speval(GCMERGE))); 
  /* default: GCMERGIN=0.25, GCMERGE=0.2 
     ==> no merge if heap occupancy rate is over 95% */
  dispose_count = 0; /* <= Is this O.K.? */

  sweeping_state.chp = chunklist;
  sweeping_state.p = &chunklist->rootcell;
  sweeping_state.tail = 
    (bpointer)((int)sweeping_state.p + (buddysize[sweeping_state.chp->chunkbix] << 2));
  gc_phase = PHASE_SWEEP; 
}

long marked_words = 0;

static int mark_a_little(int m_unit)
{
  extern _end();
  register ms_entry *lgcsp = collector_sp;
  register ms_entry *gcstack = collector_stack;
  register int credit = m_unit;
  unsigned int offset;
  register pointer p, p2;
  register bpointer bp;
  register int i, s;
  context *ctx;

 markloop:
  if(credit <= 0){ 
    /* write back the value of lgcsp */
  //fprintf(stderr, "GC stack size = %d\n", lgcsp - gcstack);
    collector_sp = lgcsp;
    marked_words -= m_unit - credit;
    return 1; /* still marking work is left */
  }
  if(lgcsp > gcstack){
    /* mark from mark stack */
//    lgcsp -= (sizeof(ms_entry)/sizeof(pointer));
    lgcsp--;
    p = lgcsp->addr;
    offset = lgcsp->offset;

  start_mark:
    if(offset == 0){
//      if(!ispointer(p)) goto markloop;  /* p may be an immediate */
      if(!ispointer(p) || !p) goto markloop;

      ASSERT((unsigned)p >= mingcheap);
      ASSERT((unsigned)p < maxgcheap);

      /* these checks aren't normally needed, 
         since this is not a conservative collector */
//      if((int)p < (int)_end) goto markloop;
//
      if(maxmemory < (char *)p) goto markloop;
//      if((char *)p < minmemory) goto markloop;
    }

    /* here, p is a pointer to a live object */
    bp = bpointerof(p);

    if(marked(bp)) goto markloop; /* already marked */
//    if(blacked(bp)) goto markloop; /* already marked */

    markon(bp); /* mark it first to avoid endless marking */

    if(pisclosure(p)){
      /*
        if (p->c.clo.env1>minmemory && p->c.clo.env1<maxmemory)
        fprintf(stderr, "Mark: closure %x's env1 points into heap %x\n", 
        p, p->c.clo.env1);
        if (p->c.clo.env2>minmemory && p->c.clo.env2<maxmemory)
        fprintf(stderr, "Mark: closure %x's env2 points into heap %x\n", 
        p, p->c.clo.env2);
      */
      goto markloop; /* avoid marking contents of closure */
    }
    if(bp->h.elmtype == ELM_FIXED){ /* contents are all pointers */
      s = buddysize[bp->h.bix & TAGMASK] - 1;

      if(s > 300){
        fprintf (stderr, "do_mark: too big object s=%d, header=%x at %x\n", 
		s, bp->h, bp);
        //goto markloop;
      }
      while(lgcsp + s > collector_stacklimit){
        pnewgcstack(lgcsp);
        gcstack = collector_stack;
        lgcsp = collector_sp;
      }
      credit -= (s + 1);
      for(i = 0; i < s; i++){
        p2 = p->c.obj.iv[i];
        if(ispointer(p2)) 
          gcpush(p2, 0);
      }
      goto markloop;
    } else if (bp->h.elmtype == ELM_POINTER) { /* varing number of pointers */
      s = buddysize[bp->h.bix & TAGMASK] - 2;
      while (lgcsp + s > collector_stacklimit) {
        pnewgcstack(lgcsp);
        gcstack = collector_stack;
        lgcsp = collector_sp; /* 961003 kagami */
      }
      credit -= (s + 2);
      for (i = 0; i < s; i++) {
        p2 = p->c.vec.v[i];
        if (ispointer(p2))
          gcpush(p2, 0);
      }
      goto markloop;
    } 

    credit -= buddysize[bp->h.bix & TAGMASK];
    goto markloop;

  } else {

  /* get another root */
  next_root:
    credit--;
    if (!marking_state.is_checking_pstack) {
      for (i = marking_state.cur_mut_num; i < MAXTHREAD; i++) {
        ctx = euscontexts[i];
        if (ctx) {
          if (ctx->gsp > ctx->gcstack) {
            p = *--(ctx->gsp);

            ASSERT((unsigned)p >= mingcheap);
            ASSERT((unsigned)p < maxgcheap);

            offset = 0;
            marking_state.cur_mut_num = i;
/*
  if(credit <= 0){ 
    // write back the value of lgcsp
    gcpush(p, 0);
    collector_sp = lgcsp;
    marked_words -= m_unit - credit;
    return 1; // still marking work is left
  }
          */
            goto start_mark;
          }
        }
      }
      marking_state.is_checking_pstack = 1;
      goto next_root;
    } else {
      mutex_lock(&pstack_lock);
      if(psp > pstack) {
#ifdef COLLECTCACHE /* this is not yet correctly implemented */
#define COLCACHE 10
        int i, ii;
        pointer array[COLCACHE];
        for (i = 0; i < COLCACHE; i++) {
          pointerpop(array[i]);
          if(psp > pstack)
        continue;
          break;
        }
        pcount = pcount + COLCACHE;
        mutex_unlock(&pstack_lock);
        for(ii = 0; ii < i; ii++){
//          mark_a_little(array[ii], 0);
        }
        mutex_lock(&pstack_lock);
#else
        pointerpop(p);
        offset = 0;
        mutex_unlock(&pstack_lock);
/*
  if (credit <= 0) { 
    // write back the value of lgcsp
    gcpush(p, 0);
    collector_sp = lgcsp;
    marked_words -= m_unit - credit;
    return 1; // still marking work is left
  }
  */
        goto start_mark;
#endif
      }
      mutex_unlock(&pstack_lock);
    }
  }

  /* marking finished, now we prepare for following sweeping */
  go_on_to_sweep_phase();
  return 0; 
}


int reclaim(bpointer p)
{
  register int rbix, stat;
  register pointer s;

  s = makepointer(p);
  if(pisfilestream(s)){
    if(!isint (s->c.fstream.fname) && s->c.fstream.direction != NIL){
      if(s->c.fstream.fd == makeint (0)
	 || s->c.fstream.fd == makeint (1)){
	fprintf(stderr, ";; gc! bogus stream at %x fd=%d\n",
		(int) s, intval (s->c.fstream.fd));
      }else if((closestream (s) == 0) && debug)
	fprintf (stderr,
		 ";; gc: dangling stream(address=%x fd=%d) is closed\n",
		 (int) s, intval (s->c.fstream.fd));
    }
  }
  p->h.cix = -1; /* free tag */
  rbix = p->h.bix & TAGMASK;

  mutex_lock(&alloc_lock);
  rw_rdlock(&gc_lock);

  p->b.nextbcell = buddy[rbix].bp;
  buddy[rbix].bp = p;
  buddy[rbix].count++;

  freeheap += buddysize[rbix];
  //  sweepheap += buddysize[rbix];

  rw_unlock(&gc_lock);
  mutex_unlock(&alloc_lock);
  return 0;
}

static int rgc_credit = 0;

/* the cell pointed by 'p' must not be marked */
/* mergecell kindly returns next uncollectable cell address */
static bpointer mergecell(register bpointer p, int cbix)
{
  register bpointer np, p2;

  np = nextbuddy(p);
  while (p->h.b == 0 && ((int) (p->h.bix & TAGMASK)) < cbix) {
//     if (colored(np)) return np;
//    if (marked(np)) return np;
    rgc_credit--;
    if (marked(np) || np->h.cix == -1) return np;
    if (np->h.nodispose == 1) return np;

    p2 = mergecell(np, cbix); /* merge neighbor cell */
    if (np->h.b == 1 && rgc_credit >= 0) { /* can be merged */
	  p->h.b = p->h.m; /* merge them into bigger cell */
      p->h.m = np->h.m;
      p->h.bix++;
      np = p2;
#ifdef GCDEBUG
      white_cells++;
#endif
    } else {
	  reclaim(np);
      return p2;
    }
  }
  return np;
}

/*
 * suppose that { sweeping_state.p, 
 *                sweeping_state.chp,
 *                sweeping_state.tail } are correctly set.
 */
static int sweep_a_little(int gcmerge, int s_unit)
{
  register struct chunk *chp;
  register bpointer p, np, tail;
  
  rgc_credit = s_unit;
  /* restore the state of sweeper */
  chp = sweeping_state.chp;
  p = sweeping_state.p;
  tail = sweeping_state.tail;
  
  if (p == NULL) {
    goto next_chunk;
  }
  //ASSERT( tail && chp );

cont_sweep:
  /* continue sweeping */
  while (p < tail) {
    if (rgc_credit <= 0) {
      sweeping_state.p = p;
      sweeping_state.tail = tail;
      return 1;
    }
//#ifndef __USE_MARK_BITMAP
//    sweeping_state.p = p;
//#endif
    rgc_credit--;
    if (p->h.cix == -1) { /* free object */
#ifdef GCDEBUG
      free_cells++;
#endif
      p = nextbuddy(p);
      continue;
    }
    if (marked(p)) { /* (possibly) live object */
//    if (blacked(p)) { /* (possibly) live object */
#ifdef GCDEBUG
      black_cells++;
#endif
      markoff(p);
      p = nextbuddy(p);
      continue;
    }
    if (p->h.nodispose == 1) {
      if(dispose_count >= MAXDISPOSE)
	fprintf(stderr, "no more space for disposal processing\n");
      else
	dispose[dispose_count++] = makepointer(p);
      p = nextbuddy(p);
    }
    if (gcmerge > freeheap) { /* reclaim and no merge */
#ifdef GCDEBUG
      white_cells++;
#endif
      np = nextbuddy(p);
      reclaim(p);
      p = np;
    } else { /* reclaim and merge *//* update free buddy list */
      np = mergecell(p, chp->chunkbix);
      reclaim(p);
      p = np;
    }
  }
  
next_chunk:
  chp = sweeping_state.chp->nextchunk;
  if (chp == NULL) {
    DPRINT2("sweeping finished: free rate = %lf", (double)freeheap / totalheap);
    DPRINT2("white: %d black: %d free: %d", white_cells, black_cells, free_cells);
    gc_phase = PHASE_EPILOGUE;
    return 0; /* sweeping finished */
  }
  sweeping_state.chp = chp;
  p = &chp->rootcell;
  tail = (bpointer)((int)p + (buddysize[chp->chunkbix] << 2));
  goto cont_sweep;

}

#ifdef __USE_SIGNAL
static void send_root_insertion_signals()
{
  int i;
  thread_t self = pthread_self();

  for (i = 0; i < MAXTHREAD; i++)
    if (!pthread_equal(thread_table[i].tid, self) && euscontexts[i]) {
      if (pthread_kill(thread_table[i].tid, SIGUSR1) != 0) {
	perror("pthread_kill");
      }
    }
}
#endif

void init_rgc(){
  void collect();
  unsigned int gc_thread;

  active_mutator_num = 1;
  gc_region_sync = 0;
  startup_barrier = barrier_init(2); /* mainthread and collector thread */
  gc_phase = PHASE_NOGC;
  ri_core_phase = 0;
  mut_stat_phase = 0x2;
  gc_wakeup_cnt = gc_cmp_cnt = 0;
#ifdef __USE_POLLING
  gc_request_flag = 0;
#endif
  init_sync_data();
  initmemory_rgc(); /* initialize object heap. define in "eus.c" */
  init_utils();
#ifdef __USE_MARK_BITMAP
  allocate_bit_table(); /* allocate mark bit table */
  clear_bit_table();
#endif

  collector_stack = collector_sp = 
    (ms_entry *)malloc(sizeof(ms_entry) * DEFAULT_MAX_RGCSTACK);
  collector_stacklimit = collector_stack + DEFAULT_MAX_RGCSTACK - 10;
   
#ifdef __GC_SEPARATE_THREAD
  thr_create(0, 0, collect, 0, 0, &gc_thread);
  barrier_wait(startup_barrier);
#endif
}

static pointer rgc_classtable = NULL;

void rgc_add_to_classtable(pointer newclass) {
  static int clsidx = 0;
  int i;
  /* allocate class table for marking */
  if (rgc_classtable == NULL) {
    rgc_classtable = 
      rgc_alloc((MAXCLASS + 1), ELM_POINTER, vectorcp.cix, MAXCLASS + 1);
    rgc_classtable->c.vec.size = makeint(MAXCLASS);
    for (i = 0; i < MAXCLASS; i++)
      rgc_classtable->c.vec.v[i] = NIL;
  }
  rgc_classtable->c.vec.v[clsidx++] = newclass;
}

static void scan_global_roots()
{
  int i;
  pointerpush(sysobj);
  pointerpush(pkglist);
  /* minimize scanning time for class table */
  pointerpush(rgc_classtable);
  /*
  for(i = 0; i < MAXCLASS; i++){
    if(ispointer(classtab[i].def)){
      pointerpush (classtab[i].def);
//      ASSERT((unsigned)(classtab[i].def == 0) || 
//      (unsigned)(classtab[i].def) >= mingcheap);
//      ASSERT((unsigned)(classtab[i].def) < maxgcheap);
    }
  }
  */
}

static void scan_local_roots(int i)
{
  register pointer *p;
  register bpointer q;
  register context *ctx = euscontexts[i];

  pgcpush(ctx->threadobj);
  pgcpush(ctx->specials);
  
  q = bpointerof(ctx->lastalloc);
  if (q && ispointer(q)) {
      pgcpush(ctx->lastalloc);
      ASSERT((unsigned)q >= mingcheap);
      ASSERT((unsigned)q < maxgcheap);
  }

#ifdef __RETURN_BARRIER
  {
    pointer *frame_base, *p;

    //DPRINT3("start scanning current frame: %d ",i);
    mutex_lock(&ctx->rbar.lock); /* <-- this lock wouldn't be needed */

    if (ctx->callfp != NULL) 
      frame_base = (pointer *)ctx->callfp;
    else 
      frame_base = ctx->stack;

    for (p = ctx->vsp - 1; p >= frame_base; p--) {
        /*
         * stack frame can contain
         * 1, immediates
         * 2, references to the words in this LISP stack (static links, dynamic links)
         * 3, this would be a bug: references to the words in a native stack.
         *    (jmp_buf in blockframe and catchframe. 
         *    See "makeblock", "eussetjmp", "funlambda", "mkcatchframe")  
         */
      if (*p == NULL) continue;
      if (((int)(*p) & 3)) continue; 
      if ((ctx->stack <= (pointer *)*p) && ((pointer *)*p <= ctx->stacklimit)) 
        continue;
      if ((pointer *)*p >= (pointer *)maxgcheap) continue;
      if ((pointer *)*p < (pointer *)mingcheap) continue;

	  pgcpush(*p);    
      ASSERT((unsigned)(*p) >= mingcheap);
      ASSERT((unsigned)(*p) < maxgcheap);
    }

    if (frame_base == ctx->stack) {
      ctx->rbar.pointer = NULL;
    } else {
      ctx->rbar.pointer = frame_base;
    }
    mutex_unlock(&ctx->rbar.lock); /* <-- this lock wouldn't be needed */
    //DPRINT3("scanning current frame completed");
  }

#else /* original snapshot gc */

  /* push roots in thread's stack */
  for (p = ctx->vsp - 1; p >= ctx->stack; p--) {
    // for(p = ctx->stack; p < ctx->vsp; p++) {
    if (*p == NULL) continue;
    if (((int)(*p) & 3)) continue; 
    if ((ctx->stack <= (pointer *)*p) && ((pointer *)*p <= ctx->stacklimit)) 
      continue;
    if ((pointer *)*p >= (pointer *)maxgcheap) continue;
    if ((pointer *)*p < (pointer *)mingcheap) continue;

	pgcpush(*p);    
    ASSERT((unsigned)(*p) >= mingcheap);
    ASSERT((unsigned)(*p) < maxgcheap);
  }
#endif 
}

#if 0
static void scan_suspending_thread_roots()
{
  int id, c;
  for(id = 0; id < MAXTHREAD; id++){
    if(thread_table[id].using){
      mutex_lock(&mut_stat_table[id].lock); 
      if(mut_stat_table[id].stat == 0x3){ /* 'suspended' & 'need_scan' */
        mut_stat_table[id].stat = 0x5; /* 'suspended' & 'scanning' */
        mutex_unlock(&mut_stat_table[id].lock); 
        scan_local_roots(id);
        finish_access_before_read();
        do{
          c = read_volatile_int(frame_scan_sync);
        }while(cas_int(frame_scan_sync, c, c + 1));
        mutex_lock(&mut_stat_table[id].lock);
        mut_stat_table[id].stat = 0x1; /* 'suspended' */
      }
      mutex_unlock(&mut_stat_table[id].lock); 
    }
  }
}
#endif

#ifdef __RETURN_BARRIER
#define INSERT_UNIT 4 /* 2 or 4 will be good */

static void scan_remaining_roots()
{
  int i, local_root_count, inserted_root_count;
  static char idx[MAXTHREAD];

  local_root_count = 0;

  for (i = 0; i < MAXTHREAD; i++) {
    if (euscontexts[i] && euscontexts[i]->rbar.pointer) {
      idx[local_root_count] = i;
      local_root_count++;
    }
  }

  inserted_root_count = local_root_count;
  
  do {
    for (i = 0; i < local_root_count; i++) {
      context *ctx;
      register pointer *p;
      int counter, tid;

      tid = idx[i];
      ctx = euscontexts[tid];
      if ((ctx)->rbar.pointer == NULL) continue;
      
      mutex_lock(&((ctx)->rbar.lock));
      //DPRINT3("scheduler inserting thread : %d's local roots", i);
      p = (ctx)->rbar.pointer - 1;
      counter = INSERT_UNIT;

      while (1) {
        if (p < ctx->stack) break;
        if ((((int)(*p) & 3) == 0)
            && ((ctx->stack > (pointer *)*p) || ((pointer *)*p > ctx->stacklimit))
            && (((pointer *)*p >= (pointer *)mingcheap && (pointer *)*p < (pointer *)maxgcheap))) {
	      pgcpush(*p);
      ASSERT((unsigned)(*p) >= mingcheap);
      ASSERT((unsigned)(*p) < maxgcheap);
        }
    	p--;
        counter--;
    	if(counter == 0) break;
      }
      (ctx)->rbar.pointer = p + 1;
	  
      if (p < ctx->stack) {
	(ctx)->rbar.pointer = NULL;
	inserted_root_count--;
      }
      mutex_unlock(&(ctx)->rbar.lock);
    }
  }
  while (inserted_root_count != 0);
}
#endif /* __RETURN_BARRIER */

unsigned int gs[MAXTHREAD];
/*
 * suppose that we don't have collector_lock
 */
void notify_gc()
{
  int id, phase, c;
  unsigned int s, e;
//  unlock_collector;
  /* reset synchronization variables */
//  lock_collector;
/*  if (gc_phase != PHASE_NOGC) {
    unlock_collector;
    return;
  }
*/
  id = thr_self(); 
//  gs[id] = current_utime();

  gc_phase = PHASE_PROLOGUE;
  gc_point_sync = 0;
  phase = ri_core_phase;
  mut_stat_phase = mut_stat_phase ^ 0x2;
#ifdef __USE_POLLING
  //  for(id = 0; id < MAXTHREAD; id++){
  //    if(thread_table[id].using){
  //      mutex_lock(&mut_stat_table[id].lock);    
  //      mut_stat_table[id].stat |= 0x2; /* set 'need_scan' flag */
  //      if(mut_stat_table[id].stat & 0x1){ /* 'suspended'? */
  //        do{
  //          c = gc_point_sync;
  //        }while(cas_int(gc_point_sync, c, c + 1));
  //      }
  //      mutex_unlock(&mut_stat_table[id].lock);    
  //    }
  //  }
#endif
#ifdef __USE_SIGNAL
  send_root_insertion_signals();
#else /* __USE_POLLING */
  gc_request_flag = 1;
#endif
  marking_state.is_checking_pstack = 0;
  marking_state.cur_mut_num = 0;
  marked_words = 0;
  unlock_collector;
  
  do {
    c = gc_point_sync;
  } while(cas_int(gc_point_sync, c, c + 1));

  if (gc_point_sync + gc_region_sync < active_mutator_num) {
    sched_yield(); // nanosleep(0) might be better
  } else {
    lock_collector;
    if (phase == ri_core_phase) {
      do_scan_roots(); 
    }
    unlock_collector;
  }
  
  /* wait until root scanning(core) is finished */
  lock_collector;
  while (phase == ri_core_phase) 
    cond_wait(&ri_end_cv, &collector_lock); 
//  unlock_collector;
//  e = current_utime();
//  if(id == 0)fprintf(stderr, " (%d:%d) ", e - gs[id], id);
}


/* suppose that we have collector lock */
static void do_scan_roots()
{ 
  int tid;
  unsigned int s, e;

    //s = current_utime();
  scan_global_roots();

  /* write barriers get activated */
  /* objects are allocated as black after here */
  gc_phase = PHASE_ROOT_CORE; 
  for (tid = 0; tid < MAXTHREAD; tid++) {
    if (euscontexts[tid]) {
      mutex_lock(&mut_stat_table[tid].lock);
      scan_local_roots(tid);
      //      mut_stat_table[tid].stat = 0x0; /* 'running' */
      mut_stat_table[tid].stat = 
        (mut_stat_table[tid].stat ^ 0x2) & 0xfffffffd; /* 'running' */
      mutex_unlock(&mut_stat_table[tid].lock);
    }
  }
    //e = current_utime();
    //fprintf(stderr, "stopped: %d\n", e - s);

  gc_request_flag = 0;
  ri_core_phase = 1 - ri_core_phase; /* release other mutator threads */
  gc_phase = PHASE_ROOT_REM; 
  cond_broadcast(&ri_end_cv);
  gc_wakeup_cnt++; /* now, we release collector threads */
  cond_broadcast(&gc_wakeup_cv);
  DPRINT2("root scan finished: free rate = %lf", (double)freeheap / totalheap);
}


static void wait_until_next_gc_cycle()
{
  /*
    numunion nu;
    int thr;
    double threshold;
    static long used;

    used += pastfree + newheap + sweepheap - freeheap;
    newheap = 0;
    threshold = max(DEFAULT_GC_THRESHOLD, fltval(speval(GCMARGIN)));
    thr = (int)((double)totalheap * threshold);
    used = freeheap;
    while(freeheap > thr && gc_counter >= gc_request_counter){
    nanosleep(&treq, NULL); // take a rest
    }
    used = used - freeheap;
    pastfree = freeheap;
  */

  /*
    mutex_lock(&gc_state_lock);
    while(gc_counter >= gc_request_counter){
    cond_wait(&wake_up_gc_thread_cv, &gc_state_lock);
    }
    mutex_unlock(&gc_state_lock);
  */
}

//#define myctx (euscontexts[thr_self()])
//static long rgc_marktime, rgc_sweeptime;

static int do_gc_epilogue()
{
  /*
    if (gc_net_free < 0.8) { // hard external fragmentation
      DPRINT1("\x1b[1;31mexpand heap(do_gc_epilogue, free/total=%d/%d)\x1b[0m",
	      freeheap, totalheap);
      newchunk(DEFAULT_EXPAND_SIZE_IDX);
      //do_allocate_heap(totalheap * (0.9 - gc_net_free));
    }
  */
#ifdef __USE_MARK_BITMAP
    clear_bit_table();
#endif
    if (gc_cmp_cnt < gc_wakeup_cnt)
      gc_cmp_cnt++;
    gc_phase = PHASE_NOGC;

    if (debug) {
      fprintf(stderr, " free/total=%d/%d\n",
          freeheap, totalheap);
//      fprintf(stderr, " mark=%d sweep%d\n", rgc_marktime, rgc_sweeptime);
    }
/* GC thread doesn't have its own context.
    if (speval(QGCHOOK) != NIL) {
      pointer gchook=speval(QGCHOOK);
      vpush(makeint(freeheap)); vpush(makeint(totalheap));
      ufuncall(ctx,gchook,gchook,(pointer)(ctx->vsp-2),ctx->bindfp,2);
      ctx->vsp -= 2;
    }
    breakck;
*/

    DPRINT2("GC cycle finished: free rate = %lf", (double)freeheap / totalheap);
    return 0;
}

void do_a_little_gc_work(int m_unit, int s_unit)
{
  unsigned int s, e;
//  s = current_utime();
  switch (gc_phase) {
    case PHASE_ROOT_REM:
#ifdef __RETURN_BARRIER
      scan_remaining_roots();
#endif
      gc_phase = PHASE_MARK;
      break;
    case PHASE_MARK:
      mark_a_little(m_unit);
    break;
    case PHASE_SWEEP:
      sweep_a_little(gcmerge, s_unit);
    break;
    case PHASE_EPILOGUE:
      do_gc_epilogue();
    default: 
      ;
  }
//  e = current_utime();
//  if(e-s > 100) printf("<<%d, %d::%d, %d>>\n", e-s, gc_phase,
//      rgc_credit, marking_state.is_checking_pstack);
}

void collect()
{
  int i;
  unsigned s, e;

#ifdef __PROFILE_GC
  int tmp_free;
  reset_utime(); /* for rdtsc */
#endif

  /* synchronize with mainthread */
  barrier_wait(startup_barrier);

#ifdef __PROFILE_GC
  //  times(&buf1);
#endif

  /* gc main loop */
  for (;;) { 

    /* gc thread waits until the core of root scanning is finished. */
    lock_collector;
    while (gc_cmp_cnt == gc_wakeup_cnt) {
      cond_wait(&gc_wakeup_cv, &collector_lock);
    }

    while (gc_phase != PHASE_NOGC) {
     // printf(".");fflush(stdout);
      do_a_little_gc_work(M_UNIT, S_UNIT);
      unlock_collector;
      //usleep(0);
      lock_collector;
    };
    unlock_collector;
      
#ifdef __PROFILE_GC
    //    times(&buf2);
    //    gctime = buf2.tms_utime+buf2.tms_stime-buf1.tms_utime-buf1.tms_stime;
    //    fprintf(stderr, "gc thread time = %d\n", gctime*1000/HZ);
    //    fprintf(stderr, "freeheap=%d\n", freeheap*4);
    //    tmp_free = freeheap;
#endif

    //    DPRINT3("took %d micro, not gc consump_rate %f", 
    //            e-s, (float)(tmp_free-freeheap)/(e-s));
  }
  
  /* never come here */
}


#ifdef __EAGER_GC_CONTROL
static int change_collector_thread_sched_policy(int t_sect_length)
{
#ifdef __ART_LINUX
  if(art_enter(ART_PRIO_MAX, ART_TASK_PERIODIC, t_sect_length) == -1){
    DPRINT2("collector error: art_enter");
    return -1;    
  }
#else /* LINUX */
#endif
  return 0;
}
static int restore_collector_thread_sched_policy()
{
#ifdef __ART_LINUX
  if(art_exit() == -1){
    DPRINT2("collector error: art_exit");
    return -1;    
  }
#else /* LINUX */
#endif
  return 0;
}
#endif /* __EAGER_GC_CONTROL */

#ifdef __USE_POLLING
void enter_gc_region(int id)
{
  int c, phase;
  mutex_lock(&mut_stat_table[id].lock);    
  //  mut_stat_table[id].stat |= 0x1; /* set 'suspended' flag */
  //  if(mut_stat_table[id].stat & 0x2){ /* 'need_scan'? */
  do {
    c = gc_region_sync;
  } while (cas_int(gc_region_sync, c, c + 1));
  mutex_unlock(&mut_stat_table[id].lock);    

  if (gc_request_flag) {
    phase = ri_core_phase;
    if (gc_point_sync + gc_region_sync == active_mutator_num) {
      lock_collector;
      if (phase == ri_core_phase)
        do_scan_roots(); 
      unlock_collector;
    }
  }
  //  }
}

void exit_gc_region(int id)
{
  int c;
 try_exit:
  mutex_lock(&mut_stat_table[id].lock);
  if (mut_stat_table[id].stat & 0x2 == mut_stat_phase) { /* 'need_scan'? */
    //    mut_stat_table[id].stat = 0x4; /* set 'scanning' and clear 'need_scan' */
    //    mutex_unlock(&mut_stat_table[id].lock);
    //    insert_my_roots();
    mutex_unlock(&mut_stat_table[id].lock);
    sched_yield(); /* this wouldn't go well on ART-Linux */
    goto try_exit;
  } else {
    //    mut_stat_table[id].stat &= 0x0; /* clear 'suspended' flag */
    do {
      c = gc_region_sync;
    } while (cas_int(gc_region_sync, c, c - 1));
    mutex_unlock(&mut_stat_table[id].lock);
  }
}

#endif /* __USE_POLLING */

int ps_sem = 1;
/* initialize data for syncronization */
static void init_sync_data()
{
  int i;
  mutex_init(&pstack_lock, NULL);
  mutex_init(&collector_lock, NULL);
  cond_init(&gc_wakeup_cv, NULL);
  cond_init(&ri_end_cv, NULL);
  mutex_init(&gc_state_lock, NULL);
  for (i = 0; i < MAXTHREAD; i++) {
    mutex_init(&mut_stat_table[i].lock, NULL);
  }
}

/**********************************************************
  mutator interface routines
*********************************************************/

#ifdef __USE_POLLING
void scan_roots()
{
  int c;
  unsigned int e;
  int myid = thr_self();
  int phase = ri_core_phase;

  myid = thr_self();
  //gs[myid] = current_utime();

  do {
    c = gc_point_sync;
  } while (cas_int(gc_point_sync, c, c + 1));

  if (gc_point_sync + gc_region_sync < active_mutator_num) {
    sched_yield(); // nanosleep(0) might be better
  } else {
    lock_collector;
    if (phase == ri_core_phase)
      do_scan_roots(); 
    unlock_collector;
  }
  
  /* wait until root scanning(core) is finished */
  lock_collector;
  while (phase == ri_core_phase)
    cond_wait(&ri_end_cv, &collector_lock);
  unlock_collector;
  //e = current_utime();
  //if(myid == 0)fprintf(stderr, " (%d:%d) ", e-gs[myid], myid);
  
  return;
}

/* function-version polling code */
int check_gc_request()
{
  if (!gc_request_flag) 
    return 0;
  scan_roots();
  return 1;
}
#endif

#ifdef __USE_SIGNAL
/*
 * it is not recommended to use pthreads tools in signal handlers
 * (mutex, conditional variables,...)
 */
void sighandler(int x)
{
  int idx;
  DPRINT2("start root scanning");
  notify_ri_start();
  idx = thr_self();
  scan_local_roots(thr_self());
  barrier_wait(end_ri_barrier);
  DPRINT2("mutators restart");
}
#endif

/* MAXSTACK 65536 */
#define PMAXSTACK (MAXSTACK * 110)
pointer pstack[PMAXSTACK];
volatile pointer *psp = pstack;
pointer *pstacklimit = pstack + PMAXSTACK;
mutex_t pstack_lock;

/***********************************************************
    barrier-synchronization functions
***********************************************************/

barrier_t barrier_init(int n_clients)
{
  barrier_t barrier = (barrier_t)malloc(sizeof(struct barrier_struct));
  if (barrier != NULL) {
    barrier->n_clients = n_clients;
    barrier->n_waiting = 0;
    barrier->phase = 0;
    mutex_init(&barrier->lock, NULL);
    cond_init(&barrier->wait_cv, NULL);
  }
  return barrier;
}

void barrier_reset(barrier_t barrier, int n_clients)
{
  /* called when active_mutator_num was changed */
  /* this implementation is not good */
  barrier->n_clients = n_clients;
}

void barrier_destroy(barrier_t barrier)
{
  mutex_destroy(&barrier->lock);
  cond_destroy(&barrier->wait_cv);
  free(barrier);
}

void barrier_wait(barrier_t barrier)
{
  int my_phase;
  mutex_lock(&barrier->lock);
  my_phase = barrier->phase;
  barrier->n_waiting++;
  if (barrier->n_waiting == barrier->n_clients) {
    barrier->n_waiting = 0;
    barrier->phase = 1 - my_phase;
    cond_broadcast(&barrier->wait_cv);
  }
  while (barrier->phase == my_phase) {
    cond_wait(&barrier->wait_cv, &barrier->lock);
  }
  mutex_unlock(&barrier->lock);
}

/***********************************************************
  other functions
***********************************************************/


unsigned int do_allocate_heap(unsigned int req_words)
{
  int i, k; 
  unsigned int rem_words = req_words;

  while (buddysize[MAXBUDDY-1] <= rem_words) {
    k = newchunk(MAXBUDDY-1);
    rem_words -= buddysize[k];
  }
  for (i = MAXBUDDY - 2; i >= 20/* or DEFAULTCHUNKINDEX */; i--) {
    if (buddysize[i] < rem_words){
      k = newchunk(i);
      rem_words -= buddysize[k];
    }
  }
  return req_words - rem_words;
}

unsigned int allocate_heap()
{ /*
   * k  buddy[k]
   * 22 85971 word 343884 byte
   * 23 139104 word 556416 byte
   * 24 225075 word 900300 byte
   * 25 364179 word 1456716 byte
   * 26 589254 word 2357016 byte
   * 27 953433 word 3813732 byte
   * 28 1542687 word 6170748 byte
   * 29 2496120 word 9984480 byte
   * 30 4038807 word 16155228 byte
   */
    return do_allocate_heap(INITIAL_HEAP_SIZE * 1000);
}

extern long long values[];

pointer RGCCOUNT(register context *ctx, int n, pointer argv[])
{
  ckarg(0);
  return makeint(gc_cmp_cnt);
}

pointer RGC_GCTIME(register context *ctx, int n, pointer argv[])
{
  struct tms buf;
  ckarg(0);
  times(&buf);
  return makeint((buf.tms_utime + buf.tms_stime) * 1000/HZ );
}

#ifdef __PROFILE_GC
pointer RGCALLOCATED(register context *ctx, int n, pointer argv[])
{
  ckarg(0);
  return makeint(allocd_words);
}
#endif

void rgcfunc(register context *ctx, pointer mod)
{
  pointer p = Spevalof(PACKAGE);
  pointer_update(Spevalof(PACKAGE), syspkg);
  defun(ctx, "RGCCOUNT", mod, RGCCOUNT,NULL);
  defun(ctx, "RGCTIME", mod, RGC_GCTIME,NULL);
#ifdef __PROFILE_GC
  defun(ctx, "RGCALLOCATED", mod, RGCALLOCATED,NULL);
#endif
  pointer_update(Spevalof(PACKAGE), p);
}