You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1622 lines
44 KiB

  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. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. _ "net/http/pprof"
  22. "os"
  23. "reflect"
  24. "regexp"
  25. "runtime"
  26. "strconv"
  27. "strings"
  28. "testing"
  29. "time"
  30. "github.com/google/pprof/internal/plugin"
  31. "github.com/google/pprof/internal/proftest"
  32. "github.com/google/pprof/internal/symbolz"
  33. "github.com/google/pprof/profile"
  34. )
  35. var updateFlag = flag.Bool("update", false, "Update the golden files")
  36. func TestParse(t *testing.T) {
  37. // Override weblist command to collect output in buffer
  38. pprofCommands["weblist"].postProcess = nil
  39. // Our mockObjTool.Open will always return success, causing
  40. // driver.locateBinaries to "find" the binaries below in a non-existent
  41. // directory. As a workaround, point the search path to the fake
  42. // directory containing out fake binaries.
  43. savePath := os.Getenv("PPROF_BINARY_PATH")
  44. os.Setenv("PPROF_BINARY_PATH", "/path/to")
  45. defer os.Setenv("PPROF_BINARY_PATH", savePath)
  46. testcase := []struct {
  47. flags, source string
  48. }{
  49. {"text,functions,flat", "cpu"},
  50. {"text,functions,noinlines,flat", "cpu"},
  51. {"text,filefunctions,noinlines,flat", "cpu"},
  52. {"text,addresses,noinlines,flat", "cpu"},
  53. {"tree,addresses,flat,nodecount=4", "cpusmall"},
  54. {"text,functions,flat,nodecount=5,call_tree", "unknown"},
  55. {"text,alloc_objects,flat", "heap_alloc"},
  56. {"text,files,flat", "heap"},
  57. {"text,files,flat,focus=[12]00,taghide=[X3]00", "heap"},
  58. {"text,inuse_objects,flat", "heap"},
  59. {"text,lines,cum,hide=line[X3]0", "cpu"},
  60. {"text,lines,cum,show=[12]00", "cpu"},
  61. {"text,lines,cum,hide=line[X3]0,focus=[12]00", "cpu"},
  62. {"topproto,lines,cum,hide=mangled[X3]0", "cpu"},
  63. {"topproto,lines", "cpu"},
  64. {"tree,lines,cum,focus=[24]00", "heap"},
  65. {"tree,relative_percentages,cum,focus=[24]00", "heap"},
  66. {"tree,lines,cum,show_from=line2", "cpu"},
  67. {"callgrind", "cpu"},
  68. {"callgrind,call_tree", "cpu"},
  69. {"callgrind", "heap"},
  70. {"dot,functions,flat", "cpu"},
  71. {"dot,functions,flat,call_tree", "cpu"},
  72. {"dot,lines,flat,focus=[12]00", "heap"},
  73. {"dot,unit=minimum", "heap_sizetags"},
  74. {"dot,addresses,flat,ignore=[X3]002,focus=[X1]000", "contention"},
  75. {"dot,files,cum", "contention"},
  76. {"comments,add_comment=some-comment", "cpu"},
  77. {"comments", "heap"},
  78. {"tags", "cpu"},
  79. {"tags,tagignore=tag[13],tagfocus=key[12]", "cpu"},
  80. {"tags", "heap"},
  81. {"tags,unit=bytes", "heap"},
  82. {"traces", "cpu"},
  83. {"traces", "heap_tags"},
  84. {"dot,alloc_space,flat,focus=[234]00", "heap_alloc"},
  85. {"dot,alloc_space,flat,tagshow=[2]00", "heap_alloc"},
  86. {"dot,alloc_space,flat,hide=line.*1?23?", "heap_alloc"},
  87. {"dot,inuse_space,flat,tagfocus=1mb:2gb", "heap"},
  88. {"dot,inuse_space,flat,tagfocus=30kb:,tagignore=1mb:2mb", "heap"},
  89. {"disasm=line[13],addresses,flat", "cpu"},
  90. {"peek=line.*01", "cpu"},
  91. {"weblist=line[13],addresses,flat", "cpu"},
  92. {"tags,tagfocus=400kb:", "heap_request"},
  93. {"tags,tagfocus=+400kb:", "heap_request"},
  94. {"dot", "longNameFuncs"},
  95. {"text", "longNameFuncs"},
  96. }
  97. baseVars := pprofVariables
  98. defer func() { pprofVariables = baseVars }()
  99. for _, tc := range testcase {
  100. t.Run(tc.flags+":"+tc.source, func(t *testing.T) {
  101. // Reset the pprof variables before processing
  102. pprofVariables = baseVars.makeCopy()
  103. testUI := &proftest.TestUI{T: t, AllowRx: "Generating report in|Ignoring local file|expression matched no samples|Interpreted .* as range, not regexp"}
  104. f := baseFlags()
  105. f.args = []string{tc.source}
  106. flags := strings.Split(tc.flags, ",")
  107. // Encode profile into a protobuf and decode it again.
  108. protoTempFile, err := ioutil.TempFile("", "profile_proto")
  109. if err != nil {
  110. t.Errorf("cannot create tempfile: %v", err)
  111. }
  112. defer os.Remove(protoTempFile.Name())
  113. defer protoTempFile.Close()
  114. f.strings["output"] = protoTempFile.Name()
  115. if flags[0] == "topproto" {
  116. f.bools["proto"] = false
  117. f.bools["topproto"] = true
  118. f.bools["addresses"] = true
  119. }
  120. // First pprof invocation to save the profile into a profile.proto.
  121. // Pass in flag set hen setting defaults, because otherwise default
  122. // transport will try to add flags to the default flag set.
  123. o1 := setDefaults(&plugin.Options{Flagset: f})
  124. o1.Fetch = testFetcher{}
  125. o1.Sym = testSymbolizer{}
  126. o1.UI = testUI
  127. if err := PProf(o1); err != nil {
  128. t.Fatalf("%s %q: %v", tc.source, tc.flags, err)
  129. }
  130. // Reset the pprof variables after the proto invocation
  131. pprofVariables = baseVars.makeCopy()
  132. // Read the profile from the encoded protobuf
  133. outputTempFile, err := ioutil.TempFile("", "profile_output")
  134. if err != nil {
  135. t.Errorf("cannot create tempfile: %v", err)
  136. }
  137. defer os.Remove(outputTempFile.Name())
  138. defer outputTempFile.Close()
  139. f = baseFlags()
  140. f.strings["output"] = outputTempFile.Name()
  141. f.args = []string{protoTempFile.Name()}
  142. delete(f.bools, "proto")
  143. addFlags(&f, flags)
  144. solution := solutionFilename(tc.source, &f)
  145. // Apply the flags for the second pprof run, and identify name of
  146. // the file containing expected results
  147. if flags[0] == "topproto" {
  148. addFlags(&f, flags)
  149. solution = solutionFilename(tc.source, &f)
  150. delete(f.bools, "topproto")
  151. f.bools["text"] = true
  152. }
  153. // Second pprof invocation to read the profile from profile.proto
  154. // and generate a report.
  155. // Pass in flag set hen setting defaults, because otherwise default
  156. // transport will try to add flags to the default flag set.
  157. o2 := setDefaults(&plugin.Options{Flagset: f})
  158. o2.Sym = testSymbolizeDemangler{}
  159. o2.Obj = new(mockObjTool)
  160. o2.UI = testUI
  161. if err := PProf(o2); err != nil {
  162. t.Errorf("%s: %v", tc.source, err)
  163. }
  164. b, err := ioutil.ReadFile(outputTempFile.Name())
  165. if err != nil {
  166. t.Errorf("Failed to read profile %s: %v", outputTempFile.Name(), err)
  167. }
  168. // Read data file with expected solution
  169. solution = "testdata/" + solution
  170. sbuf, err := ioutil.ReadFile(solution)
  171. if err != nil {
  172. t.Fatalf("reading solution file %s: %v", solution, err)
  173. }
  174. if runtime.GOOS == "windows" {
  175. sbuf = bytes.Replace(sbuf, []byte("testdata/"), []byte("testdata\\"), -1)
  176. sbuf = bytes.Replace(sbuf, []byte("/path/to/"), []byte("\\path\\to\\"), -1)
  177. }
  178. if flags[0] == "svg" {
  179. b = removeScripts(b)
  180. sbuf = removeScripts(sbuf)
  181. }
  182. if string(b) != string(sbuf) {
  183. t.Errorf("diff %s %s", solution, tc.source)
  184. d, err := proftest.Diff(sbuf, b)
  185. if err != nil {
  186. t.Fatalf("diff %s %v", solution, err)
  187. }
  188. t.Errorf("%s\n%s\n", solution, d)
  189. if *updateFlag {
  190. err := ioutil.WriteFile(solution, b, 0644)
  191. if err != nil {
  192. t.Errorf("failed to update the solution file %q: %v", solution, err)
  193. }
  194. }
  195. }
  196. })
  197. }
  198. }
  199. // removeScripts removes <script > .. </script> pairs from its input
  200. func removeScripts(in []byte) []byte {
  201. beginMarker := []byte("<script")
  202. endMarker := []byte("</script>")
  203. if begin := bytes.Index(in, beginMarker); begin > 0 {
  204. if end := bytes.Index(in[begin:], endMarker); end > 0 {
  205. in = append(in[:begin], removeScripts(in[begin+end+len(endMarker):])...)
  206. }
  207. }
  208. return in
  209. }
  210. // addFlags parses flag descriptions and adds them to the testFlags
  211. func addFlags(f *testFlags, flags []string) {
  212. for _, flag := range flags {
  213. fields := strings.SplitN(flag, "=", 2)
  214. switch len(fields) {
  215. case 1:
  216. f.bools[fields[0]] = true
  217. case 2:
  218. if i, err := strconv.Atoi(fields[1]); err == nil {
  219. f.ints[fields[0]] = i
  220. } else {
  221. f.strings[fields[0]] = fields[1]
  222. }
  223. }
  224. }
  225. }
  226. func testSourceURL(port int) string {
  227. return fmt.Sprintf("http://%s/", net.JoinHostPort(testSourceAddress, strconv.Itoa(port)))
  228. }
  229. // solutionFilename returns the name of the solution file for the test
  230. func solutionFilename(source string, f *testFlags) string {
  231. name := []string{"pprof", strings.TrimPrefix(source, testSourceURL(8000))}
  232. name = addString(name, f, []string{"flat", "cum"})
  233. name = addString(name, f, []string{"functions", "filefunctions", "files", "lines", "addresses"})
  234. name = addString(name, f, []string{"noinlines"})
  235. name = addString(name, f, []string{"inuse_space", "inuse_objects", "alloc_space", "alloc_objects"})
  236. name = addString(name, f, []string{"relative_percentages"})
  237. name = addString(name, f, []string{"seconds"})
  238. name = addString(name, f, []string{"call_tree"})
  239. name = addString(name, f, []string{"text", "tree", "callgrind", "dot", "svg", "tags", "dot", "traces", "disasm", "peek", "weblist", "topproto", "comments"})
  240. if f.strings["focus"] != "" || f.strings["tagfocus"] != "" {
  241. name = append(name, "focus")
  242. }
  243. if f.strings["ignore"] != "" || f.strings["tagignore"] != "" {
  244. name = append(name, "ignore")
  245. }
  246. if f.strings["show_from"] != "" {
  247. name = append(name, "show_from")
  248. }
  249. name = addString(name, f, []string{"hide", "show"})
  250. if f.strings["unit"] != "minimum" {
  251. name = addString(name, f, []string{"unit"})
  252. }
  253. return strings.Join(name, ".")
  254. }
  255. func addString(name []string, f *testFlags, components []string) []string {
  256. for _, c := range components {
  257. if f.bools[c] || f.strings[c] != "" || f.ints[c] != 0 {
  258. return append(name, c)
  259. }
  260. }
  261. return name
  262. }
  263. // testFlags implements the plugin.FlagSet interface.
  264. type testFlags struct {
  265. bools map[string]bool
  266. ints map[string]int
  267. floats map[string]float64
  268. strings map[string]string
  269. args []string
  270. stringLists map[string][]string
  271. }
  272. func (testFlags) ExtraUsage() string { return "" }
  273. func (testFlags) AddExtraUsage(eu string) {}
  274. func (f testFlags) Bool(s string, d bool, c string) *bool {
  275. if b, ok := f.bools[s]; ok {
  276. return &b
  277. }
  278. return &d
  279. }
  280. func (f testFlags) Int(s string, d int, c string) *int {
  281. if i, ok := f.ints[s]; ok {
  282. return &i
  283. }
  284. return &d
  285. }
  286. func (f testFlags) Float64(s string, d float64, c string) *float64 {
  287. if g, ok := f.floats[s]; ok {
  288. return &g
  289. }
  290. return &d
  291. }
  292. func (f testFlags) String(s, d, c string) *string {
  293. if t, ok := f.strings[s]; ok {
  294. return &t
  295. }
  296. return &d
  297. }
  298. func (f testFlags) BoolVar(p *bool, s string, d bool, c string) {
  299. if b, ok := f.bools[s]; ok {
  300. *p = b
  301. } else {
  302. *p = d
  303. }
  304. }
  305. func (f testFlags) IntVar(p *int, s string, d int, c string) {
  306. if i, ok := f.ints[s]; ok {
  307. *p = i
  308. } else {
  309. *p = d
  310. }
  311. }
  312. func (f testFlags) Float64Var(p *float64, s string, d float64, c string) {
  313. if g, ok := f.floats[s]; ok {
  314. *p = g
  315. } else {
  316. *p = d
  317. }
  318. }
  319. func (f testFlags) StringVar(p *string, s, d, c string) {
  320. if t, ok := f.strings[s]; ok {
  321. *p = t
  322. } else {
  323. *p = d
  324. }
  325. }
  326. func (f testFlags) StringList(s, d, c string) *[]*string {
  327. if t, ok := f.stringLists[s]; ok {
  328. // convert slice of strings to slice of string pointers before returning.
  329. tp := make([]*string, len(t))
  330. for i, v := range t {
  331. tp[i] = &v
  332. }
  333. return &tp
  334. }
  335. return &[]*string{}
  336. }
  337. func (f testFlags) Parse(func()) []string {
  338. return f.args
  339. }
  340. func baseFlags() testFlags {
  341. return testFlags{
  342. bools: map[string]bool{
  343. "proto": true,
  344. "trim": true,
  345. "compact_labels": true,
  346. },
  347. ints: map[string]int{
  348. "nodecount": 20,
  349. },
  350. floats: map[string]float64{
  351. "nodefraction": 0.05,
  352. "edgefraction": 0.01,
  353. "divide_by": 1.0,
  354. },
  355. strings: map[string]string{
  356. "unit": "minimum",
  357. },
  358. }
  359. }
  360. const testStart = 0x1000
  361. const testOffset = 0x5000
  362. type testFetcher struct{}
  363. func (testFetcher) Fetch(s string, d, t time.Duration) (*profile.Profile, string, error) {
  364. var p *profile.Profile
  365. switch s {
  366. case "cpu", "unknown":
  367. p = cpuProfile()
  368. case "cpusmall":
  369. p = cpuProfileSmall()
  370. case "heap":
  371. p = heapProfile()
  372. case "heap_alloc":
  373. p = heapProfile()
  374. p.SampleType = []*profile.ValueType{
  375. {Type: "alloc_objects", Unit: "count"},
  376. {Type: "alloc_space", Unit: "bytes"},
  377. }
  378. case "heap_request":
  379. p = heapProfile()
  380. for _, s := range p.Sample {
  381. s.NumLabel["request"] = s.NumLabel["bytes"]
  382. }
  383. case "heap_sizetags":
  384. p = heapProfile()
  385. tags := []int64{2, 4, 8, 16, 32, 64, 128, 256}
  386. for _, s := range p.Sample {
  387. numValues := append(s.NumLabel["bytes"], tags...)
  388. s.NumLabel["bytes"] = numValues
  389. }
  390. case "heap_tags":
  391. p = heapProfile()
  392. for i := 0; i < len(p.Sample); i += 2 {
  393. s := p.Sample[i]
  394. if s.Label == nil {
  395. s.Label = make(map[string][]string)
  396. }
  397. s.NumLabel["request"] = s.NumLabel["bytes"]
  398. s.Label["key1"] = []string{"tag"}
  399. }
  400. case "contention":
  401. p = contentionProfile()
  402. case "symbolz":
  403. p = symzProfile()
  404. case "longNameFuncs":
  405. p = longNameFuncsProfile()
  406. default:
  407. return nil, "", fmt.Errorf("unexpected source: %s", s)
  408. }
  409. return p, testSourceURL(8000) + s, nil
  410. }
  411. type testSymbolizer struct{}
  412. func (testSymbolizer) Symbolize(_ string, _ plugin.MappingSources, _ *profile.Profile) error {
  413. return nil
  414. }
  415. type testSymbolizeDemangler struct{}
  416. func (testSymbolizeDemangler) Symbolize(_ string, _ plugin.MappingSources, p *profile.Profile) error {
  417. for _, fn := range p.Function {
  418. if fn.Name == "" || fn.SystemName == fn.Name {
  419. fn.Name = fakeDemangler(fn.SystemName)
  420. }
  421. }
  422. return nil
  423. }
  424. func testFetchSymbols(source, post string) ([]byte, error) {
  425. var buf bytes.Buffer
  426. switch source {
  427. case testSourceURL(8000) + "symbolz":
  428. for _, address := range strings.Split(post, "+") {
  429. a, _ := strconv.ParseInt(address, 0, 64)
  430. fmt.Fprintf(&buf, "%v\t", address)
  431. if a-testStart > testOffset {
  432. fmt.Fprintf(&buf, "wrong_source_%v_", address)
  433. continue
  434. }
  435. fmt.Fprintf(&buf, "%#x\n", a-testStart)
  436. }
  437. return buf.Bytes(), nil
  438. case testSourceURL(8001) + "symbolz":
  439. for _, address := range strings.Split(post, "+") {
  440. a, _ := strconv.ParseInt(address, 0, 64)
  441. fmt.Fprintf(&buf, "%v\t", address)
  442. if a-testStart < testOffset {
  443. fmt.Fprintf(&buf, "wrong_source_%v_", address)
  444. continue
  445. }
  446. fmt.Fprintf(&buf, "%#x\n", a-testStart-testOffset)
  447. }
  448. return buf.Bytes(), nil
  449. default:
  450. return nil, fmt.Errorf("unexpected source: %s", source)
  451. }
  452. }
  453. type testSymbolzSymbolizer struct{}
  454. func (testSymbolzSymbolizer) Symbolize(variables string, sources plugin.MappingSources, p *profile.Profile) error {
  455. return symbolz.Symbolize(p, false, sources, testFetchSymbols, nil)
  456. }
  457. func fakeDemangler(name string) string {
  458. switch name {
  459. case "mangled1000":
  460. return "line1000"
  461. case "mangled2000":
  462. return "line2000"
  463. case "mangled2001":
  464. return "line2001"
  465. case "mangled3000":
  466. return "line3000"
  467. case "mangled3001":
  468. return "line3001"
  469. case "mangled3002":
  470. return "line3002"
  471. case "mangledNEW":
  472. return "operator new"
  473. case "mangledMALLOC":
  474. return "malloc"
  475. default:
  476. return name
  477. }
  478. }
  479. // longNameFuncsProfile returns a profile with function names which should be shortened in
  480. // graph and flame views.
  481. func longNameFuncsProfile() *profile.Profile {
  482. var longNameFuncsM = []*profile.Mapping{
  483. {
  484. ID: 1,
  485. Start: 0x1000,
  486. Limit: 0x4000,
  487. File: "/path/to/testbinary",
  488. HasFunctions: true,
  489. HasFilenames: true,
  490. HasLineNumbers: true,
  491. HasInlineFrames: true,
  492. },
  493. }
  494. var longNameFuncsF = []*profile.Function{
  495. {ID: 1, Name: "path/to/package1.object.function1", SystemName: "path/to/package1.object.function1", Filename: "path/to/package1.go"},
  496. {ID: 2, Name: "(anonymous namespace)::Bar::Foo", SystemName: "(anonymous namespace)::Bar::Foo", Filename: "a/long/path/to/package2.cc"},
  497. {ID: 3, Name: "java.bar.foo.FooBar.run(java.lang.Runnable)", SystemName: "java.bar.foo.FooBar.run(java.lang.Runnable)", Filename: "FooBar.java"},
  498. }
  499. var longNameFuncsL = []*profile.Location{
  500. {
  501. ID: 1000,
  502. Mapping: longNameFuncsM[0],
  503. Address: 0x1000,
  504. Line: []profile.Line{
  505. {Function: longNameFuncsF[0], Line: 1},
  506. },
  507. },
  508. {
  509. ID: 2000,
  510. Mapping: longNameFuncsM[0],
  511. Address: 0x2000,
  512. Line: []profile.Line{
  513. {Function: longNameFuncsF[1], Line: 4},
  514. },
  515. },
  516. {
  517. ID: 3000,
  518. Mapping: longNameFuncsM[0],
  519. Address: 0x3000,
  520. Line: []profile.Line{
  521. {Function: longNameFuncsF[2], Line: 9},
  522. },
  523. },
  524. }
  525. return &profile.Profile{
  526. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  527. Period: 1,
  528. DurationNanos: 10e9,
  529. SampleType: []*profile.ValueType{
  530. {Type: "samples", Unit: "count"},
  531. {Type: "cpu", Unit: "milliseconds"},
  532. },
  533. Sample: []*profile.Sample{
  534. {
  535. Location: []*profile.Location{longNameFuncsL[0], longNameFuncsL[1], longNameFuncsL[2]},
  536. Value: []int64{1000, 1000},
  537. },
  538. {
  539. Location: []*profile.Location{longNameFuncsL[0], longNameFuncsL[1]},
  540. Value: []int64{100, 100},
  541. },
  542. {
  543. Location: []*profile.Location{longNameFuncsL[2]},
  544. Value: []int64{10, 10},
  545. },
  546. },
  547. Location: longNameFuncsL,
  548. Function: longNameFuncsF,
  549. Mapping: longNameFuncsM,
  550. }
  551. }
  552. func cpuProfile() *profile.Profile {
  553. var cpuM = []*profile.Mapping{
  554. {
  555. ID: 1,
  556. Start: 0x1000,
  557. Limit: 0x4000,
  558. File: "/path/to/testbinary",
  559. HasFunctions: true,
  560. HasFilenames: true,
  561. HasLineNumbers: true,
  562. HasInlineFrames: true,
  563. },
  564. }
  565. var cpuF = []*profile.Function{
  566. {ID: 1, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  567. {ID: 2, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  568. {ID: 3, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  569. {ID: 4, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  570. {ID: 5, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  571. {ID: 6, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  572. }
  573. var cpuL = []*profile.Location{
  574. {
  575. ID: 1000,
  576. Mapping: cpuM[0],
  577. Address: 0x1000,
  578. Line: []profile.Line{
  579. {Function: cpuF[0], Line: 1},
  580. },
  581. },
  582. {
  583. ID: 2000,
  584. Mapping: cpuM[0],
  585. Address: 0x2000,
  586. Line: []profile.Line{
  587. {Function: cpuF[2], Line: 9},
  588. {Function: cpuF[1], Line: 4},
  589. },
  590. },
  591. {
  592. ID: 3000,
  593. Mapping: cpuM[0],
  594. Address: 0x3000,
  595. Line: []profile.Line{
  596. {Function: cpuF[5], Line: 2},
  597. {Function: cpuF[4], Line: 5},
  598. {Function: cpuF[3], Line: 6},
  599. },
  600. },
  601. {
  602. ID: 3001,
  603. Mapping: cpuM[0],
  604. Address: 0x3001,
  605. Line: []profile.Line{
  606. {Function: cpuF[4], Line: 8},
  607. {Function: cpuF[3], Line: 9},
  608. },
  609. },
  610. {
  611. ID: 3002,
  612. Mapping: cpuM[0],
  613. Address: 0x3002,
  614. Line: []profile.Line{
  615. {Function: cpuF[5], Line: 5},
  616. {Function: cpuF[3], Line: 9},
  617. },
  618. },
  619. }
  620. return &profile.Profile{
  621. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  622. Period: 1,
  623. DurationNanos: 10e9,
  624. SampleType: []*profile.ValueType{
  625. {Type: "samples", Unit: "count"},
  626. {Type: "cpu", Unit: "milliseconds"},
  627. },
  628. Sample: []*profile.Sample{
  629. {
  630. Location: []*profile.Location{cpuL[0], cpuL[1], cpuL[2]},
  631. Value: []int64{1000, 1000},
  632. Label: map[string][]string{
  633. "key1": {"tag1"},
  634. "key2": {"tag1"},
  635. },
  636. },
  637. {
  638. Location: []*profile.Location{cpuL[0], cpuL[3]},
  639. Value: []int64{100, 100},
  640. Label: map[string][]string{
  641. "key1": {"tag2"},
  642. "key3": {"tag2"},
  643. },
  644. },
  645. {
  646. Location: []*profile.Location{cpuL[1], cpuL[4]},
  647. Value: []int64{10, 10},
  648. Label: map[string][]string{
  649. "key1": {"tag3"},
  650. "key2": {"tag2"},
  651. },
  652. },
  653. {
  654. Location: []*profile.Location{cpuL[2]},
  655. Value: []int64{10, 10},
  656. Label: map[string][]string{
  657. "key1": {"tag4"},
  658. "key2": {"tag1"},
  659. },
  660. },
  661. },
  662. Location: cpuL,
  663. Function: cpuF,
  664. Mapping: cpuM,
  665. }
  666. }
  667. func cpuProfileSmall() *profile.Profile {
  668. var cpuM = []*profile.Mapping{
  669. {
  670. ID: 1,
  671. Start: 0x1000,
  672. Limit: 0x4000,
  673. File: "/path/to/testbinary",
  674. HasFunctions: true,
  675. HasFilenames: true,
  676. HasLineNumbers: true,
  677. HasInlineFrames: true,
  678. },
  679. }
  680. var cpuL = []*profile.Location{
  681. {
  682. ID: 1000,
  683. Mapping: cpuM[0],
  684. Address: 0x1000,
  685. },
  686. {
  687. ID: 2000,
  688. Mapping: cpuM[0],
  689. Address: 0x2000,
  690. },
  691. {
  692. ID: 3000,
  693. Mapping: cpuM[0],
  694. Address: 0x3000,
  695. },
  696. {
  697. ID: 4000,
  698. Mapping: cpuM[0],
  699. Address: 0x4000,
  700. },
  701. {
  702. ID: 5000,
  703. Mapping: cpuM[0],
  704. Address: 0x5000,
  705. },
  706. }
  707. return &profile.Profile{
  708. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  709. Period: 1,
  710. DurationNanos: 10e9,
  711. SampleType: []*profile.ValueType{
  712. {Type: "samples", Unit: "count"},
  713. {Type: "cpu", Unit: "milliseconds"},
  714. },
  715. Sample: []*profile.Sample{
  716. {
  717. Location: []*profile.Location{cpuL[0], cpuL[1], cpuL[2]},
  718. Value: []int64{1000, 1000},
  719. },
  720. {
  721. Location: []*profile.Location{cpuL[3], cpuL[1], cpuL[4]},
  722. Value: []int64{1000, 1000},
  723. },
  724. {
  725. Location: []*profile.Location{cpuL[2]},
  726. Value: []int64{1000, 1000},
  727. },
  728. {
  729. Location: []*profile.Location{cpuL[4]},
  730. Value: []int64{1000, 1000},
  731. },
  732. },
  733. Location: cpuL,
  734. Function: nil,
  735. Mapping: cpuM,
  736. }
  737. }
  738. func heapProfile() *profile.Profile {
  739. var heapM = []*profile.Mapping{
  740. {
  741. ID: 1,
  742. BuildID: "buildid",
  743. Start: 0x1000,
  744. Limit: 0x4000,
  745. HasFunctions: true,
  746. HasFilenames: true,
  747. HasLineNumbers: true,
  748. HasInlineFrames: true,
  749. },
  750. }
  751. var heapF = []*profile.Function{
  752. {ID: 1, Name: "pruneme", SystemName: "pruneme", Filename: "prune.h"},
  753. {ID: 2, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  754. {ID: 3, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  755. {ID: 4, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  756. {ID: 5, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  757. {ID: 6, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  758. {ID: 7, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  759. {ID: 8, Name: "mangledMALLOC", SystemName: "mangledMALLOC", Filename: "malloc.h"},
  760. {ID: 9, Name: "mangledNEW", SystemName: "mangledNEW", Filename: "new.h"},
  761. }
  762. var heapL = []*profile.Location{
  763. {
  764. ID: 1000,
  765. Mapping: heapM[0],
  766. Address: 0x1000,
  767. Line: []profile.Line{
  768. {Function: heapF[0], Line: 100},
  769. {Function: heapF[7], Line: 100},
  770. {Function: heapF[1], Line: 1},
  771. },
  772. },
  773. {
  774. ID: 2000,
  775. Mapping: heapM[0],
  776. Address: 0x2000,
  777. Line: []profile.Line{
  778. {Function: heapF[8], Line: 100},
  779. {Function: heapF[3], Line: 2},
  780. {Function: heapF[2], Line: 3},
  781. },
  782. },
  783. {
  784. ID: 3000,
  785. Mapping: heapM[0],
  786. Address: 0x3000,
  787. Line: []profile.Line{
  788. {Function: heapF[8], Line: 100},
  789. {Function: heapF[6], Line: 3},
  790. {Function: heapF[5], Line: 2},
  791. {Function: heapF[4], Line: 4},
  792. },
  793. },
  794. {
  795. ID: 3001,
  796. Mapping: heapM[0],
  797. Address: 0x3001,
  798. Line: []profile.Line{
  799. {Function: heapF[0], Line: 100},
  800. {Function: heapF[8], Line: 100},
  801. {Function: heapF[5], Line: 2},
  802. {Function: heapF[4], Line: 4},
  803. },
  804. },
  805. {
  806. ID: 3002,
  807. Mapping: heapM[0],
  808. Address: 0x3002,
  809. Line: []profile.Line{
  810. {Function: heapF[6], Line: 3},
  811. {Function: heapF[4], Line: 4},
  812. },
  813. },
  814. }
  815. return &profile.Profile{
  816. Comments: []string{"comment", "#hidden comment"},
  817. PeriodType: &profile.ValueType{Type: "allocations", Unit: "bytes"},
  818. Period: 524288,
  819. SampleType: []*profile.ValueType{
  820. {Type: "inuse_objects", Unit: "count"},
  821. {Type: "inuse_space", Unit: "bytes"},
  822. },
  823. Sample: []*profile.Sample{
  824. {
  825. Location: []*profile.Location{heapL[0], heapL[1], heapL[2]},
  826. Value: []int64{10, 1024000},
  827. NumLabel: map[string][]int64{"bytes": {102400}},
  828. },
  829. {
  830. Location: []*profile.Location{heapL[0], heapL[3]},
  831. Value: []int64{20, 4096000},
  832. NumLabel: map[string][]int64{"bytes": {204800}},
  833. },
  834. {
  835. Location: []*profile.Location{heapL[1], heapL[4]},
  836. Value: []int64{40, 65536000},
  837. NumLabel: map[string][]int64{"bytes": {1638400}},
  838. },
  839. {
  840. Location: []*profile.Location{heapL[2]},
  841. Value: []int64{80, 32768000},
  842. NumLabel: map[string][]int64{"bytes": {409600}},
  843. },
  844. },
  845. DropFrames: ".*operator new.*|malloc",
  846. Location: heapL,
  847. Function: heapF,
  848. Mapping: heapM,
  849. }
  850. }
  851. func contentionProfile() *profile.Profile {
  852. var contentionM = []*profile.Mapping{
  853. {
  854. ID: 1,
  855. BuildID: "buildid-contention",
  856. Start: 0x1000,
  857. Limit: 0x4000,
  858. HasFunctions: true,
  859. HasFilenames: true,
  860. HasLineNumbers: true,
  861. HasInlineFrames: true,
  862. },
  863. }
  864. var contentionF = []*profile.Function{
  865. {ID: 1, Name: "mangled1000", SystemName: "mangled1000", Filename: "testdata/file1000.src"},
  866. {ID: 2, Name: "mangled2000", SystemName: "mangled2000", Filename: "testdata/file2000.src"},
  867. {ID: 3, Name: "mangled2001", SystemName: "mangled2001", Filename: "testdata/file2000.src"},
  868. {ID: 4, Name: "mangled3000", SystemName: "mangled3000", Filename: "testdata/file3000.src"},
  869. {ID: 5, Name: "mangled3001", SystemName: "mangled3001", Filename: "testdata/file3000.src"},
  870. {ID: 6, Name: "mangled3002", SystemName: "mangled3002", Filename: "testdata/file3000.src"},
  871. }
  872. var contentionL = []*profile.Location{
  873. {
  874. ID: 1000,
  875. Mapping: contentionM[0],
  876. Address: 0x1000,
  877. Line: []profile.Line{
  878. {Function: contentionF[0], Line: 1},
  879. },
  880. },
  881. {
  882. ID: 2000,
  883. Mapping: contentionM[0],
  884. Address: 0x2000,
  885. Line: []profile.Line{
  886. {Function: contentionF[2], Line: 2},
  887. {Function: contentionF[1], Line: 3},
  888. },
  889. },
  890. {
  891. ID: 3000,
  892. Mapping: contentionM[0],
  893. Address: 0x3000,
  894. Line: []profile.Line{
  895. {Function: contentionF[5], Line: 2},
  896. {Function: contentionF[4], Line: 3},
  897. {Function: contentionF[3], Line: 5},
  898. },
  899. },
  900. {
  901. ID: 3001,
  902. Mapping: contentionM[0],
  903. Address: 0x3001,
  904. Line: []profile.Line{
  905. {Function: contentionF[4], Line: 3},
  906. {Function: contentionF[3], Line: 5},
  907. },
  908. },
  909. {
  910. ID: 3002,
  911. Mapping: contentionM[0],
  912. Address: 0x3002,
  913. Line: []profile.Line{
  914. {Function: contentionF[5], Line: 4},
  915. {Function: contentionF[3], Line: 3},
  916. },
  917. },
  918. }
  919. return &profile.Profile{
  920. PeriodType: &profile.ValueType{Type: "contentions", Unit: "count"},
  921. Period: 524288,
  922. SampleType: []*profile.ValueType{
  923. {Type: "contentions", Unit: "count"},
  924. {Type: "delay", Unit: "nanoseconds"},
  925. },
  926. Sample: []*profile.Sample{
  927. {
  928. Location: []*profile.Location{contentionL[0], contentionL[1], contentionL[2]},
  929. Value: []int64{10, 10240000},
  930. },
  931. {
  932. Location: []*profile.Location{contentionL[0], contentionL[3]},
  933. Value: []int64{20, 40960000},
  934. },
  935. {
  936. Location: []*profile.Location{contentionL[1], contentionL[4]},
  937. Value: []int64{40, 65536000},
  938. },
  939. {
  940. Location: []*profile.Location{contentionL[2]},
  941. Value: []int64{80, 32768000},
  942. },
  943. },
  944. Location: contentionL,
  945. Function: contentionF,
  946. Mapping: contentionM,
  947. Comments: []string{"Comment #1", "Comment #2"},
  948. }
  949. }
  950. func symzProfile() *profile.Profile {
  951. var symzM = []*profile.Mapping{
  952. {
  953. ID: 1,
  954. Start: testStart,
  955. Limit: 0x4000,
  956. File: "/path/to/testbinary",
  957. },
  958. }
  959. var symzL = []*profile.Location{
  960. {ID: 1, Mapping: symzM[0], Address: testStart},
  961. {ID: 2, Mapping: symzM[0], Address: testStart + 0x1000},
  962. {ID: 3, Mapping: symzM[0], Address: testStart + 0x2000},
  963. }
  964. return &profile.Profile{
  965. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  966. Period: 1,
  967. DurationNanos: 10e9,
  968. SampleType: []*profile.ValueType{
  969. {Type: "samples", Unit: "count"},
  970. {Type: "cpu", Unit: "milliseconds"},
  971. },
  972. Sample: []*profile.Sample{
  973. {
  974. Location: []*profile.Location{symzL[0], symzL[1], symzL[2]},
  975. Value: []int64{1, 1},
  976. },
  977. },
  978. Location: symzL,
  979. Mapping: symzM,
  980. }
  981. }
  982. var autoCompleteTests = []struct {
  983. in string
  984. out string
  985. }{
  986. {"", ""},
  987. {"xyz", "xyz"}, // no match
  988. {"dis", "disasm"}, // single match
  989. {"t", "t"}, // many matches
  990. {"top abc", "top abc"}, // no function name match
  991. {"top mangledM", "top mangledMALLOC"}, // single function name match
  992. {"top cmd cmd mangledM", "top cmd cmd mangledMALLOC"},
  993. {"top mangled", "top mangled"}, // many function name matches
  994. {"cmd mangledM", "cmd mangledM"}, // invalid command
  995. {"top mangledM cmd", "top mangledM cmd"}, // cursor misplaced
  996. {"top edMA", "top mangledMALLOC"}, // single infix function name match
  997. {"top -mangledM", "top -mangledMALLOC"}, // ignore sign handled
  998. {"lin", "lines"}, // single variable match
  999. {"EdGeF", "edgefraction"}, // single capitalized match
  1000. {"help dis", "help disasm"}, // help command match
  1001. {"help relative_perc", "help relative_percentages"}, // help variable match
  1002. {"help coMpa", "help compact_labels"}, // help variable capitalized match
  1003. }
  1004. func TestAutoComplete(t *testing.T) {
  1005. complete := newCompleter(functionNames(heapProfile()))
  1006. for _, test := range autoCompleteTests {
  1007. if out := complete(test.in); out != test.out {
  1008. t.Errorf("autoComplete(%s) = %s; want %s", test.in, out, test.out)
  1009. }
  1010. }
  1011. }
  1012. func TestTagFilter(t *testing.T) {
  1013. var tagFilterTests = []struct {
  1014. desc, value string
  1015. tags map[string][]string
  1016. want bool
  1017. }{
  1018. {
  1019. "1 key with 1 matching value",
  1020. "tag2",
  1021. map[string][]string{"value1": {"tag1", "tag2"}},
  1022. true,
  1023. },
  1024. {
  1025. "1 key with no matching values",
  1026. "tag3",
  1027. map[string][]string{"value1": {"tag1", "tag2"}},
  1028. false,
  1029. },
  1030. {
  1031. "two keys, each with value matching different one value in list",
  1032. "tag1,tag3",
  1033. map[string][]string{"value1": {"tag1", "tag2"}, "value2": {"tag3"}},
  1034. true,
  1035. },
  1036. {"two keys, all value matching different regex value in list",
  1037. "t..[12],t..3",
  1038. map[string][]string{"value1": {"tag1", "tag2"}, "value2": {"tag3"}},
  1039. true,
  1040. },
  1041. {
  1042. "one key, not all values in list matched",
  1043. "tag2,tag3",
  1044. map[string][]string{"value1": {"tag1", "tag2"}},
  1045. false,
  1046. },
  1047. {
  1048. "key specified, list of tags where all tags in list matched",
  1049. "key1=tag1,tag2",
  1050. map[string][]string{"key1": {"tag1", "tag2"}},
  1051. true,
  1052. },
  1053. {"key specified, list of tag values where not all are matched",
  1054. "key1=tag1,tag2",
  1055. map[string][]string{"key1": {"tag1"}},
  1056. true,
  1057. },
  1058. {
  1059. "key included for regex matching, list of values where all values in list matched",
  1060. "key1:tag1,tag2",
  1061. map[string][]string{"key1": {"tag1", "tag2"}},
  1062. true,
  1063. },
  1064. {
  1065. "key included for regex matching, list of values where not only second value matched",
  1066. "key1:tag1,tag2",
  1067. map[string][]string{"key1": {"tag2"}},
  1068. false,
  1069. },
  1070. {
  1071. "key included for regex matching, list of values where not only first value matched",
  1072. "key1:tag1,tag2",
  1073. map[string][]string{"key1": {"tag1"}},
  1074. false,
  1075. },
  1076. }
  1077. for _, test := range tagFilterTests {
  1078. t.Run(test.desc, func(*testing.T) {
  1079. filter, err := compileTagFilter(test.desc, test.value, nil, &proftest.TestUI{T: t}, nil)
  1080. if err != nil {
  1081. t.Fatalf("tagFilter %s:%v", test.desc, err)
  1082. }
  1083. s := profile.Sample{
  1084. Label: test.tags,
  1085. }
  1086. if got := filter(&s); got != test.want {
  1087. t.Errorf("tagFilter %s: got %v, want %v", test.desc, got, test.want)
  1088. }
  1089. })
  1090. }
  1091. }
  1092. func TestIdentifyNumLabelUnits(t *testing.T) {
  1093. var tagFilterTests = []struct {
  1094. desc string
  1095. tagVals []map[string][]int64
  1096. tagUnits []map[string][]string
  1097. wantUnits map[string]string
  1098. allowedRx string
  1099. wantIgnoreErrCount int
  1100. }{
  1101. {
  1102. "Multiple keys, no units for all keys",
  1103. []map[string][]int64{{"keyA": {131072}, "keyB": {128}}},
  1104. []map[string][]string{{"keyA": {}, "keyB": {""}}},
  1105. map[string]string{"keyA": "keyA", "keyB": "keyB"},
  1106. "",
  1107. 0,
  1108. },
  1109. {
  1110. "Multiple keys, different units for each key",
  1111. []map[string][]int64{{"keyA": {131072}, "keyB": {128}}},
  1112. []map[string][]string{{"keyA": {"bytes"}, "keyB": {"kilobytes"}}},
  1113. map[string]string{"keyA": "bytes", "keyB": "kilobytes"},
  1114. "",
  1115. 0,
  1116. },
  1117. {
  1118. "Multiple keys with multiple values, different units for each key",
  1119. []map[string][]int64{{"keyC": {131072, 1}, "keyD": {128, 252}}},
  1120. []map[string][]string{{"keyC": {"bytes", "bytes"}, "keyD": {"kilobytes", "kilobytes"}}},
  1121. map[string]string{"keyC": "bytes", "keyD": "kilobytes"},
  1122. "",
  1123. 0,
  1124. },
  1125. {
  1126. "Multiple keys with multiple values, some units missing",
  1127. []map[string][]int64{{"key1": {131072, 1}, "A": {128, 252}, "key3": {128}, "key4": {1}}, {"key3": {128}, "key4": {1}}},
  1128. []map[string][]string{{"key1": {"", "bytes"}, "A": {"kilobytes", ""}, "key3": {""}, "key4": {"hour"}}, {"key3": {"seconds"}, "key4": {""}}},
  1129. map[string]string{"key1": "bytes", "A": "kilobytes", "key3": "seconds", "key4": "hour"},
  1130. "",
  1131. 0,
  1132. },
  1133. {
  1134. "One key with three units in same sample",
  1135. []map[string][]int64{{"key": {8, 8, 16}}},
  1136. []map[string][]string{{"key": {"bytes", "megabytes", "kilobytes"}}},
  1137. map[string]string{"key": "bytes"},
  1138. `(For tag key used unit bytes, also encountered unit\(s\) kilobytes, megabytes)`,
  1139. 1,
  1140. },
  1141. {
  1142. "One key with four units in same sample",
  1143. []map[string][]int64{{"key": {8, 8, 16, 32}}},
  1144. []map[string][]string{{"key": {"bytes", "kilobytes", "a", "megabytes"}}},
  1145. map[string]string{"key": "bytes"},
  1146. `(For tag key used unit bytes, also encountered unit\(s\) a, kilobytes, megabytes)`,
  1147. 1,
  1148. },
  1149. {
  1150. "One key with two units in same sample",
  1151. []map[string][]int64{{"key": {8, 8}}},
  1152. []map[string][]string{{"key": {"bytes", "seconds"}}},
  1153. map[string]string{"key": "bytes"},
  1154. `(For tag key used unit bytes, also encountered unit\(s\) seconds)`,
  1155. 1,
  1156. },
  1157. {
  1158. "One key with different units in different samples",
  1159. []map[string][]int64{{"key1": {8}}, {"key1": {8}}, {"key1": {8}}},
  1160. []map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}, {"key1": {"megabytes"}}},
  1161. map[string]string{"key1": "bytes"},
  1162. `(For tag key1 used unit bytes, also encountered unit\(s\) kilobytes, megabytes)`,
  1163. 1,
  1164. },
  1165. {
  1166. "Key alignment, unit not specified",
  1167. []map[string][]int64{{"alignment": {8}}},
  1168. []map[string][]string{nil},
  1169. map[string]string{"alignment": "bytes"},
  1170. "",
  1171. 0,
  1172. },
  1173. {
  1174. "Key request, unit not specified",
  1175. []map[string][]int64{{"request": {8}}, {"request": {8, 8}}},
  1176. []map[string][]string{nil, nil},
  1177. map[string]string{"request": "bytes"},
  1178. "",
  1179. 0,
  1180. },
  1181. {
  1182. "Check units not over-written for keys with default units",
  1183. []map[string][]int64{{
  1184. "alignment": {8},
  1185. "request": {8},
  1186. "bytes": {8},
  1187. }},
  1188. []map[string][]string{{
  1189. "alignment": {"seconds"},
  1190. "request": {"minutes"},
  1191. "bytes": {"hours"},
  1192. }},
  1193. map[string]string{
  1194. "alignment": "seconds",
  1195. "request": "minutes",
  1196. "bytes": "hours",
  1197. },
  1198. "",
  1199. 0,
  1200. },
  1201. }
  1202. for _, test := range tagFilterTests {
  1203. t.Run(test.desc, func(*testing.T) {
  1204. p := profile.Profile{Sample: make([]*profile.Sample, len(test.tagVals))}
  1205. for i, numLabel := range test.tagVals {
  1206. s := profile.Sample{
  1207. NumLabel: numLabel,
  1208. NumUnit: test.tagUnits[i],
  1209. }
  1210. p.Sample[i] = &s
  1211. }
  1212. testUI := &proftest.TestUI{T: t, AllowRx: test.allowedRx}
  1213. units := identifyNumLabelUnits(&p, testUI)
  1214. if !reflect.DeepEqual(test.wantUnits, units) {
  1215. t.Errorf("got %v units, want %v", units, test.wantUnits)
  1216. }
  1217. if got, want := testUI.NumAllowRxMatches, test.wantIgnoreErrCount; want != got {
  1218. t.Errorf("got %d errors logged, want %d errors logged", got, want)
  1219. }
  1220. })
  1221. }
  1222. }
  1223. func TestNumericTagFilter(t *testing.T) {
  1224. var tagFilterTests = []struct {
  1225. desc, value string
  1226. tags map[string][]int64
  1227. identifiedUnits map[string]string
  1228. want bool
  1229. }{
  1230. {
  1231. "Match when unit conversion required",
  1232. "128kb",
  1233. map[string][]int64{"key1": {131072}, "key2": {128}},
  1234. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  1235. true,
  1236. },
  1237. {
  1238. "Match only when values equal after unit conversion",
  1239. "512kb",
  1240. map[string][]int64{"key1": {512}, "key2": {128}},
  1241. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  1242. false,
  1243. },
  1244. {
  1245. "Match when values and units initially equal",
  1246. "10bytes",
  1247. map[string][]int64{"key1": {10}, "key2": {128}},
  1248. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  1249. true,
  1250. },
  1251. {
  1252. "Match range without lower bound, no unit conversion required",
  1253. ":10bytes",
  1254. map[string][]int64{"key1": {8}},
  1255. map[string]string{"key1": "bytes"},
  1256. true,
  1257. },
  1258. {
  1259. "Match range without lower bound, unit conversion required",
  1260. ":10kb",
  1261. map[string][]int64{"key1": {8}},
  1262. map[string]string{"key1": "bytes"},
  1263. true,
  1264. },
  1265. {
  1266. "Match range without upper bound, unit conversion required",
  1267. "10b:",
  1268. map[string][]int64{"key1": {8}},
  1269. map[string]string{"key1": "kilobytes"},
  1270. true,
  1271. },
  1272. {
  1273. "Match range without upper bound, no unit conversion required",
  1274. "10b:",
  1275. map[string][]int64{"key1": {12}},
  1276. map[string]string{"key1": "bytes"},
  1277. true,
  1278. },
  1279. {
  1280. "Don't match range without upper bound, no unit conversion required",
  1281. "10b:",
  1282. map[string][]int64{"key1": {8}},
  1283. map[string]string{"key1": "bytes"},
  1284. false,
  1285. },
  1286. {
  1287. "Multiple keys with different units, don't match range without upper bound",
  1288. "10kb:",
  1289. map[string][]int64{"key1": {8}},
  1290. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  1291. false,
  1292. },
  1293. {
  1294. "Match range without upper bound, unit conversion required",
  1295. "10b:",
  1296. map[string][]int64{"key1": {8}},
  1297. map[string]string{"key1": "kilobytes"},
  1298. true,
  1299. },
  1300. {
  1301. "Don't match range without lower bound, no unit conversion required",
  1302. ":10b",
  1303. map[string][]int64{"key1": {12}},
  1304. map[string]string{"key1": "bytes"},
  1305. false,
  1306. },
  1307. {
  1308. "Match specific key, key present, one of two values match",
  1309. "bytes=5b",
  1310. map[string][]int64{"bytes": {10, 5}},
  1311. map[string]string{"bytes": "bytes"},
  1312. true,
  1313. },
  1314. {
  1315. "Match specific key, key present and value matches",
  1316. "bytes=1024b",
  1317. map[string][]int64{"bytes": {1024}},
  1318. map[string]string{"bytes": "kilobytes"},
  1319. false,
  1320. },
  1321. {
  1322. "Match specific key, matching key present and value matches, also non-matching key",
  1323. "bytes=1024b",
  1324. map[string][]int64{"bytes": {1024}, "key2": {5}},
  1325. map[string]string{"bytes": "bytes", "key2": "bytes"},
  1326. true,
  1327. },
  1328. {
  1329. "Match specific key and range of values, value matches",
  1330. "bytes=512b:1024b",
  1331. map[string][]int64{"bytes": {780}},
  1332. map[string]string{"bytes": "bytes"},
  1333. true,
  1334. },
  1335. {
  1336. "Match specific key and range of values, value too large",
  1337. "key1=1kb:2kb",
  1338. map[string][]int64{"key1": {4096}},
  1339. map[string]string{"key1": "bytes"},
  1340. false,
  1341. },
  1342. {
  1343. "Match specific key and range of values, value too small",
  1344. "key1=1kb:2kb",
  1345. map[string][]int64{"key1": {256}},
  1346. map[string]string{"key1": "bytes"},
  1347. false,
  1348. },
  1349. {
  1350. "Match specific key and value, unit conversion required",
  1351. "bytes=1024b",
  1352. map[string][]int64{"bytes": {1}},
  1353. map[string]string{"bytes": "kilobytes"},
  1354. true,
  1355. },
  1356. {
  1357. "Match specific key and value, key does not appear",
  1358. "key2=256bytes",
  1359. map[string][]int64{"key1": {256}},
  1360. map[string]string{"key1": "bytes"},
  1361. false,
  1362. },
  1363. {
  1364. "Match negative key and range of values, value matches",
  1365. "bytes=-512b:-128b",
  1366. map[string][]int64{"bytes": {-256}},
  1367. map[string]string{"bytes": "bytes"},
  1368. true,
  1369. },
  1370. {
  1371. "Match negative key and range of values, value outside range",
  1372. "bytes=-512b:-128b",
  1373. map[string][]int64{"bytes": {-2048}},
  1374. map[string]string{"bytes": "bytes"},
  1375. false,
  1376. },
  1377. }
  1378. for _, test := range tagFilterTests {
  1379. t.Run(test.desc, func(*testing.T) {
  1380. wantErrMsg := strings.Join([]string{"(", test.desc, ":Interpreted '", test.value[strings.Index(test.value, "=")+1:], "' as range, not regexp", ")"}, "")
  1381. filter, err := compileTagFilter(test.desc, test.value, test.identifiedUnits, &proftest.TestUI{T: t,
  1382. AllowRx: wantErrMsg}, nil)
  1383. if err != nil {
  1384. t.Fatalf("%v", err)
  1385. }
  1386. s := profile.Sample{
  1387. NumLabel: test.tags,
  1388. }
  1389. if got := filter(&s); got != test.want {
  1390. t.Fatalf("got %v, want %v", got, test.want)
  1391. }
  1392. })
  1393. }
  1394. }
  1395. type testSymbolzMergeFetcher struct{}
  1396. func (testSymbolzMergeFetcher) Fetch(s string, d, t time.Duration) (*profile.Profile, string, error) {
  1397. var p *profile.Profile
  1398. switch s {
  1399. case testSourceURL(8000) + "symbolz":
  1400. p = symzProfile()
  1401. case testSourceURL(8001) + "symbolz":
  1402. p = symzProfile()
  1403. p.Mapping[0].Start += testOffset
  1404. p.Mapping[0].Limit += testOffset
  1405. for i := range p.Location {
  1406. p.Location[i].Address += testOffset
  1407. }
  1408. default:
  1409. return nil, "", fmt.Errorf("unexpected source: %s", s)
  1410. }
  1411. return p, s, nil
  1412. }
  1413. func TestSymbolzAfterMerge(t *testing.T) {
  1414. baseVars := pprofVariables
  1415. pprofVariables = baseVars.makeCopy()
  1416. defer func() { pprofVariables = baseVars }()
  1417. f := baseFlags()
  1418. f.args = []string{
  1419. testSourceURL(8000) + "symbolz",
  1420. testSourceURL(8001) + "symbolz",
  1421. }
  1422. o := setDefaults(nil)
  1423. o.Flagset = f
  1424. o.Obj = new(mockObjTool)
  1425. src, cmd, err := parseFlags(o)
  1426. if err != nil {
  1427. t.Fatalf("parseFlags: %v", err)
  1428. }
  1429. if len(cmd) != 1 || cmd[0] != "proto" {
  1430. t.Fatalf("parseFlags returned command %v, want [proto]", cmd)
  1431. }
  1432. o.Fetch = testSymbolzMergeFetcher{}
  1433. o.Sym = testSymbolzSymbolizer{}
  1434. p, err := fetchProfiles(src, o)
  1435. if err != nil {
  1436. t.Fatalf("fetchProfiles: %v", err)
  1437. }
  1438. if len(p.Location) != 3 {
  1439. t.Errorf("Got %d locations after merge, want %d", len(p.Location), 3)
  1440. }
  1441. for i, l := range p.Location {
  1442. if len(l.Line) != 1 {
  1443. t.Errorf("Number of lines for symbolz %#x in iteration %d, got %d, want %d", l.Address, i, len(l.Line), 1)
  1444. continue
  1445. }
  1446. address := l.Address - l.Mapping.Start
  1447. if got, want := l.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
  1448. t.Errorf("symbolz %#x, got %s, want %s", address, got, want)
  1449. }
  1450. }
  1451. }
  1452. type mockObjTool struct{}
  1453. func (*mockObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  1454. return &mockFile{file, "abcdef", 0}, nil
  1455. }
  1456. func (m *mockObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  1457. switch start {
  1458. case 0x1000:
  1459. return []plugin.Inst{
  1460. {Addr: 0x1000, Text: "instruction one", File: "file1000.src", Line: 1},
  1461. {Addr: 0x1001, Text: "instruction two", File: "file1000.src", Line: 1},
  1462. {Addr: 0x1002, Text: "instruction three", File: "file1000.src", Line: 2},
  1463. {Addr: 0x1003, Text: "instruction four", File: "file1000.src", Line: 1},
  1464. }, nil
  1465. case 0x3000:
  1466. return []plugin.Inst{
  1467. {Addr: 0x3000, Text: "instruction one"},
  1468. {Addr: 0x3001, Text: "instruction two"},
  1469. {Addr: 0x3002, Text: "instruction three"},
  1470. {Addr: 0x3003, Text: "instruction four"},
  1471. {Addr: 0x3004, Text: "instruction five"},
  1472. }, nil
  1473. }
  1474. return nil, fmt.Errorf("unimplemented")
  1475. }
  1476. type mockFile struct {
  1477. name, buildID string
  1478. base uint64
  1479. }
  1480. // Name returns the underlyinf file name, if available
  1481. func (m *mockFile) Name() string {
  1482. return m.name
  1483. }
  1484. // Base returns the base address to use when looking up symbols in the file.
  1485. func (m *mockFile) Base() uint64 {
  1486. return m.base
  1487. }
  1488. // BuildID returns the GNU build ID of the file, or an empty string.
  1489. func (m *mockFile) BuildID() string {
  1490. return m.buildID
  1491. }
  1492. // SourceLine reports the source line information for a given
  1493. // address in the file. Due to inlining, the source line information
  1494. // is in general a list of positions representing a call stack,
  1495. // with the leaf function first.
  1496. func (*mockFile) SourceLine(addr uint64) ([]plugin.Frame, error) {
  1497. return nil, fmt.Errorf("unimplemented")
  1498. }
  1499. // Symbols returns a list of symbols in the object file.
  1500. // If r is not nil, Symbols restricts the list to symbols
  1501. // with names matching the regular expression.
  1502. // If addr is not zero, Symbols restricts the list to symbols
  1503. // containing that address.
  1504. func (m *mockFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  1505. switch r.String() {
  1506. case "line[13]":
  1507. return []*plugin.Sym{
  1508. {
  1509. Name: []string{"line1000"}, File: m.name,
  1510. Start: 0x1000, End: 0x1003,
  1511. },
  1512. {
  1513. Name: []string{"line3000"}, File: m.name,
  1514. Start: 0x3000, End: 0x3004,
  1515. },
  1516. }, nil
  1517. }
  1518. return nil, fmt.Errorf("unimplemented")
  1519. }
  1520. // Close closes the file, releasing associated resources.
  1521. func (*mockFile) Close() error {
  1522. return nil
  1523. }