Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
43c5d05d
Unverified
Commit
43c5d05d
authored
Mar 27, 2022
by
alexliyu7352
Committed by
GitHub
Mar 27, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用clone替代fork (#1518)
使用clone替代fork 因为fork子进程有时会导致提前写时复制, 进而影响性能. 而vfork又会引起父进程阻塞 所以使用clone来产生子进程运行ffmpeg
parent
58799473
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
91 行增加
和
17 行删除
+91
-17
server/Process.cpp
+90
-17
server/Process.h
+1
-0
没有找到文件。
server/Process.cpp
查看文件 @
43c5d05d
...
@@ -27,9 +27,63 @@
...
@@ -27,9 +27,63 @@
#include "Util/uv_errno.h"
#include "Util/uv_errno.h"
#include "Poller/EventPoller.h"
#include "Poller/EventPoller.h"
#include "Process.h"
#include "Process.h"
#define STACK_SIZE (8192*1024)
using
namespace
toolkit
;
using
namespace
toolkit
;
using
namespace
std
;
using
namespace
std
;
#ifndef _WIN32
typedef
struct
{
const
char
*
cmd_path
;
char
**
cmd_args
;
const
char
*
log_file
;
}
params_t
;
static
int
/* Start function for cloned child */
childFunc
(
params_t
*
params
)
{
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
setThreadAffinity
(
-
1
);
//子进程关闭core文件生成
struct
rlimit
rlim
=
{
0
,
0
};
setrlimit
(
RLIMIT_CORE
,
&
rlim
);
//重定向shell日志至文件
char
log_path
[
strlen
(
params
->
log_file
)
+
20
];
memset
(
log_path
,
0
,
sizeof
(
log_path
));
if
(
strlen
(
params
->
log_file
)
<=
1
)
{
//未指定子进程日志文件时,重定向至/dev/null
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"
);
if
(
!
fp
)
{
fprintf
(
stderr
,
"open log file %s failed:%d(%s)
\r\n
"
,
log_path
,
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_path
,
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_path
,
get_uv_error
(),
get_uv_errmsg
());
}
// 关闭日志文件
::
fclose
(
fp
);
}
fprintf
(
stderr
,
"
\r\n\r\n
#### pid=%d,cmd=%s%s #####
\r\n\r\n
"
,
getpid
(),
params
->
cmd_path
,
*
params
->
cmd_args
);
// TODO: execv or execvp
auto
ret
=
execv
(
params
->
cmd_path
,
params
->
cmd_args
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"clone process failed:%d(%s)
\r\n
"
,
get_uv_error
(),
get_uv_errmsg
());
}
return
ret
;
}
#endif
void
Process
::
run
(
const
string
&
cmd
,
string
&
log_file_tmp
)
{
void
Process
::
run
(
const
string
&
cmd
,
string
&
log_file_tmp
)
{
kill
(
2000
);
kill
(
2000
);
#ifdef _WIN32
#ifdef _WIN32
...
@@ -71,8 +125,34 @@ void Process::run(const string &cmd, string &log_file_tmp) {
...
@@ -71,8 +125,34 @@ void Process::run(const string &cmd, string &log_file_tmp) {
WarnL
<<
"start child process fail: "
<<
get_uv_errmsg
();
WarnL
<<
"start child process fail: "
<<
get_uv_errmsg
();
}
}
fclose
(
fp
);
fclose
(
fp
);
#elif ((defined(__linux) || defined(__linux__)))
_process_stack
=
malloc
(
STACK_SIZE
);
auto
params
=
split
(
cmd
,
" "
);
// memory leak in child process, it's ok.
char
**
char_params
=
new
char
*
[
params
.
size
()
+
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
();
free
(
_process_stack
);
throw
std
::
runtime_error
(
StrPrinter
<<
"fork child process failed,err:"
<<
get_uv_errmsg
());
}
_pid
=
clonePid
;
if
(
log_file_tmp
.
empty
())
{
InfoL
<<
"start child process "
<<
_pid
<<
", log file:"
<<
"/dev/null"
;
}
else
{
InfoL
<<
"start child process "
<<
_pid
<<
", log file:"
<<
log_file_tmp
.
c_str
()
<<
"."
<<
_pid
;
}
#else
#else
_pid
=
fork
();
_pid
=
v
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
());
}
}
...
@@ -91,10 +171,10 @@ void Process::run(const string &cmd, string &log_file_tmp) {
...
@@ -91,10 +171,10 @@ void Process::run(const string &cmd, string &log_file_tmp) {
setrlimit
(
RLIMIT_CORE
,
&
rlim
);
setrlimit
(
RLIMIT_CORE
,
&
rlim
);
//在启动子进程时,暂时禁用SIGINT、SIGTERM信号
//在启动子进程时,暂时禁用SIGINT、SIGTERM信号
signal
(
SIGINT
,
SIG_
IGN
);
signal
(
SIGINT
,
SIG_
DFL
);
signal
(
SIGTERM
,
SIG_
IGN
);
signal
(
SIGTERM
,
SIG_
DFL
);
signal
(
SIGSEGV
,
SIG_
IGN
);
signal
(
SIGSEGV
,
SIG_
DFL
);
signal
(
SIGABRT
,
SIG_
IGN
);
signal
(
SIGABRT
,
SIG_
DFL
);
//重定向shell日志至文件
//重定向shell日志至文件
auto
fp
=
File
::
create_file
(
log_file
.
data
(),
"ab"
);
auto
fp
=
File
::
create_file
(
log_file
.
data
(),
"ab"
);
...
@@ -114,18 +194,6 @@ void Process::run(const string &cmd, string &log_file_tmp) {
...
@@ -114,18 +194,6 @@ void Process::run(const string &cmd, string &log_file_tmp) {
}
}
fprintf
(
stderr
,
"
\r\n\r\n
#### pid=%d,cmd=%s #####
\r\n\r\n
"
,
getpid
(),
cmd
.
data
());
fprintf
(
stderr
,
"
\r\n\r\n
#### pid=%d,cmd=%s #####
\r\n\r\n
"
,
getpid
(),
cmd
.
data
());
#ifndef ANDROID
//关闭父进程继承的fd
for
(
int
i
=
3
;
i
<
getdtablesize
();
i
++
)
{
::
close
(
i
);
}
#else
//关闭父进程继承的fd
for
(
int
i
=
3
;
i
<
1024
;
i
++
)
{
::
close
(
i
);
}
#endif
auto
params
=
split
(
cmd
,
" "
);
auto
params
=
split
(
cmd
,
" "
);
// memory leak in child process, it's ok.
// memory leak in child process, it's ok.
char
**
charpv_params
=
new
char
*
[
params
.
size
()
+
1
];
char
**
charpv_params
=
new
char
*
[
params
.
size
()
+
1
];
...
@@ -298,6 +366,11 @@ void Process::kill(int max_delay, bool force) {
...
@@ -298,6 +366,11 @@ void Process::kill(int max_delay, bool force) {
CloseHandle
(
_handle
);
CloseHandle
(
_handle
);
_handle
=
nullptr
;
_handle
=
nullptr
;
}
}
#elif ((defined(__linux) || defined(__linux__)))
if
(
_process_stack
){
free
(
_process_stack
);
_process_stack
=
nullptr
;
}
#endif
#endif
}
}
...
...
server/Process.h
查看文件 @
43c5d05d
...
@@ -31,6 +31,7 @@ public:
...
@@ -31,6 +31,7 @@ public:
private
:
private
:
pid_t
_pid
=
-
1
;
pid_t
_pid
=
-
1
;
void
*
_handle
=
nullptr
;
void
*
_handle
=
nullptr
;
void
*
_process_stack
=
nullptr
;
int
_exit_code
=
0
;
int
_exit_code
=
0
;
};
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论