xref: /aosp_15_r20/external/perfetto/src/base/thread_utils.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker /*
2*6dbdd20aSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*6dbdd20aSAndroid Build Coastguard Worker  *
4*6dbdd20aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*6dbdd20aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*6dbdd20aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*6dbdd20aSAndroid Build Coastguard Worker  *
8*6dbdd20aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*6dbdd20aSAndroid Build Coastguard Worker  *
10*6dbdd20aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*6dbdd20aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*6dbdd20aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*6dbdd20aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*6dbdd20aSAndroid Build Coastguard Worker  * limitations under the License.
15*6dbdd20aSAndroid Build Coastguard Worker  */
16*6dbdd20aSAndroid Build Coastguard Worker 
17*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/base/thread_utils.h"
18*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/ext/base/thread_utils.h"
19*6dbdd20aSAndroid Build Coastguard Worker 
20*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/base/build_config.h"
21*6dbdd20aSAndroid Build Coastguard Worker 
22*6dbdd20aSAndroid Build Coastguard Worker #if PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
23*6dbdd20aSAndroid Build Coastguard Worker #include <zircon/process.h>
24*6dbdd20aSAndroid Build Coastguard Worker #include <zircon/syscalls.h>
25*6dbdd20aSAndroid Build Coastguard Worker #include <zircon/types.h>
26*6dbdd20aSAndroid Build Coastguard Worker #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
27*6dbdd20aSAndroid Build Coastguard Worker #include <Windows.h>
28*6dbdd20aSAndroid Build Coastguard Worker #endif
29*6dbdd20aSAndroid Build Coastguard Worker 
30*6dbdd20aSAndroid Build Coastguard Worker namespace perfetto {
31*6dbdd20aSAndroid Build Coastguard Worker namespace base {
32*6dbdd20aSAndroid Build Coastguard Worker 
33*6dbdd20aSAndroid Build Coastguard Worker #if PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
ResolveThreadId()34*6dbdd20aSAndroid Build Coastguard Worker static PlatformThreadId ResolveThreadId() {
35*6dbdd20aSAndroid Build Coastguard Worker   zx_info_handle_basic_t basic;
36*6dbdd20aSAndroid Build Coastguard Worker   return (zx_object_get_info(zx_thread_self(), ZX_INFO_HANDLE_BASIC, &basic,
37*6dbdd20aSAndroid Build Coastguard Worker                              sizeof(basic), nullptr, nullptr) == ZX_OK)
38*6dbdd20aSAndroid Build Coastguard Worker              ? basic.koid
39*6dbdd20aSAndroid Build Coastguard Worker              : ZX_KOID_INVALID;
40*6dbdd20aSAndroid Build Coastguard Worker }
GetThreadId()41*6dbdd20aSAndroid Build Coastguard Worker PlatformThreadId GetThreadId() {
42*6dbdd20aSAndroid Build Coastguard Worker   thread_local static PlatformThreadId thread_id = ResolveThreadId();
43*6dbdd20aSAndroid Build Coastguard Worker   return thread_id;
44*6dbdd20aSAndroid Build Coastguard Worker }
45*6dbdd20aSAndroid Build Coastguard Worker 
46*6dbdd20aSAndroid Build Coastguard Worker #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
47*6dbdd20aSAndroid Build Coastguard Worker 
48*6dbdd20aSAndroid Build Coastguard Worker // The SetThreadDescription API was brought in version 1607 of Windows 10.
49*6dbdd20aSAndroid Build Coastguard Worker typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread,
50*6dbdd20aSAndroid Build Coastguard Worker                                               PCWSTR lpThreadDescription);
51*6dbdd20aSAndroid Build Coastguard Worker 
52*6dbdd20aSAndroid Build Coastguard Worker // The SetThreadDescription API was brought in version 1607 of Windows 10.
53*6dbdd20aSAndroid Build Coastguard Worker typedef HRESULT(WINAPI* GetThreadDescription)(HANDLE hThread,
54*6dbdd20aSAndroid Build Coastguard Worker                                               PWSTR* ppszThreadDescription);
55*6dbdd20aSAndroid Build Coastguard Worker 
56*6dbdd20aSAndroid Build Coastguard Worker bool MaybeSetThreadName(const std::string& name) {
57*6dbdd20aSAndroid Build Coastguard Worker   // The SetThreadDescription API works even if no debugger is attached.
58*6dbdd20aSAndroid Build Coastguard Worker   static auto set_thread_description_func =
59*6dbdd20aSAndroid Build Coastguard Worker       reinterpret_cast<SetThreadDescription>(
60*6dbdd20aSAndroid Build Coastguard Worker           reinterpret_cast<void*>(::GetProcAddress(
61*6dbdd20aSAndroid Build Coastguard Worker               ::GetModuleHandleA("Kernel32.dll"), "SetThreadDescription")));
62*6dbdd20aSAndroid Build Coastguard Worker   if (!set_thread_description_func) {
63*6dbdd20aSAndroid Build Coastguard Worker     return false;
64*6dbdd20aSAndroid Build Coastguard Worker   }
65*6dbdd20aSAndroid Build Coastguard Worker   std::wstring wide_thread_name;
66*6dbdd20aSAndroid Build Coastguard Worker   if (!UTF8ToWide(name, wide_thread_name)) {
67*6dbdd20aSAndroid Build Coastguard Worker     return false;
68*6dbdd20aSAndroid Build Coastguard Worker   }
69*6dbdd20aSAndroid Build Coastguard Worker   HRESULT result = set_thread_description_func(::GetCurrentThread(),
70*6dbdd20aSAndroid Build Coastguard Worker                                                wide_thread_name.c_str());
71*6dbdd20aSAndroid Build Coastguard Worker   return !FAILED(result);
72*6dbdd20aSAndroid Build Coastguard Worker }
73*6dbdd20aSAndroid Build Coastguard Worker 
74*6dbdd20aSAndroid Build Coastguard Worker bool GetThreadName(std::string& out_result) {
75*6dbdd20aSAndroid Build Coastguard Worker   static auto get_thread_description_func =
76*6dbdd20aSAndroid Build Coastguard Worker       reinterpret_cast<GetThreadDescription>(
77*6dbdd20aSAndroid Build Coastguard Worker           reinterpret_cast<void*>(::GetProcAddress(
78*6dbdd20aSAndroid Build Coastguard Worker               ::GetModuleHandleA("Kernel32.dll"), "GetThreadDescription")));
79*6dbdd20aSAndroid Build Coastguard Worker   if (!get_thread_description_func) {
80*6dbdd20aSAndroid Build Coastguard Worker     return false;
81*6dbdd20aSAndroid Build Coastguard Worker   }
82*6dbdd20aSAndroid Build Coastguard Worker   wchar_t* wide_thread_name;
83*6dbdd20aSAndroid Build Coastguard Worker   HRESULT result =
84*6dbdd20aSAndroid Build Coastguard Worker       get_thread_description_func(::GetCurrentThread(), &wide_thread_name);
85*6dbdd20aSAndroid Build Coastguard Worker   if (SUCCEEDED(result)) {
86*6dbdd20aSAndroid Build Coastguard Worker     bool success = WideToUTF8(std::wstring(wide_thread_name), out_result);
87*6dbdd20aSAndroid Build Coastguard Worker     LocalFree(wide_thread_name);
88*6dbdd20aSAndroid Build Coastguard Worker     return success;
89*6dbdd20aSAndroid Build Coastguard Worker   }
90*6dbdd20aSAndroid Build Coastguard Worker   return false;
91*6dbdd20aSAndroid Build Coastguard Worker }
92*6dbdd20aSAndroid Build Coastguard Worker 
93*6dbdd20aSAndroid Build Coastguard Worker #endif  // PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
94*6dbdd20aSAndroid Build Coastguard Worker 
95*6dbdd20aSAndroid Build Coastguard Worker }  // namespace base
96*6dbdd20aSAndroid Build Coastguard Worker }  // namespace perfetto
97