blob: e174c8eff33a25271667e204a28f4ae1c4474555 [file] [log] [blame]
Rhed Jao27077b12020-07-14 18:38:08 +08001/*
2 * Copyright (C) 2020 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#define LOG_TAG "dumpstate"
18
19#include "DumpPool.h"
20
21#include <array>
22#include <thread>
23
24#include <log/log.h>
25
26#include "dumpstate.h"
27#include "DumpstateInternal.h"
28#include "DumpstateUtil.h"
29
30namespace android {
31namespace os {
32namespace dumpstate {
33
34const std::string DumpPool::PREFIX_TMPFILE_NAME = "dump-tmp.";
35
Rhed Jao1c855122020-07-16 17:37:39 +080036DumpPool::DumpPool(const std::string& tmp_root) : tmp_root_(tmp_root), shutdown_(false),
37 log_duration_(true) {
Rhed Jao27077b12020-07-14 18:38:08 +080038 assert(!tmp_root.empty());
39 deleteTempFiles(tmp_root_);
40}
41
42DumpPool::~DumpPool() {
43 shutdown();
44}
45
46void DumpPool::start(int thread_counts) {
47 assert(thread_counts > 0);
48 assert(threads_.empty());
49 if (thread_counts > MAX_THREAD_COUNT) {
50 thread_counts = MAX_THREAD_COUNT;
51 }
52 MYLOGI("Start thread pool:%d", thread_counts);
53 shutdown_ = false;
54 for (int i = 0; i < thread_counts; i++) {
55 threads_.emplace_back(std::thread([=]() {
56 setThreadName(pthread_self(), i + 1);
57 loop();
58 }));
59 }
60}
61
62void DumpPool::shutdown() {
63 std::unique_lock lock(lock_);
64 if (shutdown_ || threads_.empty()) {
65 return;
66 }
67 while (!tasks_.empty()) tasks_.pop();
68 futures_map_.clear();
69
70 shutdown_ = true;
71 condition_variable_.notify_all();
72 lock.unlock();
73
74 for (auto& thread : threads_) {
75 thread.join();
76 }
77 threads_.clear();
78 deleteTempFiles(tmp_root_);
79 MYLOGI("shutdown thread pool");
80}
81
82void DumpPool::waitForTask(const std::string& task_name, const std::string& title,
83 int out_fd) {
84 DurationReporter duration_reporter("Wait for " + task_name, true);
85 auto iterator = futures_map_.find(task_name);
86 if (iterator == futures_map_.end()) {
87 MYLOGW("Task %s does not exist", task_name.c_str());
88 return;
89 }
90 Future future = iterator->second;
91 futures_map_.erase(iterator);
92
93 std::string result = future.get();
94 if (result.empty()) {
95 return;
96 }
97 DumpFileToFd(out_fd, title, result);
98 if (unlink(result.c_str())) {
99 MYLOGE("Failed to unlink (%s): %s\n", result.c_str(), strerror(errno));
100 }
101}
102
Rhed Jao1c855122020-07-16 17:37:39 +0800103void DumpPool::setLogDuration(bool log_duration) {
104 log_duration_ = log_duration;
105}
106
107template <>
108void DumpPool::invokeTask<std::function<void()>>(std::function<void()> dump_func,
109 const std::string& duration_title, int out_fd) {
110 DurationReporter duration_reporter(duration_title, /*logcat_only =*/!log_duration_,
111 /*verbose =*/false, out_fd);
112 std::invoke(dump_func);
113}
114
115template <>
116void DumpPool::invokeTask<std::function<void(int)>>(std::function<void(int)> dump_func,
117 const std::string& duration_title, int out_fd) {
118 DurationReporter duration_reporter(duration_title, /*logcat_only =*/!log_duration_,
119 /*verbose =*/false, out_fd);
120 std::invoke(dump_func, out_fd);
121}
122
Rhed Jao27077b12020-07-14 18:38:08 +0800123std::unique_ptr<DumpPool::TmpFile> DumpPool::createTempFile() {
124 auto tmp_file_ptr = std::make_unique<TmpFile>();
125 std::string file_name_format = "%s/" + PREFIX_TMPFILE_NAME + "XXXXXX";
126 snprintf(tmp_file_ptr->path, sizeof(tmp_file_ptr->path), file_name_format.c_str(),
127 tmp_root_.c_str());
128 tmp_file_ptr->fd.reset(TEMP_FAILURE_RETRY(
129 mkostemp(tmp_file_ptr->path, O_CLOEXEC)));
130 if (tmp_file_ptr->fd.get() == -1) {
131 MYLOGE("open(%s, %s)\n", tmp_file_ptr->path, strerror(errno));
132 tmp_file_ptr = nullptr;
133 return tmp_file_ptr;
134 }
135 return tmp_file_ptr;
136}
137
138void DumpPool::deleteTempFiles(const std::string& folder) {
139 std::unique_ptr<DIR, decltype(&closedir)> dir_ptr(opendir(folder.c_str()),
140 &closedir);
141 if (!dir_ptr) {
142 MYLOGE("Failed to opendir (%s): %s\n", folder.c_str(), strerror(errno));
143 return;
144 }
145 int dir_fd = dirfd(dir_ptr.get());
146 if (dir_fd < 0) {
147 MYLOGE("Failed to get fd of dir (%s): %s\n", folder.c_str(),
148 strerror(errno));
149 return;
150 }
151
152 struct dirent* de;
153 while ((de = readdir(dir_ptr.get()))) {
154 if (de->d_type != DT_REG) {
155 continue;
156 }
157 std::string file_name(de->d_name);
158 if (file_name.find(PREFIX_TMPFILE_NAME) != 0) {
159 continue;
160 }
161 if (unlinkat(dir_fd, file_name.c_str(), 0)) {
162 MYLOGE("Failed to unlink (%s): %s\n", file_name.c_str(),
163 strerror(errno));
164 }
165 }
166}
167
168void DumpPool::setThreadName(const pthread_t thread, int id) {
169 std::array<char, 15> name;
170 snprintf(name.data(), name.size(), "dumpstate_%d", id);
171 pthread_setname_np(thread, name.data());
172}
173
174void DumpPool::loop() {
175 std::unique_lock lock(lock_);
176 while (!shutdown_) {
177 if (tasks_.empty()) {
178 condition_variable_.wait(lock);
179 continue;
180 } else {
181 std::packaged_task<std::string()> task = std::move(tasks_.front());
182 tasks_.pop();
183 lock.unlock();
184 std::invoke(task);
185 lock.lock();
186 }
187 }
188}
189
190} // namespace dumpstate
191} // namespace os
192} // namespace android