OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // --- | 5 // --- |
6 // Author: Sainbayar Sukhbaatar | 6 // Author: Sainbayar Sukhbaatar |
7 // Dai Mikurube | 7 // Dai Mikurube |
8 // | 8 // |
9 | 9 |
10 #include "deep-heap-profile.h" | 10 #include "deep-heap-profile.h" |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 } | 222 } |
223 | 223 |
224 DeepHeapProfile::~DeepHeapProfile() { | 224 DeepHeapProfile::~DeepHeapProfile() { |
225 heap_profile_->dealloc_(profiler_buffer_); | 225 heap_profile_->dealloc_(profiler_buffer_); |
226 heap_profile_->dealloc_(filename_prefix_); | 226 heap_profile_->dealloc_(filename_prefix_); |
227 delete memory_residence_info_getter_; | 227 delete memory_residence_info_getter_; |
228 } | 228 } |
229 | 229 |
230 // Global malloc() should not be used in this function. | 230 // Global malloc() should not be used in this function. |
231 // Use LowLevelAlloc if required. | 231 // Use LowLevelAlloc if required. |
232 int DeepHeapProfile::FillOrderedProfile(char raw_buffer[], int buffer_size) { | 232 int DeepHeapProfile::FillOrderedProfile(const char* reason, |
| 233 char raw_buffer[], |
| 234 int buffer_size) { |
233 TextBuffer buffer(raw_buffer, buffer_size); | 235 TextBuffer buffer(raw_buffer, buffer_size); |
234 TextBuffer global_buffer(profiler_buffer_, kProfilerBufferSize); | 236 TextBuffer global_buffer(profiler_buffer_, kProfilerBufferSize); |
235 | 237 |
236 #ifndef NDEBUG | 238 #ifndef NDEBUG |
237 int64 starting_cycles = CycleClock::Now(); | 239 int64 starting_cycles = CycleClock::Now(); |
238 #endif | 240 #endif |
239 | 241 |
240 // Get the time before starting snapshot. | 242 // Get the time before starting snapshot. |
241 // TODO(dmikurube): Consider gettimeofday if available. | 243 // TODO(dmikurube): Consider gettimeofday if available. |
242 time_t time_value = time(NULL); | 244 time_t time_value = time(NULL); |
(...skipping 25 matching lines...) Expand all Loading... |
268 buffer.AppendString(kProfileVersion, 0); | 270 buffer.AppendString(kProfileVersion, 0); |
269 buffer.AppendString("\n", 0); | 271 buffer.AppendString("\n", 0); |
270 | 272 |
271 // Fill buffer with meta information. | 273 // Fill buffer with meta information. |
272 buffer.AppendString(kMetaInformationHeader, 0); | 274 buffer.AppendString(kMetaInformationHeader, 0); |
273 | 275 |
274 buffer.AppendString("Time: ", 0); | 276 buffer.AppendString("Time: ", 0); |
275 buffer.AppendUnsignedLong(time_value, 0); | 277 buffer.AppendUnsignedLong(time_value, 0); |
276 buffer.AppendChar('\n'); | 278 buffer.AppendChar('\n'); |
277 | 279 |
| 280 if (reason != NULL) { |
| 281 buffer.AppendString("Reason: ", 0); |
| 282 buffer.AppendString(reason, 0); |
| 283 buffer.AppendChar('\n'); |
| 284 } |
| 285 |
278 // Fill buffer with the global stats. | 286 // Fill buffer with the global stats. |
279 buffer.AppendString(kMMapListHeader, 0); | 287 buffer.AppendString(kMMapListHeader, 0); |
280 | 288 |
281 stats_.SnapshotMaps(memory_residence_info_getter_, this, &buffer); | 289 stats_.SnapshotMaps(memory_residence_info_getter_, this, &buffer); |
282 | 290 |
283 // Fill buffer with the global stats. | 291 // Fill buffer with the global stats. |
284 buffer.AppendString(kGlobalStatsHeader, 0); | 292 buffer.AppendString(kGlobalStatsHeader, 0); |
285 | 293 |
286 stats_.Unparse(&buffer); | 294 stats_.Unparse(&buffer); |
287 | 295 |
(...skipping 30 matching lines...) Expand all Loading... |
318 | 326 |
319 void DeepHeapProfile::TextBuffer::Clear() { | 327 void DeepHeapProfile::TextBuffer::Clear() { |
320 cursor_ = 0; | 328 cursor_ = 0; |
321 } | 329 } |
322 | 330 |
323 void DeepHeapProfile::TextBuffer::Write(RawFD fd) { | 331 void DeepHeapProfile::TextBuffer::Write(RawFD fd) { |
324 RawWrite(fd, buffer_, cursor_); | 332 RawWrite(fd, buffer_, cursor_); |
325 } | 333 } |
326 | 334 |
327 // TODO(dmikurube): These Append* functions should not use snprintf. | 335 // TODO(dmikurube): These Append* functions should not use snprintf. |
328 bool DeepHeapProfile::TextBuffer::AppendChar(char v) { | 336 bool DeepHeapProfile::TextBuffer::AppendChar(char value) { |
329 return ForwardCursor(snprintf(buffer_ + cursor_, size_ - cursor_, "%c", v)); | 337 return ForwardCursor(snprintf(buffer_ + cursor_, size_ - cursor_, |
| 338 "%c", value)); |
330 } | 339 } |
331 | 340 |
332 bool DeepHeapProfile::TextBuffer::AppendString(const char* s, int d) { | 341 bool DeepHeapProfile::TextBuffer::AppendString(const char* value, int width) { |
| 342 char* position = buffer_ + cursor_; |
| 343 int available = size_ - cursor_; |
333 int appended; | 344 int appended; |
334 if (d == 0) | 345 if (width == 0) |
335 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%s", s); | 346 appended = snprintf(position, available, "%s", value); |
336 else | 347 else |
337 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%*s", d, s); | 348 appended = snprintf(position, available, "%*s", |
| 349 width, value); |
338 return ForwardCursor(appended); | 350 return ForwardCursor(appended); |
339 } | 351 } |
340 | 352 |
341 bool DeepHeapProfile::TextBuffer::AppendInt(int v, int d, bool leading_zero) { | 353 bool DeepHeapProfile::TextBuffer::AppendInt(int value, int width, |
| 354 bool leading_zero) { |
| 355 char* position = buffer_ + cursor_; |
| 356 int available = size_ - cursor_; |
342 int appended; | 357 int appended; |
343 if (d == 0) | 358 if (width == 0) |
344 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%d", v); | 359 appended = snprintf(position, available, "%d", value); |
345 else if (leading_zero) | 360 else if (leading_zero) |
346 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%0*d", d, v); | 361 appended = snprintf(position, available, "%0*d", width, value); |
347 else | 362 else |
348 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%*d", d, v); | 363 appended = snprintf(position, available, "%*d", width, value); |
349 return ForwardCursor(appended); | 364 return ForwardCursor(appended); |
350 } | 365 } |
351 | 366 |
352 bool DeepHeapProfile::TextBuffer::AppendLong(long v, int d) { | 367 bool DeepHeapProfile::TextBuffer::AppendLong(long value, int width) { |
| 368 char* position = buffer_ + cursor_; |
| 369 int available = size_ - cursor_; |
353 int appended; | 370 int appended; |
354 if (d == 0) | 371 if (width == 0) |
355 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%ld", v); | 372 appended = snprintf(position, available, "%ld", value); |
356 else | 373 else |
357 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%*ld", d, v); | 374 appended = snprintf(position, available, "%*ld", width, value); |
358 return ForwardCursor(appended); | 375 return ForwardCursor(appended); |
359 } | 376 } |
360 | 377 |
361 bool DeepHeapProfile::TextBuffer::AppendUnsignedLong(unsigned long v, int d) { | 378 bool DeepHeapProfile::TextBuffer::AppendUnsignedLong(unsigned long value, |
| 379 int width) { |
| 380 char* position = buffer_ + cursor_; |
| 381 int available = size_ - cursor_; |
362 int appended; | 382 int appended; |
363 if (d == 0) | 383 if (width == 0) |
364 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%lu", v); | 384 appended = snprintf(position, available, "%lu", value); |
365 else | 385 else |
366 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%*lu", d, v); | 386 appended = snprintf(position, available, "%*lu", width, value); |
367 return ForwardCursor(appended); | 387 return ForwardCursor(appended); |
368 } | 388 } |
369 | 389 |
370 bool DeepHeapProfile::TextBuffer::AppendInt64(int64 v, int d) { | 390 bool DeepHeapProfile::TextBuffer::AppendInt64(int64 value, int width) { |
| 391 char* position = buffer_ + cursor_; |
| 392 int available = size_ - cursor_; |
371 int appended; | 393 int appended; |
372 if (d == 0) | 394 if (width == 0) |
373 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%"PRId64, v); | 395 appended = snprintf(position, available, "%"PRId64, value); |
374 else | 396 else |
375 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%*"PRId64, d, v); | 397 appended = snprintf(position, available, "%*"PRId64, width, value); |
376 return ForwardCursor(appended); | 398 return ForwardCursor(appended); |
377 } | 399 } |
378 | 400 |
379 bool DeepHeapProfile::TextBuffer::AppendPtr(uint64 v, int d) { | 401 bool DeepHeapProfile::TextBuffer::AppendPtr(uint64 value, int width) { |
| 402 char* position = buffer_ + cursor_; |
| 403 int available = size_ - cursor_; |
380 int appended; | 404 int appended; |
381 if (d == 0) | 405 if (width == 0) |
382 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%"PRIx64, v); | 406 appended = snprintf(position, available, "%"PRIx64, value); |
383 else | 407 else |
384 appended = snprintf(buffer_ + cursor_, size_ - cursor_, "%0*"PRIx64, d, v); | 408 appended = snprintf(position, available, "%0*"PRIx64, width, value); |
385 return ForwardCursor(appended); | 409 return ForwardCursor(appended); |
386 } | 410 } |
387 | 411 |
388 bool DeepHeapProfile::TextBuffer::ForwardCursor(int appended) { | 412 bool DeepHeapProfile::TextBuffer::ForwardCursor(int appended) { |
389 if (appended < 0 || appended >= size_ - cursor_) | 413 if (appended < 0 || appended >= size_ - cursor_) |
390 return false; | 414 return false; |
391 cursor_ += appended; | 415 cursor_ += appended; |
392 return true; | 416 return true; |
393 } | 417 } |
394 | 418 |
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 } | 982 } |
959 | 983 |
960 DeepHeapProfile::~DeepHeapProfile() { | 984 DeepHeapProfile::~DeepHeapProfile() { |
961 } | 985 } |
962 | 986 |
963 int DeepHeapProfile::FillOrderedProfile(char raw_buffer[], int buffer_size) { | 987 int DeepHeapProfile::FillOrderedProfile(char raw_buffer[], int buffer_size) { |
964 return heap_profile_->FillOrderedProfile(raw_buffer, buffer_size); | 988 return heap_profile_->FillOrderedProfile(raw_buffer, buffer_size); |
965 } | 989 } |
966 | 990 |
967 #endif // USE_DEEP_HEAP_PROFILE | 991 #endif // USE_DEEP_HEAP_PROFILE |
OLD | NEW |