winlin

refine kbps, provides 30s,1m,5m,60m kbps. 0.9.97

@@ -172,9 +172,12 @@ int SrsEdgeIngester::ingest() @@ -172,9 +172,12 @@ int SrsEdgeIngester::ingest()
172 172
173 // pithy print 173 // pithy print
174 if (pithy_print.can_print()) { 174 if (pithy_print.can_print()) {
  175 + kbps->sample();
175 srs_trace("<- "SRS_LOG_ID_EDGE_PLAY 176 srs_trace("<- "SRS_LOG_ID_EDGE_PLAY
176 - " time=%"PRId64", okbps=%d, ikbps=%d",  
177 - pithy_print.age(), kbps->get_send_kbps(), kbps->get_recv_kbps()); 177 + " time=%"PRId64", okbps=%d,%d,%d, ikbps=%d,%d,%d",
  178 + pithy_print.age(),
  179 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  180 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
178 } 181 }
179 182
180 // read from client. 183 // read from client.
@@ -464,9 +467,12 @@ int SrsEdgeForwarder::cycle() @@ -464,9 +467,12 @@ int SrsEdgeForwarder::cycle()
464 467
465 // pithy print 468 // pithy print
466 if (pithy_print.can_print()) { 469 if (pithy_print.can_print()) {
  470 + kbps->sample();
467 srs_trace("-> "SRS_LOG_ID_EDGE_PUBLISH 471 srs_trace("-> "SRS_LOG_ID_EDGE_PUBLISH
468 - " time=%"PRId64", msgs=%d, okbps=%d, ikbps=%d",  
469 - pithy_print.age(), count, kbps->get_send_kbps(), kbps->get_recv_kbps()); 472 + " time=%"PRId64", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d",
  473 + pithy_print.age(), count,
  474 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  475 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
470 } 476 }
471 477
472 // ignore when no messages. 478 // ignore when no messages.
@@ -342,9 +342,12 @@ int SrsForwarder::forward() @@ -342,9 +342,12 @@ int SrsForwarder::forward()
342 342
343 // pithy print 343 // pithy print
344 if (pithy_print.can_print()) { 344 if (pithy_print.can_print()) {
  345 + kbps->sample();
345 srs_trace("-> "SRS_LOG_ID_FOWARDER 346 srs_trace("-> "SRS_LOG_ID_FOWARDER
346 - " time=%"PRId64", msgs=%d, okbps=%d, ikbps=%d",  
347 - pithy_print.age(), count, kbps->get_send_kbps(), kbps->get_recv_kbps()); 347 + " time=%"PRId64", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d",
  348 + pithy_print.age(), count,
  349 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  350 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
348 } 351 }
349 352
350 // ignore when no messages. 353 // ignore when no messages.
@@ -28,6 +28,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,6 +28,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_protocol_io.hpp> 28 #include <srs_protocol_io.hpp>
29 #include <srs_kernel_utility.hpp> 29 #include <srs_kernel_utility.hpp>
30 30
  31 +SrsKbpsSample::SrsKbpsSample()
  32 +{
  33 + bytes = time = 0;
  34 + kbps = 0;
  35 +}
  36 +
31 SrsKbpsSlice::SrsKbpsSlice() 37 SrsKbpsSlice::SrsKbpsSlice()
32 { 38 {
33 io.in = NULL; 39 io.in = NULL;
@@ -39,6 +45,59 @@ SrsKbpsSlice::~SrsKbpsSlice() @@ -39,6 +45,59 @@ SrsKbpsSlice::~SrsKbpsSlice()
39 { 45 {
40 } 46 }
41 47
  48 +int64_t SrsKbpsSlice::get_total_bytes()
  49 +{
  50 + return bytes + last_bytes - io_bytes_base;
  51 +}
  52 +
  53 +void SrsKbpsSlice::sample()
  54 +{
  55 + int64_t now = srs_get_system_time_ms();
  56 + int64_t total_bytes = get_total_bytes();
  57 +
  58 + if (sample_30s.time <= 0) {
  59 + sample_30s.kbps = 0;
  60 + sample_30s.time = now;
  61 + sample_30s.bytes = total_bytes;
  62 + }
  63 + if (sample_1m.time <= 0) {
  64 + sample_1m.kbps = 0;
  65 + sample_1m.time = now;
  66 + sample_1m.bytes = total_bytes;
  67 + }
  68 + if (sample_5m.time <= 0) {
  69 + sample_5m.kbps = 0;
  70 + sample_5m.time = now;
  71 + sample_5m.bytes = total_bytes;
  72 + }
  73 + if (sample_60m.time <= 0) {
  74 + sample_60m.kbps = 0;
  75 + sample_60m.time = now;
  76 + sample_60m.bytes = total_bytes;
  77 + }
  78 +
  79 + if (now - sample_30s.time > 30 * 1000) {
  80 + sample_30s.kbps = (total_bytes - sample_30s.bytes) * 8 / (now - sample_30s.time);
  81 + sample_30s.time = now;
  82 + sample_30s.bytes = total_bytes;
  83 + }
  84 + if (now - sample_1m.time > 60 * 1000) {
  85 + sample_1m.kbps = (total_bytes - sample_1m.bytes) * 8 / (now - sample_1m.time);
  86 + sample_1m.time = now;
  87 + sample_1m.bytes = total_bytes;
  88 + }
  89 + if (now - sample_5m.time > 300 * 1000) {
  90 + sample_5m.kbps = (total_bytes - sample_5m.bytes) * 8 / (now - sample_5m.time);
  91 + sample_5m.time = now;
  92 + sample_5m.bytes = total_bytes;
  93 + }
  94 + if (now - sample_60m.time > 3600 * 1000) {
  95 + sample_60m.kbps = (total_bytes - sample_60m.bytes) * 8 / (now - sample_60m.time);
  96 + sample_60m.time = now;
  97 + sample_60m.bytes = total_bytes;
  98 + }
  99 +}
  100 +
42 SrsKbps::SrsKbps() 101 SrsKbps::SrsKbps()
43 { 102 {
44 } 103 }
@@ -64,6 +123,8 @@ void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out) @@ -64,6 +123,8 @@ void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out)
64 if (in) { 123 if (in) {
65 is.last_bytes = is.io_bytes_base = in->get_recv_bytes(); 124 is.last_bytes = is.io_bytes_base = in->get_recv_bytes();
66 } 125 }
  126 + // resample
  127 + is.sample();
67 128
68 // set output stream 129 // set output stream
69 // now, set start time. 130 // now, set start time.
@@ -80,41 +141,74 @@ void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out) @@ -80,41 +141,74 @@ void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out)
80 if (out) { 141 if (out) {
81 os.last_bytes = os.io_bytes_base = out->get_send_bytes(); 142 os.last_bytes = os.io_bytes_base = out->get_send_bytes();
82 } 143 }
  144 + // resample
  145 + os.sample();
83 } 146 }
84 147
85 int SrsKbps::get_send_kbps() 148 int SrsKbps::get_send_kbps()
86 { 149 {
87 int64_t duration = srs_get_system_time_ms() - is.starttime; 150 int64_t duration = srs_get_system_time_ms() - is.starttime;
88 - int64_t bytes = get_send_bytes();  
89 if (duration <= 0) { 151 if (duration <= 0) {
90 return 0; 152 return 0;
91 } 153 }
  154 + int64_t bytes = get_send_bytes();
92 return bytes * 8 / duration; 155 return bytes * 8 / duration;
93 } 156 }
94 157
95 int SrsKbps::get_recv_kbps() 158 int SrsKbps::get_recv_kbps()
96 { 159 {
97 int64_t duration = srs_get_system_time_ms() - os.starttime; 160 int64_t duration = srs_get_system_time_ms() - os.starttime;
98 - int64_t bytes = get_recv_bytes();  
99 if (duration <= 0) { 161 if (duration <= 0) {
100 return 0; 162 return 0;
101 } 163 }
  164 + int64_t bytes = get_recv_bytes();
102 return bytes * 8 / duration; 165 return bytes * 8 / duration;
103 } 166 }
104 167
  168 +int SrsKbps::get_send_kbps_sample_high()
  169 +{
  170 + return os.sample_30s.kbps;
  171 +}
  172 +
  173 +int SrsKbps::get_recv_kbps_sample_high()
  174 +{
  175 + return is.sample_30s.kbps;
  176 +}
  177 +
  178 +int SrsKbps::get_send_kbps_sample_medium()
  179 +{
  180 + return os.sample_5m.kbps;
  181 +}
  182 +
  183 +int SrsKbps::get_recv_kbps_sample_medium()
  184 +{
  185 + return is.sample_5m.kbps;
  186 +}
  187 +
