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 "base/trace_event/trace_event.h" | 5 #include "base/trace_event/trace_event.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 // Used when testing thread-local buffers which requires the thread initiating | 118 // Used when testing thread-local buffers which requires the thread initiating |
119 // flush to have a message loop. | 119 // flush to have a message loop. |
120 void EndTraceAndFlushInThreadWithMessageLoop() { | 120 void EndTraceAndFlushInThreadWithMessageLoop() { |
121 WaitableEvent flush_complete_event( | 121 WaitableEvent flush_complete_event( |
122 WaitableEvent::ResetPolicy::AUTOMATIC, | 122 WaitableEvent::ResetPolicy::AUTOMATIC, |
123 WaitableEvent::InitialState::NOT_SIGNALED); | 123 WaitableEvent::InitialState::NOT_SIGNALED); |
124 Thread flush_thread("flush"); | 124 Thread flush_thread("flush"); |
125 flush_thread.Start(); | 125 flush_thread.Start(); |
126 flush_thread.task_runner()->PostTask( | 126 flush_thread.task_runner()->PostTask( |
127 FROM_HERE, base::Bind(&TraceEventTestFixture::EndTraceAndFlushAsync, | 127 FROM_HERE, |
128 base::Unretained(this), &flush_complete_event)); | 128 base::BindOnce(&TraceEventTestFixture::EndTraceAndFlushAsync, |
| 129 base::Unretained(this), &flush_complete_event)); |
129 flush_complete_event.Wait(); | 130 flush_complete_event.Wait(); |
130 } | 131 } |
131 | 132 |
132 void CancelTraceAsync(WaitableEvent* flush_complete_event) { | 133 void CancelTraceAsync(WaitableEvent* flush_complete_event) { |
133 TraceLog::GetInstance()->CancelTracing( | 134 TraceLog::GetInstance()->CancelTracing( |
134 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, | 135 base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
135 base::Unretained(static_cast<TraceEventTestFixture*>(this)), | 136 base::Unretained(static_cast<TraceEventTestFixture*>(this)), |
136 base::Unretained(flush_complete_event))); | 137 base::Unretained(flush_complete_event))); |
137 } | 138 } |
138 | 139 |
(...skipping 1591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1730 // Test that data sent from other threads is gathered | 1731 // Test that data sent from other threads is gathered |
1731 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { | 1732 TEST_F(TraceEventTestFixture, DataCapturedOnThread) { |
1732 BeginTrace(); | 1733 BeginTrace(); |
1733 | 1734 |
1734 Thread thread("1"); | 1735 Thread thread("1"); |
1735 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 1736 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
1736 WaitableEvent::InitialState::NOT_SIGNALED); | 1737 WaitableEvent::InitialState::NOT_SIGNALED); |
1737 thread.Start(); | 1738 thread.Start(); |
1738 | 1739 |
1739 thread.task_runner()->PostTask( | 1740 thread.task_runner()->PostTask( |
1740 FROM_HERE, base::Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 1741 FROM_HERE, |
| 1742 base::BindOnce(&TraceWithAllMacroVariants, &task_complete_event)); |
1741 task_complete_event.Wait(); | 1743 task_complete_event.Wait(); |
1742 thread.Stop(); | 1744 thread.Stop(); |
1743 | 1745 |
1744 EndTraceAndFlush(); | 1746 EndTraceAndFlush(); |
1745 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 1747 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
1746 } | 1748 } |
1747 | 1749 |
1748 // Test that data sent from multiple threads is gathered | 1750 // Test that data sent from multiple threads is gathered |
1749 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { | 1751 TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { |
1750 BeginTrace(); | 1752 BeginTrace(); |
1751 | 1753 |
1752 const int num_threads = 4; | 1754 const int num_threads = 4; |
1753 const int num_events = 4000; | 1755 const int num_events = 4000; |
1754 Thread* threads[num_threads]; | 1756 Thread* threads[num_threads]; |
1755 WaitableEvent* task_complete_events[num_threads]; | 1757 WaitableEvent* task_complete_events[num_threads]; |
1756 for (int i = 0; i < num_threads; i++) { | 1758 for (int i = 0; i < num_threads; i++) { |
1757 threads[i] = new Thread(StringPrintf("Thread %d", i)); | 1759 threads[i] = new Thread(StringPrintf("Thread %d", i)); |
1758 task_complete_events[i] = | 1760 task_complete_events[i] = |
1759 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, | 1761 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
1760 WaitableEvent::InitialState::NOT_SIGNALED); | 1762 WaitableEvent::InitialState::NOT_SIGNALED); |
1761 threads[i]->Start(); | 1763 threads[i]->Start(); |
1762 threads[i]->task_runner()->PostTask( | 1764 threads[i]->task_runner()->PostTask( |
1763 FROM_HERE, base::Bind(&TraceManyInstantEvents, i, num_events, | 1765 FROM_HERE, base::BindOnce(&TraceManyInstantEvents, i, num_events, |
1764 task_complete_events[i])); | 1766 task_complete_events[i])); |
1765 } | 1767 } |
1766 | 1768 |
1767 for (int i = 0; i < num_threads; i++) { | 1769 for (int i = 0; i < num_threads; i++) { |
1768 task_complete_events[i]->Wait(); | 1770 task_complete_events[i]->Wait(); |
1769 } | 1771 } |
1770 | 1772 |
1771 // Let half of the threads end before flush. | 1773 // Let half of the threads end before flush. |
1772 for (int i = 0; i < num_threads / 2; i++) { | 1774 for (int i = 0; i < num_threads / 2; i++) { |
1773 threads[i]->Stop(); | 1775 threads[i]->Stop(); |
1774 delete threads[i]; | 1776 delete threads[i]; |
(...skipping 28 matching lines...) Expand all Loading... |
1803 | 1805 |
1804 // Now run some trace code on these threads. | 1806 // Now run some trace code on these threads. |
1805 WaitableEvent* task_complete_events[kNumThreads]; | 1807 WaitableEvent* task_complete_events[kNumThreads]; |
1806 for (int i = 0; i < kNumThreads; i++) { | 1808 for (int i = 0; i < kNumThreads; i++) { |
1807 task_complete_events[i] = | 1809 task_complete_events[i] = |
1808 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, | 1810 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
1809 WaitableEvent::InitialState::NOT_SIGNALED); | 1811 WaitableEvent::InitialState::NOT_SIGNALED); |
1810 threads[i]->Start(); | 1812 threads[i]->Start(); |
1811 thread_ids[i] = threads[i]->GetThreadId(); | 1813 thread_ids[i] = threads[i]->GetThreadId(); |
1812 threads[i]->task_runner()->PostTask( | 1814 threads[i]->task_runner()->PostTask( |
1813 FROM_HERE, base::Bind(&TraceManyInstantEvents, i, kNumEvents, | 1815 FROM_HERE, base::BindOnce(&TraceManyInstantEvents, i, kNumEvents, |
1814 task_complete_events[i])); | 1816 task_complete_events[i])); |
1815 } | 1817 } |
1816 for (int i = 0; i < kNumThreads; i++) { | 1818 for (int i = 0; i < kNumThreads; i++) { |
1817 task_complete_events[i]->Wait(); | 1819 task_complete_events[i]->Wait(); |
1818 } | 1820 } |
1819 | 1821 |
1820 // Shut things down. | 1822 // Shut things down. |
1821 for (int i = 0; i < kNumThreads; i++) { | 1823 for (int i = 0; i < kNumThreads; i++) { |
1822 threads[i]->Stop(); | 1824 threads[i]->Stop(); |
1823 delete threads[i]; | 1825 delete threads[i]; |
1824 delete task_complete_events[i]; | 1826 delete task_complete_events[i]; |
(...skipping 888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2713 } | 2715 } |
2714 | 2716 |
2715 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopBeforeTracing) { | 2717 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopBeforeTracing) { |
2716 BeginTrace(); | 2718 BeginTrace(); |
2717 | 2719 |
2718 Thread thread("1"); | 2720 Thread thread("1"); |
2719 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2721 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2720 WaitableEvent::InitialState::NOT_SIGNALED); | 2722 WaitableEvent::InitialState::NOT_SIGNALED); |
2721 thread.Start(); | 2723 thread.Start(); |
2722 thread.task_runner()->PostTask( | 2724 thread.task_runner()->PostTask( |
2723 FROM_HERE, Bind(&TraceLog::SetCurrentThreadBlocksMessageLoop, | 2725 FROM_HERE, BindOnce(&TraceLog::SetCurrentThreadBlocksMessageLoop, |
2724 Unretained(TraceLog::GetInstance()))); | 2726 Unretained(TraceLog::GetInstance()))); |
2725 | 2727 |
2726 thread.task_runner()->PostTask( | 2728 thread.task_runner()->PostTask( |
2727 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2729 FROM_HERE, BindOnce(&TraceWithAllMacroVariants, &task_complete_event)); |
2728 task_complete_event.Wait(); | 2730 task_complete_event.Wait(); |
2729 | 2731 |
2730 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2732 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2731 WaitableEvent::InitialState::NOT_SIGNALED); | 2733 WaitableEvent::InitialState::NOT_SIGNALED); |
2732 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2734 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2733 WaitableEvent::InitialState::NOT_SIGNALED); | 2735 WaitableEvent::InitialState::NOT_SIGNALED); |
2734 thread.task_runner()->PostTask( | 2736 thread.task_runner()->PostTask( |
2735 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2737 FROM_HERE, |
| 2738 BindOnce(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2736 task_start_event.Wait(); | 2739 task_start_event.Wait(); |
2737 | 2740 |
2738 EndTraceAndFlush(); | 2741 EndTraceAndFlush(); |
2739 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2742 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2740 | 2743 |
2741 task_stop_event.Signal(); | 2744 task_stop_event.Signal(); |
2742 thread.Stop(); | 2745 thread.Stop(); |
2743 } | 2746 } |
2744 | 2747 |
2745 TEST_F(TraceEventTestFixture, ConvertTraceConfigToInternalOptions) { | 2748 TEST_F(TraceEventTestFixture, ConvertTraceConfigToInternalOptions) { |
(...skipping 23 matching lines...) Expand all Loading... |
2769 | 2772 |
2770 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopAfterTracing) { | 2773 TEST_F(TraceEventTestFixture, SetCurrentThreadBlocksMessageLoopAfterTracing) { |
2771 BeginTrace(); | 2774 BeginTrace(); |
2772 | 2775 |
2773 Thread thread("1"); | 2776 Thread thread("1"); |
2774 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2777 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2775 WaitableEvent::InitialState::NOT_SIGNALED); | 2778 WaitableEvent::InitialState::NOT_SIGNALED); |
2776 thread.Start(); | 2779 thread.Start(); |
2777 | 2780 |
2778 thread.task_runner()->PostTask( | 2781 thread.task_runner()->PostTask( |
2779 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2782 FROM_HERE, BindOnce(&TraceWithAllMacroVariants, &task_complete_event)); |
2780 task_complete_event.Wait(); | 2783 task_complete_event.Wait(); |
2781 | 2784 |
2782 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2785 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2783 WaitableEvent::InitialState::NOT_SIGNALED); | 2786 WaitableEvent::InitialState::NOT_SIGNALED); |
2784 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2787 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2785 WaitableEvent::InitialState::NOT_SIGNALED); | 2788 WaitableEvent::InitialState::NOT_SIGNALED); |
2786 thread.task_runner()->PostTask( | 2789 thread.task_runner()->PostTask(FROM_HERE, |
2787 FROM_HERE, Bind(&SetBlockingFlagAndBlockUntilStopped, &task_start_event, | 2790 BindOnce(&SetBlockingFlagAndBlockUntilStopped, |
2788 &task_stop_event)); | 2791 &task_start_event, &task_stop_event)); |
2789 task_start_event.Wait(); | 2792 task_start_event.Wait(); |
2790 | 2793 |
2791 EndTraceAndFlush(); | 2794 EndTraceAndFlush(); |
2792 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2795 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2793 | 2796 |
2794 task_stop_event.Signal(); | 2797 task_stop_event.Signal(); |
2795 thread.Stop(); | 2798 thread.Stop(); |
2796 } | 2799 } |
2797 | 2800 |
2798 TEST_F(TraceEventTestFixture, ThreadOnceBlocking) { | 2801 TEST_F(TraceEventTestFixture, ThreadOnceBlocking) { |
2799 BeginTrace(); | 2802 BeginTrace(); |
2800 | 2803 |
2801 Thread thread("1"); | 2804 Thread thread("1"); |
2802 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2805 WaitableEvent task_complete_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2803 WaitableEvent::InitialState::NOT_SIGNALED); | 2806 WaitableEvent::InitialState::NOT_SIGNALED); |
2804 thread.Start(); | 2807 thread.Start(); |
2805 | 2808 |
2806 thread.task_runner()->PostTask( | 2809 thread.task_runner()->PostTask( |
2807 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2810 FROM_HERE, BindOnce(&TraceWithAllMacroVariants, &task_complete_event)); |
2808 task_complete_event.Wait(); | 2811 task_complete_event.Wait(); |
2809 task_complete_event.Reset(); | 2812 task_complete_event.Reset(); |
2810 | 2813 |
2811 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2814 WaitableEvent task_start_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2812 WaitableEvent::InitialState::NOT_SIGNALED); | 2815 WaitableEvent::InitialState::NOT_SIGNALED); |
2813 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 2816 WaitableEvent task_stop_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
2814 WaitableEvent::InitialState::NOT_SIGNALED); | 2817 WaitableEvent::InitialState::NOT_SIGNALED); |
2815 thread.task_runner()->PostTask( | 2818 thread.task_runner()->PostTask( |
2816 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2819 FROM_HERE, |
| 2820 BindOnce(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2817 task_start_event.Wait(); | 2821 task_start_event.Wait(); |
2818 | 2822 |
2819 // The thread will timeout in this flush. | 2823 // The thread will timeout in this flush. |
2820 EndTraceAndFlushInThreadWithMessageLoop(); | 2824 EndTraceAndFlushInThreadWithMessageLoop(); |
2821 Clear(); | 2825 Clear(); |
2822 | 2826 |
2823 // Let the thread's message loop continue to spin. | 2827 // Let the thread's message loop continue to spin. |
2824 task_stop_event.Signal(); | 2828 task_stop_event.Signal(); |
2825 | 2829 |
2826 // The following sequence ensures that the FlushCurrentThread task has been | 2830 // The following sequence ensures that the FlushCurrentThread task has been |
2827 // executed in the thread before continuing. | 2831 // executed in the thread before continuing. |
2828 task_start_event.Reset(); | 2832 task_start_event.Reset(); |
2829 task_stop_event.Reset(); | 2833 task_stop_event.Reset(); |
2830 thread.task_runner()->PostTask( | 2834 thread.task_runner()->PostTask( |
2831 FROM_HERE, Bind(&BlockUntilStopped, &task_start_event, &task_stop_event)); | 2835 FROM_HERE, |
| 2836 BindOnce(&BlockUntilStopped, &task_start_event, &task_stop_event)); |
2832 task_start_event.Wait(); | 2837 task_start_event.Wait(); |
2833 task_stop_event.Signal(); | 2838 task_stop_event.Signal(); |
2834 Clear(); | 2839 Clear(); |
2835 | 2840 |
2836 // TraceLog should discover the generation mismatch and recover the thread | 2841 // TraceLog should discover the generation mismatch and recover the thread |
2837 // local buffer for the thread without any error. | 2842 // local buffer for the thread without any error. |
2838 BeginTrace(); | 2843 BeginTrace(); |
2839 thread.task_runner()->PostTask( | 2844 thread.task_runner()->PostTask( |
2840 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); | 2845 FROM_HERE, BindOnce(&TraceWithAllMacroVariants, &task_complete_event)); |
2841 task_complete_event.Wait(); | 2846 task_complete_event.Wait(); |
2842 task_complete_event.Reset(); | 2847 task_complete_event.Reset(); |
2843 EndTraceAndFlushInThreadWithMessageLoop(); | 2848 EndTraceAndFlushInThreadWithMessageLoop(); |
2844 ValidateAllTraceMacrosCreatedData(trace_parsed_); | 2849 ValidateAllTraceMacrosCreatedData(trace_parsed_); |
2845 } | 2850 } |
2846 | 2851 |
2847 std::string* g_log_buffer = NULL; | 2852 std::string* g_log_buffer = NULL; |
2848 bool MockLogMessageHandler(int, const char*, int, size_t, | 2853 bool MockLogMessageHandler(int, const char*, int, size_t, |
2849 const std::string& str) { | 2854 const std::string& str) { |
2850 if (!g_log_buffer) | 2855 if (!g_log_buffer) |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3211 | 3216 |
3212 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { | 3217 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { |
3213 BeginSpecificTrace("-*"); | 3218 BeginSpecificTrace("-*"); |
3214 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); | 3219 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); |
3215 EndTraceAndFlush(); | 3220 EndTraceAndFlush(); |
3216 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); | 3221 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); |
3217 } | 3222 } |
3218 | 3223 |
3219 } // namespace trace_event | 3224 } // namespace trace_event |
3220 } // namespace base | 3225 } // namespace base |
OLD | NEW |