winlin

fix #109: fix the system jump time, adjust system startup time. 0.9.135

@@ -242,6 +242,7 @@ Supported operating systems and hardware: @@ -242,6 +242,7 @@ Supported operating systems and hardware:
242 * 2013-10-17, Created.<br/> 242 * 2013-10-17, Created.<br/>
243 243
244 ## History 244 ## History
  245 +* v1.0, 2014-06-25, fix [#109](https://github.com/winlinvip/simple-rtmp-server/issues/109), fix the system jump time, adjust system startup time. 0.9.135
245 * <strong>v1.0, 2014-06-27, [1.0 mainline5(0.9.134)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.mainline5) released. 41573 lines.</strong> 246 * <strong>v1.0, 2014-06-27, [1.0 mainline5(0.9.134)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.mainline5) released. 41573 lines.</strong>
246 * v1.0, 2014-06-27, SRS online 30days with RTMP/HLS. 247 * v1.0, 2014-06-27, SRS online 30days with RTMP/HLS.
247 * v1.0, 2014-06-25, fix [#108](https://github.com/winlinvip/simple-rtmp-server/issues/108), support config time jitter for encoder non-monotonical stream. 0.9.133 248 * v1.0, 2014-06-25, fix [#108](https://github.com/winlinvip/simple-rtmp-server/issues/108), support config time jitter for encoder non-monotonical stream. 0.9.133
@@ -61,6 +61,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -61,6 +61,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61 61
62 // update time interval: 62 // update time interval:
63 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_TIME_RESOLUTION_MS_TIMES 63 // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_TIME_RESOLUTION_MS_TIMES
  64 +// @see SYS_TIME_RESOLUTION_US
64 #define SRS_SYS_TIME_RESOLUTION_MS_TIMES 3 65 #define SRS_SYS_TIME_RESOLUTION_MS_TIMES 3
65 66
66 // update rusage interval: 67 // update rusage interval:
@@ -399,6 +400,9 @@ int SrsServer::initialize() @@ -399,6 +400,9 @@ int SrsServer::initialize()
399 { 400 {
400 int ret = ERROR_SUCCESS; 401 int ret = ERROR_SUCCESS;
401 402
  403 + // ensure the time is ok.
  404 + srs_update_system_time_ms();
  405 +
402 // for the main objects(server, config, log, context), 406 // for the main objects(server, config, log, context),
403 // never subscribe handler in constructor, 407 // never subscribe handler in constructor,
404 // instead, subscribe handler in initialize method. 408 // instead, subscribe handler in initialize method.
@@ -77,7 +77,6 @@ void srs_update_system_rusage() @@ -77,7 +77,6 @@ void srs_update_system_rusage()
77 return; 77 return;
78 } 78 }
79 79
80 - srs_update_system_time_ms();  
81 _srs_system_rusage.sample_time = srs_get_system_time_ms(); 80 _srs_system_rusage.sample_time = srs_get_system_time_ms();
82 81
83 _srs_system_rusage.ok = true; 82 _srs_system_rusage.ok = true;
@@ -233,8 +232,6 @@ bool get_proc_self_stat(SrsProcSelfStat& r) @@ -233,8 +232,6 @@ bool get_proc_self_stat(SrsProcSelfStat& r)
233 232
234 void srs_update_proc_stat() 233 void srs_update_proc_stat()
235 { 234 {
236 - srs_update_system_time_ms();  
237 -  
238 // system cpu stat 235 // system cpu stat
239 if (true) { 236 if (true) {
240 SrsProcSystemStat r; 237 SrsProcSystemStat r;
@@ -266,7 +263,6 @@ void srs_update_proc_stat() @@ -266,7 +263,6 @@ void srs_update_proc_stat()
266 return; 263 return;
267 } 264 }
268 265
269 - srs_update_system_time_ms();  
270 r.sample_time = srs_get_system_time_ms(); 266 r.sample_time = srs_get_system_time_ms();
271 267
272 // calc usage in percent 268 // calc usage in percent
@@ -390,7 +386,7 @@ SrsPlatformInfo::SrsPlatformInfo() @@ -390,7 +386,7 @@ SrsPlatformInfo::SrsPlatformInfo()
390 { 386 {
391 ok = false; 387 ok = false;
392 388
393 - srs_startup_time = srs_get_system_time_ms(); 389 + srs_startup_time = 0;
394 390
395 os_uptime = 0; 391 os_uptime = 0;
396 os_ilde_time = 0; 392 os_ilde_time = 0;
@@ -412,6 +408,8 @@ void srs_update_platform_info() @@ -412,6 +408,8 @@ void srs_update_platform_info()
412 SrsPlatformInfo& r = _srs_system_platform_info; 408 SrsPlatformInfo& r = _srs_system_platform_info;
413 r.ok = true; 409 r.ok = true;
414 410
  411 + r.srs_startup_time = srs_get_system_startup_time_ms();
  412 +
415 if (true) { 413 if (true) {
416 FILE* f = fopen("/proc/uptime", "r"); 414 FILE* f = fopen("/proc/uptime", "r");
417 if (f == NULL) { 415 if (f == NULL) {
@@ -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 "134" 34 +#define VERSION_REVISION "135"
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"
@@ -33,7 +33,13 @@ using namespace std; @@ -33,7 +33,13 @@ using namespace std;
33 33
34 #include <srs_kernel_log.hpp> 34 #include <srs_kernel_log.hpp>
35 35
  36 +// this value must:
  37 +// equals to (SRS_SYS_CYCLE_INTERVAL*SRS_SYS_TIME_RESOLUTION_MS_TIMES)*1000
  38 +// @see SRS_SYS_TIME_RESOLUTION_MS_TIMES
  39 +#define SYS_TIME_RESOLUTION_US 300*1000
  40 +
36 static int64_t _srs_system_time_us_cache = 0; 41 static int64_t _srs_system_time_us_cache = 0;
  42 +static int64_t _srs_system_time_startup_time = 0;
37 43
38 int64_t srs_get_system_time_ms() 44 int64_t srs_get_system_time_ms()
39 { 45 {
@@ -43,7 +49,14 @@ int64_t srs_get_system_time_ms() @@ -43,7 +49,14 @@ int64_t srs_get_system_time_ms()
43 49
44 return _srs_system_time_us_cache / 1000; 50 return _srs_system_time_us_cache / 1000;
45 } 51 }
  52 +int64_t srs_get_system_startup_time_ms()
  53 +{
  54 + if (_srs_system_time_startup_time <= 0) {
  55 + srs_update_system_time_ms();
  56 + }
46 57
  58 + return _srs_system_time_startup_time / 1000;
  59 +}
47 void srs_update_system_time_ms() 60 void srs_update_system_time_ms()
48 { 61 {
49 timeval now; 62 timeval now;
@@ -56,12 +69,32 @@ void srs_update_system_time_ms() @@ -56,12 +69,32 @@ void srs_update_system_time_ms()
56 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/35 69 // @see: https://github.com/winlinvip/simple-rtmp-server/issues/35
57 // we must convert the tv_sec/tv_usec to int64_t. 70 // we must convert the tv_sec/tv_usec to int64_t.
58 int64_t now_us = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec; 71 int64_t now_us = ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
59 - if (now_us < _srs_system_time_us_cache) {  
60 - srs_warn("system time negative, "  
61 - "history=%"PRId64"us, now=%"PRId64"", _srs_system_time_us_cache, now_us); 72 +
  73 + // for some ARM os, the starttime maybe invalid,
  74 + // for example, on the cubieboard2, the srs_startup_time is 1262304014640,
  75 + // while now is 1403842979210 in ms, diff is 141538964570 ms, 1638 days
  76 + // it's impossible, and maybe the problem of startup time is invalid.
  77 + // use date +%s to get system time is 1403844851.
  78 + // so we use relative time.
  79 + if (_srs_system_time_us_cache <= 0) {
  80 + _srs_system_time_us_cache = now_us;
  81 + _srs_system_time_startup_time = now_us;
  82 + return;
  83 + }
  84 +
  85 + // use relative time.
  86 + int64_t diff = now_us - _srs_system_time_us_cache;
  87 + diff = srs_max(0, diff);
  88 + if (diff < 0 || diff > 1000 * SYS_TIME_RESOLUTION_US) {
  89 + srs_warn("system time jump, history=%"PRId64"us, now=%"PRId64"us, diff=%"PRId64"us",
  90 + _srs_system_time_us_cache, now_us, diff);
  91 + // @see: https://github.com/winlinvip/simple-rtmp-server/issues/109
  92 + _srs_system_time_startup_time += diff;
62 } 93 }
63 94
64 _srs_system_time_us_cache = now_us; 95 _srs_system_time_us_cache = now_us;
  96 + srs_info("system time updated, startup=%"PRId64"us, now=%"PRId64"us",
  97 + _srs_system_time_startup_time, _srs_system_time_us_cache);
65 } 98 }
66 99
67 string srs_dns_resolve(string host) 100 string srs_dns_resolve(string host)
@@ -38,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -38,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 38
39 // get current system time in ms, use cache to avoid performance problem 39 // get current system time in ms, use cache to avoid performance problem
40 extern int64_t srs_get_system_time_ms(); 40 extern int64_t srs_get_system_time_ms();
  41 +extern int64_t srs_get_system_startup_time_ms();
41 // the deamon st-thread will update it. 42 // the deamon st-thread will update it.
42 extern void srs_update_system_time_ms(); 43 extern void srs_update_system_time_ms();
43 44