Skip to content

Commit

Permalink
Export gProfiler-needed functionality from asprof (#7)
Browse files Browse the repository at this point in the history
In AP 3.0, @Jongy edited this commit to export fdtransfer from asprof, as jattach
and jcmd were added in async-profiler@b8a60e6
to upstream.
  • Loading branch information
marcin-ol authored and Jongy committed Jan 27, 2024
1 parent 37963c0 commit 5faa3eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ build/%:
mkdir -p $@

build/$(LAUNCHER): src/launcher/* src/jattach/* src/fdtransfer.h
$(CC) $(CPPFLAGS) $(CFLAGS) -DPROFILER_VERSION=\"$(PROFILER_VERSION)\" -o $@ src/launcher/*.cpp src/jattach/*.c
$(CC) $(CPPFLAGS) $(CFLAGS) -static -DPROFILER_VERSION=\"$(PROFILER_VERSION)\" -o $@ src/launcher/*.cpp src/jattach/*.c
strip $@

PROFILER_FLAGS=-static-libgcc
Expand Down
4 changes: 2 additions & 2 deletions src/launcher/fdtransferServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FdTransferServer {
public:
static bool supported() { return true; }

static bool runOnce(int pid, const char *path);
static bool runOnce(int pid, const char *path, unsigned int timeout);
static bool runLoop(const char *path);
};

Expand All @@ -35,7 +35,7 @@ class FdTransferServer {
public:
static bool supported() { return false; }

static bool runOnce(int pid, const char *path) { return false; }
static bool runOnce(int pid, const char *path, unsigned int timeout) { return false; }
static bool runLoop(const char *path) { return false; }
};

Expand Down
4 changes: 2 additions & 2 deletions src/launcher/fdtransferServer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ bool FdTransferServer::sendFd(int fd, struct fd_response *resp, size_t resp_size
return true;
}

bool FdTransferServer::runOnce(int pid, const char *path) {
bool FdTransferServer::runOnce(int pid, const char *path, unsigned int timeout) {
// get its nspid prior to moving to its PID namespace.
int nspid;
uid_t target_uid;
Expand All @@ -276,7 +276,7 @@ bool FdTransferServer::runOnce(int pid, const char *path) {
}
}

if (!bindServer(&sun, addrlen, 30)) {
if (!bindServer(&sun, addrlen, timeout)) {
return false;
}

Expand Down
33 changes: 28 additions & 5 deletions src/launcher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static const char USAGE_STRING[] =
" jcmd run JVM diagnostic command (jattach action)\n"
" collect collect profile for the specified period of time\n"
" and then stop (default action)\n"
" fdtransfer start fdtransfer to serve perf requests on behalf of profiled process\n"
"\n"
"Options:\n"
" -e event profiling event: cpu|alloc|lock|cache-misses etc.\n"
" -d duration run profiling for <duration> seconds\n"
Expand Down Expand Up @@ -81,6 +83,7 @@ static const char USAGE_STRING[] =
" --jfrsync config synchronize profiler with JFR recording\n"
" --fdtransfer use fdtransfer to serve perf requests\n"
" from the non-privileged target\n"
" --fd-path string socket path for fdtransfer to bind to\n"
"\n"
"<pid> is a numeric process ID of the target JVM\n"
" or 'jps' keyword to find running JVM automatically\n"
Expand All @@ -91,6 +94,7 @@ static const char USAGE_STRING[] =
" " APP_BINARY " stop -o flat jps\n"
" " APP_BINARY " -d 5 -e alloc MyAppName\n";

static const unsigned int DEFAULT_FDTRANSFER_TIMEOUT = 30;

extern "C" int jattach(int pid, int argc, const char** argv, int print_output);

Expand Down Expand Up @@ -164,6 +168,10 @@ class String {
return strcmp(_str, other._str) == 0;
}

bool operator!=(const char* other) const {
return !operator==(other);
}

String& operator<<(const char* tail) {
size_t len = strlen(_str);
_str = (char*)realloc(_str, len + strlen(tail) + 1);
Expand Down Expand Up @@ -205,6 +213,7 @@ static bool use_tmp_file = false;
static int duration = 60;
static int pid = 0;
static volatile unsigned long long end_time;
static unsigned int fdtransfer_timeout = DEFAULT_FDTRANSFER_TIMEOUT; // gprofiler-specific: holds timeout value for fdtransfer command

static void sigint_handler(int sig) {
end_time = 0;
Expand Down Expand Up @@ -322,7 +331,7 @@ static int jps(const char* cmd, const char* app_name = NULL) {
return pid;
}

static void run_fdtransfer(int pid, String& fdtransfer) {
static void run_fdtransfer(int pid, String& fdtransfer, unsigned int timeout) {
if (!FdTransferServer::supported() || fdtransfer == "") return;

pid_t child = fork();
Expand All @@ -331,7 +340,7 @@ static void run_fdtransfer(int pid, String& fdtransfer) {
}

if (child == 0) {
exit(FdTransferServer::runOnce(pid, fdtransfer.str()) ? 0 : 1);
exit(FdTransferServer::runOnce(pid, fdtransfer.str(), timeout) ? 0 : 1);
} else {
int ret = wait_for_exit(child);
if (ret != 0) {
Expand Down Expand Up @@ -371,7 +380,8 @@ int main(int argc, const char** argv) {
String arg = args.next();

if (arg == "start" || arg == "resume" || arg == "stop" || arg == "dump" || arg == "check" ||
arg == "status" || arg == "meminfo" || arg == "list" || arg == "collect") {
arg == "status" || arg == "meminfo" || arg == "list" || arg == "collect" ||
arg == "fdtransfer") {
action = arg;

} else if (arg == "load" || arg == "jcmd" || arg == "threaddump" || arg == "dumpheap" || arg == "inspectheap") {
Expand Down Expand Up @@ -477,6 +487,12 @@ int main(int argc, const char** argv) {
params << "," << (arg.str() + 2) << "=" << args.next();
if (action == "collect") action = "start";

} else if (arg == "--fdtransfer-timeout") {
fdtransfer_timeout = atoi(args.next());

} else if (arg == "--fd-path") {
fdtransfer = String(args.next());

} else if (arg == "--fdtransfer") {
char buf[64];
snprintf(buf, sizeof(buf), "@asprof-%d-%08x", getpid(), (unsigned int)time_micros());
Expand Down Expand Up @@ -518,7 +534,7 @@ int main(int argc, const char** argv) {
setup_lib_path();

if (action == "collect") {
run_fdtransfer(pid, fdtransfer);
run_fdtransfer(pid, fdtransfer, 0);
run_jattach(pid, String("start,file=") << file << "," << output << format << params << ",log=" << logfile);

fprintf(stderr, "Profiling for %d seconds\n", duration);
Expand All @@ -540,8 +556,15 @@ int main(int argc, const char** argv) {
// Do not reset SIGTERM handler to allow graceful shutdown

run_jattach(pid, String("stop,file=") << file << "," << output << format << ",log=" << logfile);
} else if (action == "fdtransfer") {
if (params != "") {
fprintf(stderr, "Action fdtransfer was given, all parameters are to be passed with --jattach-cmd\n");
return 1;
}
run_fdtransfer(pid, fdtransfer, fdtransfer_timeout);

} else {
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer);
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer, 0);
run_jattach(pid, String(action) << ",file=" << file << "," << output << format << params << ",log=" << logfile);
}

Expand Down

0 comments on commit 5faa3eb

Please sign in to comment.