Skip to content

Commit

Permalink
Merge pull request #1102 from fragcolor-xyz/guus/daily
Browse files Browse the repository at this point in the history
Add constant native os string for thread names
  • Loading branch information
guusw authored Jan 19, 2025
2 parents 4d442ba + 829ee60 commit 1051621
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ jobs:
genhtml \
coverage/coverage.linux.info \
coverage/coverage-macos-gpu.info \
--ignore-errors source \
--ignore-errors inconsistent,source,unmapped \
--synthesize-missing \
--output-directory coverage/output
- name: Upload report
if: ${{ steps.setup.outputs.html-report == 'true' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-macos-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ jobs:
--output-file coverage/coverage-macos-gpu.info \
--ignore-errors inconsistent,gcov,range
# convert absolute path to relative path
sed -e "s|${PWD}/|./|g" coverage/coverage-macos-gpu.info
sed -i '' -e "s#${PWD}/#./#g" coverage/coverage-macos-gpu.info
- name: Upload coverage (Debug)
if: ${{ steps.setup.outputs.build-type == 'Debug' }}
uses: actions/upload-artifact@v3
Expand Down
6 changes: 3 additions & 3 deletions include/shards/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ template <auto V> struct constant {
constexpr static decltype(V) value = V;
};

inline SHOptionalString operator"" _optional(const char *s, size_t) { return SHOptionalString{s}; }
inline SHStringWithLen operator"" _swl(const char *s, size_t l) { return SHStringWithLen{s, l}; }
inline std::string_view operator"" _sv(const char *s, size_t l) { return std::string_view{s, l}; }
inline SHOptionalString operator""_optional(const char *s, size_t) { return SHOptionalString{s}; }
inline SHStringWithLen operator""_swl(const char *s, size_t l) { return SHStringWithLen{s, l}; }
inline std::string_view operator""_sv(const char *s, size_t l) { return std::string_view{s, l}; }

constexpr std::size_t StrLen(const char *str) {
std::size_t len = 0;
Expand Down
7 changes: 5 additions & 2 deletions shards/core/async.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ struct TidePool {
Worker(boost::lockfree::queue<Work *> &queue, std::atomic_size_t &counter, std::mutex &condMutex,
std::condition_variable &cond)
: _queue(queue), _counter(counter), _condMutex(condMutex), _cond(cond) {
using namespace shards::literals;
_running = true;
boost::thread::attributes attrs;
attrs.set_stack_size(SH_STACK_SIZE);
_thread = boost::thread(attrs, [this]() {
pushThreadName("TidePool worker");
pushThreadName("TidePool worker"_ns);
while (_running) {
Work *work{};
if (_queue.pop(work)) {
Expand Down Expand Up @@ -142,7 +143,9 @@ struct TidePool {
}

void controllerWorker() {
pushThreadName("TidePool controller");
using namespace shards::literals;

pushThreadName("TidePool controller"_ns);

// spawn workers first
for (size_t i = 0; i < NumWorkers; ++i) {
Expand Down
17 changes: 9 additions & 8 deletions shards/core/foundation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "type_matcher.hpp"
#include "type_info.hpp"
#include "trait.hpp"
#include "utils.hpp"
#include <shards/fast_string/fast_string.hpp>

#include <algorithm>
Expand Down Expand Up @@ -515,20 +516,20 @@ struct SHWire : public std::enable_shared_from_this<SHWire> {

#if SH_DEBUG_THREAD_NAMES
struct ThreadNameStrings {
ThreadNameStrings& init(SHWire *wire) {
ThreadNameStrings &init(SHWire *wire) {
if (!initialized) {
resumeStr = fmt::format("Wire \"{}\"", (wire)->name);
extResumeStr = fmt::format("<resuming wire> \"{}\"", (wire)->name);
suspendedStr= fmt::format("<suspended wire> \"{}\"", wire->name);
resumeStr = shards::NativeString{fmt::format("Wire \"{}\"", (wire)->name)};
extResumeStr = shards::NativeString{fmt::format("<resuming wire> \"{}\"", (wire)->name)};

suspendedStr = shards::NativeString{fmt::format("<suspended wire> \"{}\"", wire->name)};
initialized = true;
}
return *this;
}
bool initialized = false;
std::string resumeStr;
std::string extResumeStr;
std::string suspendedStr;
shards::NativeString resumeStr;
shards::NativeString extResumeStr;
shards::NativeString suspendedStr;
} threadNameStrings;
#endif
};
Expand Down
2 changes: 1 addition & 1 deletion shards/core/taskflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace shards {
struct TaskFlowDebugInterface : tf::WorkerInterface {
std::string debugName;
#if SH_DEBUG_THREAD_NAMES
std::list<std::string> debugThreadNameStack;
std::list<NativeString> debugThreadNameStack;
#endif

TaskFlowDebugInterface(std::string debugName) : debugName(debugName) {}
Expand Down
6 changes: 3 additions & 3 deletions shards/core/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "utils.hpp"
namespace shards {
thread_local std::list<const char*> *_debugThreadStack;
std::list<const char*> &getThreadNameStack() {
thread_local std::list<const char*> stack;
thread_local std::list<NativeStrViewType> *_debugThreadStack;
std::list<NativeStrViewType> &getThreadNameStack() {
thread_local std::list<NativeStrViewType> stack;
return stack;
}
} // namespace shards
93 changes: 76 additions & 17 deletions shards/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

namespace shards {

template <auto N> struct conststr {
char value[N];
constexpr conststr(const char (&str)[N]) { std::copy_n(str, N, value); }
};

#if SH_WINDOWS
inline std::wstring toWindowsWString(std::string_view utf8) {
std::wstring result;
Expand All @@ -34,40 +39,93 @@ inline std::wstring toWindowsWString(std::string_view utf8) {
}
#endif

inline void setThreadName(std::string_view name_sv) {
struct NativeString {
#if SH_WINDOWS
NativeString() = default;
NativeString(std::string_view name) : name(toWindowsWString(name)) {}
NativeString &operator=(const std::string_view &name) {
this->name = toWindowsWString(name);
return *this;
}
~NativeString() {}
std::wstring name;
using value_type = std::wstring;
#else
NativeString() = default;
NativeString(std::string_view name) : name(name) {}
NativeString &operator=(const std::string_view &name) {
this->name = name;
return *this;
}
~NativeString() {}
std::string name;
using value_type = std::string;
#endif
};
#if SH_WINDOWS
std::wstring name = toWindowsWString(name_sv);
SetThreadDescription(GetCurrentThread(), name.c_str());
using NativeStrType = std::wstring;
using NativeStrViewType = std::wstring_view;
#else
using NativeStrType = std::string;
using NativeStrViewType = std::string_view;
#endif
using NativeCStrType = const NativeStrType::value_type *;

namespace detail {
template <conststr Name> struct ConstNativeStringHolder {
static inline NativeString v;
static const auto &get() {
if (v.name.empty()) {
v = Name.value;
}
return v.name;
}
};
struct ConstNativeStringValue {
const NativeStrType &name;
};
} // namespace detail

namespace literals {
template <conststr cts> constexpr auto operator""_ns() {
return detail::ConstNativeStringValue{detail::ConstNativeStringHolder<cts>::get()};
}
} // namespace literals

// NOTE: Should be null terminated
inline void setThreadName(NativeCStrType v) {
#if SH_WINDOWS
SetThreadDescription(GetCurrentThread(), v);
#elif SH_LINUX
std::string name{name_sv};
pthread_setname_np(pthread_self(), name.c_str());
pthread_setname_np(pthread_self(), v);
#elif SH_APPLE
std::string name{name_sv};
pthread_setname_np(name.c_str());
pthread_setname_np(v);
#endif
}

std::list<const char *> &getThreadNameStack();
std::list<NativeStrViewType> &getThreadNameStack();

#if SH_DEBUG_THREAD_NAMES
inline void pushThreadNameConst(std::string_view name) {
// Use _ns suffix after constant
inline void pushThreadName(const detail::ConstNativeStringValue &v) {
auto &stack = getThreadNameStack();
stack.emplace_back(name.data());
setThreadName(name);
stack.emplace_back(v.name);
setThreadName(v.name.c_str());
}

// NOTE: by reference, since you should keep the string alive for the duration of the thread
inline void pushThreadName(std::string &name) { pushThreadNameConst(name); }

template <size_t N> inline void pushThreadName(const char (&str)[N]) { pushThreadNameConst(std::string_view(str, N)); }
inline void pushThreadName(NativeString &str) {
auto &stack = getThreadNameStack();
stack.emplace_back(str.name);
setThreadName(str.name.c_str());
}
#else
template <typename T> inline void pushThreadName(const T &v) {}
#endif

#if SH_DEBUG_THREAD_NAMES
// You can add this to the debugger watch window (shards::_debugThreadStack)
// to see the current thread stack
extern thread_local std::list<const char *> *_debugThreadStack;
extern thread_local std::list<NativeStrViewType> *_debugThreadStack;
#endif

inline void popThreadName() {
Expand All @@ -76,7 +134,8 @@ inline void popThreadName() {
_debugThreadStack = &stack;
shassert(stack.size() > 0);
stack.pop_back();
setThreadName(stack.size() > 0 ? stack.back() : "Unnamed thread");
static NativeString unnamed{"Unnamed thread"};
setThreadName(stack.size() > 0 ? stack.back().data() : unnamed.name.data());
#endif
}
} // namespace shards
Expand Down
12 changes: 8 additions & 4 deletions shards/gfx/geom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void SphereGenerator::generate() {
// normal
vertex.setNormal(linalg::normalize(position));

vertex.setTexCoord(float2(1.0 - (u + uOffset), v));
vertex.setTexCoord(float2(u + uOffset, v));

verticesRow.push_back(index++);
}
Expand Down Expand Up @@ -131,7 +131,11 @@ void PlaneGenerator::generate() {
VertexPNT &vertex = vertices.emplace_back();
vertex.setPosition(float3(x, -y, 0));
vertex.setNormal(float3(0, 0, 1));
vertex.setTexCoord(float2(float(ix) / gridX, 1 - (float(iy) / gridY)));
if(flipTextureVertically) {
vertex.setTexCoord(float2(float(ix) / gridX, 1 - float(iy) / gridY));
} else {
vertex.setTexCoord(float2(float(ix) / gridX, float(iy) / gridY));
}
}
}

Expand Down Expand Up @@ -194,7 +198,7 @@ void CubeGenerator::generate() {

float2 uv;
uv.x = (ix / gridX);
uv.y = (1 - (iy / gridY));
uv.y = (iy / gridY);
vertex.setTexCoord(uv);
}
}
Expand Down Expand Up @@ -341,7 +345,7 @@ void CylinderGenerator::generate() {
float3 normal = linalg::normalize(float3(sinTheta, slope, cosTheta));
vertex.setNormal(normal);

vertex.setTexCoord(float2(u, 1 - v));
vertex.setTexCoord(float2(u, v));

indexRow.emplace_back(index++);
}
Expand Down
3 changes: 3 additions & 0 deletions shards/gfx/geom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ struct PlaneGenerator : public GeneratorBase {
float height = 1;
size_t widthSegments = 1;
size_t heightSegments = 1;
// Flip to match UI space (where Y is down, X is right)
// otherwise it's Y up, X right
bool flipTextureVertically = false;

void generate();
};
Expand Down
2 changes: 1 addition & 1 deletion shards/gfx/tests/renderer_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline MeshPtr createSphereMesh() {
}

inline MeshPtr createPlaneMesh() {
geom::PlaneGenerator gen;
geom::PlaneGenerator gen{.flipTextureVertically = true};
gen.generate();
return createMesh(gen.vertices, gen.indices);
}
Expand Down
7 changes: 5 additions & 2 deletions shards/mal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ function(setup_shards_target TARGET)
if(NOT EMSCRIPTEN)
target_link_libraries(${TARGET} Boost::process)
endif()

if(APPLE)
target_link_libraries(${TARGET} replxx shards-core shards_core_swift)
else()
target_link_libraries(${TARGET} replxx shards-core)
endif()

set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE CXX)

# Need the lang-ffi bindings
Expand Down Expand Up @@ -64,7 +66,7 @@ endif()
if(APPLE)
add_library(shards-framework SHARED ${shards_SOURCES})
setup_shards_target(shards-framework)

# Framework specific settings
set_target_properties(shards-framework PROPERTIES
FRAMEWORK TRUE
Expand All @@ -77,6 +79,7 @@ if(APPLE)
XCODE_ATTRIBUTE_SWIFT_VERSION "5.0"
XCODE_ATTRIBUTE_SWIFT_INSTALL_OBJC_HEADER "NO"
OUTPUT_NAME "Shards"

# Debug symbols generation
XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf-with-dsym"
XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS "YES"
Expand All @@ -93,7 +96,7 @@ if(APPLE)
INSTALL_RPATH "@executable_path/../Frameworks"
BUILD_WITH_INSTALL_RPATH TRUE
)

# Link Swift runtime for MacOS
target_link_libraries(shards-framework
"-framework Foundation"
Expand Down
4 changes: 2 additions & 2 deletions shards/mal/stepA_mal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ int malmain(int argc, const char *argv[]) {
}

#ifndef NO_MAL_MAIN

using namespace shards::literals;
int main(int argc, const char *argv[]) {
shards::parseArguments(argc, argv);

shards::pushThreadName("Main Thread");
shards::pushThreadName("Main Thread"_ns);

auto result = shards_process_args(argc, const_cast<char **>(argv), false);
if (result != 99) // 99 triggers our old main
Expand Down
6 changes: 3 additions & 3 deletions shards/modules/core/wires.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,9 +1590,9 @@ struct ParallelBase : public CapturingSpawners {
cref->mesh = mesh;

#if SH_DEBUG_THREAD_NAMES
static thread_local std::string debugThreadName;
debugThreadName.clear();
fmt::format_to(std::back_inserter(debugThreadName), "tf::Executor \"{}\" ({} idx: {})", cref->wire->name, context->currentWire()->name, idx);
static thread_local shards::NativeString debugThreadName;
debugThreadName.name.clear();
fmt::format_to(std::back_inserter(debugThreadName.name), "tf::Executor \"{}\" ({} idx: {})", cref->wire->name, context->currentWire()->name, idx);
pushThreadName(debugThreadName);
DEFER({ popThreadName(); });
#endif
Expand Down
Loading

0 comments on commit 1051621

Please sign in to comment.