]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add threadprof tag for test that starts busy thread
authorIan Lance Taylor <iant@golang.org>
Wed, 5 Oct 2016 14:31:11 +0000 (07:31 -0700)
committerIan Lance Taylor <iant@golang.org>
Thu, 6 Oct 2016 01:23:09 +0000 (01:23 +0000)
The CgoExternalThreadSIGPROF test starts a thread at constructor time
that does a busy loop. That can throw off some other tests. So only
build that code if testprogcgo is built with the tag threadprof, and
adjust the tests that use that code to pass that build tag.

This revealed that the CgoPprofThread test was not testing what it
should have, as it never actually started the cpuHog thread. It was
passing because of the busy loop thread. Fix it to start the thread as
intended.

Change-Id: I087a9e4fc734a86be16a287456441afac5676beb
Reviewed-on: https://go-review.googlesource.com/30362
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/runtime/crash_cgo_test.go
src/runtime/testdata/testprogcgo/threadpprof.go
src/runtime/testdata/testprogcgo/threadprof.go

index 2e2f95e5f0ded0bc821c6bd03b2e0a7e51c69dc0..2642c28f0d9e57638fa016f17542bdd89dca2462 100644 (file)
@@ -100,9 +100,18 @@ func TestCgoExternalThreadSIGPROF(t *testing.T) {
                // ppc64 (issue #8912)
                t.Skipf("no external linking on ppc64")
        }
-       got := runTestProg(t, "testprogcgo", "CgoExternalThreadSIGPROF")
-       want := "OK\n"
-       if got != want {
+
+       exe, err := buildTestProg(t, "testprogcgo", "-tags=threadprof")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       got, err := testEnv(exec.Command(exe, "CgoExternalThreadSIGPROF")).CombinedOutput()
+       if err != nil {
+               t.Fatalf("exit status: %v\n%s", err, got)
+       }
+
+       if want := "OK\n"; string(got) != want {
                t.Fatalf("expected %q, but got:\n%s", want, got)
        }
 }
@@ -113,9 +122,19 @@ func TestCgoExternalThreadSignal(t *testing.T) {
        case "plan9", "windows":
                t.Skipf("no pthreads on %s", runtime.GOOS)
        }
-       got := runTestProg(t, "testprogcgo", "CgoExternalThreadSignal")
-       want := "OK\n"
-       if got != want {
+
+       exe, err := buildTestProg(t, "testprogcgo", "-tags=threadprof")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       got, err := testEnv(exec.Command(exe, "CgoExternalThreadSIGPROF")).CombinedOutput()
+       if err != nil {
+               t.Fatalf("exit status: %v\n%s", err, got)
+       }
+
+       want := []byte("OK\n")
+       if !bytes.Equal(got, want) {
                t.Fatalf("expected %q, but got:\n%s", want, got)
        }
 }
index f057d591c34ba47091ad87270915f985532e1f34..81965831a718e83cf7257d821a9ecba3eaf2d683 100644 (file)
@@ -53,6 +53,18 @@ void pprofCgoThreadTraceback(void* parg) {
 int getCPUHogThreadCount() {
        return __sync_add_and_fetch(&cpuHogThreadCount, 0);
 }
+
+static void* cpuHogDriver(void* arg __attribute__ ((unused))) {
+       while (1) {
+               cpuHogThread();
+       }
+       return 0;
+}
+
+void runCPUHogThread() {
+       pthread_t tid;
+       pthread_create(&tid, 0, cpuHogDriver, 0);
+}
 */
 import "C"
 
@@ -84,6 +96,8 @@ func CgoPprofThread() {
                os.Exit(2)
        }
 
+       C.runCPUHogThread()
+
        t0 := time.Now()
        for C.getCPUHogThreadCount() < 2 && time.Since(t0) < time.Second {
                time.Sleep(100 * time.Millisecond)
index 516f8dce9e22bcf8efccb20db9ba4e55eafe16e5..2d4c1039fb737fb2b3e00c2712dc81033781ff9e 100644 (file)
@@ -2,7 +2,11 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// We only build this file with the tag "threadprof", since it starts
+// a thread running a busy loop at constructor time.
+
 // +build !plan9,!windows
+// +build threadprof
 
 package main
 
@@ -21,6 +25,7 @@ static void *thread1(void *p) {
        spinlock = 0;
        return NULL;
 }
+
 __attribute__((constructor)) void issue9456() {
        pthread_t tid;
        pthread_create(&tid, 0, thread1, NULL);