Added some performance logging capabilities and socket cleanup for new broadcast thread sockets

This commit is contained in:
Mark Vejvoda 2010-04-15 01:19:00 +00:00
parent 86f0b8adb0
commit 30c4eb2b96
12 changed files with 108 additions and 38 deletions

View File

@ -234,13 +234,17 @@ void Commander::updateNetwork(){
GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface();
perfTimer.start();
//update the keyframe //update the keyframe
gameNetworkInterface->updateKeyframe(world->getFrameCount()); gameNetworkInterface->updateKeyframe(world->getFrameCount());
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] gameNetworkInterface->updateKeyframe for %d took %d msecs\n",__FILE__,__FUNCTION__,__LINE__,world->getFrameCount(),perfTimer.getMillis());
perfTimer.start();
//give pending commands //give pending commands
for(int i= 0; i < gameNetworkInterface->getPendingCommandCount(); ++i){ for(int i= 0; i < gameNetworkInterface->getPendingCommandCount(); ++i){
giveNetworkCommand(gameNetworkInterface->getPendingCommand(i)); giveNetworkCommand(gameNetworkInterface->getPendingCommand(i));
} }
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] giveNetworkCommand took %d msecs\n",__FILE__,__FUNCTION__,__LINE__,perfTimer.getMillis());
gameNetworkInterface->clearPendingCommands(); gameNetworkInterface->clearPendingCommands();
} }
} }
@ -254,7 +258,6 @@ void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand)
case ncstRotateUnit: { case ncstRotateUnit: {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found ncstRotateUnit [%d]\n",__FILE__,__FUNCTION__,__LINE__,networkCommand->getTargetId()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found ncstRotateUnit [%d]\n",__FILE__,__FUNCTION__,__LINE__,networkCommand->getTargetId());
//!!!
int unitTypeId = networkCommand->getUnitId(); int unitTypeId = networkCommand->getUnitId();
int factionIndex = networkCommand->getUnitTypeId(); int factionIndex = networkCommand->getUnitTypeId();
int rotateAmount = networkCommand->getTargetId(); int rotateAmount = networkCommand->getTargetId();

View File

@ -17,12 +17,14 @@
#include "vec.h" #include "vec.h"
#include "selection.h" #include "selection.h"
#include "command_type.h" #include "command_type.h"
#include "platform_util.h"
using std::vector; using std::vector;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
using Shared::Graphics::Vec2i; using Shared::Graphics::Vec2i;
using Shared::Platform::Chrono;
class World; class World;
class Unit; class Unit;
@ -42,6 +44,7 @@ private:
private: private:
World *world; World *world;
Chrono perfTimer;
public: public:
void init(World *world); void init(World *world);

View File

@ -848,9 +848,9 @@ void Game::render2d(){
renderer.renderChatManager(&chatManager); renderer.renderChatManager(&chatManager);
//debug info //debug info
if(config.getBool("DebugMode")){
string str;
string str;
if(config.getBool("DebugMode") || difftime(time(NULL),lastRenderLog2d) >= 1){
str+= "MouseXY: " + intToStr(mouseX) + "," + intToStr(mouseY)+"\n"; str+= "MouseXY: " + intToStr(mouseX) + "," + intToStr(mouseY)+"\n";
str+= "PosObjWord: " + intToStr(gui.getPosObjWorld().x) + "," + intToStr(gui.getPosObjWorld().y)+"\n"; str+= "PosObjWord: " + intToStr(gui.getPosObjWorld().x) + "," + intToStr(gui.getPosObjWorld().y)+"\n";
str+= "Render FPS: "+intToStr(lastRenderFps)+"\n"; str+= "Render FPS: "+intToStr(lastRenderFps)+"\n";
@ -880,7 +880,9 @@ void Game::render2d(){
} }
str+="\n"; str+="\n";
} }
}
if(config.getBool("DebugMode")){
renderer.renderText( renderer.renderText(
str, coreData.getMenuFontNormal(), str, coreData.getMenuFontNormal(),
Vec3f(1.0f), 10, 500, false); Vec3f(1.0f), 10, 500, false);
@ -902,6 +904,11 @@ void Game::render2d(){
//2d mouse //2d mouse
renderer.renderMouse2d(mouseX, mouseY, mouse2d, gui.isSelectingPos()? 1.f: 0.f); renderer.renderMouse2d(mouseX, mouseY, mouse2d, gui.isSelectingPos()? 1.f: 0.f);
if(difftime(time(NULL),lastRenderLog2d) >= 1) {
lastRenderLog2d = time(NULL);
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str());
}
} }

