winlin

refine the kbps of server.

... ... @@ -92,6 +92,7 @@ public:
virtual void on_thread_stop();
public:
/**
* reset and start sample of bytes.
* when server to get the kbps of connection,
* it cannot wait the connection terminated then get the kbps,
* it must sample the kbps every some interval, for instance, 9s to sample all connections kbps,
... ...
... ... @@ -122,12 +122,12 @@ public:
* to statistic the kbps of io.
* itself can be a statistic source, for example, used for SRS bytes stat.
* there are two usage scenarios:
* 1. connections to calc kbps:
* 1. connections to calc kbps by sample():
* set_io(in, out)
* sample()
* get_xxx_kbps().
* the connections know how many bytes already send/recv.
* 2. server to calc kbps:
* 2. server to calc kbps by add_delta():
* set_io(NULL, NULL)
* for each connection in connections:
* add_delta(connections) // where connection is a IKbpsDelta*
... ...
... ... @@ -745,8 +745,12 @@ void SrsServer::remove(SrsConnection* conn)
srs_info("conn removed. conns=%d", (int)conns.size());
// resample the resource of specified connection.
resample_kbps(conn);
// resample the kbps to collect the delta.
conn->kbps_resample();
// add delta of connection to server kbps.,
// for next sample() of server kbps can get the stat.
kbps->add_delta(conn);
// all connections are created by server,
// so we free it here.
... ... @@ -862,8 +866,8 @@ int SrsServer::do_cycle()
srs_update_network_devices();
}
if ((i % SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES) == 0) {
srs_info("update network rtmp server info.");
resample_kbps(NULL);
srs_info("update network server kbps info.");
resample_kbps();
srs_update_rtmp_server((int)conns.size(), kbps);
}
#ifdef SRS_AUTO_HTTP_PARSER
... ... @@ -1013,31 +1017,24 @@ void SrsServer::close_listeners(SrsListenerType type)
}
}
void SrsServer::resample_kbps(SrsConnection* conn, bool do_resample)
void SrsServer::resample_kbps()
{
// resample all when conn is NULL.
if (!conn) {
for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
SrsConnection* client = *it;
srs_assert(client);
// only resample, do resample when all finished.
resample_kbps(client, false);
}
// collect delta from all clients.
for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
SrsConnection* conn = *it;
// resample the kbps to collect the delta.
conn->kbps_resample();
kbps->sample();
return;
// add delta of connection to server kbps.,
// for next sample() of server kbps can get the stat.
kbps->add_delta(conn);
}
// resample for connection.
conn->kbps_resample();
kbps->add_delta(conn);
// resample for server.
if (do_resample) {
kbps->sample();
}
// TODO: FXME: support all other connections.
// sample the kbps, get the stat.
kbps->sample();
}
int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
... ...
... ... @@ -278,12 +278,9 @@ private:
*/
virtual void close_listeners(SrsListenerType type);
/**
* resample the server kbps.
* if conn is NULL, resample all connections delta, then calc the total kbps.
* @param conn, the connection to do resample the kbps. NULL to resample all connections.
* @param do_resample, whether resample the server kbps. always false when sample a connection.
* resample the server kbs.
*/
virtual void resample_kbps(SrsConnection* conn, bool do_resample = true);
virtual void resample_kbps();
// internal only
public:
/**
... ...
... ... @@ -1119,9 +1119,7 @@ void srs_api_dump_summaries(std::stringstream& ss)
<< __SRS_JFIELD_ORG("net_send_bytes", ns_bytes) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("srs_sample_time", nrs->sample_time) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("srs_recv_bytes", nrs->rbytes) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("srs_recv_kbps", nrs->rkbps) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("srs_send_bytes", nrs->sbytes) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("srs_send_kbps", nrs->skbps) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << __SRS_JFIELD_CONT
... ...