Skip to content

Commit 50fd104

Browse files
committed
Fix missing base/base and contrib
1 parent 07ae9b0 commit 50fd104

File tree

178 files changed

+96481
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+96481
-0
lines changed

base/base/Decimal.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <base/Decimal.h>
2+
#include <base/extended_types.h>
3+
4+
namespace DB
5+
{
6+
7+
/// Explicit template instantiations.
8+
9+
#define FOR_EACH_UNDERLYING_DECIMAL_TYPE(M) \
10+
M(Int32) \
11+
M(Int64) \
12+
M(Int128) \
13+
M(Int256)
14+
15+
#define FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS(M, X) \
16+
M(Int32, X) \
17+
M(Int64, X) \
18+
M(Int128, X) \
19+
M(Int256, X)
20+
21+
template <typename T> const Decimal<T> & Decimal<T>::operator += (const T & x) { value += x; return *this; }
22+
template <typename T> const Decimal<T> & Decimal<T>::operator -= (const T & x) { value -= x; return *this; }
23+
template <typename T> const Decimal<T> & Decimal<T>::operator *= (const T & x) { value *= x; return *this; }
24+
template <typename T> const Decimal<T> & Decimal<T>::operator /= (const T & x) { value /= x; return *this; }
25+
template <typename T> const Decimal<T> & Decimal<T>::operator %= (const T & x) { value %= x; return *this; }
26+
27+
template <typename T> void NO_SANITIZE_UNDEFINED Decimal<T>::addOverflow(const T & x) { value += x; }
28+
29+
/// Maybe this explicit instantiation affects performance since operators cannot be inlined.
30+
31+
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator += (const Decimal<U> & x) { value += static_cast<T>(x.value); return *this; }
32+
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator -= (const Decimal<U> & x) { value -= static_cast<T>(x.value); return *this; }
33+
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator *= (const Decimal<U> & x) { value *= static_cast<T>(x.value); return *this; }
34+
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator /= (const Decimal<U> & x) { value /= static_cast<T>(x.value); return *this; }
35+
template <typename T> template <typename U> const Decimal<T> & Decimal<T>::operator %= (const Decimal<U> & x) { value %= static_cast<T>(x.value); return *this; }
36+
37+
#define DISPATCH(TYPE_T, TYPE_U) \
38+
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator += (const Decimal<TYPE_U> & x); \
39+
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator -= (const Decimal<TYPE_U> & x); \
40+
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator *= (const Decimal<TYPE_U> & x); \
41+
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator /= (const Decimal<TYPE_U> & x); \
42+
template const Decimal<TYPE_T> & Decimal<TYPE_T>::operator %= (const Decimal<TYPE_U> & x);
43+
#define INVOKE(X) FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS(DISPATCH, X)
44+
FOR_EACH_UNDERLYING_DECIMAL_TYPE(INVOKE);
45+
#undef INVOKE
46+
#undef DISPATCH
47+
48+
#define DISPATCH(TYPE) template struct Decimal<TYPE>;
49+
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH)
50+
#undef DISPATCH
51+
52+
template <typename T> bool operator< (const Decimal<T> & x, const Decimal<T> & y) { return x.value < y.value; }
53+
template <typename T> bool operator> (const Decimal<T> & x, const Decimal<T> & y) { return x.value > y.value; }
54+
template <typename T> bool operator<= (const Decimal<T> & x, const Decimal<T> & y) { return x.value <= y.value; }
55+
template <typename T> bool operator>= (const Decimal<T> & x, const Decimal<T> & y) { return x.value >= y.value; }
56+
template <typename T> bool operator== (const Decimal<T> & x, const Decimal<T> & y) { return x.value == y.value; }
57+
template <typename T> bool operator!= (const Decimal<T> & x, const Decimal<T> & y) { return x.value != y.value; }
58+
59+
#define DISPATCH(TYPE) \
60+
template bool operator< (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
61+
template bool operator> (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
62+
template bool operator<= (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
63+
template bool operator>= (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
64+
template bool operator== (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
65+
template bool operator!= (const Decimal<TYPE> & x, const Decimal<TYPE> & y);
66+
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH)
67+
#undef DISPATCH
68+
69+
70+
template <typename T> Decimal<T> operator+ (const Decimal<T> & x, const Decimal<T> & y) { return x.value + y.value; }
71+
template <typename T> Decimal<T> operator- (const Decimal<T> & x, const Decimal<T> & y) { return x.value - y.value; }
72+
template <typename T> Decimal<T> operator* (const Decimal<T> & x, const Decimal<T> & y) { return x.value * y.value; }
73+
template <typename T> Decimal<T> operator/ (const Decimal<T> & x, const Decimal<T> & y) { return x.value / y.value; }
74+
template <typename T> Decimal<T> operator- (const Decimal<T> & x) { return -x.value; }
75+
76+
#define DISPATCH(TYPE) \
77+
template Decimal<TYPE> operator+ (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
78+
template Decimal<TYPE> operator- (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
79+
template Decimal<TYPE> operator* (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
80+
template Decimal<TYPE> operator/ (const Decimal<TYPE> & x, const Decimal<TYPE> & y); \
81+
template Decimal<TYPE> operator- (const Decimal<TYPE> & x);
82+
FOR_EACH_UNDERLYING_DECIMAL_TYPE(DISPATCH)
83+
#undef DISPATCH
84+
85+
#undef FOR_EACH_UNDERLYING_DECIMAL_TYPE_PASS
86+
#undef FOR_EACH_UNDERLYING_DECIMAL_TYPE
87+
}

base/base/cgroupsv2.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <base/cgroupsv2.h>
2+
3+
#include <base/defines.h>
4+
5+
#include <fstream>
6+
#include <sstream>
7+
8+
9+
bool cgroupsV2Enabled()
10+
{
11+
#if defined(OS_LINUX)
12+
try
13+
{
14+
/// This file exists iff the host has cgroups v2 enabled.
15+
auto controllers_file = default_cgroups_mount / "cgroup.controllers";
16+
if (!std::filesystem::exists(controllers_file))
17+
return false;
18+
return true;
19+
}
20+
catch (const std::filesystem::filesystem_error &) /// all "underlying OS API errors", typically: permission denied
21+
{
22+
return false; /// not logging the exception as most callers fall back to cgroups v1
23+
}
24+
#else
25+
return false;
26+
#endif
27+
}
28+
29+
bool cgroupsV2MemoryControllerEnabled()
30+
{
31+
#if defined(OS_LINUX)
32+
chassert(cgroupsV2Enabled());
33+
/// According to https://docs.kernel.org/admin-guide/cgroup-v2.html, file "cgroup.controllers" defines which controllers are available
34+
/// for the current + child cgroups. The set of available controllers can be restricted from level to level using file
35+
/// "cgroups.subtree_control". It is therefore sufficient to check the bottom-most nested "cgroup.controllers" file.
36+
std::string cgroup = cgroupV2OfProcess();
37+
auto cgroup_dir = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup);
38+
std::ifstream controllers_file(cgroup_dir / "cgroup.controllers");
39+
if (!controllers_file.is_open())
40+
return false;
41+
std::string controllers;
42+
std::getline(controllers_file, controllers);
43+
return controllers.find("memory") != std::string::npos;
44+
#else
45+
return false;
46+
#endif
47+
}
48+
49+
std::string cgroupV2OfProcess()
50+
{
51+
#if defined(OS_LINUX)
52+
chassert(cgroupsV2Enabled());
53+
/// All PIDs assigned to a cgroup are in /sys/fs/cgroups/{cgroup_name}/cgroup.procs
54+
/// A simpler way to get the membership is:
55+
std::ifstream cgroup_name_file("/proc/self/cgroup");
56+
if (!cgroup_name_file.is_open())
57+
return "";
58+
/// With cgroups v2, there will be a *single* line with prefix "0::/"
59+
/// (see https://docs.kernel.org/admin-guide/cgroup-v2.html)
60+
std::string cgroup;
61+
std::getline(cgroup_name_file, cgroup);
62+
static const std::string v2_prefix = "0::/";
63+
if (!cgroup.starts_with(v2_prefix))
64+
return "";
65+
cgroup = cgroup.substr(v2_prefix.length());
66+
return cgroup;
67+
#else
68+
return "";
69+
#endif
70+
}

base/base/cgroupsv2.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
#include <string>
5+
6+
#if defined(OS_LINUX)
7+
/// I think it is possible to mount the cgroups hierarchy somewhere else (e.g. when in containers).
8+
/// /sys/fs/cgroup was still symlinked to the actual mount in the cases that I have seen.
9+
static inline const std::filesystem::path default_cgroups_mount = "/sys/fs/cgroup";
10+
#endif
11+
12+
/// Is cgroups v2 enabled on the system?
13+
bool cgroupsV2Enabled();
14+
15+
/// Is the memory controller of cgroups v2 enabled on the system?
16+
/// Assumes that cgroupsV2Enabled() is enabled.
17+
bool cgroupsV2MemoryControllerEnabled();
18+
19+
/// Which cgroup does the process belong to?
20+
/// Returns an empty string if the cgroup cannot be determined.
21+
/// Assumes that cgroupsV2Enabled() is enabled.
22+
std::string cgroupV2OfProcess();

base/base/int8_to_string.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <base/int8_to_string.h>
2+
3+
namespace std
4+
{
5+
std::string to_string(Int8 v) /// NOLINT (cert-dcl58-cpp)
6+
{
7+
return to_string(int8_t{v});
8+
}
9+
}

base/base/int8_to_string.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <base/defines.h>
4+
#include <base/types.h>
5+
6+
#include <fmt/format.h>
7+
8+
template <>
9+
struct fmt::formatter<Int8> : fmt::formatter<int8_t>
10+
{
11+
};
12+
13+
14+
namespace std
15+
{
16+
std::string to_string(Int8 v); /// NOLINT (cert-dcl58-cpp)
17+
}

0 commit comments

Comments
 (0)