1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-12 19:31:53 +00:00

Refine get_mr_sleep in time unit

This commit is contained in:
winlin 2019-04-09 09:20:44 +08:00
parent 5df8f2f6c1
commit 839a496d50
17 changed files with 52 additions and 54 deletions

5
trunk/configure vendored
View file

@ -172,7 +172,8 @@ fi
MODULE_ID="CORE"
MODULE_DEPENDS=()
ModuleLibIncs=(${SRS_OBJS_DIR})
MODULE_FILES=("srs_core" "srs_core_autofree" "srs_core_performance" "srs_core_mem_watch")
MODULE_FILES=("srs_core" "srs_core_autofree" "srs_core_performance"
"srs_core_mem_watch" "srs_core_time")
CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh
CORE_OBJS="${MODULE_OBJS[@]}"
#
@ -203,7 +204,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
MODULE_ID="SERVICE"
MODULE_DEPENDS=("CORE" "KERNEL" "PROTOCOL")
ModuleLibIncs=(${LibSTRoot} ${SRS_OBJS_DIR} ${LibSSLRoot})
MODULE_FILES=("srs_service_log" "srs_service_time" "srs_service_st" "srs_service_http_client"
MODULE_FILES=("srs_service_log" "srs_service_st" "srs_service_http_client"
"srs_service_http_conn" "srs_service_rtmp_conn" "srs_service_utility"
"srs_service_conn")
DEFINES=""

View file

@ -4654,24 +4654,26 @@ bool SrsConfig::get_mr_enabled(string vhost)
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
int SrsConfig::get_mr_sleep_ms(string vhost)
srs_utime_t SrsConfig::get_mr_sleep(string vhost)
{
static srs_utime_t DEFAULT = SRS_PERF_MR_SLEEP;
SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return SRS_PERF_MR_SLEEP;
return DEFAULT;
}
conf = conf->get("publish");
if (!conf) {
return SRS_PERF_MR_SLEEP;
return DEFAULT;
}
conf = conf->get("mr_latency");
if (!conf || conf->arg0().empty()) {
return SRS_PERF_MR_SLEEP;
return DEFAULT;
}
return ::atoi(conf->arg0().c_str());
return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_MILLISECONDS);
}
int SrsConfig::get_mw_sleep_ms(string vhost)

View file

