Skip to content

Commit a0205eb

Browse files
committed
Support timeout for fdtransfer command
1 parent b84bd80 commit a0205eb

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/launcher/fdtransferServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class FdTransferServer {
3636
public:
3737
static bool supported() { return true; }
3838

39-
static bool runOnce(int pid, const char *path);
39+
static bool runOnce(int pid, const char *path, unsigned int timeout);
4040
static bool runLoop(const char *path);
4141
};
4242

@@ -46,7 +46,7 @@ class FdTransferServer {
4646
public:
4747
static bool supported() { return false; }
4848

49-
static bool runOnce(int pid, const char *path) { return false; }
49+
static bool runOnce(int pid, const char *path, unsigned int timeout) { return false; }
5050
static bool runLoop(const char *path) { return false; }
5151
};
5252

src/launcher/fdtransferServer_linux.cpp

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

263-
bool FdTransferServer::runOnce(int pid, const char *path) {
263+
bool FdTransferServer::runOnce(int pid, const char *path, unsigned int timeout) {
264264
// get its nspid prior to moving to its PID namespace.
265265
int nspid;
266266
uid_t target_uid;
@@ -287,7 +287,7 @@ bool FdTransferServer::runOnce(int pid, const char *path) {
287287
}
288288
}
289289

290-
if (!bindServer(&sun, addrlen, 30)) {
290+
if (!bindServer(&sun, addrlen, timeout)) {
291291
return false;
292292
}
293293

src/launcher/main.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static const char USAGE_STRING[] =
107107
" " APP_BINARY " stop -o flat jps\n"
108108
" " APP_BINARY " -d 5 -e alloc MyAppName\n";
109109

110+
static const unsigned int DEFAULT_FDTRANSFER_TIMEOUT = 30;
110111

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

@@ -227,6 +228,7 @@ static bool use_tmp_file = false;
227228
static int duration = 60;
228229
static int pid = 0;
229230
static volatile unsigned long long end_time;
231+
static unsigned int timeout = DEFAULT_FDTRANSFER_TIMEOUT;
230232

231233
static void sigint_handler(int sig) {
232234
end_time = 0;
@@ -331,7 +333,7 @@ static int jps(const char* cmd, const char* app_name = NULL) {
331333
return pid;
332334
}
333335

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

337339
pid_t child = fork();
@@ -340,7 +342,7 @@ static void run_fdtransfer(int pid, String& fdtransfer) {
340342
}
341343

342344
if (child == 0) {
343-
exit(FdTransferServer::runOnce(pid, fdtransfer.str()) ? 0 : 1);
345+
exit(FdTransferServer::runOnce(pid, fdtransfer.str(), timeout) ? 0 : 1);
344346
} else {
345347
int ret = wait_for_exit(child);
346348
if (ret != 0) {
@@ -483,7 +485,9 @@ int main(int argc, const char** argv) {
483485
output = "jfr";
484486

485487
} else if (arg == "--timeout" || arg == "--loop") {
486-
params << "," << (arg.str() + 2) << "=" << args.next();
488+
const char* value = args.next();
489+
params << "," << (arg.str() + 2) << "=" << value;
490+
timeout = atoi(value);
487491
if (action == "collect") action = "start";
488492

489493
} else if (arg == "--fd-path") {
@@ -537,7 +541,7 @@ int main(int argc, const char** argv) {
537541
}
538542

539543
if (action == "collect") {
540-
run_fdtransfer(pid, fdtransfer);
544+
run_fdtransfer(pid, fdtransfer, timeout);
541545
run_jattach(pid, kJattachLoad, String("start,file=") << file << "," << output << format << params << ",log=" << logfile);
542546

543547
fprintf(stderr, "Profiling for %d seconds\n", duration);
@@ -561,7 +565,7 @@ int main(int argc, const char** argv) {
561565
if (params != kEmpty) {
562566
fprintf(stderr, "Warning: action fdtransfer was given, these parameters will be ignored: %s\n", params.str());
563567
}
564-
run_fdtransfer(pid, fdtransfer);
568+
run_fdtransfer(pid, fdtransfer, timeout);
565569

566570
} else if (action == "jattach") {
567571
if (jattach_cmd == kEmpty) {
@@ -578,7 +582,7 @@ int main(int argc, const char** argv) {
578582
run_jattach(pid, kJattachJcmd, jattach_cmd);
579583

580584
} else {
581-
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer);
585+
if (action == "start" || action == "resume") run_fdtransfer(pid, fdtransfer, timeout);
582586
run_jattach(pid, kJattachLoad, String(action) << ",file=" << file << "," << output << format << params << ",log=" << logfile);
583587
}
584588

0 commit comments

Comments
 (0)