diff --git a/velox/exec/Task.cpp b/velox/exec/Task.cpp index ddbd33238c5..2b022608e3f 100644 --- a/velox/exec/Task.cpp +++ b/velox/exec/Task.cpp @@ -1697,6 +1697,11 @@ ContinueFuture Task::terminate(TaskState terminalState) { if (taskStats_.executionEndTimeMs == 0) { taskStats_.executionEndTimeMs = getCurrentTimeMs(); } + if (taskStats_.terminationTimeMs == 0) { + // In case terminate gets called multiple times somehow, + // this represents the first time. + taskStats_.terminationTimeMs = getCurrentTimeMs(); + } if (not isRunningLocked()) { return makeFinishFutureLocked("Task::terminate"); } @@ -1944,6 +1949,14 @@ uint64_t Task::timeSinceEndMs() const { return getCurrentTimeMs() - taskStats_.executionEndTimeMs; } +uint64_t Task::timeSinceTerminationMs() const { + std::lock_guard l(mutex_); + if (taskStats_.terminationTimeMs == 0UL) { + return 0UL; + } + return getCurrentTimeMs() - taskStats_.terminationTimeMs; +} + void Task::onTaskCompletion() { listeners().withRLock([&](auto& listeners) { if (listeners.empty()) { diff --git a/velox/exec/Task.h b/velox/exec/Task.h index 7180875c41c..75e5b64afbe 100644 --- a/velox/exec/Task.h +++ b/velox/exec/Task.h @@ -266,6 +266,10 @@ class Task : public std::enable_shared_from_this { /// Returns time (ms) since the task execution ended or zero, if not finished. uint64_t timeSinceEndMs() const; + /// Returns time (ms) since the task was terminated or zero, if not terminated + /// yet. + uint64_t timeSinceTerminationMs() const; + /// Returns the total number of drivers in the output pipeline, e.g. the /// pipeline that produces the results. uint32_t numOutputDrivers() const { diff --git a/velox/exec/TaskStats.h b/velox/exec/TaskStats.h index 5ce1a4654e0..4df28b303f8 100644 --- a/velox/exec/TaskStats.h +++ b/velox/exec/TaskStats.h @@ -73,6 +73,11 @@ struct TaskStats { /// and results have been consumed. uint64_t endTimeMs{0}; + /// Epoch time (ms) when the task was terminated, i.e. its terminal state + /// has been set, whether by finishing successfully or with an error, or + /// being cancelled or aborted. + uint64_t terminationTimeMs{0}; + /// Total number of drivers. uint64_t numTotalDrivers{0}; /// The number of completed drivers (which slots are null in Task 'drivers_'