Chromium Code Reviews| Index: runtime/vm/heap_histogram.cc |
| diff --git a/runtime/vm/heap_histogram.cc b/runtime/vm/heap_histogram.cc |
| index 09fd5cdacb78b68a4aef6fec75ab98f271bf580c..a4f7c160ea9f6e26df7588abc3566dd00ba31be7 100644 |
| --- a/runtime/vm/heap_histogram.cc |
| +++ b/runtime/vm/heap_histogram.cc |
| @@ -7,6 +7,7 @@ |
| #include "platform/assert.h" |
| #include "vm/flags.h" |
| #include "vm/object.h" |
| +#include "vm/json_stream.h" |
| namespace dart { |
| @@ -38,7 +39,7 @@ ObjectHistogram::ObjectHistogram(Isolate* isolate) { |
| major_gc_count_ = 0; |
| table_length_ = 512; |
| table_ = reinterpret_cast<Element*>( |
| - calloc(table_length_, sizeof(Element))); // NOLINT |
| + calloc(table_length_, sizeof(Element))); // NOLINT |
| for (int index = 0; index < table_length_; index++) { |
| table_[index].class_id_ = index; |
| } |
| @@ -81,10 +82,7 @@ int ObjectHistogram::compare(const Element** a, const Element** b) { |
| } |
| -void ObjectHistogram::Print() { |
| - OS::Print("Printing Object Histogram\n"); |
| - OS::Print("____bytes___count_description____________\n"); |
| - // First count the number of non empty entries. |
| +ObjectHistogram::Element** ObjectHistogram::GetSortedArray(int* array_length) { |
| int length = 0; |
| for (int index = 0; index < table_length_; index++) { |
| if (table_[index].count_ > 0) length++; |
| @@ -100,11 +98,26 @@ void ObjectHistogram::Print() { |
| qsort(array, length, sizeof(Element*), // NOLINT |
| reinterpret_cast<CmpFunc>(compare)); |
| + *array_length = length; |
| + return array; |
| +} |
| + |
| +void ObjectHistogram::Print() { |
| + OS::Print("Printing Object Histogram\n"); |
| + OS::Print("____bytes___count_description____________\n"); |
| + // First count the number of non empty entries. |
| + |
| + int length = 0; |
| + Element** array = NULL; |
| + |
| + array = GetSortedArray(&length); |
| + ASSERT(array != NULL); |
| + |
| // Finally print the sorted array. |
| Class& cls = Class::Handle(); |
| String& str = String::Handle(); |
| Library& lib = Library::Handle(); |
| - for (pos = 0; pos < length; pos++) { |
| + for (int pos = 0; pos < length; pos++) { |
|
siva
2013/08/01 18:16:18
Ditto question about 'int' here.
Cutch
2013/08/01 22:22:04
Legacy code used int. I'll change it to intptr_t.
|
| Element* e = array[pos]; |
| if (e->count_ > 0) { |
| cls = isolate_->class_table()->At(e->class_id_); |
| @@ -130,4 +143,59 @@ void ObjectHistogram::Print() { |
| free(array); |
| } |
| +void ObjectHistogram::PrintToJSONStream(JSONStream* stream) { |
| + int length = 0; |
| + Element** array = NULL; |
| + |
| + array = GetSortedArray(&length); |
| + ASSERT(array != NULL); |
| + |
| + // Finally print the sorted array. |
| + Class& cls = Class::Handle(); |
| + String& str = String::Handle(); |
| + Library& lib = Library::Handle(); |
| + |
| + intptr_t size_sum = 0; |
| + intptr_t count_sum = 0; |
| + stream->OpenObject(); |
| + stream->PrintProperty("type", "ObjectHistogram"); |
| + stream->OpenArray("properties"); |
| + stream->PrintValue("size"); |
| + stream->PrintValue("count"); |
| + stream->CloseArray(); |
| + stream->OpenArray("members"); |
| + for (int pos = 0; pos < length; pos++) { |
| + Element* e = array[pos]; |
| + if (e->count_ > 0) { |
| + cls = isolate_->class_table()->At(e->class_id_); |
| + str = cls.Name(); |
| + lib = cls.library(); |
| + stream->OpenObject(); |
| + stream->PrintProperty("type", "ObjectHistogramEntry"); |
| + size_sum += e->size_ / major_gc_count_; |
| + count_sum += e->count_ / major_gc_count_; |
|
siva
2013/08/01 18:16:18
(e->size_ / major_gc_count_) and (e->count_ / majo
Cutch
2013/08/01 22:22:04
Done.
|
| + stream->PrintProperty("size", e->size_ / major_gc_count_); |
| + stream->PrintProperty("count", e->count_ / major_gc_count_); |
| + stream->PrintProperty("name", str.ToCString()); |
| + if (lib.IsNull()) { |
| + stream->PrintProperty("category", ""); |
| + } else { |
| + str = lib.url(); |
| + stream->PrintProperty("category", str.ToCString()); |
| + } |
| + stream->CloseObject(); |
| + } |
| + } |
| + stream->CloseArray(); |
| + stream->OpenObject("sums"); |
| + stream->PrintProperty("size", size_sum); |
| + stream->PrintProperty("count", count_sum); |
| + stream->CloseObject(); |
| + stream->CloseObject(); |
| + |
| + // Deallocate the array for sorting. |
| + free(array); |
| +} |
| + |
| + |
| } // namespace dart |