1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "perfetto/tracing/platform.h"
18 
19 #include <atomic>
20 
21 #include "perfetto/base/task_runner.h"
22 #include "perfetto/ext/base/waitable_event.h"
23 #include "perfetto/tracing/internal/tracing_tls.h"
24 #include "test/gtest_and_gmock.h"
25 
26 namespace perfetto {
27 namespace {
28 
GetTLS()29 internal::TracingTLS* GetTLS() {
30   return static_cast<internal::TracingTLS*>(
31       Platform::GetDefaultPlatform()->GetOrCreateThreadLocalObject());
32 }
33 
34 // We use this class only as a listener to detect thread-local destruction.
35 class FakeTraceWriter : public TraceWriterBase {
36  public:
37   std::atomic<bool>* destroyed_flag;
38 
~FakeTraceWriter()39   ~FakeTraceWriter() override { *destroyed_flag = true; }
NewTracePacket()40   protozero::MessageHandle<protos::pbzero::TracePacket> NewTracePacket() {
41     PERFETTO_CHECK(false);
42   }
Flush(std::function<void ()>)43   void Flush(std::function<void()>) override {}
written() const44   uint64_t written() const override { return 0; }
45 };
46 
47 // This test mainly checks that the thread at-exit logic works properly and
48 // destroys the TracingTLS when a thread exits.
TEST(PlatformUnittest,ThreadingAndTLSDtor)49 TEST(PlatformUnittest, ThreadingAndTLSDtor) {
50   auto* platform = Platform::GetDefaultPlatform();
51   if (!platform)
52     GTEST_SKIP() << "Platform::GetDefaultPlatform() not implemented";
53 
54   auto proc_name = platform->GetCurrentProcessName();
55   EXPECT_FALSE(proc_name.empty());
56 
57   // Create two threads.
58 
59   Platform::CreateTaskRunnerArgs tr_args{};
60   auto thread1 = platform->CreateTaskRunner(tr_args);
61   ASSERT_TRUE(thread1);
62 
63   auto thread2 = platform->CreateTaskRunner(tr_args);
64   ASSERT_TRUE(thread2);
65 
66   // Check that the TLS is actually thread-local.
67 
68   thread1->PostTask([] { GetTLS()->generation = 101; });
69   thread2->PostTask([] { GetTLS()->generation = 102; });
70   std::atomic<bool> thread1_destroyed{};
71   std::atomic<bool> thread2_destroyed{};
72 
73   // Now post another task on each thread. The task will:
74   // 1. Check that the generation matches what previously set.
75   // 2. Create a FakeTraceWriter and wire up a destruction event.
76   base::WaitableEvent evt1;
77   thread1->PostTask([&] {
78     EXPECT_EQ(GetTLS()->generation, 101u);
79     GetTLS()->data_sources_tls[0].per_instance[0].Reset();
80     std::unique_ptr<FakeTraceWriter> tw(new FakeTraceWriter());
81     tw->destroyed_flag = &thread1_destroyed;
82     GetTLS()->data_sources_tls[0].per_instance[0].trace_writer = std::move(tw);
83     evt1.Notify();
84   });
85   evt1.Wait();
86 
87   base::WaitableEvent evt2;
88   thread2->PostTask([&] {
89     EXPECT_EQ(GetTLS()->generation, 102u);
90     GetTLS()->data_sources_tls[0].per_instance[0].Reset();
91     std::unique_ptr<FakeTraceWriter> tw(new FakeTraceWriter());
92     tw->destroyed_flag = &thread2_destroyed;
93     GetTLS()->data_sources_tls[0].per_instance[0].trace_writer = std::move(tw);
94     evt2.Notify();
95   });
96   evt2.Wait();
97 
98   EXPECT_FALSE(thread1_destroyed);
99   EXPECT_FALSE(thread2_destroyed);
100 
101   thread1.reset();
102   EXPECT_TRUE(thread1_destroyed);
103   EXPECT_FALSE(thread2_destroyed);
104 
105   thread2.reset();
106   EXPECT_TRUE(thread2_destroyed);
107 }
108 
109 }  // namespace
110 }  // namespace perfetto
111