설명 없음

profile_test.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 profile
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "path/filepath"
  20. "regexp"
  21. "strings"
  22. "testing"
  23. "github.com/google/pprof/internal/proftest"
  24. )
  25. func TestParse(t *testing.T) {
  26. const path = "testdata/"
  27. for _, source := range []string{
  28. "go.crc32.cpu",
  29. "go.godoc.thread",
  30. "gobench.cpu",
  31. "gobench.heap",
  32. "cppbench.cpu",
  33. "cppbench.heap",
  34. "cppbench.contention",
  35. "cppbench.growth",
  36. "cppbench.thread",
  37. "cppbench.thread.all",
  38. "cppbench.thread.none",
  39. "java.cpu",
  40. "java.heap",
  41. "java.contention",
  42. } {
  43. inbytes, err := ioutil.ReadFile(filepath.Join(path, source))
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. p, err := Parse(bytes.NewBuffer(inbytes))
  48. if err != nil {
  49. t.Fatalf("%s: %s", source, err)
  50. }
  51. js := p.String()
  52. goldFilename := path + source + ".string"
  53. gold, err := ioutil.ReadFile(goldFilename)
  54. if err != nil {
  55. t.Fatalf("%s: %v", source, err)
  56. }
  57. if js != string(gold) {
  58. t.Errorf("diff %s %s", source, goldFilename)
  59. d, err := proftest.Diff(gold, []byte(js))
  60. if err != nil {
  61. t.Fatalf("%s: %v", source, err)
  62. }
  63. t.Error(source + "\n" + string(d) + "\n" + "new profile at:\n" + leaveTempfile([]byte(js)))
  64. }
  65. // Reencode and decode.
  66. bw := bytes.NewBuffer(nil)
  67. if err := p.Write(bw); err != nil {
  68. t.Fatalf("%s: %v", source, err)
  69. }
  70. if p, err = Parse(bw); err != nil {
  71. t.Fatalf("%s: %v", source, err)
  72. }
  73. js2 := p.String()
  74. if js2 != string(gold) {
  75. d, err := proftest.Diff(gold, []byte(js2))
  76. if err != nil {
  77. t.Fatalf("%s: %v", source, err)
  78. }
  79. t.Error(source + "\n" + string(d) + "\n" + "gold:\n" + goldFilename +
  80. "\nnew profile at:\n" + leaveTempfile([]byte(js)))
  81. }
  82. }
  83. }
  84. func TestParseError(t *testing.T) {
  85. testcases := []string{
  86. "",
  87. "garbage text",
  88. "\x1f\x8b", // truncated gzip header
  89. "\x1f\x8b\x08\x08\xbe\xe9\x20\x58\x00\x03\x65\x6d\x70\x74\x79\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // empty gzipped file
  90. }
  91. for i, input := range testcases {
  92. _, err := Parse(strings.NewReader(input))
  93. if err == nil {
  94. t.Errorf("got nil, want error for input #%d", i)
  95. }
  96. }
  97. }
  98. // leaveTempfile leaves |b| in a temporary file on disk and returns the
  99. // temp filename. This is useful to recover a profile when the test
  100. // fails.
  101. func leaveTempfile(b []byte) string {
  102. f1, err := ioutil.TempFile("", "profile_test")
  103. if err != nil {
  104. panic(err)
  105. }
  106. if _, err := f1.Write(b); err != nil {
  107. panic(err)
  108. }
  109. return f1.Name()
  110. }
  111. const mainBinary = "/bin/main"
  112. var cpuM = []*Mapping{
  113. {
  114. ID: 1,
  115. Start: 0x10000,
  116. Limit: 0x40000,
  117. File: mainBinary,
  118. HasFunctions: true,
  119. HasFilenames: true,
  120. HasLineNumbers: true,
  121. HasInlineFrames: true,
  122. },
  123. {
  124. ID: 2,
  125. Start: 0x1000,
  126. Limit: 0x4000,
  127. File: "/lib/lib.so",
  128. HasFunctions: true,
  129. HasFilenames: true,
  130. HasLineNumbers: true,
  131. HasInlineFrames: true,
  132. },
  133. {
  134. ID: 3,
  135. Start: 0x4000,
  136. Limit: 0x5000,
  137. File: "/lib/lib2_c.so.6",
  138. HasFunctions: true,
  139. HasFilenames: true,
  140. HasLineNumbers: true,
  141. HasInlineFrames: true,
  142. },
  143. {
  144. ID: 4,
  145. Start: 0x5000,
  146. Limit: 0x9000,
  147. File: "/lib/lib.so_6 (deleted)",
  148. HasFunctions: true,
  149. HasFilenames: true,
  150. HasLineNumbers: true,
  151. HasInlineFrames: true,
  152. },
  153. }
  154. var cpuF = []*Function{
  155. {ID: 1, Name: "main", SystemName: "main", Filename: "main.c"},
  156. {ID: 2, Name: "foo", SystemName: "foo", Filename: "foo.c"},
  157. {ID: 3, Name: "foo_caller", SystemName: "foo_caller", Filename: "foo.c"},
  158. }
  159. var cpuL = []*Location{
  160. {
  161. ID: 1000,
  162. Mapping: cpuM[1],
  163. Address: 0x1000,
  164. Line: []Line{
  165. {Function: cpuF[0], Line: 1},
  166. },
  167. },
  168. {
  169. ID: 2000,
  170. Mapping: cpuM[0],
  171. Address: 0x2000,
  172. Line: []Line{
  173. {Function: cpuF[1], Line: 2},
  174. {Function: cpuF[2], Line: 1},
  175. },
  176. },
  177. {
  178. ID: 3000,
  179. Mapping: cpuM[0],
  180. Address: 0x3000,
  181. Line: []Line{
  182. {Function: cpuF[1], Line: 2},
  183. {Function: cpuF[2], Line: 1},
  184. },
  185. },
  186. {
  187. ID: 3001,
  188. Mapping: cpuM[0],
  189. Address: 0x3001,
  190. Line: []Line{
  191. {Function: cpuF[2], Line: 2},
  192. },
  193. },
  194. {
  195. ID: 3002,
  196. Mapping: cpuM[0],
  197. Address: 0x3002,
  198. Line: []Line{
  199. {Function: cpuF[2], Line: 3},
  200. },
  201. },
  202. }
  203. var testProfile = &Profile{
  204. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  205. Period: 1,
  206. DurationNanos: 10e9,
  207. SampleType: []*ValueType{
  208. {Type: "samples", Unit: "count"},
  209. {Type: "cpu", Unit: "milliseconds"},
  210. },
  211. Sample: []*Sample{
  212. {
  213. Location: []*Location{cpuL[0]},
  214. Value: []int64{1000, 1000},
  215. Label: map[string][]string{
  216. "key1": []string{"tag1"},
  217. "key2": []string{"tag1"},
  218. },
  219. },
  220. {
  221. Location: []*Location{cpuL[1], cpuL[0]},
  222. Value: []int64{100, 100},
  223. Label: map[string][]string{
  224. "key1": []string{"tag2"},
  225. "key3": []string{"tag2"},
  226. },
  227. },
  228. {
  229. Location: []*Location{cpuL[2], cpuL[0]},
  230. Value: []int64{10, 10},
  231. Label: map[string][]string{
  232. "key1": []string{"tag3"},
  233. "key2": []string{"tag2"},
  234. },
  235. },
  236. {
  237. Location: []*Location{cpuL[3], cpuL[0]},
  238. Value: []int64{10000, 10000},
  239. Label: map[string][]string{
  240. "key1": []string{"tag4"},
  241. "key2": []string{"tag1"},
  242. },
  243. },
  244. {
  245. Location: []*Location{cpuL[4], cpuL[0]},
  246. Value: []int64{1, 1},
  247. Label: map[string][]string{
  248. "key1": []string{"tag4"},
  249. "key2": []string{"tag1"},
  250. },
  251. },
  252. },
  253. Location: cpuL,
  254. Function: cpuF,
  255. Mapping: cpuM,
  256. }
  257. var aggTests = map[string]aggTest{
  258. "precise": aggTest{true, true, true, true, 5},
  259. "fileline": aggTest{false, true, true, true, 4},
  260. "inline_function": aggTest{false, true, false, true, 3},
  261. "function": aggTest{false, true, false, false, 2},
  262. }
  263. type aggTest struct {
  264. precise, function, fileline, inlineFrame bool
  265. rows int
  266. }
  267. const totalSamples = int64(11111)
  268. func TestAggregation(t *testing.T) {
  269. prof := testProfile.Copy()
  270. for _, resolution := range []string{"precise", "fileline", "inline_function", "function"} {
  271. a := aggTests[resolution]
  272. if !a.precise {
  273. if err := prof.Aggregate(a.inlineFrame, a.function, a.fileline, a.fileline, false); err != nil {
  274. t.Error("aggregating to " + resolution + ":" + err.Error())
  275. }
  276. }
  277. if err := checkAggregation(prof, &a); err != nil {
  278. t.Error("failed aggregation to " + resolution + ": " + err.Error())
  279. }
  280. }
  281. }
  282. // checkAggregation verifies that the profile remained consistent
  283. // with its aggregation.
  284. func checkAggregation(prof *Profile, a *aggTest) error {
  285. // Check that the total number of samples for the rows was preserved.
  286. total := int64(0)
  287. samples := make(map[string]bool)
  288. for _, sample := range prof.Sample {
  289. tb := locationHash(sample)
  290. samples[tb] = true
  291. total += sample.Value[0]
  292. }
  293. if total != totalSamples {
  294. return fmt.Errorf("sample total %d, want %d", total, totalSamples)
  295. }
  296. // Check the number of unique sample locations
  297. if a.rows != len(samples) {
  298. return fmt.Errorf("number of samples %d, want %d", len(samples), a.rows)
  299. }
  300. // Check that all mappings have the right detail flags.
  301. for _, m := range prof.Mapping {
  302. if m.HasFunctions != a.function {
  303. return fmt.Errorf("unexpected mapping.HasFunctions %v, want %v", m.HasFunctions, a.function)
  304. }
  305. if m.HasFilenames != a.fileline {
  306. return fmt.Errorf("unexpected mapping.HasFilenames %v, want %v", m.HasFilenames, a.fileline)
  307. }
  308. if m.HasLineNumbers != a.fileline {
  309. return fmt.Errorf("unexpected mapping.HasLineNumbers %v, want %v", m.HasLineNumbers, a.fileline)
  310. }
  311. if m.HasInlineFrames != a.inlineFrame {
  312. return fmt.Errorf("unexpected mapping.HasInlineFrames %v, want %v", m.HasInlineFrames, a.inlineFrame)
  313. }
  314. }
  315. // Check that aggregation has removed finer resolution data.
  316. for _, l := range prof.Location {
  317. if !a.inlineFrame && len(l.Line) > 1 {
  318. return fmt.Errorf("found %d lines on location %d, want 1", len(l.Line), l.ID)
  319. }
  320. for _, ln := range l.Line {
  321. if !a.fileline && (ln.Function.Filename != "" || ln.Line != 0) {
  322. return fmt.Errorf("found line %s:%d on location %d, want :0",
  323. ln.Function.Filename, ln.Line, l.ID)
  324. }
  325. if !a.function && (ln.Function.Name != "") {
  326. return fmt.Errorf(`found file %s location %d, want ""`,
  327. ln.Function.Name, l.ID)
  328. }
  329. }
  330. }
  331. return nil
  332. }
  333. // Test merge leaves the main binary in place.
  334. func TestMergeMain(t *testing.T) {
  335. prof := testProfile.Copy()
  336. p1, err := Merge([]*Profile{prof})
  337. if err != nil {
  338. t.Fatalf("merge error: %v", err)
  339. }
  340. if cpuM[0].File != p1.Mapping[0].File {
  341. t.Errorf("want Mapping[0]=%s got %s", cpuM[0].File, p1.Mapping[0].File)
  342. }
  343. }
  344. func TestMerge(t *testing.T) {
  345. // Aggregate a profile with itself and once again with a factor of
  346. // -2. Should end up with an empty profile (all samples for a
  347. // location should add up to 0).
  348. prof := testProfile.Copy()
  349. p1, err := Merge([]*Profile{prof, prof})
  350. if err != nil {
  351. t.Errorf("merge error: %v", err)
  352. }
  353. prof.Scale(-2)
  354. prof, err = Merge([]*Profile{p1, prof})
  355. if err != nil {
  356. t.Errorf("merge error: %v", err)
  357. }
  358. // Use aggregation to merge locations at function granularity.
  359. if err := prof.Aggregate(false, true, false, false, false); err != nil {
  360. t.Errorf("aggregating after merge: %v", err)
  361. }
  362. samples := make(map[string]int64)
  363. for _, s := range prof.Sample {
  364. tb := locationHash(s)
  365. samples[tb] = samples[tb] + s.Value[0]
  366. }
  367. for s, v := range samples {
  368. if v != 0 {
  369. t.Errorf("nonzero value for sample %s: %d", s, v)
  370. }
  371. }
  372. }
  373. func TestMergeAll(t *testing.T) {
  374. // Aggregate 10 copies of the profile.
  375. profs := make([]*Profile, 10)
  376. for i := 0; i < 10; i++ {
  377. profs[i] = testProfile.Copy()
  378. }
  379. prof, err := Merge(profs)
  380. if err != nil {
  381. t.Errorf("merge error: %v", err)
  382. }
  383. samples := make(map[string]int64)
  384. for _, s := range prof.Sample {
  385. tb := locationHash(s)
  386. samples[tb] = samples[tb] + s.Value[0]
  387. }
  388. for _, s := range testProfile.Sample {
  389. tb := locationHash(s)
  390. if samples[tb] != s.Value[0]*10 {
  391. t.Errorf("merge got wrong value at %s : %d instead of %d", tb, samples[tb], s.Value[0]*10)
  392. }
  393. }
  394. }
  395. func TestFilter(t *testing.T) {
  396. // Perform several forms of filtering on the test profile.
  397. type filterTestcase struct {
  398. focus, ignore, hide, show *regexp.Regexp
  399. fm, im, hm, hnm bool
  400. }
  401. for tx, tc := range []filterTestcase{
  402. {nil, nil, nil, nil, true, false, false, false},
  403. {regexp.MustCompile("notfound"), nil, nil, nil, false, false, false, false},
  404. {nil, regexp.MustCompile("foo.c"), nil, nil, true, true, false, false},
  405. {nil, nil, regexp.MustCompile("lib.so"), nil, true, false, true, false},
  406. } {
  407. prof := *testProfile.Copy()
  408. gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
  409. if gf != tc.fm {
  410. t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
  411. }
  412. if gi != tc.im {
  413. t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
  414. }
  415. if gh != tc.hm {
  416. t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
  417. }
  418. if gnh != tc.hnm {
  419. t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
  420. }
  421. }
  422. }
  423. func TestTagFilter(t *testing.T) {
  424. // Perform several forms of tag filtering on the test profile.
  425. type filterTestcase struct {
  426. include, exclude *regexp.Regexp
  427. im, em bool
  428. count int
  429. }
  430. countTags := func(p *Profile) map[string]bool {
  431. tags := make(map[string]bool)
  432. for _, s := range p.Sample {
  433. for l := range s.Label {
  434. tags[l] = true
  435. }
  436. for l := range s.NumLabel {
  437. tags[l] = true
  438. }
  439. }
  440. return tags
  441. }
  442. for tx, tc := range []filterTestcase{
  443. {nil, nil, true, false, 3},
  444. {regexp.MustCompile("notfound"), nil, false, false, 0},
  445. {regexp.MustCompile("key1"), nil, true, false, 1},
  446. {nil, regexp.MustCompile("key[12]"), true, true, 1},
  447. } {
  448. prof := testProfile.Copy()
  449. gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
  450. if gim != tc.im {
  451. t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
  452. }
  453. if gem != tc.em {
  454. t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
  455. }
  456. if tags := countTags(prof); len(tags) != tc.count {
  457. t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
  458. }
  459. }
  460. }
  461. // locationHash constructs a string to use as a hashkey for a sample, based on its locations
  462. func locationHash(s *Sample) string {
  463. var tb string
  464. for _, l := range s.Location {
  465. for _, ln := range l.Line {
  466. tb = tb + fmt.Sprintf("%s:%d@%d ", ln.Function.Name, ln.Line, l.Address)
  467. }
  468. }
  469. return tb
  470. }
  471. func TestSetMain(t *testing.T) {
  472. testProfile.massageMappings()
  473. if testProfile.Mapping[0].File != mainBinary {
  474. t.Errorf("got %s for main", testProfile.Mapping[0].File)
  475. }
  476. }