105 int64_t SrsKbps::get_send_bytes() 188 int64_t SrsKbps::get_send_bytes()
106 { 189 {
107 - if (os.io.out) {  
108 - os.last_bytes = os.io.out->get_send_bytes();  
109 - }  
110 - return os.bytes + os.last_bytes - os.io_bytes_base; 190 + return os.get_total_bytes();
111 } 191 }
112 192
113 int64_t SrsKbps::get_recv_bytes() 193 int64_t SrsKbps::get_recv_bytes()
114 { 194 {
  195 + return is.get_total_bytes();
  196 +}
  197 +
  198 +void SrsKbps::sample()
  199 +{
  200 + if (os.io.out) {
  201 + os.last_bytes = os.io.out->get_send_bytes();
  202 + }
  203 +
  204 + // resample
  205 + os.sample();
  206 +
115 if (is.io.in) { 207 if (is.io.in) {
116 is.last_bytes = is.io.in->get_recv_bytes(); 208 is.last_bytes = is.io.in->get_recv_bytes();
117 } 209 }
118 - return is.bytes + is.last_bytes - is.io_bytes_base; 210 +
  211 + // resample
  212 + is.sample();
119 } 213 }
120 214
@@ -34,7 +34,32 @@ class ISrsProtocolReader; @@ -34,7 +34,32 @@ class ISrsProtocolReader;
34 class ISrsProtocolWriter; 34 class ISrsProtocolWriter;
35 35
36 /** 36 /**
  37 +* a kbps sample, for example, 1minute kbps,
  38 +* 10minute kbps sample.
  39 +*/
  40 +class SrsKbpsSample
  41 +{
  42 +public:
  43 + int64_t bytes;
  44 + int64_t time;
  45 + int kbps;
  46 +public:
  47 + SrsKbpsSample();
  48 +};
  49 +
  50 +/**
37 * a slice of kbps statistic, for input or output. 51 * a slice of kbps statistic, for input or output.
  52 +* a slice contains a set of sessions, which has a base offset of bytes,
  53 +* where a slice is:
  54 +* starttime(oldest session startup time)
  55 +* bytes(total bytes of previous sessions)
  56 +* io_bytes_base(bytes offset of current session)
  57 +* last_bytes(bytes of current session)
  58 +* so, the total send bytes now is:
  59 +* send_bytes = bytes + last_bytes - io_bytes_base
  60 +* so, the bytes sent duration current session is:
  61 +* send_bytes = last_bytes - io_bytes_base
  62 +* @remark user use set_io to start new session.
38 */ 63 */
39 class SrsKbpsSlice 64 class SrsKbpsSlice
40 { 65 {
@@ -45,17 +70,34 @@ private: @@ -45,17 +70,34 @@ private:
45 }; 70 };
46 public: 71 public:
47 slice_io io; 72 slice_io io;
  73 + // session startup bytes
  74 + // @remark, use total_bytes() to get the total bytes of slice.
