Skip to content

Commit 085ad22

Browse files
committed
server : add pidfile option
So we can track the pid of this process Signed-off-by: Eric Curtin <[email protected]>
1 parent 860a9e4 commit 085ad22

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

common/arg.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,5 +3373,9 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
33733373
}
33743374
).set_examples({LLAMA_EXAMPLE_SERVER}));
33753375

3376+
add_opt(common_arg({ "--pidfile" }, "FILE", "path to PID file for server process",
3377+
[](common_params & params, const std::string & value) { params.pidfile = value; })
3378+
.set_examples({ LLAMA_EXAMPLE_SERVER }));
3379+
33763380
return ctx_arg;
33773381
}

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ struct common_params {
366366
std::string hostname = "127.0.0.1";
367367
std::string public_path = ""; // NOLINT
368368
std::string chat_template = ""; // NOLINT
369+
std::string pidfile = ""; // path to PID file for server process // NOLINT
369370
bool use_jinja = false; // NOLINT
370371
bool enable_chat_template = true;
371372
common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_DEEPSEEK;

tools/server/server.cpp

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@
1515
#define MIMETYPE_JSON "application/json; charset=utf-8"
1616

1717
// auto generated files (see README.md for details)
18-
#include "index.html.gz.hpp"
19-
#include "loading.html.hpp"
18+
#include <signal.h>
19+
#include <unistd.h>
2020

2121
#include <atomic>
2222
#include <chrono>
23+
#include <cinttypes>
2324
#include <condition_variable>
2425
#include <cstddef>
25-
#include <cinttypes>
2626
#include <deque>
2727
#include <memory>
2828
#include <mutex>
29-
#include <signal.h>
3029
#include <thread>
3130
#include <unordered_map>
3231
#include <unordered_set>
3332

33+
#include "index.html.gz.hpp"
34+
#include "loading.html.hpp"
35+
36+
#ifdef _WIN32
37+
#include <process.h>
38+
#define getpid _getpid
39+
#endif
40+
3441
using json = nlohmann::ordered_json;
3542

3643
constexpr int HTTP_POLLING_SECONDS = 1;
@@ -3691,6 +3698,72 @@ inline void signal_handler(int signal) {
36913698
shutdown_handler(signal);
36923699
}
36933700

3701+
static bool check_pid_alive(const pid_t pid) {
3702+
if (pid > 0 && kill(pid, 0) == 0) {
3703+
return true;
3704+
}
3705+
3706+
return false;
3707+
}
3708+
3709+
class File {
3710+
public:
3711+
FILE * file = nullptr;
3712+
std::string fname;
3713+
bool rm = false;
3714+
3715+
FILE * open(const std::string & filename, const char * mode, const bool r = false) {
3716+
file = ggml_fopen(filename.c_str(), mode);
3717+
fname = filename;
3718+
rm = r;
3719+
3720+
return file;
3721+
}
3722+
3723+
void close() {
3724+
fclose(file);
3725+
file = nullptr;
3726+
3727+
if (rm) {
3728+
// Remove stale pidfil
3729+
unlink(fname.c_str());
3730+
}
3731+
}
3732+
3733+
~File() {
3734+
if (file) {
3735+
close();
3736+
}
3737+
}
3738+
};
3739+
3740+
static bool is_old_pid_alive(const std::string & filename) {
3741+
pid_t oldpid = 0;
3742+
File f;
3743+
if (f.open(filename, "r")) {
3744+
if (fscanf(f.file, "%d", &oldpid) == 1) {
3745+
if (check_pid_alive(oldpid)) {
3746+
LOG_ERR("Process already running with PID %d\n", oldpid);
3747+
return true;
3748+
}
3749+
}
3750+
}
3751+
3752+
return false;
3753+
}
3754+
3755+
static int create_pidfile(const std::string & pidfile, File & f) {
3756+
if (!f.open(pidfile.c_str(), "w", true)) {
3757+
LOG_ERR("Unable to open pidfile %s: %s\n", pidfile.c_str(), strerror(errno));
3758+
return -1;
3759+
}
3760+
3761+
fprintf(f.file, "%d\n", getpid());
3762+
fflush(f.file);
3763+
3764+
return 0;
3765+
}
3766+
36943767
int main(int argc, char ** argv) {
36953768
// own arguments required by this example
36963769
common_params params;
@@ -3699,6 +3772,13 @@ int main(int argc, char ** argv) {
36993772
return 1;
37003773
}
37013774

3775+
File f;
3776+
if (!params.pidfile.empty()) {
3777+
if (is_old_pid_alive(params.pidfile) || create_pidfile(params.pidfile, f)) {
3778+
return 1;
3779+
}
3780+
}
3781+
37023782
common_init();
37033783

37043784
// struct that contains llama context and inference

0 commit comments

Comments
 (0)