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

Side by Side Diff: src/heap-snapshot-generator.cc

Issue 27227005: Record allocation stack traces (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 2 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 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #include "heap-snapshot-generator-inl.h" 30 #include "heap-snapshot-generator-inl.h"
31 31
32 #include "allocation-tracker.h"
32 #include "heap-profiler.h" 33 #include "heap-profiler.h"
33 #include "debug.h" 34 #include "debug.h"
34 #include "types.h" 35 #include "types.h"
35 36
36 namespace v8 { 37 namespace v8 {
37 namespace internal { 38 namespace internal {
38 39
39 40
40 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) 41 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to)
41 : type_(type), 42 : type_(type),
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 ids_(heap) { 628 ids_(heap) {
628 } 629 }
629 630
630 631
631 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { 632 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
632 delete *snapshot_ptr; 633 delete *snapshot_ptr;
633 } 634 }
634 635
635 636
636 HeapSnapshotsCollection::~HeapSnapshotsCollection() { 637 HeapSnapshotsCollection::~HeapSnapshotsCollection() {
638 delete allocation_tracker_;
637 snapshots_.Iterate(DeleteHeapSnapshot); 639 snapshots_.Iterate(DeleteHeapSnapshot);
638 } 640 }
639 641
640 642
643 void HeapSnapshotsCollection::StartHeapObjectsTracking() {
644 ids_.UpdateHeapObjectsMap();
645 if (allocation_tracker_ == NULL) {
646 allocation_tracker_ = new AllocationTracker(&ids_, names());
647 }
648 is_tracking_objects_ = true;
649 }
650
651
652 void HeapSnapshotsCollection::StopHeapObjectsTracking() {
653 ids_.StopHeapObjectsTracking();
654 if (allocation_tracker_ != NULL) {
655 allocation_tracker_->allocation_traces()->Print();
656 delete allocation_tracker_;
657 allocation_tracker_ = NULL;
658 }
659 }
660
661
641 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, 662 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name,
642 unsigned uid) { 663 unsigned uid) {
643 is_tracking_objects_ = true; // Start watching for heap objects moves. 664 is_tracking_objects_ = true; // Start watching for heap objects moves.
644 return new HeapSnapshot(this, name, uid); 665 return new HeapSnapshot(this, name, uid);
645 } 666 }
646 667
647 668
648 void HeapSnapshotsCollection::SnapshotGenerationFinished( 669 void HeapSnapshotsCollection::SnapshotGenerationFinished(
649 HeapSnapshot* snapshot) { 670 HeapSnapshot* snapshot) {
650 ids_.SnapshotGenerationFinished(); 671 ids_.SnapshotGenerationFinished();
(...skipping 23 matching lines...) Expand all
674 if (ids_.FindEntry(obj->address()) == id) { 695 if (ids_.FindEntry(obj->address()) == id) {
675 ASSERT(object == NULL); 696 ASSERT(object == NULL);
676 object = obj; 697 object = obj;
677 // Can't break -- kFilterUnreachable requires full heap traversal. 698 // Can't break -- kFilterUnreachable requires full heap traversal.
678 } 699 }
679 } 700 }
680 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); 701 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
681 } 702 }
682 703
683 704
705 void HeapSnapshotsCollection::NewObjectEvent(Address addr, int size) {
706 DisallowHeapAllocation no_allocation;
707 ids_.NewObject(addr, size);
708 if (allocation_tracker_ != NULL) {
709 allocation_tracker_->NewObjectEvent(addr, size);
710 }
711 }
712
713
684 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { 714 size_t HeapSnapshotsCollection::GetUsedMemorySize() const {
685 size_t size = sizeof(*this); 715 size_t size = sizeof(*this);
686 size += names_.GetUsedMemorySize(); 716 size += names_.GetUsedMemorySize();
687 size += ids_.GetUsedMemorySize(); 717 size += ids_.GetUsedMemorySize();
688 size += GetMemoryUsedByList(snapshots_); 718 size += GetMemoryUsedByList(snapshots_);
689 for (int i = 0; i < snapshots_.length(); ++i) { 719 for (int i = 0; i < snapshots_.length(); ++i) {
690 size += snapshots_[i]->RawSnapshotSize(); 720 size += snapshots_[i]->RawSnapshotSize();
691 } 721 }
692 return size; 722 return size;
693 } 723 }
(...skipping 1778 matching lines...) Expand 10 before | Expand all | Expand 10 after
2472 bool aborted_; 2502 bool aborted_;
2473 }; 2503 };
2474 2504
2475 2505
2476 // type, name|index, to_node. 2506 // type, name|index, to_node.
2477 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; 2507 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3;
2478 // type, name, id, self_size, children_index. 2508 // type, name, id, self_size, children_index.
2479 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; 2509 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5;
2480 2510
2481 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { 2511 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) {
2512 if (AllocationTracker* allocation_tracker =
2513 snapshot_->collection()->allocation_tracker()) {
2514 allocation_tracker->PrepareForSerialization();
2515 }
2482 ASSERT(writer_ == NULL); 2516 ASSERT(writer_ == NULL);
2483 writer_ = new OutputStreamWriter(stream); 2517 writer_ = new OutputStreamWriter(stream);
2484 SerializeImpl(); 2518 SerializeImpl();
2485 delete writer_; 2519 delete writer_;
2486 writer_ = NULL; 2520 writer_ = NULL;
2487 } 2521 }
2488 2522
2489 2523
2490 void HeapSnapshotJSONSerializer::SerializeImpl() { 2524 void HeapSnapshotJSONSerializer::SerializeImpl() {
2491 ASSERT(0 == snapshot_->root()->index()); 2525 ASSERT(0 == snapshot_->root()->index());
2492 writer_->AddCharacter('{'); 2526 writer_->AddCharacter('{');
2493 writer_->AddString("\"snapshot\":{"); 2527 writer_->AddString("\"snapshot\":{");
2494 SerializeSnapshot(); 2528 SerializeSnapshot();
2495 if (writer_->aborted()) return; 2529 if (writer_->aborted()) return;
2496 writer_->AddString("},\n"); 2530 writer_->AddString("},\n");
2497 writer_->AddString("\"nodes\":["); 2531 writer_->AddString("\"nodes\":[");
2498 SerializeNodes(); 2532 SerializeNodes();
2499 if (writer_->aborted()) return; 2533 if (writer_->aborted()) return;
2500 writer_->AddString("],\n"); 2534 writer_->AddString("],\n");
2501 writer_->AddString("\"edges\":["); 2535 writer_->AddString("\"edges\":[");
2502 SerializeEdges(); 2536 SerializeEdges();
2503 if (writer_->aborted()) return; 2537 if (writer_->aborted()) return;
2504 writer_->AddString("],\n"); 2538 writer_->AddString("],\n");
2539
2540 writer_->AddString("\"trace_function_infos\":[");
2541 SerializeTraceNodeInfos();
2542 if (writer_->aborted()) return;
2543 writer_->AddString("],\n");
2544 writer_->AddString("\"trace_tree\":[");
2545 SerializeTraceTree();
2546 if (writer_->aborted()) return;
2547 writer_->AddString("],\n");
2548
2505 writer_->AddString("\"strings\":["); 2549 writer_->AddString("\"strings\":[");
2506 SerializeStrings(); 2550 SerializeStrings();
2507 if (writer_->aborted()) return; 2551 if (writer_->aborted()) return;
2508 writer_->AddCharacter(']'); 2552 writer_->AddCharacter(']');
2509 writer_->AddCharacter('}'); 2553 writer_->AddCharacter('}');
2510 writer_->Finalize(); 2554 writer_->Finalize();
2511 } 2555 }
2512 2556
2513 2557
2514 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { 2558 int HeapSnapshotJSONSerializer::GetStringId(const char* s) {
2559 if (s == NULL) s = "";
2515 HashMap::Entry* cache_entry = strings_.Lookup( 2560 HashMap::Entry* cache_entry = strings_.Lookup(
2516 const_cast<char*>(s), StringHash(s), true); 2561 const_cast<char*>(s), StringHash(s), true);
2517 if (cache_entry->value == NULL) { 2562 if (cache_entry->value == NULL) {
2518 cache_entry->value = reinterpret_cast<void*>(next_string_id_++); 2563 cache_entry->value = reinterpret_cast<void*>(next_string_id_++);
2519 } 2564 }
2520 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); 2565 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value));
2521 } 2566 }
2522 2567
2523 2568
2524 static int utoa(unsigned value, const Vector<char>& buffer, int buffer_pos) { 2569 static int utoa(unsigned value, const Vector<char>& buffer, int buffer_pos) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
2655 JSON_S("edge_types") ":" JSON_A( 2700 JSON_S("edge_types") ":" JSON_A(
2656 JSON_A( 2701 JSON_A(
2657 JSON_S("context") "," 2702 JSON_S("context") ","
2658 JSON_S("element") "," 2703 JSON_S("element") ","
2659 JSON_S("property") "," 2704 JSON_S("property") ","
2660 JSON_S("internal") "," 2705 JSON_S("internal") ","
2661 JSON_S("hidden") "," 2706 JSON_S("hidden") ","
2662 JSON_S("shortcut") "," 2707 JSON_S("shortcut") ","
2663 JSON_S("weak")) "," 2708 JSON_S("weak")) ","
2664 JSON_S("string_or_number") "," 2709 JSON_S("string_or_number") ","
2665 JSON_S("node")))); 2710 JSON_S("node")) ","
2711 JSON_S("trace_function_info_fields") ":" JSON_A(
2712 JSON_S("function_id") ","
2713 JSON_S("name") ","
2714 JSON_S("script_name") ","
2715 JSON_S("script_id") ","
2716 JSON_S("line") ","
2717 JSON_S("column")) ","
2718 JSON_S("trace_node_fields") ":" JSON_A(
2719 JSON_S("id") ","
2720 JSON_S("function_id") ","
2721 JSON_S("count") ","
2722 JSON_S("size") ","
2723 JSON_S("children"))));
2666 #undef JSON_S 2724 #undef JSON_S
2667 #undef JSON_O 2725 #undef JSON_O
2668 #undef JSON_A 2726 #undef JSON_A
2669 writer_->AddString(",\"node_count\":"); 2727 writer_->AddString(",\"node_count\":");
2670 writer_->AddNumber(snapshot_->entries().length()); 2728 writer_->AddNumber(snapshot_->entries().length());
2671 writer_->AddString(",\"edge_count\":"); 2729 writer_->AddString(",\"edge_count\":");
2672 writer_->AddNumber(snapshot_->edges().length()); 2730 writer_->AddNumber(snapshot_->edges().length());
2731 writer_->AddString(",\"trace_function_count\":");
2732 uint32_t count = 0;
2733 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker();
2734 if (tracker) {
2735 count = tracker->id_to_function_info()->occupancy();
2736 }
2737 writer_->AddNumber(count);
2673 } 2738 }
2674 2739
2675 2740
2676 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { 2741 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) {
2677 static const char hex_chars[] = "0123456789ABCDEF"; 2742 static const char hex_chars[] = "0123456789ABCDEF";
2678 w->AddString("\\u"); 2743 w->AddString("\\u");
2679 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); 2744 w->AddCharacter(hex_chars[(u >> 12) & 0xf]);
2680 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); 2745 w->AddCharacter(hex_chars[(u >> 8) & 0xf]);
2681 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); 2746 w->AddCharacter(hex_chars[(u >> 4) & 0xf]);
2682 w->AddCharacter(hex_chars[u & 0xf]); 2747 w->AddCharacter(hex_chars[u & 0xf]);
2683 } 2748 }
2684 2749
2685 2750
2751 void HeapSnapshotJSONSerializer::SerializeTraceTree() {
2752 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker();
2753 if (!tracker) return;
2754 AllocationTraceTree* traces = tracker->allocation_traces();
2755 SerializeTraceNode(traces->root());
2756 }
2757
2758
2759 void HeapSnapshotJSONSerializer::SerializeTraceNode(AllocationTraceNode* node) {
2760 // The buffer needs space for 4 unsigned ints, 4 commas, [ and \0
2761 const int kBufferSize =
2762 4 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT
2763 + 4 + 1 + 1;
2764 EmbeddedVector<char, kBufferSize> buffer;
loislo 2013/10/16 15:14:08 looks like you missed tracker check here.
yurys 2013/10/17 15:27:35 The check is performed in its caller SerializeTrac
2765 int buffer_pos = 0;
2766 buffer_pos = utoa(node->id(), buffer, buffer_pos);
2767 buffer[buffer_pos++] = ',';
2768 buffer_pos = utoa(node->function_id(), buffer, buffer_pos);
2769 buffer[buffer_pos++] = ',';
2770 buffer_pos = utoa(node->allocation_count(), buffer, buffer_pos);
2771 buffer[buffer_pos++] = ',';
2772 buffer_pos = utoa(node->allocation_size(), buffer, buffer_pos);
2773 buffer[buffer_pos++] = ',';
2774 buffer[buffer_pos++] = '[';
2775 buffer[buffer_pos++] = '\0';
2776 writer_->AddString(buffer.start());
2777
2778 Vector<AllocationTraceNode*> children = node->children();
2779 for (int i = 0; i < children.length(); i++) {
2780 if (i > 0) {
2781 writer_->AddCharacter(',');
2782 }
2783 SerializeTraceNode(children[i]);
2784 }
2785 writer_->AddCharacter(']');
2786 }
2787
2788
2789 static int SerializePosition(int position, const Vector<char>& buffer,
2790 int buffer_pos) {
2791 if (position == -1) {
2792 // FIXME: serialize sifned int.
2793 buffer[buffer_pos++] = '0';
2794 } else {
2795 ASSERT(position >= 0);
2796 buffer_pos = utoa(static_cast<unsigned>(position), buffer, buffer_pos);
2797 }
2798 return buffer_pos;
2799 }
2800
2801 void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() {
2802 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker();
2803 if (!tracker) return;
2804 // The buffer needs space for 6 unsigned ints, 6 commas, \n and \0
2805 const int kBufferSize =
2806 6 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT
2807 + 6 + 1 + 1;
2808 EmbeddedVector<char, kBufferSize> buffer;
2809 HashMap* id_to_function_info = tracker->id_to_function_info();
2810 bool first_entry = true;
2811 for (HashMap::Entry* p = id_to_function_info->Start();
2812 p != NULL;
2813 p = id_to_function_info->Next(p)) {
2814 SnapshotObjectId id =
2815 static_cast<SnapshotObjectId>(reinterpret_cast<intptr_t>(p->key));
2816 AllocationTracker::FunctionInfo* info =
2817 reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value);
2818 int buffer_pos = 0;
2819 if (first_entry) {
2820 first_entry = false;
2821 } else {
2822 buffer[buffer_pos++] = ',';
2823 }
2824 buffer_pos = utoa(id, buffer, buffer_pos);
2825 buffer[buffer_pos++] = ',';
2826 buffer_pos = utoa(GetStringId(info->name), buffer, buffer_pos);
2827 buffer[buffer_pos++] = ',';
2828 buffer_pos = utoa(GetStringId(info->script_name), buffer, buffer_pos);
2829 buffer[buffer_pos++] = ',';
2830 // The cast is safe because script id is a non-negative Smi.
2831 buffer_pos = utoa(static_cast<unsigned>(info->script_id), buffer,
2832 buffer_pos);
2833 buffer[buffer_pos++] = ',';
2834 buffer_pos = SerializePosition(info->line, buffer, buffer_pos);
2835 buffer[buffer_pos++] = ',';
2836 buffer_pos = SerializePosition(info->column, buffer, buffer_pos);
2837 buffer[buffer_pos++] = '\n';
2838 buffer[buffer_pos++] = '\0';
2839 writer_->AddString(buffer.start());
2840 }
2841 }
2842
2843
2686 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { 2844 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) {
2687 writer_->AddCharacter('\n'); 2845 writer_->AddCharacter('\n');
2688 writer_->AddCharacter('\"'); 2846 writer_->AddCharacter('\"');
2689 for ( ; *s != '\0'; ++s) { 2847 for ( ; *s != '\0'; ++s) {
2690 switch (*s) { 2848 switch (*s) {
2691 case '\b': 2849 case '\b':
2692 writer_->AddString("\\b"); 2850 writer_->AddString("\\b");
2693 continue; 2851 continue;
2694 case '\f': 2852 case '\f':
2695 writer_->AddString("\\f"); 2853 writer_->AddString("\\f");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2745 writer_->AddString("\"<dummy>\""); 2903 writer_->AddString("\"<dummy>\"");
2746 for (int i = 1; i < sorted_strings.length(); ++i) { 2904 for (int i = 1; i < sorted_strings.length(); ++i) {
2747 writer_->AddCharacter(','); 2905 writer_->AddCharacter(',');
2748 SerializeString(sorted_strings[i]); 2906 SerializeString(sorted_strings[i]);
2749 if (writer_->aborted()) return; 2907 if (writer_->aborted()) return;
2750 } 2908 }
2751 } 2909 }
2752 2910
2753 2911
2754 } } // namespace v8::internal 2912 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698