暂无描述

fetch.go 13KB

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