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

Sanchda/add metrics #236

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor daemomnize
  • Loading branch information
sanchda committed Dec 17, 2022
commit 8700bae507b7bb64d92d17604a65e74467abd3c9
5 changes: 1 addition & 4 deletions include/daemonize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ struct DaemonizeResult {
// Daemonization function
// cleanup_function is a callable invoked in the context of the intermediate,
// short-lived process that will be killed by daemon process.
DaemonizeResult(std::function<void()> cleanup_function);

// Create an empty/error result
DaemonizeResult(bool _) { (void)_; };
bool daemonize(std::function<void()> cleanup_function);

private:
int pipe_read = -1;
Expand Down
13 changes: 6 additions & 7 deletions src/daemonize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "logger.hpp"

namespace ddprof {
DaemonizeResult::DaemonizeResult(std::function<void()> cleanup_function) {
bool DaemonizeResult::daemonize(std::function<void()> cleanup_function) {
if (!openpipes()) {
return;
return false;
}
pid_t parent_pid = getpid();
pid_t temp_pid = fork(); // "middle"/"child" (temporary) PID
Expand Down Expand Up @@ -38,20 +38,20 @@ DaemonizeResult::DaemonizeResult(std::function<void()> cleanup_function) {
state = DaemonizeState::Daemon;
invoker_pid = parent_pid;
daemon_pid = grandchild_pid;
return;
return true;
}
} else if (temp_pid != -1) { // parent PID enter branch
close(pipe_write);
pipe_write = -1;
if (read(pipe_read, &grandchild_pid, pid_sz) != pid_sz) {
return; // error
return false;
}

signal(SIGCHLD, SIG_IGN);
state = DaemonizeState::Invoker;
invoker_pid = parent_pid;
daemon_pid = grandchild_pid;
return;
return true;
}

// Should only arrive here if the first-level fork failed, but add a sink
Expand All @@ -66,7 +66,6 @@ DaemonizeResult::DaemonizeResult(std::function<void()> cleanup_function) {
close(pipe_write);
pipe_read = -1;
pipe_write = -1;
return;
return false;
}

} // namespace ddprof
6 changes: 3 additions & 3 deletions src/exe/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static int start_profiler_internal(DDProfContext *ctx, bool &is_profiler) {

const bool in_wrapper_mode = ctx->params.pid == 0;
TempFileHolder dd_profiling_lib_holder, dd_loader_lib_holder;
ddprof::DaemonizeResult daemonize_res{false};
ddprof::DaemonizeResult daemonize_res;

if (in_wrapper_mode) {
// If no PID was specified earlier, we autodaemonize and target current pid
Expand Down Expand Up @@ -259,10 +259,10 @@ static int start_profiler_internal(DDProfContext *ctx, bool &is_profiler) {
}

ctx->params.pid = getpid();
daemonize_res = {[ctx] {
daemonize_res.daemonize({[ctx] {
ctx->release();
throw ddprof::exit();
}};
}});

if (daemonize_res.is_failure()) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dd_profiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ int ddprof_start_profiling_internal() {
auto defer_parent_socket_close =
make_defer([&sockfds]() { close(sockfds[kParentIdx]); });

ddprof::DaemonizeResult daemonize_res{nullptr};
if (daemonize_res.is_failure()) {
ddprof::DaemonizeResult daemonize_res;
if (!daemonize_res.daemonize(nullptr)) {
return -1;
}

Expand Down