From e4f06812c5c291e21c07bb19cc48f947f0a84060 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 8 Feb 2010 14:32:22 -0800 Subject: [PATCH] runtime: instrument malloc + garbage collector. add simple garbage collection benchmark. R=iant CC=golang-dev https://golang.org/cl/204053 --- src/pkg/runtime/darwin/386/sys.s | 16 +++ src/pkg/runtime/darwin/amd64/sys.s | 13 +- src/pkg/runtime/extern.go | 9 ++ src/pkg/runtime/freebsd/386/sys.s | 17 +++ src/pkg/runtime/freebsd/amd64/sys.s | 15 +++ src/pkg/runtime/linux/386/sys.s | 17 +++ src/pkg/runtime/linux/amd64/sys.s | 15 +++ src/pkg/runtime/malloc.cgo | 4 + src/pkg/runtime/malloc.h | 11 +- src/pkg/runtime/mgc0.c | 7 + src/pkg/runtime/msize.c | 4 + src/pkg/runtime/runtime.c | 11 ++ src/pkg/runtime/runtime.h | 2 + test/garbage/parser.go | 201 ++++++++++++++++++++++++++++ 14 files changed, 340 insertions(+), 2 deletions(-) create mode 100644 test/garbage/parser.go diff --git a/src/pkg/runtime/darwin/386/sys.s b/src/pkg/runtime/darwin/386/sys.s index 326cc23e52..79628a463f 100644 --- a/src/pkg/runtime/darwin/386/sys.s +++ b/src/pkg/runtime/darwin/386/sys.s @@ -42,6 +42,22 @@ TEXT ·mmap(SB),7,$0 CALL notok(SB) RET +// void gettime(int64 *sec, int32 *usec) +TEXT gettime(SB), 7, $32 + LEAL 12(SP), AX // must be non-nil, unused + MOVL AX, 4(SP) + MOVL $0, 8(SP) // time zone pointer + MOVL $116, AX + INT $0x80 + + MOVL sec+0(FP), DI + MOVL AX, (DI) + MOVL $0, 4(DI) // zero extend 32 -> 64 + + MOVL usec+4(FP), DI + MOVL DX, (DI) + RET + TEXT sigaction(SB),7,$0 MOVL $46, AX INT $0x80 diff --git a/src/pkg/runtime/darwin/amd64/sys.s b/src/pkg/runtime/darwin/amd64/sys.s index 223790a523..50b50d5de0 100644 --- a/src/pkg/runtime/darwin/amd64/sys.s +++ b/src/pkg/runtime/darwin/amd64/sys.s @@ -37,6 +37,18 @@ TEXT write(SB),7,$0 CALL notok(SB) RET +// void gettime(int64 *sec, int32 *usec) +TEXT gettime(SB), 7, $32 + MOVQ SP, DI // must be non-nil, unused + MOVQ $0, SI + MOVQ $(0x2000000+116), AX + SYSCALL + MOVQ sec+0(FP), DI + MOVQ AX, (DI) + MOVQ usec+8(FP), DI + MOVL DX, (DI) + RET + TEXT sigaction(SB),7,$0 MOVL 8(SP), DI // arg 1 sig MOVQ 16(SP), SI // arg 2 act @@ -226,4 +238,3 @@ TEXT mach_semaphore_signal_all(SB),7,$0 MOVL $(0x1000000+34), AX // semaphore_signal_all_trap SYSCALL RET - diff --git a/src/pkg/runtime/extern.go b/src/pkg/runtime/extern.go index a397c3b905..0834f78791 100644 --- a/src/pkg/runtime/extern.go +++ b/src/pkg/runtime/extern.go @@ -73,13 +73,22 @@ func Siginit() type MemStatsType struct { Alloc uint64 + TotalAlloc uint64 Sys uint64 Stacks uint64 InusePages uint64 NextGC uint64 Lookups uint64 Mallocs uint64 + PauseNs uint64 + NumGC uint32 EnableGC bool + DebugGC bool + BySize [67]struct { + Size uint32 + Mallocs uint64 + Frees uint64 + } } // MemStats holds statistics about the memory system. diff --git a/src/pkg/runtime/freebsd/386/sys.s b/src/pkg/runtime/freebsd/386/sys.s index d0afeae3a9..a0860db81b 100644 --- a/src/pkg/runtime/freebsd/386/sys.s +++ b/src/pkg/runtime/freebsd/386/sys.s @@ -73,6 +73,23 @@ TEXT ·mmap(SB),7,$-4 CALL notok(SB) RET +TEXT gettime(SB), 7, $32 + MOVL $116, AX + LEAL 12(SP), BX + MOVL BX, 4(SP) + MOVL $0, 8(SP) + INT $0x80 + + MOVL 12(SP), BX // sec + MOVL sec+0(FP), DI + MOVL BX, (DI) + MOVL $0, 4(DI) // zero extend 32 -> 64 bits + + MOVL 16(SP), BX // usec + MOVL usec+4(FP), DI + MOVL BX, (DI) + RET + TEXT sigaction(SB),7,$-4 MOVL $416, AX INT $0x80 diff --git a/src/pkg/runtime/freebsd/amd64/sys.s b/src/pkg/runtime/freebsd/amd64/sys.s index 53773b99f3..02c3e91436 100644 --- a/src/pkg/runtime/freebsd/amd64/sys.s +++ b/src/pkg/runtime/freebsd/amd64/sys.s @@ -58,6 +58,21 @@ TEXT write(SB),7,$-8 CALL notok(SB) RET +TEXT gettime(SB), 7, $32 + MOVL $116, AX + LEAQ 8(SP), DI + SYSCALL + + MOVQ 8(SP), BX // sec + MOVQ sec+0(FP), DI + MOVQ BX, (DI) + + MOVL 16(SP), BX // usec + MOVQ usec+8(FP), DI + MOVL BX, (DI) + RET + + TEXT sigaction(SB),7,$-8 MOVL 8(SP), DI // arg 1 sig MOVQ 16(SP), SI // arg 2 act diff --git a/src/pkg/runtime/linux/386/sys.s b/src/pkg/runtime/linux/386/sys.s index 7f644cbf3f..ed7c155f1f 100644 --- a/src/pkg/runtime/linux/386/sys.s +++ b/src/pkg/runtime/linux/386/sys.s @@ -30,6 +30,23 @@ TEXT write(SB),7,$0 INT $0x80 RET +TEXT gettime(SB), 7, $32 + MOVL $78, AX // syscall - gettimeofday + LEAL 8(SP), BX + MOVL $0, CX + MOVL $0, DX + INT $0x80 + + MOVL 8(SP), BX // sec + MOVL sec+0(FP), DI + MOVL BX, (DI) + MOVL $0, 4(DI) // zero extend 32 -> 64 bits + + MOVL 12(SP), BX // usec + MOVL usec+4(FP), DI + MOVL BX, (DI) + RET + TEXT rt_sigaction(SB),7,$0 MOVL $174, AX // syscall - rt_sigaction MOVL 4(SP), BX diff --git a/src/pkg/runtime/linux/amd64/sys.s b/src/pkg/runtime/linux/amd64/sys.s index 8e0905ee15..18bf5b5092 100644 --- a/src/pkg/runtime/linux/amd64/sys.s +++ b/src/pkg/runtime/linux/amd64/sys.s @@ -44,6 +44,21 @@ TEXT ·write(SB),7,$0-24 SYSCALL RET +TEXT gettime(SB), 7, $32 + LEAQ 8(SP), DI + MOVQ $0, SI + MOVQ $0xffffffffff600000, AX + CALL AX + + MOVQ 8(SP), BX // sec + MOVQ sec+0(FP), DI + MOVQ BX, (DI) + + MOVL 16(SP), BX // usec + MOVQ usec+8(FP), DI + MOVL BX, (DI) + RET + TEXT rt_sigaction(SB),7,$0-32 MOVL 8(SP), DI MOVQ 16(SP), SI diff --git a/src/pkg/runtime/malloc.cgo b/src/pkg/runtime/malloc.cgo index d7e3e4151d..286aa2bf3c 100644 --- a/src/pkg/runtime/malloc.cgo +++ b/src/pkg/runtime/malloc.cgo @@ -46,6 +46,8 @@ mallocgc(uintptr size, uint32 refflag, int32 dogc) if(v == nil) throw("out of memory"); mstats.alloc += size; + mstats.total_alloc += size; + mstats.by_size[sizeclass].nmalloc++; } else { // TODO(rsc): Report tracebacks for very large allocations. @@ -57,6 +59,7 @@ mallocgc(uintptr size, uint32 refflag, int32 dogc) if(s == nil) throw("out of memory"); mstats.alloc += npages<start << PageShift); } @@ -127,6 +130,7 @@ free(void *v) size = class_to_size[sizeclass]; runtime_memclr(v, size); mstats.alloc -= size; + mstats.by_size[sizeclass].nfree++; MCache_Free(c, v, sizeclass, size); out: diff --git a/src/pkg/runtime/malloc.h b/src/pkg/runtime/malloc.h index 133ed02329..05f500a1e7 100644 --- a/src/pkg/runtime/malloc.h +++ b/src/pkg/runtime/malloc.h @@ -156,17 +156,26 @@ void FixAlloc_Free(FixAlloc *f, void *p); // Statistics. -// Shared with Go: if you edit this structure, also edit ../malloc/malloc.go. +// Shared with Go: if you edit this structure, also edit extern.go. struct MStats { uint64 alloc; + uint64 total_alloc; uint64 sys; uint64 stacks; uint64 inuse_pages; // protected by mheap.Lock uint64 next_gc; // protected by mheap.Lock uint64 nlookup; // unprotected (approximate) uint64 nmalloc; // unprotected (approximate) + uint64 pause_ns; + uint32 numgc; bool enablegc; + bool debuggc; + struct { + uint32 size; + uint64 nmalloc; + uint64 nfree; + } by_size[NumSizeClasses]; }; #define mstats ·MemStats /* name shared with Go */ diff --git a/src/pkg/runtime/mgc0.c b/src/pkg/runtime/mgc0.c index d8a943e2a2..83d217320d 100644 --- a/src/pkg/runtime/mgc0.c +++ b/src/pkg/runtime/mgc0.c @@ -240,6 +240,7 @@ static int32 gcpercent = -2; void gc(int32 force) { + int64 t0, t1; byte *p; void **fp; @@ -268,6 +269,7 @@ gc(int32 force) //printf("gc...\n"); semacquire(&gcsema); + t0 = nanotime(); m->gcing = 1; stoptheworld(); if(mheap.Lock.key != 0) @@ -289,6 +291,11 @@ gc(int32 force) pfinq = finq; m->locks--; + t1 = nanotime(); + mstats.numgc++; + mstats.pause_ns += t1 - t0; + if(mstats.debuggc) + printf("pause %D\n", t1-t0); semrelease(&gcsema); starttheworld(); } diff --git a/src/pkg/runtime/msize.c b/src/pkg/runtime/msize.c index 25e22637d2..aebc15416d 100644 --- a/src/pkg/runtime/msize.c +++ b/src/pkg/runtime/msize.c @@ -134,6 +134,10 @@ InitSizes(void) } } + // Copy out for statistics table. + for(i=0; i