OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / runtime / pprof / pprof_test.go
1 // Copyright 2011 The Go Authors.  All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package pprof_test
6
7 import (
8         "bytes"
9         "hash/crc32"
10         "runtime"
11         . "runtime/pprof"
12         "strings"
13         "testing"
14         "unsafe"
15 )
16
17 func TestCPUProfile(t *testing.T) {
18         switch runtime.GOOS {
19         case "darwin":
20                 // see Apple Bug Report #9177434 (copied into change description)
21                 return
22         case "plan9":
23                 // unimplemented
24                 return
25         case "windows":
26                 // unimplemented
27                 return
28         }
29
30         buf := make([]byte, 100000)
31         var prof bytes.Buffer
32         if err := StartCPUProfile(&prof); err != nil {
33                 t.Fatal(err)
34         }
35         // This loop takes about a quarter second on a 2 GHz laptop.
36         // We only need to get one 100 Hz clock tick, so we've got
37         // a 25x safety buffer.
38         for i := 0; i < 1000; i++ {
39                 crc32.ChecksumIEEE(buf)
40         }
41         StopCPUProfile()
42
43         // Convert []byte to []uintptr.
44         bytes := prof.Bytes()
45         val := *(*[]uintptr)(unsafe.Pointer(&bytes))
46         val = val[:len(bytes)/int(unsafe.Sizeof(uintptr(0)))]
47
48         if len(val) < 10 {
49                 t.Fatalf("profile too short: %#x", val)
50         }
51         if val[0] != 0 || val[1] != 3 || val[2] != 0 || val[3] != 1e6/100 || val[4] != 0 {
52                 t.Fatalf("unexpected header %#x", val[:5])
53         }
54
55         // Check that profile is well formed and contains ChecksumIEEE.
56         found := false
57         val = val[5:]
58         for len(val) > 0 {
59                 if len(val) < 2 || val[0] < 1 || val[1] < 1 || uintptr(len(val)) < 2+val[1] {
60                         t.Fatalf("malformed profile.  leftover: %#x", val)
61                 }
62                 for _, pc := range val[2 : 2+val[1]] {
63                         f := runtime.FuncForPC(pc)
64                         if f == nil {
65                                 continue
66                         }
67                         if strings.Contains(f.Name(), "ChecksumIEEE") {
68                                 found = true
69                         }
70                 }
71                 val = val[2+val[1]:]
72         }
73
74         if !found {
75                 t.Fatal("did not find ChecksumIEEE in the profile")
76         }
77 }