暂无描述

fetch.go 14KB

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