优化进程管理代码: #1518
This commit is contained in:
parent
43c5d05d8f
commit
4b9b022690
|
|
@ -14,74 +14,93 @@
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#else
|
#else
|
||||||
//#include <TlHelp32.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <csignal>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <signal.h>
|
#include "Process.h"
|
||||||
#include "Util/util.h"
|
|
||||||
#include "Util/File.h"
|
#include "Util/File.h"
|
||||||
|
#include "Util/util.h"
|
||||||
#include "Util/logger.h"
|
#include "Util/logger.h"
|
||||||
#include "Util/uv_errno.h"
|
#include "Util/uv_errno.h"
|
||||||
#include "Poller/EventPoller.h"
|
#include "Poller/EventPoller.h"
|
||||||
#include "Process.h"
|
|
||||||
|
|
||||||
#define STACK_SIZE (8192 * 1024)
|
#define STACK_SIZE (8192 * 1024)
|
||||||
using namespace toolkit;
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace toolkit;
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
const char *cmd_path;
|
|
||||||
char **cmd_args;
|
|
||||||
const char *log_file;
|
|
||||||
} params_t;
|
|
||||||
|
|
||||||
|
static void setupChildProcess() {
|
||||||
static int /* Start function for cloned child */
|
|
||||||
childFunc(params_t *params)
|
|
||||||
{
|
|
||||||
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
|
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
|
||||||
setThreadAffinity(-1);
|
setThreadAffinity(-1);
|
||||||
//子进程关闭core文件生成
|
//子进程关闭core文件生成
|
||||||
struct rlimit rlim = { 0, 0 };
|
struct rlimit rlim = { 0, 0 };
|
||||||
setrlimit(RLIMIT_CORE, &rlim);
|
setrlimit(RLIMIT_CORE, &rlim);
|
||||||
//重定向shell日志至文件
|
//子进程恢复默认信号处理
|
||||||
char log_path[strlen(params->log_file) + 20];
|
signal(SIGINT, SIG_DFL);
|
||||||
memset(log_path,0,sizeof(log_path));
|
signal(SIGTERM, SIG_DFL);
|
||||||
if (strlen(params->log_file) <= 1) {
|
signal(SIGSEGV, SIG_DFL);
|
||||||
//未指定子进程日志文件时,重定向至/dev/null
|
signal(SIGABRT, SIG_DFL);
|
||||||
snprintf(log_path, sizeof(log_path), "%s", "/dev/null");
|
|
||||||
} else {
|
|
||||||
snprintf(log_path, sizeof(log_path), "%s.%d", params->log_file, getpid());
|
|
||||||
}
|
}
|
||||||
auto fp = File::create_file(params->log_file, "ab");
|
|
||||||
|
/* Start function for cloned child */
|
||||||
|
static int runChildProcess(const string &cmd, string &log_file_tmp) {
|
||||||
|
setupChildProcess();
|
||||||
|
|
||||||
|
string log_file;
|
||||||
|
if (log_file_tmp.empty()) {
|
||||||
|
//未指定子进程日志文件时,重定向至/dev/null
|
||||||
|
log_file = "/dev/null";
|
||||||
|
} else {
|
||||||
|
log_file = StrPrinter << log_file_tmp << "." << getpid();
|
||||||
|
}
|
||||||
|
|
||||||
|
//重定向shell日志至文件
|
||||||
|
auto fp = File::create_file(log_file.data(), "ab");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg());
|
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
||||||
} else {
|
} else {
|
||||||
auto log_fd = fileno(fp);
|
auto log_fd = fileno(fp);
|
||||||
// dup to stdout and stderr.
|
// dup to stdout and stderr.
|
||||||
if (dup2(log_fd, STDOUT_FILENO) < 0) {
|
if (dup2(log_fd, STDOUT_FILENO) < 0) {
|
||||||
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg());
|
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
||||||
}
|
}
|
||||||
if (dup2(log_fd, STDERR_FILENO) < 0) {
|
if (dup2(log_fd, STDERR_FILENO) < 0) {
|
||||||
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg());
|
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
||||||
}
|
}
|
||||||
// 关闭日志文件
|
// 关闭日志文件
|
||||||
::fclose(fp);
|
::fclose(fp);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s%s #####\r\n\r\n", getpid(), params->cmd_path, *params->cmd_args);
|
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());
|
||||||
|
|
||||||
|
auto params = split(cmd, " ");
|
||||||
|
// memory leak in child process, it's ok.
|
||||||
|
char **charpv_params = new char *[params.size() + 1];
|
||||||
|
for (int i = 0; i < (int)params.size(); i++) {
|
||||||
|
std::string &p = params[i];
|
||||||
|
charpv_params[i] = (char *)p.data();
|
||||||
|
}
|
||||||
|
// EOF: NULL
|
||||||
|
charpv_params[params.size()] = NULL;
|
||||||
// TODO: execv or execvp
|
// TODO: execv or execvp
|
||||||
auto ret = execv(params->cmd_path, params->cmd_args);
|
auto ret = execv(params[0].c_str(), charpv_params);
|
||||||
|
delete[] charpv_params;
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "clone process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
|
fprintf(stderr, "execv process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int cloneFunc(void *ptr) {
|
||||||
|
auto pair = reinterpret_cast<std::pair<string, string> *>(ptr);
|
||||||
|
return runChildProcess(pair->first, pair->second);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Process::run(const string &cmd, string &log_file_tmp) {
|
void Process::run(const string &cmd, string &log_file_tmp) {
|
||||||
|
|
@ -127,89 +146,27 @@ void Process::run(const string &cmd, string &log_file_tmp) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
#elif ((defined(__linux) || defined(__linux__)))
|
#elif ((defined(__linux) || defined(__linux__)))
|
||||||
_process_stack = malloc(STACK_SIZE);
|
_process_stack = malloc(STACK_SIZE);
|
||||||
auto params = split(cmd, " ");
|
auto args = std::make_pair(cmd, log_file_tmp);
|
||||||
// memory leak in child process, it's ok.
|
int pid = clone(reinterpret_cast<int (*)(void *)>(&cloneFunc), (char *)_process_stack + STACK_SIZE, CLONE_FS | SIGCHLD, (void *)(&args));
|
||||||
char **char_params = new char *[params.size() + 1];
|
if (pid == -1) {
|
||||||
for (int i = 0; i < (int) params.size(); i++) {
|
|
||||||
std::string &p = params[i];
|
|
||||||
char_params[i] = (char *) p.data();
|
|
||||||
}
|
|
||||||
// EOF: NULL
|
|
||||||
char_params[params.size()] = NULL;
|
|
||||||
params_t cmd_params = {params[0].c_str(), char_params, log_file_tmp.c_str()};
|
|
||||||
int clonePid = clone(reinterpret_cast<int (*)(void *)>(&childFunc), (char *)_process_stack + STACK_SIZE, CLONE_FS | SIGCHLD, (void *) (&cmd_params));
|
|
||||||
delete[] char_params;
|
|
||||||
if (clonePid == -1) {
|
|
||||||
WarnL << "clone process failed:" << get_uv_errmsg();
|
WarnL << "clone process failed:" << get_uv_errmsg();
|
||||||
free(_process_stack);
|
free(_process_stack);
|
||||||
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
||||||
}
|
}
|
||||||
_pid = clonePid;
|
_pid = pid;
|
||||||
if (log_file_tmp.empty()) {
|
if (log_file_tmp.empty()) {
|
||||||
InfoL << "start child process " << _pid << ", log file:" << "/dev/null";
|
InfoL << "start child process " << _pid << ", log file:" << "/dev/null";
|
||||||
} else {
|
} else {
|
||||||
InfoL << "start child process " << _pid << ", log file:" << log_file_tmp.c_str() << "." << _pid;
|
InfoL << "start child process " << _pid << ", log file:" << log_file_tmp.c_str() << "." << _pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
_pid = vfork();
|
_pid = fork();
|
||||||
if (_pid < 0) {
|
if (_pid < 0) {
|
||||||
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
||||||
}
|
}
|
||||||
if (_pid == 0) {
|
if (_pid == 0) {
|
||||||
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
|
//子进程
|
||||||
setThreadAffinity(-1);
|
exit(runChildProcess(cmd, log_file_tmp));
|
||||||
string log_file;
|
|
||||||
if (log_file_tmp.empty()) {
|
|
||||||
//未指定子进程日志文件时,重定向至/dev/null
|
|
||||||
log_file = "/dev/null";
|
|
||||||
} else {
|
|
||||||
log_file = StrPrinter << log_file_tmp << "." << getpid();
|
|
||||||
}
|
|
||||||
//子进程关闭core文件生成
|
|
||||||
struct rlimit rlim = {0, 0};
|
|
||||||
setrlimit(RLIMIT_CORE, &rlim);
|
|
||||||
|
|
||||||
//在启动子进程时,暂时禁用SIGINT、SIGTERM信号
|
|
||||||
signal(SIGINT, SIG_DFL);
|
|
||||||
signal(SIGTERM, SIG_DFL);
|
|
||||||
signal(SIGSEGV, SIG_DFL);
|
|
||||||
signal(SIGABRT, SIG_DFL);
|
|
||||||
|
|
||||||
//重定向shell日志至文件
|
|
||||||
auto fp = File::create_file(log_file.data(), "ab");
|
|
||||||
if (!fp) {
|
|
||||||
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
|
||||||
} else {
|
|
||||||
auto log_fd = fileno(fp);
|
|
||||||
// dup to stdout and stderr.
|
|
||||||
if (dup2(log_fd, STDOUT_FILENO) < 0) {
|
|
||||||
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
|
||||||
}
|
|
||||||
if (dup2(log_fd, STDERR_FILENO) < 0) {
|
|
||||||
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
|
|
||||||
}
|
|
||||||
// 关闭日志文件
|
|
||||||
::fclose(fp);
|
|
||||||
}
|
|
||||||
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());
|
|
||||||
|
|
||||||
auto params = split(cmd, " ");
|
|
||||||
// memory leak in child process, it's ok.
|
|
||||||
char **charpv_params = new char *[params.size() + 1];
|
|
||||||
for (int i = 0; i < (int) params.size(); i++) {
|
|
||||||
std::string &p = params[i];
|
|
||||||
charpv_params[i] = (char *) p.data();
|
|
||||||
}
|
|
||||||
// EOF: NULL
|
|
||||||
charpv_params[params.size()] = NULL;
|
|
||||||
|
|
||||||
// TODO: execv or execvp
|
|
||||||
auto ret = execv(params[0].c_str(), charpv_params);
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "fork process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
|
|
||||||
}
|
|
||||||
exit(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string log_file;
|
string log_file;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue