From e43a2cac32cb773994b11a3d964ec4acc372d273 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 26 Jul 2023 21:33:36 -0700 Subject: Added a profiler and investageted Server. --- src/stable/client.cpp | 11 +++++++++++ src/stable/client.h | 10 +++++++++- src/stable/server.cpp | 22 ++++++++++++++++++++++ src/stable/server.h | 8 ++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) (limited to 'src/stable') diff --git a/src/stable/client.cpp b/src/stable/client.cpp index f6feb7c..56c5094 100644 --- a/src/stable/client.cpp +++ b/src/stable/client.cpp @@ -16,6 +16,13 @@ /** Read buffer size. */ #define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess +#ifdef PROFILE_BU_SERVER +#define BU_PROFILE_START( x ) Bu::Profiler::getInstance().startEvent( x ) +#define BU_PROFILE_END( x ) Bu::Profiler::getInstance().endEvent( x ) +#else +#define BU_PROFILE_START( x ) (void)0 +#define BU_PROFILE_END( x ) (void)0 +#endif Bu::Client::Client( Bu::TcpSocket *pSocket, class Bu::ClientLinkFactory *pfLink ) : @@ -47,6 +54,7 @@ void Bu::Client::processInput() char buf[RBS]; Bu::size nRead, nTotal=0; + BU_PROFILE_START("client.read"); for(;;) { try @@ -72,6 +80,7 @@ void Bu::Client::processInput() break; } } + BU_PROFILE_END("client.read"); mRead.unlock(); if( nTotal == 0 ) @@ -82,7 +91,9 @@ void Bu::Client::processInput() if( pProto && nTotal ) { + BU_PROFILE_START("client.process"); pProto->onNewData( this ); + BU_PROFILE_END("client.process"); } } diff --git a/src/stable/client.h b/src/stable/client.h index 119e2f7..abde682 100644 --- a/src/stable/client.h +++ b/src/stable/client.h @@ -16,6 +16,14 @@ #include "bu/mutex.h" #include "bu/readwritemutex.h" +#ifndef PROFILE_BU_SERVER +// #define PROFILE_BU_SERVER 1 +#endif + +#ifdef PROFILE_BU_SERVER +#include "bu/profiler.h" +#endif + namespace Bu { class Protocol; @@ -29,7 +37,7 @@ namespace Bu class Client : public Bu::Stream { public: - Client( Bu::TcpSocket *pSocket, Bu::ClientLinkFactory *pfLink ); + Client( Bu::TcpSocket *pSocket, Bu::ClientLinkFactory *pfLink); virtual ~Client(); void processInput(); diff --git a/src/stable/server.cpp b/src/stable/server.cpp index 3c42bf4..0552510 100644 --- a/src/stable/server.cpp +++ b/src/stable/server.cpp @@ -13,17 +13,27 @@ #include "bu/tcpsocket.h" #include "bu/config.h" +#ifdef PROFILE_BU_SERVER +#define BU_PROFILE_START( x ) Bu::Profiler::getInstance().startEvent( x ) +#define BU_PROFILE_END( x ) Bu::Profiler::getInstance().endEvent( x ) +#else +#define BU_PROFILE_START( x ) (void)0 +#define BU_PROFILE_END( x ) (void)0 +#endif + Bu::Server::Server() : nTimeoutSec( 0 ), nTimeoutUSec( 0 ), bAutoTick( false ) { + BU_PROFILE_START("server"); FD_ZERO( &fdActive ); } Bu::Server::~Server() { shutdown(); + BU_PROFILE_START("server"); } void Bu::Server::addPort( int nPort, int nPoolSize ) @@ -50,6 +60,7 @@ void Bu::Server::setTimeout( int nTimeoutSec, int nTimeoutUSec ) void Bu::Server::scan() { + BU_PROFILE_START("scan"); struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; fd_set fdRead = fdActive; @@ -68,6 +79,7 @@ void Bu::Server::scan() { char buf[1024]; strerror_r( errno, buf, 1024 ); + BU_PROFILE_END("scan"); throw ExceptionBase( Bu::String("Error attempting to scan open connections: %1: %2").arg( errno ).arg( buf ).end().getStr() ); @@ -85,7 +97,9 @@ void Bu::Server::scan() else { Client *pClient = hClients.get( j ); + BU_PROFILE_START("processInput"); pClient->processInput(); + BU_PROFILE_END("processInput"); if( !pClient->isOpen() ) { closeClient( j ); @@ -99,7 +113,9 @@ void Bu::Server::scan() Client *pClient = hClients.get( j ); try { + BU_PROFILE_START("processOutput"); pClient->processOutput(); + BU_PROFILE_END("processOutput"); } catch( Bu::TcpSocketException &e ) { @@ -133,10 +149,13 @@ void Bu::Server::scan() if( bAutoTick ) tick(); + + BU_PROFILE_END("scan"); } void Bu::Server::addClient( socket_t nSocket, int nPort ) { + BU_PROFILE_START("addClient"); FD_SET( nSocket, &fdActive ); Client *c = new Client( @@ -146,6 +165,7 @@ void Bu::Server::addClient( socket_t nSocket, int nPort ) hClients.insert( nSocket, c ); onNewConnection( c, nPort ); + BU_PROFILE_END("addClient"); } Bu::Server::SrvClientLink::SrvClientLink( Bu::Client *pClient ) : @@ -208,11 +228,13 @@ void Bu::Server::shutdown() void Bu::Server::closeClient( socket_t iSocket ) { + BU_PROFILE_START("closeClient"); Bu::Client *pClient = hClients.get( iSocket ); onClosedConnection( pClient ); pClient->close(); hClients.erase( iSocket ); FD_CLR( iSocket, &fdActive ); delete pClient; + BU_PROFILE_END("closeClient"); } diff --git a/src/stable/server.h b/src/stable/server.h index ee76fd2..d3f0903 100644 --- a/src/stable/server.h +++ b/src/stable/server.h @@ -23,6 +23,14 @@ #include "bu/config.h" +#ifndef PROFILE_BU_SERVER +// #define PROFILE_BU_SERVER 1 +#endif + +#ifdef PROFILE_BU_SERVER +#include "bu/profiler.h" +#endif + namespace Bu { class TcpServerSocket; -- cgit v1.2.3