OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "platform/assert.h" | |
6 #include "vm/heap_profiler.h" | |
7 #include "vm/growable_array.h" | |
8 #include "vm/unit_test.h" | |
9 | |
10 namespace dart { | |
11 | |
12 static void WriteCallback(const void* data, intptr_t length, void* stream) { | |
13 GrowableArray<uint8_t>* array = | |
14 reinterpret_cast<GrowableArray<uint8_t>*>(stream); | |
15 for (intptr_t i = 0; i < length; ++i) { | |
16 array->Add(reinterpret_cast<const uint8_t*>(data)[i]); | |
17 } | |
18 } | |
19 | |
20 | |
21 static uint8_t Read8(GrowableArray<uint8_t>* array, intptr_t* i) { | |
22 EXPECT(array != NULL); | |
23 EXPECT(i != NULL); | |
24 EXPECT_LE(*i + 1, array->length()); | |
25 return (*array)[(*i)++]; | |
26 } | |
27 | |
28 | |
29 static uint32_t Read32(GrowableArray<uint8_t>* array, intptr_t* i) { | |
30 EXPECT(array != NULL); | |
31 EXPECT(i != NULL); | |
32 EXPECT_LE(*i + 4, array->length()); | |
33 uint32_t result = 0; | |
34 result |= ((*array)[(*i)++] << 0); | |
35 result |= ((*array)[(*i)++] << 8); | |
36 result |= ((*array)[(*i)++] << 16); | |
37 result |= ((*array)[(*i)++] << 24); | |
38 return ntohl(result); | |
39 } | |
40 | |
41 | |
42 static bool IsTagValid(uint8_t tag) { | |
43 switch (static_cast<HeapProfiler::Tag>(tag)) { | |
44 case HeapProfiler::kStringInUtf8: | |
45 case HeapProfiler::kLoadClass: | |
46 case HeapProfiler::kUnloadClass: | |
47 case HeapProfiler::kStackFrame: | |
48 case HeapProfiler::kStackTrace: | |
49 case HeapProfiler::kAllocSites: | |
50 case HeapProfiler::kHeapSummary: | |
51 case HeapProfiler::kStartThread: | |
52 case HeapProfiler::kEndThread: | |
53 case HeapProfiler::kHeapDump: | |
54 case HeapProfiler::kCpuSamples: | |
55 case HeapProfiler::kControlSettings: | |
56 case HeapProfiler::kHeapDumpSummary: | |
57 case HeapProfiler::kHeapDumpEnd: | |
58 return true; | |
59 default: | |
60 return false; | |
61 } | |
62 } | |
63 | |
64 | |
65 // Write an empty profile. Validate the presence of a header and a | |
66 // minimal set of records. | |
67 TEST_CASE(HeapProfileEmpty) { | |
68 uint64_t before = OS::GetCurrentTimeMillis(); | |
69 GrowableArray<uint8_t> array; | |
70 { | |
71 HeapProfiler(WriteCallback, &array); | |
72 } | |
73 uint64_t after = OS::GetCurrentTimeMillis(); | |
74 intptr_t i = 0; | |
75 EXPECT_LE(i + 19, array.length()); | |
76 EXPECT_EQ('J', array[i++]); | |
77 EXPECT_EQ('A', array[i++]); | |
78 EXPECT_EQ('V', array[i++]); | |
79 EXPECT_EQ('A', array[i++]); | |
80 EXPECT_EQ(' ', array[i++]); | |
81 EXPECT_EQ('P', array[i++]); | |
82 EXPECT_EQ('R', array[i++]); | |
83 EXPECT_EQ('O', array[i++]); | |
84 EXPECT_EQ('F', array[i++]); | |
85 EXPECT_EQ('I', array[i++]); | |
86 EXPECT_EQ('L', array[i++]); | |
87 EXPECT_EQ('E', array[i++]); | |
88 EXPECT_EQ(' ', array[i++]); | |
89 EXPECT_EQ('1', array[i++]); | |
90 EXPECT_EQ('.', array[i++]); | |
91 EXPECT_EQ('0', array[i++]); | |
92 EXPECT_EQ('.', array[i++]); | |
93 EXPECT_EQ('1', array[i++]); | |
94 EXPECT_EQ('\0', array[i++]); | |
95 uint32_t size = Read32(&array, &i); | |
96 EXPECT_EQ(8u, size); | |
97 uint64_t hi = Read32(&array, &i); | |
98 uint64_t lo = Read32(&array, &i); | |
99 uint64_t time = (hi << 32) | lo; | |
100 EXPECT_LE(before, time); | |
101 EXPECT_GE(after, time); | |
102 while (i != array.length()) { | |
103 // Check tag | |
104 uint8_t tag = Read8(&array, &i); | |
105 EXPECT(IsTagValid(tag)); | |
106 // Check time diff | |
107 uint32_t time_diff = Read32(&array, &i); | |
108 EXPECT_LE(before, time + time_diff); | |
109 EXPECT_GE(after, time + time_diff); | |
110 // Check length diff | |
111 uint32_t length = Read32(&array, &i); | |
112 EXPECT_LE((intptr_t)length + i , array.length()); | |
113 // skip body | |
114 i += length; | |
115 } | |
116 } | |
117 | |
118 } // namespace dart | |
OLD | NEW |