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