Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "bytes"
  17. "crypto/tls"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "net/http"
  22. "net/url"
  23. "os"
  24. "os/exec"
  25. "path/filepath"
  26. "runtime"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "time"
  31. "github.com/google/pprof/internal/measurement"
  32. "github.com/google/pprof/internal/plugin"
  33. "github.com/google/pprof/profile"
  34. )
  35. // fetchProfiles fetches and symbolizes the profiles specified by s.
  36. // It will merge all the profiles it is able to retrieve, even if
  37. // there are some failures. It will return an error if it is unable to
  38. // fetch any profiles.
  39. func fetchProfiles(s *source, o *plugin.Options) (*profile.Profile, error) {
  40. sources := make([]profileSource, 0, len(s.Sources)+len(s.Base))
  41. for _, src := range s.Sources {
  42. sources = append(sources, profileSource{
  43. addr: src,
  44. source: s,
  45. scale: 1,
  46. })
  47. }
  48. for _, src := range s.Base {
  49. sources = append(sources, profileSource{
  50. addr: src,
  51. source: s,
  52. scale: -1,
  53. })
  54. }
  55. p, msrcs, save, cnt, err := chunkedGrab(sources, o.Fetch, o.Obj, o.UI)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if cnt == 0 {
  60. return nil, fmt.Errorf("failed to fetch any profiles")
  61. }
  62. if want, got := len(sources), cnt; want != got {
  63. o.UI.PrintErr(fmt.Sprintf("fetched %d profiles out of %d", got, want))
  64. }
  65. // Symbolize the merged profile.
  66. if err := o.Sym.Symbolize(s.Symbolize, msrcs, p); err != nil {
  67. return nil, err
  68. }
  69. p.RemoveUninteresting()
  70. unsourceMappings(p)
  71. // Save a copy of the merged profile if there is at least one remote source.
  72. if save {
  73. dir, err := setTmpDir(o.UI)
  74. if err != nil {
  75. return nil, err
  76. }
  77. prefix := "pprof."
  78. if len(p.Mapping) > 0 && p.Mapping[0].File != "" {
  79. prefix += filepath.Base(p.Mapping[0].File) + "."
  80. }
  81. for _, s := range p.SampleType {
  82. prefix += s.Type + "."
  83. }
  84. tempFile, err := newTempFile(dir, prefix, ".pb.gz")
  85. if err == nil {
  86. if err = p.Write(tempFile); err == nil {
  87. o.UI.PrintErr("Saved profile in ", tempFile.Name())
  88. }
  89. }
  90. if err != nil {
  91. o.UI.PrintErr("Could not save profile: ", err)
  92. }
  93. }
  94. if err := p.CheckValid(); err != nil {
  95. return nil, err
  96. }
  97. return p, nil
  98. }
  99. // chunkedGrab fetches the profiles described in source and merges them into
  100. // a single profile. It fetches a chunk of profiles concurrently, with a maximum
  101. // chunk size to limit its memory usage.
  102. func chunkedGrab(sources []profileSource, fetch plugin.Fetcher, obj plugin.ObjTool, ui plugin.UI) (*profile.Profile, plugin.MappingSources, bool, int, error) {
  103. const chunkSize = 64
  104. var p *profile.Profile
  105. var msrc plugin.MappingSources
  106. var save bool
  107. var count int
  108. for start := 0; start < len(sources); start += chunkSize {
  109. end := start + chunkSize
  110. if end > len(sources) {
  111. end = len(sources)
  112. }
  113. chunkP, chunkMsrc, chunkSave, chunkCount, chunkErr := concurrentGrab(sources[start:end], fetch, obj, ui)
  114. switch {
  115. case chunkErr != nil:
  116. return nil, nil, false, 0, chunkErr
  117. case chunkP == nil:
  118. continue
  119. case p == nil:
  120. p, msrc, save, count = chunkP, chunkMsrc, chunkSave, chunkCount
  121. default:
  122. p, msrc, chunkErr = combineProfiles([]*profile.Profile{p, chunkP}, []plugin.MappingSources{msrc, chunkMsrc})
  123. if chunkErr != nil {
  124. return nil, nil, false, 0, chunkErr
  125. }
  126. if chunkSave {
  127. save = true
  128. }
  129. count += chunkCount
  130. }
  131. }
  132. return p, msrc, save, count, nil
  133. }
  134. // concurrentGrab fetches multiple profiles concurrently
  135. func concurrentGrab(sources []profileSource, fetch plugin.Fetcher, obj plugin.ObjTool, ui plugin.UI) (*profile.Profile, plugin.MappingSources, bool, int, error) {
  136. wg := sync.WaitGroup{}
  137. wg.Add(len(sources))
  138. for i := range sources {
  139. go func(s *profileSource) {
  140. defer wg.Done()
  141. s.p, s.msrc, s.remote, s.err = grabProfile(s.source, s.addr, s.scale, fetch, obj, ui)
  142. }(&sources[i])
  143. }
  144. wg.Wait()
  145. var save bool
  146. profiles := make([]*profile.Profile, 0, len(sources))
  147. msrcs := make([]plugin.MappingSources, 0, len(sources))
  148. for i := range sources {
  149. s := &sources[i]
  150. if err := s.err; err != nil {
  151. ui.PrintErr(s.addr + ": " + err.Error())
  152. continue
  153. }
  154. save = save || s.remote
  155. profiles = append(profiles, s.p)
  156. msrcs = append(msrcs, s.msrc)
  157. *s = profileSource{}
  158. }
  159. if len(profiles) == 0 {
  160. return nil, nil, false, 0, nil
  161. }
  162. p, msrc, err := combineProfiles(profiles, msrcs)
  163. if err != nil {
  164. return nil, nil, false, 0, err
  165. }
  166. return p, msrc, save, len(profiles), nil
  167. }
  168. func combineProfiles(profiles []*profile.Profile, msrcs []plugin.MappingSources) (*profile.Profile, plugin.MappingSources, error) {
  169. // Merge profiles.
  170. if err := measurement.ScaleProfiles(profiles); err != nil {
  171. return nil, nil, err
  172. }
  173. p, err := profile.Merge(profiles)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. // Combine mapping sources.
  178. msrc := make(plugin.MappingSources)
  179. for _, ms := range msrcs {
  180. for m, s := range ms {
  181. msrc[m] = append(msrc[m], s...)
  182. }
  183. }
  184. return p, msrc, nil
  185. }
  186. type profileSource struct {
  187. addr string
  188. source *source
  189. scale float64
  190. p *profile.Profile
  191. msrc plugin.MappingSources
  192. remote bool
  193. err error
  194. }
  195. func homeEnv() string {
  196. switch runtime.GOOS {
  197. case "windows":
  198. return "USERPROFILE"
  199. case "plan9":
  200. return "home"
  201. default:
  202. return "HOME"
  203. }
  204. }
  205. // setTmpDir prepares the directory to use to save profiles retrieved
  206. // remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
  207. func setTmpDir(ui plugin.UI) (string, error) {
  208. if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
  209. return profileDir, nil
  210. }
  211. for _, tmpDir := range []string{os.Getenv(homeEnv()) + "/pprof", os.TempDir()} {
  212. if err := os.MkdirAll(tmpDir, 0755); err != nil {
  213. ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
  214. continue
  215. }
  216. return tmpDir, nil
  217. }
  218. return "", fmt.Errorf("failed to identify temp dir")
  219. }
  220. const testSourceAddress = "pproftest.local"
  221. // grabProfile fetches a profile. Returns the profile, sources for the
  222. // profile mappings, a bool indicating if the profile was fetched
  223. // remotely, and an error.
  224. func grabProfile(s *source, source string, scale float64, fetcher plugin.Fetcher, obj plugin.ObjTool, ui plugin.UI) (p *profile.Profile, msrc plugin.MappingSources, remote bool, err error) {
  225. var src string
  226. duration, timeout := time.Duration(s.Seconds)*time.Second, time.Duration(s.Timeout)*time.Second
  227. if fetcher != nil {
  228. p, src, err = fetcher.Fetch(source, duration, timeout)
  229. if err != nil {
  230. return
  231. }
  232. }
  233. if err != nil || p == nil {
  234. // Fetch the profile over HTTP or from a file.
  235. p, src, err = fetch(source, duration, timeout, ui)
  236. if err != nil {
  237. return
  238. }
  239. }
  240. if err = p.CheckValid(); err != nil {
  241. return
  242. }
  243. // Apply local changes to the profile.
  244. p.Scale(scale)
  245. // Update the binary locations from command line and paths.
  246. locateBinaries(p, s, obj, ui)
  247. // Collect the source URL for all mappings.
  248. if src != "" {
  249. msrc = collectMappingSources(p, src)
  250. remote = true
  251. if strings.HasPrefix(src, "http://"+testSourceAddress) {
  252. // Treat test inputs as local to avoid saving
  253. // testcase profiles during driver testing.
  254. remote = false
  255. }
  256. }
  257. return
  258. }
  259. // collectMappingSources saves the mapping sources of a profile.
  260. func collectMappingSources(p *profile.Profile, source string) plugin.MappingSources {
  261. ms := plugin.MappingSources{}
  262. for _, m := range p.Mapping {
  263. src := struct {
  264. Source string
  265. Start uint64
  266. }{
  267. source, m.Start,
  268. }
  269. key := m.BuildID
  270. if key == "" {
  271. key = m.File
  272. }
  273. if key == "" {
  274. // If there is no build id or source file, use the source as the
  275. // mapping file. This will enable remote symbolization for this
  276. // mapping, in particular for Go profiles on the legacy format.
  277. // The source is reset back to empty string by unsourceMapping
  278. // which is called after symbolization is finished.
  279. m.File = source
  280. key = source
  281. }
  282. ms[key] = append(ms[key], src)
  283. }
  284. return ms
  285. }
  286. // unsourceMappings iterates over the mappings in a profile and replaces file
  287. // set to the remote source URL by collectMappingSources back to empty string.
  288. func unsourceMappings(p *profile.Profile) {
  289. for _, m := range p.Mapping {
  290. if m.BuildID == "" {
  291. if u, err := url.Parse(m.File); err == nil && u.IsAbs() {
  292. m.File = ""
  293. }
  294. }
  295. }
  296. }
  297. // locateBinaries searches for binary files listed in the profile and, if found,
  298. // updates the profile accordingly.
  299. func locateBinaries(p *profile.Profile, s *source, obj plugin.ObjTool, ui plugin.UI) {
  300. // Construct search path to examine
  301. searchPath := os.Getenv("PPROF_BINARY_PATH")
  302. if searchPath == "" {
  303. // Use $HOME/pprof/binaries as default directory for local symbolization binaries
  304. searchPath = filepath.Join(os.Getenv(homeEnv()), "pprof", "binaries")
  305. }
  306. mapping:
  307. for _, m := range p.Mapping {
  308. var baseName string
  309. if m.File != "" {
  310. baseName = filepath.Base(m.File)
  311. }
  312. for _, path := range filepath.SplitList(searchPath) {
  313. var fileNames []string
  314. if m.BuildID != "" {
  315. fileNames = []string{filepath.Join(path, m.BuildID, baseName)}
  316. if matches, err := filepath.Glob(filepath.Join(path, m.BuildID, "*")); err == nil {
  317. fileNames = append(fileNames, matches...)
  318. }
  319. }
  320. if m.File != "" {
  321. // Try both the basename and the full path, to support the same directory
  322. // structure as the perf symfs option.
  323. if baseName != "" {
  324. fileNames = append(fileNames, filepath.Join(path, baseName))
  325. }
  326. fileNames = append(fileNames, filepath.Join(path, m.File))
  327. }
  328. for _, name := range fileNames {
  329. if f, err := obj.Open(name, m.Start, m.Limit, m.Offset); err == nil {
  330. defer f.Close()
  331. fileBuildID := f.BuildID()
  332. if m.BuildID != "" && m.BuildID != fileBuildID {
  333. ui.PrintErr("Ignoring local file " + name + ": build-id mismatch (" + m.BuildID + " != " + fileBuildID + ")")
  334. } else {
  335. m.File = name
  336. continue mapping
  337. }
  338. }
  339. }
  340. }
  341. }
  342. if len(p.Mapping) == 0 {
  343. // If there are no mappings, add a fake mapping to attempt symbolization.
  344. // This is useful for some profiles generated by the golang runtime, which
  345. // do not include any mappings. Symbolization with a fake mapping will only
  346. // be successful against a non-PIE binary.
  347. m := &profile.Mapping{ID: 1}
  348. p.Mapping = []*profile.Mapping{m}
  349. for _, l := range p.Location {
  350. l.Mapping = m
  351. }
  352. }
  353. // Replace executable filename/buildID with the overrides from source.
  354. // Assumes the executable is the first Mapping entry.
  355. if execName, buildID := s.ExecName, s.BuildID; execName != "" || buildID != "" {
  356. m := p.Mapping[0]
  357. if execName != "" {
  358. m.File = execName
  359. }
  360. if buildID != "" {
  361. m.BuildID = buildID
  362. }
  363. }
  364. }
  365. // fetch fetches a profile from source, within the timeout specified,
  366. // producing messages through the ui. It returns the profile and the
  367. // url of the actual source of the profile for remote profiles.
  368. func fetch(source string, duration, timeout time.Duration, ui plugin.UI) (p *profile.Profile, src string, err error) {
  369. var f io.ReadCloser
  370. if sourceURL, timeout := adjustURL(source, duration, timeout); sourceURL != "" {
  371. ui.Print("Fetching profile over HTTP from " + sourceURL)
  372. if duration > 0 {
  373. ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
  374. }
  375. f, err = fetchURL(sourceURL, timeout)
  376. src = sourceURL
  377. } else if isPerfFile(source) {
  378. f, err = convertPerfData(source, ui)
  379. } else {
  380. f, err = os.Open(source)
  381. }
  382. if err == nil {
  383. defer f.Close()
  384. p, err = profile.Parse(f)
  385. }
  386. return
  387. }
  388. // fetchURL fetches a profile from a URL using HTTP.
  389. func fetchURL(source string, timeout time.Duration) (io.ReadCloser, error) {
  390. resp, err := httpGet(source, timeout)
  391. if err != nil {
  392. return nil, fmt.Errorf("http fetch: %v", err)
  393. }
  394. if resp.StatusCode != http.StatusOK {
  395. defer resp.Body.Close()
  396. return nil, statusCodeError(resp)
  397. }
  398. return resp.Body, nil
  399. }
  400. func statusCodeError(resp *http.Response) error {
  401. if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
  402. // error is from pprof endpoint
  403. if body, err := ioutil.ReadAll(resp.Body); err == nil {
  404. return fmt.Errorf("server response: %s - %s", resp.Status, body)
  405. }
  406. }
  407. return fmt.Errorf("server response: %s", resp.Status)
  408. }
  409. // isPerfFile checks if a file is in perf.data format. It also returns false
  410. // if it encounters an error during the check.
  411. func isPerfFile(path string) bool {
  412. sourceFile, openErr := os.Open(path)
  413. if openErr != nil {
  414. return false
  415. }
  416. defer sourceFile.Close()
  417. // If the file is the output of a perf record command, it should begin
  418. // with the string PERFILE2.
  419. perfHeader := []byte("PERFILE2")
  420. actualHeader := make([]byte, len(perfHeader))
  421. if _, readErr := sourceFile.Read(actualHeader); readErr != nil {
  422. return false
  423. }
  424. return bytes.Equal(actualHeader, perfHeader)
  425. }
  426. // convertPerfData converts the file at path which should be in perf.data format
  427. // using the perf_to_profile tool and returns the file containing the
  428. // profile.proto formatted data.
  429. func convertPerfData(perfPath string, ui plugin.UI) (*os.File, error) {
  430. ui.Print(fmt.Sprintf(
  431. "Converting %s to a profile.proto... (May take a few minutes)",
  432. perfPath))
  433. profile, err := newTempFile(os.TempDir(), "pprof_", ".pb.gz")
  434. if err != nil {
  435. return nil, err
  436. }
  437. deferDeleteTempFile(profile.Name())
  438. cmd := exec.Command("perf_to_profile", perfPath, profile.Name())
  439. if err := cmd.Run(); err != nil {
  440. profile.Close()
  441. return nil, fmt.Errorf("failed to convert perf.data file. Try github.com/google/perf_data_converter: %v", err)
  442. }
  443. return profile, nil
  444. }
  445. // adjustURL validates if a profile source is a URL and returns an
  446. // cleaned up URL and the timeout to use for retrieval over HTTP.
  447. // If the source cannot be recognized as a URL it returns an empty string.
  448. func adjustURL(source string, duration, timeout time.Duration) (string, time.Duration) {
  449. u, err := url.Parse(source)
  450. if err != nil || (u.Host == "" && u.Scheme != "" && u.Scheme != "file") {
  451. // Try adding http:// to catch sources of the form hostname:port/path.
  452. // url.Parse treats "hostname" as the scheme.
  453. u, err = url.Parse("http://" + source)
  454. }
  455. if err != nil || u.Host == "" {
  456. return "", 0
  457. }
  458. // Apply duration/timeout overrides to URL.
  459. values := u.Query()
  460. if duration > 0 {
  461. values.Set("seconds", fmt.Sprint(int(duration.Seconds())))
  462. } else {
  463. if urlSeconds := values.Get("seconds"); urlSeconds != "" {
  464. if us, err := strconv.ParseInt(urlSeconds, 10, 32); err == nil {
  465. duration = time.Duration(us) * time.Second
  466. }
  467. }
  468. }
  469. if timeout <= 0 {
  470. if duration > 0 {
  471. timeout = duration + duration/2
  472. } else {
  473. timeout = 60 * time.Second
  474. }
  475. }
  476. u.RawQuery = values.Encode()
  477. return u.String(), timeout
  478. }
  479. // httpGet is a wrapper around http.Get; it is defined as a variable
  480. // so it can be redefined during for testing.
  481. var httpGet = func(source string, timeout time.Duration) (*http.Response, error) {
  482. url, err := url.Parse(source)
  483. if err != nil {
  484. return nil, err
  485. }
  486. var tlsConfig *tls.Config
  487. if url.Scheme == "https+insecure" {
  488. tlsConfig = &tls.Config{
  489. InsecureSkipVerify: true,
  490. }
  491. url.Scheme = "https"
  492. source = url.String()
  493. }
  494. client := &http.Client{
  495. Transport: &http.Transport{
  496. ResponseHeaderTimeout: timeout + 5*time.Second,
  497. Proxy: http.ProxyFromEnvironment,
  498. TLSClientConfig: tlsConfig,
  499. },
  500. }
  501. return client.Get(source)
  502. }