@@ -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
9599extern " 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;
205213static int duration = 60 ;
206214static int pid = 0 ;
207215static volatile unsigned long long end_time;
216+ static unsigned int fdtransfer_timeout = DEFAULT_FDTRANSFER_TIMEOUT; // gprofiler-specific: holds timeout value for fdtransfer command
208217
209218static 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