-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathagenda_BM.c
1487 lines (1375 loc) · 52.1 KB
/
agenda_BM.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
// file agenda_BM.c
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "bismon.h"
#include "agenda_BM.const.h"
#define TI_MAGICNUM_BM 0x67d1
/// thread-local data for work threads
struct threadinfo_stBM
{
unsigned ti_magic;
short ti_rank;
atomic_bool ti_stop;
atomic_bool ti_gc;
pthread_t ti_pthread;
double ti_thstartelapsedtime;
double ti_thstartcputime;
}; /* end struct threadinfo_stBM */
static struct threadinfo_stBM ti_array_BM[MAXNBWORKJOBS_BM + 2];
static pthread_mutex_t ti_agendamtx_BM;
static pthread_cond_t ti_agendacond_BM;
static int ti_nbworkers_BM;
static atomic_bool ti_needgc_BM;
static atomic_int ti_countendedthreads_BM;
static struct hashsetobj_stBM *ti_veryhigh_taskhset_BM;
static struct hashsetobj_stBM *ti_high_taskhset_BM;
static struct hashsetobj_stBM *ti_normal_taskhset_BM;
static struct hashsetobj_stBM *ti_low_taskhset_BM;
static struct hashsetobj_stBM *ti_verylow_taskhset_BM;
// agenda defer data is under ti_agendamtx_BM lock
#define AGD_MAGIC_BM 996723975 /*0x3b68cd07 */
struct agenda_defer_stBM
{
unsigned agd_magic; // always AGD_MAGIC_BM
unsigned agd_nbval;
deferredaftergc_sigBM *agd_rout;
void *agd_data;
struct agenda_defer_stBM *agd_next;
value_tyBM agd_valarr[]; /* useful length is agd_nbval */
};
#define AGD_MAXCOUNT_BM 0x1ffff
static struct agenda_defer_stBM *agd_first_BM;
static struct agenda_defer_stBM *agd_last_BM;
#define POSTPONE_MAGIC_BM 0x18e6b7a9 /*417773481 */
struct agenda_postpone_stBM
{
unsigned agpo_magic; /*always POSTPONE_MAGIC_BM */
struct agenda_postpone_stBM *agpo_next;
struct agenda_postpone_stBM *agpo_prev;
double agpo_timestamp;
value_tyBM agpo_todo; /* closure to apply or selector to send */
value_tyBM agpo_recv; /* reciever, when sending */
value_tyBM agpo_arg1;
value_tyBM agpo_arg2;
value_tyBM agpo_arg3;
}; /* end of agenda_postpone_stBM */
/// under ti_agendamtx_BM mutex lock!!
static struct agenda_postpone_stBM *agpostpone_first_BM;
static struct agenda_postpone_stBM *agpostpone_last_BM;
static void *run_agendaworker_BM (void *);
static void run_agenda_internal_tasklet_BM (objectval_tyBM * obtk,
struct failurelockset_stBM *flh);
static objectval_tyBM *choose_task_internal_agenda_BM (void);
static struct hashsetobj_stBM *maybe_reorganize_taskhshet_agenda_BM //
(struct hashsetobj_stBM *tkhset);
bool
agenda_need_gc_BM (void)
{
return atomic_load (&ti_needgc_BM);
} /* end agenda_need_gc_BM */
void
initialize_agenda_BM (void)
{
pthread_mutex_init (&ti_agendamtx_BM, NULL);
pthread_cond_init (&ti_agendacond_BM, NULL);
} /* end initialize_agenda_BM */
#define GCWAITMILLISECONDS_BM 550
#define TASKWAITMILLISECONDS_BM 300
#define STOPWAITMILLISECONDS_BM 440
#define WORKTHREADSTACKSIZE_BM (10*1024*1024)
void
agenda_wait_gc_BM (void)
{
struct timespec ts = { 0, 0 };
get_realtimespec_delayedms_BM (&ts, GCWAITMILLISECONDS_BM);
NONPRINTF_BM
("agenda_wait_gc_BM tid#%ld before timedwait elapsed %.3f s",
(long) gettid_BM (), elapsedtime_BM ());
pthread_mutex_lock (&ti_agendamtx_BM);
pthread_cond_timedwait (&ti_agendacond_BM, &ti_agendamtx_BM, &ts);
pthread_mutex_unlock (&ti_agendamtx_BM);
} /* end agenda_wait_gc_BM */
// the work routine, passed to pthread_create
void *
run_agendaworker_BM (void *ad)
{
intptr_t tix = (intptr_t) ad;
ASSERT_BM (tix > 0 && tix <= MAXNBWORKJOBS_BM && tix <= ti_nbworkers_BM);
char thnambuf[16];
memset (thnambuf, 0, sizeof (thnambuf));
snprintf (thnambuf, sizeof (thnambuf), "bmwth#%d", (int) tix);
pthread_setname_np (pthread_self (), thnambuf);
curthreadinfo_BM = ti_array_BM + tix;
ASSERT_BM (curthreadinfo_BM->ti_magic == TI_MAGICNUM_BM);
ASSERT_BM (curthreadinfo_BM->ti_rank == (short) tix);
usleep (5 + 3 * tix);
// be sure that clocktime_BM(CLOCK_THREAD_CPUTIME_ID) is positive, so warmup the CPU
{
volatile double x = tix * 0.02 + 0.001;
while (clocktime_BM (CLOCK_THREAD_CPUTIME_ID) <= 0.0 && x < 0.8)
{
// some useless computation
x +=
0.25 * sin (x + tix * 0.0001) + 1.0e-6 + 1.0e-7 * (getpid () % 16);
};
}
usleep (40 + 17 * tix);
long loopcnt = 0;
for (;;)
{
loopcnt++;
objectval_tyBM *taskob = NULL;
if (atomic_load (&curthreadinfo_BM->ti_stop))
break;
/// run the GC if needed
if (atomic_load (&ti_needgc_BM))
{
// wait for GC to terminate
NONPRINTF_BM
("run_agendaworker tix#%d needgc start tid#%ld elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
atomic_store (&curthreadinfo_BM->ti_gc, true);
pthread_cond_broadcast (&ti_agendacond_BM);
do
{
NONPRINTF_BM
("run_agendaworker tix#%d needgc timedwait tid#%ld elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
pthread_mutex_lock (&ti_agendamtx_BM);
{
struct timespec ts = { 0, 0 };
get_realtimespec_delayedms_BM (&ts, GCWAITMILLISECONDS_BM);
pthread_cond_timedwait (&ti_agendacond_BM, &ti_agendamtx_BM,
&ts);
}
pthread_mutex_unlock (&ti_agendamtx_BM);
NONPRINTF_BM
("run_agendaworker tix#%d needgcendloop tid#%ld elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
}
while (atomic_load (&curthreadinfo_BM->ti_gc));
NONPRINTF_BM
("run_agendaworker tix#%d needgc done tid#%ld elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
}
/// choose a task to run
{
pthread_mutex_lock (&ti_agendamtx_BM);
taskob = choose_task_internal_agenda_BM ();
pthread_mutex_unlock (&ti_agendamtx_BM);
NONPRINTF_BM
("run_agendaworker tix%d choose tid#%ld elapsed %.3f s taskob %s",
(int) tix, (long) gettid_BM (), elapsedtime_BM (),
objectdbg_BM (taskob));
if (taskob)
{
long flspace[12];
memset (&flspace, 0, sizeof (flspace));
initialize_failurelockset_BM ((struct failurelockset_stBM *)
&flspace, sizeof (flspace));
curthreadinfo_BM->ti_thstartcputime =
clocktime_BM (CLOCK_THREAD_CPUTIME_ID);
curthreadinfo_BM->ti_thstartelapsedtime =
clocktime_BM (CLOCK_MONOTONIC);
run_agenda_internal_tasklet_BM (taskob,
(struct failurelockset_stBM *)
&flspace);
curthreadinfo_BM->ti_thstartcputime = 0.0;
curthreadinfo_BM->ti_thstartelapsedtime = 0.0;
destroy_failurelockset_BM ((struct failurelockset_stBM *)
&flspace);
NONPRINTF_BM
("run_agendaworker tix%d didrun tid#%ld elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
}
else
{ // no task to run
NONPRINTF_BM
("run_agendaworker tix%d tid#%ld notask elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
pthread_mutex_lock (&ti_agendamtx_BM);
{
struct timespec ts = { 0, 0 };
get_realtimespec_delayedms_BM (&ts, TASKWAITMILLISECONDS_BM);
pthread_cond_timedwait (&ti_agendacond_BM, &ti_agendamtx_BM,
&ts);
}
pthread_mutex_unlock (&ti_agendamtx_BM);
NONPRINTF_BM
("run_agendaworker tix%d tid#%ld notask after elapsed %.3f s",
(int) tix, (long) gettid_BM (), elapsedtime_BM ());
}
}
}; /* end forever */
curthreadinfo_BM = NULL;
atomic_fetch_add (&ti_countendedthreads_BM, 1);
pthread_cond_broadcast (&ti_agendacond_BM);
usleep (1);
return NULL;
} /* end run_agendaworker_BM */
int
agenda_nb_work_jobs_BM (void)
{
return ti_nbworkers_BM;
} /* end agenda_nb_work_jobs_BM */
extern double taskletcputime_BM (void) __attribute__((optimize ("-O3")));
extern double taskletelapsedtime_BM (void) __attribute__((optimize ("-O3")));
double
taskletcputime_BM (void)
{
double ths = 0.0;
if (curthreadinfo_BM && (ths = curthreadinfo_BM->ti_thstartcputime) > 0.0)
return clocktime_BM (CLOCK_THREAD_CPUTIME_ID) - ths;
return NAN;
} // end taskletcputime_BM
double
taskletelapsedtime_BM (void)
{
double ths = 0.0;
if (curthreadinfo_BM
&& (ths = curthreadinfo_BM->ti_thstartelapsedtime) > 0.0)
return clocktime_BM (CLOCK_MONOTONIC) - ths;
return NAN;
} // end taskletelapsedtime_BM
static struct hashsetobj_stBM *
maybe_reorganize_taskhshet_agenda_BM (struct hashsetobj_stBM *hset)
{
ASSERT_BM (valtype_BM (hset) == typayl_hashsetobj_BM);
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
int r = g_random_int ();
if (alsiz > 20 && 3 * ucnt < alsiz && r % 8 == 0)
return hashsetobj_grow_BM (hset, 1);
else if (ucnt == 0 && r % 16 == 1)
return NULL;
else if (4 * ucnt + 10 < 3 * alsiz && r % 8 == 0)
return hashsetobj_grow_BM (hset, ucnt / 4 + 2);
return hset;
} /* end maybe_reorganize_taskhshet_agenda_BM */
objectval_tyBM *
choose_task_internal_agenda_BM (void)
{
// the ti_agendamtx_BM has been locked by caller, run_agendaworker_BM
int r = g_random_int ();
objectval_tyBM *taskob = NULL;
if (!taskob && ti_veryhigh_taskhset_BM)
{
objectval_tyBM *curtaskob =
hashsetobj_take_random_BM (ti_veryhigh_taskhset_BM);
if (curtaskob)
{
ti_veryhigh_taskhset_BM =
maybe_reorganize_taskhshet_agenda_BM (ti_veryhigh_taskhset_BM);
taskob = curtaskob;
}
}
if ((!taskob || r % 32 == 0) && ti_high_taskhset_BM)
{
objectval_tyBM *curtaskob =
hashsetobj_take_random_BM (ti_high_taskhset_BM);
if (curtaskob)
{
ti_high_taskhset_BM =
maybe_reorganize_taskhshet_agenda_BM (ti_high_taskhset_BM);
taskob = curtaskob;
}
}
if ((!taskob || r % 32 == 1) && ti_normal_taskhset_BM)
{
objectval_tyBM *curtaskob =
hashsetobj_take_random_BM (ti_normal_taskhset_BM);
if (curtaskob)
{
ti_normal_taskhset_BM =
maybe_reorganize_taskhshet_agenda_BM (ti_normal_taskhset_BM);
taskob = curtaskob;
}
}
if ((!taskob || r % 32 == 2) && ti_low_taskhset_BM)
{
objectval_tyBM *curtaskob =
hashsetobj_take_random_BM (ti_low_taskhset_BM);
if (curtaskob)
{
ti_low_taskhset_BM =
maybe_reorganize_taskhshet_agenda_BM (ti_low_taskhset_BM);
taskob = curtaskob;
}
}
if ((!taskob || r % 32 == 3) && ti_verylow_taskhset_BM)
{
objectval_tyBM *curtaskob =
hashsetobj_take_random_BM (ti_verylow_taskhset_BM);
if (curtaskob)
{
ti_verylow_taskhset_BM =
maybe_reorganize_taskhshet_agenda_BM (ti_verylow_taskhset_BM);
taskob = curtaskob;
}
}
return taskob;
} /* end choose_task_internal_agenda_BM */
void
gcmarkagenda_BM (struct garbcoll_stBM *gc)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
pthread_mutex_lock (&ti_agendamtx_BM);
if (ti_veryhigh_taskhset_BM)
hashsetgcmark_BM (gc, ti_veryhigh_taskhset_BM, NULL);
if (ti_high_taskhset_BM)
hashsetgcmark_BM (gc, ti_high_taskhset_BM, NULL);
if (ti_normal_taskhset_BM)
hashsetgcmark_BM (gc, ti_normal_taskhset_BM, NULL);
if (ti_low_taskhset_BM)
hashsetgcmark_BM (gc, ti_low_taskhset_BM, NULL);
if (ti_verylow_taskhset_BM)
hashsetgcmark_BM (gc, ti_verylow_taskhset_BM, NULL);
if (agd_first_BM != NULL)
{
ASSERT_BM (agd_last_BM != NULL);
unsigned agdcount = 0;
unsigned agdvalcount = 0;
for (struct agenda_defer_stBM * agd = agd_first_BM; agd != NULL;
agd = agd->agd_next)
{
ASSERT_BM (agd->agd_magic == AGD_MAGIC_BM);
unsigned nbv = agd->agd_nbval;
for (unsigned vix = 0; vix < nbv; vix++)
{
agdvalcount++;
VALUEGCPROC_BM (gc, agd->agd_valarr[vix], 0);
if (agdvalcount > MILLION_BM)
FATAL_BM ("too many agenda defer values %u", agdvalcount);
}
agdcount++;
if (agdcount > AGD_MAXCOUNT_BM)
FATAL_BM ("too many agenda defer %u", agdcount);
}
};
pthread_mutex_unlock (&ti_agendamtx_BM);
} /* end gcmarkagenda_BM */
void
start_agenda_work_threads_BM (int nbjobs)
{
ASSERT_BM (nbjobs >= MINNBWORKJOBS_BM && nbjobs < MAXNBWORKJOBS_BM);
ASSERT_BM (pthread_self () == mainthreadid_BM);
ti_nbworkers_BM = nbjobs;
pthread_attr_t at = { };
pthread_attr_init (&at);
pthread_attr_setdetachstate (&at, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize (&at, WORKTHREADSTACKSIZE_BM);
for (int ix = 1; ix <= nbjobs; ix++)
{
ti_array_BM[ix].ti_magic = TI_MAGICNUM_BM;
ti_array_BM[ix].ti_rank = ix;
atomic_init (&ti_array_BM[ix].ti_stop, false);
atomic_init (&ti_array_BM[ix].ti_gc, false);
ti_array_BM[ix].ti_pthread = (pthread_t) 0;
ti_array_BM[ix].ti_thstartelapsedtime = 0.0;
ti_array_BM[ix].ti_thstartcputime = 0.0;
};
usleep (2000 + 100 * nbjobs);
for (int ix = 1; ix <= nbjobs; ix++)
{
usleep (333);
pthread_create (&ti_array_BM[ix].ti_pthread, &at,
run_agendaworker_BM, (void *) (intptr_t) ix);
}
usleep (100);
pthread_attr_destroy (&at);
} /* end start_agenda_work_threads_BM */
void
stop_agenda_work_threads_BM (void)
{
int nbwth = ti_nbworkers_BM;
NONPRINTF_BM
("stop_agenda_work_threads start nbwth=%d tid#%ld elapsed %.3f s", nbwth,
(long) gettid_BM (), elapsedtime_BM ());
{
pthread_mutex_lock (&ti_agendamtx_BM);
for (int tix = 1; tix <= nbwth; tix++)
atomic_store (&ti_array_BM[tix].ti_stop, true);
pthread_mutex_unlock (&ti_agendamtx_BM);
}
usleep (50);
pthread_cond_broadcast (&ti_agendacond_BM);
usleep (1000);
do
{
usleep (100);
pthread_mutex_lock (&ti_agendamtx_BM);
struct timespec ts = { 0, 0 };
get_realtimespec_delayedms_BM (&ts, STOPWAITMILLISECONDS_BM);
NONPRINTF_BM ("stop_agenda_work_threads tid#%ld", (long) gettid_BM ());
pthread_cond_timedwait (&ti_agendacond_BM, &ti_agendamtx_BM, &ts);
pthread_mutex_unlock (&ti_agendamtx_BM);
}
while (atomic_load (&ti_countendedthreads_BM) < nbwth);
} /* end stop_agenda_work_threads_BM */
// agenda_suspend_for_gc_BM is called from full_garbage_collection_BM
void
agenda_suspend_for_gc_BM (void)
{
ASSERT_BM (curthreadinfo_BM == NULL);
atomic_store (&ti_needgc_BM, true);
pthread_cond_broadcast (&ti_agendacond_BM);
bool alldoinggc = false;
int nbwth = ti_nbworkers_BM;
long loopcnt = 0;
DBGPRINTF_BM ("agenda_suspend_for_gc_BM nwth=%d", nbwth);
while (!alldoinggc)
{
alldoinggc = false;
int wix = 0;
loopcnt++;
DBGPRINTF_BM
("agenda_suspend_for_gc_BM elapsed %.3f s tid%ld loopcnt%ld",
elapsedtime_BM (), (long) gettid_BM (), loopcnt);
pthread_mutex_lock (&ti_agendamtx_BM);
for (int tix = 1; tix <= nbwth && !wix; tix++)
if (!atomic_load (&ti_array_BM[tix].ti_gc))
wix = tix;
pthread_mutex_unlock (&ti_agendamtx_BM);
NONPRINTF_BM
("agenda_suspend_for_gc_BM tid#%ld wix=%d elapsed %.3f s",
(long) gettid_BM (), wix, elapsedtime_BM ());
if (wix > 0)
{
struct timespec ts = { 0, 0 };
get_realtimespec_delayedms_BM (&ts, GCWAITMILLISECONDS_BM);
NONPRINTF_BM
("agenda_suspend_for_gc_BM tid#%ld wix=%d before timedwait elapsed %.3f s",
(long) gettid_BM (), wix, elapsedtime_BM ());
pthread_mutex_lock (&ti_agendamtx_BM);
pthread_cond_timedwait (&ti_agendacond_BM, &ti_agendamtx_BM, &ts);
pthread_mutex_unlock (&ti_agendamtx_BM);
NONPRINTF_BM
("agenda_suspend_for_gc_BM tid#%ld after timedwait elapsed %.3f s",
(long) gettid_BM (), elapsedtime_BM ());
}
else
break;
}
DBGPRINTF_BM ("agenda_suspend_for_gc_BM done");
} /* end agenda_suspend_for_gc_BM */
void
agenda_continue_after_gc_BM (void)
{
ASSERT_BM (curthreadinfo_BM == NULL);
int nbwth = ti_nbworkers_BM;
DBGPRINTF_BM ("agenda_continue_after_gc tid#%ld elapsed %.3f s",
(long) gettid_BM (), elapsedtime_BM ());
atomic_store (&ti_needgc_BM, false);
{
pthread_mutex_lock (&ti_agendamtx_BM);
for (int tix = 1; tix <= nbwth; tix++)
atomic_store (&ti_array_BM[tix].ti_gc, false);
pthread_mutex_unlock (&ti_agendamtx_BM);
}
pthread_cond_broadcast (&ti_agendacond_BM);
atomic_store (&ti_needgc_BM, false);
usleep (5);
DBGPRINTF_BM ("agenda_continue_after_gc end tid#%ld elapsed %.3f s",
(long) gettid_BM (), elapsedtime_BM ());
} /* end agenda_continue_after_gc_BM */
void
agenda_run_deferred_after_gc_BM (void)
{
LOCALFRAME_BM (NULL, NULL, value_tyBM curv);
struct agenda_defer_stBM *oldfirst = NULL;
struct agenda_defer_stBM *oldlast = NULL;
struct failurehandler_stBM *oldflh =
(struct failurehandler_stBM *) curfailurehandle_BM;
curfailurehandle_BM = NULL;
pthread_mutex_lock (&ti_agendamtx_BM);
oldfirst = agd_first_BM;
oldlast = agd_last_BM;
agd_first_BM = NULL;
agd_last_BM = NULL;
pthread_mutex_unlock (&ti_agendamtx_BM);
struct agenda_defer_stBM *nextagd = NULL;
unsigned agdcount = 0;
for (struct agenda_defer_stBM * curagd = oldfirst; curagd != NULL;
curagd = nextagd)
{
ASSERT_BM (curagd->agd_magic == AGD_MAGIC_BM);
nextagd = curagd->agd_next;
ASSERT_BM (nextagd != NULL || curagd == oldlast);
agdcount++;
ASSERT_BM (curagd->agd_rout != NULL);
if (showdebugmsg_BM)
{
Dl_info di = { };
if (dladdr (curagd->agd_rout, &di) && di.dli_sname)
DBGBACKTRACEPRINTF_BM
("agenda_run_deferred_after_gc_BM rout %s @%p nbval#%d",
di.dli_sname, (void *) curagd->agd_rout,
(int) curagd->agd_nbval);
else
DBGBACKTRACEPRINTF_BM
("agenda_run_deferred_after_gc_BM rout@%p nbval#%d",
(void *) curagd->agd_rout, (int) curagd->agd_nbval);
for (int ix = 0; ix < (int) curagd->agd_nbval; ix++)
{
_.curv = curagd->agd_valarr[ix];
#warning perhaps dangerous DBGPRINTF_BM in agenda_run_deferred_after_gc_BM
/// this might not be safe if GC happens inside
DBGPRINTF_BM ("agenda_run_deferred_after_gc..val[%d]: %s",
ix, OUTSTRVALUE_BM (_.curv));
}
}
(*curagd->agd_rout) (curagd->agd_valarr, curagd->agd_nbval,
curagd->agd_data);
DBGPRINTF_BM ("agenda_run_deferred_after_gc done rout@%p\n",
(void *) curagd->agd_rout);
memset (curagd, 0, sizeof (*curagd));
free (curagd), curagd = NULL;
ASSERT_BM (agdcount <= AGD_MAXCOUNT_BM);
}
curfailurehandle_BM = oldflh;
} /* end agenda_run_deferred_after_gc_BM */
void
agenda_defer_after_gc_BM (deferredaftergc_sigBM * rout,
value_tyBM * valarr, unsigned nbval, void *data)
{
ASSERT_BM (rout != NULL);
ASSERT_BM (valarr != NULL || nbval == 0);
ASSERT_BM (nbval < 0x3ffff);
unsigned siz = prime_above_BM (nbval);
struct agenda_defer_stBM *newagd =
malloc (sizeof (struct agenda_defer_stBM) + siz * sizeof (value_tyBM));
if (!newagd)
FATAL_BM ("failed to malloc newagd for %d values", siz);
memset (newagd, 0,
sizeof (struct agenda_defer_stBM) + siz * sizeof (value_tyBM));
newagd->agd_magic = AGD_MAGIC_BM;
newagd->agd_nbval = nbval;
newagd->agd_rout = rout;
newagd->agd_data = data;
if (nbval > 0)
memcpy (newagd->agd_valarr, valarr, nbval * sizeof (value_tyBM));
pthread_mutex_lock (&ti_agendamtx_BM);
if (agd_first_BM == NULL)
{
ASSERT_BM (agd_last_BM == NULL);
agd_first_BM = agd_last_BM = newagd;
}
else
{
ASSERT_BM (agd_last_BM != NULL);
agd_last_BM->agd_next = newagd;
agd_last_BM = newagd;
newagd->agd_next = NULL;
}
pthread_mutex_unlock (&ti_agendamtx_BM);
} /* end agenda_defer_after_gc_BM */
void
agenda_notify_BM (void)
{
pthread_cond_broadcast (&ti_agendacond_BM);
} // end agenda_notify_BM
static bool agenda_internal_remove_tasklet_BM (objectval_tyBM * taskob);
bool
agenda_internal_remove_tasklet_BM (objectval_tyBM * taskob)
{
// caller has locked the ti_agendamtx_BM
if (!isobject_BM ((value_tyBM) taskob))
return false;
#define REMOVEPRIOTASKHSET_BM(Thset) do { \
if (Thset \
&& hashsetobj_contains_BM (Thset, taskob)) { \
Thset = hashsetobj_remove_BM (Thset, taskob); \
return true; \
} \
} while(0)
REMOVEPRIOTASKHSET_BM (ti_veryhigh_taskhset_BM);
REMOVEPRIOTASKHSET_BM (ti_high_taskhset_BM);
REMOVEPRIOTASKHSET_BM (ti_normal_taskhset_BM);
REMOVEPRIOTASKHSET_BM (ti_low_taskhset_BM);
REMOVEPRIOTASKHSET_BM (ti_verylow_taskhset_BM);
#undef REMOVEPRIOTASKHSET_BM
return false;
} /* end agenda_internal_remove_tasklet_BM */
void
agenda_add_very_high_priority_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return;
pthread_mutex_lock (&ti_agendamtx_BM);
agenda_internal_remove_tasklet_BM (taskob);
ti_veryhigh_taskhset_BM =
hashsetobj_add_BM (ti_veryhigh_taskhset_BM, taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
} /* end agenda_add_very_high_priority_tasklet_BM */
void
agenda_add_high_priority_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return;
pthread_mutex_lock (&ti_agendamtx_BM);
agenda_internal_remove_tasklet_BM (taskob);
ti_high_taskhset_BM = hashsetobj_add_BM (ti_high_taskhset_BM, taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
} /* end agenda_add_high_priority_tasklet_BM */
void
agenda_add_normal_priority_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return;
pthread_mutex_lock (&ti_agendamtx_BM);
agenda_internal_remove_tasklet_BM (taskob);
ti_normal_taskhset_BM = hashsetobj_add_BM (ti_normal_taskhset_BM, taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
} /* end agenda_add_normal_priority_tasklet_BM */
void
agenda_add_low_priority_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return;
pthread_mutex_lock (&ti_agendamtx_BM);
agenda_internal_remove_tasklet_BM (taskob);
ti_low_taskhset_BM = hashsetobj_add_BM (ti_low_taskhset_BM, taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
} /* end agenda_add_low_priority_tasklet_BM */
void
agenda_add_very_low_priority_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return;
pthread_mutex_lock (&ti_agendamtx_BM);
agenda_internal_remove_tasklet_BM (taskob);
ti_verylow_taskhset_BM = hashsetobj_add_BM (ti_verylow_taskhset_BM, taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
} /* end agenda_add_low_priority_tasklet_BM */
bool
agenda_remove_tasklet_BM (objectval_tyBM * taskob)
{
if (!isobject_BM ((value_tyBM) taskob))
return false;
bool r = false;
pthread_mutex_lock (&ti_agendamtx_BM);
r = agenda_internal_remove_tasklet_BM (taskob);
pthread_mutex_unlock (&ti_agendamtx_BM);
pthread_cond_broadcast (&ti_agendacond_BM);
return r;
} /* end agenda_remove_tasklet_BM */
long
agenda_get_counts_BM (long *pveryhigh, long *phigh, long *pnormal, long *plow,
long *pverylow)
{
long totcnt = 0;
pthread_mutex_lock (&ti_agendamtx_BM);
{
long veryhighcnt = hashsetobj_cardinal_BM (ti_veryhigh_taskhset_BM);
if (pveryhigh)
*pveryhigh = veryhighcnt;
totcnt += veryhighcnt;
}
{
long highcnt = hashsetobj_cardinal_BM (ti_high_taskhset_BM);
if (phigh)
*phigh = highcnt;
totcnt += highcnt;
}
{
long normalcnt = hashsetobj_cardinal_BM (ti_normal_taskhset_BM);
if (pnormal)
*pnormal = normalcnt;
totcnt += normalcnt;
}
{
long lowcnt = hashsetobj_cardinal_BM (ti_low_taskhset_BM);
if (plow)
*plow = lowcnt;
totcnt += lowcnt;
}
{
long verylowcnt = hashsetobj_cardinal_BM (ti_verylow_taskhset_BM);
if (pverylow)
*pverylow = verylowcnt;
totcnt += verylowcnt;
}
pthread_mutex_unlock (&ti_agendamtx_BM);
return totcnt;
} /* end agenda_get_counts_BM */
long
agenda_get_sets_BM (value_tyBM * pveryhighset,
value_tyBM * phighset, value_tyBM * pnormalset,
value_tyBM * plowset, value_tyBM * pverylowset)
{
long totcnt = 0;
pthread_mutex_lock (&ti_agendamtx_BM);
{
long veryhighcnt = hashsetobj_cardinal_BM (ti_veryhigh_taskhset_BM);
if (pveryhighset)
*pveryhighset =
(value_tyBM) hashsetobj_to_set_BM (ti_veryhigh_taskhset_BM);;
totcnt += veryhighcnt;
}
{
long highcnt = hashsetobj_cardinal_BM (ti_high_taskhset_BM);
if (phighset)
*phighset = (value_tyBM) hashsetobj_to_set_BM (ti_high_taskhset_BM);
totcnt += highcnt;
}
{
long normalcnt = hashsetobj_cardinal_BM (ti_normal_taskhset_BM);
if (pnormalset)
*pnormalset = (value_tyBM) hashsetobj_to_set_BM (ti_normal_taskhset_BM);
totcnt += normalcnt;
}
{
long lowcnt = hashsetobj_cardinal_BM (ti_low_taskhset_BM);
if (plowset)
*plowset = (value_tyBM) hashsetobj_to_set_BM (ti_low_taskhset_BM);
totcnt += lowcnt;
}
{
long verylowcnt = hashsetobj_cardinal_BM (ti_verylow_taskhset_BM);
if (pverylowset)
*pverylowset =
(value_tyBM) hashsetobj_to_set_BM (ti_verylow_taskhset_BM);
totcnt += verylowcnt;
}
pthread_mutex_unlock (&ti_agendamtx_BM);
return totcnt;
} /* end agenda_get_sets_BM */
void
run_agenda_internal_tasklet_BM (objectval_tyBM * obtk,
struct failurelockset_stBM *flh)
{
if (!isobject_BM (obtk)) // should never happen
FATAL_BM ("bad tasklet object @%p", obtk);
ASSERT_BM (flh != NULL);
LOCALFRAME_BM ( /*prev: */ NULL, /*descr: */ NULL,
objectval_tyBM * obtk;
value_tyBM failres;);
_.obtk = obtk;
curfailurehandle_BM = NULL;
objlock_BM (_.obtk);
volatile int failcod = 0;
struct failurehandler_stBM fh = //
{
.pA = {.htyp = typayl_FailureHandler_BM},
.failh_magic = FAILUREHANDLEMAGIC_BM,
.failh_lockset = NULL,
.failh_reason = NULL,
.failh_jmpbuf = {}
};
fh.failh_lockset = flh;
curfailurehandle_BM = &fh;
failcod = setjmp (fh.failh_jmpbuf);
if (!failcod)
{
(void) send0_BM (_.obtk, BMP_run_tasklet, CURFRAME_BM);
}
else
{
char curidbuf[32];
memset (curidbuf, 0, sizeof (curidbuf));
idtocbuf32_BM (objid_BM (_.obtk), curidbuf);
_.failres = fh.failh_reason;
const char *failmsg = debug_outstr_value_BM (_.failres, CURFRAME_BM,
0);
WARNPRINTF_BM ("tasklet %s failed code#%d reason %s\n", curidbuf,
failcod, failmsg);
};
curfailurehandle_BM = NULL;
objunlock_BM (_.obtk);
} /* end run_agenda_internal_tasklet_BM */
void
defer_module_dynload_BM (objectval_tyBM * modulobarg, const closure_tyBM * postclosarg, //
value_tyBM arg1, value_tyBM arg2, value_tyBM arg3, //
struct stackframe_stBM *stkf)
{
objectval_tyBM *k_defer_module_dynload = BMK_4fBgmgx2KrN_3PCZfdnI1CJ;
objectval_tyBM *k_plain_temporary_module = BMK_1oEp0eAAyFN_4lsobepyr1T;
LOCALFRAME_BM (stkf, /*descr: */ k_defer_module_dynload,
objectval_tyBM * modulob; //
const closure_tyBM * postclos;
value_tyBM arg1v; //
value_tyBM arg2v; //
value_tyBM arg3v; //
objectval_tyBM * curob; //
objectval_tyBM * routob; //
value_tyBM causev;
value_tyBM errorv;
);
extern void deferred_do_module_dynload_BM (value_tyBM * valarr, unsigned nbval, void *data); /* in misc_BM.cc */
_.modulob = objectcast_BM (modulobarg);
_.postclos = closurecast_BM ((value_tyBM) postclosarg);
_.arg1v = arg1;
_.arg2v = arg2;
_.arg3v = arg3;
int failin = -1;
#define FAILHERE(Cause) do { failin = __LINE__ ; _.causev = (value_tyBM)(Cause); goto failure; } while(0)
if (!_.modulob)
FAILHERE (BMP_load_module);
if (!isclosure_BM ((value_tyBM) _.postclos))
FAILHERE (BMP_closure);
bool modulistemporary =
(objectisinstance_BM (_.modulob, k_plain_temporary_module));
char modulidbuf[32];
memset (modulidbuf, 0, sizeof (modulidbuf));
idtocbuf32_BM (objid_BM (_.modulob), modulidbuf);
DBGBACKTRACEPRINTF_BM
("defer_module_dynload_BM start modulob %s %s postclos %s arg1 %s arg2 %s arg3 %s",
objectdbg_BM (_.modulob), modulistemporary ? "temporary" : "persistent",
OUTSTRVALUE_BM ((value_tyBM) _.postclos),
OUTSTRVALUE_BM (_.arg1v),
OUTSTRVALUE_BM (_.arg2v), OUTSTRVALUE_BM (_.arg3v));
// test that the module file exists
char *progmodulpath = NULL; // the asprintf-ed binary module in bismon_directory
char *curmodulpath = NULL; // the asprintf-ed binary module in current directory
char *modulpath = NULL;
{
int a = -1;
if (modulistemporary)
a = asprintf (&progmodulpath,
"%s/" MODULEBINDIR_BM "/" TEMPMODULEPREFIX_BM "%s.so",
bismon_directory, modulidbuf);
else
a =
asprintf (&progmodulpath,
"%s/" MODULEBINDIR_BM "/" MODULEPREFIX_BM "%s.so",
bismon_directory, modulidbuf);
if (a < 0 || !progmodulpath)
FATAL_BM ("failed to make progmodulpath for %s", modulidbuf);
}
{
int a = -1;
if (modulistemporary)
a = asprintf (&curmodulpath,
MODULEBINDIR_BM "/" TEMPMODULEPREFIX_BM "%s.so",
modulidbuf);
else
a =
asprintf (&curmodulpath,
MODULEBINDIR_BM "/" MODULEPREFIX_BM "%s.so", modulidbuf);
if (a < 0 || !curmodulpath)
FATAL_BM ("failed to make curmodulpath for %s", modulidbuf);
}
if (!access (curmodulpath, F_OK))
{
modulpath = curmodulpath;
free (progmodulpath), progmodulpath = NULL;
}
else if (!access (progmodulpath, F_OK))
{
modulpath = progmodulpath;
free (curmodulpath), curmodulpath = NULL;
};
FILE *binmodf = modulpath ? fopen (modulpath, "r") : NULL;
if (!binmodf)
{
WARNPRINTF_BM ("failed to read binary %s module %s for %s: %m\n",
modulpath ? : "*missing*",
modulistemporary ? "temporary" : "persistent",
objectdbg_BM (_.modulob));
FAILHERE (makenode1_BM
(BMP_load_module, (value_tyBM) makestring_BM (modulpath)));
}
char eident[EI_NIDENT];
memset (eident, 0, sizeof (eident));
int nbread = fread (eident, EI_NIDENT, 1, binmodf);
if (nbread < 1
|| eident[0] != ELFMAG0 || eident[1] != ELFMAG1 || eident[2] != ELFMAG2
|| eident[3] != ELFMAG3)
{
DBGPRINTF_BM ("defer_module_dynload bad ELF ident %x %x %x %x (want %x %x %x %x) nbread %d for modulpath %s modulob %s", //
eident[0], eident[1], eident[2], eident[3], //
ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, //
nbread, modulpath, objectdbg_BM (_.modulob));
WARNPRINTF_BM ("module binary %s for %s is not ELF.\n",
modulpath, objectdbg_BM (_.modulob));
fclose (binmodf);
FAILHERE (makenode1_BM (BMP_load_module,
(value_tyBM) makestring_BM (modulpath)));
};
fclose (binmodf);
{
value_tyBM varr[8] = { };
varr[0] = _.modulob;
varr[1] = (value_tyBM) _.postclos;
varr[2] = _.arg1v;
varr[3] = _.arg2v;
varr[4] = _.arg3v;