Skip to content

Commit 75ba57a

Browse files
authoredFeb 24, 2025
Name the explicit constants used with caml_verb_gc (runtime5) (#3584)
Name the explicit constants used with caml_verb_gc
1 parent 5da76f5 commit 75ba57a

File tree

20 files changed

+190
-140
lines changed

20 files changed

+190
-140
lines changed
 

‎runtime/caml/misc.h

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,19 +503,70 @@ CAMLextern int caml_read_directory(char_os * dirname,
503503

504504
#ifdef CAML_INTERNALS
505505

506-
/* GC flags and messages */
506+
/* runtime message flags. Settable with v= in OCAMLRUNPARAM */
507507

508508
extern atomic_uintnat caml_verb_gc;
509509

510-
void caml_gc_log (char *, ...)
511-
#ifdef __GNUC__
512-
__attribute__ ((format (printf, 1, 2)))
510+
/* Bits which may be set in caml_verb_gc. The quotations are from the
511+
* OCaml manual. */
512+
513+
/* "Start and end of major GC cycle" (unused) */
514+
#define CAML_GC_MSG_MAJOR 0x0001
515+
/* "Minor collection and major GC slice" (unused) */
516+
#define CAML_GC_MSG_MINOR 0x0002
517+
/* "Growing and shrinking of the heap" */
518+
#define CAML_GC_MSG_HEAPSIZE 0x0004
519+
/* "Resizing of stacks and memory manager tables" */
520+
#define CAML_GC_MSG_STACKSIZE 0x0008
521+
/* "Heap compaction" (unused) */
522+
#define CAML_GC_MSG_COMPACT 0x0010
523+
/* "Change of GC parameters" */
524+
#define CAML_GC_MSG_PARAMS 0x0020
525+
/* "Computation of major GC slice size" */
526+
#define CAML_GC_MSG_SLICESIZE 0x0040
527+
/* "Calling of finalization functions" */
528+
#define CAML_GC_MSG_FINALIZE 0x0080
529+
/* "Startup messages" */
530+
#define CAML_GC_MSG_STARTUP 0x0100
531+
/* "Computation of compaction-triggering condition" (unused) */
532+
#define CAML_GC_MSG_COMPACT_TRIGGER 0x0200
533+
/* "Output GC statistics at program exit" */
534+
#define CAML_GC_MSG_STATS 0x0400
535+
/* "GC debugging messages */
536+
#define CAML_GC_MSG_DEBUG 0x0800
537+
/* "Address space reservation changes" */
538+
#define CAML_GC_MSG_ADDRSPACE 0x1000
539+
540+
/* Default set of messages when runtime invoked with -v */
541+
542+
#define CAML_GC_MSG_VERBOSE (CAML_GC_MSG_MAJOR | \
543+
CAML_GC_MSG_HEAPSIZE | \
544+
CAML_GC_MSG_STACKSIZE | \
545+
CAML_GC_MSG_COMPACT | \
546+
CAML_GC_MSG_PARAMS)
547+
548+
/* Use to control messages which should be output at any non-zero verbosity */
549+
550+
#define CAML_GC_MSG_ANY (-1)
551+
552+
/* output message if caml_verb_gc includes any bits in `category`. */
553+
554+
void caml_gc_message (int category, const char *, ...)
555+
#if __has_attribute(format) || defined(__GNUC__)
556+
__attribute__ ((format (printf, 2, 3)))
513557
#endif
514558
;
515559

516-
void caml_gc_message (int, char *, ...)
517-
#ifdef __GNUC__
518-
__attribute__ ((format (printf, 2, 3)))
560+
/* Short-hand for calls to `caml_gc_message` */
561+
562+
#define CAML_GC_MESSAGE(category, ...) \
563+
caml_gc_message(CAML_GC_MSG_ ## category, __VA_ARGS__)
564+
565+
/* Output message if CAML_GC_MSG_DEBUG is set */
566+
567+
void caml_gc_log (const char *, ...)
568+
#if __has_attribute(format) || defined(__GNUC__)
569+
__attribute__ ((format (printf, 1, 2)))
519570
#endif
520571
;
521572

‎runtime/compare.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void compare_free_stack(struct compare_stack* stk)
5151
/* Same, then raise Out_of_memory */
5252
CAMLnoret static void compare_stack_overflow(struct compare_stack* stk)
5353
{
54-
caml_gc_message (0x04, "Stack overflow in structural comparison\n");
54+
CAML_GC_MESSAGE(HEAPSIZE, "Stack overflow in structural comparison\n");
5555
compare_free_stack(stk);
5656
caml_raise_out_of_memory();
5757
}

‎runtime/dynlink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void open_shared_lib(char_os * name)
144144

145145
realname = caml_search_dll_in_path(&caml_shared_libs_path, name);
146146
u8 = caml_stat_strdup_of_os(realname);
147-
caml_gc_message(0x100, "Loading shared library %s\n", u8);
147+
CAML_GC_MESSAGE(STARTUP, "Loading shared library %s\n", u8);
148148
caml_stat_free(u8);
149149
caml_enter_blocking_section();
150150
handle = caml_dlopen(realname, 1);
@@ -311,7 +311,7 @@ CAMLprim value caml_dynlink_open_lib(value mode, value filename)
311311
value result;
312312
char_os * p;
313313

314-
caml_gc_message(0x100, "Opening shared library %s\n",
314+
CAML_GC_MESSAGE(STARTUP, "Opening shared library %s\n",
315315
String_val(filename));
316316
p = caml_stat_strdup_to_os(String_val(filename));
317317
caml_enter_blocking_section();

‎runtime/extern.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ static void extern_failwith(struct caml_extern_state* s, char *msg)
470470

471471
static void extern_stack_overflow(struct caml_extern_state* s)
472472
{
473-
caml_gc_message (0x04, "Stack overflow in marshaling value\n");
473+
CAML_GC_MESSAGE(HEAPSIZE,
474+
"Stack overflow in marshaling value\n");
474475
free_extern_output(s);
475476
caml_raise_out_of_memory();
476477
}

‎runtime/finalise.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ value caml_final_do_calls_exn(void)
151151
if (fi->running_finalisation_function) return Val_unit;
152152
if (fi->todo_head != NULL) {
153153
call_timing_hook(&caml_finalise_begin_hook);
154-
caml_gc_message (0x80, "Calling finalisation functions.\n");
154+
CAML_GC_MESSAGE(FINALIZE,
155+
"Calling finalisation functions.\n");
155156
while (1) {
156157
while (fi->todo_head != NULL && fi->todo_head->size == 0) {
157158
struct final_todo *next_head = fi->todo_head->next;
@@ -168,7 +169,8 @@ value caml_final_do_calls_exn(void)
168169
fi->running_finalisation_function = 0;
169170
if (Is_exception_result(res)) return res;
170171
}
171-
caml_gc_message (0x80, "Done calling finalisation functions.\n");
172+
CAML_GC_MESSAGE(FINALIZE,
173+
"Done calling finalisation functions.\n");
172174
call_timing_hook(&caml_finalise_end_hook);
173175
}
174176
return Val_unit;

‎runtime/gc_ctrl.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,15 @@ CAMLprim value caml_gc_set(value v)
187187

188188
if (newpf != caml_percent_free){
189189
caml_percent_free = newpf;
190-
caml_gc_message (0x20, "New space overhead: %"
191-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_free);
190+
CAML_GC_MESSAGE(PARAMS,
191+
"New space overhead: %" ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
192+
caml_percent_free);
192193
}
193194

194195
if (newpm != caml_max_percent_free) {
195196
caml_max_percent_free = newpm;
196-
caml_gc_message (0x20, "New max space overhead: %"
197-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_max_percent_free);
197+
CAML_GC_MESSAGE(PARAMS, "New max space overhead: %"
198+
ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_max_percent_free);
198199
}
199200

200201
atomic_store_relaxed(&caml_verb_gc, new_verb_gc);
@@ -203,29 +204,29 @@ CAMLprim value caml_gc_set(value v)
203204
if (Wosize_val (v) >= 11){
204205
if (new_custom_maj != caml_custom_major_ratio){
205206
caml_custom_major_ratio = new_custom_maj;
206-
caml_gc_message (0x20, "New custom major ratio: %"
207-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
208-
caml_custom_major_ratio);
207+
CAML_GC_MESSAGE(PARAMS, "New custom major ratio: %"
208+
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
209+
caml_custom_major_ratio);
209210
}
210211
if (new_custom_min != caml_custom_minor_ratio){
211212
caml_custom_minor_ratio = new_custom_min;
212-
caml_gc_message (0x20, "New custom minor ratio: %"
213-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
214-
caml_custom_minor_ratio);
213+
CAML_GC_MESSAGE(PARAMS, "New custom minor ratio: %"
214+
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
215+
caml_custom_minor_ratio);
215216
}
216217
if (new_custom_sz != caml_custom_minor_max_bsz){
217218
caml_custom_minor_max_bsz = new_custom_sz;
218-
caml_gc_message (0x20, "New custom minor size limit: %"
219-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
220-
caml_custom_minor_max_bsz);
219+
CAML_GC_MESSAGE(PARAMS, "New custom minor size limit: %"
220+
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
221+
caml_custom_minor_max_bsz);
221222
}
222223
}
223224

224225
/* Minor heap size comes last because it will trigger a minor collection
225226
(thus invalidating [v]) and it can raise [Out_of_memory]. */
226227
if (newminwsz != Caml_state->minor_heap_wsz) {
227-
caml_gc_message (0x20, "New minor heap size: %"
228-
ARCH_INTNAT_PRINTF_FORMAT "uk words\n", newminwsz / 1024);
228+
CAML_GC_MESSAGE(PARAMS, "New minor heap size: %"
229+
ARCH_INTNAT_PRINTF_FORMAT "uk words\n", newminwsz / 1024);
229230
}
230231

231232
if (newminwsz > caml_minor_heap_max_wsz) {
@@ -388,25 +389,6 @@ void caml_init_gc (void)
388389
caml_percent_free = norm_pfree (percent_fr);
389390
caml_max_percent_free = norm_pmax (percent_m);
390391
caml_init_major_heap (major_heap_size);
391-
caml_gc_message (0x20, "Initial minor heap size: %luk bytes\n",
392-
Caml_state->minor_heap_size / 1024);
393-
caml_gc_message (0x20, "Initial major heap size: %luk bytes\n",
394-
major_heap_size / 1024);
395-
caml_gc_message (0x20, "Initial space overhead: %"
396-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_percent_free);
397-
caml_gc_message (0x20, "Initial max overhead: %"
398-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n", caml_max_percent_free);
399-
if (caml_major_heap_increment > 1000){
400-
caml_gc_message (0x20, "Initial heap increment: %"
401-
ARCH_INTNAT_PRINTF_FORMAT "uk words\n",
402-
caml_major_heap_increment / 1024);
403-
}else{
404-
caml_gc_message (0x20, "Initial heap increment: %"
405-
ARCH_INTNAT_PRINTF_FORMAT "u%%\n",
406-
caml_major_heap_increment);
407-
}
408-
caml_gc_message (0x20, "Initial allocation policy: %d\n",
409-
caml_allocation_policy);
410392
*/
411393
}
412394

‎runtime/intern.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void readfloats(struct caml_intern_state* s,
337337

338338
CAMLnoret static void intern_stack_overflow(struct caml_intern_state* s)
339339
{
340-
caml_gc_message (0x04, "Stack overflow in un-marshaling value\n");
340+
CAML_GC_MESSAGE(HEAPSIZE, "Stack overflow in un-marshaling value\n");
341341
intern_cleanup(s);
342342
caml_raise_out_of_memory();
343343
}

‎runtime/major_gc.c

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -702,36 +702,36 @@ static void update_major_slice_work(intnat howmuch,
702702

703703
extra_work = (intnat) (my_extra_count * (double) total_cycle_work);
704704

705-
caml_gc_message (0x40, "heap_words = %"
706-
ARCH_INTNAT_PRINTF_FORMAT "u\n",
707-
(uintnat)heap_words);
708-
caml_gc_message (0x40, "allocated_words = %"
709-
ARCH_INTNAT_PRINTF_FORMAT "u\n",
705+
CAML_GC_MESSAGE(SLICESIZE,
706+
"heap_words = %" ARCH_INTNAT_PRINTF_FORMAT "u\n",
707+
(uintnat)heap_words);
708+
CAML_GC_MESSAGE(SLICESIZE,
709+
"allocated_words = %" ARCH_INTNAT_PRINTF_FORMAT "u\n",
710710
my_alloc_count);
711-
caml_gc_message (0x40, "allocated_words_direct = %"
712-
ARCH_INTNAT_PRINTF_FORMAT "u\n",
711+
CAML_GC_MESSAGE(SLICESIZE,
712+
"allocated_words_direct = %" ARCH_INTNAT_PRINTF_FORMAT "u\n",
713713
my_alloc_direct_count);
714-
caml_gc_message (0x40, "alloc work-to-do = %"
715-
ARCH_INTNAT_PRINTF_FORMAT "d\n",
714+
CAML_GC_MESSAGE(SLICESIZE,
715+
"alloc work-to-do = %" ARCH_INTNAT_PRINTF_FORMAT "d\n",
716716
alloc_work);
717-
caml_gc_message (0x40, "dependent_words = %"
718-
ARCH_INTNAT_PRINTF_FORMAT "u\n",
717+
CAML_GC_MESSAGE(SLICESIZE,
718+
"dependent_words = %" ARCH_INTNAT_PRINTF_FORMAT "u\n",
719719
my_dependent_count);
720-
caml_gc_message (0x40, "dependent work-to-do = %"
721-
ARCH_INTNAT_PRINTF_FORMAT "d\n",
722-
dependent_work);
723-
caml_gc_message (0x40, "extra_heap_resources = %"
724-
ARCH_INTNAT_PRINTF_FORMAT "uu\n",
725-
(uintnat) (my_extra_count * 1000000));
726-
caml_gc_message (0x40, "extra work-to-do = %"
727-
ARCH_INTNAT_PRINTF_FORMAT "d\n",
728-
extra_work);
720+
CAML_GC_MESSAGE(SLICESIZE,
721+
"dependent work-to-do = %" ARCH_INTNAT_PRINTF_FORMAT "d\n",
722+
dependent_work);
723+
CAML_GC_MESSAGE(SLICESIZE,
724+
"extra_heap_resources = %" ARCH_INTNAT_PRINTF_FORMAT "uu\n",
725+
(uintnat) (my_extra_count * 1000000));
726+
CAML_GC_MESSAGE(SLICESIZE,
727+
"extra work-to-do = %" ARCH_INTNAT_PRINTF_FORMAT "d\n",
728+
extra_work);
729729

730730
intnat offheap_work = max2 (dependent_work, extra_work);
731731
intnat clamp = alloc_work * caml_custom_work_max_multiplier;
732732
if (offheap_work > clamp) {
733-
caml_gc_message(0x40, "Work clamped to %"
734-
ARCH_INTNAT_PRINTF_FORMAT "d\n",
733+
CAML_GC_MESSAGE(SLICESIZE, "Work clamped to %"
734+
ARCH_INTNAT_PRINTF_FORMAT "d\n",
735735
clamp);
736736
offheap_work = clamp;
737737
}
@@ -1456,21 +1456,21 @@ static bool should_compact_from_stw_single(int compaction_mode)
14561456
if (compaction_mode == Compaction_none) {
14571457
return false;
14581458
} else if (compaction_mode == Compaction_forced) {
1459-
caml_gc_message (0x200, "Forced compaction.\n");
1459+
CAML_GC_MESSAGE (COMPACT_TRIGGER, "Forced compaction.\n");
14601460
return true;
14611461
}
14621462
CAMLassert (compaction_mode == Compaction_auto);
14631463

14641464
/* runtime 4 algorithm, as close as possible.
14651465
* TODO: revisit this in future. */
14661466
if (caml_max_percent_free >= 1000 * 1000) {
1467-
caml_gc_message (0x200,
1467+
CAML_GC_MESSAGE (COMPACT_TRIGGER,
14681468
"Max percent free %"ARCH_INTNAT_PRINTF_FORMAT"u%%:"
14691469
"compaction off.\n", caml_max_percent_free);
14701470
return false;
14711471
}
14721472
if (caml_major_cycles_completed < 3) {
1473-
caml_gc_message (0x200,
1473+
CAML_GC_MESSAGE (COMPACT_TRIGGER,
14741474
"Only %"ARCH_INTNAT_PRINTF_FORMAT"u major cycles: "
14751475
"compaction off.\n", caml_major_cycles_completed);
14761476
return false;
@@ -1489,7 +1489,7 @@ static bool should_compact_from_stw_single(int compaction_mode)
14891489
double current_overhead = 100.0 * free_words / live_words;
14901490

14911491
bool compacting = current_overhead >= caml_max_percent_free;
1492-
caml_gc_message (0x200, "Current overhead: %"
1492+
CAML_GC_MESSAGE (COMPACT_TRIGGER, "Current overhead: %"
14931493
ARCH_INTNAT_PRINTF_FORMAT "u%% %s %"
14941494
ARCH_INTNAT_PRINTF_FORMAT "u%%: %scompacting.\n",
14951495
(uintnat) current_overhead,
@@ -1511,9 +1511,9 @@ static void cycle_major_heap_from_stw_single(
15111511
(long unsigned int)caml_major_cycles_completed);
15121512

15131513
caml_major_cycles_completed++;
1514-
caml_gc_message(0x40, "Starting major GC cycle\n");
1514+
CAML_GC_MESSAGE(SLICESIZE, "Starting major GC cycle\n");
15151515

1516-
if (atomic_load_relaxed(&caml_verb_gc) & 0x400) {
1516+
if (atomic_load_relaxed(&caml_verb_gc) & CAML_GC_MSG_STATS) {
15171517
struct gc_stats s;
15181518
intnat heap_words, not_garbage_words, swept_words;
15191519

@@ -1789,8 +1789,9 @@ static void major_collection_slice(intnat howmuch,
17891789
/* Opportunistic slices may run concurrently with gc phase updates. */
17901790
int may_access_gc_phase = (mode != Slice_opportunistic);
17911791

1792-
bool log_events = (mode != Slice_opportunistic ||
1793-
(atomic_load_relaxed(&caml_verb_gc) & 0x40));
1792+
bool log_events = mode != Slice_opportunistic ||
1793+
(atomic_load_relaxed(&caml_verb_gc) &
1794+
CAML_GC_MSG_SLICESIZE);
17941795

17951796
update_major_slice_work(howmuch, may_access_gc_phase, log_events);
17961797

‎runtime/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ void caml_local_realloc(void)
590590
for (i = s->saved_sp; i < 0; i += sizeof(value)) {
591591
*((header_t*)(arena + s->next_length + i)) = Local_uninit_hd;
592592
}
593-
caml_gc_message(0x08,
593+
CAML_GC_MESSAGE(STACKSIZE,
594594
"Growing local stack to %"ARCH_INTNAT_PRINTF_FORMAT"d kB\n",
595595
s->next_length / 1024);
596596
s->count++;

‎runtime/meta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ CAMLprim value caml_realloc_global(value size)
150150
actual_size = Wosize_val(old_global_data);
151151
if (requested_size >= actual_size) {
152152
requested_size = (requested_size + 0x100) & 0xFFFFFF00;
153-
caml_gc_message (0x08, "Growing global data to %"
153+
CAML_GC_MESSAGE(STACKSIZE, "Growing global data to %"
154154
ARCH_INTNAT_PRINTF_FORMAT "u entries\n",
155155
requested_size);
156156
new_global_data = caml_alloc_shr(requested_size, 0);

0 commit comments

Comments
 (0)