winlin

for bug #241, simplify the merged read config macros.

... ... @@ -736,7 +736,7 @@ The publish benchmark by [st-load](https://github.com/winlinvip/st-load):
* 2014-12-03, SRS 2.0.48, 1.4k(1400) publishers, 95%CPU, 140MB. [commit](https://github.com/winlinvip/simple-rtmp-server/commit/f35ec2155b1408d528a9f37da7904c9625186bcf)
* 2014-12-04, SRS 2.0.49, 1.4k(1400) publishers, 68%CPU, 144MB.
* 2014-12-04, SRS 2.0.49, 2.5k(2500) publishers, 95%CPU, 404MB. [commit](https://github.com/winlinvip/simple-rtmp-server/commit/29324fab469e0f7cef9ad04ffdbce832ac7dd9ff)
* 2014-12-04, SRS 2.0.51, 2.5k(2500) publishers, 92%CPU, 259MB. [commit](https://github.com/winlinvip/simple-rtmp-server/commit/f57801eb46c16755b173984b915a4166922df6a6)
* 2014-12-04, SRS 2.0.51, 2.5k(2500) publishers, 91%CPU, 259MB. [commit](https://github.com/winlinvip/simple-rtmp-server/commit/f57801eb46c16755b173984b915a4166922df6a6)
## Architecture
... ...
... ... @@ -30,23 +30,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_kernel_utility.hpp>
#include <srs_core_performance.hpp>
// when we read from socket less than this value,
// sleep a while to merge read.
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
// use the bitrate in kbps to calc the max sleep time.
#define SRS_MR_MAX_BITRATE_KBPS 10000
#define SRS_MR_AVERAGE_BITRATE_KBPS 1000
#define SRS_MR_MIN_BITRATE_KBPS 32
// the max sleep time in ms
#define SRS_MR_MAX_SLEEP_MS 2500
// the max small bytes to group
#define SRS_MR_SMALL_BYTES 4096
// the percent of buffer to set as small bytes
#define SRS_MR_SMALL_PERCENT 100
// set the socket buffer to specified bytes.
// the underlayer api will set to SRS_MR_SOCKET_BUFFER bytes.
#define SRS_MR_SOCKET_BUFFER SOCKET_READ_SIZE
ISrsMessageHandler::ISrsMessageHandler()
{
}
... ... @@ -253,8 +236,6 @@ SrsPublishRecvThread::SrsPublishRecvThread(
error = st_cond_new();
mr_fd = fd;
mr_small_bytes = 0;
mr_sleep_ms = 0;
}
SrsPublishRecvThread::~SrsPublishRecvThread()
... ... @@ -309,7 +290,8 @@ void SrsPublishRecvThread::on_thread_start()
}
getsockopt(mr_fd, SOL_SOCKET, SO_RCVBUF, &nb_rbuf, &sock_buf_size);
srs_trace("set socket buffer to %d, actual %d KB", SRS_MR_SOCKET_BUFFER / 1024, nb_rbuf / 1024);
srs_trace("merged read sockbuf=%d, actual=%d, sleep %d when nread<=%d",
SRS_MR_SOCKET_BUFFER, nb_rbuf, SRS_MR_MAX_SLEEP_MS, SRS_MR_SMALL_BYTES);
// enable the merge read
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
... ... @@ -367,7 +349,7 @@ void SrsPublishRecvThread::on_recv_error(int ret)
#ifdef SRS_PERF_MERGED_READ
void SrsPublishRecvThread::on_read(ssize_t nread)
{
if (nread < 0 || mr_sleep_ms <= 0) {
if (nread < 0 || SRS_MR_MAX_SLEEP_MS <= 0) {
return;
}
... ... @@ -377,34 +359,8 @@ void SrsPublishRecvThread::on_read(ssize_t nread)
* that is, we merge some data to read together.
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
*/
if (nread < mr_small_bytes) {
st_usleep(mr_sleep_ms * 1000);
if (nread < SRS_MR_SMALL_BYTES) {
st_usleep(SRS_MR_MAX_SLEEP_MS * 1000);
}
}
void SrsPublishRecvThread::on_buffer_change(int nb_buffer)
{
srs_assert(nb_buffer > 0);
// set percent.
mr_small_bytes = (int)(nb_buffer / SRS_MR_SMALL_PERCENT);
// select the smaller
mr_small_bytes = srs_max(mr_small_bytes, SRS_MR_SMALL_BYTES);
// the recv sleep is [buffer / max_kbps, buffer / min_kbps]
// for example, buffer is 256KB, max kbps is 10Mbps, min kbps is 10Kbps,
// the buffer is 256KB*8=2048Kb, which can provides sleep time in
// min: 2038Kb/10Mbps=2038Kb/10Kbpms=203.8ms
// max: 2038Kb/10Kbps=203.8s
// sleep = Xb * 8 / (N * 1000 b / 1000 ms) = (X * 8 / N) ms
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
int min_sleep = (int)(nb_buffer * 8.0 / SRS_MR_MAX_BITRATE_KBPS);
int average_sleep = (int)(nb_buffer * 8.0 / SRS_MR_AVERAGE_BITRATE_KBPS);
int max_sleep = (int)(nb_buffer * 8.0 / SRS_MR_MIN_BITRATE_KBPS);
// 80% min, 16% average, 4% max.
mr_sleep_ms = (int)(min_sleep * 0.8 + average_sleep * 0.16 + max_sleep * 0.04);
mr_sleep_ms = srs_min(mr_sleep_ms, SRS_MR_MAX_SLEEP_MS);
srs_trace("merged read, buffer=%d, small=%d, sleep=%d", nb_buffer, mr_small_bytes, mr_sleep_ms);
}
#endif
... ...
... ... @@ -147,8 +147,6 @@ private:
// for mr(merged read),
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
int mr_fd;
int mr_small_bytes;
int mr_sleep_ms;
// the recv thread error code.
int recv_error_code;
SrsRtmpConn* _conn;
... ... @@ -184,7 +182,6 @@ public:
public:
#ifdef SRS_PERF_MERGED_READ
virtual void on_read(ssize_t nread);
virtual void on_buffer_change(int nb_buffer);
#endif
};
... ...
... ... @@ -64,8 +64,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* buffer=65536B, small=4096B, sleep=780ms
* that is, when got nread bytes smaller than 4KB, sleep(780ms).
*/
#undef SRS_PERF_MERGED_READ
#define SRS_PERF_MERGED_READ
#if 1
// to enable merged read.
#define SRS_PERF_MERGED_READ
// the max sleep time in ms
#define SRS_MR_MAX_SLEEP_MS 1000
// the max small bytes to group
#define SRS_MR_SMALL_BYTES 4096
// the underlayer api will set to SRS_MR_SOCKET_BUFFER bytes.
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536
#define SRS_MR_SOCKET_BUFFER 65536
#endif
/**
* the send cache time in ms.
... ...
... ... @@ -93,7 +93,7 @@ SrsFastBuffer::SrsFastBuffer()
p = end = buffer = NULL;
nb_buffer = 0;
reset_buffer(SOCKET_READ_SIZE);
reset_buffer(SRS_MR_SOCKET_BUFFER);
}
SrsFastBuffer::~SrsFastBuffer()
... ... @@ -192,15 +192,11 @@ void SrsFastBuffer::set_merge_read(bool v, int max_buffer, IMergeReadHandler* ha
_handler = handler;
// limit the max buffer.
int buffer_size = srs_min(max_buffer, SOCKET_MAX_BUF);
int buffer_size = srs_min(max_buffer, SRS_MR_SOCKET_BUFFER);
if (v && buffer_size != nb_buffer) {
reset_buffer(buffer_size);
}
if (_handler) {
_handler->on_buffer_change(nb_buffer);
}
}
#endif
... ... @@ -211,17 +207,11 @@ void SrsFastBuffer::on_chunk_size(int32_t chunk_size)
}
// limit the max buffer.
int buffer_size = srs_min(chunk_size, SOCKET_MAX_BUF);
int buffer_size = srs_min(chunk_size, SRS_MR_SOCKET_BUFFER);
if (buffer_size != nb_buffer) {
reset_buffer(buffer_size);
}
#ifdef SRS_PERF_MERGED_READ
if (_handler) {
_handler->on_buffer_change(nb_buffer);
}
#endif
}
int SrsFastBuffer::buffer_size()
... ...
... ... @@ -35,16 +35,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_protocol_io.hpp>
#include <srs_core_performance.hpp>
// 4KB=4096
// 8KB=8192
// 16KB=16384
// 32KB=32768
// 64KB=65536
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
#define SOCKET_READ_SIZE 65536
// the max buffer for user space socket buffer.
#define SOCKET_MAX_BUF SOCKET_READ_SIZE
/**
* the simple buffer use vector to append bytes,
* it's for hls and http, and need to be refined in future.
... ... @@ -101,11 +91,6 @@ public:
* @remark, it only for server-side, client srs-librtmp just ignore.
*/
virtual void on_read(ssize_t nread) = 0;
/**
* when buffer size changed.
* @param nb_buffer the new buffer size.
*/
virtual void on_buffer_change(int nb_buffer) = 0;
};
#endif
... ...