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

Side by Side Diff: test/cctest/test-heap-profiler.cc

Issue 1555553002: [profiler] Implement POC Sampling Heap Profiler (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove commented code Created 4 years, 11 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
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 2834 matching lines...) Expand 10 before | Expand all | Expand 10 after
2845 map.AddRange(ToAddress(0x180), 0x80, 6U); 2845 map.AddRange(ToAddress(0x180), 0x80, 6U);
2846 map.AddRange(ToAddress(0x180), 0x80, 7U); 2846 map.AddRange(ToAddress(0x180), 0x80, 7U);
2847 CHECK_EQ(7u, map.GetTraceNodeId(ToAddress(0x180))); 2847 CHECK_EQ(7u, map.GetTraceNodeId(ToAddress(0x180)));
2848 CHECK_EQ(5u, map.GetTraceNodeId(ToAddress(0x200))); 2848 CHECK_EQ(5u, map.GetTraceNodeId(ToAddress(0x200)));
2849 CHECK_EQ(3u, map.size()); 2849 CHECK_EQ(3u, map.size());
2850 2850
2851 map.Clear(); 2851 map.Clear();
2852 CHECK_EQ(0u, map.size()); 2852 CHECK_EQ(0u, map.size());
2853 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x400))); 2853 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x400)));
2854 } 2854 }
2855
2856
2857 static const v8::AllocationProfile::Node* FindAllocationProfileNode(
2858 v8::AllocationProfile& profile, const Vector<const char*>& names) {
2859 v8::AllocationProfile::Node* node = profile.GetRootNode();
2860 for (int i = 0; node != nullptr && i < names.length(); ++i) {
2861 const char* name = names[i];
2862 auto children = node->children;
2863 node = nullptr;
2864 for (v8::AllocationProfile::Node* child : children) {
2865 v8::String::Utf8Value child_name(child->name);
2866 if (strcmp(*child_name, name) == 0) {
2867 node = child;
2868 break;
2869 }
2870 }
2871 }
2872 return node;
2873 }
2874
2875
2876 TEST(SamplingHeapProfiler) {
2877 v8::HandleScope scope(v8::Isolate::GetCurrent());
2878 LocalContext env;
2879 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2880
2881 // Turn off always_opt. Inlining can cause stack traces to be shorter than
2882 // what we expect in this test.
2883 v8::internal::FLAG_always_opt = false;
2884
2885 const char* script_source =
2886 "var A = [];\n"
2887 "function bar(size) { return new Array(size); }\n"
2888 "var foo = function() {\n"
2889 " for (var i = 0; i < 1024; ++i) {\n"
2890 " A[i] = bar(1024);\n"
2891 " }\n"
2892 "}\n"
2893 "foo();";
2894
2895 // Sample should be empty if requested before sampling has started.
2896 {
2897 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2898 CHECK(profile.GetRootNode() == nullptr);
2899 }
2900
2901 int count_512kb = 0;
2902 {
2903 heap_profiler->StartSamplingHeapProfiler(512 * 1024);
2904 CompileRun(script_source);
2905
2906 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2907
2908 const char* names[] = {"", "foo", "bar"};
2909 auto node_bar = FindAllocationProfileNode(
2910 profile, Vector<const char*>(names, arraysize(names)));
2911 CHECK(node_bar);
2912
2913 // Count the number of allocations we sampled from bar.
2914 for (auto allocation : node_bar->allocations) {
2915 count_512kb += allocation.count;
2916 }
2917
2918 heap_profiler->StopSamplingHeapProfiler();
2919 }
2920
2921 // Samples should get cleared once sampling is stopped.
2922 {
2923 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2924 CHECK(profile.GetRootNode() == nullptr);
2925 }
2926
2927 // Sampling at a higher rate should give us more sampled objects.
2928 {
2929 heap_profiler->StartSamplingHeapProfiler(256 * 1024);
2930 CompileRun(script_source);
2931
2932 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2933
2934 const char* names[] = {"", "foo", "bar"};
2935 auto node_bar = FindAllocationProfileNode(
2936 profile, Vector<const char*>(names, arraysize(names)));
2937 CHECK(node_bar);
2938
2939 // Count the number of allocations we sampled from bar.
2940 int count_256kb = 0;
2941 for (auto allocation : node_bar->allocations) {
2942 count_256kb += allocation.count;
2943 }
2944
2945 // We should have roughly twice as many sampled allocations. However, since
2946 // sampling is a randomized process, we use a weaker test.
2947 CHECK_GT(count_256kb, count_512kb);
2948
2949 heap_profiler->StopSamplingHeapProfiler();
2950 }
2951
2952 // A more complicated test cases with deeper call graph and dynamically
2953 // generated function names.
2954 {
2955 heap_profiler->StartSamplingHeapProfiler(512);
2956 CompileRun(record_trace_tree_source);
2957
2958 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2959
2960 const char* names1[] = {"", "start", "f_0_0", "f_0_1", "f_0_2"};
2961 auto node1 = FindAllocationProfileNode(
2962 profile, Vector<const char*>(names1, arraysize(names1)));
2963 CHECK(node1);
2964
2965 const char* names2[] = {"", "generateFunctions"};
2966 auto node2 = FindAllocationProfileNode(
2967 profile, Vector<const char*>(names2, arraysize(names2)));
2968 CHECK(node2);
2969
2970 heap_profiler->StopSamplingHeapProfiler();
2971 }
2972 }
2973
2974
2975 TEST(SamplingHeapProfilerApiAllocation) {
2976 v8::HandleScope scope(v8::Isolate::GetCurrent());
2977 LocalContext env;
2978 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2979
2980 heap_profiler->StartSamplingHeapProfiler(2 * 1024); // 2 KiB.
2981
2982 for (int i = 0; i < 8 * 1024; ++i) v8::Object::New(env->GetIsolate());
2983
2984 v8::AllocationProfile profile = heap_profiler->GetAllocationProfile();
2985 const char* names[] = {"(V8 API)"};
2986 auto node = FindAllocationProfileNode(
2987 profile, Vector<const char*>(names, arraysize(names)));
2988 CHECK(node);
2989
2990 heap_profiler->StopSamplingHeapProfiler();
2991 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698