-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
178 changed files
with
96,481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <base/Decimal.h> | ||
#include <base/extended_types.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
/// Explicit template instantiations. | ||
|
||
#define FOR_EACH_UNDERLYING_DECIMAL_TYPE(M) \ | ||
M(Int32) \ | ||
M(Int64) \ | ||
M(Int128) \ | ||
M(Int256) | ||
|
||
#define FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS(M, X) \ | ||
M(Int32, X) \ | ||
M(Int64, X) \ | ||
M(Int128, X) \ | ||
M(Int256, X) | ||
|
||
template <typename T> const Decimal<T> & Decimal<T>::operator += (const T & x) { value += x; return *this; } | ||
template <typename T> const Decimal<T> & Decimal<T>::operator -= (const T & x) { value -= x; return *this; } | ||
template <typename T> const Decimal<T> & Decimal<T>::operator *= (const T & x) { value *= x; return *this; } | ||
template <typename T> const Decimal<T> & Decimal<T>::operator /= (const T & x) { value /= x; return *this; } | ||
template <typename T> const Decimal<T> & Decimal<T>::operator %= (const T & x) { value %= x; return *this; } | ||
|
||
template <typename T> void NO_SANITIZE_UNDEFINED Decimal<T>::addOverflow(const T & x) { value += x; } | ||
|
||
/// Maybe this explicit instantiation affects performance since operators cannot be inlined. | ||
|
||
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator += (const Decimal<U> & x) { value += static_cast<T>(x.value); return *this; } | ||
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator -= (const Decimal<U> & x) { value -= static_cast<T>(x.value); return *this; } | ||
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator *= (const Decimal<U> & x) { value *= static_cast<T>(x.value); return *this; } | ||
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator /= (const Decimal<U> & x) { value /= static_cast<T>(x.value); return *this; } | ||
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator %= (const Decimal<U> & x) { value %= static_cast<T>(x.value); return *this; } | ||
|
||
#define DISPATCH(TYPE_T, TYPE_U) \ | ||
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator += (const Decimal<TYPE_U> & x); \ | ||
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator -= (const Decimal<TYPE_U> & x); \ | ||
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator *= (const Decimal<TYPE_U> & x); \ | ||
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator /= (const Decimal<TYPE_U> & x); \ | ||
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator %= (const Decimal<TYPE_U> & x); | ||
#define INVOKE(X) FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS(DISPATCH, X) | ||
FOR_EACH_UNDERLYING_DECIMAL_TYPE(INVOKE); | ||
#undef INVOKE | ||
#undef DISPATCH | ||
|
||
#define DISPATCH(TYPE) template struct Decimal<TYPE>; | ||
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH) | ||
#undef DISPATCH | ||
|
||
template <typename T> bool operator< (const Decimal<T> & x, const Decimal<T> & y) { return x.value < y.value; } | ||
template <typename T> bool operator> (const Decimal<T> & x, const Decimal<T> & y) { return x.value > y.value; } | ||
template <typename T> bool operator<= (const Decimal<T> & x, const Decimal<T> & y) { return x.value <= y.value; } | ||
template <typename T> bool operator>= (const Decimal<T> & x, const Decimal<T> & y) { return x.value >= y.value; } | ||
template <typename T> bool operator== (const Decimal<T> & x, const Decimal<T> & y) { return x.value == y.value; } | ||
template <typename T> bool operator!= (const Decimal<T> & x, const Decimal<T> & y) { return x.value != y.value; } | ||
|
||
#define DISPATCH(TYPE) \ | ||
template bool operator< (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template bool operator> (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template bool operator<= (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template bool operator>= (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template bool operator== (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template bool operator!= (const Decimal<TYPE> & x, const Decimal<TYPE> & y); | ||
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH) | ||
#undef DISPATCH | ||
|
||
|
||
template <typename T> Decimal<T> operator+ (const Decimal<T> & x, const Decimal<T> & y) { return x.value + y.value; } | ||
template <typename T> Decimal<T> operator- (const Decimal<T> & x, const Decimal<T> & y) { return x.value - y.value; } | ||
template <typename T> Decimal<T> operator* (const Decimal<T> & x, const Decimal<T> & y) { return x.value * y.value; } | ||
template <typename T> Decimal<T> operator/ (const Decimal<T> & x, const Decimal<T> & y) { return x.value / y.value; } | ||
template <typename T> Decimal<T> operator- (const Decimal<T> & x) { return -x.value; } | ||
|
||
#define DISPATCH(TYPE) \ | ||
template Decimal<TYPE> operator+ (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template Decimal<TYPE> operator- (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template Decimal<TYPE> operator* (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template Decimal<TYPE> operator/ (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \ | ||
template Decimal<TYPE> operator- (const Decimal<TYPE> & x); | ||
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH) | ||
#undef DISPATCH | ||
|
||
#undef FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS | ||
#undef FOR_EACH_UNDERLYING_DECIMAL_TYPE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <base/cgroupsv2.h> | ||
|
||
#include <base/defines.h> | ||
|
||
#include <fstream> | ||
#include <sstream> | ||
|
||
|
||
bool cgroupsV2Enabled() | ||
{ | ||
#if defined(OS_LINUX) | ||
try | ||
{ | ||
/// This file exists iff the host has cgroups v2 enabled. | ||
auto controllers_file = default_cgroups_mount / "cgroup.controllers"; | ||
if (!std::filesystem::exists(controllers_file)) | ||
return false; | ||
return true; | ||
} | ||
catch (const std::filesystem::filesystem_error &) /// all "underlying OS API errors", typically: permission denied | ||
{ | ||
return false; /// not logging the exception as most callers fall back to cgroups v1 | ||
} | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
bool cgroupsV2MemoryControllerEnabled() | ||
{ | ||
#if defined(OS_LINUX) | ||
chassert(cgroupsV2Enabled()); | ||
/// According to https://docs.kernel.org/admin-guide/cgroup-v2.html, file "cgroup.controllers" defines which controllers are available | ||
/// for the current + child cgroups. The set of available controllers can be restricted from level to level using file | ||
/// "cgroups.subtree_control". It is therefore sufficient to check the bottom-most nested "cgroup.controllers" file. | ||
std::string cgroup = cgroupV2OfProcess(); | ||
auto cgroup_dir = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup); | ||
std::ifstream controllers_file(cgroup_dir / "cgroup.controllers"); | ||
if (!controllers_file.is_open()) | ||
return false; | ||
std::string controllers; | ||
std::getline(controllers_file, controllers); | ||
return controllers.find("memory") != std::string::npos; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
std::string cgroupV2OfProcess() | ||
{ | ||
#if defined(OS_LINUX) | ||
chassert(cgroupsV2Enabled()); | ||
/// All PIDs assigned to a cgroup are in /sys/fs/cgroups/{cgroup_name}/cgroup.procs | ||
/// A simpler way to get the membership is: | ||
std::ifstream cgroup_name_file("/proc/self/cgroup"); | ||
if (!cgroup_name_file.is_open()) | ||
return ""; | ||
/// With cgroups v2, there will be a *single* line with prefix "0::/" | ||
/// (see https://docs.kernel.org/admin-guide/cgroup-v2.html) | ||
std::string cgroup; | ||
std::getline(cgroup_name_file, cgroup); | ||
static const std::string v2_prefix = "0::/"; | ||
if (!cgroup.starts_with(v2_prefix)) | ||
return ""; | ||
cgroup = cgroup.substr(v2_prefix.length()); | ||
return cgroup; | ||
#else | ||
return ""; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
|
||
#if defined(OS_LINUX) | ||
/// I think it is possible to mount the cgroups hierarchy somewhere else (e.g. when in containers). | ||
/// /sys/fs/cgroup was still symlinked to the actual mount in the cases that I have seen. | ||
static inline const std::filesystem::path default_cgroups_mount = "/sys/fs/cgroup"; | ||
#endif | ||
|
||
/// Is cgroups v2 enabled on the system? | ||
bool cgroupsV2Enabled(); | ||
|
||
/// Is the memory controller of cgroups v2 enabled on the system? | ||
/// Assumes that cgroupsV2Enabled() is enabled. | ||
bool cgroupsV2MemoryControllerEnabled(); | ||
|
||
/// Which cgroup does the process belong to? | ||
/// Returns an empty string if the cgroup cannot be determined. | ||
/// Assumes that cgroupsV2Enabled() is enabled. | ||
std::string cgroupV2OfProcess(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <base/int8_to_string.h> | ||
|
||
namespace std | ||
{ | ||
std::string to_string(Int8 v) /// NOLINT (cert-dcl58-cpp) | ||
{ | ||
return to_string(int8_t{v}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <base/defines.h> | ||
#include <base/types.h> | ||
|
||
#include <fmt/format.h> | ||
|
||
template <> | ||
struct fmt::formatter<Int8> : fmt::formatter<int8_t> | ||
{ | ||
}; | ||
|
||
|
||
namespace std | ||
{ | ||
std::string to_string(Int8 v); /// NOLINT (cert-dcl58-cpp) | ||
} |
Oops, something went wrong.