| Index: runtime/vm/trace_buffer.cc
 | 
| diff --git a/runtime/vm/trace_buffer.cc b/runtime/vm/trace_buffer.cc
 | 
| index 831e95d6abd74a5d45a79d9ff3a807a25fb9f004..93f0558071039aad70901adb6ee7904673b72595 100644
 | 
| --- a/runtime/vm/trace_buffer.cc
 | 
| +++ b/runtime/vm/trace_buffer.cc
 | 
| @@ -8,7 +8,10 @@
 | 
|  
 | 
|  namespace dart {
 | 
|  
 | 
| -TraceBuffer::TraceBuffer(intptr_t capacity) : ring_capacity_(capacity) {
 | 
| +TraceBuffer::TraceBuffer(intptr_t initial_capacity, intptr_t maximum_capacity)
 | 
| +    : capacity_(initial_capacity),
 | 
| +      max_capacity_(maximum_capacity) {
 | 
| +  size_ = 0;
 | 
|    ring_cursor_ = 0;
 | 
|    Init();
 | 
|  }
 | 
| @@ -23,18 +26,37 @@ TraceBuffer::~TraceBuffer() {
 | 
|  
 | 
|  void TraceBuffer::Init() {
 | 
|    ring_ = reinterpret_cast<TraceBufferEntry*>(
 | 
| -      calloc(ring_capacity_, sizeof(TraceBufferEntry)));  // NOLINT
 | 
| +      calloc(capacity_, sizeof(TraceBufferEntry)));  // NOLINT
 | 
| +}
 | 
| +
 | 
| +
 | 
| +void TraceBuffer::Resize(intptr_t new_capacity) {
 | 
| +  ASSERT(new_capacity > capacity_);
 | 
| +  if (new_capacity > max_capacity_) {
 | 
| +    // Clamp to max_capacity_.
 | 
| +    new_capacity = max_capacity_;
 | 
| +  }
 | 
| +  ASSERT(new_capacity <= max_capacity_);
 | 
| +  ring_ = reinterpret_cast<TraceBufferEntry*>(
 | 
| +      realloc(ring_, sizeof(*ring_) * new_capacity));
 | 
| +  // Initialize new TraceBufferEntries.
 | 
| +  for (intptr_t i = capacity_; i < new_capacity; i++) {
 | 
| +    ring_[i].micros = 0;
 | 
| +    ring_[i].message = NULL;
 | 
| +  }
 | 
| +  capacity_ = new_capacity;
 | 
|  }
 | 
|  
 | 
|  
 | 
|  void TraceBuffer::Clear() {
 | 
| -  for (intptr_t i = 0; i < ring_capacity_; i++) {
 | 
| +  for (intptr_t i = 0; i < capacity_; i++) {
 | 
|      TraceBufferEntry& entry = ring_[i];
 | 
|      entry.micros = 0;
 | 
|      free(entry.message);
 | 
|      entry.message = NULL;
 | 
|    }
 | 
|    ring_cursor_ = 0;
 | 
| +  size_ = 0;
 | 
|  }
 | 
|  
 | 
|  
 | 
| @@ -49,6 +71,14 @@ void TraceBuffer::Fill(TraceBufferEntry* entry, int64_t micros, char* msg) {
 | 
|  
 | 
|  
 | 
|  void TraceBuffer::AppendTrace(int64_t micros, char* message) {
 | 
| +  if (size_ < capacity_) {
 | 
| +    size_++;
 | 
| +    if ((size_ == capacity_) &&
 | 
| +        (capacity_ < max_capacity_)) {
 | 
| +      // Double size.
 | 
| +      Resize(capacity_ * 2);
 | 
| +    }
 | 
| +  }
 | 
|    const intptr_t index = ring_cursor_;
 | 
|    TraceBufferEntry* trace_entry = &ring_[index];
 | 
|    Fill(trace_entry, micros, message);
 | 
| @@ -83,15 +113,15 @@ void TraceBuffer::TraceF(const char* format, ...) {
 | 
|  }
 | 
|  
 | 
|  
 | 
| -void TraceBuffer::PrintToJSONStream(JSONStream* stream) const {
 | 
| -  JSONObject json_trace_buffer(stream);
 | 
| -  json_trace_buffer.AddProperty("type", "TraceBuffer");
 | 
| +void TraceBuffer::PrintToJSONObject(JSONObject* json_trace_buffer) const {
 | 
| +  json_trace_buffer->AddProperty("type", "TraceBuffer");
 | 
| +  json_trace_buffer->AddProperty("id", "");
 | 
|    // TODO(johnmccutchan): Send cursor position in response.
 | 
| -  JSONArray json_trace_buffer_array(&json_trace_buffer, "members");
 | 
| +  JSONArray json_trace_buffer_array(json_trace_buffer, "members");
 | 
|    // Scan forward until we find the first entry which isn't empty.
 | 
|    // TODO(johnmccutchan): Accept cursor start position as input.
 | 
|    intptr_t start = -1;
 | 
| -  for (intptr_t i = 0; i < ring_capacity_; i++) {
 | 
| +  for (intptr_t i = 0; i < capacity_; i++) {
 | 
|      intptr_t index = RingIndex(i + ring_cursor_);
 | 
|      if (!ring_[index].empty()) {
 | 
|        start = index;
 | 
| @@ -102,7 +132,7 @@ void TraceBuffer::PrintToJSONStream(JSONStream* stream) const {
 | 
|    if (start == -1) {
 | 
|      return;
 | 
|    }
 | 
| -  for (intptr_t i = 0; i < ring_capacity_; i++) {
 | 
| +  for (intptr_t i = 0; i < capacity_; i++) {
 | 
|      intptr_t index = RingIndex(start + i);
 | 
|      const TraceBufferEntry& entry = ring_[index];
 | 
|      if (entry.empty()) {
 | 
| @@ -111,11 +141,18 @@ void TraceBuffer::PrintToJSONStream(JSONStream* stream) const {
 | 
|      }
 | 
|      JSONObject trace_entry(&json_trace_buffer_array);
 | 
|      trace_entry.AddProperty("type", "TraceBufferEntry");
 | 
| -    double seconds = static_cast<double>(entry.micros) /
 | 
| -                     static_cast<double>(kMicrosecondsPerSecond);
 | 
| -    trace_entry.AddProperty("time", seconds);
 | 
| +    trace_entry.AddProperty("id", "");
 | 
| +    intptr_t millis =
 | 
| +        static_cast<intptr_t>(entry.micros / kMicrosecondsPerMillisecond);
 | 
| +    trace_entry.AddProperty("time", millis);
 | 
|      trace_entry.AddProperty("message", entry.message);
 | 
|    }
 | 
|  }
 | 
|  
 | 
| +
 | 
| +void TraceBuffer::PrintToJSONStream(JSONStream* stream) const {
 | 
| +  JSONObject json_trace_buffer(stream);
 | 
| +  PrintToJSONObject(&json_trace_buffer);
 | 
| +}
 | 
| +
 | 
|  }  // namespace dart
 | 
| 
 |