1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

Clients report their PIDs so the memory usage can be monitored.

This commit is contained in:
Michał W. Urbańczyk
2011-10-23 00:50:00 +00:00
parent 92e508fe1f
commit 8de4a4aef2
5 changed files with 27 additions and 8 deletions

View File

@ -19,6 +19,7 @@
#include "../lib/VCMI_Lib.h" #include "../lib/VCMI_Lib.h"
#include "../lib/BattleState.h" #include "../lib/BattleState.h"
#include "../lib/NetPacks.h" #include "../lib/NetPacks.h"
#include "../lib/CThreadHelper.h"
using namespace std; using namespace std;
using namespace boost; using namespace boost;
@ -29,13 +30,9 @@ std::string NAME = NAME_VER + std::string(" DLL runner");
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int pid = -1; int pid = getMyPid();
#ifdef _WIN32
pid = GetCurrentProcessId();
#else
pid = getpid();
#endif
initDLL(console,logfile); initDLL(console,logfile);
logfile = new std::ofstream(("VCMI_Runner_log_" + boost::lexical_cast<std::string>(pid) + ".txt").c_str()); logfile = new std::ofstream(("VCMI_Runner_log_" + boost::lexical_cast<std::string>(pid) + ".txt").c_str());
@ -67,6 +64,7 @@ int main(int argc, char** argv)
ui8 color; ui8 color;
StartInfo si; StartInfo si;
string battleAIName; string battleAIName;
*serv << getMyPid();
*serv >> si >> battleAIName >> color; *serv >> si >> battleAIName >> color;
assert(si.mode == StartInfo::DUEL); assert(si.mode == StartInfo::DUEL);
tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % (int)color; tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % (int)color;

View File

@ -448,6 +448,7 @@ void CClient::newDuel(CConnection *con, StartInfo *si)
ui8 color; ui8 color;
std::string battleAIName; std::string battleAIName;
*serv << getMyPid();
*serv >> *si >> battleAIName >> color; *serv >> *si >> battleAIName >> color;
assert(si->mode == StartInfo::DUEL); assert(si->mode == StartInfo::DUEL);
assert(color > 1); //we are NOT participants assert(color > 1); //we are NOT participants

View File

@ -79,4 +79,14 @@ void setThreadName(long threadID, const std::string &name)
#else #else
//*nix counterpart? //*nix counterpart?
#endif #endif
}
si32 getMyPid()
{
#ifdef _WIN32
return GetCurrentProcessId();
#else
return getpid();
#endif
} }

View File

@ -32,6 +32,9 @@ public:
void run(); void run();
}; };
si32 DLL_EXPORT getMyPid();
template <typename T> inline void setData(T * data, boost::function<T()> func) template <typename T> inline void setData(T * data, boost::function<T()> func)
{ {
*data = func(); *data = func();

View File

@ -500,7 +500,8 @@ void CVCMIServer::loadGame()
void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI, int howManyClients) void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI, int howManyClients)
{ {
std::map<CConnection *, si32> pidsFromConns;
si32 PIDs[3] = {0}; //[0] left [1] right; [2] reference
//we need three connections //we need three connections
std::vector<boost::thread*> threads(howManyClients, NULL); std::vector<boost::thread*> threads(howManyClients, NULL);
std::vector<CConnection*> conns(howManyClients, NULL); std::vector<CConnection*> conns(howManyClients, NULL);
@ -548,12 +549,14 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI
gh->conns.insert(c); gh->conns.insert(c);
gh->states.addPlayer(player); gh->states.addPlayer(player);
*c << si; *c << si;
*c >> pidsFromConns[c];
std::set<int> pom; std::set<int> pom;
pom.insert(player); pom.insert(player);
threads[player] = new boost::thread(boost::bind(&CGameHandler::handleConnection, gh, pom, boost::ref(*c))); threads[player] = new boost::thread(boost::bind(&CGameHandler::handleConnection, gh, pom, boost::ref(*c)));
} }
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
tlog0 << boost::format("Sending start info to connections!\n"); tlog0 << boost::format("Sending start info to connections!\n");
int aisSoFar = 0; int aisSoFar = 0;
for (int i = 0; i < howManyClients ; i++) for (int i = 0; i < howManyClients ; i++)
@ -572,22 +575,26 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI
{ {
tlog0 << " will run " << (aisSoFar ? "right" : "left") << " AI " << std::endl; tlog0 << " will run " << (aisSoFar ? "right" : "left") << " AI " << std::endl;
*c << gh->ais[aisSoFar] << ui8(aisSoFar); *c << gh->ais[aisSoFar] << ui8(aisSoFar);
PIDs[aisSoFar] = pidsFromConns[c];
aisSoFar++; aisSoFar++;
} }
else else
{ {
tlog0 << " will serve as a memory reference.\n"; tlog0 << " will serve as a memory reference.\n";
*c << std::string() << ui8(254); *c << std::string() << ui8(254);
PIDs[2] = pidsFromConns[c];
} }
} }
} }
//TODO monitor memory of PIDs
std::string logFName = "duel_log.vdat"; std::string logFName = "duel_log.vdat";
tlog0 << "Logging battle activities (for replay possibility) in " << logFName << std::endl; tlog0 << "Logging battle activities (for replay possibility) in " << logFName << std::endl;
gh->gameLog = new CSaveFile(logFName); gh->gameLog = new CSaveFile(logFName);
gh->gameLog->smartPointerSerialization = false; gh->gameLog->smartPointerSerialization = false;
*gh->gameLog << battle << leftAI << rightAI << ui8('$'); *gh->gameLog << battle << leftAI << rightAI << ui8('$');
tlog0 << "Starting battle!\n"; tlog0 << "Starting battle!\n";
gh->runBattle(); gh->runBattle();
tlog0 << "Battle over!\n"; tlog0 << "Battle over!\n";