|
| 1 | +/* Copyright 2024, MariaDB. |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ |
| 15 | + |
| 16 | +#include <my_global.h> |
| 17 | +#include <my_pthread.h> |
| 18 | +#include <my_sys.h> |
| 19 | +#include "mysql/psi/psi.h" |
| 20 | +#include <stdio.h> |
| 21 | + |
| 22 | +#ifdef _WIN32 |
| 23 | +#define MAX_THREAD_NAME 256 |
| 24 | +typedef HRESULT (*func_SetThreadDescription)(HANDLE,PCWSTR); |
| 25 | +#elif defined(__linux__) |
| 26 | +#define MAX_THREAD_NAME 16 |
| 27 | +#elif defined(__FreeBSD__) || defined(__OpenBSD__) |
| 28 | +#include <pthread_np.h> |
| 29 | +#endif |
| 30 | + |
| 31 | +#if defined(HAVE_PSI_THREAD_INTERFACE) && !defined DBUG_OFF |
| 32 | +/** |
| 33 | + Check that the name is consistent with PSI. |
| 34 | + Require that the name matches the last part of PSI's class name |
| 35 | + (e.g. "thread/sql/main" -> "main"). |
| 36 | +
|
| 37 | + We drop the namespace prefix, because these thread names are |
| 38 | + truncated to 15 characters on Linux, and something like "innodb/" would |
| 39 | + already take up about half of that. |
| 40 | +*/ |
| 41 | +static void dbug_verify_thread_name(const char *name) |
| 42 | +{ |
| 43 | + const char *psi_name= NULL; |
| 44 | + const char *thread_class_name= PSI_THREAD_CALL(get_thread_class_name)(); |
| 45 | + if (thread_class_name) |
| 46 | + { |
| 47 | + /* Remove the path prefix */ |
| 48 | + const char *last_part= strrchr(thread_class_name, '/'); |
| 49 | + if (last_part) |
| 50 | + psi_name= last_part + 1; |
| 51 | + else |
| 52 | + psi_name= thread_class_name; |
| 53 | + } |
| 54 | + if (psi_name && strcmp(psi_name, name)) |
| 55 | + { |
| 56 | + fprintf(stderr, "my_thread_set_name() mismatch: PSI name '%s' != '%s'\n", |
| 57 | + psi_name, name); |
| 58 | + abort(); |
| 59 | + } |
| 60 | +} |
| 61 | +#else |
| 62 | +#define dbug_verify_thread_name(name) do {} while (0) |
| 63 | +#endif |
| 64 | + |
| 65 | +/* Set current thread name for debugger/profiler and similar tools. */ |
| 66 | +extern "C" void my_thread_set_name(const char *name) |
| 67 | +{ |
| 68 | + dbug_verify_thread_name(name); |
| 69 | + |
| 70 | +#ifdef _WIN32 |
| 71 | + /* |
| 72 | + SetThreadDescription might not be there on older Windows versions. |
| 73 | + Load it dynamically. |
| 74 | + */ |
| 75 | + static func_SetThreadDescription my_SetThreadDescription= |
| 76 | + (func_SetThreadDescription) GetProcAddress(GetModuleHandle("kernel32"), |
| 77 | + "SetThreadDescription"); |
| 78 | + if (!my_SetThreadDescription) |
| 79 | + return; |
| 80 | + wchar_t wname[MAX_THREAD_NAME]; |
| 81 | + wname[0]= 0; |
| 82 | + MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, MAX_THREAD_NAME); |
| 83 | + my_SetThreadDescription(GetCurrentThread(), wname); |
| 84 | +#elif defined __linux__ |
| 85 | + char shortname[MAX_THREAD_NAME]; |
| 86 | + snprintf(shortname, MAX_THREAD_NAME, "%s", name); |
| 87 | + pthread_setname_np(pthread_self(), shortname); |
| 88 | +#elif defined __NetBSD__ |
| 89 | + pthread_setname_np(pthread_self(), "%s", (void *) name); |
| 90 | +#elif defined __FreeBSD__ || defined __OpenBSD__ |
| 91 | + pthread_set_name_np(pthread_self(), name); |
| 92 | +#elif defined __APPLE__ |
| 93 | + pthread_setname_np(name); |
| 94 | +#else |
| 95 | + (void) name; |
| 96 | +#endif |
| 97 | +} |
0 commit comments