48 int64_t bytes; 75 int64_t bytes;
  76 + // slice starttime, the first time to record bytes.
49 int64_t starttime; 77 int64_t starttime;
50 - // startup bytes number for io when set it, 78 + // session startup bytes number for io when set it,
51 // the base offset of bytes for io. 79 // the base offset of bytes for io.
52 int64_t io_bytes_base; 80 int64_t io_bytes_base;
53 // last updated bytes number, 81 // last updated bytes number,
54 // cache for io maybe freed. 82 // cache for io maybe freed.
55 int64_t last_bytes; 83 int64_t last_bytes;
  84 + // samples
  85 + SrsKbpsSample sample_30s;
  86 + SrsKbpsSample sample_1m;
  87 + SrsKbpsSample sample_5m;
  88 + SrsKbpsSample sample_60m;
56 public: 89 public:
57 SrsKbpsSlice(); 90 SrsKbpsSlice();
58 virtual ~SrsKbpsSlice(); 91 virtual ~SrsKbpsSlice();
  92 +public:
  93 + /**
  94 + * get current total bytes.
  95 + */
  96 + virtual int64_t get_total_bytes();
  97 + /**
  98 + * resample all samples.
  99 + */
  100 + virtual void sample();
59 }; 101 };
60 102
61 /** 103 /**
@@ -71,6 +113,7 @@ public: @@ -71,6 +113,7 @@ public:
71 virtual ~SrsKbps(); 113 virtual ~SrsKbps();
72 public: 114 public:
73 /** 115 /**
  116 + * set io to start new session.
74 * set the underlayer reader/writer, 117 * set the underlayer reader/writer,
75 * if the io destroied, for instance, the forwarder reconnect, 118 * if the io destroied, for instance, the forwarder reconnect,
76 * user must set the io of SrsKbps to NULL to continue to use the kbps object. 119 * user must set the io of SrsKbps to NULL to continue to use the kbps object.
@@ -82,15 +125,28 @@ public: @@ -82,15 +125,28 @@ public:
82 public: 125 public:
83 /** 126 /**
84 * get total kbps, duration is from the startup of io. 127 * get total kbps, duration is from the startup of io.
  128 + * @remark, use sample() to update data.
85 */ 129 */
86 virtual int get_send_kbps(); 130 virtual int get_send_kbps();
87 virtual int get_recv_kbps(); 131 virtual int get_recv_kbps();
  132 + // 30s
  133 + virtual int get_send_kbps_sample_high();
  134 + virtual int get_recv_kbps_sample_high();
  135 + // 5m
  136 + virtual int get_send_kbps_sample_medium();
  137 + virtual int get_recv_kbps_sample_medium();
