Nav apraksta

driver_test.go 44KB

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