胡斌

add root directive cpu_affinity, to set the cpu affinity of srs

@@ -65,6 +65,9 @@ asprocess off; @@ -65,6 +65,9 @@ asprocess off;
65 # whether using build-in speex to aac transcoding 65 # whether using build-in speex to aac transcoding
66 # default: off 66 # default: off
67 speex2aac off; 67 speex2aac off;
  68 +# set cpu affinity,the index of the cpu core,0,1,2,...
  69 +# default -1,don't set affinity
  70 +cpu_affinity -1;
68 71
69 ############################################################################################# 72 #############################################################################################
70 # heartbeat/stats sections 73 # heartbeat/stats sections
@@ -47,6 +47,10 @@ using namespace std; @@ -47,6 +47,10 @@ using namespace std;
47 #include <srs_app_utility.hpp> 47 #include <srs_app_utility.hpp>
48 #include <srs_core_performance.hpp> 48 #include <srs_core_performance.hpp>
49 #include "../../objs/version.h" 49 #include "../../objs/version.h"
  50 +#ifndef __USE_GNU
  51 +#define __USE_GNU
  52 +#endif
  53 +#include<sched.h>
50 54
51 using namespace _srs_internal; 55 using namespace _srs_internal;
52 56
@@ -1007,8 +1011,6 @@ int SrsConfig::reload_conf(SrsConfig* conf) @@ -1007,8 +1011,6 @@ int SrsConfig::reload_conf(SrsConfig* conf)
1007 return ret; 1011 return ret;
1008 } 1012 }
1009 1013
1010 - set_config_static();  
1011 -  
1012 return ret; 1014 return ret;
1013 } 1015 }
1014 1016
@@ -1584,6 +1586,7 @@ int SrsConfig::check_config() @@ -1584,6 +1586,7 @@ int SrsConfig::check_config()
1584 && n != "http_api" && n != "stats" && n != "vhost" && n != "pithy_print_ms" 1586 && n != "http_api" && n != "stats" && n != "vhost" && n != "pithy_print_ms"
1585 && n != "http_stream" && n != "http_server" && n != "stream_caster" 1587 && n != "http_stream" && n != "http_server" && n != "stream_caster"
1586 && n != "utc_time" && n != "work_dir" && n != "asprocess" && n != "speex2aac" 1588 && n != "utc_time" && n != "work_dir" && n != "asprocess" && n != "speex2aac"
  1589 + && n != "cpu_affinity"
1587 ) { 1590 ) {
1588 ret = ERROR_SYSTEM_CONFIG_INVALID; 1591 ret = ERROR_SYSTEM_CONFIG_INVALID;
1589 srs_error("unsupported directive %s, ret=%d", n.c_str(), ret); 1592 srs_error("unsupported directive %s, ret=%d", n.c_str(), ret);
@@ -2102,7 +2105,23 @@ void SrsConfig::set_config_static() @@ -2102,7 +2105,23 @@ void SrsConfig::set_config_static()
2102 } 2105 }
2103 else 2106 else
2104 _speex2aac = false; 2107 _speex2aac = false;
2105 - 2108 +
  2109 + conf = root->get("cpu_affinity");
  2110 + int cpu_index = -1;
  2111 + if(conf){
  2112 + cpu_index = atoi(conf->arg0().c_str());
  2113 + }
  2114 + if(cpu_index >= 0){
  2115 + cpu_set_t mask;
  2116 + CPU_ZERO(&mask);
  2117 + CPU_SET(cpu_index, &mask);
  2118 + if (sched_setaffinity(0, sizeof(mask), &mask) == -1){
  2119 + srs_error("error when set cpu affinity to CPU:%d",cpu_index);
  2120 + }
  2121 + else{
  2122 + srs_trace("success in set cpu affinity to CPU:%d",cpu_index);
  2123 + }
  2124 + }
2106 } 2125 }
2107 2126
2108 int SrsConfig::parse_buffer(SrsConfigBuffer* buffer) 2127 int SrsConfig::parse_buffer(SrsConfigBuffer* buffer)