Aucune description

driver_test.go 45KB

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