diff --git a/lib/runtime/AccessCounter.h b/lib/runtime/AccessCounter.h index 68dca194..d0930396 100644 --- a/lib/runtime/AccessCounter.h +++ b/lib/runtime/AccessCounter.h @@ -31,6 +31,13 @@ class AccessRecorder { inline void incHeapAlloc(int typeId, size_t count) { ++curHeapAllocs; + + // Always check here for max + // A program without free would otherwise never update maxHeap (see test 20_softcounter_max) + if (curHeapAllocs > maxHeapAllocs) { + maxHeapAllocs = curHeapAllocs; + } + ++heapAllocs; if (count > 1) { ++heapArray; @@ -72,9 +79,10 @@ class AccessRecorder { } inline void decHeapAlloc() { - if (curHeapAllocs > maxHeapAllocs) { - maxHeapAllocs = curHeapAllocs; - } + // Removed, since we already increment maxHeapAllocs just in time: + // if (curHeapAllocs > maxHeapAllocs) { + // maxHeapAllocs = curHeapAllocs; + // } --curHeapAllocs; } diff --git a/test/runtime/19_softcounter.c b/test/runtime/19_softcounter.c new file mode 100644 index 00000000..a5824aef --- /dev/null +++ b/test/runtime/19_softcounter.c @@ -0,0 +1,38 @@ +// RUN: %run %s -o -O3 2>&1 | FileCheck %s +// REQUIRED: softcounter + +#include + +int main(void) { + for (int i = 1; i <= 5; ++i) { + // 5 heap alloc and free: one single double, and then arrays + // max heap (concurrently) 1 + double* d = (double*)malloc(i * sizeof(double)); + free(d); + } + return 0; +} + +// CHECK: Alloc Stats from softcounters +// CHECK-NEXT: Total heap : 5 , 4 , - +// CHECK-NEXT: Total stack : 0 , 0 , - +// CHECK-NEXT: Total global : 0 , 0 , - +// CHECK-NEXT: Max. Heap Allocs : 1 , - , - +// CHECK-NEXT: Max. Stack Allocs : 0 , - , - +// CHECK-NEXT: Addresses checked : 0 , - , - +// CHECK-NEXT: Distinct Addresses checked : 0 , - , - +// CHECK-NEXT: Addresses re-used : 0 , - , - +// CHECK-NEXT: Addresses missed : 0 , - , - +// CHECK-NEXT: Distinct Addresses missed : 0 , - , - +// CHECK-NEXT: Total free heap : 5 , 4 , - +// CHECK-NEXT: Total free stack : 0 , 0 , - +// CHECK-NEXT: Null/Zero/NullZero Addr : 0 , 0 , 0 +// CHECK-NEXT: User-def. types : 0 , - , - +// CHECK-NEXT: Estimated memory use (KiB) : 4 , - , - +// CHECK-NEXT: Bytes per node map/stack : 96 , 8 , - +// CHECK-NEXT: {{(#|-)+}} +// CHECK-NEXT: Allocation type detail (heap, stack, global) +// CHECK-NEXT: 6 : 5 , 0 , 0 , double +// CHECK-NEXT: {{(#|-)+}} +// CHECK-NEXT: Free allocation type detail (heap, stack) +// CHECK-NEXT: 6 : 5 , 0 , double \ No newline at end of file diff --git a/test/runtime/20_softcounter_max.c b/test/runtime/20_softcounter_max.c new file mode 100644 index 00000000..d21444ee --- /dev/null +++ b/test/runtime/20_softcounter_max.c @@ -0,0 +1,38 @@ +// RUN: %run %s -o -O1 2>&1 | FileCheck %s +// REQUIRED: softcounter + +#include + +int main(void) { + for (int i = 1; i <= 6; ++i) { + // 6 heap alloc + // max heap (concurrently) 6 + double* d = (double*)malloc(sizeof(double)); + } + + return 0; +} + +// CHECK: Alloc Stats from softcounters +// CHECK-NEXT: Total heap : 6 , 0 , - +// CHECK-NEXT: Total stack : 0 , 0 , - +// CHECK-NEXT: Total global : 0 , 0 , - +// CHECK-NEXT: Max. Heap Allocs : 6 , - , - +// CHECK-NEXT: Max. Stack Allocs : 0 , - , - +// CHECK-NEXT: Addresses checked : 0 , - , - +// CHECK-NEXT: Distinct Addresses checked : 0 , - , - +// CHECK-NEXT: Addresses re-used : 0 , - , - +// CHECK-NEXT: Addresses missed : 0 , - , - +// CHECK-NEXT: Distinct Addresses missed : 0 , - , - +// CHECK-NEXT: Total free heap : 0 , 0 , - +// CHECK-NEXT: Total free stack : 0 , 0 , - +// CHECK-NEXT: Null/Zero/NullZero Addr : 0 , 0 , 0 +// CHECK-NEXT: User-def. types : 0 , - , - +// CHECK-NEXT: Estimated memory use (KiB) : {{[4-9]}} , - , - +// CHECK-NEXT: Bytes per node map/stack : 96 , 8 , - +// CHECK-NEXT: {{(#|-)+}} +// CHECK-NEXT: Allocation type detail (heap, stack, global) +// CHECK-NEXT: 6 : 6 , 0 , 0 , double +// CHECK-NEXT: {{(#|-)+}} +// CHECK-NEXT: Free allocation type detail (heap, stack) +// CHECK-NEXT: 6 : 0 , 0 , double \ No newline at end of file