View File

@ -77,6 +77,7 @@ private:
ParticleSystem *weatherParticleSystem; ParticleSystem *weatherParticleSystem;
GameSettings gameSettings; GameSettings gameSettings;
Vec2i lastMousePos; Vec2i lastMousePos;
time_t lastRenderLog2d;
public: public:
Game(Program *program, const GameSettings *gameSettings); Game(Program *program, const GameSettings *gameSettings);

View File

@ -262,6 +262,7 @@ SystemFlags debugger;
int glestMain(int argc, char** argv){ int glestMain(int argc, char** argv){
SystemFlags::enableNetworkDebugInfo = true; SystemFlags::enableNetworkDebugInfo = true;
SystemFlags::enableDebugText = true; SystemFlags::enableDebugText = true;
SystemFlags::enablePerformanceDebugInfo = false;
MainWindow *mainWindow= NULL; MainWindow *mainWindow= NULL;
Program *program= NULL; Program *program= NULL;
@ -273,13 +274,14 @@ int glestMain(int argc, char** argv){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::enableNetworkDebugInfo = config.getBool("DebugNetwork","0"); SystemFlags::enableNetworkDebugInfo = config.getBool("DebugNetwork","false");
SystemFlags::enableDebugText = config.getBool("DebugMode","0"); SystemFlags::enableDebugText = config.getBool("DebugMode","false");
debugLogFile = config.getString("DebugLogFile",""); debugLogFile = config.getString("DebugLogFile","");
if(getGameReadWritePath() != "") { if(getGameReadWritePath() != "") {
debugLogFile = getGameReadWritePath() + debugLogFile; debugLogFile = getGameReadWritePath() + debugLogFile;
} }
SystemFlags::debugLogFile = debugLogFile.c_str(); SystemFlags::debugLogFile = debugLogFile.c_str();
SystemFlags::enablePerformanceDebugInfo = config.getBool("DebugPerformance","false");
NetworkInterface::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); NetworkInterface::setDisplayMessageFunction(ExceptionHandler::DisplayMessage);

View File

@ -36,7 +36,7 @@ namespace Glest{ namespace Game{
// ===================================================== // =====================================================
const int ClientInterface::messageWaitTimeout= 10000; //10 seconds const int ClientInterface::messageWaitTimeout= 10000; //10 seconds
const int ClientInterface::waitSleepTime= 50; const int ClientInterface::waitSleepTime= 10;
ClientInterface::ClientInterface(){ ClientInterface::ClientInterface(){
clientSocket= NULL; clientSocket= NULL;
@ -353,13 +353,21 @@ void ClientInterface::updateKeyframe(int frameCount)
{ {
case nmtCommandList: case nmtCommandList:
{ {
Chrono chrono;
chrono.start();
int waitCount = 0;
//make sure we read the message //make sure we read the message
NetworkMessageCommandList networkMessageCommandList; NetworkMessageCommandList networkMessageCommandList;
while(!receiveMessage(&networkMessageCommandList)) while(receiveMessage(&networkMessageCommandList) == false)
{ {
sleep(waitSleepTime); sleep(waitSleepTime);
waitCount++;
} }
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] receiveMessage took %d msecs, waitCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),waitCount);
chrono.start();
//check that we are in the right frame //check that we are in the right frame
if(networkMessageCommandList.getFrameCount() != frameCount) if(networkMessageCommandList.getFrameCount() != frameCount)
{ {
@ -378,6 +386,8 @@ void ClientInterface::updateKeyframe(int frameCount)
pendingCommands.push_back(*networkMessageCommandList.getCommand(i)); pendingCommands.push_back(*networkMessageCommandList.getCommand(i));
} }
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] transfer network commands took %d msecs\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
done= true; done= true;
} }
break; break;
@ -574,6 +584,7 @@ void ClientInterface::waitForMessage()
Chrono chrono; Chrono chrono;
chrono.start(); chrono.start();
int waitLoopCount = 0;
while(getNextMessageType(true) == nmtInvalid) while(getNextMessageType(true) == nmtInvalid)
{ {
if(!isConnected()) if(!isConnected())
@ -597,7 +608,10 @@ void ClientInterface::waitForMessage()
} }
sleep(waitSleepTime); sleep(waitSleepTime);
waitLoopCount++;
} }
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] waiting took %d msecs, waitLoopCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),waitLoopCount);
} }
void ClientInterface::quitGame(bool userManuallyQuit) void ClientInterface::quitGame(bool userManuallyQuit)

