| Index: runtime/vm/heap_trace_test.cc
|
| diff --git a/runtime/vm/heap_trace_test.cc b/runtime/vm/heap_trace_test.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..45b1524c3b9d1f03817c930a0d326b409284db50
|
| --- /dev/null
|
| +++ b/runtime/vm/heap_trace_test.cc
|
| @@ -0,0 +1,107 @@
|
| +// Copyright 2012 Google Inc. All Rights Reserved.
|
| +// Author: nricci@google.com (Nathan Ricci)
|
| +//
|
| +#include <sstream>
|
| +#include <iostream>
|
| +#include <string>
|
| +#include "vm/growable_array.h"
|
| +#include "vm/heap_trace.h"
|
| +#include "vm/unit_test.h"
|
| +#include "vm/heap.h"
|
| +#include "vm/dart_api_impl.h"
|
| +
|
| +
|
| +namespace dart {
|
| +
|
| +static std::stringstream* the_stream;
|
| +
|
| +static void* OpenTraceFile(const char* name) {
|
| + return reinterpret_cast<void*>(the_stream);
|
| +}
|
| +
|
| +
|
| +static void WriteToTraceFile(const void* buffer,
|
| + intptr_t num_bytes,
|
| + void* stream) {
|
| + std::stringstream* s_stream = (std::stringstream*)stream;
|
| + s_stream->write(reinterpret_cast<const char*>(buffer), num_bytes);
|
| +}
|
| +
|
| +
|
| +static void CloseTraceFile(void *stream) {
|
| +}
|
| +
|
| +
|
| +bool DoesAllocationRecordExist(uword addr, std::string trace_string) {
|
| + const char* raw_trace = trace_string.c_str();
|
| + for (unsigned int i = 0; i < trace_string.length(); i++) {
|
| + if (raw_trace[i] == 'A' && i + 4 < trace_string.length()) {
|
| + const uword candidate_address =
|
| + *(reinterpret_cast<const uword*>(raw_trace+i+1));
|
| + if (candidate_address == addr) {
|
| + return true;
|
| + }
|
| + }
|
| + }
|
| +
|
| + return false;
|
| +}
|
| +
|
| +
|
| +bool DoesSweepRecordExist(uword addr, std::string trace_string) {
|
| + const char* raw_trace = trace_string.c_str();
|
| + for (unsigned int i = 0; i < trace_string.length(); i++) {
|
| + if (raw_trace[i] == 'S' && i + 4 < trace_string.length()) {
|
| + const uword candidate_address =
|
| + *(reinterpret_cast<const uword*>(raw_trace+i+1));
|
| + if (candidate_address == addr) {
|
| + return true;
|
| + }
|
| + }
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +
|
| +TEST_CASE(GCTraceAllocate) {
|
| + const int kArrayLen = 5;
|
| + the_stream = new std::stringstream();
|
| + HeapTrace::InitTracing(OpenTraceFile
|
| + , WriteToTraceFile
|
| + , CloseTraceFile
|
| + , "test_prefix");
|
| +
|
| + Isolate* isolate = Isolate::Current();
|
| + isolate->heap()->trace()->InitializeIsolateTracing(isolate);
|
| +
|
| + RawArray* raw_arr = Array::New(kArrayLen);
|
| + uword addr = RawObject::ToAddr(raw_arr);
|
| +
|
| +
|
| + ASSERT(DoesAllocationRecordExist(addr, the_stream->str()));
|
| + delete the_stream;
|
| +}
|
| +
|
| +TEST_CASE(GCTraceSweep) {
|
| + the_stream = new std::stringstream();
|
| + const int kArrayLen = 5;
|
| +
|
| + HeapTrace::InitTracing(OpenTraceFile,
|
| + WriteToTraceFile,
|
| + CloseTraceFile,
|
| + "test_prefix");
|
| +
|
| +
|
| + Isolate* isolate = Isolate::Current();
|
| + isolate->heap()->trace()->InitializeIsolateTracing(isolate);
|
| +
|
| + RawArray* raw_arr = Array::New(kArrayLen, Heap::kOld);
|
| + uword addr = RawObject::ToAddr(raw_arr);
|
| +
|
| + Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
|
| + DoesSweepRecordExist(addr, the_stream->str());
|
| + delete the_stream;
|
| +}
|
| +
|
| +
|
| +} // namespace dart
|
|
|