This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new ec5bc4919cd [improve](profile) Check profile counter should always gt
than 0 (#62475)
ec5bc4919cd is described below
commit ec5bc4919cda7eccc38c7c4b1fddb9568f9b661d
Author: TengJianPing <[email protected]>
AuthorDate: Tue Apr 28 12:54:59 2026 +0800
[improve](profile) Check profile counter should always gt than 0 (#62475)
---
be/src/common/daemon.cpp | 1 +
be/src/exec/scan/scanner.h | 21 +++----
be/src/exec/scan/scanner_scheduler.cpp | 13 ++---
be/src/exec/sort/heap_sorter.h | 2 +-
be/src/format/orc/vorc_reader.h | 6 +-
be/src/format/parquet/vparquet_reader.h | 2 +-
be/src/runtime/cache/result_node.h | 4 +-
be/src/runtime/memory/memory_reclamation.cpp | 14 ++++-
be/src/runtime/runtime_profile.h | 85 +++++++++++++++++++++++++++-
be/src/util/stopwatch.hpp | 70 ++++++++++++++++++++---
10 files changed, 176 insertions(+), 42 deletions(-)
diff --git a/be/src/common/daemon.cpp b/be/src/common/daemon.cpp
index 74c375ffdd2..54986277356 100644
--- a/be/src/common/daemon.cpp
+++ b/be/src/common/daemon.cpp
@@ -328,6 +328,7 @@ void memory_gc() {
}
void Daemon::memory_maintenance_thread() {
+ doris::enable_profile_counter_check = 0;
while (!_stop_background_threads_latch.wait_for(
std::chrono::milliseconds(config::memory_maintenance_sleep_time_ms))) {
// step 1. Refresh process memory metrics.
diff --git a/be/src/exec/scan/scanner.h b/be/src/exec/scan/scanner.h
index 14d1b9e734c..ee668b56fb2 100644
--- a/be/src/exec/scan/scanner.h
+++ b/be/src/exec/scan/scanner.h
@@ -128,13 +128,6 @@ protected:
Status _do_projections(Block* origin_block, Block* output_block);
private:
- // Call start_wait_worker_timer() when submit the scanner to the thread
pool.
- // And call update_wait_worker_timer() when it is actually being executed.
- void _start_wait_worker_timer() {
- _watch.reset();
- _watch.start();
- }
-
void _start_scan_cpu_timer() {
_cpu_watch.reset();
_cpu_watch.start();
@@ -144,19 +137,21 @@ private:
void _update_scan_cpu_timer();
public:
+ // Call start_wait_worker_timer() when submit the scanner to the thread
pool.
+ // And call update_wait_worker_timer() when it is actually being executed.
+ void start_wait_worker_timer() {
+ _watch.reset();
+ _watch.start();
+ }
+
void resume() {
_update_wait_worker_timer();
_start_scan_cpu_timer();
}
void pause() {
_update_scan_cpu_timer();
- _start_wait_worker_timer();
+ start_wait_worker_timer();
}
- // Called when submitting the scanner to the thread pool queue.
- // Only starts the wait timer without touching the CPU timer, because the
CPU
- // timer uses CLOCK_THREAD_CPUTIME_ID which must be read on the same thread
- // that started it.
- void start_queue_wait() { _start_wait_worker_timer(); }
int64_t get_time_cost_ns() const { return _per_scanner_timer; }
int64_t projection_time() const { return _projection_timer; }
diff --git a/be/src/exec/scan/scanner_scheduler.cpp
b/be/src/exec/scan/scanner_scheduler.cpp
index 7abe1929b1f..b61f26e2c88 100644
--- a/be/src/exec/scan/scanner_scheduler.cpp
+++ b/be/src/exec/scan/scanner_scheduler.cpp
@@ -68,7 +68,10 @@ Status
ScannerScheduler::submit(std::shared_ptr<ScannerContext> ctx,
}
scan_task->set_state(ScanTask::State::IN_FLIGHT);
- scanner_delegate->_scanner->start_queue_wait();
+ // Only starts the wait timer without touching the CPU timer, because the
CPU
+ // timer uses CLOCK_THREAD_CPUTIME_ID which must be read on the same thread
+ // that started it.
+ scanner_delegate->_scanner->start_wait_worker_timer();
TabletStorageType type = scanner_delegate->_scanner->get_storage_type();
auto sumbit_task = [&]() {
auto work_func = [scanner_ref = scan_task, ctx]() {
@@ -164,13 +167,9 @@ void
ScannerScheduler::_scanner_scan(std::shared_ptr<ScannerContext> ctx,
max_run_time_watch.start();
scanner->resume();
- bool need_update_profile = true;
auto update_scanner_profile = [&]() {
- if (need_update_profile) {
- scanner->pause();
- scanner->update_realtime_counters();
- need_update_profile = false;
- }
+ scanner->pause();
+ scanner->update_realtime_counters();
};
Status status = Status::OK();
diff --git a/be/src/exec/sort/heap_sorter.h b/be/src/exec/sort/heap_sorter.h
index de3cfd53ea7..f520230d486 100644
--- a/be/src/exec/sort/heap_sorter.h
+++ b/be/src/exec/sort/heap_sorter.h
@@ -50,7 +50,7 @@ public:
private:
void _do_filter(MergeSortCursorImpl& block, size_t num_rows);
- size_t _data_size = 0;
+ int64_t _data_size = 0;
size_t _heap_size = 0;
size_t _queue_row_num = 0;
MergeSorterQueue _queue;
diff --git a/be/src/format/orc/vorc_reader.h b/be/src/format/orc/vorc_reader.h
index 1cf03a4b593..6972954e2ee 100644
--- a/be/src/format/orc/vorc_reader.h
+++ b/be/src/format/orc/vorc_reader.h
@@ -796,7 +796,7 @@ private:
bool _enable_filter_by_min_max = true;
std::vector<DecimalScaleParams> _decimal_scale_params;
- size_t _decimal_scale_params_index;
+ size_t _decimal_scale_params_index = 0;
protected:
bool _is_acid = false;
@@ -973,8 +973,8 @@ private:
io::FileReaderSPtr _tracing_file_reader;
bool _is_all_tiny_stripes = false;
- int64_t _orc_once_max_read_bytes;
- int64_t _orc_max_merge_distance_bytes;
+ int64_t _orc_once_max_read_bytes = 0;
+ int64_t _orc_max_merge_distance_bytes = 0;
std::vector<std::shared_ptr<StripeStreamInputStream>> _stripe_streams;
diff --git a/be/src/format/parquet/vparquet_reader.h
b/be/src/format/parquet/vparquet_reader.h
index fd6785b2600..d8a59f5d528 100644
--- a/be/src/format/parquet/vparquet_reader.h
+++ b/be/src/format/parquet/vparquet_reader.h
@@ -374,7 +374,7 @@ private:
RowGroupReader::RowGroupIndex _current_row_group_index {-1, 0, 0};
// read to the end of current reader
bool _row_group_eof = true;
- size_t _total_groups; // num of groups(stripes) of a parquet(orc) file
+ size_t _total_groups = 0; // num of groups(stripes) of a parquet(orc) file
std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
diff --git a/be/src/runtime/cache/result_node.h
b/be/src/runtime/cache/result_node.h
index 10aff6c0d04..261bc943a00 100644
--- a/be/src/runtime/cache/result_node.h
+++ b/be/src/runtime/cache/result_node.h
@@ -93,7 +93,7 @@ private:
private:
int64_t _partition_key;
PCacheValue* _cache_value = nullptr;
- size_t _data_size;
+ int64_t _data_size;
CacheStat _cache_stat;
};
@@ -175,7 +175,7 @@ private:
UniqueId _sql_key;
ResultNode* _prev = nullptr;
ResultNode* _next = nullptr;
- size_t _data_size;
+ int64_t _data_size;
PartitionRowBatchList _partition_list;
PartitionRowBatchMap _partition_map;
};
diff --git a/be/src/runtime/memory/memory_reclamation.cpp
b/be/src/runtime/memory/memory_reclamation.cpp
index 97458d3911d..2b310a7539e 100644
--- a/be/src/runtime/memory/memory_reclamation.cpp
+++ b/be/src/runtime/memory/memory_reclamation.cpp
@@ -17,6 +17,7 @@
#include "runtime/memory/memory_reclamation.h"
+#include <algorithm>
#include <unordered_map>
#include "runtime/exec_env.h"
@@ -117,8 +118,12 @@ int64_t MemoryReclamation::revoke_tasks_memory(
} else {
keep_wait_cancelling_tasks.push_back(
resource_ctx->task_controller()->debug_string());
- COUNTER_UPDATE(freed_memory_counter,
-
resource_ctx->memory_context()->current_memory_bytes());
+ // Memory tracker consumption can be slightly negative due
to
+ // concurrent batched tracking; clamp to 0 for freed
memory accounting.
+ COUNTER_UPDATE(
+ freed_memory_counter,
+ std::max(int64_t(0),
+
resource_ctx->memory_context()->current_memory_bytes()));
}
is_filtered = true;
}
@@ -157,8 +162,11 @@ int64_t MemoryReclamation::revoke_tasks_memory(
if (ActionFuncImpl[action](resource_ctx.get(),
Status::MemoryLimitExceeded(task_revoke_reason))) {
this_time_revoked_tasks.push_back(resource_ctx->task_controller()->debug_string());
+ // Memory tracker consumption can be slightly negative due to
+ // concurrent batched tracking; clamp to 0 for freed memory
accounting.
COUNTER_UPDATE(freed_memory_counter,
-
resource_ctx->memory_context()->current_memory_bytes());
+ std::max(int64_t(0),
+
resource_ctx->memory_context()->current_memory_bytes()));
COUNTER_UPDATE(this_time_revoked_tasks_counter, 1);
if (freed_memory_counter->value() > need_free_mem) {
break;
diff --git a/be/src/runtime/runtime_profile.h b/be/src/runtime/runtime_profile.h
index d539ebe390a..c5fe4aeb78d 100644
--- a/be/src/runtime/runtime_profile.h
+++ b/be/src/runtime/runtime_profile.h
@@ -49,7 +49,7 @@ namespace doris {
class TRuntimeProfileNode;
class TRuntimeProfileTree;
class RuntimeProfileCounterTreeNode;
-
+inline thread_local bool enable_profile_counter_check = true;
// Some macro magic to generate unique ids using __COUNTER__
#define CONCAT_IMPL(x, y) x##y
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
@@ -189,15 +189,65 @@ public:
virtual Counter* clone() const { return new Counter(type(), value(),
_level); }
- virtual void update(int64_t delta) { _value.fetch_add(delta,
std::memory_order_relaxed); }
+ virtual void update(int64_t delta) {
+#ifndef NDEBUG
+ int64_t prev_value = _value.load(std::memory_order_seq_cst);
+ // Using memory_order_seq_cst to make sure no concurrency issues,
it may affect
+ // performance. So that only in debug mode, we check the counter
value.
+ _value.fetch_add(delta, std::memory_order_seq_cst);
+#else
+ _value.fetch_add(delta, std::memory_order_relaxed);
+#endif
+#ifndef NDEBUG
+ (void)prev_value;
+#if !defined(BE_TEST)
+ if (enable_profile_counter_check) {
+ if (delta < 0) {
+ DCHECK_GT(_value.load(std::memory_order_seq_cst), -1L)
+ << " delta: " << delta << " prev_value: " <<
prev_value;
+ }
+ }
+#endif
+#endif
+ }
void bit_or(int64_t delta) { _value.fetch_or(delta,
std::memory_order_relaxed); }
- virtual void set(int64_t value) { _value.store(value,
std::memory_order_relaxed); }
+ virtual void set(int64_t value) {
+#ifndef NDEBUG
+ int64_t prev_value = _value.load(std::memory_order_seq_cst);
+ _value.store(value, std::memory_order_seq_cst);
+#else
+ _value.store(value, std::memory_order_relaxed);
+#endif
+#ifndef NDEBUG
+ (void)prev_value;
+#if !defined(BE_TEST)
+ if (enable_profile_counter_check) {
+ DCHECK_GT(_value.load(std::memory_order_seq_cst), -1L)
+ << " new value: " << value << " prev_value: " <<
prev_value;
+ }
+#endif
+#endif
+ }
virtual void set(double value) {
DCHECK_EQ(sizeof(value), sizeof(int64_t));
+#ifndef NDEBUG
+ int64_t prev_value = _value.load(std::memory_order_seq_cst);
+ _value.store(binary_cast<double, int64_t>(value),
std::memory_order_seq_cst);
+#else
_value.store(binary_cast<double, int64_t>(value),
std::memory_order_relaxed);
+#endif
+#ifndef NDEBUG
+ (void)prev_value;
+#if !defined(BE_TEST)
+ if (enable_profile_counter_check) {
+ DCHECK_GT(_value.load(std::memory_order_seq_cst), -1L)
+ << " new value: " << value << " prev_value: " <<
prev_value;
+ }
+#endif
+#endif
}
virtual int64_t value() const { return
_value.load(std::memory_order_relaxed); }
@@ -265,10 +315,20 @@ public:
}
void add(int64_t delta) {
+#ifndef NDEBUG
+ current_value_.fetch_add(delta, std::memory_order_seq_cst);
+ if (delta > 0) {
+ UpdateMax(current_value_);
+ }
+ //if (enable_profile_counter_check) {
+ // DCHECK_GT(current_value_.load(std::memory_order_seq_cst),
-1L);
+ //}
+#else
current_value_.fetch_add(delta, std::memory_order_relaxed);
if (delta > 0) {
UpdateMax(current_value_);
}
+#endif
}
virtual void update(int64_t delta) override { add(delta); }
@@ -328,8 +388,27 @@ public:
}
void set(int64_t v) override {
+#ifndef NDEBUG
+ int64_t prev_value =
current_value_.load(std::memory_order_seq_cst);
+ int64_t prev_max_value = _value.load(std::memory_order_seq_cst);
+ current_value_.store(v, std::memory_order_seq_cst);
+#else
current_value_.store(v, std::memory_order_relaxed);
+#endif
UpdateMax(v);
+#ifndef NDEBUG
+ (void)prev_value;
+ (void)prev_max_value;
+#if !defined(BE_TEST)
+
+ if (enable_profile_counter_check) {
+ DCHECK_GT(current_value_.load(std::memory_order_seq_cst), -1L)
+ << " prev_value: " << prev_value;
+ DCHECK_GT(_value.load(std::memory_order_seq_cst), -1L)
+ << " prev_max_value: " << prev_max_value << "
prev_value: " << prev_value;
+ }
+#endif
+#endif
}
int64_t current_value() const { return
current_value_.load(std::memory_order_relaxed); }
diff --git a/be/src/util/stopwatch.hpp b/be/src/util/stopwatch.hpp
index f0f9442bcf5..700f32f1f92 100644
--- a/be/src/util/stopwatch.hpp
+++ b/be/src/util/stopwatch.hpp
@@ -22,6 +22,12 @@
#include <time.h>
+#include <ctime>
+#include <sstream>
+#include <thread>
+
+#include "util/time.h"
+
namespace doris {
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
@@ -45,6 +51,7 @@ public:
void start() {
if (!_running) {
+ _record_start_thread_id();
clock_gettime(Clock, &_start);
_running = true;
}
@@ -58,10 +65,11 @@ public:
}
// Restarts the timer. Returns the elapsed time until this point.
- uint64_t reset() {
- uint64_t ret = elapsed_time();
+ int64_t reset() {
+ int64_t ret = elapsed_time();
if (_running) {
+ _record_start_thread_id();
clock_gettime(Clock, &_start);
}
@@ -69,35 +77,79 @@ public:
}
// Returns time in nanosecond.
- uint64_t elapsed_time() const {
+ // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
+ int64_t elapsed_time() const {
if (!_running) {
return _total_time;
}
+ _check_thread_id();
timespec end;
clock_gettime(Clock, &end);
- return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
- (end.tv_nsec - _start.tv_nsec);
+ auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
+ auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
+ if (end_nanos < start_nanos) {
+ LOG(INFO) << "WARNING: time went backwards from " << start_nanos
<< " to " << end_nanos;
+ return 0;
+ }
+ return end_nanos - start_nanos;
}
// Return time in microseconds
- uint64_t elapsed_time_microseconds() const { return elapsed_time() / 1000;
}
+ int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
// Return time in milliseconds
- uint64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000
/ 1000; }
+ int64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 /
1000; }
- // Returns time in nanosecond.
+ // Returns time in seconds.
+ // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
int64_t elapsed_time_seconds(timespec end) const {
if (!_running) {
return _total_time / 1000L / 1000L / 1000L;
}
+ if (end.tv_sec < _start.tv_sec) {
+ auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
+ auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
+ LOG(INFO) << "WARNING: time went backwards from " << start_nanos
<< " to " << end_nanos;
+ return 0;
+ }
return end.tv_sec - _start.tv_sec;
}
private:
+ static std::string _get_thread_id() {
+ std::stringstream ss;
+ ss << std::this_thread::get_id();
+ return ss.str();
+ }
+ void _record_start_thread_id() {
+#ifndef NDEBUG
+ if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
+ // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms,
e.g. macOS.
+ // So that we need to check it at runtime and fallback to
CLOCK_MONOTONIC if it is not supported.
+ _start_thread_id = _get_thread_id();
+ }
+#endif
+ }
+
+ void _check_thread_id() const {
+#ifndef NDEBUG
+ if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
+ auto current_thread_id = _get_thread_id();
+ if (current_thread_id != _start_thread_id) {
+ LOG(WARNING) << "StopWatch started in thread " <<
_start_thread_id
+ << " but stopped in thread " << current_thread_id;
+ }
+ }
+#endif
+ }
+
timespec _start;
- uint64_t _total_time; // in nanosec
+ int64_t _total_time; // in nanosec
bool _running;
+#ifndef NDEBUG
+ std::string _start_thread_id;
+#endif
};
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]