Skip to content

Commit 9d70ff3

Browse files
committed
Add thread names
1 parent 99765af commit 9d70ff3

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

Source/astcenccli_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ void launch_threads(
397397
void (*func)(int, int, void*),
398398
void *payload);
399399

400+
/**
401+
* @brief Set the current thread name to a string value
402+
*
403+
* For portability strings should be less than 16 characters.
404+
*
405+
* @param name The thread name.
406+
*/
407+
void set_thread_name(
408+
const char* name);
409+
400410
/**
401411
* @brief The main entry point.
402412
*

Source/astcenccli_platform_dependents.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// ----------------------------------------------------------------------------
3-
// Copyright 2011-2023 Arm Limited
3+
// Copyright 2011-2024 Arm Limited
44
//
55
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
66
// use this file except in compliance with the License. You may obtain a copy
@@ -39,6 +39,9 @@
3939

4040
#define WIN32_LEAN_AND_MEAN
4141
#include <windows.h>
42+
#include <Processthreadsapi.h>
43+
#include <algorithm>
44+
#include <cstring>
4245

4346
/** @brief Alias pthread_t to one of the internal Windows types. */
4447
typedef HANDLE pthread_t;
@@ -58,7 +61,7 @@ static int pthread_create(
5861
static_cast<void>(attribs);
5962
LPTHREAD_START_ROUTINE func = reinterpret_cast<LPTHREAD_START_ROUTINE>(threadfunc);
6063
*thread = CreateThread(nullptr, 0, func, thread_arg, 0, nullptr);
61-
64+
6265
// Ensure we return 0 on success, non-zero on error
6366
if (*thread == NULL)
6467
{
@@ -142,6 +145,24 @@ double get_time()
142145
return static_cast<double>(ticks) / 1.0e7;
143146
}
144147

148+
/* See header for documentation */
149+
void set_thread_name(
150+
const char* name
151+
) {
152+
// Names are limited to 16 characters
153+
wchar_t wname [17] { 0 };
154+
size_t name_len = std::strlen(name);
155+
size_t clamp_len = std::min(name_len, 16);
156+
157+
// We know we only have basic 7-bit ASCII so just widen
158+
for (size_t i = 0; i < clamp_len; i++)
159+
{
160+
wname = static_cast<wchar_t>(name[i]);
161+
}
162+
163+
SetThreadDescription(GetCurrentThread(), wname);
164+
}
165+
145166
/* ============================================================================
146167
Platform code for an platform using POSIX APIs.
147168
============================================================================ */
@@ -165,6 +186,13 @@ double get_time()
165186
return static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1.0e-6;
166187
}
167188

189+
/* See header for documentation */
190+
void set_thread_name(
191+
const char* name
192+
) {
193+
pthread_setname_np(pthread_self(), name);
194+
}
195+
168196
#endif
169197

170198
/**
@@ -215,9 +243,9 @@ void launch_threads(
215243
}
216244

217245
// Otherwise spawn worker threads
218-
launch_desc *thread_descs = new launch_desc[thread_count];
246+
launch_desc *thread_descs = new launch_desc[thread_count];
219247
int actual_thread_count { 0 };
220-
248+
221249
for (int i = 0; i < thread_count; i++)
222250
{
223251
thread_descs[actual_thread_count].thread_count = thread_count;
@@ -230,7 +258,7 @@ void launch_threads(
230258
&(thread_descs[actual_thread_count].thread_handle),
231259
nullptr,
232260
launch_threads_helper,
233-
reinterpret_cast<void*>(thread_descs + actual_thread_count));
261+
reinterpret_cast<void*>(thread_descs + actual_thread_count));
234262

235263
// Track how many threads we actually created
236264
if (!error)
@@ -248,7 +276,7 @@ void launch_threads(
248276

249277
// If we did not create thread_count threads then emit a warning
250278
if (actual_thread_count != thread_count)
251-
{
279+
{
252280
int log_count = actual_thread_count == 0 ? 1 : actual_thread_count;
253281
const char* log_s = log_count == 1 ? "" : "s";
254282
printf("WARNING: %s using %d thread%s due to thread creation error\n\n",

Source/astcenccli_toplevel.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ static void compression_workload_runner(
232232
) {
233233
(void)thread_count;
234234

235+
char name[17] { 0 };
236+
std::snprintf(name, 16, "astc workc %d", thread_id);
237+
set_thread_name(name);
238+
235239
compression_workload* work = static_cast<compression_workload*>(payload);
236240
astcenc_error error = astcenc_compress_image(
237241
work->context, work->image, &work->swizzle,
@@ -259,6 +263,10 @@ static void decompression_workload_runner(
259263
) {
260264
(void)thread_count;
261265

266+
char name[17] { 0 };
267+
std::snprintf(name, 16, "astc workd %d", thread_id);
268+
set_thread_name(name);
269+
262270
decompression_workload* work = static_cast<decompression_workload*>(payload);
263271
astcenc_error error = astcenc_decompress_image(
264272
work->context, work->data, work->data_len,
@@ -1881,10 +1889,13 @@ static void print_diagnostic_images(
18811889
*
18821890
* @return 0 on success, non-zero otherwise.
18831891
*/
1884-
int astcenc_main(
1892+
int
1893+
astcenc_main(
18851894
int argc,
18861895
char **argv
18871896
) {
1897+
set_thread_name("astc main");
1898+
18881899
#if ASTCENC_SVE != 0
18891900
// Do this check here because is needs SVE instructions so cannot be in
18901901
// the veneer check which is compiled as stock Armv8. We know we have SVE

0 commit comments

Comments
 (0)