-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements ability to change sample rate in go profiler
Co-authored-by: alonlong <alonlong@163.com> Co-authored-by: Dmitry Filimonov <dmitry@pyroscope.io>
- Loading branch information
1 parent
411e51e
commit f98350d
Showing
15 changed files
with
1,590 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2009 The Go Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is a copy of golang `runtime/pprof` package. The original code is available [here](https://github.com/golang/go/tree/master/src/runtime/pprof) and all the credit goes to the Go Authors. | ||
|
||
It is licensed under BSD-style license found in the LICENSE file. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2017 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package pprof | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var ( | ||
errBadELF = errors.New("malformed ELF binary") | ||
errNoBuildID = errors.New("no NT_GNU_BUILD_ID found in ELF binary") | ||
) | ||
|
||
// elfBuildID returns the GNU build ID of the named ELF binary, | ||
// without introducing a dependency on debug/elf and its dependencies. | ||
func elfBuildID(file string) (string, error) { | ||
buf := make([]byte, 256) | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
if _, err := f.ReadAt(buf[:64], 0); err != nil { | ||
return "", err | ||
} | ||
|
||
// ELF file begins with \x7F E L F. | ||
if buf[0] != 0x7F || buf[1] != 'E' || buf[2] != 'L' || buf[3] != 'F' { | ||
return "", errBadELF | ||
} | ||
|
||
var byteOrder binary.ByteOrder | ||
switch buf[5] { | ||
default: | ||
return "", errBadELF | ||
case 1: // little-endian | ||
byteOrder = binary.LittleEndian | ||
case 2: // big-endian | ||
byteOrder = binary.BigEndian | ||
} | ||
|
||
var shnum int | ||
var shoff, shentsize int64 | ||
switch buf[4] { | ||
default: | ||
return "", errBadELF | ||
case 1: // 32-bit file header | ||
shoff = int64(byteOrder.Uint32(buf[32:])) | ||
shentsize = int64(byteOrder.Uint16(buf[46:])) | ||
if shentsize != 40 { | ||
return "", errBadELF | ||
} | ||
shnum = int(byteOrder.Uint16(buf[48:])) | ||
case 2: // 64-bit file header | ||
shoff = int64(byteOrder.Uint64(buf[40:])) | ||
shentsize = int64(byteOrder.Uint16(buf[58:])) | ||
if shentsize != 64 { | ||
return "", errBadELF | ||
} | ||
shnum = int(byteOrder.Uint16(buf[60:])) | ||
} | ||
|
||
for i := 0; i < shnum; i++ { | ||
if _, err := f.ReadAt(buf[:shentsize], shoff+int64(i)*shentsize); err != nil { | ||
return "", err | ||
} | ||
if typ := byteOrder.Uint32(buf[4:]); typ != 7 { // SHT_NOTE | ||
continue | ||
} | ||
var off, size int64 | ||
if shentsize == 40 { | ||
// 32-bit section header | ||
off = int64(byteOrder.Uint32(buf[16:])) | ||
size = int64(byteOrder.Uint32(buf[20:])) | ||
} else { | ||
// 64-bit section header | ||
off = int64(byteOrder.Uint64(buf[24:])) | ||
size = int64(byteOrder.Uint64(buf[32:])) | ||
} | ||
size += off | ||
for off < size { | ||
if _, err := f.ReadAt(buf[:16], off); err != nil { // room for header + name GNU\x00 | ||
return "", err | ||
} | ||
nameSize := int(byteOrder.Uint32(buf[0:])) | ||
descSize := int(byteOrder.Uint32(buf[4:])) | ||
noteType := int(byteOrder.Uint32(buf[8:])) | ||
descOff := off + int64(12+(nameSize+3)&^3) | ||
off = descOff + int64((descSize+3)&^3) | ||
if nameSize != 4 || noteType != 3 || buf[12] != 'G' || buf[13] != 'N' || buf[14] != 'U' || buf[15] != '\x00' { // want name GNU\x00 type 3 (NT_GNU_BUILD_ID) | ||
continue | ||
} | ||
if descSize > len(buf) { | ||
return "", errBadELF | ||
} | ||
if _, err := f.ReadAt(buf[:descSize], descOff); err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%x", buf[:descSize]), nil | ||
} | ||
} | ||
return "", errNoBuildID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2017 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package pprof | ||
|
||
import "unsafe" | ||
|
||
// A profMap is a map from (stack, tag) to mapEntry. | ||
// It grows without bound, but that's assumed to be OK. | ||
type profMap struct { | ||
hash map[uintptr]*profMapEntry | ||
all *profMapEntry | ||
last *profMapEntry | ||
free []profMapEntry | ||
freeStk []uintptr | ||
} | ||
|
||
// A profMapEntry is a single entry in the profMap. | ||
type profMapEntry struct { | ||
nextHash *profMapEntry // next in hash list | ||
nextAll *profMapEntry // next in list of all entries | ||
stk []uintptr | ||
tag unsafe.Pointer | ||
count int64 | ||
} | ||
|
||
func (m *profMap) lookup(stk []uint64, tag unsafe.Pointer) *profMapEntry { | ||
// Compute hash of (stk, tag). | ||
h := uintptr(0) | ||
for _, x := range stk { | ||
h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1))) | ||
h += uintptr(x) * 41 | ||
} | ||
h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1))) | ||
h += uintptr(tag) * 41 | ||
|
||
// Find entry if present. | ||
var last *profMapEntry | ||
Search: | ||
for e := m.hash[h]; e != nil; last, e = e, e.nextHash { | ||
if len(e.stk) != len(stk) || e.tag != tag { | ||
continue | ||
} | ||
for j := range stk { | ||
if e.stk[j] != uintptr(stk[j]) { | ||
continue Search | ||
} | ||
} | ||
// Move to front. | ||
if last != nil { | ||
last.nextHash = e.nextHash | ||
e.nextHash = m.hash[h] | ||
m.hash[h] = e | ||
} | ||
return e | ||
} | ||
|
||
// Add new entry. | ||
if len(m.free) < 1 { | ||
m.free = make([]profMapEntry, 128) | ||
} | ||
e := &m.free[0] | ||
m.free = m.free[1:] | ||
e.nextHash = m.hash[h] | ||
e.tag = tag | ||
|
||
if len(m.freeStk) < len(stk) { | ||
m.freeStk = make([]uintptr, 1024) | ||
} | ||
e.stk = m.freeStk[:len(stk)] | ||
m.freeStk = m.freeStk[len(stk):] | ||
|
||
for j := range stk { | ||
e.stk[j] = uintptr(stk[j]) | ||
} | ||
if m.hash == nil { | ||
m.hash = make(map[uintptr]*profMapEntry) | ||
} | ||
m.hash[h] = e | ||
if m.all == nil { | ||
m.all = e | ||
m.last = e | ||
} else { | ||
m.last.nextAll = e | ||
m.last = e | ||
} | ||
return e | ||
} |
Oops, something went wrong.