View File

@ -63,6 +63,9 @@ NetworkMessageType NetworkInterface::getNextMessageType(bool checkHasDataFirst)
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] socket->getDataToRead() iPeek = %d, messageType = %d\n",__FILE__,__FUNCTION__,iPeek,messageType); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] socket->getDataToRead() iPeek = %d, messageType = %d\n",__FILE__,__FUNCTION__,iPeek,messageType);
} }
else {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] PEEK WARNING, socket->getDataToRead() messageType = %d, dataSize = %d\n",__FILE__,__FUNCTION__,messageType,dataSize);
}
//sanity check new message type //sanity check new message type
if(messageType<0 || messageType>=nmtCount){ if(messageType<0 || messageType>=nmtCount){

View File

@ -227,6 +227,8 @@ void ServerInterface::update()
} }
void ServerInterface::updateKeyframe(int frameCount){ void ServerInterface::updateKeyframe(int frameCount){
Chrono chrono;
chrono.start();
NetworkMessageCommandList networkMessageCommandList(frameCount); NetworkMessageCommandList networkMessageCommandList(frameCount);
@ -241,8 +243,12 @@ void ServerInterface::updateKeyframe(int frameCount){
} }
} }
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] build command list took %d msecs, networkMessageCommandList.getCommandCount() = %d, frameCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),networkMessageCommandList.getCommandCount(),frameCount);
//broadcast commands //broadcast commands
broadcastMessage(&networkMessageCommandList); broadcastMessage(&networkMessageCommandList);
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] broadcastMessage took %d msecs, networkMessageCommandList.getCommandCount() = %d, frameCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),networkMessageCommandList.getCommandCount(),frameCount);
} }
void ServerInterface::waitUntilReady(Checksum* checksum){ void ServerInterface::waitUntilReady(Checksum* checksum){

View File

@ -120,7 +120,8 @@ public:
static string getHostName(); static string getHostName();
static string getIp(); static string getIp();
bool isSocketValid(PLATFORM_SOCKET *validateSocket=NULL) const; bool isSocketValid() const;
static bool isSocketValid(const PLATFORM_SOCKET *validateSocket);
protected: protected:
static void throwException(string str); static void throwException(string str);

View File

@ -34,11 +34,13 @@ public:
enum DebugType { enum DebugType {
debugSystem, debugSystem,
debugNetwork debugNetwork,
debugPerformance
}; };
static bool enableDebugText; static bool enableDebugText;
static bool enableNetworkDebugInfo; static bool enableNetworkDebugInfo;
static bool enablePerformanceDebugInfo;
static const char *debugLogFile; static const char *debugLogFile;
static void OutputDebug(DebugType type, const char *fmt, ...); static void OutputDebug(DebugType type, const char *fmt, ...);

View File

@ -650,17 +650,21 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
return ipList; return ipList;
} }
bool Socket::isSocketValid(PLATFORM_SOCKET *validateSocket) const { bool Socket::isSocketValid() const {
return Socket::isSocketValid(&sock);
}
bool Socket::isSocketValid(const PLATFORM_SOCKET *validateSocket) {
#ifdef WIN32 #ifdef WIN32
if(validateSocket == NULL) { if(validateSocket == NULL) {
return (sock != INVALID_SOCKET); return false;
} }
else { else {
return (*validateSocket != INVALID_SOCKET); return (*validateSocket != INVALID_SOCKET);
} }
#else #else
if(validateSocket == NULL) { if(validateSocket == NULL) {
return (sock > 0); return false;
} }
else { else {
return (*validateSocket > 0); return (*validateSocket > 0);
@ -726,7 +730,7 @@ bool Socket::hasDataToRead(std::map<PLATFORM_SOCKET,bool> &socketTriggeredList)
itermap != socketTriggeredList.end(); itermap++) itermap != socketTriggeredList.end(); itermap++)
{ {
PLATFORM_SOCKET socket = itermap->first; PLATFORM_SOCKET socket = itermap->first;
if(socket > 0) if(Socket::isSocketValid(&socket) == true)
{ {
FD_SET(socket, &rfds); FD_SET(socket, &rfds);
imaxsocket = max(socket,imaxsocket); imaxsocket = max(socket,imaxsocket);
@ -784,7 +788,7 @@ bool Socket::hasDataToRead(PLATFORM_SOCKET socket)
{ {
bool bResult = false; bool bResult = false;
if(socket > 0) if(Socket::isSocketValid(&socket) == true)
{ {
fd_set rfds; fd_set rfds;
struct timeval tv; struct timeval tv;
@ -1452,6 +1456,15 @@ void BroadCastClientSocketThread::execute() {
setRunningStatus(false); setRunningStatus(false);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast Client thread is exiting\n"); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast Client thread is exiting\n");
} }
#ifndef WIN32
::close(bcfd);
bcfd = INVALID_SOCKET;
#else
::closesocket(bcfd);
bcfd = -1;
#endif
} }
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -1725,7 +1738,7 @@ void BroadCastSocketThread::execute() {
for( pn = 1; ; pn++ ) for( pn = 1; ; pn++ )
{ {
for(unsigned int idx = 0; idx < ipList.size() && idx < MAX_NIC_COUNT; idx++) { for(unsigned int idx = 0; idx < ipList.size() && idx < MAX_NIC_COUNT; idx++) {
if( bcfd[idx] > 0 ) { if( Socket::isSocketValid(&bcfd[idx]) == true ) {
try { try {
// Send this machine's host name and address in hostname:n.n.n.n format // Send this machine's host name and address in hostname:n.n.n.n format
sprintf(buff,"%s",myhostname); sprintf(buff,"%s",myhostname);
@ -1779,6 +1792,18 @@ void BroadCastSocketThread::execute() {
} }
} }
for(unsigned int idx = 0; idx < ipList.size() && idx < MAX_NIC_COUNT; idx++) {
if( Socket::isSocketValid(&bcfd[idx]) == true ) {
#ifndef WIN32
::close(bcfd[idx]);
bcfd[idx] = INVALID_SOCKET;
#else
::closesocket(bcfd[idx]);
bcfd[idx] = -1;
#endif
}
}
setRunningStatus(false); setRunningStatus(false);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast thread is exiting\n"); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcast thread is exiting\n");

View File

@ -37,6 +37,8 @@ namespace Shared{ namespace Util{
bool SystemFlags::enableDebugText = false; bool SystemFlags::enableDebugText = false;
bool SystemFlags::enableNetworkDebugInfo = false; bool SystemFlags::enableNetworkDebugInfo = false;
bool SystemFlags::enablePerformanceDebugInfo = false;
const char * SystemFlags::debugLogFile = NULL; const char * SystemFlags::debugLogFile = NULL;
ofstream SystemFlags::fileStream; ofstream SystemFlags::fileStream;
int SystemFlags::lockFile = -1; int SystemFlags::lockFile = -1;
@ -83,7 +85,8 @@ void SystemFlags::Close() {
void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) {
if((type == debugSystem && SystemFlags::enableDebugText == false) || if((type == debugSystem && SystemFlags::enableDebugText == false) ||
(type == debugNetwork && SystemFlags::enableNetworkDebugInfo == false)) { (type == debugNetwork && SystemFlags::enableNetworkDebugInfo == false ||
type == debugPerformance && SystemFlags::enablePerformanceDebugInfo == false)) {
return; return;
} }