/root/doris/be/src/util/thread.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | // Licensed to the Apache Software Foundation (ASF) under one | 
| 2 |  | // or more contributor license agreements.  See the NOTICE file | 
| 3 |  | // distributed with this work for additional information | 
| 4 |  | // regarding copyright ownership.  The ASF licenses this file | 
| 5 |  | // to you under the Apache License, Version 2.0 (the | 
| 6 |  | // "License"); you may not use this file except in compliance | 
| 7 |  | // with the License.  You may obtain a copy of the License at | 
| 8 |  | // | 
| 9 |  | //   http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  | // | 
| 11 |  | // Unless required by applicable law or agreed to in writing, | 
| 12 |  | // software distributed under the License is distributed on an | 
| 13 |  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
| 14 |  | // KIND, either express or implied.  See the License for the | 
| 15 |  | // specific language governing permissions and limitations | 
| 16 |  | // under the License. | 
| 17 |  | // This file is copied from | 
| 18 |  | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/thread.cc | 
| 19 |  | // and modified by Doris | 
| 20 |  |  | 
| 21 |  | #include "thread.h" | 
| 22 |  |  | 
| 23 |  | #include <sys/resource.h> | 
| 24 |  |  | 
| 25 |  | #ifndef __APPLE__ | 
| 26 |  | // IWYU pragma: no_include <bits/types/struct_sched_param.h> | 
| 27 |  | #include <sched.h> | 
| 28 |  | #include <sys/prctl.h> | 
| 29 |  | #else | 
| 30 |  | #include <pthread.h> | 
| 31 |  |  | 
| 32 |  | #include <cstdint> | 
| 33 |  | #endif | 
| 34 |  |  | 
| 35 |  | // IWYU pragma: no_include <bthread/errno.h> | 
| 36 |  | #include <errno.h> // IWYU pragma: keep | 
| 37 |  | #include <sys/syscall.h> | 
| 38 |  | #include <time.h> | 
| 39 |  | #include <unistd.h> | 
| 40 |  |  | 
| 41 |  | #include <algorithm> | 
| 42 |  | // IWYU pragma: no_include <bits/chrono.h> | 
| 43 |  | #include <chrono> // IWYU pragma: keep | 
| 44 |  | #include <cstring> | 
| 45 |  | #include <functional> | 
| 46 |  | #include <limits> | 
| 47 |  | #include <map> | 
| 48 |  | #include <memory> | 
| 49 |  | #include <mutex> | 
| 50 |  | #include <ostream> | 
| 51 |  | #include <string> | 
| 52 |  | #include <vector> | 
| 53 |  |  | 
| 54 |  | #include "common/config.h" | 
| 55 |  | #include "common/logging.h" | 
| 56 |  | #include "gutil/atomicops.h" | 
| 57 |  | #include "gutil/dynamic_annotations.h" | 
| 58 |  | #include "gutil/map-util.h" | 
| 59 |  | #include "gutil/stringprintf.h" | 
| 60 |  | #include "gutil/strings/substitute.h" | 
| 61 |  | #include "http/web_page_handler.h" | 
| 62 |  | #include "runtime/thread_context.h" | 
| 63 |  | #include "util/debug/sanitizer_scopes.h" | 
| 64 |  | #include "util/easy_json.h" | 
| 65 |  | #include "util/os_util.h" | 
| 66 |  | #include "util/scoped_cleanup.h" | 
| 67 |  | #include "util/url_coding.h" | 
| 68 |  |  | 
| 69 |  | namespace doris { | 
| 70 |  |  | 
| 71 |  | class ThreadMgr; | 
| 72 |  |  | 
| 73 |  | __thread Thread* Thread::_tls = nullptr; | 
| 74 |  |  | 
| 75 |  | // Singleton instance of ThreadMgr. Only visible in this file, used only by Thread. | 
| 76 |  | // // The Thread class adds a reference to thread_manager while it is supervising a thread so | 
| 77 |  | // // that a race between the end of the process's main thread (and therefore the destruction | 
| 78 |  | // // of thread_manager) and the end of a thread that tries to remove itself from the | 
| 79 |  | // // manager after the destruction can be avoided. | 
| 80 |  | static std::shared_ptr<ThreadMgr> thread_manager; | 
| 81 |  | // | 
| 82 |  | // Controls the single (lazy) initialization of thread_manager. | 
| 83 |  | static std::once_flag once; | 
| 84 |  |  | 
| 85 |  | // A singleton class that tracks all live threads, and groups them together for easy | 
| 86 |  | // auditing. Used only by Thread. | 
| 87 |  | class ThreadMgr { | 
| 88 |  | public: | 
| 89 | 8 |     ThreadMgr() : _threads_started_metric(0), _threads_running_metric(0) {} | 
| 90 |  |  | 
| 91 | 0 |     ~ThreadMgr() { | 
| 92 | 0 |         std::unique_lock<std::mutex> lock(_lock); | 
| 93 | 0 |         _thread_categories.clear(); | 
| 94 | 0 |     } | 
| 95 |  |  | 
| 96 |  |     static void set_thread_name(const std::string& name, int64_t tid); | 
| 97 |  |  | 
| 98 |  | #ifndef __APPLE__ | 
| 99 |  |     static void set_idle_sched(int64_t tid); | 
| 100 |  |  | 
| 101 |  |     static void set_thread_nice_value(int64_t tid); | 
| 102 |  | #endif | 
| 103 |  |  | 
| 104 |  |     // not the system TID, since pthread_t is less prone to being recycled. | 
| 105 |  |     void add_thread(const pthread_t& pthread_id, const std::string& name, | 
| 106 |  |                     const std::string& category, int64_t tid); | 
| 107 |  |  | 
| 108 |  |     // Removes a thread from the supplied category. If the thread has | 
| 109 |  |     // already been removed, this is a no-op. | 
| 110 |  |     void remove_thread(const pthread_t& pthread_id, const std::string& category); | 
| 111 |  |  | 
| 112 |  |     void display_thread_callback(const WebPageHandler::ArgumentMap& args, EasyJson* ej) const; | 
| 113 |  |  | 
| 114 |  | private: | 
| 115 |  |     // Container class for any details we want to capture about a thread | 
| 116 |  |     // TODO: Add start-time. | 
| 117 |  |     // TODO: Track fragment ID. | 
| 118 |  |     class ThreadDescriptor { | 
| 119 |  |     public: | 
| 120 | 27.0k |         ThreadDescriptor() {} | 
| 121 |  |         ThreadDescriptor(std::string category, std::string name, int64_t thread_id) | 
| 122 | 27.0k |                 : _name(std::move(name)), _category(std::move(category)), _thread_id(thread_id) {} | 
| 123 |  |  | 
| 124 | 0 |         const std::string& name() const { return _name; } | 
| 125 | 0 |         const std::string& category() const { return _category; } | 
| 126 | 0 |         int64_t thread_id() const { return _thread_id; } | 
| 127 |  |  | 
| 128 |  |     private: | 
| 129 |  |         std::string _name; | 
| 130 |  |         std::string _category; | 
| 131 |  |         int64_t _thread_id; | 
| 132 |  |     }; | 
| 133 |  |  | 
| 134 |  |     void summarize_thread_descriptor(const ThreadDescriptor& desc, EasyJson* ej) const; | 
| 135 |  |  | 
| 136 |  |     // A ThreadCategory is a set of threads that are logically related. | 
| 137 |  |     // TODO: unordered_map is incompatible with pthread_t, but would be more | 
| 138 |  |     // efficient here. | 
| 139 |  |     typedef std::map<const pthread_t, ThreadDescriptor> ThreadCategory; | 
| 140 |  |  | 
| 141 |  |     // All thread categories, keyed on the category name. | 
| 142 |  |     typedef std::map<std::string, ThreadCategory> ThreadCategoryMap; | 
| 143 |  |  | 
| 144 |  |     // Protects _thread_categories and thread metrics. | 
| 145 |  |     mutable std::mutex _lock; | 
| 146 |  |  | 
| 147 |  |     // All thread categories that ever contained a thread, even if empty | 
| 148 |  |     ThreadCategoryMap _thread_categories; | 
| 149 |  |  | 
| 150 |  |     // Counters to track all-time total number of threads, and the | 
| 151 |  |     // current number of running threads. | 
| 152 |  |     uint64_t _threads_started_metric; | 
| 153 |  |     uint64_t _threads_running_metric; | 
| 154 |  |  | 
| 155 |  |     DISALLOW_COPY_AND_ASSIGN(ThreadMgr); | 
| 156 |  | }; | 
| 157 |  |  | 
| 158 | 34.3k | void ThreadMgr::set_thread_name(const std::string& name, int64_t tid) { | 
| 159 | 34.3k |     if (tid == getpid()) { | 
| 160 | 0 |         return; | 
| 161 | 0 |     } | 
| 162 |  | #ifdef __APPLE__ | 
| 163 |  |     int err = pthread_setname_np(name.c_str()); | 
| 164 |  | #else | 
| 165 | 34.3k |     int err = prctl(PR_SET_NAME, name.c_str()); | 
| 166 | 34.3k | #endif | 
| 167 | 34.3k |     if (err < 0 && errno != EPERM) { | 
| 168 | 0 |         LOG(ERROR) << "set_thread_name"; | 
| 169 | 0 |     } | 
| 170 | 34.3k | } | 
| 171 |  |  | 
| 172 |  | #ifndef __APPLE__ | 
| 173 | 184 | void ThreadMgr::set_idle_sched(int64_t tid) { | 
| 174 | 184 |     if (tid == getpid()) { | 
| 175 | 0 |         return; | 
| 176 | 0 |     } | 
| 177 | 184 |     struct sched_param sp = {.sched_priority = 0}; | 
| 178 | 184 |     int err = sched_setscheduler(0, SCHED_IDLE, &sp); | 
| 179 | 184 |     if (err < 0 && errno != EPERM) { | 
| 180 | 0 |         LOG(ERROR) << "set_thread_idle_sched"; | 
| 181 | 0 |     } | 
| 182 | 184 | } | 
| 183 |  |  | 
| 184 | 0 | void ThreadMgr::set_thread_nice_value(int64_t tid) { | 
| 185 | 0 |     if (tid == getpid()) { | 
| 186 | 0 |         return; | 
| 187 | 0 |     } | 
| 188 |  |     // From Linux kernel: | 
| 189 |  |     // In the current implementation, each unit of difference in the nice values of two | 
| 190 |  |     // processes results in a factor of 1.25 in the degree to which the | 
| 191 |  |     // scheduler favors the higher priority process.  This causes very | 
| 192 |  |     // low nice values (+19) to truly provide little CPU to a process | 
| 193 |  |     // whenever there is any other higher priority load on the system, | 
| 194 |  |     // and makes high nice values (-20) deliver most of the CPU to | 
| 195 |  |     // applications that require it (e.g., some audio applications). | 
| 196 |  |  | 
| 197 |  |     // Choose 5 as lower priority value, default is 0 | 
| 198 | 0 |     int err = setpriority(PRIO_PROCESS, 0, config::scan_thread_nice_value); | 
| 199 | 0 |     if (err < 0 && errno != EPERM) { | 
| 200 | 0 |         LOG(ERROR) << "set_thread_low_priority"; | 
| 201 | 0 |     } | 
| 202 | 0 | } | 
| 203 |  | #endif | 
| 204 |  |  | 
| 205 |  | void ThreadMgr::add_thread(const pthread_t& pthread_id, const std::string& name, | 
| 206 | 26.9k |                            const std::string& category, int64_t tid) { | 
| 207 |  |     // These annotations cause TSAN to ignore the synchronization on lock_ | 
| 208 |  |     // without causing the subsequent mutations to be treated as data races | 
| 209 |  |     // in and of themselves (that's what IGNORE_READS_AND_WRITES does). | 
| 210 |  |     // | 
| 211 |  |     // Why do we need them here and in SuperviseThread()? TSAN operates by | 
| 212 |  |     // observing synchronization events and using them to establish "happens | 
| 213 |  |     // before" relationships between threads. Where these relationships are | 
| 214 |  |     // not built, shared state access constitutes a data race. The | 
| 215 |  |     // synchronization events here, in RemoveThread(), and in | 
| 216 |  |     // SuperviseThread() may cause TSAN to establish a "happens before" | 
| 217 |  |     // relationship between thread functors, ignoring potential data races. | 
| 218 |  |     // The annotations prevent this from happening. | 
| 219 | 26.9k |     ANNOTATE_IGNORE_SYNC_BEGIN(); | 
| 220 | 26.9k |     debug::ScopedTSANIgnoreReadsAndWrites ignore_tsan; | 
| 221 | 26.9k |     { | 
| 222 | 26.9k |         std::unique_lock<std::mutex> l(_lock); | 
| 223 | 26.9k |         _thread_categories[category][pthread_id] = ThreadDescriptor(category, name, tid); | 
| 224 | 26.9k |         _threads_running_metric++; | 
| 225 | 26.9k |         _threads_started_metric++; | 
| 226 | 26.9k |     } | 
| 227 | 26.9k |     ANNOTATE_IGNORE_SYNC_END(); | 
| 228 | 26.9k | } | 
| 229 |  |  | 
| 230 | 22.4k | void ThreadMgr::remove_thread(const pthread_t& pthread_id, const std::string& category) { | 
| 231 | 22.4k |     ANNOTATE_IGNORE_SYNC_BEGIN(); | 
| 232 | 22.4k |     debug::ScopedTSANIgnoreReadsAndWrites ignore_tsan; | 
| 233 | 22.4k |     { | 
| 234 | 22.4k |         std::unique_lock<std::mutex> l(_lock); | 
| 235 | 22.4k |         auto category_it = _thread_categories.find(category); | 
| 236 | 22.4k |         DCHECK(category_it != _thread_categories.end()); | 
| 237 | 22.4k |         category_it->second.erase(pthread_id); | 
| 238 | 22.4k |         _threads_running_metric--; | 
| 239 | 22.4k |     } | 
| 240 | 22.4k |     ANNOTATE_IGNORE_SYNC_END(); | 
| 241 | 22.4k | } | 
| 242 |  |  | 
| 243 |  | void ThreadMgr::display_thread_callback(const WebPageHandler::ArgumentMap& args, | 
| 244 | 0 |                                         EasyJson* ej) const { | 
| 245 | 0 |     const auto* category_name = FindOrNull(args, "group"); | 
| 246 | 0 |     if (category_name) { | 
| 247 | 0 |         bool requested_all = (*category_name == "all"); | 
| 248 | 0 |         ej->Set("requested_thread_group", EasyJson::kObject); | 
| 249 | 0 |         (*ej)["group_name"] = escape_for_html_to_string(*category_name); | 
| 250 | 0 |         (*ej)["requested_all"] = requested_all; | 
| 251 |  |  | 
| 252 |  |         // The critical section is as short as possible so as to minimize the delay | 
| 253 |  |         // imposed on new threads that acquire the lock in write mode. | 
| 254 | 0 |         std::vector<ThreadDescriptor> descriptors_to_print; | 
| 255 | 0 |         if (!requested_all) { | 
| 256 | 0 |             std::unique_lock<std::mutex> l(_lock); | 
| 257 | 0 |             const auto* category = FindOrNull(_thread_categories, *category_name); | 
| 258 | 0 |             if (!category) { | 
| 259 | 0 |                 return; | 
| 260 | 0 |             } | 
| 261 | 0 |             for (const auto& elem : *category) { | 
| 262 | 0 |                 descriptors_to_print.emplace_back(elem.second); | 
| 263 | 0 |             } | 
| 264 | 0 |         } else { | 
| 265 | 0 |             std::unique_lock<std::mutex> l(_lock); | 
| 266 | 0 |             for (const auto& category : _thread_categories) { | 
| 267 | 0 |                 for (const auto& elem : category.second) { | 
| 268 | 0 |                     descriptors_to_print.emplace_back(elem.second); | 
| 269 | 0 |                 } | 
| 270 | 0 |             } | 
| 271 | 0 |         } | 
| 272 |  |  | 
| 273 | 0 |         EasyJson found = (*ej).Set("found", EasyJson::kObject); | 
| 274 | 0 |         EasyJson threads = found.Set("threads", EasyJson::kArray); | 
| 275 | 0 |         for (const auto& desc : descriptors_to_print) { | 
| 276 | 0 |             summarize_thread_descriptor(desc, &threads); | 
| 277 | 0 |         } | 
| 278 | 0 |     } else { | 
| 279 |  |         // List all thread groups and the number of threads running in each. | 
| 280 | 0 |         std::vector<std::pair<string, uint64_t>> thread_categories_info; | 
| 281 | 0 |         uint64_t running; | 
| 282 | 0 |         { | 
| 283 | 0 |             std::unique_lock<std::mutex> l(_lock); | 
| 284 | 0 |             running = _threads_running_metric; | 
| 285 | 0 |             thread_categories_info.reserve(_thread_categories.size()); | 
| 286 | 0 |             for (const auto& category : _thread_categories) { | 
| 287 | 0 |                 thread_categories_info.emplace_back(category.first, category.second.size()); | 
| 288 | 0 |             } | 
| 289 |  | 
 | 
| 290 | 0 |             (*ej)["total_threads_running"] = running; | 
| 291 | 0 |             EasyJson groups = ej->Set("groups", EasyJson::kArray); | 
| 292 | 0 |             for (const auto& elem : thread_categories_info) { | 
| 293 | 0 |                 string category_arg; | 
| 294 | 0 |                 url_encode(elem.first, &category_arg); | 
| 295 | 0 |                 EasyJson group = groups.PushBack(EasyJson::kObject); | 
| 296 | 0 |                 group["encoded_group_name"] = category_arg; | 
| 297 | 0 |                 group["group_name"] = elem.first; | 
| 298 | 0 |                 group["threads_running"] = elem.second; | 
| 299 | 0 |             } | 
| 300 | 0 |         } | 
| 301 | 0 |     } | 
| 302 | 0 | } | 
| 303 |  |  | 
| 304 |  | void ThreadMgr::summarize_thread_descriptor(const ThreadMgr::ThreadDescriptor& desc, | 
| 305 | 0 |                                             EasyJson* ej) const { | 
| 306 | 0 |     ThreadStats stats; | 
| 307 | 0 |     Status status = get_thread_stats(desc.thread_id(), &stats); | 
| 308 | 0 |     if (!status.ok()) { | 
| 309 | 0 |         LOG(WARNING) << "Could not get per-thread statistics: " << status.to_string(); | 
| 310 | 0 |     } | 
| 311 |  | 
 | 
| 312 | 0 |     EasyJson thread = ej->PushBack(EasyJson::kObject); | 
| 313 | 0 |     thread["thread_name"] = desc.name(); | 
| 314 | 0 |     thread["user_sec"] = static_cast<double>(stats.user_ns) / 1e9; | 
| 315 | 0 |     thread["kernel_sec"] = static_cast<double>(stats.kernel_ns) / 1e9; | 
| 316 | 0 |     thread["iowait_sec"] = static_cast<double>(stats.iowait_ns) / 1e9; | 
| 317 | 0 | } | 
| 318 |  |  | 
| 319 | 22.4k | Thread::~Thread() { | 
| 320 | 22.4k |     if (_joinable) { | 
| 321 | 21.2k |         int ret = pthread_detach(_thread); | 
| 322 | 21.2k |         CHECK_EQ(ret, 0); | 
| 323 | 21.2k |     } | 
| 324 | 22.4k | } | 
| 325 |  |  | 
| 326 | 7.34k | void Thread::set_self_name(const std::string& name) { | 
| 327 | 7.34k |     ThreadMgr::set_thread_name(name, current_thread_id()); | 
| 328 | 7.34k | } | 
| 329 |  |  | 
| 330 |  | #ifndef __APPLE__ | 
| 331 | 184 | void Thread::set_idle_sched() { | 
| 332 | 184 |     ThreadMgr::set_idle_sched(current_thread_id()); | 
| 333 | 184 | } | 
| 334 |  |  | 
| 335 | 0 | void Thread::set_thread_nice_value() { | 
| 336 | 0 |     ThreadMgr::set_thread_nice_value(current_thread_id()); | 
| 337 | 0 | } | 
| 338 |  | #endif | 
| 339 |  |  | 
| 340 | 1.16k | void Thread::join() { | 
| 341 | 1.16k |     static_cast<void>(ThreadJoiner(this).join()); | 
| 342 | 1.16k | } | 
| 343 |  |  | 
| 344 | 1.00k | int64_t Thread::tid() const { | 
| 345 | 1.00k |     int64_t t = base::subtle::Acquire_Load(&_tid); | 
| 346 | 1.00k |     if (t != PARENT_WAITING_TID) { | 
| 347 | 1.00k |         return _tid; | 
| 348 | 1.00k |     } | 
| 349 | 1 |     return wait_for_tid(); | 
| 350 | 1.00k | } | 
| 351 |  |  | 
| 352 | 0 | pthread_t Thread::pthread_id() const { | 
| 353 | 0 |     return _thread; | 
| 354 | 0 | } | 
| 355 |  |  | 
| 356 | 26.9k | const std::string& Thread::name() const { | 
| 357 | 26.9k |     return _name; | 
| 358 | 26.9k | } | 
| 359 |  |  | 
| 360 | 49.4k | const std::string& Thread::category() const { | 
| 361 | 49.4k |     return _category; | 
| 362 | 49.4k | } | 
| 363 |  |  | 
| 364 | 0 | std::string Thread::to_string() const { | 
| 365 | 0 |     return strings::Substitute("Thread $0 (name: \"$1\", category: \"$2\")", tid(), _name, | 
| 366 | 0 |                                _category); | 
| 367 | 0 | } | 
| 368 |  |  | 
| 369 | 706k | Thread* Thread::current_thread() { | 
| 370 | 706k |     return _tls; | 
| 371 | 706k | } | 
| 372 |  |  | 
| 373 | 0 | int64_t Thread::unique_thread_id() { | 
| 374 |  | #ifdef __APPLE__ | 
| 375 |  |     uint64_t tid; | 
| 376 |  |     pthread_threadid_np(pthread_self(), &tid); | 
| 377 |  |     return tid; | 
| 378 |  | #else | 
| 379 | 0 |     return static_cast<int64_t>(pthread_self()); | 
| 380 | 0 | #endif | 
| 381 | 0 | } | 
| 382 |  |  | 
| 383 | 34.5k | int64_t Thread::current_thread_id() { | 
| 384 |  | #ifdef __APPLE__ | 
| 385 |  |     uint64_t tid; | 
| 386 |  |     pthread_threadid_np(nullptr, &tid); | 
| 387 |  |     return tid; | 
| 388 |  | #else | 
| 389 | 34.5k |     return syscall(SYS_gettid); | 
| 390 | 34.5k | #endif | 
| 391 | 34.5k | } | 
| 392 |  |  | 
| 393 | 1 | int64_t Thread::wait_for_tid() const { | 
| 394 | 1 |     int loop_count = 0; | 
| 395 | 38 |     while (true) { | 
| 396 | 38 |         int64_t t = Acquire_Load(&_tid); | 
| 397 | 38 |         if (t != PARENT_WAITING_TID) { | 
| 398 | 1 |             return t; | 
| 399 | 1 |         } | 
| 400 |  |         // copied from boost::detail::yield | 
| 401 | 37 |         int k = loop_count++; | 
| 402 | 37 |         if (k < 32 || k & 1) { | 
| 403 | 34 |             sched_yield(); | 
| 404 | 34 |         } else { | 
| 405 |  |             // g++ -Wextra warns on {} or {0} | 
| 406 | 3 |             struct timespec rqtp = {0, 0}; | 
| 407 |  |  | 
| 408 |  |             // POSIX says that timespec has tv_sec and tv_nsec | 
| 409 |  |             // But it doesn't guarantee order or placement | 
| 410 |  |  | 
| 411 | 3 |             rqtp.tv_sec = 0; | 
| 412 | 3 |             rqtp.tv_nsec = 1000; | 
| 413 |  |  | 
| 414 | 3 |             nanosleep(&rqtp, 0); | 
| 415 | 3 |         } | 
| 416 | 37 |     } | 
| 417 | 1 | } | 
| 418 |  |  | 
| 419 |  | Status Thread::start_thread(const std::string& category, const std::string& name, | 
| 420 |  |                             const ThreadFunctor& functor, uint64_t flags, | 
| 421 | 27.0k |                             scoped_refptr<Thread>* holder) { | 
| 422 | 27.0k |     std::call_once(once, init_threadmgr); | 
| 423 |  |  | 
| 424 |  |     // Temporary reference for the duration of this function. | 
| 425 | 27.0k |     scoped_refptr<Thread> t(new Thread(category, name, functor)); | 
| 426 |  |  | 
| 427 |  |     // Optional, and only set if the thread was successfully created. | 
| 428 |  |     // | 
| 429 |  |     // We have to set this before we even start the thread because it's | 
| 430 |  |     // allowed for the thread functor to access 'holder'. | 
| 431 | 27.0k |     if (holder) { | 
| 432 | 1.37k |         *holder = t; | 
| 433 | 1.37k |     } | 
| 434 |  |  | 
| 435 | 27.0k |     t->_tid = PARENT_WAITING_TID; | 
| 436 |  |  | 
| 437 |  |     // Add a reference count to the thread since SuperviseThread() needs to | 
| 438 |  |     // access the thread object, and we have no guarantee that our caller | 
| 439 |  |     // won't drop the reference as soon as we return. This is dereferenced | 
| 440 |  |     // in FinishThread(). | 
| 441 | 27.0k |     t->AddRef(); | 
| 442 |  |  | 
| 443 | 27.0k |     auto cleanup = MakeScopedCleanup([&]() { | 
| 444 |  |         // If we failed to create the thread, we need to undo all of our prep work. | 
| 445 | 0 |         t->_tid = INVALID_TID; | 
| 446 | 0 |         t->Release(); | 
| 447 | 0 |     }); | 
| 448 |  |  | 
| 449 | 27.0k |     int ret = pthread_create(&t->_thread, nullptr, &Thread::supervise_thread, t.get()); | 
| 450 | 27.0k |     if (ret) { | 
| 451 | 0 |         return Status::RuntimeError("Could not create thread. (error {}) {}", ret, strerror(ret)); | 
| 452 | 0 |     } | 
| 453 |  |  | 
| 454 |  |     // The thread has been created and is now joinable. | 
| 455 |  |     // | 
| 456 |  |     // Why set this in the parent and not the child? Because only the parent | 
| 457 |  |     // (or someone communicating with the parent) can join, so joinable must | 
| 458 |  |     // be set before the parent returns. | 
| 459 | 27.0k |     t->_joinable = true; | 
| 460 | 27.0k |     cleanup.cancel(); | 
| 461 |  |  | 
| 462 | 18.4E |     VLOG_NOTICE << "Started thread " << t->tid() << " - " << category << ":" << name; | 
| 463 | 27.0k |     return Status::OK(); | 
| 464 | 27.0k | } | 
| 465 |  |  | 
| 466 | 27.0k | void* Thread::supervise_thread(void* arg) { | 
| 467 | 27.0k |     Thread* t = static_cast<Thread*>(arg); | 
| 468 | 27.0k |     int64_t system_tid = Thread::current_thread_id(); | 
| 469 | 27.0k |     PCHECK(system_tid != -1); | 
| 470 |  |  | 
| 471 |  |     // Take an additional reference to the thread manager, which we'll need below. | 
| 472 | 27.0k |     ANNOTATE_IGNORE_SYNC_BEGIN(); | 
| 473 | 27.0k |     std::shared_ptr<ThreadMgr> thread_mgr_ref = thread_manager; | 
| 474 | 27.0k |     ANNOTATE_IGNORE_SYNC_END(); | 
| 475 |  |  | 
| 476 |  |     // Set up the TLS. | 
| 477 |  |     // | 
| 478 |  |     // We could store a scoped_refptr in the TLS itself, but as its | 
| 479 |  |     // lifecycle is poorly defined, we'll use a bare pointer. We | 
| 480 |  |     // already incremented the reference count in StartThread. | 
| 481 | 27.0k |     Thread::_tls = t; | 
| 482 |  |  | 
| 483 |  |     // Create thread context, there is no need to create it when func is executed. | 
| 484 | 27.0k |     ThreadLocalHandle::create_thread_local_if_not_exits(); | 
| 485 |  |  | 
| 486 |  |     // Publish our tid to '_tid', which unblocks any callers waiting in | 
| 487 |  |     // WaitForTid(). | 
| 488 | 27.0k |     Release_Store(&t->_tid, system_tid); | 
| 489 |  |  | 
| 490 | 27.0k |     std::string name = strings::Substitute("$0-$1", t->name(), system_tid); | 
| 491 | 27.0k |     thread_manager->set_thread_name(name, t->_tid); | 
| 492 | 27.0k |     thread_manager->add_thread(pthread_self(), name, t->category(), t->_tid); | 
| 493 |  |  | 
| 494 |  |     // FinishThread() is guaranteed to run (even if functor_ throws an | 
| 495 |  |     // exception) because pthread_cleanup_push() creates a scoped object | 
| 496 |  |     // whose destructor invokes the provided callback. | 
| 497 | 27.0k |     pthread_cleanup_push(&Thread::finish_thread, t); | 
| 498 | 27.0k |     t->_functor(); | 
| 499 | 27.0k |     pthread_cleanup_pop(true); | 
| 500 |  |  | 
| 501 | 27.0k |     return nullptr; | 
| 502 | 27.0k | } | 
| 503 |  |  | 
| 504 | 22.4k | void Thread::finish_thread(void* arg) { | 
| 505 | 22.4k |     Thread* t = static_cast<Thread*>(arg); | 
| 506 |  |  | 
| 507 |  |     // We're here either because of the explicit pthread_cleanup_pop() in | 
| 508 |  |     // SuperviseThread() or through pthread_exit(). In either case, | 
| 509 |  |     // thread_manager is guaranteed to be live because thread_mgr_ref in | 
| 510 |  |     // SuperviseThread() is still live. | 
| 511 | 22.4k |     thread_manager->remove_thread(pthread_self(), t->category()); | 
| 512 |  |  | 
| 513 |  |     // Signal any Joiner that we're done. | 
| 514 | 22.4k |     t->_done.count_down(); | 
| 515 |  |  | 
| 516 | 18.4E |     VLOG_CRITICAL << "Ended thread " << t->_tid << " - " << t->category() << ":" << t->name(); | 
| 517 | 22.4k |     t->Release(); | 
| 518 |  |     // NOTE: the above 'Release' call could be the last reference to 'this', | 
| 519 |  |     // so 'this' could be destructed at this point. Do not add any code | 
| 520 |  |     // following here! | 
| 521 |  |  | 
| 522 | 22.4k |     ThreadLocalHandle::del_thread_local_if_count_is_zero(); | 
| 523 | 22.4k | } | 
| 524 |  |  | 
| 525 | 8 | void Thread::init_threadmgr() { | 
| 526 | 8 |     thread_manager.reset(new ThreadMgr()); | 
| 527 | 8 | } | 
| 528 |  |  | 
| 529 |  | ThreadJoiner::ThreadJoiner(Thread* thr) | 
| 530 |  |         : _thread(CHECK_NOTNULL(thr)), | 
| 531 |  |           _warn_after_ms(kDefaultWarnAfterMs), | 
| 532 |  |           _warn_every_ms(kDefaultWarnEveryMs), | 
| 533 | 1.16k |           _give_up_after_ms(kDefaultGiveUpAfterMs) {} | 
| 534 |  |  | 
| 535 | 1 | ThreadJoiner& ThreadJoiner::warn_after_ms(int ms) { | 
| 536 | 1 |     _warn_after_ms = ms; | 
| 537 | 1 |     return *this; | 
| 538 | 1 | } | 
| 539 |  |  | 
| 540 | 1 | ThreadJoiner& ThreadJoiner::warn_every_ms(int ms) { | 
| 541 | 1 |     _warn_every_ms = ms; | 
| 542 | 1 |     return *this; | 
| 543 | 1 | } | 
| 544 |  |  | 
| 545 | 1 | ThreadJoiner& ThreadJoiner::give_up_after_ms(int ms) { | 
| 546 | 1 |     _give_up_after_ms = ms; | 
| 547 | 1 |     return *this; | 
| 548 | 1 | } | 
| 549 |  |  | 
| 550 | 1.16k | Status ThreadJoiner::join() { | 
| 551 | 1.16k |     if (Thread::current_thread() && Thread::current_thread()->tid() == _thread->tid()) { | 
| 552 | 1 |         return Status::InvalidArgument("Can't join on own thread. (error {}) {}", -1, | 
| 553 | 1 |                                        _thread->_name); | 
| 554 | 1 |     } | 
| 555 |  |  | 
| 556 |  |     // Early exit: double join is a no-op. | 
| 557 | 1.16k |     if (!_thread->_joinable) { | 
| 558 | 1 |         return Status::OK(); | 
| 559 | 1 |     } | 
| 560 |  |  | 
| 561 | 1.16k |     int waited_ms = 0; | 
| 562 | 1.16k |     bool keep_trying = true; | 
| 563 | 1.17k |     while (keep_trying) { | 
| 564 | 1.17k |         if (waited_ms >= _warn_after_ms) { | 
| 565 | 10 |             LOG(WARNING) << strings::Substitute("Waited for $0ms trying to join with $1 (tid $2)", | 
| 566 | 10 |                                                 waited_ms, _thread->_name, _thread->_tid); | 
| 567 | 10 |         } | 
| 568 |  |  | 
| 569 | 1.17k |         int remaining_before_giveup = std::numeric_limits<int>::max(); | 
| 570 | 1.17k |         if (_give_up_after_ms != -1) { | 
| 571 | 1 |             remaining_before_giveup = _give_up_after_ms - waited_ms; | 
| 572 | 1 |         } | 
| 573 |  |  | 
| 574 | 1.17k |         int remaining_before_next_warn = _warn_every_ms; | 
| 575 | 1.17k |         if (waited_ms < _warn_after_ms) { | 
| 576 | 1.16k |             remaining_before_next_warn = _warn_after_ms - waited_ms; | 
| 577 | 1.16k |         } | 
| 578 |  |  | 
| 579 | 1.17k |         if (remaining_before_giveup < remaining_before_next_warn) { | 
| 580 | 1 |             keep_trying = false; | 
| 581 | 1 |         } | 
| 582 |  |  | 
| 583 | 1.17k |         int wait_for = std::min(remaining_before_giveup, remaining_before_next_warn); | 
| 584 |  |  | 
| 585 | 1.17k |         if (_thread->_done.wait_for(std::chrono::milliseconds(wait_for))) { | 
| 586 |  |             // Unconditionally join before returning, to guarantee that any TLS | 
| 587 |  |             // has been destroyed (pthread_key_create() destructors only run | 
| 588 |  |             // after a pthread's user method has returned). | 
| 589 | 1.16k |             int ret = pthread_join(_thread->_thread, nullptr); | 
| 590 | 1.16k |             CHECK_EQ(ret, 0); | 
| 591 | 1.16k |             _thread->_joinable = false; | 
| 592 | 1.16k |             return Status::OK(); | 
| 593 | 1.16k |         } | 
| 594 | 11 |         waited_ms += wait_for; | 
| 595 | 11 |     } | 
| 596 | 1 |     return Status::Aborted("Timed out after {}ms joining on {}", waited_ms, _thread->_name); | 
| 597 | 1.16k | } | 
| 598 |  |  | 
| 599 | 8 | void register_thread_display_page(WebPageHandler* web_page_handler) { | 
| 600 | 8 |     web_page_handler->register_template_page( | 
| 601 | 8 |             "/threadz", "Threads", | 
| 602 | 8 |             std::bind(&ThreadMgr::display_thread_callback, thread_manager.get(), | 
| 603 | 8 |                       std::placeholders::_1, std::placeholders::_2), | 
| 604 | 8 |             true); | 
| 605 | 8 | } | 
| 606 |  | } // namespace doris |