@@ -14,11 +14,15 @@
1414VM* init_vm(int stack_size, size_t heap_size,
1515 int max_threads, // not implemented yet
1616 int argc, char* argv[]) {
17− VAL* valstack = malloc(stack_size*sizeof(VAL));
18− int* intstack = malloc(stack_size*sizeof(int));
19− double* floatstack = malloc(stack_size*sizeof(double));
2017
2118 VM* vm = malloc(sizeof(VM));
19+ STATS_INIT_STATS(vm->stats)
20+ STATS_ENTER_INIT(vm->stats)
21+
22+ VAL* valstack = malloc(stack_size * sizeof(VAL));
23+ int* intstack = malloc(stack_size * sizeof(int));
24+ double* floatstack = malloc(stack_size*sizeof(double));
25+
2226 vm->valstack = valstack;
2327 vm->valstack_top = valstack;
2428 vm->valstack_base = valstack;
@@ -30,9 +34,6 @@ VM* init_vm(int stack_size, size_t heap_size,
3034
3135 alloc_heap(&(vm->heap), heap_size);
3236
33− vm->collections = 0;
34− vm->allocations = 0;
35−
3637 vm->ret = NULL;
3738 vm->reg1 = NULL;
3839
@@ -59,23 +60,28 @@ VM* init_vm(int stack_size, size_t heap_size,
5960 vm->argv[i] = MKSTR(vm, argv[i]);
6061 }
6162
63+ STATS_LEAVE_INIT(vm->stats)
6264 return vm;
6365}
6466
65−void terminate(VM* vm) {
67+Stats terminate(VM* vm) {
68+ Stats stats = vm->stats;
69+ STATS_ENTER_EXIT(stats)
70+
6671 free(vm->inbox);
6772 free(vm->valstack);
6873 free(vm->intstack);
6974 free(vm->floatstack);
70−
7175 free_heap(&(vm->heap));
72−
7376 free(vm->argv);
7477
7578 pthread_mutex_destroy(&(vm -> inbox_lock));
7679 pthread_mutex_destroy(&(vm -> inbox_block));
7780 pthread_cond_destroy(&(vm -> inbox_waiting));
7881 free(vm);
82+
83+ STATS_LEAVE_EXIT(stats)
84+ return stats;
7985}
8086
8187void idris_requireAlloc(VM* vm, size_t size) {
@@ -111,7 +117,7 @@ void* allocate(VM* vm, size_t size, int outerlock) {
111117 size_t chunk_size = size + sizeof(size_t);
112118
113119 if (vm->heap.next + chunk_size < vm->heap.end) {
114− vm->allocations += chunk_size;
120+ STATS_ALLOC(vm->stats, chunk_size)
115121 void* ptr = (void*)(vm->heap.next + sizeof(size_t));
116122 *((size_t*)(vm->heap.next)) = chunk_size;
117123 vm->heap.next += chunk_size;
@@ -409,7 +415,9 @@ void* runThread(void* arg) {
409415 callvm->processes--;
410416
411417 free(td);
412− terminate(vm);
418+
419+ Stats stats = terminate(vm);
420+ // aggregate_stats(&(td->vm->stats), &stats);
413421 return NULL;
414422}
415423
@@ -501,12 +509,12 @@ void idris_sendMessage(VM* sender, VM* dest, VAL msg) {
501509 // So: we try to copy, if a collection happens, we do the copy again
502510 // under the assumption there's enough space this time.
503511
504− int gcs = dest->collections;
512+ int gcs = dest->stats.collections;
505513 pthread_mutex_lock(&dest->alloc_lock);
506514 VAL dmsg = copyTo(dest, msg);
507515 pthread_mutex_unlock(&dest->alloc_lock);
508516
509− if (dest->collections>gcs) {
517+ if (dest->stats.collections > gcs) {
510518 // a collection will have invalidated the copy
511519 pthread_mutex_lock(&dest->alloc_lock);
512520 dmsg = copyTo(dest, msg); // try again now there's room...