Skip to content

Commit

Permalink
Optimize code, sync master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Oct 18, 2024
1 parent a96b649 commit b28c2dd
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 72 deletions.
10 changes: 3 additions & 7 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,13 @@ enum swDNSLookupFlag {
SW_DNS_LOOKUP_RANDOM = (1u << 11),
};

#ifdef __MACH__
char *sw_error_();
#define sw_error sw_error_()
#else
extern __thread char sw_error[SW_ERROR_MSG_SIZE];
#endif
extern thread_local char sw_error[SW_ERROR_MSG_SIZE];

enum swProcessType {
SW_PROCESS_MASTER = 1,
SW_PROCESS_WORKER = 2,
SW_PROCESS_MANAGER = 3,
SW_PROCESS_EVENTWORKER = 2,
SW_PROCESS_TASKWORKER = 4,
SW_PROCESS_USERWORKER = 5,
};
Expand Down Expand Up @@ -753,7 +749,7 @@ double microtime(void);
} // namespace swoole

extern swoole::Global SwooleG; // Local Global Variable
extern __thread swoole::ThreadGlobal SwooleTG; // Thread Global Variable
extern thread_local swoole::ThreadGlobal SwooleTG; // Thread Global Variable

#define SW_CPU_NUM (SwooleG.cpu_num)

