OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 20 matching lines...) Expand all Loading... |
31 | 31 |
32 #include "include/v8-profiler.h" | 32 #include "include/v8-profiler.h" |
33 #include "src/base/platform/platform.h" | 33 #include "src/base/platform/platform.h" |
34 #include "src/deoptimizer.h" | 34 #include "src/deoptimizer.h" |
35 #include "src/profiler/cpu-profiler-inl.h" | 35 #include "src/profiler/cpu-profiler-inl.h" |
36 #include "src/profiler/profiler-listener.h" | 36 #include "src/profiler/profiler-listener.h" |
37 #include "src/utils.h" | 37 #include "src/utils.h" |
38 #include "test/cctest/cctest.h" | 38 #include "test/cctest/cctest.h" |
39 #include "test/cctest/profiler-extension.h" | 39 #include "test/cctest/profiler-extension.h" |
40 | 40 |
| 41 #include "include/libplatform/v8-tracing.h" |
| 42 #include "src/tracing/trace-event.h" |
| 43 |
41 using i::CodeEntry; | 44 using i::CodeEntry; |
42 using i::CpuProfile; | 45 using i::CpuProfile; |
43 using i::CpuProfiler; | 46 using i::CpuProfiler; |
44 using i::CpuProfilesCollection; | 47 using i::CpuProfilesCollection; |
45 using i::Heap; | 48 using i::Heap; |
46 using i::ProfileGenerator; | 49 using i::ProfileGenerator; |
47 using i::ProfileNode; | 50 using i::ProfileNode; |
48 using i::ProfilerEventsProcessor; | 51 using i::ProfilerEventsProcessor; |
49 using i::ProfilerListener; | 52 using i::ProfilerListener; |
50 using i::ScopedVector; | 53 using i::ScopedVector; |
(...skipping 2040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2091 iprofile->Print(); | 2094 iprofile->Print(); |
2092 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); | 2095 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile); |
2093 | 2096 |
2094 const char* branch[] = {"", "test"}; | 2097 const char* branch[] = {"", "test"}; |
2095 const ProfileNode* itest_node = | 2098 const ProfileNode* itest_node = |
2096 GetSimpleBranch(env, profile, branch, arraysize(branch)); | 2099 GetSimpleBranch(env, profile, branch, arraysize(branch)); |
2097 CHECK_EQ(0U, itest_node->deopt_infos().size()); | 2100 CHECK_EQ(0U, itest_node->deopt_infos().size()); |
2098 | 2101 |
2099 iprofiler->DeleteProfile(iprofile); | 2102 iprofiler->DeleteProfile(iprofile); |
2100 } | 2103 } |
| 2104 |
| 2105 using v8::platform::tracing::TraceBuffer; |
| 2106 using v8::platform::tracing::TraceConfig; |
| 2107 using v8::platform::tracing::TraceObject; |
| 2108 |
| 2109 namespace { |
| 2110 |
| 2111 class CpuProfileEventChecker : public v8::platform::tracing::TraceWriter { |
| 2112 public: |
| 2113 void AppendTraceEvent(TraceObject* trace_event) override { |
| 2114 if (trace_event->name() != std::string("CpuProfile") && |
| 2115 trace_event->name() != std::string("CpuProfileChunk")) |
| 2116 return; |
| 2117 CHECK(!profile_id_ || trace_event->id() == profile_id_); |
| 2118 CHECK_EQ(1, trace_event->num_args()); |
| 2119 CHECK_EQ(TRACE_VALUE_TYPE_CONVERTABLE, trace_event->arg_types()[0]); |
| 2120 profile_id_ = trace_event->id(); |
| 2121 v8::ConvertableToTraceFormat* arg = |
| 2122 trace_event->arg_convertables()[0].get(); |
| 2123 arg->AppendAsTraceFormat(&result_json_); |
| 2124 } |
| 2125 void Flush() override {} |
| 2126 |
| 2127 std::string result_json() const { return result_json_; } |
| 2128 |
| 2129 private: |
| 2130 std::string result_json_; |
| 2131 uint64_t profile_id_ = 0; |
| 2132 }; |
| 2133 |
| 2134 } // namespace |
| 2135 |
| 2136 TEST(TracingCpuProfiler) { |
| 2137 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| 2138 v8::Platform* default_platform = v8::platform::CreateDefaultPlatform(); |
| 2139 i::V8::SetPlatformForTesting(default_platform); |
| 2140 |
| 2141 v8::platform::tracing::TracingController tracing_controller; |
| 2142 v8::platform::SetTracingController(default_platform, &tracing_controller); |
| 2143 |
| 2144 CpuProfileEventChecker* event_checker = new CpuProfileEventChecker(); |
| 2145 TraceBuffer* ring_buffer = |
| 2146 TraceBuffer::CreateTraceBufferRingBuffer(1, event_checker); |
| 2147 tracing_controller.Initialize(ring_buffer); |
| 2148 TraceConfig* trace_config = new TraceConfig(); |
| 2149 trace_config->AddIncludedCategory( |
| 2150 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler")); |
| 2151 |
| 2152 LocalContext env; |
| 2153 v8::HandleScope scope(env->GetIsolate()); |
| 2154 { |
| 2155 tracing_controller.StartTracing(trace_config); |
| 2156 auto profiler = v8::TracingCpuProfiler::Create(env->GetIsolate()); |
| 2157 CompileRun("function foo() { } foo();"); |
| 2158 tracing_controller.StopTracing(); |
| 2159 CompileRun("function bar() { } bar();"); |
| 2160 } |
| 2161 |
| 2162 const char* profile_checker = |
| 2163 "function checkProfile(profile) {\n" |
| 2164 " if (typeof profile['startTime'] !== 'number') return 'startTime';\n" |
| 2165 " return '';\n" |
| 2166 "}\n" |
| 2167 "checkProfile("; |
| 2168 std::string profile_json = event_checker->result_json(); |
| 2169 CHECK_LT(0u, profile_json.length()); |
| 2170 printf("Profile JSON: %s\n", profile_json.c_str()); |
| 2171 std::string code = profile_checker + profile_json + ")"; |
| 2172 v8::Local<v8::Value> result = |
| 2173 CompileRunChecked(CcTest::isolate(), code.c_str()); |
| 2174 v8::String::Utf8Value value(result); |
| 2175 printf("Check result: %*s\n", value.length(), *value); |
| 2176 CHECK_EQ(0, value.length()); |
| 2177 |
| 2178 i::V8::SetPlatformForTesting(old_platform); |
| 2179 } |
OLD | NEW |