Skip to content

Commit af215c5

Browse files
marcin-olJongy
authored andcommitted
Export gProfiler-needed functionality from asprof (#7)
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.
1 parent 37963c0 commit af215c5

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ build/%:
118118
mkdir -p $@
119119

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

124124
PROFILER_FLAGS=-static-libgcc

src/launcher/fdtransferServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FdTransferServer {
2525
public:
2626
static bool supported() { return true; }
2727

28-
static bool runOnce(int pid, const char *path);
28+
static bool runOnce(int pid, const char *path, unsigned int timeout);
2929
static bool runLoop(const char *path);
3030
};
3131

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

38-
static bool runOnce(int pid, const char *path) { return false; }
38+
static bool runOnce(int pid, const char *path, unsigned int timeout) { return false; }
3939
static bool runLoop(const char *path) { return false; }
4040
};
4141

src/launcher/fdtransferServer_linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ bool FdTransferServer::sendFd(int fd, struct fd_response *resp, size_t resp_size
249249
return true;
250250
}
251251

252-
bool FdTransferServer::runOnce(int pid, const char *path) {
252+
bool FdTransferServer::runOnce(int pid, const char *path, unsigned int timeout) {
253253
// get its nspid prior to moving to its PID namespace.
254254
int nspid;
255255
uid_t target_uid;
@@ -276,7 +276,7 @@ bool FdTransferServer::runOnce(int pid, const char *path) {
276276
}
277277
}
278278

279-
if (!bindServer(&sun, addrlen, 30)) {
279+
if (!bindServer(&sun, addrlen, timeout)) {
280280
return false;
281281
}
282282

src/launcher/main.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static const char USAGE_STRING[] =
4141
" jcmd run JVM diagnostic command (jattach action)\n"
4242
" collect collect profile for the specified period of time\n"
4343
" and then stop (default action)\n"
44+
" fdtransfer start fdtransfer to serve perf requests on behalf of profiled process\n"
45+
"\n"
4446
"Options:\n"
4547
" -e event profiling event: cpu|alloc|lock|cache-misses etc.\n"
4648
" -d duration run profiling for <duration> seconds\n"
@@ -81,6 +83,7 @@ static const char USAGE_STRING[] =
8183
" --jfrsync config synchronize profiler with JFR recording\n"
8284
" --fdtransfer use fdtransfer to serve perf requests\n"
8385
" from the non-privileged target\n"
86+
" --fd-path string socket path for fdtransfer to bind to\n"
8487
"\n"
8588
"<pid> is a numeric process ID of the target JVM\n"
8689
" or 'jps' keyword to find running JVM automatically\n"
@@ -91,6 +94,7 @@ static const char USAGE_STRING[] =
9194
" " APP_BINARY " stop -o flat jps\n"
9295
" " APP_BINARY " -d 5 -e alloc MyAppName\n";
9396

97+
static const unsigned int DEFAULT_FDTRANSFER_TIMEOUT = 30;
9498

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

@@ -164,6 +168,10 @@ class String {
164168
return strcmp(_str, other._str) == 0;
165169
}
166170

171+
bool operator!=(const char* other) const {
172+
return !operator==(other);
173+
}
174+
167175
String& operator<<(const char* tail) {
168176
size_t len = strlen(_str);
169177
_str = (char*)realloc(_str, len + strlen(tail) + 1);
@@ -205,6 +213,7 @@ static bool use_tmp_file = false;
205213
static int duration = 60;
206214
static int pid = 0;
207215
static volatile unsigned long long end_time;
216+
static unsigned int fdtransfer_timeout = DEFAULT_FDTRANSFER_TIMEOUT; // gprofiler-specific: holds timeout value for fdtransfer command
208217

209218
static void sigint_handler(int sig) {
210219
end_time = 0;
@@ -322,7 +331,7 @@ static int jps(const char* cmd, const char* app_name = NULL) {
322331
return pid;
323332
}
324333

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

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

333342
if (child == 0) {
334-
exit(FdTransferServer::runOnce(pid, fdtransfer.str()) ? 0 : 1);
343+
exit(FdTransferServer::runOnce(pid, fdtransfer.str(), timeout) ? 0 : 1);
335344
} else {
336345
int ret = wait_for_exit(child);
337346
if (ret != 0) {
@@ -371,7 +380,8 @@ int main(int argc, const char** argv) {
371380
String arg = args.next();
372381

373382
if (arg == "start" || arg == "resume" || arg == "stop" || arg == "dump" || arg == "check" ||
374-
arg == "status" || arg == "meminfo" || arg == "list" || arg == "collect") {
383+
arg == "status" || arg == "meminfo" || arg == "list" || arg == "collect" ||
384+
arg == "fdtransfer") {
375385
action = arg;
376386

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

490+
} else if (arg == "--fdtransfer-timeout") {
491+
fdtransfer_timeout = atoi(args.next());
492+
493+
} else if (arg == "--fd-path") {
494+
fdtransfer = String(args.next());
495+
480496
} else if (arg == "--fdtransfer") {
481497
char buf[64];
482498
snprintf(buf, sizeof(buf), "@asprof-%d-%08x", getpid(), (unsigned int)time_micros());
@@ -518,7 +534,7 @@ int main(int argc, const char** argv) {
518534
setup_lib_path();
519535

520536
if (action == "collect") {
521-
run_fdtransfer(pid, fdtransfer);
537+
run_fdtransfer(pid, fdtransfer, 0);
522538
run_jattach(pid, String("start,file=") << file << "," << output << format << params << ",log=" << logfile);
523539

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

542558
run_jattach(pid, String("stop,file=") << file << "," << output << format << ",log=" << logfile);
559+
} else if (action == "fdtransfer") {
560+
if (params != "") {
561+
fprintf(stderr, "fdtransfer mode does not support parameters besides --fd-path and --fdtransfer-timeout");
562+
return 1;
563+
}
564+
run_fdtransfer(pid, fdtransfer, fdtransfer_timeout);
565+
543566
} else {
544-
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer);
567+
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer, 0);
545568
run_jattach(pid, String(action) << ",file=" << file << "," << output << format << params << ",log=" << logfile);
546569
}
547570

0 commit comments

Comments
 (0)