@ -34,7 +34,7 @@
#include <srs_app_reload.hpp>
#include <srs_app_async_call.hpp>
#include <srs_app_thread.hpp>
#include <srs_service_time.hpp>
#include <srs_core_time.hpp>
class SrsRequest;
class SrsFileWriter;
@ -775,11 +775,11 @@ public:
*/
virtual bool get_mr_enabled(std::string vhost);
/**
* get the mr sleep time in ms for vhost.
* get the mr sleep time in srs_utime_t for vhost.
* @param vhost, the vhost to get the mr sleep time.
*/
// TODO: FIXME: add utest for mr config.
virtual int get_mr_sleep_ms(std::string vhost);
virtual srs_utime_t get_mr_sleep(std::string vhost);
/**
* get the mw sleep time in ms for vhost.
* @param vhost, the vhost to get the mw sleep time.

View file

@ -114,7 +114,7 @@ srs_error_t SrsConnection::set_tcp_nodelay(bool v)
return err;
}
srs_error_t SrsConnection::set_socket_buffer(int buffer_ms)
srs_error_t SrsConnection::set_socket_buffer(srs_utime_t buffer_v)
{
srs_error_t err = srs_success;
@ -143,7 +143,7 @@ srs_error_t SrsConnection::set_socket_buffer(int buffer_ms)
// 2000*3000/8=750000B(about 732KB).
// 2000*5000/8=1250000B(about 1220KB).
int kbps = 4000;
int iv = buffer_ms * kbps / 8;
int iv = (buffer_v / SRS_UTIME_MILLISECONDS) * kbps / 8;
// socket send buffer, system will double it.
iv = iv / 2;
@ -161,7 +161,7 @@ srs_error_t SrsConnection::set_socket_buffer(int buffer_ms)
return srs_error_new(ERROR_SOCKET_SNDBUF, "getsockopt fd=%d, r0=%d", fd, r0);
}
srs_trace("set fd=%d, SO_SNDBUF=%d=>%d, buffer=%dms", fd, ov, iv, buffer_ms);
srs_trace("set fd=%d, SO_SNDBUF=%d=>%d, buffer=%dms", fd, ov, iv, buffer_v / SRS_UTIME_MILLISECONDS);
return err;
}

View file

@ -33,6 +33,7 @@
#include <srs_protocol_kbps.hpp>
#include <srs_app_reload.hpp>
#include <srs_service_conn.hpp>
#include <srs_core_time.hpp>
class SrsWallClock;
@ -103,7 +104,7 @@ public:
// Set socket option TCP_NODELAY.
virtual srs_error_t set_tcp_nodelay(bool v);
// Set socket option SO_SNDBUF in ms.
virtual srs_error_t set_socket_buffer(int buffer_ms);
virtual srs_error_t set_socket_buffer(srs_utime_t buffer_v);
// interface ISrsOneCycleThreadHandler
public:
/**

View file

@ -30,7 +30,7 @@
#include <vector>
#include <srs_app_fragment.hpp>
#include <srs_service_time.hpp>
#include <srs_core_time.hpp>
class SrsRequest;
class SrsOriginHub;

View file

@ -589,7 +589,7 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess
}
}
int mw_sleep = _srs_config->get_mw_sleep_ms(req->vhost);
int mw_sleep = _srs_config->get_mw_sleep_ms(req->vhost) * SRS_UTIME_MILLISECONDS;
if ((err = hc->set_socket_buffer(mw_sleep)) != srs_success) {
return srs_error_wrap(err, "set mw_sleep");
}

View file

@ -27,7 +27,7 @@
#include <srs_core.hpp>
#include <srs_app_reload.hpp>
#include <srs_service_time.hpp>
#include <srs_core_time.hpp>
/**
* the stage info to calc the age.

View file

@ -292,7 +292,7 @@ SrsPublishRecvThread::SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest*
// the mr settings,
// @see https://github.com/ossrs/srs/issues/241
mr = _srs_config->get_mr_enabled(req->vhost);
mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost);
mr_sleep = _srs_config->get_mr_sleep(req->vhost);
realtime = _srs_config->get_realtime_enabled(req->vhost);
@ -466,7 +466,7 @@ void SrsPublishRecvThread::on_read(ssize_t nread)
* @see https://github.com/ossrs/srs/issues/241
*/
if (nread < SRS_MR_SMALL_BYTES) {
srs_usleep(mr_sleep * 1000);
srs_usleep(mr_sleep);
}
}
#endif
@ -482,11 +482,11 @@ srs_error_t SrsPublishRecvThread::on_reload_vhost_publish(string vhost)
// the mr settings,
// @see https://github.com/ossrs/srs/issues/241
bool mr_enabled = _srs_config->get_mr_enabled(req->vhost);
int sleep_ms = _srs_config->get_mr_sleep_ms(req->vhost);
srs_utime_t sleep_v = _srs_config->get_mr_sleep(req->vhost);
// update buffer when sleep ms changed.
if (mr_sleep != sleep_ms) {
set_socket_buffer(sleep_ms);
if (mr_sleep != sleep_v) {
set_socket_buffer(sleep_v);
}
#ifdef SRS_PERF_MERGED_READ
@ -506,7 +506,7 @@ srs_error_t SrsPublishRecvThread::on_reload_vhost_publish(string vhost)
// update to new state
mr = mr_enabled;
mr_sleep = sleep_ms;
mr_sleep = sleep_v;
return err;
}
@ -526,7 +526,7 @@ srs_error_t SrsPublishRecvThread::on_reload_vhost_realtime(string vhost)
return err;
}
void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
void SrsPublishRecvThread::set_socket_buffer(srs_utime_t sleep_v)
{
// the bytes:
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536,
@ -539,7 +539,7 @@ void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
// 2000*3000/8=750000B(about 732KB).
// 2000*5000/8=1250000B(about 1220KB).
int kbps = 5000;
int socket_buffer_size = sleep_ms * kbps / 8;
int socket_buffer_size = (sleep_v / SRS_UTIME_MILLISECONDS) * kbps / 8;
int fd = mr_fd;
int onb_rbuf = 0;
@ -554,7 +554,7 @@ void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nb_rbuf, &sock_buf_size);
srs_trace("mr change sleep %d=>%d, erbuf=%d, rbuf %d=>%d, sbytes=%d, realtime=%d",
mr_sleep, sleep_ms, socket_buffer_size, onb_rbuf, nb_rbuf,
mr_sleep / SRS_UTIME_MILLISECONDS, sleep_v / SRS_UTIME_MILLISECONDS, socket_buffer_size, onb_rbuf, nb_rbuf,
SRS_MR_SMALL_BYTES, realtime);
rtmp->set_recv_buffer(nb_rbuf);

View file

