Sin descripción

fetch.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. // grabProfile fetches a profile. Returns the profile, sources for the
  221. // profile mappings, a bool indicating if the profile was fetched
  222. // remotely, and an error.
  223. 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) {
  224. var src string
  225. duration, timeout := time.Duration(s.Seconds)*time.Second, time.Duration(s.Timeout)*time.Second
  226. if fetcher != nil {
  227. p, src, err = fetcher.Fetch(source, duration, timeout)
  228. if err != nil {
  229. return
  230. }
  231. }
  232. if err != nil || p == nil {
  233. // Fetch the profile over HTTP or from a file.
  234. p, src, err = fetch(source, duration, timeout, ui)
  235. if err != nil {
  236. return
  237. }
  238. }
  239. if err = p.CheckValid(); err != nil {
  240. return
  241. }
  242. // Apply local changes to the profile.
  243. p.Scale(scale)
  244. // Update the binary locations from command line and paths.
  245. locateBinaries(p, s, obj, ui)
  246. // Collect the source URL for all mappings.
  247. if src != "" {
  248. msrc = collectMappingSources(p, src)
  249. remote = true
  250. }
  251. return
  252. }
  253. // collectMappingSources saves the mapping sources of a profile.
  254. func collectMappingSources(p *profile.Profile, source string) plugin.MappingSources {
  255. ms := plugin.MappingSources{}
  256. for _, m := range p.Mapping {
  257. src := struct {
  258. Source string
  259. Start uint64
  260. }{
  261. source, m.Start,
  262. }
  263. key := m.BuildID
  264. if key == "" {
  265. key = m.File
  266. }
  267. if key == "" {
  268. // If there is no build id or source file, use the source as the
  269. // mapping file. This will enable remote symbolization for this
  270. // mapping, in particular for Go profiles on the legacy format.
  271. // The source is reset back to empty string by unsourceMapping
  272. // which is called after symbolization is finished.
  273. m.File = source
  274. key = source
  275. }
  276. ms[key] = append(ms[key], src)
  277. }
  278. return ms
  279. }
  280. // unsourceMappings iterates over the mappings in a profile and replaces file
  281. // set to the remote source URL by collectMappingSources back to empty string.
  282. func unsourceMappings(p *profile.Profile) {
  283. for _, m := range p.Mapping {
  284. if m.BuildID == "" {
  285. if u, err := url.Parse(m.File); err == nil && u.IsAbs() {
  286. m.File = ""
  287. }
  288. }
  289. }
  290. }
  291. // locateBinaries searches for binary files listed in the profile and, if found,
  292. // updates the profile accordingly.
  293. func locateBinaries(p *profile.Profile, s *source, obj plugin.ObjTool, ui plugin.UI) {
  294. // Construct search path to examine
  295. searchPath := os.Getenv("PPROF_BINARY_PATH")
  296. if searchPath == "" {
  297. // Use $HOME/pprof/binaries as default directory for local symbolization binaries
  298. searchPath = filepath.Join(os.Getenv(homeEnv()), "pprof", "binaries")
  299. }
  300. mapping:
  301. for _, m := range p.Mapping {
  302. var baseName string
  303. if m.File != "" {
  304. baseName = filepath.Base(m.File)
  305. }
  306. for _, path := range filepath.SplitList(searchPath) {
  307. var fileNames []string
  308. if m.BuildID != "" {
  309. fileNames = []string{filepath.Join(path, m.BuildID, baseName)}
  310. if matches, err := filepath.Glob(filepath.Join(path, m.BuildID, "*")); err == nil {
  311. fileNames = append(fileNames, matches...)
  312. }
  313. }
  314. if baseName != "" {
  315. fileNames = append(fileNames, filepath.Join(path, baseName))
  316. }
  317. for _, name := range fileNames {
  318. if f, err := obj.Open(name, m.Start, m.Limit, m.Offset); err == nil {
  319. defer f.Close()
  320. fileBuildID := f.BuildID()
  321. if m.BuildID != "" && m.BuildID != fileBuildID {
  322. ui.PrintErr("Ignoring local file " + name + ": build-id mismatch (" + m.BuildID + " != " + fileBuildID + ")")
  323. } else {
  324. m.File = name
  325. continue mapping
  326. }
  327. }
  328. }
  329. }
  330. }
  331. // Replace executable filename/buildID with the overrides from source.
  332. // Assumes the executable is the first Mapping entry.
  333. if execName, buildID := s.ExecName, s.BuildID; execName != "" || buildID != "" {
  334. if len(p.Mapping) == 0 {
  335. // If there are no mappings, add a fake mapping to attempt symbolization.
  336. // This is useful for some profiles generated by the golang runtime, which
  337. // do not include any mappings. Symbolization with a fake mapping will only
  338. // be successful against a non-PIE binary.
  339. m := &profile.Mapping{ID: 1}
  340. p.Mapping = []*profile.Mapping{m}
  341. for _, l := range p.Location {
  342. l.Mapping = m
  343. }
  344. }
  345. m := p.Mapping[0]
  346. if execName != "" {
  347. m.File = execName
  348. }
  349. if buildID != "" {
  350. m.BuildID = buildID
  351. }
  352. }
  353. }
  354. // fetch fetches a profile from source, within the timeout specified,
  355. // producing messages through the ui. It returns the profile and the
  356. // url of the actual source of the profile for remote profiles.
  357. func fetch(source string, duration, timeout time.Duration, ui plugin.UI) (p *profile.Profile, src string, err error) {
  358. var f io.ReadCloser
  359. if sourceURL, timeout := adjustURL(source, duration, timeout); sourceURL != "" {
  360. ui.Print("Fetching profile over HTTP from " + sourceURL)
  361. if duration > 0 {
  362. ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
  363. }
  364. f, err = fetchURL(sourceURL, timeout)
  365. src = sourceURL
  366. } else if isPerfFile(source) {
  367. f, err = convertPerfData(source, ui)
  368. } else {
  369. f, err = os.Open(source)
  370. }
  371. if err == nil {
  372. defer f.Close()
  373. p, err = profile.Parse(f)
  374. }
  375. return
  376. }
  377. // fetchURL fetches a profile from a URL using HTTP.
  378. func fetchURL(source string, timeout time.Duration) (io.ReadCloser, error) {
  379. resp, err := httpGet(source, timeout)
  380. if err != nil {
  381. return nil, fmt.Errorf("http fetch: %v", err)
  382. }
  383. if resp.StatusCode != http.StatusOK {
  384. defer resp.Body.Close()
  385. return nil, statusCodeError(resp)
  386. }
  387. return resp.Body, nil
  388. }
  389. func statusCodeError(resp *http.Response) error {
  390. if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
  391. // error is from pprof endpoint
  392. if body, err := ioutil.ReadAll(resp.Body); err == nil {
  393. return fmt.Errorf("server response: %s - %s", resp.Status, body)
  394. }
  395. }
  396. return fmt.Errorf("server response: %s", resp.Status)
  397. }
  398. // isPerfFile checks if a file is in perf.data format. It also returns false
  399. // if it encounters an error during the check.
  400. func isPerfFile(path string) bool {
  401. sourceFile, openErr := os.Open(path)
  402. if openErr != nil {
  403. return false
  404. }
  405. defer sourceFile.Close()
  406. // If the file is the output of a perf record command, it should begin
  407. // with the string PERFILE2.
  408. perfHeader := []byte("PERFILE2")
  409. actualHeader := make([]byte, len(perfHeader))
  410. if _, readErr := sourceFile.Read(actualHeader); readErr != nil {
  411. return false
  412. }
  413. return bytes.Equal(actualHeader, perfHeader)
  414. }
  415. // convertPerfData converts the file at path which should be in perf.data format
  416. // using the perf_to_profile tool and returns the file containing the
  417. // profile.proto formatted data.
  418. func convertPerfData(perfPath string, ui plugin.UI) (*os.File, error) {
  419. ui.Print(fmt.Sprintf(
  420. "Converting %s to a profile.proto... (May take a few minutes)",
  421. perfPath))
  422. profile, err := newTempFile(os.TempDir(), "pprof_", ".pb.gz")
  423. if err != nil {
  424. return nil, err
  425. }
  426. deferDeleteTempFile(profile.Name())
  427. cmd := exec.Command("perf_to_profile", perfPath, profile.Name())
  428. if err := cmd.Run(); err != nil {
  429. profile.Close()
  430. return nil, fmt.Errorf("failed to convert perf.data file. Try github.com/google/perf_data_converter: %v", err)
  431. }
  432. return profile, nil
  433. }
  434. // adjustURL validates if a profile source is a URL and returns an
  435. // cleaned up URL and the timeout to use for retrieval over HTTP.
  436. // If the source cannot be recognized as a URL it returns an empty string.
  437. func adjustURL(source string, duration, timeout time.Duration) (string, time.Duration) {
  438. u, err := url.Parse(source)
  439. if err != nil || (u.Host == "" && u.Scheme != "" && u.Scheme != "file") {
  440. // Try adding http:// to catch sources of the form hostname:port/path.
  441. // url.Parse treats "hostname" as the scheme.
  442. u, err = url.Parse("http://" + source)
  443. }
  444. if err != nil || u.Host == "" {
  445. return "", 0
  446. }
  447. // Apply duration/timeout overrides to URL.
  448. values := u.Query()
  449. if duration > 0 {
  450. values.Set("seconds", fmt.Sprint(int(duration.Seconds())))
  451. } else {
  452. if urlSeconds := values.Get("seconds"); urlSeconds != "" {
  453. if us, err := strconv.ParseInt(urlSeconds, 10, 32); err == nil {
  454. duration = time.Duration(us) * time.Second
  455. }
  456. }
  457. }
  458. if timeout <= 0 {
  459. if duration > 0 {
  460. timeout = duration + duration/2
  461. } else {
  462. timeout = 60 * time.Second
  463. }
  464. }
  465. u.RawQuery = values.Encode()
  466. return u.String(), timeout
  467. }
  468. // httpGet is a wrapper around http.Get; it is defined as a variable
  469. // so it can be redefined during for testing.
  470. var httpGet = func(source string, timeout time.Duration) (*http.Response, error) {
  471. url, err := url.Parse(source)
  472. if err != nil {
  473. return nil, err
  474. }
  475. var tlsConfig *tls.Config
  476. if url.Scheme == "https+insecure" {
  477. tlsConfig = &tls.Config{
  478. InsecureSkipVerify: true,
  479. }
  480. url.Scheme = "https"
  481. source = url.String()
  482. }
  483. client := &http.Client{
  484. Transport: &http.Transport{
  485. ResponseHeaderTimeout: timeout + 5*time.Second,
  486. Proxy: http.ProxyFromEnvironment,
  487. TLSClientConfig: tlsConfig,
  488. },
  489. }
  490. return client.Get(source)
  491. }