Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native] Move static functions from PrestoTask to Util. #22617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 2 additions & 35 deletions presto-native-execution/presto_cpp/main/PrestoTask.cpp
Expand Up @@ -272,7 +272,7 @@ PrestoTask::PrestoTask(
: id(taskId),
startProcessCpuTime{
_startProcessCpuTime > 0 ? _startProcessCpuTime
: getProcessCpuTime()} {
: util::getProcessCpuTime()} {
info.taskId = taskId;
info.nodeId = nodeId;
}
Expand All @@ -290,24 +290,12 @@ uint64_t PrestoTask::timeSinceLastHeartbeatMs() const {
return getCurrentTimeMs() - lastHeartbeatMs;
}

// static
long PrestoTask::getProcessCpuTime() {
struct rusage rusageEnd;
getrusage(RUSAGE_SELF, &rusageEnd);

auto tvNanos = [](struct timeval tv) {
return tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
};

return tvNanos(rusageEnd.ru_utime) + tvNanos(rusageEnd.ru_stime);
}

void PrestoTask::recordProcessCpuTime() {
if (processCpuTime_ > 0) {
return;
}

processCpuTime_ = getProcessCpuTime() - startProcessCpuTime;
processCpuTime_ = util::getProcessCpuTime() - startProcessCpuTime;
}

protocol::TaskStatus PrestoTask::updateStatusLocked() {
Expand Down Expand Up @@ -775,27 +763,6 @@ void PrestoTask::updateExecutionInfoLocked(
prestoTaskStats);
}

/*static*/ std::string PrestoTask::taskNumbersToString(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to keep this as static method of PrestoTask as the states are specific to PrestoTask internal

s/taskNumbersToString/taskStatesToString/

const std::array<size_t, 5>& taskNumbers) {
// Names of five TaskState (enum defined in exec/Task.h).
static constexpr std::array<folly::StringPiece, 5> taskStateNames{
"Running",
"Finished",
"Canceled",
"Aborted",
"Failed",
};

std::string str;
for (size_t i = 0; i < taskNumbers.size(); ++i) {
if (taskNumbers[i] != 0) {
folly::toAppend(
fmt::format("{}={} ", taskStateNames[i], taskNumbers[i]), &str);
}
}
return str;
}

folly::dynamic PrestoTask::toJson() const {
std::lock_guard<std::mutex> l(mutex);
folly::dynamic obj = folly::dynamic::object;
Expand Down
3 changes: 0 additions & 3 deletions presto-native-execution/presto_cpp/main/PrestoTask.h
Expand Up @@ -147,9 +147,6 @@ struct PrestoTask {
static std::string taskNumbersToString(
const std::array<size_t, 5>& taskNumbers);

/// Returns process-wide CPU time in nanoseconds.
static long getProcessCpuTime();

/// Invoked to update presto task status from the updated velox task stats.
protocol::TaskStatus updateStatusLocked();
protocol::TaskInfo updateInfoLocked();
Expand Down
2 changes: 1 addition & 1 deletion presto-native-execution/presto_cpp/main/TaskManager.cpp
Expand Up @@ -1112,7 +1112,7 @@ void TaskManager::shutdown() {
PRESTO_SHUTDOWN_LOG(INFO)
<< "Waited (" << seconds
<< " seconds so far) for 'Running' tasks to complete. " << numTasks
<< " tasks left: " << PrestoTask::taskNumbersToString(taskNumbers);
<< " tasks left: " << util::taskNumbersToString(taskNumbers);
std::this_thread::sleep_for(std::chrono::seconds(1));
taskNumbers = getTaskNumbers(numTasks);
++seconds;
Expand Down
3 changes: 2 additions & 1 deletion presto-native-execution/presto_cpp/main/TaskResource.cpp
Expand Up @@ -14,6 +14,7 @@
#include "presto_cpp/main/TaskResource.h"
#include <presto_cpp/main/common/Exception.h>
#include "presto_cpp/main/common/Configs.h"
#include "presto_cpp/main/common/Utils.h"
#include "presto_cpp/main/thrift/ProtocolToThrift.h"
#include "presto_cpp/main/thrift/ThriftIO.h"
#include "presto_cpp/main/thrift/gen-cpp2/PrestoThrift.h"
Expand Down Expand Up @@ -220,7 +221,7 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTaskImpl(
folly::via(
httpSrvCpuExecutor_,
[this, &body, taskId, createOrUpdateFunc]() {
const auto startProcessCpuTime = PrestoTask::getProcessCpuTime();
const auto startProcessCpuTime = util::getProcessCpuTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NYC: s/startProcessCpuTime/startProcessCpuTimeNs/


// TODO Avoid copy
std::ostringstream oss;
Expand Down
32 changes: 32 additions & 0 deletions presto-native-execution/presto_cpp/main/common/Utils.cpp
Expand Up @@ -14,6 +14,7 @@

#include "presto_cpp/main/common/Utils.h"
#include <fmt/format.h>
#include <sys/resource.h>

namespace facebook::presto::util {

Expand Down Expand Up @@ -43,4 +44,35 @@ std::shared_ptr<folly::SSLContext> createSSLContext(
}
}

long getProcessCpuTime() {
struct rusage rusageEnd;
getrusage(RUSAGE_SELF, &rusageEnd);

auto tvNanos = [](struct timeval tv) {
return tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
};

return tvNanos(rusageEnd.ru_utime) + tvNanos(rusageEnd.ru_stime);
}

std::string taskNumbersToString(const std::array<size_t, 5>& taskNumbers) {
// Names of five TaskState (enum defined in exec/Task.h).
static constexpr std::array<folly::StringPiece, 5> taskStateNames{
"Running",
"Finished",
"Canceled",
"Aborted",
"Failed",
};

std::string str;
for (size_t i = 0; i < taskNumbers.size(); ++i) {
if (taskNumbers[i] != 0) {
folly::toAppend(
fmt::format("{}={} ", taskStateNames[i], taskNumbers[i]), &str);
}
}
return str;
}

} // namespace facebook::presto::util
5 changes: 5 additions & 0 deletions presto-native-execution/presto_cpp/main/common/Utils.h
Expand Up @@ -31,4 +31,9 @@ std::shared_ptr<folly::SSLContext> createSSLContext(
const std::string& clientCertAndKeyPath,
const std::string& ciphers);

/// Returns process-wide CPU time in nanoseconds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to avoid files / classes named Util as they tend to become a "dumping ground" for lots of unrelated code. It would be better to come with more specific names.

long getProcessCpuTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/getProcessCpuTime/getProcessCpuTimeNs/?


std::string taskNumbersToString(const std::array<size_t, 5>& taskNumbers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const std::array<size_t, 5>& taskNumbers


} // namespace facebook::presto::util