Skip to content

Commit

Permalink
Add cpu pinning for relay threads and udp listen sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-signal committed Feb 8, 2024
1 parent 43c5ff1 commit d14a567
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/apps/common/apputils.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ int set_raw_socket_tos(evutil_socket_t fd, int family, int tos) {
return 0;
}

// Signal change to add cpu pinning

int set_raw_socket_incoming_cpu(evutil_socket_t fd, int cpu) {
return setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu));
}

int is_stream_socket(int st) {
switch (st) {
case TCP_SOCKET:
Expand Down
2 changes: 2 additions & 0 deletions src/apps/common/apputils.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ int handle_socket_error(void);

int set_raw_socket_tos(evutil_socket_t fd, int family, int tos);
int set_raw_socket_ttl(evutil_socket_t fd, int family, int ttl);
// Signal change to add cpu pinning
int set_raw_socket_incoming_cpu(evutil_socket_t fd, int cpu);
int get_raw_socket_tos(evutil_socket_t fd, int family);
int get_raw_socket_ttl(evutil_socket_t fd, int family);

Expand Down
3 changes: 3 additions & 0 deletions src/apps/relay/dtls_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ static int create_server_socket(dtls_listener_relay_server_type *server, int rep
set_raw_socket_ttl_options(udp_listen_fd, server->addr.ss.sa_family);
set_raw_socket_tos_options(udp_listen_fd, server->addr.ss.sa_family);

// Signal change to add cpu pinning
set_raw_socket_incoming_cpu(udp_listen_fd, server->ts->id);

{
const int max_binding_time = 60;
int addr_bind_cycle = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/server/ns_turn_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
// Signal change to add cpu pinning
#define _GNU_SOURCE
#include <sched.h>

#include "ns_turn_server.h"

Expand Down Expand Up @@ -5016,6 +5019,14 @@ void init_turn_server(turn_turnserver *server, turnserver_id id, int verbose, io

// Signal change to add rtt metrics
server->rtt_ms_mins = ur_map_create();

// Signal change to add cpu pinning
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(id, &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "turn server id=%d, could not set cpu affinity\n", (int) id);
}
}

ioa_engine_handle turn_server_get_engine(turn_turnserver *s) {
Expand Down

0 comments on commit d14a567

Please sign in to comment.