2018-10-21 09:50:41 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/util.h
|
|
|
|
//
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::util::size(std::ostream &s)
|
|
|
|
{
|
|
|
|
const auto cur(s.tellp());
|
|
|
|
s.seekp(0, std::ios::end);
|
|
|
|
const auto ret(s.tellp());
|
|
|
|
s.seekp(cur, std::ios::beg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/env.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::getenv(const string_view &key)
|
|
|
|
{
|
|
|
|
thread_local char keystr[128];
|
|
|
|
if(unlikely(size(key) >= sizeof(keystr)))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"getenv(): variable key is too long."
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ensure the key is null terminated for the std:: call.
|
|
|
|
const size_t len
|
|
|
|
{
|
|
|
|
strlcpy(keystr, key)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view var
|
|
|
|
{
|
|
|
|
std::getenv(keystr)
|
|
|
|
};
|
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/pretty.h
|
|
|
|
//
|
|
|
|
|
2018-10-21 09:50:41 +02:00
|
|
|
//
|
|
|
|
// Human readable time suite
|
|
|
|
//
|
|
|
|
|
2020-09-28 02:53:18 +02:00
|
|
|
namespace ircd { inline namespace util
|
|
|
|
{
|
|
|
|
using pretty_time_formats = std::array<string_view, 2>;
|
|
|
|
using pretty_time_element = std::tuple<pretty_time_formats, long double>;
|
|
|
|
|
|
|
|
template<size_t i,
|
|
|
|
class T>
|
|
|
|
static string_view pretty(const mutable_buffer &, const T &, const uint &fmt);
|
|
|
|
|
|
|
|
[[gnu::visibility("internal")]]
|
|
|
|
extern const std::array<pretty_time_element, 9> pretty_time_unit;
|
|
|
|
}}
|
|
|
|
|
|
|
|
decltype(ircd::util::pretty_time_unit)
|
|
|
|
ircd::util::pretty_time_unit
|
|
|
|
{{
|
|
|
|
// fmt=0 fmt=1
|
|
|
|
{ { "nanoseconds", "ns" }, 1000.0L },
|
|
|
|
{ { "microseconds", "us" }, 1000.0L },
|
|
|
|
{ { "milliseconds", "ms" }, 1000.0L },
|
|
|
|
{ { "seconds", "s" }, 60.0L },
|
|
|
|
{ { "minutes", "m" }, 60.0L },
|
|
|
|
{ { "hours", "h" }, 24.0L },
|
|
|
|
{ { "days", "d" }, 7.0L },
|
|
|
|
{ { "weeks", "w" }, 4.0L },
|
|
|
|
{ { "months", "M" }, 12.0L },
|
|
|
|
}};
|
|
|
|
|
|
|
|
template<size_t i,
|
|
|
|
class T>
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
|
|
|
const T &dur,
|
|
|
|
const uint &fmt)
|
2018-10-21 09:50:41 +02:00
|
|
|
{
|
2020-09-28 02:53:18 +02:00
|
|
|
static const auto &unit
|
|
|
|
{
|
|
|
|
pretty_time_unit
|
|
|
|
};
|
2018-10-21 09:50:41 +02:00
|
|
|
|
2019-01-01 00:22:09 +01:00
|
|
|
const string_view &fmtstr
|
|
|
|
{
|
|
|
|
fmt == 1?
|
|
|
|
"%.2lf%s"_sv:
|
|
|
|
"%.2lf %s"_sv
|
|
|
|
};
|
|
|
|
|
2020-09-28 02:53:18 +02:00
|
|
|
size_t pos(i);
|
|
|
|
long double val(dur.count());
|
2019-08-24 00:52:58 +02:00
|
|
|
for(; val > std::get<1>(unit.at(pos)) && pos < unit.size() - 1; ++pos)
|
2019-08-21 05:10:25 +02:00
|
|
|
val /= std::get<1>(unit.at(pos));
|
2018-10-21 09:50:41 +02:00
|
|
|
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2020-09-28 02:53:18 +02:00
|
|
|
out, fmtstr,
|
|
|
|
val,
|
|
|
|
std::get<0>(unit.at(pos)).at(fmt)
|
2018-10-21 09:50:41 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-28 02:53:18 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
|
|
|
const nanoseconds &val,
|
|
|
|
const uint &fmt)
|
|
|
|
{
|
|
|
|
return pretty<0>(out, val, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
|
|
|
const microseconds &val,
|
|
|
|
const uint &fmt)
|
|
|
|
{
|
|
|
|
return pretty<1>(out, val, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
|
|
|
const milliseconds &val,
|
|
|
|
const uint &fmt)
|
|
|
|
{
|
|
|
|
return pretty<2>(out, val, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
|
|
|
const seconds &val,
|
|
|
|
const uint &fmt)
|
|
|
|
{
|
|
|
|
return pretty<3>(out, val, fmt);
|
|
|
|
}
|
|
|
|
|
2018-10-21 09:50:41 +02:00
|
|
|
//
|
|
|
|
// Human readable space suite
|
|
|
|
//
|
|
|
|
|
2019-09-23 00:38:51 +02:00
|
|
|
decltype(ircd::util::pretty_fmt)
|
|
|
|
ircd::util::pretty_fmt
|
2018-11-28 22:49:14 +01:00
|
|
|
{
|
2019-09-23 00:38:51 +02:00
|
|
|
"%.2lf %s (%lu)"_sv,
|
|
|
|
"%.2lf %s"_sv,
|
|
|
|
"%.2lf%s"_sv,
|
|
|
|
string_view{},
|
2018-11-28 22:49:14 +01:00
|
|
|
};
|
|
|
|
|
2018-10-21 09:50:41 +02:00
|
|
|
std::string
|
2019-09-23 00:38:51 +02:00
|
|
|
ircd::util::pretty(const human_readable_size &value,
|
|
|
|
const uint &fmt)
|
2018-10-21 09:50:41 +02:00
|
|
|
{
|
2019-09-23 00:38:51 +02:00
|
|
|
return pretty(value, pretty_fmt[fmt]);
|
2018-11-28 22:49:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
2019-09-23 00:38:51 +02:00
|
|
|
const human_readable_size &value,
|
|
|
|
const uint &fmt)
|
2018-11-28 22:49:14 +01:00
|
|
|
{
|
2019-09-23 00:38:51 +02:00
|
|
|
return pretty(out, pretty_fmt[fmt], value);
|
2018-11-28 22:49:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::util::pretty(const human_readable_size &value,
|
|
|
|
const string_view &fmt)
|
|
|
|
{
|
|
|
|
return util::string(64, [&value, &fmt]
|
2018-10-21 09:50:41 +02:00
|
|
|
(const mutable_buffer &out)
|
|
|
|
{
|
2018-11-28 22:49:14 +01:00
|
|
|
return pretty(out, fmt, value);
|
2018-10-21 09:50:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::pretty(const mutable_buffer &out,
|
2018-11-28 22:49:14 +01:00
|
|
|
const string_view &fmt,
|
2018-10-21 09:50:41 +02:00
|
|
|
const human_readable_size &value)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2018-11-28 22:49:14 +01:00
|
|
|
out, fmt,
|
2018-10-21 09:50:41 +02:00
|
|
|
std::get<long double>(value),
|
|
|
|
std::get<const string_view &>(value),
|
|
|
|
std::get<uint64_t>(value)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-09-23 00:38:51 +02:00
|
|
|
out, "%lu",
|
2018-10-21 09:50:41 +02:00
|
|
|
std::get<uint64_t>(value)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::human_readable_size
|
2019-09-23 00:28:36 +02:00
|
|
|
ircd::util::si(const uint64_t &value)
|
2018-10-21 09:50:41 +02:00
|
|
|
{
|
|
|
|
static const std::array<string_view, 7> unit
|
|
|
|
{
|
2019-09-23 00:28:36 +02:00
|
|
|
" ", "K", "M", "G", "T", "P", "E"
|
2018-10-21 09:50:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto pos(0);
|
|
|
|
long double v(value);
|
2022-06-13 23:46:38 +02:00
|
|
|
for(; v > 1000.0L; v /= 1000.0L, ++pos);
|
2018-10-21 09:50:41 +02:00
|
|
|
return
|
|
|
|
{
|
|
|
|
value, v, unit.at(pos)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::human_readable_size
|
2019-09-23 00:28:36 +02:00
|
|
|
ircd::util::iec(const uint64_t &value)
|
2018-10-21 09:50:41 +02:00
|
|
|
{
|
|
|
|
static const std::array<string_view, 7> unit
|
|
|
|
{
|
2019-09-23 00:28:36 +02:00
|
|
|
"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
|
2018-10-21 09:50:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto pos(0);
|
|
|
|
long double v(value);
|
2022-06-13 23:46:38 +02:00
|
|
|
for(; v > 1024.0L; v /= 1024.0L, ++pos);
|
2018-10-21 09:50:41 +02:00
|
|
|
return
|
|
|
|
{
|
|
|
|
value, v, unit.at(pos)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-10-21 09:50:41 +02:00
|
|
|
//
|
2018-10-25 22:39:41 +02:00
|
|
|
// util/string.h
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Close over the common pattern to write directly into a post-C++11 standard
|
|
|
|
/// string through the data() member requiring a const_cast. Closure returns
|
|
|
|
/// a view of the data actually written to the buffer.
|
|
|
|
std::string
|
|
|
|
ircd::util::string(const size_t &size,
|
2019-02-08 02:52:24 +01:00
|
|
|
const string_closure_view &closure)
|
2018-10-25 22:39:41 +02:00
|
|
|
{
|
|
|
|
return string(size, [&closure]
|
|
|
|
(const mutable_buffer &buffer)
|
|
|
|
{
|
|
|
|
return ircd::size(closure(buffer));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close over the common pattern to write directly into a post-C++11 standard
|
|
|
|
/// string through the data() member requiring a const_cast. Closure returns
|
|
|
|
/// the final size of the data written into the buffer.
|
|
|
|
std::string
|
|
|
|
ircd::util::string(const size_t &size,
|
2019-02-08 02:52:24 +01:00
|
|
|
const string_closure_size &closure)
|
2018-10-25 22:39:41 +02:00
|
|
|
{
|
2019-02-08 02:52:24 +01:00
|
|
|
const size_t alloc_size
|
|
|
|
{
|
|
|
|
size & ~SHRINK_TO_FIT
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string ret(alloc_size, char{});
|
2018-10-25 22:39:41 +02:00
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
2020-11-03 20:30:15 +01:00
|
|
|
mutable_cast(ret.data()), ret.size()
|
2018-10-25 22:39:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const size_t consumed
|
|
|
|
{
|
|
|
|
closure(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(consumed <= buffer::size(buf));
|
|
|
|
data(buf)[consumed] = '\0';
|
|
|
|
ret.resize(consumed);
|
2019-02-08 02:52:24 +01:00
|
|
|
|
|
|
|
if(size & SHRINK_TO_FIT)
|
|
|
|
ret.shrink_to_fit();
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-08 02:52:24 +01:00
|
|
|
std::string
|
|
|
|
ircd::util::string(const const_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(data(buf), size(buf));
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
std::string
|
|
|
|
ircd::util::string(const char *const &buf,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
return std::string{buf, size};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::util::string(const uint8_t *const &buf,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
return string(reinterpret_cast<const char *>(buf), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/timer.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::util::timer::timer(const std::function<void ()> &func)
|
|
|
|
:timer{}
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::util::timer::stop()
|
|
|
|
{
|
2020-09-10 20:42:53 +02:00
|
|
|
const auto now
|
|
|
|
{
|
|
|
|
!stopped()?
|
|
|
|
clock::now():
|
|
|
|
start
|
|
|
|
};
|
2018-10-25 22:39:41 +02:00
|
|
|
|
2020-09-10 20:42:53 +02:00
|
|
|
const auto elapsed
|
|
|
|
{
|
|
|
|
duration_cast<decltype(accumulator)>(now - start)
|
|
|
|
};
|
|
|
|
|
|
|
|
accumulator += elapsed;
|
2018-10-25 22:39:41 +02:00
|
|
|
start = clock::time_point::min();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::util::timer::cont()
|
|
|
|
{
|
2020-09-10 20:42:53 +02:00
|
|
|
const auto now
|
2018-10-25 22:39:41 +02:00
|
|
|
{
|
2020-09-10 20:42:53 +02:00
|
|
|
clock::now()
|
|
|
|
};
|
2018-10-25 22:39:41 +02:00
|
|
|
|
2020-09-10 20:42:53 +02:00
|
|
|
const auto elapsed
|
|
|
|
{
|
|
|
|
!stopped()?
|
|
|
|
duration_cast<decltype(accumulator)>(now - start):
|
|
|
|
decltype(accumulator)(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
accumulator += elapsed;
|
|
|
|
start = now;
|
2018-10-25 22:39:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 00:51:36 +02:00
|
|
|
std::string
|
|
|
|
ircd::util::timer::pretty(const int &fmt)
|
|
|
|
const
|
|
|
|
{
|
2020-09-10 20:42:53 +02:00
|
|
|
return util::pretty(at<nanoseconds>(), fmt);
|
2019-04-28 00:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::timer::pretty(const mutable_buffer &out,
|
|
|
|
const int &fmt)
|
|
|
|
const
|
|
|
|
{
|
2020-09-10 20:42:53 +02:00
|
|
|
return util::pretty(out, at<nanoseconds>(), fmt);
|
2018-10-25 22:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/u2a.h
|
2018-10-21 09:50:41 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::util::u2a(const const_buffer &in)
|
|
|
|
{
|
|
|
|
return string(size(in) * 2, [&in]
|
|
|
|
(const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
return u2a(out, in);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::u2a(const mutable_buffer &out,
|
|
|
|
const const_buffer &in)
|
|
|
|
{
|
|
|
|
char *p(data(out));
|
|
|
|
for(size_t i(0); i < size(in) && p + 2 <= end(out); ++i)
|
|
|
|
{
|
|
|
|
char tmp[3];
|
2019-01-19 22:42:05 +01:00
|
|
|
::snprintf(tmp, sizeof(tmp), "%02x", uint8_t(in[i]));
|
2018-10-21 09:50:41 +02:00
|
|
|
*p++ = tmp[0];
|
|
|
|
*p++ = tmp[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return { data(out), p };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::util::a2u(const mutable_buffer &out,
|
|
|
|
const const_buffer &in)
|
|
|
|
{
|
|
|
|
const size_t len{size(in) / 2};
|
|
|
|
for(size_t i(0); i < len; ++i)
|
|
|
|
{
|
|
|
|
const char gl[3]
|
|
|
|
{
|
|
|
|
in[i * 2],
|
|
|
|
in[i * 2 + 1],
|
|
|
|
'\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
out[i] = strtol(gl, nullptr, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { data(out), len };
|
|
|
|
}
|
2018-10-25 22:39:41 +02:00
|
|
|
|
2019-04-18 14:46:07 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/unwind.h
|
|
|
|
//
|
|
|
|
|
2019-12-03 22:05:32 +01:00
|
|
|
ircd::util::unwind_defer::~unwind_defer()
|
2019-04-18 14:46:07 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2020-12-20 14:13:18 +01:00
|
|
|
static ios::descriptor descriptor
|
2019-04-18 14:46:07 +02:00
|
|
|
{
|
2020-12-20 14:13:18 +01:00
|
|
|
"ircd.unwind"
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::dispatch
|
|
|
|
{
|
|
|
|
descriptor, ios::defer, std::move(func)
|
2019-04-18 14:46:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:39:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// util/what.h
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Get what() from exception_ptr
|
|
|
|
///
|
|
|
|
ircd::string_view
|
|
|
|
ircd::util::what(const std::exception_ptr eptr)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(likely(eptr))
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
return e.what();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|