Add flag to report GC collections

This commit is contained in:
K Lange 2021-03-24 21:23:15 +09:00
parent 5c2de206b9
commit dbe6ae13d9
3 changed files with 9 additions and 1 deletions

View File

@ -742,7 +742,7 @@ int main(int argc, char * argv[]) {
int flags = 0;
int moduleAsMain = 0;
int opt;
while ((opt = getopt(argc, argv, "+c:C:dgm:rstTMSV-:")) != -1) {
while ((opt = getopt(argc, argv, "+c:C:dgGm:rstTMSV-:")) != -1) {
switch (opt) {
case 'c':
return runString(argv, flags, optarg);
@ -754,6 +754,9 @@ int main(int argc, char * argv[]) {
/* Always garbage collect during an allocation. */
flags |= KRK_GLOBAL_ENABLE_STRESS_GC;
break;
case 'G':
flags |= KRK_GLOBAL_REPORT_GC_COLLECTS;
break;
case 's':
/* Print debug information during compilation. */
flags |= KRK_THREAD_ENABLE_SCAN_TRACING;
@ -793,6 +796,7 @@ int main(int argc, char * argv[]) {
"Interpreter options:\n"
" -d Debug output from the bytecode compiler.\n"
" -g Collect garbage on every allocation.\n"
" -G Report GC collections.\n"
" -m mod Run a module as a script.\n"
" -r Disable complex line editing in the REPL.\n"
" -s Debug output from the scanner/tokenizer.\n"

View File

@ -237,6 +237,7 @@ typedef struct KrkVM {
#define KRK_GLOBAL_GC_PAUSED (1 << 9)
#define KRK_GLOBAL_CLEAN_OUTPUT (1 << 10)
#define KRK_GLOBAL_CALLGRIND (1 << 11)
#define KRK_GLOBAL_REPORT_GC_COLLECTS (1 << 12)
#ifdef ENABLE_THREADING
# define threadLocal __thread

View File

@ -297,6 +297,9 @@ size_t krk_collectGarbage(void) {
tableRemoveWhite(&vm.strings);
size_t out = sweep();
vm.nextGC = vm.bytesAllocated * 2;
if (vm.globalFlags & KRK_GLOBAL_REPORT_GC_COLLECTS) {
fprintf(stderr, "[gc] collected %llu, next collection at %llu\n", (unsigned long long)out, (unsigned long long)vm.nextGC);
}
return out;
}