From 23084c8c462808c3f13db3922aa018c9243e0b1f Mon Sep 17 00:00:00 2001 From: freeeyes Date: Thu, 10 Aug 2023 09:18:47 +0800 Subject: [PATCH] fix windows compile error. --- Build/Linux/server_config.json | 3 ++- Build/Linux/server_config_test_gcov.json | 3 ++- Build/Windows/server_config.json | 3 ++- Common/define.h | 17 ++++++++++--- PSS_ASIO/Common/IoContextPool.cpp | 7 +++++- PSS_ASIO/Common/IoContextPool.h | 2 +- PSS_ASIO/Common/serverconfig.cpp | 1 + PSS_ASIO/Common/serverconfigtype.h | 1 + PSS_ASIO/PSS_ASIO.cpp | 31 +----------------------- 9 files changed, 29 insertions(+), 39 deletions(-) diff --git a/Build/Linux/server_config.json b/Build/Linux/server_config.json index 23ee2de..8049511 100644 --- a/Build/Linux/server_config.json +++ b/Build/Linux/server_config.json @@ -5,7 +5,8 @@ "work time check": 60, "server to server time check": 30, "client connect timeout" : 60, - "IO send data check": 0 + "IO send data check": 0, + "logic thread bind cpu": 0 }, "packet parse library": [ diff --git a/Build/Linux/server_config_test_gcov.json b/Build/Linux/server_config_test_gcov.json index 5c6108e..3ab7b0a 100644 --- a/Build/Linux/server_config_test_gcov.json +++ b/Build/Linux/server_config_test_gcov.json @@ -5,7 +5,8 @@ "work time check": 60, "server to server time check": 30, "client connect timeout" : 60, - "IO send data check": 0 + "IO send data check": 0, + "logic thread bind cpu": 0 }, "packet parse library": [ diff --git a/Build/Windows/server_config.json b/Build/Windows/server_config.json index e60f1b5..c2752bd 100644 --- a/Build/Windows/server_config.json +++ b/Build/Windows/server_config.json @@ -5,7 +5,8 @@ "work time check": 60, "server to server time check": 30, "client connect timeout": 60, - "IO send data check": 0 + "IO send data check": 0, + "logic thread bind cpu": 0 }, "packet parse library": [ diff --git a/Common/define.h b/Common/define.h index 31d2d67..fab4e36 100644 --- a/Common/define.h +++ b/Common/define.h @@ -280,23 +280,32 @@ inline vector string_split(const string& srcStr, const string& deli return vec; } -inline void bind_thread_to_cpu(std::thread* ptrthread) +//设定绑定的CPU +inline void bind_thread_to_cpu(std::thread* logic_thread) { -#if PSS_PLATFORM != PLATFORM_WIN - if(nullptr == ptrthread) + if(nullptr == logic_thread) { return; } + static std::size_t cpunum = std::thread::hardware_concurrency(); static std::size_t cpuidx = 0; + +#if PSS_PLATFORM != PLATFORM_WIN cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET((cpuidx++)%cpunum,&cpuset); - int rc =pthread_setaffinity_np(ptrthread->native_handle(),sizeof(cpu_set_t), &cpuset); + int rc =pthread_setaffinity_np(logic_thread->native_handle(), sizeof(cpu_set_t), &cpuset); if (rc != 0) { PSS_LOGGER_ERROR("[bind_thread_to_cpu]Error calling pthread_setaffinity_np:{}",rc); } +#else + auto mask = SetThreadAffinityMask(logic_thread->native_handle(), (cpuidx++) % cpunum); + if (mask == 0) + { + PSS_LOGGER_ERROR("[bind_thread_to_cpu]Error calling SetThreadAffinityMask:{}", cpunum); + } #endif } diff --git a/PSS_ASIO/Common/IoContextPool.cpp b/PSS_ASIO/Common/IoContextPool.cpp index aa088f6..98ec542 100644 --- a/PSS_ASIO/Common/IoContextPool.cpp +++ b/PSS_ASIO/Common/IoContextPool.cpp @@ -30,7 +30,12 @@ void CIoContextPool::run() { std::shared_ptr thread(new std::thread(std::bind(&IoContextRun, io_contexts_[i]))); threads.push_back(thread); - bind_thread_to_cpu(thread.get()); + + //CPU󶨹ϵ + if (App_ServerConfig::instance()->get_config_workthread().logic_thread_bind_cpu != 0) + { + bind_thread_to_cpu(thread.get()); + } } // Wait for all threads in the pool to exit. diff --git a/PSS_ASIO/Common/IoContextPool.h b/PSS_ASIO/Common/IoContextPool.h index 4636f78..a07f052 100644 --- a/PSS_ASIO/Common/IoContextPool.h +++ b/PSS_ASIO/Common/IoContextPool.h @@ -1,7 +1,7 @@ #ifndef _PSS_ASIO_IO_SERVICE_POOL_H_ #define _PSS_ASIO_IO_SERVICE_POOL_H_ -#include "define.h" +#include "serverconfig.h" #include #include diff --git a/PSS_ASIO/Common/serverconfig.cpp b/PSS_ASIO/Common/serverconfig.cpp index 0b76685..44745f2 100644 --- a/PSS_ASIO/Common/serverconfig.cpp +++ b/PSS_ASIO/Common/serverconfig.cpp @@ -15,6 +15,7 @@ bool CServerConfig::read_server_config_file(const std::string& file_name) config_work_thread_.client_connect_timeout_ = config_work_thread["client connect timeout"]; config_work_thread_.linux_daemonize_ = config_work_thread["linux daemonize"]; config_work_thread_.io_send_time_check_ = config_work_thread["IO send data check"]; + config_work_thread_.logic_thread_bind_cpu = config_work_thread["logic thread bind cpu"]; for (auto packet_parse : json_config["packet parse library"]) { diff --git a/PSS_ASIO/Common/serverconfigtype.h b/PSS_ASIO/Common/serverconfigtype.h index ec59ff1..8a4f2dd 100644 --- a/PSS_ASIO/Common/serverconfigtype.h +++ b/PSS_ASIO/Common/serverconfigtype.h @@ -17,6 +17,7 @@ class CConfigWorkThread int s2s_timeout_seconds_ = 60; int client_connect_timeout_ = 0; int io_send_time_check_ = 0; + int logic_thread_bind_cpu = 0; //0为不绑定CPU,1为绑定 }; class CConfigPacketParseInfo diff --git a/PSS_ASIO/PSS_ASIO.cpp b/PSS_ASIO/PSS_ASIO.cpp index ccc8a70..cbac2ed 100644 --- a/PSS_ASIO/PSS_ASIO.cpp +++ b/PSS_ASIO/PSS_ASIO.cpp @@ -12,33 +12,6 @@ #define BUF_SIZE 1024 -string GetNameByPid(int pid) -{ - char proc_pid_path[BUF_SIZE] = {0}; - char buf[BUF_SIZE] = {0}; - char task_name[BUF_SIZE] = {0}; - string strTaskName = ""; - - sprintf(proc_pid_path, "/proc/%d/status", pid); - FILE* fp = fopen(proc_pid_path, "r"); - if(NULL != fp) - { - if( fgets(buf, BUF_SIZE-1, fp) == nullptr) - { - fclose(fp); - } - else - { - fclose(fp); - sscanf(buf, "%*s %s", task_name); - } - - strTaskName = task_name; - } - - return strTaskName; -} - int main(int argc, char* argv[]) { //读取配置文件参数 @@ -55,9 +28,7 @@ int main(int argc, char* argv[]) } else { - string strAppName = GetNameByPid(getpid()); - string strCfgFile = strAppName + ".json"; - App_ServerService::instance()->init_service(strCfgFile); + App_ServerService::instance()->init_service("server_config.json"); } return 0; }