From 57fd59b73b27ead34f3dcce7c010778cca07d826 Mon Sep 17 00:00:00 2001 From: Benjamin Naecker Date: Wed, 28 Apr 2021 16:32:41 -0700 Subject: [PATCH] Adds support for building on Solaris-derived systems This commit adds a number of changes to platform-detection and compile-time directives required to support building ClickHouse on Solaris-derived systems, most notably illumos. --- base/common/getThreadId.cpp | 4 ++++ base/common/time.h | 2 +- base/pcg-random/pcg_random.hpp | 8 ++++---- cmake/target.cmake | 3 +++ programs/install/Install.cpp | 4 ++-- src/Common/Config/ConfigProcessor.cpp | 2 +- src/Common/Elf.cpp | 8 +++++++- src/Common/OpenTelemetryTraceContext.h | 5 +++++ src/Common/Stopwatch.cpp | 6 +++++- src/Common/TerminalSize.cpp | 5 ++++- src/Common/ThreadProfileEvents.h | 6 +++++- src/Common/checkStackSize.cpp | 2 +- src/Common/examples/int_hashes_perf.cpp | 2 +- src/Common/setThreadName.cpp | 6 ++++-- src/IO/BitHelpers.h | 2 ++ src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp | 3 +++ src/Storages/PostgreSQL/PostgreSQLPoolWithFailover.h | 1 + utils/CMakeLists.txt | 2 +- utils/memcpy-bench/CMakeLists.txt | 4 ++++ 19 files changed, 58 insertions(+), 17 deletions(-) diff --git a/base/common/getThreadId.cpp b/base/common/getThreadId.cpp index 700c51f21fc..054e9be9074 100644 --- a/base/common/getThreadId.cpp +++ b/base/common/getThreadId.cpp @@ -25,6 +25,10 @@ uint64_t getThreadId() current_tid = syscall(SYS_gettid); /// This call is always successful. - man gettid #elif defined(OS_FREEBSD) current_tid = pthread_getthreadid_np(); +#elif defined(OS_SUNOS) + // On Solaris-derived systems, this returns the ID of the LWP, analogous + // to a thread. + current_tid = static_cast(pthread_self()); #else if (0 != pthread_threadid_np(nullptr, ¤t_tid)) throw std::logic_error("pthread_threadid_np returned error"); diff --git a/base/common/time.h b/base/common/time.h index 1bf588b7cb3..d0b8e94a9a5 100644 --- a/base/common/time.h +++ b/base/common/time.h @@ -2,7 +2,7 @@ #include -#if defined (OS_DARWIN) +#if defined (OS_DARWIN) || defined (OS_SUNOS) # define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC #elif defined (OS_FREEBSD) # define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC_FAST diff --git a/base/pcg-random/pcg_random.hpp b/base/pcg-random/pcg_random.hpp index abf83a60ee1..11e775d0c4e 100644 --- a/base/pcg-random/pcg_random.hpp +++ b/base/pcg-random/pcg_random.hpp @@ -1643,22 +1643,22 @@ typedef setseq_base template -using ext_std8 = extended; template -using ext_std16 = extended; template -using ext_std32 = extended; template -using ext_std64 = extended; diff --git a/cmake/target.cmake b/cmake/target.cmake index 7174ca3c2a9..d1a0b8f9cbf 100644 --- a/cmake/target.cmake +++ b/cmake/target.cmake @@ -12,6 +12,9 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin") set (OS_DARWIN 1) add_definitions(-D OS_DARWIN) +elseif (CMAKE_SYSTEM_NAME MATCHES "SunOS") + set (OS_SUNOS 1) + add_definitions(-D OS_SUNOS) endif () if (CMAKE_CROSSCOMPILING) diff --git a/programs/install/Install.cpp b/programs/install/Install.cpp index 739ee26cf31..96d336673d0 100644 --- a/programs/install/Install.cpp +++ b/programs/install/Install.cpp @@ -844,8 +844,8 @@ namespace fmt::print("The pidof command returned unusual output.\n"); } - WriteBufferFromFileDescriptor stderr(STDERR_FILENO); - copyData(sh->err, stderr); + WriteBufferFromFileDescriptor std_err(STDERR_FILENO); + copyData(sh->err, std_err); sh->tryWait(); } diff --git a/src/Common/Config/ConfigProcessor.cpp b/src/Common/Config/ConfigProcessor.cpp index 5399826fe20..39ab407579d 100644 --- a/src/Common/Config/ConfigProcessor.cpp +++ b/src/Common/Config/ConfigProcessor.cpp @@ -234,7 +234,7 @@ void ConfigProcessor::merge(XMLDocumentPtr config, XMLDocumentPtr with) static std::string layerFromHost() { - utsname buf; + struct utsname buf; if (uname(&buf)) throw Poco::Exception(std::string("uname failed: ") + errnoToString(errno)); diff --git a/src/Common/Elf.cpp b/src/Common/Elf.cpp index ee78c988f69..9e312dc79f2 100644 --- a/src/Common/Elf.cpp +++ b/src/Common/Elf.cpp @@ -129,7 +129,12 @@ String Elf::getBuildID() const return {}; } - +#if defined(OS_SUNOS) +String Elf::getBuildID(const char * nhdr_pos, size_t size) +{ + return {}; +} +#else String Elf::getBuildID(const char * nhdr_pos, size_t size) { const char * nhdr_end = nhdr_pos + size; @@ -149,6 +154,7 @@ String Elf::getBuildID(const char * nhdr_pos, size_t size) return {}; } +#endif // OS_SUNOS String Elf::getBinaryHash() const diff --git a/src/Common/OpenTelemetryTraceContext.h b/src/Common/OpenTelemetryTraceContext.h index fe88981cb1e..421d14818fa 100644 --- a/src/Common/OpenTelemetryTraceContext.h +++ b/src/Common/OpenTelemetryTraceContext.h @@ -11,7 +11,12 @@ struct OpenTelemetryTraceContext // The incoming tracestate header and the trace flags, we just pass them // downstream. See https://www.w3.org/TR/trace-context/ String tracestate; + +#if defined(OS_SUNOS) + uint8_t trace_flags = 0; +#else __uint8_t trace_flags = 0; +#endif // Parse/compose OpenTelemetry traceparent header. bool parseTraceparentHeader(const std::string & traceparent, std::string & error); diff --git a/src/Common/Stopwatch.cpp b/src/Common/Stopwatch.cpp index d792e054d24..b17e343f1af 100644 --- a/src/Common/Stopwatch.cpp +++ b/src/Common/Stopwatch.cpp @@ -7,8 +7,12 @@ StopwatchRUsage::Timestamp StopwatchRUsage::Timestamp::current() ::rusage rusage {}; #if !defined(__APPLE__) +#if defined(OS_SUNOS) + ::getrusage(RUSAGE_LWP, &rusage); +#else ::getrusage(RUSAGE_THREAD, &rusage); -#endif +#endif // OS_SUNOS +#endif // __APPLE__ res.user_ns = rusage.ru_utime.tv_sec * 1000000000UL + rusage.ru_utime.tv_usec * 1000UL; res.sys_ns = rusage.ru_stime.tv_sec * 1000000000UL + rusage.ru_stime.tv_usec * 1000UL; return res; diff --git a/src/Common/TerminalSize.cpp b/src/Common/TerminalSize.cpp index 714a19b188a..a020098aa44 100644 --- a/src/Common/TerminalSize.cpp +++ b/src/Common/TerminalSize.cpp @@ -1,5 +1,8 @@ #include #include +#if defined(OS_SUNOS) +# include +#endif #include #include #include @@ -14,7 +17,7 @@ uint16_t getTerminalWidth() { if (isatty(STDIN_FILENO)) { - winsize terminal_size {}; + struct winsize terminal_size {}; if (ioctl(STDIN_FILENO, TIOCGWINSZ, &terminal_size)) DB::throwFromErrno("Cannot obtain terminal window size (ioctl TIOCGWINSZ)", DB::ErrorCodes::SYSTEM_ERROR); diff --git a/src/Common/ThreadProfileEvents.h b/src/Common/ThreadProfileEvents.h index d96339add26..4422067bd1c 100644 --- a/src/Common/ThreadProfileEvents.h +++ b/src/Common/ThreadProfileEvents.h @@ -105,8 +105,12 @@ struct RUsageCounters { ::rusage rusage {}; #if !defined(__APPLE__) +#if defined(OS_SUNOS) + ::getrusage(RUSAGE_LWP, &rusage); +#else ::getrusage(RUSAGE_THREAD, &rusage); -#endif +#endif // OS_SUNOS +#endif // __APPLE return RUsageCounters(rusage, getClockMonotonic()); } diff --git a/src/Common/checkStackSize.cpp b/src/Common/checkStackSize.cpp index e94abc08c6b..54ffab7f3d0 100644 --- a/src/Common/checkStackSize.cpp +++ b/src/Common/checkStackSize.cpp @@ -49,7 +49,7 @@ __attribute__((__weak__)) void checkStackSize() stack_address = reinterpret_cast(reinterpret_cast(pthread_get_stackaddr_np(thread)) - max_stack_size); #else pthread_attr_t attr; -# if defined(__FreeBSD__) +# if defined(__FreeBSD__) || defined(OS_SUNOS) pthread_attr_init(&attr); if (0 != pthread_attr_get_np(pthread_self(), &attr)) throwFromErrno("Cannot pthread_attr_get_np", ErrorCodes::CANNOT_PTHREAD_ATTR); diff --git a/src/Common/examples/int_hashes_perf.cpp b/src/Common/examples/int_hashes_perf.cpp index 543326a9e5c..8a32f8f536e 100644 --- a/src/Common/examples/int_hashes_perf.cpp +++ b/src/Common/examples/int_hashes_perf.cpp @@ -12,7 +12,7 @@ static void setAffinity() { -#if !defined(__APPLE__) && !defined(__FreeBSD__) +#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__sun) cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0, &mask); diff --git a/src/Common/setThreadName.cpp b/src/Common/setThreadName.cpp index 3c20711a761..9e45c62a9c9 100644 --- a/src/Common/setThreadName.cpp +++ b/src/Common/setThreadName.cpp @@ -1,6 +1,6 @@ #include -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(OS_SUNOS) #elif defined(__FreeBSD__) #include #else @@ -34,6 +34,8 @@ void setThreadName(const char * name) if ((false)) #elif defined(OS_DARWIN) if (0 != pthread_setname_np(name)) +#elif defined(OS_SUNOS) + if (0 != pthread_setname_np(pthread_self(), name)) #else if (0 != prctl(PR_SET_NAME, name, 0, 0, 0)) #endif @@ -44,7 +46,7 @@ std::string getThreadName() { std::string name(16, '\0'); -#if defined(__APPLE__) +#if defined(__APPLE__) || defined(OS_SUNOS) if (pthread_getname_np(pthread_self(), name.data(), name.size())) throw DB::Exception("Cannot get thread name with pthread_getname_np()", DB::ErrorCodes::PTHREAD_ERROR); #elif defined(__FreeBSD__) diff --git a/src/IO/BitHelpers.h b/src/IO/BitHelpers.h index 0e2a08aa9a0..1cb62f5f367 100644 --- a/src/IO/BitHelpers.h +++ b/src/IO/BitHelpers.h @@ -9,6 +9,8 @@ #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined (__ANDROID__) # include +#elif defined(__sun) +# include #elif defined(__APPLE__) # include diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp index 0825d9f329e..8e178b6629e 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp @@ -1,4 +1,7 @@ #include +#if defined(OS_SUNOS) +# include +#endif #include #include #include diff --git a/src/Storages/PostgreSQL/PostgreSQLPoolWithFailover.h b/src/Storages/PostgreSQL/PostgreSQLPoolWithFailover.h index 8f6027c2c0d..048200f012a 100644 --- a/src/Storages/PostgreSQL/PostgreSQLPoolWithFailover.h +++ b/src/Storages/PostgreSQL/PostgreSQLPoolWithFailover.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include "PostgreSQLConnectionPool.h" diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 83ad8181b3f..3da8612e6c1 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -39,7 +39,7 @@ if (NOT DEFINED ENABLE_UTILS OR ENABLE_UTILS) endif () # memcpy_jart.S contains position dependent code - if (NOT CMAKE_POSITION_INDEPENDENT_CODE AND NOT OS_DARWIN) + if (NOT CMAKE_POSITION_INDEPENDENT_CODE AND NOT OS_DARWIN AND NOT OS_SUNOS) add_subdirectory (memcpy-bench) endif () endif () diff --git a/utils/memcpy-bench/CMakeLists.txt b/utils/memcpy-bench/CMakeLists.txt index 5fcde231688..5353b6fb68e 100644 --- a/utils/memcpy-bench/CMakeLists.txt +++ b/utils/memcpy-bench/CMakeLists.txt @@ -15,6 +15,10 @@ add_executable (memcpy-bench add_compile_options(memcpy-bench PRIVATE -fno-tree-loop-distribute-patterns) +if (OS_SUNOS) + target_compile_options(memcpy-bench PRIVATE "-Wa,--divide") +endif() + set_source_files_properties(FastMemcpy.cpp PROPERTIES COMPILE_FLAGS "-Wno-old-style-cast") set_source_files_properties(FastMemcpy_Avx.cpp PROPERTIES COMPILE_FLAGS "-mavx -Wno-old-style-cast -Wno-cast-qual -Wno-cast-align")