winlin

support got uptime and loadavg for osx. 2.0.145

@@ -849,10 +849,12 @@ int SrsServer::do_cycle() @@ -849,10 +849,12 @@ int SrsServer::do_cycle()
849 srs_info("update memory info, usage/free."); 849 srs_info("update memory info, usage/free.");
850 srs_update_meminfo(); 850 srs_update_meminfo();
851 } 851 }
  852 +#endif
852 if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) { 853 if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) {
853 srs_info("update platform info, uptime/load."); 854 srs_info("update platform info, uptime/load.");
854 srs_update_platform_info(); 855 srs_update_platform_info();
855 } 856 }
  857 +#ifndef SRS_OSX
856 if ((i % SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES) == 0) { 858 if ((i % SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES) == 0) {
857 srs_info("update network devices info."); 859 srs_info("update network devices info.");
858 srs_update_network_devices(); 860 srs_update_network_devices();
@@ -28,6 +28,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,6 +28,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <ifaddrs.h> 28 #include <ifaddrs.h>
29 #include <arpa/inet.h> 29 #include <arpa/inet.h>
30 30
  31 +#ifdef SRS_OSX
  32 +#include <sys/sysctl.h>
  33 +#endif
31 using namespace std; 34 using namespace std;
32 35
33 #include <srs_kernel_log.hpp> 36 #include <srs_kernel_log.hpp>
@@ -666,6 +669,7 @@ void srs_update_platform_info() @@ -666,6 +669,7 @@ void srs_update_platform_info()
666 669
667 r.srs_startup_time = srs_get_system_startup_time_ms(); 670 r.srs_startup_time = srs_get_system_startup_time_ms();
668 671
  672 +#ifndef SRS_OSX
669 if (true) { 673 if (true) {
670 FILE* f = fopen("/proc/uptime", "r"); 674 FILE* f = fopen("/proc/uptime", "r");
671 if (f == NULL) { 675 if (f == NULL) {
@@ -694,6 +698,43 @@ void srs_update_platform_info() @@ -694,6 +698,43 @@ void srs_update_platform_info()
694 698
695 fclose(f); 699 fclose(f);
696 } 700 }
  701 +#else
  702 + // man 3 sysctl
  703 + if (true) {
  704 + struct timeval tv;
  705 + size_t len = sizeof(timeval);
  706 +
  707 + int mib[2];
  708 + mib[0] = CTL_KERN;
  709 + mib[1] = KERN_BOOTTIME;
  710 + if (sysctl(mib, 2, &tv, &len, NULL, 0) < 0) {
  711 + srs_warn("sysctl boottime failed, ignore");
  712 + return;
  713 + }
  714 +
  715 + time_t bsec = tv.tv_sec;
  716 + time_t csec = ::time(NULL);
  717 + r.os_uptime = difftime(csec, bsec);
  718 + }
  719 +
  720 + // man 3 sysctl
  721 + if (true) {
  722 + struct loadavg la;
  723 + size_t len = sizeof(loadavg);
  724 +
  725 + int mib[2];
  726 + mib[0] = CTL_VM;
  727 + mib[1] = VM_LOADAVG;
  728 + if (sysctl(mib, 2, &la, &len, NULL, 0) < 0) {
  729 + srs_warn("sysctl loadavg failed, ignore");
  730 + return;
  731 + }
  732 +
  733 + r.load_one_minutes = (double)la.ldavg[0] / la.fscale;
  734 + r.load_five_minutes = (double)la.ldavg[1] / la.fscale;
  735 + r.load_fifteen_minutes = (double)la.ldavg[2] / la.fscale;
  736 + }
  737 +#endif
697 738
698 r.ok = true; 739 r.ok = true;
699 } 740 }
@@ -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 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 144 34 +#define VERSION_REVISION 145
35 35
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"