OSDN Git Service

Update to current version of Go library.
[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         if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
19                 return
20         }
21
22         buf := make([]byte, 100000)
23         var prof bytes.Buffer
24         if err := StartCPUProfile(&prof); err != nil {
25                 t.Fatal(err)
26         }
27         // This loop takes about a quarter second on a 2 GHz laptop.
28         // We only need to get one 100 Hz clock tick, so we've got
29         // a 25x safety buffer.
30         for i := 0; i < 1000; i++ {
31                 crc32.ChecksumIEEE(buf)
32         }
33         StopCPUProfile()
34
35         // Convert []byte to []uintptr.
36         bytes := prof.Bytes()
37         val := *(*[]uintptr)(unsafe.Pointer(&bytes))
38         val = val[:len(bytes)/unsafe.Sizeof(uintptr(0))]
39
40         if len(val) < 10 {
41                 t.Fatalf("profile too short: %#x", val)
42         }
43         if val[0] != 0 || val[1] != 3 || val[2] != 0 || val[3] != 1e6/100 || val[4] != 0 {
44                 t.Fatalf("unexpected header %#x", val[:5])
45         }
46
47         // Check that profile is well formed and contains ChecksumIEEE.
48         found := false
49         val = val[5:]
50         for len(val) > 0 {
51                 if len(val) < 2 || val[0] < 1 || val[1] < 1 || uintptr(len(val)) < 2+val[1] {
52                         t.Fatalf("malformed profile.  leftover: %#x", val)
53                 }
54                 for _, pc := range val[2 : 2+val[1]] {
55                         f := runtime.FuncForPC(pc)
56                         if f == nil {
57                                 continue
58                         }
59                         if strings.Contains(f.Name(), "ChecksumIEEE") {
60                                 found = true
61                         }
62                 }
63                 val = val[2+val[1]:]
64         }
65
66         if !found {
67                 t.Fatal("did not find ChecksumIEEE in the profile")
68         }
69 }