blob: dc9e4ac37303c98e7cf9bab0c2dd5ce2cdd190d6 [file] [log] [blame]
Florian Mayerc7255fd2018-01-25 10:29:24 +00001/*
2 * Copyright (C) 2018 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
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000017#include <pthread.h>
18#include <stdlib.h>
19#include <sys/syscall.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#include <condition_variable>
Florian Mayerc7255fd2018-01-25 10:29:24 +000024#include <thread>
25
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000026#include "perfetto/base/file_utils.h"
27#include "perfetto/base/string_splitter.h"
28#include "perfetto/base/time.h"
Florian Mayerc7255fd2018-01-25 10:29:24 +000029#include "test/task_runner_thread.h"
30
31namespace perfetto {
32
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000033TaskRunnerThread::TaskRunnerThread(const char* name) : name_(name) {}
Florian Mayerc7255fd2018-01-25 10:29:24 +000034TaskRunnerThread::~TaskRunnerThread() {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000035 Stop();
Florian Mayerc7255fd2018-01-25 10:29:24 +000036}
37
38void TaskRunnerThread::Start(std::unique_ptr<ThreadDelegate> delegate) {
39 // Begin holding the lock for the condition variable.
40 std::unique_lock<std::mutex> lock(mutex_);
41
42 // Start the thread.
43 PERFETTO_DCHECK(!runner_);
44 thread_ = std::thread(&TaskRunnerThread::Run, this, std::move(delegate));
45
46 // Wait for runner to be ready.
47 ready_.wait_for(lock, std::chrono::seconds(10),
48 [this]() { return runner_ != nullptr; });
49}
50
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000051void TaskRunnerThread::Stop() {
52 {
53 std::unique_lock<std::mutex> lock(mutex_);
54 if (runner_)
55 runner_->Quit();
56 }
57
58 if (thread_.joinable())
59 thread_.join();
60}
61
62uint64_t TaskRunnerThread::GetThreadCPUTimeNs() {
63 std::condition_variable cv;
64 std::unique_lock<std::mutex> lock(mutex_);
65 uint64_t thread_time_ns = 0;
66
67 if (!runner_)
68 return 0;
69
70 runner_->PostTask([this, &thread_time_ns, &cv] {
71 std::unique_lock<std::mutex> inner_lock(mutex_);
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010072 thread_time_ns = static_cast<uint64_t>(base::GetThreadCPUTimeNs().count());
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000073 cv.notify_one();
74 });
75
76 cv.wait(lock, [&thread_time_ns] { return thread_time_ns != 0; });
77 return thread_time_ns;
78}
79
Florian Mayerc7255fd2018-01-25 10:29:24 +000080void TaskRunnerThread::Run(std::unique_ptr<ThreadDelegate> delegate) {
Primiano Tucci808d6df2018-03-31 13:24:18 +010081#if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
82 pthread_setname_np(name_);
83#else
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000084 pthread_setname_np(pthread_self(), name_);
Primiano Tucci808d6df2018-03-31 13:24:18 +010085#endif
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000086
Florian Mayerc7255fd2018-01-25 10:29:24 +000087 // Create the task runner and execute the specicalised code.
88 base::PlatformTaskRunner task_runner;
89 delegate->Initialize(&task_runner);
90
91 // Pass the runner back to the main thread.
92 {
93 std::unique_lock<std::mutex> lock(mutex_);
94 runner_ = &task_runner;
95 }
96
97 // Notify the main thread that the runner is ready.
98 ready_.notify_one();
99
100 // Spin the loop.
101 task_runner.Run();
102
103 // Ensure we clear out the delegate before runner goes out
104 // of scope.
105 delegate.reset();
106
107 // Cleanup the runner.
108 {
109 std::unique_lock<std::mutex> lock(mutex_);
110 runner_ = nullptr;
111 }
112}
113
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100114ThreadDelegate::~ThreadDelegate() = default;
115
Florian Mayerc7255fd2018-01-25 10:29:24 +0000116} // namespace perfetto