88 public: 138 public:
89 /** 139 /**
90 * get the total send/recv bytes, from the startup of the oldest io. 140 * get the total send/recv bytes, from the startup of the oldest io.
  141 + * @remark, use sample() to update data.
91 */ 142 */
92 virtual int64_t get_send_bytes(); 143 virtual int64_t get_send_bytes();
93 virtual int64_t get_recv_bytes(); 144 virtual int64_t get_recv_bytes();
  145 +public:
  146 + /**
  147 + * resample all samples.
  148 + */
  149 + virtual void sample();
94 }; 150 };
95 151
96 #endif 152 #endif
@@ -504,9 +504,12 @@ int SrsRtmpConn::playing(SrsSource* source) @@ -504,9 +504,12 @@ int SrsRtmpConn::playing(SrsSource* source)
504 504
505 // reportable 505 // reportable
506 if (pithy_print.can_print()) { 506 if (pithy_print.can_print()) {
  507 + kbps->sample();
507 srs_trace("-> "SRS_LOG_ID_PLAY 508 srs_trace("-> "SRS_LOG_ID_PLAY
508 - " time=%"PRId64", duration=%"PRId64", msgs=%d, okbps=%d, ikbps=%d",  
509 - pithy_print.age(), duration, count, kbps->get_send_kbps(), kbps->get_recv_kbps()); 509 + " time=%"PRId64", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d",
  510 + pithy_print.age(), count,
  511 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  512 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
510 } 513 }
511 514
512 if (count <= 0) { 515 if (count <= 0) {
@@ -590,9 +593,11 @@ int SrsRtmpConn::fmle_publish(SrsSource* source) @@ -590,9 +593,11 @@ int SrsRtmpConn::fmle_publish(SrsSource* source)
590 593
591 // reportable 594 // reportable
592 if (pithy_print.can_print()) { 595 if (pithy_print.can_print()) {
  596 + kbps->sample();
593 srs_trace("<- "SRS_LOG_ID_CLIENT_PUBLISH 597 srs_trace("<- "SRS_LOG_ID_CLIENT_PUBLISH
594 - " time=%"PRId64", okbps=%d, ikbps=%d",  
595 - pithy_print.age(), kbps->get_send_kbps(), kbps->get_recv_kbps()); 598 + " time=%"PRId64", okbps=%d,%d,%d, ikbps=%d,%d,%d", pithy_print.age(),
  599 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  600 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
596 } 601 }
597 602
598 // process UnPublish event. 603 // process UnPublish event.
@@ -666,9 +671,12 @@ int SrsRtmpConn::flash_publish(SrsSource* source) @@ -666,9 +671,12 @@ int SrsRtmpConn::flash_publish(SrsSource* source)
666 671
667 // reportable 672 // reportable
668 if (pithy_print.can_print()) { 673 if (pithy_print.can_print()) {
  674 + kbps->sample();
669 srs_trace("<- "SRS_LOG_ID_WEB_PUBLISH 675 srs_trace("<- "SRS_LOG_ID_WEB_PUBLISH
670 - " time=%"PRId64", okbps=%d, ikbps=%d",  
671 - pithy_print.age(), kbps->get_send_kbps(), kbps->get_recv_kbps()); 676 + " time=%"PRId64", okbps=%d,%d,%d, ikbps=%d,%d,%d",
  677 + pithy_print.age(),
  678 + kbps->get_send_kbps(), kbps->get_send_kbps_sample_high(), kbps->get_send_kbps_sample_medium(),
  679 + kbps->get_recv_kbps(), kbps->get_recv_kbps_sample_high(), kbps->get_recv_kbps_sample_medium());
672 } 680 }
673 681
674 // process UnPublish event. 682 // process UnPublish event.
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "96" 34 +#define VERSION_REVISION "97"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"