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