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 <iostream> |
| 6 #include <sstream> |
| 7 #include <string> |
| 8 #include "vm/dart_api_impl.h" |
| 9 #include "vm/growable_array.h" |
| 10 #include "vm/heap.h" |
| 11 #include "vm/heap_trace.h" |
| 12 #include "vm/unit_test.h" |
| 13 |
| 14 namespace dart { |
| 15 |
| 16 static std::stringstream* global_stream; |
| 17 |
| 18 static void* OpenTraceFile(const char* name) { |
| 19 ASSERT(global_stream == NULL); |
| 20 global_stream = new std::stringstream; |
| 21 return reinterpret_cast<void*>(global_stream); |
| 22 } |
| 23 |
| 24 |
| 25 static void WriteToTraceFile(const void* data, intptr_t length, void* stream) { |
| 26 ASSERT(stream == global_stream); |
| 27 std::stringstream* sstream = reinterpret_cast<std::stringstream*>(stream); |
| 28 sstream->write(reinterpret_cast<const char*>(data), length); |
| 29 } |
| 30 |
| 31 |
| 32 static void CloseTraceFile(void *stream) { |
| 33 ASSERT(stream == global_stream); |
| 34 global_stream = NULL; |
| 35 delete reinterpret_cast<std::stringstream*>(stream); |
| 36 } |
| 37 |
| 38 |
| 39 bool DoesAllocationRecordExist(uword addr, const std::string& trace_string) { |
| 40 const char* raw_trace = trace_string.c_str(); |
| 41 for (size_t i = 0; i < trace_string.length(); ++i) { |
| 42 if ((raw_trace[i] == 'A') && (i + 4 < trace_string.length())) { |
| 43 const uword candidate_address = |
| 44 *(reinterpret_cast<const uword*>(raw_trace + i + 1)); |
| 45 if (candidate_address == addr) { |
| 46 return true; |
| 47 } |
| 48 } |
| 49 } |
| 50 return false; |
| 51 } |
| 52 |
| 53 |
| 54 bool DoesSweepRecordExist(uword addr, const std::string& trace_string) { |
| 55 const char* raw_trace = trace_string.c_str(); |
| 56 for (size_t i = 0; i < trace_string.length(); ++i) { |
| 57 if ((raw_trace[i] == 'S') && (i + 4 < trace_string.length())) { |
| 58 const uword candidate_address = |
| 59 *(reinterpret_cast<const uword*>(raw_trace + i + 1)); |
| 60 if (candidate_address == addr) { |
| 61 return true; |
| 62 } |
| 63 } |
| 64 } |
| 65 return false; |
| 66 } |
| 67 |
| 68 |
| 69 TEST_CASE(GCTraceAllocate) { |
| 70 HeapTrace::InitOnce(OpenTraceFile, |
| 71 WriteToTraceFile, |
| 72 CloseTraceFile); |
| 73 |
| 74 Isolate* isolate = Isolate::Current(); |
| 75 isolate->heap()->trace()->Init(isolate); |
| 76 |
| 77 const int kArrayLen = 5; |
| 78 RawArray* raw_arr = Array::New(kArrayLen); |
| 79 uword addr = RawObject::ToAddr(raw_arr); |
| 80 |
| 81 ASSERT(DoesAllocationRecordExist(addr, global_stream->str())); |
| 82 } |
| 83 |
| 84 |
| 85 TEST_CASE(GCTraceSweep) { |
| 86 HeapTrace::InitOnce(OpenTraceFile, |
| 87 WriteToTraceFile, |
| 88 CloseTraceFile); |
| 89 |
| 90 Isolate* isolate = Isolate::Current(); |
| 91 isolate->heap()->trace()->Init(isolate); |
| 92 |
| 93 const int kArrayLen = 5; |
| 94 RawArray* raw_arr = Array::New(kArrayLen, Heap::kOld); |
| 95 uword addr = RawObject::ToAddr(raw_arr); |
| 96 |
| 97 Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
| 98 DoesSweepRecordExist(addr, global_stream->str()); |
| 99 } |
| 100 |
| 101 } // namespace dart |
OLD | NEW |