@ -172,7 +172,7 @@ private:
// @see https://github.com/ossrs/srs/issues/241
bool mr;
int mr_fd;
int mr_sleep;
srs_utime_t mr_sleep;
// for realtime
// @see https://github.com/ossrs/srs/issues/257
bool realtime;
@ -220,7 +220,7 @@ public:
virtual srs_error_t on_reload_vhost_publish(std::string vhost);
virtual srs_error_t on_reload_vhost_realtime(std::string vhost);
private:
virtual void set_socket_buffer(int sleep_ms);
virtual void set_socket_buffer(srs_utime_t sleep_v);
};
/**

View file

@ -861,7 +861,7 @@ srs_error_t SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread*
if (true) {
bool mr = _srs_config->get_mr_enabled(req->vhost);
int mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost);
int mr_sleep = _srs_config->get_mr_sleep(req->vhost) / SRS_UTIME_MILLISECONDS;
srs_trace("start publish mr=%d/%d, p1stpt=%d, pnt=%d, tcp_nodelay=%d, rtcid=%d",
mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout, tcp_nodelay, receive_thread_cid);
}
@ -908,7 +908,7 @@ srs_error_t SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread*
if (pprint->can_print()) {
kbps->sample();
bool mr = _srs_config->get_mr_enabled(req->vhost);
int mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost);
int mr_sleep = _srs_config->get_mr_sleep(req->vhost) / SRS_UTIME_MILLISECONDS;
srs_trace("<- " SRS_CONSTS_LOG_CLIENT_PUBLISH " time=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d, mr=%d/%d, p1stpt=%d, pnt=%d",
(int)pprint->age(), kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(),
kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m(), mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout);
@ -1117,7 +1117,7 @@ void SrsRtmpConn::change_mw_sleep(int sleep_ms)
return;
}
set_socket_buffer(sleep_ms);
set_socket_buffer(sleep_ms * SRS_UTIME_MILLISECONDS);
mw_sleep = sleep_ms;
}

View file

@ -25,6 +25,7 @@
#define SRS_CORE_PERFORMANCE_HPP
#include <srs_core.hpp>
#include <srs_core_time.hpp>
/**
* this file defines the perfromance options.
@ -56,7 +57,7 @@
#define SRS_PERF_MERGED_READ
// the default config of mr.
#define SRS_PERF_MR_ENABLED false
#define SRS_PERF_MR_SLEEP 350
#define SRS_PERF_MR_SLEEP (350 * SRS_UTIME_MILLISECONDS)
/**
* the MW(merged-write) send cache time in ms.

View file

@ -21,5 +21,5 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_service_time.hpp>
#include <srs_core_time.hpp>

View file

@ -21,8 +21,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_SERVICE_TIME_HPP
#define SRS_SERVICE_TIME_HPP
#ifndef SRS_CORE_TIME_HPP
#define SRS_CORE_TIME_HPP
#include <srs_core.hpp>

View file

@ -48,6 +48,7 @@ using namespace std;
#include <srs_core_performance.hpp>
#include <srs_app_utility.hpp>
#include <srs_core_autofree.hpp>
#include <srs_core_time.hpp>
// pre-declare
srs_error_t run(SrsServer* svr);
@ -295,7 +296,7 @@ void show_macro_features()
#else
ss << "enabled:off";
#endif
ss << ", default:" << SRS_PERF_MR_ENABLED << ", sleep:" << SRS_PERF_MR_SLEEP << "ms";
ss << ", default:" << SRS_PERF_MR_ENABLED << ", sleep:" << SRS_PERF_MR_SLEEP / SRS_UTIME_MILLISECONDS << "ms";
srs_trace(ss.str().c_str());
}
@ -339,7 +340,7 @@ void show_macro_features()
// others
int possible_mr_latency = 0;
#ifdef SRS_PERF_MERGED_READ
possible_mr_latency = SRS_PERF_MR_SLEEP;
possible_mr_latency = SRS_PERF_MR_SLEEP / SRS_UTIME_MILLISECONDS;
#endif
srs_trace("system default latency in ms: mw(0-%d) + mr(0-%d) + play-queue(0-%d)",
SRS_PERF_MW_SLEEP, possible_mr_latency, SRS_PERF_PLAY_QUEUE*1000);

View file

@ -25,33 +25,17 @@
#define SRS_SERVICE_ST_HPP
#include <srs_core.hpp>
#include <srs_core_time.hpp>
#include <string>
#include <srs_protocol_io.hpp>
#include <srs_service_time.hpp>
// Wrap for coroutine.
typedef void* srs_netfd_t;
typedef void* srs_thread_t;
typedef void* srs_cond_t;
typedef void* srs_mutex_t;
typedef uint64_t srs_utime_t;
// The time unit in ms, for example 100 * SRS_UTIME_MILLISECONDS means 100ms.
#define SRS_UTIME_MILLISECONDS 1000
// The time unit in ms, for example 120 * SRS_UTIME_SECONDS means 120s.
#define SRS_UTIME_SECONDS 1000000
// The time unit in minutes, for example 3 * SRS_UTIME_MINUTES means 3m.
#define SRS_UTIME_MINUTES 60000000LL
// The time unit in hours, for example 2 * SRS_UTIME_HOURS means 2h.
#define SRS_UTIME_HOURS 3600000000LL
// Never timeout.
#define SRS_UTIME_NO_TIMEOUT ((srs_utime_t) -1LL)
// initialize st, requires epoll.
extern srs_error_t srs_st_init();

View file

@ -1843,5 +1843,13 @@ VOID TEST(ConfigUnitTest, CheckDefaultValues)
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"pithy_print_ms 20000;"));
EXPECT_EQ(20 * SRS_UTIME_SECONDS, conf.get_pithy_print());
}
if (true) {
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF));
EXPECT_EQ(350 * SRS_UTIME_MILLISECONDS, conf.get_mr_sleep(""));
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"vhost v{publish{mr_latency 1000;}}"));
EXPECT_EQ(1000 * SRS_UTIME_MILLISECONDS, conf.get_mr_sleep("v"));
}
}