Implement multi-core CPU utilization for wssshc

- Added CPU affinity management to distribute tunnel threads across available CPU cores
- Implemented round-robin CPU core assignment for optimal load distribution
- Added _GNU_SOURCE define to enable CPU affinity functions
- Updated configure.sh to include -D_GNU_SOURCE in CFLAGS for proper compilation
- Enhanced parallel processing capabilities for multiple concurrent tunnels
- Updated CHANGELOG.md and TODO.md with multi-core optimization details
parent f53f0220
...@@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Increased select() timeout from 50ms to 200ms in tunnel forwarding threads - Increased select() timeout from 50ms to 200ms in tunnel forwarding threads
- Reduced polling frequency by 75% to minimize CPU overhead during data transfers - Reduced polling frequency by 75% to minimize CPU overhead during data transfers
- Maintained responsiveness while dramatically improving efficiency for bulk transfers - Maintained responsiveness while dramatically improving efficiency for bulk transfers
- **Multi-Core CPU Utilization**: Enhanced thread distribution across CPU cores
- Implemented CPU affinity management for tunnel forwarding threads
- Added round-robin CPU core assignment to distribute load across available cores
- Improved parallel processing capabilities for multiple concurrent tunnels
### Technical Details ### Technical Details
- **Import Management**: Added explicit `websockets` import to server.py for PyInstaller compatibility - **Import Management**: Added explicit `websockets` import to server.py for PyInstaller compatibility
......
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
- Increased select() timeout from 50ms to 200ms in tunnel forwarding threads - Increased select() timeout from 50ms to 200ms in tunnel forwarding threads
- Reduced polling frequency by 75% to minimize CPU overhead during data transfers - Reduced polling frequency by 75% to minimize CPU overhead during data transfers
- Maintained responsiveness while dramatically improving efficiency for bulk transfers - Maintained responsiveness while dramatically improving efficiency for bulk transfers
- [x] **Multi-Core CPU Utilization**: Enhanced thread distribution across CPU cores
- Implemented CPU affinity management for tunnel forwarding threads
- Added round-robin CPU core assignment to distribute load across available cores
- Improved parallel processing capabilities for multiple concurrent tunnels
## Recently Completed (v1.4.8) ## Recently Completed (v1.4.8)
- [x] **Critical SSL Connection Stability Issues**: Comprehensive SSL error handling and connection resilience improvements - [x] **Critical SSL Connection Stability Issues**: Comprehensive SSL error handling and connection resilience improvements
......
...@@ -52,7 +52,7 @@ cat > Makefile << 'EOF' ...@@ -52,7 +52,7 @@ cat > Makefile << 'EOF'
# Makefile for wssshtools # Makefile for wssshtools
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -O2 $(shell pkg-config --cflags openssl) CFLAGS = -Wall -Wextra -O2 -D_GNU_SOURCE $(shell pkg-config --cflags openssl)
LDFLAGS = $(shell pkg-config --libs openssl) LDFLAGS = $(shell pkg-config --libs openssl)
# Source files # Source files
......
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <sched.h>
#include <pthread.h>
#define INITIAL_FRAME_BUFFER_SIZE 8192 #define INITIAL_FRAME_BUFFER_SIZE 8192
...@@ -44,6 +46,41 @@ pthread_mutex_t tunnel_mutex = PTHREAD_MUTEX_INITIALIZER; ...@@ -44,6 +46,41 @@ pthread_mutex_t tunnel_mutex = PTHREAD_MUTEX_INITIALIZER;
// Global shutdown flag for threads // Global shutdown flag for threads
volatile int global_shutdown = 0; volatile int global_shutdown = 0;
// CPU affinity management
static int num_cpu_cores = 0;
static int next_cpu_core = 0;
static pthread_mutex_t cpu_affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
// Initialize CPU core detection
void init_cpu_affinity() {
num_cpu_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (num_cpu_cores <= 0) {
num_cpu_cores = 1; // Fallback to 1 core
}
next_cpu_core = 0;
}
// Set CPU affinity for a thread to distribute across cores
void set_thread_cpu_affinity(pthread_t thread) {
if (num_cpu_cores <= 1) {
return; // No need to set affinity for single core
}
pthread_mutex_lock(&cpu_affinity_mutex);
int target_core = next_cpu_core;
next_cpu_core = (next_cpu_core + 1) % num_cpu_cores;
pthread_mutex_unlock(&cpu_affinity_mutex);
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(target_core, &cpuset);
if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset) != 0) {
// Silently ignore affinity setting failures - not critical
// fprintf(stderr, "Warning: Failed to set CPU affinity for thread\n");
}
}
frame_buffer_t *frame_buffer_init() { frame_buffer_t *frame_buffer_init() {
frame_buffer_t *fb = malloc(sizeof(frame_buffer_t)); frame_buffer_t *fb = malloc(sizeof(frame_buffer_t));
if (!fb) return NULL; if (!fb) return NULL;
...@@ -252,6 +289,7 @@ void handle_tunnel_request(SSL *ssl, const char *request_id, int debug, const ch ...@@ -252,6 +289,7 @@ void handle_tunnel_request(SSL *ssl, const char *request_id, int debug, const ch
pthread_t thread; pthread_t thread;
pthread_create(&thread, NULL, forward_ws_to_ssh_server, thread_args); pthread_create(&thread, NULL, forward_ws_to_ssh_server, thread_args);
set_thread_cpu_affinity(thread); // Distribute thread across CPU cores
pthread_detach(thread); pthread_detach(thread);
} }
} }
......
...@@ -78,4 +78,8 @@ void cleanup_tunnel(int debug); ...@@ -78,4 +78,8 @@ void cleanup_tunnel(int debug);
int reconnect_websocket(tunnel_t *tunnel, const char *wssshd_host, int wssshd_port, const char *client_id, const char *request_id, int debug); int reconnect_websocket(tunnel_t *tunnel, const char *wssshd_host, int wssshd_port, const char *client_id, const char *request_id, int debug);
int setup_tunnel(const char *wssshd_host, int wssshd_port, const char *client_id, int local_port, int debug, int use_buffer); int setup_tunnel(const char *wssshd_host, int wssshd_port, const char *client_id, int local_port, int debug, int use_buffer);
// CPU affinity functions
void init_cpu_affinity(void);
void set_thread_cpu_affinity(pthread_t thread);
#endif // TUNNEL_H #endif // TUNNEL_H
\ No newline at end of file
...@@ -907,6 +907,9 @@ int main(int argc, char *argv[]) { ...@@ -907,6 +907,9 @@ int main(int argc, char *argv[]) {
.debug = 0 .debug = 0
}; };
// Initialize CPU affinity system for multi-core utilization
init_cpu_affinity();
pthread_mutex_init(&tunnel_mutex, NULL); pthread_mutex_init(&tunnel_mutex, NULL);
// Set up signal handler for SIGINT // Set up signal handler for SIGINT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment