Miscellaneous #2482

This commit is contained in:
Alexey Milovidov 2018-08-17 21:57:07 +03:00
parent aef490d173
commit 1c4f5d3359
5 changed files with 14 additions and 22 deletions

View File

@ -56,7 +56,7 @@ public:
static void attachQueryContext(Context & query_context); static void attachQueryContext(Context & query_context);
/// You must call one of these methods when create a query child thread: /// You must call one of these methods when create a query child thread:
/// Add current thread to a group associated with thr thread group /// Add current thread to a group associated with the thread group
static void attachTo(const ThreadGroupStatusPtr & thread_group); static void attachTo(const ThreadGroupStatusPtr & thread_group);
/// Is useful for a ThreadPool tasks /// Is useful for a ThreadPool tasks
static void attachToIfDetached(const ThreadGroupStatusPtr & thread_group); static void attachToIfDetached(const ThreadGroupStatusPtr & thread_group);

View File

@ -59,9 +59,10 @@ namespace ProfileEvents
} while (current != nullptr); } while (current != nullptr);
} }
/// Every single value is fetched atomically, but not all values as a whole.
Counters getPartiallyAtomicSnapshot() const; Counters getPartiallyAtomicSnapshot() const;
/// Reset metrics and parent /// Reset all counters to zero and reset parent.
void reset(); void reset();
/// Get parent (thread unsafe) /// Get parent (thread unsafe)

View File

@ -174,7 +174,7 @@ bool TaskStatsInfoGetter::getStatImpl(int tid, ::taskstats & out_stats, bool thr
{ {
::nlmsgerr * err = static_cast<::nlmsgerr *>(NLMSG_DATA(&msg)); ::nlmsgerr * err = static_cast<::nlmsgerr *>(NLMSG_DATA(&msg));
if (throw_on_error) if (throw_on_error)
throw Exception("Can't get Netlink response, error=" + std::to_string(err->error), ErrorCodes::NETLINK_ERROR); throw Exception("Can't get Netlink response, error: " + std::to_string(err->error), ErrorCodes::NETLINK_ERROR);
else else
return false; return false;
} }
@ -198,16 +198,16 @@ bool TaskStatsInfoGetter::getStatImpl(int tid, ::taskstats & out_stats, bool thr
{ {
if (na->nla_type == TASKSTATS_TYPE_STATS) if (na->nla_type == TASKSTATS_TYPE_STATS)
{ {
::taskstats *ts = static_cast<::taskstats *>(NLA_DATA(na)); ::taskstats * ts = static_cast<::taskstats *>(NLA_DATA(na));
out_stats = *ts; out_stats = *ts;
} }
len2 += NLA_ALIGN(na->nla_len); len2 += NLA_ALIGN(na->nla_len);
na = reinterpret_cast<::nlattr *>((char *) na + len2); na = reinterpret_cast<::nlattr *>(reinterpret_cast<char *>(na) + len2);
} }
} }
na = reinterpret_cast<::nlattr *>((char *) GENLMSG_DATA(&msg) + len); na = reinterpret_cast<::nlattr *>(reinterpret_cast<char *>(GENLMSG_DATA(&msg)) + len);
} }
return true; return true;
@ -215,13 +215,13 @@ bool TaskStatsInfoGetter::getStatImpl(int tid, ::taskstats & out_stats, bool thr
void TaskStatsInfoGetter::getStat(::taskstats & stat, int tid) void TaskStatsInfoGetter::getStat(::taskstats & stat, int tid)
{ {
tid = tid < 0 ? getDefaultTid() : tid; tid = tid < 0 ? getDefaultTID() : tid;
getStatImpl(tid, stat, true); getStatImpl(tid, stat, true);
} }
bool TaskStatsInfoGetter::tryGetStat(::taskstats & stat, int tid) bool TaskStatsInfoGetter::tryGetStat(::taskstats & stat, int tid)
{ {
tid = tid < 0 ? getDefaultTid() : tid; tid = tid < 0 ? getDefaultTID() : tid;
return getStatImpl(tid, stat, false); return getStatImpl(tid, stat, false);
} }
@ -233,10 +233,11 @@ TaskStatsInfoGetter::~TaskStatsInfoGetter()
int TaskStatsInfoGetter::getCurrentTID() int TaskStatsInfoGetter::getCurrentTID()
{ {
/// This call is always successful. - man gettid
return static_cast<int>(syscall(SYS_gettid)); return static_cast<int>(syscall(SYS_gettid));
} }
int TaskStatsInfoGetter::getDefaultTid() int TaskStatsInfoGetter::getDefaultTID()
{ {
if (default_tid < 0) if (default_tid < 0)
default_tid = getCurrentTID(); default_tid = getCurrentTID();

View File

@ -10,11 +10,10 @@ namespace DB
class Exception; class Exception;
/// Get taskstat infor from OS kernel via Netlink protocol /// Get taskstat info from OS kernel via Netlink protocol.
class TaskStatsInfoGetter class TaskStatsInfoGetter
{ {
public: public:
TaskStatsInfoGetter(); TaskStatsInfoGetter();
TaskStatsInfoGetter(const TaskStatsInfoGetter &) = delete; TaskStatsInfoGetter(const TaskStatsInfoGetter &) = delete;
@ -30,9 +29,8 @@ public:
static bool checkProcessHasRequiredPermissions(); static bool checkProcessHasRequiredPermissions();
private: private:
/// Caches current thread tid to avoid extra sys calls /// Caches current thread tid to avoid extra sys calls
int getDefaultTid(); int getDefaultTID();
int default_tid = -1; int default_tid = -1;
bool getStatImpl(int tid, ::taskstats & out_stats, bool throw_on_error = false); bool getStatImpl(int tid, ::taskstats & out_stats, bool throw_on_error = false);

View File

@ -99,7 +99,7 @@ void do_io(size_t id)
get_info.getStat(stat, tid); get_info.getStat(stat, tid);
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
std::cerr << "#" << id << ", tid " << tid << ", step2\n" << stat << "\n"; std::cerr << "#" << id << ", tid " << tid << ", step3\n" << stat << "\n";
} }
Poco::File(path_dst).remove(false); Poco::File(path_dst).remove(false);
@ -113,16 +113,12 @@ void test_perf()
TaskStatsInfoGetter get_info; TaskStatsInfoGetter get_info;
rusage rusage; rusage rusage;
timespec ts;
constexpr size_t num_samples = 1000000; constexpr size_t num_samples = 1000000;
{ {
Stopwatch watch; Stopwatch watch;
for (size_t i = 0; i < num_samples; ++i) for (size_t i = 0; i < num_samples; ++i)
{
getrusage(RUSAGE_THREAD, &rusage); getrusage(RUSAGE_THREAD, &rusage);
clock_gettime(CLOCK_MONOTONIC, &ts);
}
auto ms = watch.elapsedMilliseconds(); auto ms = watch.elapsedMilliseconds();
if (ms > 0) if (ms > 0)
@ -151,13 +147,9 @@ try
ThreadPool pool(num_threads); ThreadPool pool(num_threads);
for (size_t i = 0; i < num_threads; ++i) for (size_t i = 0; i < num_threads; ++i)
pool.schedule([i]() { do_io(i); }); pool.schedule([i]() { do_io(i); });
pool.wait(); pool.wait();
test_perf(); test_perf();
return 0; return 0;
} }
catch (...) catch (...)