1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

set send socket buf. mw/mr sleep default to 350.

This commit is contained in:
winlin 2014-12-05 11:24:05 +08:00
parent 4d1c162ebd
commit fad6074a25
10 changed files with 101 additions and 36 deletions

View file

@ -421,13 +421,14 @@ int SrsPublishRecvThread::on_reload_vhost_mr(string vhost)
void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
{
// the underlayer api will set to SRS_MR_SOCKET_BUFFER bytes.
// the bytes:
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536,
// 128KB=131072, 256KB=262144, 512KB=524288
// the buffer should set to SRS_MR_MAX_SLEEP_MS*kbps/8,
// the buffer should set to sleep*kbps/8,
// for example, your system delivery stream in 1000kbps,
// sleep 800ms for small bytes, the buffer should set to:
// 800*1000/8=100000B(about 128KB).
// other examples:
// 2000*3000/8=750000B(about 732KB).
// 2000*5000/8=1250000B(about 1220KB).
int kbps = 5000;

View file

@ -83,7 +83,9 @@ SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd)
duration = 0;
kbps = new SrsKbps();
kbps->set_io(skt, skt);
mw_sleep = SRS_PERF_MW_SLEEP;
mw_enabled = false;
_srs_config->subscribe(this);
}
@ -212,7 +214,10 @@ int SrsRtmpConn::on_reload_vhost_removed(string vhost)
int SrsRtmpConn::on_reload_vhost_mw(string /*vhost*/)
{
mw_sleep = _srs_config->get_mw_sleep_ms(req->vhost);
int sleep_ms = _srs_config->get_mw_sleep_ms(req->vhost);
// when mw_sleep changed, resize the socket send buffer.
change_mw_sleep(sleep_ms);
return ERROR_SUCCESS;
}
@ -513,8 +518,7 @@ int SrsRtmpConn::playing(SrsSource* source)
// use isolate thread to recv,
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/217
SrsQueueRecvThread trd(rtmp,
SRS_CONSTS_RTMP_PULSE_TIMEOUT_US / 1000);
SrsQueueRecvThread trd(rtmp, SRS_PERF_MW_SLEEP);
// start isolate recv thread.
if ((ret = trd.start()) != ERROR_SUCCESS) {
@ -558,10 +562,15 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsQueueRecvThread* trd)
// initialize other components
SrsPithyPrint pithy_print(SRS_CONSTS_STAGE_PLAY_USER);
SrsMessageArray msgs(SYS_CONSTS_MAX_PLAY_SEND_MSGS);
SrsMessageArray msgs(SRS_PERF_MW_MSGS);
bool user_specified_duration_to_stop = (req->duration > 0);
int64_t starttime = -1;
// setup the mw config.
// when mw_sleep changed, resize the socket send buffer.
mw_enabled = true;
change_mw_sleep(_srs_config->get_mw_sleep_ms(req->vhost));
while (true) {
// to use isolate thread to recv, can improve about 33% performance.
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/196
@ -602,6 +611,7 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsQueueRecvThread* trd)
srs_verbose("sleep for no messages to send");
st_usleep(mw_sleep * 1000);
}
srs_info("got %d msgs, mw=%d", count, mw_sleep);
// reportable
if (pithy_print.can_print()) {
@ -980,6 +990,46 @@ int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsMessage* msg
return ret;
}
void SrsRtmpConn::change_mw_sleep(int sleep_ms)
{
if (!mw_enabled) {
return;
}
// the bytes:
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536,
// 128KB=131072, 256KB=262144, 512KB=524288
// the buffer should set to sleep*kbps/8,
// for example, your system delivery stream in 1000kbps,
// sleep 800ms for small bytes, the buffer should set to:
// 800*1000/8=100000B(about 128KB).
// other examples:
// 2000*3000/8=750000B(about 732KB).
// 2000*5000/8=1250000B(about 1220KB).
int kbps = 5000;
int socket_buffer_size = sleep_ms * kbps / 8;
int fd = st_netfd_fileno(stfd);
int onb_sbuf = 0;
socklen_t sock_buf_size = sizeof(int);
getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &onb_sbuf, &sock_buf_size);
// socket send buffer, system will double it.
int nb_sbuf = socket_buffer_size / 2;
// set the socket send buffer when required larger buffer
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &nb_sbuf, sock_buf_size) < 0) {
srs_warn("set sock SO_SENDBUF=%d failed.", nb_sbuf);
}
getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &nb_sbuf, &sock_buf_size);
srs_trace("mw changed %d=>%d, max_msgs=%d, esbuf=%d, sbuf %d=>%d",
mw_sleep, sleep_ms, SRS_PERF_MW_MSGS, socket_buffer_size,
onb_sbuf, nb_sbuf);
mw_sleep = sleep_ms;
}
int SrsRtmpConn::check_edge_token_traverse_auth()
{
int ret = ERROR_SUCCESS;

View file

@ -73,6 +73,8 @@ private:
SrsKbps* kbps;
// the MR(merged-write) sleep time in ms.
int mw_sleep;
// the MR(merged-write) only enabled for play.
int mw_enabled;
public:
SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd);
virtual ~SrsRtmpConn();
@ -102,6 +104,7 @@ private:
virtual int handle_publish_message(SrsSource* source, SrsMessage* msg, bool is_fmle, bool vhost_is_edge);
virtual int process_publish_message(SrsSource* source, SrsMessage* msg, bool vhost_is_edge);
virtual int process_play_control_msg(SrsConsumer* consumer, SrsMessage* msg);
virtual void change_mw_sleep(int sleep_ms);
private:
virtual int check_edge_token_traverse_auth();
virtual int connect_server(int origin_index, st_netfd_t* pstsock);

View file

@ -216,7 +216,7 @@ int SrsMessageQueue::dump_packets(int max_count, SrsMessage** pmsgs, int& count)
} else {
// erase some vector elements may cause memory copy,
// maybe can use more efficient vector.swap to avoid copy.
// @remark for the pmsgs is big enough, for instance, SYS_CONSTS_MAX_PLAY_SEND_MSGS 128,
// @remark for the pmsgs is big enough, for instance, SRS_PERF_MW_MSGS 128,
// the rtmp play client will get 128msgs once, so this branch rarely execute.
msgs.erase(msgs.begin(), msgs.begin() + count);
}