暫無描述

driver_test.go 26KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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/ioutil"
  19. "os"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "time"
  24. "internal/binutils"
  25. "internal/plugin"
  26. "profile"
  27. "internal/proftest"
  28. "internal/symbolz"
  29. )
  30. func TestParse(t *testing.T) {
  31. // Override weblist command to collect output in buffer
  32. pprofCommands["weblist"].postProcess = nil
  33. // Update PATH to use fake dot for svg output
  34. os.Setenv("PATH", "testdata/wrapper"+":"+os.Getenv("PATH"))
  35. testcase := []struct {
  36. flags, source string
  37. }{
  38. {"text,functions,flat", "cpu"},
  39. {"tree,addresses,flat,nodecount=4", "cpusmall"},
  40. {"text,functions,flat", "unknown"},
  41. {"text,alloc_objects,flat", "heap_alloc"},
  42. {"text,files,flat", "heap"},
  43. {"text,inuse_objects,flat", "heap"},
  44. {"text,lines,cum,hide=line[X3]0", "cpu"},
  45. {"text,lines,cum,show=[12]00", "cpu"},
  46. {"topproto,lines,cum,hide=mangled[X3]0", "cpu"},
  47. {"tree,lines,cum,focus=[24]00", "heap"},
  48. {"tree,relative_percentages,cum,focus=[24]00", "heap"},
  49. {"callgrind", "cpu"},
  50. {"callgrind", "heap"},
  51. {"dot,functions,flat", "cpu"},
  52. {"dot,lines,flat,focus=[12]00", "heap"},
  53. {"dot,addresses,flat,ignore=[X3]002,focus=[X1]000", "contention"},
  54. {"dot,files,cum", "contention"},
  55. {"svg", "cpu"},
  56. {"tags", "cpu"},
  57. {"tags,tagignore=tag[13],tagfocus=key[12]", "cpu"},
  58. {"traces", "cpu"},
  59. {"dot,alloc_space,flat,focus=[234]00", "heap_alloc"},
  60. {"dot,alloc_space,flat,hide=line.*1?23?", "heap_alloc"},
  61. {"dot,inuse_space,flat,tagfocus=1mb:2gb", "heap"},
  62. {"dot,inuse_space,flat,tagfocus=30kb:,tagignore=1mb:2mb", "heap"},
  63. {"disasm=line[13],addresses,flat", "cpu"},
  64. {"peek=line.*01", "cpu"},
  65. {"weblist=line[13],addresses,flat", "cpu"},
  66. }
  67. baseVars := pprofVariables
  68. defer func() { pprofVariables = baseVars }()
  69. for _, tc := range testcase {
  70. // Reset the pprof variables before processing
  71. pprofVariables = baseVars.makeCopy()
  72. f := baseFlags()
  73. f.args = []string{tc.source}
  74. flags := strings.Split(tc.flags, ",")
  75. // Skip the output format in the first flag, to output to a proto
  76. addFlags(&f, flags[1:])
  77. // Encode profile into a protobuf and decode it again.
  78. protoTempFile, err := ioutil.TempFile("", "profile_proto")
  79. if err != nil {
  80. t.Errorf("cannot create tempfile: %v", err)
  81. }
  82. defer protoTempFile.Close()
  83. f.strings["output"] = protoTempFile.Name()
  84. if flags[0] == "topproto" {
  85. f.bools["proto"] = false
  86. f.bools["topproto"] = true
  87. }
  88. // First pprof invocation to save the profile into a profile.proto.
  89. o1 := setDefaults(nil)
  90. o1.Flagset = f
  91. o1.Fetch = testFetcher{}
  92. o1.Sym = testSymbolizer{}
  93. if err := PProf(o1); err != nil {
  94. t.Errorf("%s %q: %v", tc.source, tc.flags, err)
  95. continue
  96. }
  97. // Reset the pprof variables after the proto invocation
  98. pprofVariables = baseVars.makeCopy()
  99. // Read the profile from the encoded protobuf
  100. outputTempFile, err := ioutil.TempFile("", "profile_output")
  101. if err != nil {
  102. t.Errorf("cannot create tempfile: %v", err)
  103. }
  104. defer outputTempFile.Close()
  105. f.strings["output"] = outputTempFile.Name()
  106. f.args = []string{protoTempFile.Name()}
  107. var solution string
  108. // Apply the flags for the second pprof run, and identify name of
  109. // the file containing expected results
  110. if flags[0] == "topproto" {
  111. solution = solutionFilename(tc.source, &f)
  112. delete(f.bools, "topproto")
  113. f.bools["text"] = true
  114. } else {
  115. delete(f.bools, "proto")
  116. addFlags(&f, flags[:1])
  117. solution = solutionFilename(tc.source, &f)
  118. }
  119. // Second pprof invocation to read the profile from profile.proto
  120. // and generate a report.
  121. o2 := setDefaults(nil)
  122. o2.Flagset = f
  123. o2.Sym = testSymbolizeDemangler{}
  124. o2.Obj = new(binutils.Binutils)
  125. if err := PProf(o2); err != nil {
  126. t.Errorf("%s: %v", tc.source, err)
  127. }
  128. b, err := ioutil.ReadFile(outputTempFile.Name())
  129. if err != nil {
  130. t.Errorf("Failed to read profile %s: %v", outputTempFile.Name(), err)
  131. }
  132. // Read data file with expected solution
  133. solution = "testdata/" + solution
  134. sbuf, err := ioutil.ReadFile(solution)
  135. if err != nil {
  136. t.Errorf("reading solution file %s: %v", solution, err)
  137. continue
  138. }
  139. if flags[0] == "svg" {
  140. b = removeScripts(b)
  141. sbuf = removeScripts(sbuf)
  142. }
  143. if string(b) != string(sbuf) {
  144. t.Errorf("diff %s %s", solution, tc.source)
  145. d, err := proftest.Diff(sbuf, b)
  146. if err != nil {
  147. t.Fatalf("diff %s %v", solution, err)
  148. }
  149. t.Errorf("%s\n%s\n", solution, d)
  150. }
  151. }
  152. }
  153. // removeScripts removes <script > .. </script> pairs from its input
  154. func removeScripts(in []byte) []byte {
  155. beginMarker := []byte("<script")
  156. endMarker := []byte("</script>")
  157. if begin := bytes.Index(in, beginMarker); begin > 0 {
  158. if end := bytes.Index(in[begin:], endMarker); end > 0 {
  159. in = append(in[:begin], removeScripts(in[begin+end+len(endMarker):])...)
  160. }
  161. }
  162. return in
  163. }
  164. // addFlags parses flag descriptions and adds them to the testFlags
  165. func addFlags(f *testFlags, flags []string) {
  166. for _, flag := range flags {
  167. fields := strings.SplitN(flag, "=", 2)
  168. switch len(fields) {
  169. case 1:
  170. f.bools[fields[0]] = true
  171. case 2:
  172. if i, err := strconv.Atoi(fields[1]); err == nil {
  173. f.ints[fields[0]] = i
  174. } else {
  175. f.strings[fields[0]] = fields[1]
  176. }
  177. }
  178. }
  179. }
  180. // solutionFilename returns the name of the solution file for the test
  181. func solutionFilename(source string, f *testFlags) string {
  182. name := []string{"pprof", strings.TrimPrefix(source, "http://host:8000/")}
  183. name = addString(name, f, []string{"flat", "cum"})
  184. name = addString(name, f, []string{"functions", "files", "lines", "addresses"})
  185. name = addString(name, f, []string{"inuse_space", "inuse_objects", "alloc_space", "alloc_objects"})
  186. name = addString(name, f, []string{"relative_percentages"})
  187. name = addString(name, f, []string{"seconds"})
  188. name = addString(name, f, []string{"text", "tree", "callgrind", "dot", "svg", "tags", "dot", "traces", "disasm", "peek", "weblist", "topproto"})
  189. if f.strings["focus"] != "" || f.strings["tagfocus"] != "" {
  190. name = append(name, "focus")
  191. }
  192. if f.strings["ignore"] != "" || f.strings["tagignore"] != "" {
  193. name = append(name, "ignore")
  194. }
  195. name = addString(name, f, []string{"hide", "show"})
  196. return strings.Join(name, ".")
  197. }
  198. func addString(name []string, f *testFlags, components []string) []string {
  199. for _, c := range components {
  200. if f.bools[c] || f.strings[c] != "" || f.ints[c] != 0 {
  201. return append(name, c)
  202. }
  203. }
  204. return name
  205. }
  206. // testFlags implements the plugin.FlagSet interface.
  207. type testFlags struct {
  208. bools map[string]bool
  209. ints map[string]int
  210. floats map[string]float64
  211. strings map[string]string
  212. args []string
  213. }
  214. func (testFlags) ExtraUsage() string { return "" }
  215. func (f testFlags) Bool(s string, d bool, c string) *bool {
  216. if b, ok := f.bools[s]; ok {
  217. return &b
  218. }
  219. return &d
  220. }
  221. func (f testFlags) Int(s string, d int, c string) *int {
  222. if i, ok := f.ints[s]; ok {
  223. return &i
  224. }
  225. return &d
  226. }
  227. func (f testFlags) Float64(s string, d float64, c string) *float64 {
  228. if g, ok := f.floats[s]; ok {
  229. return &g
  230. }
  231. return &d
  232. }
  233. func (f testFlags) String(s, d, c string) *string {
  234. if t, ok := f.strings[s]; ok {
  235. return &t
  236. }
  237. return &d
  238. }
  239. func (f testFlags) BoolVar(p *bool, s string, d bool, c string) {
  240. if b, ok := f.bools[s]; ok {
  241. *p = b
  242. } else {
  243. *p = d
  244. }
  245. }
  246. func (f testFlags) IntVar(p *int, s string, d int, c string) {
  247. if i, ok := f.ints[s]; ok {
  248. *p = i
  249. } else {
  250. *p = d
  251. }
  252. }
  253. func (f testFlags) Float64Var(p *float64, s string, d float64, c string) {
  254. if g, ok := f.floats[s]; ok {
  255. *p = g
  256. } else {
  257. *p = d
  258. }
  259. }
  260. func (f testFlags) StringVar(p *string, s, d, c string) {
  261. if t, ok := f.strings[s]; ok {
  262. *p = t
  263. } else {
  264. *p = d
  265. }
  266. }
  267. func (f testFlags) StringList(s, d, c string) *[]*string {
  268. return &[]*string{}
  269. }
  270. func (f testFlags) Parse(func()) []string {
  271. return f.args
  272. }
  273. func baseFlags() testFlags {
  274. return testFlags{
  275. bools: map[string]bool{
  276. "proto": true,
  277. "trim": true,
  278. "compact_labels": true,
  279. },
  280. ints: map[string]int{
  281. "nodecount": 20,
  282. },
  283. floats: map[string]float64{
  284. "nodefraction": 0.05,
  285. "edgefraction": 0.01,
  286. "divide_by": 1.0,
  287. },
  288. strings: map[string]string{
  289. "tools": "testdata/wrapper",
  290. "unit": "minimum",
  291. },
  292. }
  293. }
  294. type testProfile struct {
  295. }
  296. const testStart = 0x1000
  297. const testOffset = 0x5000
  298. type testFetcher struct{}
  299. func (testFetcher) Fetch(s string, d, t time.Duration) (*profile.Profile, string, error) {
  300. var p *profile.Profile
  301. s = strings.TrimPrefix(s, "http://host:8000/")
  302. switch s {
  303. case "cpu", "unknown":
  304. p = cpuProfile()
  305. case "cpusmall":
  306. p = cpuProfileSmall()
  307. case "heap":
  308. p = heapProfile()
  309. case "heap_alloc":
  310. p = heapProfile()
  311. p.SampleType = []*profile.ValueType{
  312. {Type: "alloc_objects", Unit: "count"},
  313. {Type: "alloc_space", Unit: "bytes"},
  314. }
  315. case "contention":
  316. p = contentionProfile()
  317. case "symbolz":
  318. p = symzProfile()
  319. case "http://host2/symbolz":
  320. p = symzProfile()
  321. p.Mapping[0].Start += testOffset
  322. p.Mapping[0].Limit += testOffset
  323. for i := range p.Location {
  324. p.Location[i].Address += testOffset
  325. }
  326. default:
  327. return nil, "", fmt.Errorf("unexpected source: %s", s)
  328. }
  329. return p, s, nil
  330. }
  331. type testSymbolizer struct{}
  332. func (testSymbolizer) Symbolize(_ string, _ plugin.MappingSources, _ *profile.Profile) error {
  333. return nil
  334. }
  335. type testSymbolizeDemangler struct{}
  336. func (testSymbolizeDemangler) Symbolize(_ string, _ plugin.MappingSources, p *profile.Profile) error {
  337. for _, fn := range p.Function {
  338. if fn.Name == "" || fn.SystemName == fn.Name {
  339. fn.Name = fakeDemangler(fn.SystemName)
  340. }
  341. }
  342. return nil
  343. }
  344. func testFetchSymbols(source, post string) ([]byte, error) {
  345. var buf bytes.Buffer
  346. if source == "http://host2/symbolz" {
  347. for _, address := range strings.Split(post, "+") {
  348. a, _ := strconv.ParseInt(address, 0, 64)
  349. fmt.Fprintf(&buf, "%v\t", address)
  350. if a-testStart < testOffset {
  351. fmt.Fprintf(&buf, "wrong_source_%v_", address)
  352. continue
  353. }
  354. fmt.Fprintf(&buf, "%#x\n", a-testStart-testOffset)
  355. }
  356. return buf.Bytes(), nil
  357. }
  358. for _, address := range strings.Split(post, "+") {
  359. a, _ := strconv.ParseInt(address, 0, 64)
  360. fmt.Fprintf(&buf, "%v\t", address)
  361. if a-testStart > testOffset {
  362. fmt.Fprintf(&buf, "wrong_source_%v_", address)
  363. continue
  364. }
  365. fmt.Fprintf(&buf, "%#x\n", a-testStart)
  366. }
  367. return buf.Bytes(), nil
  368. }
  369. type testSymbolzSymbolizer struct{}
  370. func (testSymbolzSymbolizer) Symbolize(variables string, sources plugin.MappingSources, p *profile.Profile) error {
  371. return symbolz.Symbolize(sources, testFetchSymbols, p, nil)
  372. }
  373. func fakeDemangler(name string) string {
  374. switch name {
  375. case "mangled1000":
  376. return "line1000"
  377. case "mangled2000":
  378. return "line2000"
  379. case "mangled2001":
  380. return "line2001"
  381. case "mangled3000":
  382. return "line3000"
  383. case "mangled3001":
  384. return "line3001"
  385. case "mangled3002":
  386. return "line3002"
  387. case "mangledNEW":
  388. return "operator new"
  389. case "mangledMALLOC":
  390. return "malloc"
  391. default:
  392. return name
  393. }
  394. }
  395. func cpuProfile() *profile.Profile {
  396. var cpuM = []*profile.Mapping{
  397. {
  398. ID: 1,
  399. Start: 0x1000,
  400. Limit: 0x4000,
  401. File: "testbinary",
  402. HasFunctions: true,
  403. HasFilenames: true,
  404. HasLineNumbers: true,
  405. HasInlineFrames: true,
  406. },
  407. }
  408. var cpuF = []*profile.Function{
  409. {ID: 1, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  410. {ID: 2, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  411. {ID: 3, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  412. {ID: 4, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  413. {ID: 5, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  414. {ID: 6, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  415. }
  416. var cpuL = []*profile.Location{
  417. {
  418. ID: 1000,
  419. Mapping: cpuM[0],
  420. Address: 0x1000,
  421. Line: []profile.Line{
  422. {Function: cpuF[0], Line: 1},
  423. },
  424. },
  425. {
  426. ID: 2000,
  427. Mapping: cpuM[0],
  428. Address: 0x2000,
  429. Line: []profile.Line{
  430. {Function: cpuF[2], Line: 9},
  431. {Function: cpuF[1], Line: 4},
  432. },
  433. },
  434. {
  435. ID: 3000,
  436. Mapping: cpuM[0],
  437. Address: 0x3000,
  438. Line: []profile.Line{
  439. {Function: cpuF[5], Line: 2},
  440. {Function: cpuF[4], Line: 5},
  441. {Function: cpuF[3], Line: 6},
  442. },
  443. },
  444. {
  445. ID: 3001,
  446. Mapping: cpuM[0],
  447. Address: 0x3001,
  448. Line: []profile.Line{
  449. {Function: cpuF[4], Line: 8},
  450. {Function: cpuF[3], Line: 9},
  451. },
  452. },
  453. {
  454. ID: 3002,
  455. Mapping: cpuM[0],
  456. Address: 0x3002,
  457. Line: []profile.Line{
  458. {Function: cpuF[5], Line: 5},
  459. {Function: cpuF[3], Line: 7},
  460. },
  461. },
  462. }
  463. return &profile.Profile{
  464. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  465. Period: 1,
  466. DurationNanos: 10e9,
  467. SampleType: []*profile.ValueType{
  468. {Type: "samples", Unit: "count"},
  469. {Type: "cpu", Unit: "milliseconds"},
  470. },
  471. Sample: []*profile.Sample{
  472. {
  473. Location: []*profile.Location{cpuL[0], cpuL[1], cpuL[2]},
  474. Value: []int64{1000, 1000},
  475. Label: map[string][]string{
  476. "key1": []string{"tag1"},
  477. "key2": []string{"tag1"},
  478. },
  479. },
  480. {
  481. Location: []*profile.Location{cpuL[0], cpuL[3]},
  482. Value: []int64{100, 100},
  483. Label: map[string][]string{
  484. "key1": []string{"tag2"},
  485. "key3": []string{"tag2"},
  486. },
  487. },
  488. {
  489. Location: []*profile.Location{cpuL[1], cpuL[4]},
  490. Value: []int64{10, 10},
  491. Label: map[string][]string{
  492. "key1": []string{"tag3"},
  493. "key2": []string{"tag2"},
  494. },
  495. },
  496. {
  497. Location: []*profile.Location{cpuL[2]},
  498. Value: []int64{10, 10},
  499. Label: map[string][]string{
  500. "key1": []string{"tag4"},
  501. "key2": []string{"tag1"},
  502. },
  503. },
  504. },
  505. Location: cpuL,
  506. Function: cpuF,
  507. Mapping: cpuM,
  508. }
  509. }
  510. func cpuProfileSmall() *profile.Profile {
  511. var cpuM = []*profile.Mapping{
  512. {
  513. ID: 1,
  514. Start: 0x1000,
  515. Limit: 0x4000,
  516. File: "testbinary",
  517. HasFunctions: true,
  518. HasFilenames: true,
  519. HasLineNumbers: true,
  520. HasInlineFrames: true,
  521. },
  522. }
  523. var cpuL = []*profile.Location{
  524. {
  525. ID: 1000,
  526. Mapping: cpuM[0],
  527. Address: 0x1000,
  528. },
  529. {
  530. ID: 2000,
  531. Mapping: cpuM[0],
  532. Address: 0x2000,
  533. },
  534. {
  535. ID: 3000,
  536. Mapping: cpuM[0],
  537. Address: 0x3000,
  538. },
  539. {
  540. ID: 4000,
  541. Mapping: cpuM[0],
  542. Address: 0x4000,
  543. },
  544. {
  545. ID: 5000,
  546. Mapping: cpuM[0],
  547. Address: 0x5000,
  548. },
  549. }
  550. return &profile.Profile{
  551. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  552. Period: 1,
  553. DurationNanos: 10e9,
  554. SampleType: []*profile.ValueType{
  555. {Type: "samples", Unit: "count"},
  556. {Type: "cpu", Unit: "milliseconds"},
  557. },
  558. Sample: []*profile.Sample{
  559. {
  560. Location: []*profile.Location{cpuL[0], cpuL[1], cpuL[2]},
  561. Value: []int64{1000, 1000},
  562. },
  563. {
  564. Location: []*profile.Location{cpuL[3], cpuL[1], cpuL[4]},
  565. Value: []int64{1000, 1000},
  566. },
  567. {
  568. Location: []*profile.Location{cpuL[2]},
  569. Value: []int64{1000, 1000},
  570. },
  571. {
  572. Location: []*profile.Location{cpuL[4]},
  573. Value: []int64{1000, 1000},
  574. },
  575. },
  576. Location: cpuL,
  577. Function: nil,
  578. Mapping: cpuM,
  579. }
  580. }
  581. func heapProfile() *profile.Profile {
  582. var heapM = []*profile.Mapping{
  583. {
  584. ID: 1,
  585. BuildID: "buildid",
  586. Start: 0x1000,
  587. Limit: 0x4000,
  588. HasFunctions: true,
  589. HasFilenames: true,
  590. HasLineNumbers: true,
  591. HasInlineFrames: true,
  592. },
  593. }
  594. var heapF = []*profile.Function{
  595. {ID: 1, Name: "pruneme", SystemName: "pruneme", Filename: "prune.h"},
  596. {ID: 2, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  597. {ID: 3, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  598. {ID: 4, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  599. {ID: 5, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  600. {ID: 6, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  601. {ID: 7, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  602. {ID: 8, Name: "mangledMALLOC", SystemName: "mangledMALLOC", Filename: "malloc.h"},
  603. {ID: 9, Name: "mangledNEW", SystemName: "mangledNEW", Filename: "new.h"},
  604. }
  605. var heapL = []*profile.Location{
  606. {
  607. ID: 1000,
  608. Mapping: heapM[0],
  609. Address: 0x1000,
  610. Line: []profile.Line{
  611. {Function: heapF[0], Line: 100},
  612. {Function: heapF[7], Line: 100},
  613. {Function: heapF[1], Line: 1},
  614. },
  615. },
  616. {
  617. ID: 2000,
  618. Mapping: heapM[0],
  619. Address: 0x2000,
  620. Line: []profile.Line{
  621. {Function: heapF[8], Line: 100},
  622. {Function: heapF[3], Line: 2},
  623. {Function: heapF[2], Line: 3},
  624. },
  625. },
  626. {
  627. ID: 3000,
  628. Mapping: heapM[0],
  629. Address: 0x3000,
  630. Line: []profile.Line{
  631. {Function: heapF[8], Line: 100},
  632. {Function: heapF[6], Line: 3},
  633. {Function: heapF[5], Line: 2},
  634. {Function: heapF[4], Line: 4},
  635. },
  636. },
  637. {
  638. ID: 3001,
  639. Mapping: heapM[0],
  640. Address: 0x3001,
  641. Line: []profile.Line{
  642. {Function: heapF[0], Line: 100},
  643. {Function: heapF[8], Line: 100},
  644. {Function: heapF[5], Line: 2},
  645. {Function: heapF[4], Line: 4},
  646. },
  647. },
  648. {
  649. ID: 3002,
  650. Mapping: heapM[0],
  651. Address: 0x3002,
  652. Line: []profile.Line{
  653. {Function: heapF[6], Line: 3},
  654. {Function: heapF[4], Line: 4},
  655. },
  656. },
  657. }
  658. return &profile.Profile{
  659. PeriodType: &profile.ValueType{Type: "allocations", Unit: "bytes"},
  660. Period: 524288,
  661. SampleType: []*profile.ValueType{
  662. {Type: "inuse_objects", Unit: "count"},
  663. {Type: "inuse_space", Unit: "bytes"},
  664. },
  665. Sample: []*profile.Sample{
  666. {
  667. Location: []*profile.Location{heapL[0], heapL[1], heapL[2]},
  668. Value: []int64{10, 1024000},
  669. NumLabel: map[string][]int64{
  670. "bytes": []int64{102400},
  671. },
  672. },
  673. {
  674. Location: []*profile.Location{heapL[0], heapL[3]},
  675. Value: []int64{20, 4096000},
  676. NumLabel: map[string][]int64{
  677. "bytes": []int64{204800},
  678. },
  679. },
  680. {
  681. Location: []*profile.Location{heapL[1], heapL[4]},
  682. Value: []int64{40, 65536000},
  683. NumLabel: map[string][]int64{
  684. "bytes": []int64{1638400},
  685. },
  686. },
  687. {
  688. Location: []*profile.Location{heapL[2]},
  689. Value: []int64{80, 32768000},
  690. NumLabel: map[string][]int64{
  691. "bytes": []int64{409600},
  692. },
  693. },
  694. },
  695. DropFrames: ".*operator new.*|malloc",
  696. Location: heapL,
  697. Function: heapF,
  698. Mapping: heapM,
  699. }
  700. }
  701. func contentionProfile() *profile.Profile {
  702. var contentionM = []*profile.Mapping{
  703. {
  704. ID: 1,
  705. BuildID: "buildid-contention",
  706. Start: 0x1000,
  707. Limit: 0x4000,
  708. HasFunctions: true,
  709. HasFilenames: true,
  710. HasLineNumbers: true,
  711. HasInlineFrames: true,
  712. },
  713. }
  714. var contentionF = []*profile.Function{
  715. {ID: 1, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  716. {ID: 2, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  717. {ID: 3, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  718. {ID: 4, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  719. {ID: 5, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  720. {ID: 6, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  721. }
  722. var contentionL = []*profile.Location{
  723. {
  724. ID: 1000,
  725. Mapping: contentionM[0],
  726. Address: 0x1000,
  727. Line: []profile.Line{
  728. {Function: contentionF[0], Line: 1},
  729. },
  730. },
  731. {
  732. ID: 2000,
  733. Mapping: contentionM[0],
  734. Address: 0x2000,
  735. Line: []profile.Line{
  736. {Function: contentionF[2], Line: 2},
  737. {Function: contentionF[1], Line: 3},
  738. },
  739. },
  740. {
  741. ID: 3000,
  742. Mapping: contentionM[0],
  743. Address: 0x3000,
  744. Line: []profile.Line{
  745. {Function: contentionF[5], Line: 2},
  746. {Function: contentionF[4], Line: 3},
  747. {Function: contentionF[3], Line: 5},
  748. },
  749. },
  750. {
  751. ID: 3001,
  752. Mapping: contentionM[0],
  753. Address: 0x3001,
  754. Line: []profile.Line{
  755. {Function: contentionF[4], Line: 3},
  756. {Function: contentionF[3], Line: 5},
  757. },
  758. },
  759. {
  760. ID: 3002,
  761. Mapping: contentionM[0],
  762. Address: 0x3002,
  763. Line: []profile.Line{
  764. {Function: contentionF[5], Line: 4},
  765. {Function: contentionF[3], Line: 3},
  766. },
  767. },
  768. }
  769. return &profile.Profile{
  770. PeriodType: &profile.ValueType{Type: "contentions", Unit: "count"},
  771. Period: 524288,
  772. SampleType: []*profile.ValueType{
  773. {Type: "contentions", Unit: "count"},
  774. {Type: "delay", Unit: "nanoseconds"},
  775. },
  776. Sample: []*profile.Sample{
  777. {
  778. Location: []*profile.Location{contentionL[0], contentionL[1], contentionL[2]},
  779. Value: []int64{10, 10240000},
  780. },
  781. {
  782. Location: []*profile.Location{contentionL[0], contentionL[3]},
  783. Value: []int64{20, 40960000},
  784. },
  785. {
  786. Location: []*profile.Location{contentionL[1], contentionL[4]},
  787. Value: []int64{40, 65536000},
  788. },
  789. {
  790. Location: []*profile.Location{contentionL[2]},
  791. Value: []int64{80, 32768000},
  792. },
  793. },
  794. Location: contentionL,
  795. Function: contentionF,
  796. Mapping: contentionM,
  797. Comments: []string{"Comment #1", "Comment #2"},
  798. }
  799. }
  800. func symzProfile() *profile.Profile {
  801. var symzM = []*profile.Mapping{
  802. {
  803. ID: 1,
  804. Start: testStart,
  805. Limit: 0x4000,
  806. File: "testbinary",
  807. },
  808. }
  809. var symzL = []*profile.Location{
  810. {ID: 1, Mapping: symzM[0], Address: testStart},
  811. {ID: 2, Mapping: symzM[0], Address: testStart + 0x1000},
  812. {ID: 3, Mapping: symzM[0], Address: testStart + 0x2000},
  813. }
  814. return &profile.Profile{
  815. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  816. Period: 1,
  817. DurationNanos: 10e9,
  818. SampleType: []*profile.ValueType{
  819. {Type: "samples", Unit: "count"},
  820. {Type: "cpu", Unit: "milliseconds"},
  821. },
  822. Sample: []*profile.Sample{
  823. {
  824. Location: []*profile.Location{symzL[0], symzL[1], symzL[2]},
  825. Value: []int64{1, 1},
  826. },
  827. },
  828. Location: symzL,
  829. Mapping: symzM,
  830. }
  831. }
  832. var autoCompleteTests = []struct {
  833. in string
  834. out string
  835. }{
  836. {"", ""},
  837. {"xyz", "xyz"}, // no match
  838. {"dis", "disasm"}, // single match
  839. {"t", "t"}, // many matches
  840. {"top abc", "top abc"}, // no function name match
  841. {"top mangledM", "top mangledMALLOC"}, // single function name match
  842. {"top cmd cmd mangledM", "top cmd cmd mangledMALLOC"},
  843. {"top mangled", "top mangled"}, // many function name matches
  844. {"cmd mangledM", "cmd mangledM"}, // invalid command
  845. {"top mangledM cmd", "top mangledM cmd"}, // cursor misplaced
  846. {"top edMA", "top mangledMALLOC"}, // single infix function name match
  847. {"top -mangledM", "top -mangledMALLOC"}, // ignore sign handled
  848. {"lin", "lines"}, // single variable match
  849. {"EdGeF", "edgefraction"}, // single capitalized match
  850. {"help dis", "help disasm"}, // help command match
  851. {"help relative_perc", "help relative_percentages"}, // help variable match
  852. {"help coMpa", "help compact_labels"}, // help variable capitalized match
  853. }
  854. func TestAutoComplete(t *testing.T) {
  855. complete := newCompleter(functionNames(heapProfile()))
  856. for _, test := range autoCompleteTests {
  857. if out := complete(test.in); out != test.out {
  858. t.Errorf("autoComplete(%s) = %s; want %s", test.in, out, test.out)
  859. }
  860. }
  861. }
  862. func TestTagFilter(t *testing.T) {
  863. var tagFilterTests = []struct {
  864. name, value string
  865. tags map[string][]string
  866. want bool
  867. }{
  868. {"test1", "tag2", map[string][]string{"value1": {"tag1", "tag2"}}, true},
  869. {"test2", "tag3", map[string][]string{"value1": {"tag1", "tag2"}}, false},
  870. {"test3", "tag1,tag3", map[string][]string{"value1": {"tag1", "tag2"}, "value2": {"tag3"}}, true},
  871. {"test4", "t..[12],t..3", map[string][]string{"value1": {"tag1", "tag2"}, "value2": {"tag3"}}, true},
  872. {"test5", "tag2,tag3", map[string][]string{"value1": {"tag1", "tag2"}}, false},
  873. }
  874. for _, test := range tagFilterTests {
  875. filter, err := compileTagFilter(test.name, test.value, &proftest.TestUI{T: t}, nil)
  876. if err != nil {
  877. t.Errorf("tagFilter %s:%v", test.name, err)
  878. continue
  879. }
  880. s := profile.Sample{
  881. Label: test.tags,
  882. }
  883. if got := filter(&s); got != test.want {
  884. t.Errorf("tagFilter %s: got %v, want %v", test.name, got, test.want)
  885. }
  886. }
  887. }
  888. func TestSymbolzAfterMerge(t *testing.T) {
  889. baseVars := pprofVariables
  890. pprofVariables = baseVars.makeCopy()
  891. defer func() { pprofVariables = baseVars }()
  892. f := baseFlags()
  893. f.args = []string{"symbolz", "http://host2/symbolz"}
  894. o := setDefaults(nil)
  895. o.Flagset = f
  896. o.Obj = new(binutils.Binutils)
  897. src, cmd, err := parseFlags(o)
  898. if err != nil {
  899. t.Fatalf("parseFlags: %v", err)
  900. }
  901. if len(cmd) != 1 || cmd[0] != "proto" {
  902. t.Fatalf("parseFlags returned command %v, want [proto]", cmd)
  903. }
  904. o.Fetch = testFetcher{}
  905. o.Sym = testSymbolzSymbolizer{}
  906. p, err := fetchProfiles(src, o)
  907. if err != nil {
  908. t.Fatalf("fetchProfiles: %v", err)
  909. }
  910. if len(p.Location) != 3 {
  911. t.Errorf("Got %d locations after merge, want %d", len(p.Location), 3)
  912. }
  913. for i, l := range p.Location {
  914. if len(l.Line) != 1 {
  915. t.Errorf("Number of lines for symbolz %#x in iteration %d, got %d, want %d", l.Address, i, len(l.Line), 1)
  916. continue
  917. }
  918. address := l.Address - l.Mapping.Start
  919. if got, want := l.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
  920. t.Errorf("symbolz %#x, got %s, want %s", address, got, want)
  921. }
  922. }
  923. }