2021-05-13 22:56:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-15 15:45:43 +00:00
|
|
|
#include <unordered_map>
|
2021-09-14 11:06:00 +00:00
|
|
|
#include <unordered_set>
|
2022-05-31 08:05:35 +00:00
|
|
|
#include <mutex>
|
2021-05-13 22:56:42 +00:00
|
|
|
#include <IO/Progress.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-09-14 11:06:00 +00:00
|
|
|
#include <base/types.h>
|
2021-05-13 22:56:42 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
|
|
|
|
|
|
|
|
/// http://en.wikipedia.org/wiki/ANSI_escape_code
|
|
|
|
#define CLEAR_TO_END_OF_LINE "\033[K"
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-09-17 16:47:54 +00:00
|
|
|
struct ThreadEventData
|
2021-09-17 15:00:13 +00:00
|
|
|
{
|
2022-06-14 21:36:16 +00:00
|
|
|
UInt64 time() const noexcept { return user_ms + system_ms; }
|
2021-09-17 15:00:13 +00:00
|
|
|
|
2022-06-14 21:36:16 +00:00
|
|
|
UInt64 user_ms = 0;
|
|
|
|
UInt64 system_ms = 0;
|
|
|
|
UInt64 memory_usage = 0;
|
2021-09-17 15:00:13 +00:00
|
|
|
};
|
|
|
|
|
2021-09-17 16:47:54 +00:00
|
|
|
using ThreadIdToTimeMap = std::unordered_map<UInt64, ThreadEventData>;
|
2021-09-17 15:00:13 +00:00
|
|
|
using HostToThreadTimesMap = std::unordered_map<String, ThreadIdToTimeMap>;
|
|
|
|
|
2021-05-13 22:56:42 +00:00
|
|
|
class ProgressIndication
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Write progress to stderr.
|
|
|
|
void writeProgress();
|
|
|
|
|
|
|
|
void writeFinalProgress();
|
|
|
|
|
|
|
|
/// Clear stderr output.
|
|
|
|
void clearProgressOutput();
|
|
|
|
|
|
|
|
/// Reset progress values.
|
|
|
|
void resetProgress();
|
|
|
|
|
|
|
|
/// Update Progress object. It can be updated from:
|
|
|
|
/// 1. onProgress in clickhouse-client;
|
|
|
|
/// 2. ProgressCallback via setProgressCallback methrod in:
|
|
|
|
/// - context (used in clickhouse-local, can also be added in arbitrary place)
|
2022-05-20 19:49:31 +00:00
|
|
|
/// - ISource (also in streams)
|
2021-05-13 22:56:42 +00:00
|
|
|
/// - readBufferFromFileDescriptor (for file processing progress)
|
|
|
|
bool updateProgress(const Progress & value);
|
|
|
|
|
|
|
|
/// In some cases there is a need to update progress value, when there is no access to progress_inidcation object.
|
|
|
|
/// In this case it is added via context.
|
|
|
|
/// `write_progress_on_update` is needed to write progress for loading files data via pipe in non-interactive mode.
|
2021-06-01 07:56:20 +00:00
|
|
|
void setFileProgressCallback(ContextMutablePtr context, bool write_progress_on_update = false);
|
2021-05-13 22:56:42 +00:00
|
|
|
|
|
|
|
/// How much seconds passed since query execution start.
|
2021-06-21 12:19:25 +00:00
|
|
|
double elapsedSeconds() const { return watch.elapsedSeconds(); }
|
2021-05-13 22:56:42 +00:00
|
|
|
|
2021-09-15 15:45:43 +00:00
|
|
|
void addThreadIdToList(String const & host, UInt64 thread_id);
|
2021-09-14 11:06:00 +00:00
|
|
|
|
2021-11-08 13:38:31 +00:00
|
|
|
void updateThreadEventData(HostToThreadTimesMap & new_thread_data, UInt64 elapsed_time);
|
2021-09-14 11:06:00 +00:00
|
|
|
|
2021-05-13 22:56:42 +00:00
|
|
|
private:
|
2021-09-15 15:45:43 +00:00
|
|
|
size_t getUsedThreadsCount() const;
|
2021-09-14 13:24:57 +00:00
|
|
|
|
2021-12-28 22:20:34 +00:00
|
|
|
double getCPUUsage() const;
|
2021-09-14 13:24:57 +00:00
|
|
|
|
2021-10-13 13:26:54 +00:00
|
|
|
struct MemoryUsage
|
|
|
|
{
|
|
|
|
UInt64 total = 0;
|
|
|
|
UInt64 max = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
MemoryUsage getMemoryUsage() const;
|
2021-09-17 16:47:54 +00:00
|
|
|
|
2021-06-17 21:24:28 +00:00
|
|
|
/// This flag controls whether to show the progress bar. We start showing it after
|
|
|
|
/// the query has been executing for 0.5 seconds, and is still less than half complete.
|
2021-05-13 22:56:42 +00:00
|
|
|
bool show_progress_bar = false;
|
|
|
|
|
|
|
|
/// Width of how much has been printed currently into stderr. Used to define size of progress bar and
|
|
|
|
/// to check whether progress output needs to be cleared.
|
|
|
|
size_t written_progress_chars = 0;
|
|
|
|
|
|
|
|
/// The server periodically sends information about how much data was read since last time.
|
|
|
|
/// This information is stored here.
|
|
|
|
Progress progress;
|
|
|
|
|
|
|
|
/// Track query execution time.
|
|
|
|
Stopwatch watch;
|
2021-05-14 08:35:51 +00:00
|
|
|
|
|
|
|
bool write_progress_on_update = false;
|
2021-09-14 11:06:00 +00:00
|
|
|
|
2021-12-28 22:20:34 +00:00
|
|
|
std::unordered_map<String, double> host_cpu_usage;
|
2021-09-17 16:47:54 +00:00
|
|
|
HostToThreadTimesMap thread_data;
|
2022-05-31 08:05:35 +00:00
|
|
|
/// In case of all of the above:
|
|
|
|
/// - clickhouse-local
|
|
|
|
/// - input_format_parallel_parsing=true
|
|
|
|
/// - write_progress_on_update=true
|
|
|
|
///
|
|
|
|
/// It is possible concurrent access to the following:
|
|
|
|
/// - writeProgress() (class properties) (guarded with progress_mutex)
|
|
|
|
/// - thread_data/host_cpu_usage (guarded with profile_events_mutex)
|
|
|
|
mutable std::mutex profile_events_mutex;
|
|
|
|
mutable std::mutex progress_mutex;
|
2021-05-13 22:56:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|