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 #include <math.h> | 5 #include <math.h> |
6 #include <cstdlib> | 6 #include <cstdlib> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/location.h" | |
13 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
16 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
19 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
20 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
21 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
22 #include "base/time/time.h" | 20 #include "base/time/time.h" |
23 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
24 #include "base/trace_event/trace_event_synthetic_delay.h" | 22 #include "base/trace_event/trace_event_synthetic_delay.h" |
25 #include "base/values.h" | 23 #include "base/values.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 EndTraceAndFlushAsync(&flush_complete_event); | 88 EndTraceAndFlushAsync(&flush_complete_event); |
91 flush_complete_event.Wait(); | 89 flush_complete_event.Wait(); |
92 } | 90 } |
93 | 91 |
94 // Used when testing thread-local buffers which requires the thread initiating | 92 // Used when testing thread-local buffers which requires the thread initiating |
95 // flush to have a message loop. | 93 // flush to have a message loop. |
96 void EndTraceAndFlushInThreadWithMessageLoop() { | 94 void EndTraceAndFlushInThreadWithMessageLoop() { |
97 WaitableEvent flush_complete_event(false, false); | 95 WaitableEvent flush_complete_event(false, false); |
98 Thread flush_thread("flush"); | 96 Thread flush_thread("flush"); |
99 flush_thread.Start(); | 97 flush_thread.Start(); |
100 flush_thread.task_runner()->PostTask( | 98 flush_thread.message_loop()->PostTask(FROM_HERE, |
101 FROM_HERE, base::Bind(&TraceEventTestFixture::EndTraceAndFlushAsync, | 99 base::Bind(&TraceEventTestFixture::EndTraceAndFlushAsync, |
102 base::Unretained(this), &flush_complete_event)); | 100 base::Unretained(this), |
| 101 &flush_complete_event)); |
103 flush_complete_event.Wait(); | 102 flush_complete_event.Wait(); |
104 } | 103 } |
105 | 104 |
106 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { | 105 void EndTraceAndFlushAsync(WaitableEvent* flush_complete_event) { |
107 TraceLog::GetInstance()->SetDisabled(); | 106 TraceLog::GetInstance()->SetDisabled(); |
108 TraceLog::GetInstance()->Flush( | 107 TraceLog::GetInstance()->Flush( |
109 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 108 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
110 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | 109 base::Unretained(static_cast<TraceEventTestFixture*>(this)), |
111 base::Unretained(flush_complete_event))); | 110 base::Unretained(flush_complete_event))); |
112 } | 111 } |
(...skipping 1305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 } | 1417 } |
1419 | 1418 |
1420 // Test that data sent from other threads is gathered | 1419 // Test that data sent from other threads is gathered |
1421 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { | 1420 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { |
1422 BeginTrace(); | 1421 BeginTrace(); |
1423 | 1422 |
1424 Thread thread("1"); | 1423 Thread thread("1"); |
1425 WaitableEvent task_complete_event(false, false); | 1424 WaitableEvent task_complete_event(false, false); |
1426 thread.Start(); | 1425 thread.Start(); |
1427 | 1426 |
1428 thread.task_runner()->PostTask( | 1427 thread.message_loop()->PostTask( |
1429 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 1428 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event)); |
1430 task_complete_event.Wait(); | 1429 task_complete_event.Wait(); |
1431 thread.Stop(); | 1430 thread.Stop(); |
1432 | 1431 |
1433 EndTraceAndFlush(); | 1432 EndTraceAndFlush(); |
1434 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 1433 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
1435 } | 1434 } |
1436 | 1435 |
1437 // Test that data sent from multiple threads is gathered | 1436 // Test that data sent from multiple threads is gathered |
1438 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { | 1437 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { |
1439 BeginTrace(); | 1438 BeginTrace(); |
1440 | 1439 |
1441 const int num_threads = 4; | 1440 const int num_threads = 4; |
1442 const int num_events = 4000; | 1441 const int num_events = 4000; |
1443 Thread* threads[num_threads]; | 1442 Thread* threads[num_threads]; |
1444 WaitableEvent* task_complete_events[num_threads]; | 1443 WaitableEvent* task_complete_events[num_threads]; |
1445 for (int i = 0; i < num_threads; i++) { | 1444 for (int i = 0; i < num_threads; i++) { |
1446 threads[i] = new Thread(StringPrintf("Thread %d", i)); | 1445 threads[i] = new Thread(StringPrintf("Thread %d", i)); |
1447 task_complete_events[i] = new WaitableEvent(false, false); | 1446 task_complete_events[i] = new WaitableEvent(false, false); |
1448 threads[i]->Start(); | 1447 threads[i]->Start(); |
1449 threads[i]->task_runner()->PostTask( | 1448 threads[i]->message_loop()->PostTask( |
1450 FROM_HERE, base::Bind(&TraceManyInstantEvents, i, num_events, | 1449 FROM_HERE, base::Bind(&TraceManyInstantEvents, |
1451 task_complete_events[i])); | 1450 i, num_events, task_complete_events[i])); |
1452 } | 1451 } |
1453 | 1452 |
1454 for (int i = 0; i < num_threads; i++) { | 1453 for (int i = 0; i < num_threads; i++) { |
1455 task_complete_events[i]->Wait(); | 1454 task_complete_events[i]->Wait(); |
1456 } | 1455 } |
1457 | 1456 |
1458 // Let half of the threads end before flush. | 1457 // Let half of the threads end before flush. |
1459 for (int i = 0; i < num_threads / 2; i++) { | 1458 for (int i = 0; i < num_threads / 2; i++) { |
1460 threads[i]->Stop(); | 1459 threads[i]->Stop(); |
1461 delete threads[i]; | 1460 delete threads[i]; |
(...skipping 25 matching lines...) Expand all Loading... |
1487 | 1486 |
1488 // Enable tracing. | 1487 // Enable tracing. |
1489 BeginTrace(); | 1488 BeginTrace(); |
1490 | 1489 |
1491 // Now run some trace code on these threads. | 1490 // Now run some trace code on these threads. |
1492 WaitableEvent* task_complete_events[kNumThreads]; | 1491 WaitableEvent* task_complete_events[kNumThreads]; |
1493 for (int i = 0; i < kNumThreads; i++) { | 1492 for (int i = 0; i < kNumThreads; i++) { |
1494 task_complete_events[i] = new WaitableEvent(false, false); | 1493 task_complete_events[i] = new WaitableEvent(false, false); |
1495 threads[i]->Start(); | 1494 threads[i]->Start(); |
1496 thread_ids[i] = threads[i]->thread_id(); | 1495 thread_ids[i] = threads[i]->thread_id(); |
1497 threads[i]->task_runner()->PostTask( | 1496 threads[i]->message_loop()->PostTask( |
1498 FROM_HERE, base::Bind(&TraceManyInstantEvents, i, kNumEvents, | 1497 FROM_HERE, base::Bind(&TraceManyInstantEvents, |
1499 task_complete_events[i])); | 1498 i, kNumEvents, task_complete_events[i])); |
1500 } | 1499 } |
1501 for (int i = 0; i < kNumThreads; i++) { | 1500 for (int i = 0; i < kNumThreads; i++) { |
1502 task_complete_events[i]->Wait(); | 1501 task_complete_events[i]->Wait(); |
1503 } | 1502 } |
1504 | 1503 |
1505 // Shut things down. | 1504 // Shut things down. |
1506 for (int i = 0; i < kNumThreads; i++) { | 1505 for (int i = 0; i < kNumThreads; i++) { |
1507 threads[i]->Stop(); | 1506 threads[i]->Stop(); |
1508 delete threads[i]; | 1507 delete threads[i]; |
1509 delete task_complete_events[i]; | 1508 delete task_complete_events[i]; |
(...skipping 1209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2719 task_start_event->Signal(); | 2718 task_start_event->Signal(); |
2720 task_stop_event->Wait(); | 2719 task_stop_event->Wait(); |
2721 } | 2720 } |
2722 | 2721 |
2723 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopBeforeTracing) { | 2722 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopBeforeTracing) { |
2724 BeginTrace(); | 2723 BeginTrace(); |
2725 | 2724 |
2726 Thread thread("1"); | 2725 Thread thread("1"); |
2727 WaitableEvent task_complete_event(false, false); | 2726 WaitableEvent task_complete_event(false, false); |
2728 thread.Start(); | 2727 thread.Start(); |
2729 thread.task_runner()->PostTask( | 2728 thread.message_loop()->PostTask( |
2730 FROM_HERE, Bind(&TraceLog::SetCurrentThreadBlocksMessageLoop, | 2729 FROM_HERE, Bind(&TraceLog::SetCurrentThreadBlocksMessageLoop, |
2731 Unretained(TraceLog::GetInstance()))); | 2730 Unretained(TraceLog::GetInstance()))); |
2732 | 2731 |
2733 thread.task_runner()->PostTask( | 2732 thread.message_loop()->PostTask( |
2734 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2733 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); |
2735 task_complete_event.Wait(); | 2734 task_complete_event.Wait(); |
2736 | 2735 |
2737 WaitableEvent task_start_event(false, false); | 2736 WaitableEvent task_start_event(false, false); |
2738 WaitableEvent task_stop_event(false, false); | 2737 WaitableEvent task_stop_event(false, false); |
2739 thread.task_runner()->PostTask( | 2738 thread.message_loop()->PostTask( |
2740 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2739 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2741 task_start_event.Wait(); | 2740 task_start_event.Wait(); |
2742 | 2741 |
2743 EndTraceAndFlush(); | 2742 EndTraceAndFlush(); |
2744 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2743 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2745 | 2744 |
2746 task_stop_event.Signal(); | 2745 task_stop_event.Signal(); |
2747 thread.Stop(); | 2746 thread.Stop(); |
2748 } | 2747 } |
2749 | 2748 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2790 BlockUntilStopped(task_start_event, task_stop_event); | 2789 BlockUntilStopped(task_start_event, task_stop_event); |
2791 } | 2790 } |
2792 | 2791 |
2793 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopAfterTracing) { | 2792 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopAfterTracing) { |
2794 BeginTrace(); | 2793 BeginTrace(); |
2795 | 2794 |
2796 Thread thread("1"); | 2795 Thread thread("1"); |
2797 WaitableEvent task_complete_event(false, false); | 2796 WaitableEvent task_complete_event(false, false); |
2798 thread.Start(); | 2797 thread.Start(); |
2799 | 2798 |
2800 thread.task_runner()->PostTask( | 2799 thread.message_loop()->PostTask( |
2801 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2800 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); |
2802 task_complete_event.Wait(); | 2801 task_complete_event.Wait(); |
2803 | 2802 |
2804 WaitableEvent task_start_event(false, false); | 2803 WaitableEvent task_start_event(false, false); |
2805 WaitableEvent task_stop_event(false, false); | 2804 WaitableEvent task_stop_event(false, false); |
2806 thread.task_runner()->PostTask( | 2805 thread.message_loop()->PostTask( |
2807 FROM_HERE, Bind(&SetBlockingFlagAndBlockUntilStopped, &task_start_event, | 2806 FROM_HERE, Bind(&SetBlockingFlagAndBlockUntilStopped, |
2808 &task_stop_event)); | 2807 &task_start_event, &task_stop_event)); |
2809 task_start_event.Wait(); | 2808 task_start_event.Wait(); |
2810 | 2809 |
2811 EndTraceAndFlush(); | 2810 EndTraceAndFlush(); |
2812 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2811 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2813 | 2812 |
2814 task_stop_event.Signal(); | 2813 task_stop_event.Signal(); |
2815 thread.Stop(); | 2814 thread.Stop(); |
2816 } | 2815 } |
2817 | 2816 |
2818 TEST_F(TraceEventTestFixture, ThreadOnceBlocking) { | 2817 TEST_F(TraceEventTestFixture, ThreadOnceBlocking) { |
2819 BeginTrace(); | 2818 BeginTrace(); |
2820 | 2819 |
2821 Thread thread("1"); | 2820 Thread thread("1"); |
2822 WaitableEvent task_complete_event(false, false); | 2821 WaitableEvent task_complete_event(false, false); |
2823 thread.Start(); | 2822 thread.Start(); |
2824 | 2823 |
2825 thread.task_runner()->PostTask( | 2824 thread.message_loop()->PostTask( |
2826 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2825 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); |
2827 task_complete_event.Wait(); | 2826 task_complete_event.Wait(); |
2828 task_complete_event.Reset(); | 2827 task_complete_event.Reset(); |
2829 | 2828 |
2830 WaitableEvent task_start_event(false, false); | 2829 WaitableEvent task_start_event(false, false); |
2831 WaitableEvent task_stop_event(false, false); | 2830 WaitableEvent task_stop_event(false, false); |
2832 thread.task_runner()->PostTask( | 2831 thread.message_loop()->PostTask( |
2833 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2832 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2834 task_start_event.Wait(); | 2833 task_start_event.Wait(); |
2835 | 2834 |
2836 // The thread will timeout in this flush. | 2835 // The thread will timeout in this flush. |
2837 EndTraceAndFlushInThreadWithMessageLoop(); | 2836 EndTraceAndFlushInThreadWithMessageLoop(); |
2838 Clear(); | 2837 Clear(); |
2839 | 2838 |
2840 // Let the thread's message loop continue to spin. | 2839 // Let the thread's message loop continue to spin. |
2841 task_stop_event.Signal(); | 2840 task_stop_event.Signal(); |
2842 | 2841 |
2843 // The following sequence ensures that the FlushCurrentThread task has been | 2842 // The following sequence ensures that the FlushCurrentThread task has been |
2844 // executed in the thread before continuing. | 2843 // executed in the thread before continuing. |
2845 task_start_event.Reset(); | 2844 task_start_event.Reset(); |
2846 task_stop_event.Reset(); | 2845 task_stop_event.Reset(); |
2847 thread.task_runner()->PostTask( | 2846 thread.message_loop()->PostTask( |
2848 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2847 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2849 task_start_event.Wait(); | 2848 task_start_event.Wait(); |
2850 task_stop_event.Signal(); | 2849 task_stop_event.Signal(); |
2851 Clear(); | 2850 Clear(); |
2852 | 2851 |
2853 // TraceLog should discover the generation mismatch and recover the thread | 2852 // TraceLog should discover the generation mismatch and recover the thread |
2854 // local buffer for the thread without any error. | 2853 // local buffer for the thread without any error. |
2855 BeginTrace(); | 2854 BeginTrace(); |
2856 thread.task_runner()->PostTask( | 2855 thread.message_loop()->PostTask( |
2857 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2856 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); |
2858 task_complete_event.Wait(); | 2857 task_complete_event.Wait(); |
2859 task_complete_event.Reset(); | 2858 task_complete_event.Reset(); |
2860 EndTraceAndFlushInThreadWithMessageLoop(); | 2859 EndTraceAndFlushInThreadWithMessageLoop(); |
2861 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2860 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2862 } | 2861 } |
2863 | 2862 |
2864 std::string* g_log_buffer = NULL; | 2863 std::string* g_log_buffer = NULL; |
2865 bool MockLogMessageHandler(int, const char*, int, size_t, | 2864 bool MockLogMessageHandler(int, const char*, int, size_t, |
2866 const std::string& str) { | 2865 const std::string& str) { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3075 EXPECT_EQ(original_option.record_mode, new_options.record_mode); | 3074 EXPECT_EQ(original_option.record_mode, new_options.record_mode); |
3076 EXPECT_EQ(original_option.enable_sampling, new_options.enable_sampling); | 3075 EXPECT_EQ(original_option.enable_sampling, new_options.enable_sampling); |
3077 EXPECT_EQ(original_option.enable_systrace, new_options.enable_systrace); | 3076 EXPECT_EQ(original_option.enable_systrace, new_options.enable_systrace); |
3078 } | 3077 } |
3079 } | 3078 } |
3080 } | 3079 } |
3081 } | 3080 } |
3082 | 3081 |
3083 } // namespace trace_event | 3082 } // namespace trace_event |
3084 } // namespace base | 3083 } // namespace base |
OLD | NEW |