Expand Down
17 changes: 16 additions & 1 deletion include/swoole_process_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,29 @@ struct Worker {
void *ptr2;

ssize_t send_pipe_message(const void *buf, size_t n, int flags);
bool has_exceeded_max_request();
void set_max_request(uint32_t max_request, uint32_t max_request_grace);
void start();
void shutdown();
bool is_shutdown();
bool is_running();

void set_status(enum swWorkerStatus _status) {
status = _status;
}

void set_status_to_idle() {
set_status(SW_WORKER_IDLE);
}

void set_status_to_busy() {
set_status(SW_WORKER_BUSY);
}

void add_request_count() {
request_count++;
}

bool is_busy() {
return status == SW_WORKER_BUSY;
}
Expand Down Expand Up @@ -300,7 +316,6 @@ struct ProcessPool {
void set_protocol(enum ProtocolType _protocol_type);

void set_max_request(uint32_t _max_request, uint32_t _max_request_grace);
int get_max_request();
bool detach();
int wait();
int start();
Expand Down
12 changes: 2 additions & 10 deletions src/core/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,12 @@ static ssize_t getrandom(void *buffer, size_t size, unsigned int __flags) {
#endif

swoole::Global SwooleG = {};
__thread swoole::ThreadGlobal SwooleTG = {};
thread_local swoole::ThreadGlobal SwooleTG = {};
thread_local char sw_error[SW_ERROR_MSG_SIZE];

static std::unordered_map<std::string, void *> functions;
static swoole::Logger *g_logger_instance = nullptr;

#ifdef __MACH__
static __thread char _sw_error_buf[SW_ERROR_MSG_SIZE];
char *sw_error_() {
return _sw_error_buf;
}
#else
__thread char sw_error[SW_ERROR_MSG_SIZE];
#endif

static void swoole_fatal_error_impl(int code, const char *format, ...);

swoole::Logger *sw_logger() {
Expand Down
53 changes: 31 additions & 22 deletions src/os/process_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,6 @@ pid_t ProcessPool::spawn(Worker *worker) {
return pid;
}

int ProcessPool::get_max_request() {
int task_n;
if (max_request < 1) {
return -1;
} else {
task_n = max_request;
if (max_request_grace > 0) {
task_n += swoole_system_random(1, max_request_grace);
}
}
return task_n;
}

void ProcessPool::set_max_request(uint32_t _max_request, uint32_t _max_request_grace) {
max_request = _max_request;
max_request_grace = _max_request_grace;
Expand All @@ -518,12 +505,7 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
EventData buf;
} out{};

ssize_t n = 0, ret, worker_task_always = 0;
int task_n = pool->get_max_request();
if (task_n <= 0) {
worker_task_always = 1;
task_n = 1;
}
ssize_t n = 0, ret;

/**
* Use from_fd save the task_worker->id
Expand All @@ -536,7 +518,7 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
out.mtype = worker->id + 1;
}

while (pool->running && !SwooleWG.shutdown && task_n > 0) {
while (pool->running && !worker->is_shutdown() && !worker->has_exceeded_max_request()) {
/**
* fetch task
*/
Expand Down Expand Up @@ -609,8 +591,8 @@ static int ProcessPool_worker_loop_with_task_protocol(ProcessPool *pool, Worker
goto _alarm_handler;
}

if (ret >= 0 && !worker_task_always) {
task_n--;
if (ret >= 0) {
worker->add_request_count();
}
}
return SW_OK;
Expand Down Expand Up @@ -986,6 +968,30 @@ void ProcessPool::destroy() {
sw_mem_pool()->free(workers);
}

bool Worker::has_exceeded_max_request() {
return !SwooleWG.run_always && request_count >= SwooleWG.max_request;
}

void Worker::start() {
start_time = ::time(nullptr);
request_count = 0;
set_status_to_idle();
SwooleWG.running = true;
SwooleWG.shutdown = false;
}

void Worker::set_max_request(uint32_t max_request, uint32_t max_request_grace) {
if (max_request < 1) {
SwooleWG.run_always = true;
} else {
SwooleWG.run_always = false;
SwooleWG.max_request = max_request;
if (max_request_grace > 0) {
SwooleWG.max_request += swoole_system_random(1, max_request_grace);
}
}
}

void Worker::shutdown() {
status = SW_WORKER_EXIT;
SwooleWG.shutdown = true;
Expand All @@ -995,4 +1001,7 @@ bool Worker::is_shutdown() {
return SwooleWG.shutdown;
}

bool Worker::is_running() {
return SwooleWG.running;
}
} // namespace swoole
1 change: 0 additions & 1 deletion src/server/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
namespace swoole {

bool BaseFactory::start() {
SwooleWG.run_always = true;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ bool Server::reload(bool reload_all_workers) {
}

if (getpid() != gs->manager_pid) {
return swoole_kill(get_manager_pid(), reload_all_workers ? SIGUSR1 : SIGUSR2) != 0;
return swoole_kill(get_manager_pid(), reload_all_workers ? SIGUSR1 : SIGUSR2) == 0;
}

ProcessPool *pool = &gs->event_workers;
Expand Down
13 changes: 2 additions & 11 deletions src/server/master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,17 +575,8 @@ void Server::init_worker(Worker *worker) {
}
#endif

if (max_request < 1) {
SwooleWG.run_always = true;
} else {
SwooleWG.max_request = max_request;
if (max_request_grace > 0) {
SwooleWG.max_request += swoole_system_random(1, max_request_grace);
}
}

worker->start_time = ::time(nullptr);
worker->request_count = 0;
worker->start();
worker->set_max_request(max_request, max_request_grace);
}

int Server::start() {
Expand Down
4 changes: 0 additions & 4 deletions src/server/reactor_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ static int ReactorProcess_loop(ProcessPool *pool, Worker *worker) {
SwooleG.pid = getpid();

SwooleG.process_id = worker->id;
if (serv->max_request > 0) {
SwooleWG.run_always = false;
}
SwooleWG.max_request = serv->max_request;
SwooleWG.worker = worker;
SwooleTG.id = 0;

Expand Down
17 changes: 3 additions & 14 deletions src/server/task_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,9 @@ static void TaskWorker_onStart(ProcessPool *pool, Worker *worker) {
TaskWorker_signal_init(pool);
serv->worker_start_callback(worker);

worker->start_time = ::time(nullptr);
worker->request_count = 0;
worker->start();
worker->set_max_request(pool->max_request, pool->max_request_grace);
SwooleWG.worker = worker;
SwooleWG.worker->status = SW_WORKER_IDLE;
/**
* task_max_request
*/
if (pool->max_request > 0) {
SwooleWG.run_always = false;
SwooleWG.max_request = pool->get_max_request();
} else {
SwooleWG.run_always = true;
}
SwooleWG.shutdown = false;
}

static void TaskWorker_onStop(ProcessPool *pool, Worker *worker) {
Expand All @@ -228,7 +217,7 @@ static int TaskWorker_onPipeReceive(Reactor *reactor, Event *event) {
worker->status = SW_WORKER_IDLE;
worker->request_count++;
// maximum number of requests, process will exit.
if (!SwooleWG.run_always && worker->request_count >= SwooleWG.max_request) {
if (worker->has_exceeded_max_request()) {
serv->stop_async_worker(worker);
}
return retval;
Expand Down
2 changes: 1 addition & 1 deletion src/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void Server::worker_accept_event(DataHead *info) {
worker->status = SW_WORKER_IDLE;

// maximum number of requests, process will exit.
if (!SwooleWG.run_always && worker->request_count >= SwooleWG.max_request) {
if (worker->has_exceeded_max_request()) {
stop_async_worker(worker);
}
}
Expand Down

0 comments on commit b28c2dd

Please sign in to comment.