Skip to content

Commit

Permalink
fix windows compile error.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeeyes committed Aug 10, 2023
1 parent 2eaf6d5 commit 23084c8
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Build/Linux/server_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion Build/Linux/server_config_test_gcov.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion Build/Windows/server_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
17 changes: 13 additions & 4 deletions Common/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,32 @@ inline vector<std::string> 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
}

Expand Down
7 changes: 6 additions & 1 deletion PSS_ASIO/Common/IoContextPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ void CIoContextPool::run()
{
std::shared_ptr<std::thread> 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.
Expand Down
2 changes: 1 addition & 1 deletion PSS_ASIO/Common/IoContextPool.h
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <asio.hpp>
Expand Down
1 change: 1 addition & 0 deletions PSS_ASIO/Common/serverconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
{
Expand Down
1 change: 1 addition & 0 deletions PSS_ASIO/Common/serverconfigtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 1 addition & 30 deletions PSS_ASIO/PSS_ASIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
{
//读取配置文件参数
Expand All @@ -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;
}

0 comments on commit 23084c8

Please sign in to comment.