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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 v8::String::NewFromUtf8(env->GetIsolate(), "test"); | 394 v8::String::NewFromUtf8(env->GetIsolate(), "test"); |
395 cpu_profiler->StartProfiling(profile_name); | 395 cpu_profiler->StartProfiling(profile_name); |
396 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); | 396 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
397 CHECK(profile->GetStartTime() <= profile->GetEndTime()); | 397 CHECK(profile->GetStartTime() <= profile->GetEndTime()); |
398 } | 398 } |
399 | 399 |
400 | 400 |
401 static v8::CpuProfile* RunProfiler( | 401 static v8::CpuProfile* RunProfiler( |
402 v8::Handle<v8::Context> env, v8::Handle<v8::Function> function, | 402 v8::Handle<v8::Context> env, v8::Handle<v8::Function> function, |
403 v8::Handle<v8::Value> argv[], int argc, | 403 v8::Handle<v8::Value> argv[], int argc, |
404 unsigned min_js_samples) { | 404 unsigned min_js_samples, bool collect_samples = false) { |
405 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); | 405 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
406 v8::Local<v8::String> profile_name = | 406 v8::Local<v8::String> profile_name = |
407 v8::String::NewFromUtf8(env->GetIsolate(), "my_profile"); | 407 v8::String::NewFromUtf8(env->GetIsolate(), "my_profile"); |
408 | 408 |
409 cpu_profiler->StartProfiling(profile_name); | 409 cpu_profiler->StartProfiling(profile_name, collect_samples); |
410 | 410 |
411 i::Sampler* sampler = | 411 i::Sampler* sampler = |
412 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler(); | 412 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler(); |
413 sampler->StartCountingSamples(); | 413 sampler->StartCountingSamples(); |
414 do { | 414 do { |
415 function->Call(env->Global(), argc, argv); | 415 function->Call(env->Global(), argc, argv); |
416 } while (sampler->js_and_external_sample_count() < min_js_samples); | 416 } while (sampler->js_and_external_sample_count() < min_js_samples); |
417 | 417 |
418 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); | 418 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name); |
419 | 419 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 CheckSimpleBranch(env->GetIsolate(), fooNode, bazBranch, | 580 CheckSimpleBranch(env->GetIsolate(), fooNode, bazBranch, |
581 ARRAY_SIZE(bazBranch)); | 581 ARRAY_SIZE(bazBranch)); |
582 const char* delayBranch[] = { "delay", "loop" }; | 582 const char* delayBranch[] = { "delay", "loop" }; |
583 CheckSimpleBranch(env->GetIsolate(), fooNode, delayBranch, | 583 CheckSimpleBranch(env->GetIsolate(), fooNode, delayBranch, |
584 ARRAY_SIZE(delayBranch)); | 584 ARRAY_SIZE(delayBranch)); |
585 | 585 |
586 profile->Delete(); | 586 profile->Delete(); |
587 } | 587 } |
588 | 588 |
589 | 589 |
| 590 TEST(CollectCpuProfileSamples) { |
| 591 LocalContext env; |
| 592 v8::HandleScope scope(env->GetIsolate()); |
| 593 |
| 594 v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
| 595 cpu_profiler_test_source))->Run(); |
| 596 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
| 597 env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "start"))); |
| 598 |
| 599 int32_t profiling_interval_ms = 200; |
| 600 v8::Handle<v8::Value> args[] = { |
| 601 v8::Integer::New(env->GetIsolate(), profiling_interval_ms) |
| 602 }; |
| 603 v8::CpuProfile* profile = |
| 604 RunProfiler(env.local(), function, args, ARRAY_SIZE(args), 200, true); |
| 605 |
| 606 CHECK_LE(200, profile->GetSamplesCount()); |
| 607 uint64_t end_time = profile->GetEndTime(); |
| 608 uint64_t current_time = profile->GetStartTime(); |
| 609 CHECK_LE(current_time, end_time); |
| 610 for (int i = 0; i < profile->GetSamplesCount(); i++) { |
| 611 CHECK_NE(NULL, profile->GetSample(i)); |
| 612 uint64_t timestamp = profile->GetSampleTimestamp(i); |
| 613 CHECK_LE(current_time, timestamp); |
| 614 CHECK_LE(timestamp, end_time); |
| 615 current_time = timestamp; |
| 616 } |
| 617 |
| 618 profile->Delete(); |
| 619 } |
| 620 |
590 | 621 |
591 static const char* cpu_profiler_test_source2 = "function loop() {}\n" | 622 static const char* cpu_profiler_test_source2 = "function loop() {}\n" |
592 "function delay() { loop(); }\n" | 623 "function delay() { loop(); }\n" |
593 "function start(count) {\n" | 624 "function start(count) {\n" |
594 " var k = 0;\n" | 625 " var k = 0;\n" |
595 " do {\n" | 626 " do {\n" |
596 " delay();\n" | 627 " delay();\n" |
597 " } while (++k < count*100*1000);\n" | 628 " } while (++k < count*100*1000);\n" |
598 "}\n"; | 629 "}\n"; |
599 | 630 |
(...skipping 956 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1556 inner_profile = NULL; | 1587 inner_profile = NULL; |
1557 CHECK_EQ(0, iprofiler->GetProfilesCount()); | 1588 CHECK_EQ(0, iprofiler->GetProfilesCount()); |
1558 | 1589 |
1559 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer); | 1590 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer); |
1560 CHECK(outer_profile); | 1591 CHECK(outer_profile); |
1561 CHECK_EQ(1, iprofiler->GetProfilesCount()); | 1592 CHECK_EQ(1, iprofiler->GetProfilesCount()); |
1562 outer_profile->Delete(); | 1593 outer_profile->Delete(); |
1563 outer_profile = NULL; | 1594 outer_profile = NULL; |
1564 CHECK_EQ(0, iprofiler->GetProfilesCount()); | 1595 CHECK_EQ(0, iprofiler->GetProfilesCount()); |
1565 } | 1596 } |
OLD | NEW |