Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: runtime/vm/heap_histogram.cc

Issue 19870006: Support stacktrace and objecthistogram service commands (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/heap_histogram.h" 5 #include "vm/heap_histogram.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/flags.h" 8 #include "vm/flags.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/json_stream.h"
10 11
11 namespace dart { 12 namespace dart {
12 13
13 DEFINE_FLAG(bool, print_object_histogram, false, 14 DEFINE_FLAG(bool, print_object_histogram, false,
14 "Print average object histogram at isolate shutdown"); 15 "Print average object histogram at isolate shutdown");
15 16
16 class ObjectHistogramVisitor : public ObjectVisitor { 17 class ObjectHistogramVisitor : public ObjectVisitor {
17 public: 18 public:
18 explicit ObjectHistogramVisitor(Isolate* isolate) : ObjectVisitor(isolate) { } 19 explicit ObjectHistogramVisitor(Isolate* isolate) : ObjectVisitor(isolate) { }
19 20
(...skipping 11 matching lines...) Expand all
31 ObjectHistogramVisitor object_visitor(isolate_); 32 ObjectHistogramVisitor object_visitor(isolate_);
32 isolate_->heap()->IterateObjects(&object_visitor); 33 isolate_->heap()->IterateObjects(&object_visitor);
33 } 34 }
34 35
35 36
36 ObjectHistogram::ObjectHistogram(Isolate* isolate) { 37 ObjectHistogram::ObjectHistogram(Isolate* isolate) {
37 isolate_ = isolate; 38 isolate_ = isolate;
38 major_gc_count_ = 0; 39 major_gc_count_ = 0;
39 table_length_ = 512; 40 table_length_ = 512;
40 table_ = reinterpret_cast<Element*>( 41 table_ = reinterpret_cast<Element*>(
41 calloc(table_length_, sizeof(Element))); // NOLINT 42 calloc(table_length_, sizeof(Element))); // NOLINT
42 for (int index = 0; index < table_length_; index++) { 43 for (int index = 0; index < table_length_; index++) {
43 table_[index].class_id_ = index; 44 table_[index].class_id_ = index;
44 } 45 }
45 } 46 }
46 47
47 48
48 ObjectHistogram::~ObjectHistogram() { 49 ObjectHistogram::~ObjectHistogram() {
49 free(table_); 50 free(table_);
50 } 51 }
51 52
(...skipping 22 matching lines...) Expand all
74 ASSERT(class_id < table_length_); 75 ASSERT(class_id < table_length_);
75 table_[class_id].Add(obj->Size()); 76 table_[class_id].Add(obj->Size());
76 } 77 }
77 78
78 79
79 int ObjectHistogram::compare(const Element** a, const Element** b) { 80 int ObjectHistogram::compare(const Element** a, const Element** b) {
80 return (*b)->size_ - (*a)->size_; 81 return (*b)->size_ - (*a)->size_;
81 } 82 }
82 83
83 84
84 void ObjectHistogram::Print() { 85 ObjectHistogram::Element** ObjectHistogram::GetSortedArray(int* array_length) {
85 OS::Print("Printing Object Histogram\n");
86 OS::Print("____bytes___count_description____________\n");
87 // First count the number of non empty entries.
88 int length = 0; 86 int length = 0;
89 for (int index = 0; index < table_length_; index++) { 87 for (int index = 0; index < table_length_; index++) {
90 if (table_[index].count_ > 0) length++; 88 if (table_[index].count_ > 0) length++;
91 } 89 }
92 // Then add them to a new array and sort. 90 // Then add them to a new array and sort.
93 Element** array = reinterpret_cast<Element**>( 91 Element** array = reinterpret_cast<Element**>(
94 calloc(length, sizeof(Element*))); // NOLINT 92 calloc(length, sizeof(Element*))); // NOLINT
95 int pos = 0; 93 int pos = 0;
96 for (int index = 0; index < table_length_; index++) { 94 for (int index = 0; index < table_length_; index++) {
97 if (table_[index].count_ > 0) array[pos++] = &table_[index]; 95 if (table_[index].count_ > 0) array[pos++] = &table_[index];
98 } 96 }
99 typedef int (*CmpFunc)(const void*, const void*); 97 typedef int (*CmpFunc)(const void*, const void*);
100 qsort(array, length, sizeof(Element*), // NOLINT 98 qsort(array, length, sizeof(Element*), // NOLINT
101 reinterpret_cast<CmpFunc>(compare)); 99 reinterpret_cast<CmpFunc>(compare));
102 100
101 *array_length = length;
102 return array;
103 }
104
105 void ObjectHistogram::Print() {
106 OS::Print("Printing Object Histogram\n");
107 OS::Print("____bytes___count_description____________\n");
108 // First count the number of non empty entries.
109
110 int length = 0;
111 Element** array = NULL;
112
113 array = GetSortedArray(&length);
114 ASSERT(array != NULL);
115
103 // Finally print the sorted array. 116 // Finally print the sorted array.
104 Class& cls = Class::Handle(); 117 Class& cls = Class::Handle();
105 String& str = String::Handle(); 118 String& str = String::Handle();
106 Library& lib = Library::Handle(); 119 Library& lib = Library::Handle();
107 for (pos = 0; pos < length; pos++) { 120 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.
108 Element* e = array[pos]; 121 Element* e = array[pos];
109 if (e->count_ > 0) { 122 if (e->count_ > 0) {
110 cls = isolate_->class_table()->At(e->class_id_); 123 cls = isolate_->class_table()->At(e->class_id_);
111 str = cls.Name(); 124 str = cls.Name();
112 lib = cls.library(); 125 lib = cls.library();
113 OS::Print("%9"Pd" %7"Pd" ", 126 OS::Print("%9"Pd" %7"Pd" ",
114 e->size_ / major_gc_count_, 127 e->size_ / major_gc_count_,
115 e->count_ / major_gc_count_); 128 e->count_ / major_gc_count_);
116 if (e->class_id_ < kInstanceCid) { 129 if (e->class_id_ < kInstanceCid) {
117 OS::Print("`%s`", str.ToCString()); // VM names. 130 OS::Print("`%s`", str.ToCString()); // VM names.
118 } else { 131 } else {
119 OS::Print("%s", str.ToCString()); 132 OS::Print("%s", str.ToCString());
120 } 133 }
121 if (lib.IsNull()) { 134 if (lib.IsNull()) {
122 OS::Print("\n"); 135 OS::Print("\n");
123 } else { 136 } else {
124 str = lib.url(); 137 str = lib.url();
125 OS::Print(", library \'%s\'\n", str.ToCString()); 138 OS::Print(", library \'%s\'\n", str.ToCString());
126 } 139 }
127 } 140 }
128 } 141 }
129 // Deallocate the array for sorting. 142 // Deallocate the array for sorting.
130 free(array); 143 free(array);
131 } 144 }
132 145
146 void ObjectHistogram::PrintToJSONStream(JSONStream* stream) {
147 int length = 0;
148 Element** array = NULL;
149
150 array = GetSortedArray(&length);
151 ASSERT(array != NULL);
152
153 // Finally print the sorted array.
154 Class& cls = Class::Handle();
155 String& str = String::Handle();
156 Library& lib = Library::Handle();
157
158 intptr_t size_sum = 0;
159 intptr_t count_sum = 0;
160 stream->OpenObject();
161 stream->PrintProperty("type", "ObjectHistogram");
162 stream->OpenArray("properties");
163 stream->PrintValue("size");
164 stream->PrintValue("count");
165 stream->CloseArray();
166 stream->OpenArray("members");
167 for (int pos = 0; pos < length; pos++) {
168 Element* e = array[pos];
169 if (e->count_ > 0) {
170 cls = isolate_->class_table()->At(e->class_id_);
171 str = cls.Name();
172 lib = cls.library();
173 stream->OpenObject();
174 stream->PrintProperty("type", "ObjectHistogramEntry");
175 size_sum += e->size_ / major_gc_count_;
176 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.
177 stream->PrintProperty("size", e->size_ / major_gc_count_);
178 stream->PrintProperty("count", e->count_ / major_gc_count_);
179 stream->PrintProperty("name", str.ToCString());
180 if (lib.IsNull()) {
181 stream->PrintProperty("category", "");
182 } else {
183 str = lib.url();
184 stream->PrintProperty("category", str.ToCString());
185 }
186 stream->CloseObject();
187 }
188 }
189 stream->CloseArray();
190 stream->OpenObject("sums");
191 stream->PrintProperty("size", size_sum);
192 stream->PrintProperty("count", count_sum);
193 stream->CloseObject();
194 stream->CloseObject();
195
196 // Deallocate the array for sorting.
197 free(array);
198 }
199
200
133 } // namespace dart 201 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698