]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/race: lazily allocate shadow memory
authorDmitriy Vyukov <dvyukov@google.com>
Wed, 7 Nov 2012 08:48:58 +0000 (12:48 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Wed, 7 Nov 2012 08:48:58 +0000 (12:48 +0400)
Currently race detector runtime maps shadow memory eagerly at process startup.
It works poorly on Windows, because Windows requires reservation in swap file
(especially problematic if several Go program runs at the same, each consuming GBs
of memory).
With this change race detector maps shadow memory lazily, so Go runtime must notify
about all new heap memory.
It will help with Windows port, but also eliminates scary 16TB virtual mememory
consumption in top output (which sometimes confuses some monitoring scripts).

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6811085

src/pkg/runtime/malloc.goc
src/pkg/runtime/race.c
src/pkg/runtime/race.h
src/pkg/runtime/race/race.go
src/pkg/runtime/race/race_darwin_amd64.syso
src/pkg/runtime/race/race_linux_amd64.syso
src/pkg/runtime/race0.c

index f8aa1c949e7a20e48c77f2ec87db4e723d52aa68..a96372451c2d41df3082061f7c8401d5c7eef573 100644 (file)
@@ -434,6 +434,8 @@ runtime·MHeap_SysAlloc(MHeap *h, uintptr n)
                runtime·SysMap(p, n);
                h->arena_used += n;
                runtime·MHeap_MapBits(h);
+               if(raceenabled)
+                       runtime·racemapshadow(p, n);
                return p;
        }
        
@@ -460,6 +462,8 @@ runtime·MHeap_SysAlloc(MHeap *h, uintptr n)
                if(h->arena_used > h->arena_end)
                        h->arena_end = h->arena_used;
                runtime·MHeap_MapBits(h);
+               if(raceenabled)
+                       runtime·racemapshadow(p, n);
        }
        
        return p;
index ef7eec2b6b75beb25c32a7c0dfba0fe7ec5ad77b..49c7d4eb2cac0278af8d0f0a6f3a9da74848b7cc 100644 (file)
@@ -11,6 +11,7 @@
 #include "race.h"
 
 void runtime∕race·Initialize(void);
+void runtime∕race·MapShadow(void *addr, uintptr size);
 void runtime∕race·Finalize(void);
 void runtime∕race·FinalizerGoroutine(int32);
 void runtime∕race·Read(int32 goid, void *addr, void *pc);
@@ -35,6 +36,7 @@ runtime·raceinit(void)
 {
        m->racecall = true;
        runtime∕race·Initialize();
+       runtime∕race·MapShadow(noptrdata, enoptrbss - noptrdata);
        m->racecall = false;
 }
 
@@ -46,6 +48,14 @@ runtime·racefini(void)
        m->racecall = false;
 }
 
+void
+runtime·racemapshadow(void *addr, uintptr size)
+{
+       m->racecall = true;
+       runtime∕race·MapShadow(addr, size);
+       m->racecall = false;
+}
+
 // Called from instrumented code.
 // If we split stack, getcallerpc() can return runtime·lessstack().
 #pragma textflag 7
index eea1f9465ea4dfce076c283fbb38772f628074ac..9ebb9d3738e73f6d3a5f09f8c81228d58d7d81da 100644 (file)
@@ -15,6 +15,7 @@ void  runtime·raceinit(void);
 // Finalize race detection subsystem, does not return.
 void   runtime·racefini(void);
 
+void   runtime·racemapshadow(void *addr, uintptr size);
 void   runtime·racemalloc(void *p, uintptr sz, void *pc);
 void   runtime·racefree(void *p);
 void   runtime·racegostart(int32 goid, void *pc);
index f3a7be50c6dfc14019fd1cb813b2c59f5f27b303..fb59ee833846550328d36b8c489d166cef7432a4 100644 (file)
@@ -10,6 +10,7 @@ package race
 /*
 void __tsan_init(void);
 void __tsan_fini(void);
+void __tsan_map_shadow(void *addr, void *size);
 void __tsan_go_start(int pgoid, int chgoid, void *pc);
 void __tsan_go_end(int goid);
 void __tsan_read(int goid, void *addr, void *pc);
@@ -38,6 +39,10 @@ func Finalize() {
        C.__tsan_fini()
 }
 
+func MapShadow(addr, size uintptr) {
+       C.__tsan_map_shadow(unsafe.Pointer(addr), unsafe.Pointer(size))
+}
+
 func FinalizerGoroutine(goid int32) {
        C.__tsan_finalizer_goroutine(C.int(goid))
 }
index b291e8e07853a92b86c7f07e61a09d33b76214e5..b323a7129744adbc4bfc918aa12f63c37cc9c051 100644 (file)
Binary files a/src/pkg/runtime/race/race_darwin_amd64.syso and b/src/pkg/runtime/race/race_darwin_amd64.syso differ
index c8e331f71631e32bfeef37b9d5ed8888f49704d0..ccc42e57ca615447cdce1d7822cc79d61ca286f9 100644 (file)
Binary files a/src/pkg/runtime/race/race_linux_amd64.syso and b/src/pkg/runtime/race/race_linux_amd64.syso differ
index b650a147126bd0dbf820b59b39a05b60f3c8396f..82ebba82298712bb279e58186fa71880edec3afd 100644 (file)
@@ -17,6 +17,14 @@ runtime·racefini(void)
 {
 }
 
+
+void
+runtime·racemapshadow(void *addr, uintptr size)
+{
+       USED(addr);
+       USED(size);
+}
+
 void
 runtime·racewritepc(void *addr, void *pc)
 {