Skip to content

Commit 748fd32

Browse files
Add missing thread names (#1534)
Extract Thread helper into separate file, and use it to name spawned threads. Relates-To: HERESDK-3466 Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com>
1 parent aff34d2 commit 748fd32

File tree

6 files changed

+106
-21
lines changed

6 files changed

+106
-21
lines changed

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ set(OLP_SDK_UTILS_HEADERS
142142
./include/olp/core/utils/Credentials.h
143143
./include/olp/core/utils/Dir.h
144144
./include/olp/core/utils/LruCache.h
145+
./include/olp/core/utils/Thread.h
145146
./include/olp/core/utils/Url.h
146147
./include/olp/core/utils/WarningWorkarounds.h
147148
)
@@ -351,6 +352,7 @@ set(OLP_SDK_UTILS_SOURCES
351352
./src/utils/BoostExceptionHandle.cpp
352353
./src/utils/Credentials.cpp
353354
./src/utils/Dir.cpp
355+
./src/utils/Thread.cpp
354356
./src/utils/Url.cpp
355357
)
356358

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2024 HERE Europe B.V.
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+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
#include <olp/core/CoreApi.h>
22+
#include <string>
23+
24+
namespace olp {
25+
namespace utils {
26+
27+
/** @brief Manages threads.
28+
*/
29+
class CORE_API Thread {
30+
public:
31+
/**
32+
* @brief Set the name of current thread.
33+
*
34+
* @param name The new name of thread.
35+
*
36+
*/
37+
static void SetCurrentThreadName(const std::string& name);
38+
};
39+
40+
} // namespace utils
41+
} // namespace olp

olp-cpp-sdk-core/src/cache/DiskCache.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
#include "olp/core/logging/Log.h"
3737
#include "olp/core/porting/make_unique.h"
3838
#include "olp/core/utils/Dir.h"
39+
#include "olp/core/utils/Thread.h"
3940

4041
namespace olp {
4142
namespace cache {
4243

4344
namespace {
45+
constexpr auto kThreadNameCompactDb = "CompactDB";
4446
constexpr auto kLogTag = "DiskCache";
4547
constexpr auto kLevelDbLostFolder = "lost";
4648
constexpr auto kMaxL0Files = 4;
@@ -432,6 +434,7 @@ DiskCache::OperationOutcome<> DiskCache::ApplyBatch(
432434
}
433435

434436
compaction_thread_ = std::thread([this]() {
437+
olp::utils::Thread::SetCurrentThreadName(kThreadNameCompactDb);
435438
OLP_SDK_LOG_INFO(kLogTag, "Compacting database started");
436439
database_->CompactRange(nullptr, nullptr);
437440
compacting_ = false;

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cstring>
2828
#include <memory>
2929
#include <utility>
30+
#include "olp/core/utils/Thread.h"
3031

3132
#if defined(HAVE_SIGNAL_H)
3233
#include <signal.h>
@@ -53,6 +54,7 @@ namespace http {
5354
namespace {
5455

5556
const char* kLogTag = "CURL";
57+
const char* kCurlThreadName = "OLPSDKCURL";
5658

5759
#if defined(OLP_SDK_ENABLE_ANDROID_CURL) && !defined(ANDROID_HOST)
5860
const auto kCurlAndroidCaBundleFolder = "/system/etc/security/cacerts";
@@ -1095,6 +1097,8 @@ int NetworkCurl::GetHandleIndex(CURL* handle) {
10951097
}
10961098

10971099
void NetworkCurl::Run() {
1100+
olp::utils::Thread::SetCurrentThreadName(kCurlThreadName);
1101+
10981102
{
10991103
std::lock_guard<std::mutex> lock(event_mutex_);
11001104
state_ = WorkerState::STARTED;

olp-cpp-sdk-core/src/thread/ThreadPoolTaskScheduler.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
* License-Filename: LICENSE
1818
*/
19-
2019
#include "olp/core/thread/ThreadPoolTaskScheduler.h"
20+
#include "olp/core/utils/Thread.h"
2121

2222
#if defined(PORTING_PLATFORM_QNX)
2323
#include <process.h>
@@ -35,7 +35,6 @@
3535
#include "olp/core/logging/LogContext.h"
3636
#include "olp/core/porting/platform.h"
3737
#include "olp/core/thread/SyncQueue.h"
38-
#include "olp/core/utils/WarningWorkarounds.h"
3938
#include "thread/PriorityQueueExtended.h"
4039

4140
namespace olp {
@@ -44,24 +43,6 @@ namespace thread {
4443
namespace {
4544
constexpr auto kLogTag = "ThreadPoolTaskScheduler";
4645

47-
void SetCurrentThreadName(const std::string& thread_name) {
48-
// Currently only supported for pthread users
49-
OLP_SDK_CORE_UNUSED(thread_name);
50-
51-
#if defined(PORTING_PLATFORM_MAC)
52-
// Note that in Mac based systems the pthread_setname_np takes 1 argument
53-
// only.
54-
pthread_setname_np(thread_name.c_str());
55-
#elif defined(OLP_SDK_HAVE_PTHREAD_SETNAME_NP) // Linux, Android, QNX
56-
// QNX allows 100 but Linux only 16 so select min value and apply for both.
57-
// If maximum length is exceeded on some systems, e.g. Linux, the name is not
58-
// set at all. So better truncate it to have at least the minimum set.
59-
constexpr size_t kMaxThreadNameLength = 16u;
60-
std::string truncated_name = thread_name.substr(0, kMaxThreadNameLength - 1);
61-
pthread_setname_np(pthread_self(), truncated_name.c_str());
62-
#endif // OLP_SDK_HAVE_PTHREAD_SETNAME_NP
63-
}
64-
6546
struct PrioritizedTask {
6647
TaskScheduler::CallFuncType function;
6748
uint32_t priority;
@@ -76,7 +57,7 @@ struct ComparePrioritizedTask {
7657

7758
void SetExecutorName(size_t idx) {
7859
std::string thread_name = "OLPSDKPOOL_" + std::to_string(idx);
79-
SetCurrentThreadName(thread_name);
60+
olp::utils::Thread::SetCurrentThreadName(thread_name);
8061
OLP_SDK_LOG_INFO_F(kLogTag, "Starting thread '%s'", thread_name.c_str());
8162
}
8263

olp-cpp-sdk-core/src/utils/Thread.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2024 HERE Europe B.V.
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+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include "olp/core/utils/Thread.h"
21+
#include "olp/core/porting/platform.h"
22+
#include "olp/core/utils/WarningWorkarounds.h"
23+
#if defined(PORTING_PLATFORM_MAC)
24+
#include <pthread.h>
25+
#elif defined(PORTING_PLATFORM_ANDROID) || defined(PORTING_PLATFORM_LINUX) || \
26+
defined(PORTING_PLATFORM_QNX)
27+
#include <pthread.h>
28+
#endif
29+
30+
#include <cstring>
31+
namespace olp {
32+
namespace utils {
33+
34+
void Thread::SetCurrentThreadName(const std::string& thread_name) {
35+
// Currently only supported for pthread users
36+
OLP_SDK_CORE_UNUSED(thread_name);
37+
38+
#if defined(PORTING_PLATFORM_MAC)
39+
// Note that in Mac based systems the pthread_setname_np takes 1 argument
40+
// only.
41+
pthread_setname_np(thread_name.c_str());
42+
#elif defined(PORTING_PLATFORM_ANDROID) || defined(PORTING_PLATFORM_LINUX) || \
43+
defined(PORTING_PLATFORM_QNX)
44+
// QNX allows 100 but Linux only 16 so select min value and apply for both.
45+
// If maximum length is exceeded on some systems, e.g. Linux, the name is not
46+
// set at all. So better truncate it to have at least the minimum set.
47+
constexpr size_t kMaxThreadNameLength = 16u;
48+
std::string truncated_name = thread_name.substr(0, kMaxThreadNameLength - 1);
49+
pthread_setname_np(pthread_self(), truncated_name.c_str());
50+
#endif
51+
}
52+
53+
} // namespace utils
54+
} // namespace olp

0 commit comments

Comments
 (0)