Нема описа

driver_test.go 43KB

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