Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

322 wiersze
8.8 KiB

  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. "reflect"
  19. "strconv"
  20. "strings"
  21. "testing"
  22. )
  23. func TestLegacyProfileType(t *testing.T) {
  24. type testcase struct {
  25. sampleTypes []string
  26. typeSet [][]string
  27. want bool
  28. setName string
  29. }
  30. heap := heapzSampleTypes
  31. cont := contentionzSampleTypes
  32. testcases := []testcase{
  33. // True cases
  34. {[]string{"allocations", "size"}, heap, true, "heapzSampleTypes"},
  35. {[]string{"objects", "space"}, heap, true, "heapzSampleTypes"},
  36. {[]string{"inuse_objects", "inuse_space"}, heap, true, "heapzSampleTypes"},
  37. {[]string{"alloc_objects", "alloc_space"}, heap, true, "heapzSampleTypes"},
  38. {[]string{"alloc_objects", "alloc_space", "inuse_objects", "inuse_space"}, heap, true, "heapzSampleTypes"},
  39. {[]string{"contentions", "delay"}, cont, true, "contentionzSampleTypes"},
  40. // False cases
  41. {[]string{"objects"}, heap, false, "heapzSampleTypes"},
  42. {[]string{"objects", "unknown"}, heap, false, "heapzSampleTypes"},
  43. {[]string{"inuse_objects", "inuse_space", "alloc_objects", "alloc_space"}, heap, false, "heapzSampleTypes"},
  44. {[]string{"contentions", "delay"}, heap, false, "heapzSampleTypes"},
  45. {[]string{"samples", "cpu"}, heap, false, "heapzSampleTypes"},
  46. {[]string{"samples", "cpu"}, cont, false, "contentionzSampleTypes"},
  47. }
  48. for _, tc := range testcases {
  49. p := profileOfType(tc.sampleTypes)
  50. if got := isProfileType(p, tc.typeSet); got != tc.want {
  51. t.Error("isProfileType({"+strings.Join(tc.sampleTypes, ",")+"},", tc.setName, "), got", got, "want", tc.want)
  52. }
  53. }
  54. }
  55. func TestCpuParse(t *testing.T) {
  56. // profileString is a legacy encoded profile, represnted by words separated by ":"
  57. // Each sample has the form value : N : stack1..stackN
  58. // EOF is represented as "0:1:0"
  59. profileString := "1:3:100:999:100:" // sample with bogus 999 and duplicate leaf
  60. profileString += "1:5:200:999:200:501:502:" // sample with bogus 999 and duplicate leaf
  61. profileString += "1:12:300:999:300:601:602:603:604:605:606:607:608:609:" // sample with bogus 999 and duplicate leaf
  62. profileString += "0:1:0000" // EOF -- must use 4 bytes for the final zero
  63. p, err := cpuProfile([]byte(profileString), 1, parseString)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if err := checkTestSample(p, []uint64{100}); err != nil {
  68. t.Error(err)
  69. }
  70. if err := checkTestSample(p, []uint64{200, 500, 501}); err != nil {
  71. t.Error(err)
  72. }
  73. if err := checkTestSample(p, []uint64{300, 600, 601, 602, 603, 604, 605, 606, 607, 608}); err != nil {
  74. t.Error(err)
  75. }
  76. }
  77. func parseString(b []byte) (uint64, []byte) {
  78. slices := bytes.SplitN(b, []byte(":"), 2)
  79. var value, remainder []byte
  80. if len(slices) > 0 {
  81. value = slices[0]
  82. }
  83. if len(slices) > 1 {
  84. remainder = slices[1]
  85. }
  86. v, _ := strconv.ParseUint(string(value), 10, 64)
  87. return v, remainder
  88. }
  89. func checkTestSample(p *Profile, want []uint64) error {
  90. for _, s := range p.Sample {
  91. got := []uint64{}
  92. for _, l := range s.Location {
  93. got = append(got, l.Address)
  94. }
  95. if reflect.DeepEqual(got, want) {
  96. return nil
  97. }
  98. }
  99. return fmt.Errorf("Could not find sample : %v", want)
  100. }
  101. // profileOfType creates an empty profile with only sample types set,
  102. // for testing purposes only.
  103. func profileOfType(sampleTypes []string) *Profile {
  104. p := new(Profile)
  105. p.SampleType = make([]*ValueType, len(sampleTypes))
  106. for i, t := range sampleTypes {
  107. p.SampleType[i] = new(ValueType)
  108. p.SampleType[i].Type = t
  109. }
  110. return p
  111. }
  112. func TestParseMappingEntry(t *testing.T) {
  113. for _, test := range []*struct {
  114. entry string
  115. want *Mapping
  116. }{
  117. {
  118. entry: "00400000-02e00000 r-xp 00000000 00:00 0",
  119. want: &Mapping{
  120. Start: 0x400000,
  121. Limit: 0x2e00000,
  122. },
  123. },
  124. {
  125. entry: "02e00000-02e8a000 r-xp 02a00000 00:00 15953927 /foo/bin",
  126. want: &Mapping{
  127. Start: 0x2e00000,
  128. Limit: 0x2e8a000,
  129. Offset: 0x2a00000,
  130. File: "/foo/bin",
  131. },
  132. },
  133. {
  134. entry: "02e00000-02e8a000 r-xp 000000 00:00 15953927 [vdso]",
  135. want: &Mapping{
  136. Start: 0x2e00000,
  137. Limit: 0x2e8a000,
  138. File: "[vdso]",
  139. },
  140. },
  141. {
  142. entry: " 02e00000-02e8a000: /foo/bin (@2a00000)",
  143. want: &Mapping{
  144. Start: 0x2e00000,
  145. Limit: 0x2e8a000,
  146. Offset: 0x2a00000,
  147. File: "/foo/bin",
  148. },
  149. },
  150. {
  151. entry: " 02e00000-02e8a000: /foo/bin (deleted)",
  152. want: &Mapping{
  153. Start: 0x2e00000,
  154. Limit: 0x2e8a000,
  155. File: "/foo/bin",
  156. },
  157. },
  158. {
  159. entry: " 02e00000-02e8a000: /foo/bin",
  160. want: &Mapping{
  161. Start: 0x2e00000,
  162. Limit: 0x2e8a000,
  163. File: "/foo/bin",
  164. },
  165. },
  166. {
  167. entry: " 02e00000-02e8a000: [vdso]",
  168. want: &Mapping{
  169. Start: 0x2e00000,
  170. Limit: 0x2e8a000,
  171. File: "[vdso]",
  172. },
  173. },
  174. {entry: "0xff6810563000 0xff6810565000 r-xp abc_exe 87c4d547f895cfd6a370e08dc5c5ee7bd4199d5b",
  175. want: &Mapping{
  176. Start: 0xff6810563000,
  177. Limit: 0xff6810565000,
  178. File: "abc_exe",
  179. BuildID: "87c4d547f895cfd6a370e08dc5c5ee7bd4199d5b",
  180. },
  181. },
  182. {entry: "7f5e5435e000-7f5e5455e000 --xp 00002000 00:00 1531 myprogram",
  183. want: &Mapping{
  184. Start: 0x7f5e5435e000,
  185. Limit: 0x7f5e5455e000,
  186. Offset: 0x2000,
  187. File: "myprogram",
  188. },
  189. },
  190. {entry: "7f7472710000-7f7472722000 r-xp 00000000 fc:00 790190 /usr/lib/libfantastic-1.2.so",
  191. want: &Mapping{
  192. Start: 0x7f7472710000,
  193. Limit: 0x7f7472722000,
  194. File: "/usr/lib/libfantastic-1.2.so",
  195. },
  196. },
  197. {entry: "7f47a542f000-7f47a5447000: /lib/libpthread-2.15.so",
  198. want: &Mapping{
  199. Start: 0x7f47a542f000,
  200. Limit: 0x7f47a5447000,
  201. File: "/lib/libpthread-2.15.so",
  202. },
  203. },
  204. {entry: "0x40000-0x80000 /path/to/binary (@FF00) abc123456",
  205. want: &Mapping{
  206. Start: 0x40000,
  207. Limit: 0x80000,
  208. File: "/path/to/binary",
  209. Offset: 0xFF00,
  210. BuildID: "abc123456",
  211. },
  212. },
  213. {entry: "W1220 15:07:15.201776 8272 logger.cc:12033] --- Memory map: ---\n" +
  214. "0x40000-0x80000 /path/to/binary (@FF00) abc123456",
  215. want: &Mapping{
  216. Start: 0x40000,
  217. Limit: 0x80000,
  218. File: "/path/to/binary",
  219. Offset: 0xFF00,
  220. BuildID: "abc123456",
  221. },
  222. },
  223. {entry: "W1220 15:07:15.201776 8272 logger.cc:12033] --- Memory map: ---\n" +
  224. "W1220 15:07:15.202776 8272 logger.cc:12036] 0x40000-0x80000 /path/to/binary (@FF00) abc123456",
  225. want: &Mapping{
  226. Start: 0x40000,
  227. Limit: 0x80000,
  228. File: "/path/to/binary",
  229. Offset: 0xFF00,
  230. BuildID: "abc123456",
  231. },
  232. },
  233. {entry: "7f5e5435e000-7f5e5455e000 ---p 00002000 00:00 1531 myprogram",
  234. want: nil,
  235. },
  236. } {
  237. got, err := ParseProcMaps(strings.NewReader(test.entry))
  238. if err != nil {
  239. t.Errorf("%s: %v", test.entry, err)
  240. continue
  241. }
  242. if test.want == nil {
  243. if got, want := len(got), 0; got != want {
  244. t.Errorf("%s: got %d mappings, want %d", test.entry, got, want)
  245. }
  246. continue
  247. }
  248. if got, want := len(got), 1; got != want {
  249. t.Errorf("%s: got %d mappings, want %d", test.entry, got, want)
  250. continue
  251. }
  252. if !reflect.DeepEqual(test.want, got[0]) {
  253. t.Errorf("%s want=%v got=%v", test.entry, test.want, got[0])
  254. }
  255. }
  256. }
  257. func TestParseThreadProfileWithInvalidAddress(t *testing.T) {
  258. profile := `
  259. --- threadz 1 ---
  260. --- Thread 7eff063d9940 (name: main/25376) stack: ---
  261. PC: 0x40b688 0x4d5f51 0x40be31 0x473add693e639c6f0
  262. --- Memory map: ---
  263. 00400000-00fcb000: /home/rsilvera/cppbench/cppbench_server_main.unstripped
  264. `
  265. wantErr := "failed to parse as hex 64-bit number: 0x473add693e639c6f0"
  266. if _, gotErr := parseThread([]byte(profile)); !strings.Contains(gotErr.Error(), wantErr) {
  267. t.Errorf("parseThread(): got error %q, want error containing %q", gotErr, wantErr)
  268. }
  269. }
  270. func TestParseGoCount(t *testing.T) {
  271. for _, test := range []struct {
  272. in string
  273. typ string
  274. }{
  275. {
  276. in: `# ignored comment
  277. threadcreate profile: total 123
  278. `,
  279. typ: "threadcreate",
  280. },
  281. {
  282. in: `
  283. # ignored comment
  284. goroutine profile: total 123456
  285. `,
  286. typ: "goroutine",
  287. },
  288. {
  289. in: `
  290. sub/dir-ect_o.ry profile: total 999
  291. `,
  292. typ: "sub/dir-ect_o.ry",
  293. },
  294. } {
  295. t.Run(test.typ, func(t *testing.T) {
  296. p, err := parseGoCount([]byte(test.in))
  297. if err != nil {
  298. t.Fatalf("parseGoCount(%q) = %v", test.in, err)
  299. }
  300. if typ := p.PeriodType.Type; typ != test.typ {
  301. t.Fatalf("parseGoCount(%q).PeriodType.Type = %q want %q", test.in, typ, test.typ)
  302. }
  303. })
  304. }
  305. }