[Box Backup-commit] COMMIT r2781 - in box/invisnet/perfhack1/lib: common server
subversion at boxbackup.org
subversion at boxbackup.org
Tue Oct 5 15:58:11 BST 2010
Author: invisnet
Date: 2010-10-05 15:58:10 +0100 (Tue, 05 Oct 2010)
New Revision: 2781
Modified:
box/invisnet/perfhack1/lib/common/Logging.cpp
box/invisnet/perfhack1/lib/common/Logging.h
box/invisnet/perfhack1/lib/server/SocketStream.cpp
box/invisnet/perfhack1/lib/server/SocketStream.h
box/invisnet/perfhack1/lib/server/SocketStreamTLS.cpp
Log:
Add STATS level to logging so we can play nicer with e.g. MRTG
Modified: box/invisnet/perfhack1/lib/common/Logging.cpp
===================================================================
--- box/invisnet/perfhack1/lib/common/Logging.cpp 2010-10-05 14:49:41 UTC (rev 2780)
+++ box/invisnet/perfhack1/lib/common/Logging.cpp 2010-10-05 14:58:10 UTC (rev 2781)
@@ -69,12 +69,12 @@
{
Add(spSyslog);
}
-
+
if (sLogToSyslog && !enabled)
{
Remove(spSyslog);
}
-
+
sLogToSyslog = enabled;
}
@@ -84,12 +84,12 @@
{
Add(spConsole);
}
-
+
if (sLogToConsole && !enabled)
{
Remove(spConsole);
}
-
+
sLogToConsole = enabled;
}
@@ -113,7 +113,7 @@
return;
}
}
-
+
sLoggers.insert(sLoggers.begin(), pNewLogger);
}
@@ -130,7 +130,7 @@
}
}
-void Logging::Log(Log::Level level, const std::string& rFile,
+void Logging::Log(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage)
{
if (level > sGlobalLevel)
@@ -139,14 +139,14 @@
}
std::string newMessage;
-
+
if (sContextSet)
{
newMessage += "[" + sContext + "] ";
}
-
+
newMessage += rMessage;
-
+
for (std::vector<Logger*>::iterator i = sLoggers.begin();
i != sLoggers.end(); i++)
{
@@ -158,7 +158,7 @@
}
}
-void Logging::LogToSyslog(Log::Level level, const std::string& rFile,
+void Logging::LogToSyslog(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage)
{
if (!sLogToSyslog)
@@ -172,12 +172,12 @@
}
std::string newMessage;
-
+
if (sContextSet)
{
newMessage += "[" + sContext + "] ";
}
-
+
newMessage += rMessage;
spSyslog->Log(level, rFile, line, newMessage);
@@ -227,19 +227,19 @@
spSyslog->SetFacility(facility);
}
-Logger::Logger()
-: mCurrentLevel(Log::EVERYTHING)
+Logger::Logger()
+: mCurrentLevel(Log::EVERYTHING)
{
Logging::Add(this);
}
-Logger::Logger(Log::Level Level)
-: mCurrentLevel(Level)
+Logger::Logger(Log::Level Level)
+: mCurrentLevel(Level)
{
Logging::Add(this);
}
-Logger::~Logger()
+Logger::~Logger()
{
Logging::Remove(this);
}
@@ -275,16 +275,16 @@
sShowPID = enabled;
}
-bool Console::Log(Log::Level level, const std::string& rFile,
+bool Console::Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage)
{
if (level > GetLevel())
{
return true;
}
-
+
FILE* target = stdout;
-
+
if (level <= Log::WARNING)
{
target = stderr;
@@ -334,6 +334,10 @@
{
buf << "INFO: ";
}
+ else if (level <= Log::STATS)
+ {
+ buf << "STATS: ";
+ }
else if (level <= Log::TRACE)
{
buf << "TRACE: ";
@@ -348,20 +352,20 @@
#else
fprintf(target, "%s\n", buf.str().c_str());
#endif
-
+
return true;
}
-bool Syslog::Log(Log::Level level, const std::string& rFile,
+bool Syslog::Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage)
{
if (level > GetLevel())
{
return true;
}
-
+
int syslogLevel = LOG_ERR;
-
+
switch(level)
{
case Log::NOTHING: /* fall through */
@@ -397,7 +401,7 @@
msg += rMessage;
syslog(syslogLevel, "%s", msg.c_str());
-
+
return true;
}
@@ -443,14 +447,14 @@
return LOG_LOCAL6;
}
-bool FileLogger::Log(Log::Level Level, const std::string& rFile,
+bool FileLogger::Log(Log::Level Level, const std::string& rFile,
int line, std::string& rMessage)
{
if (Level > GetLevel())
{
return true;
}
-
+
/* avoid infinite loop if this throws an exception */
Logging::Remove(this);
Modified: box/invisnet/perfhack1/lib/common/Logging.h
===================================================================
--- box/invisnet/perfhack1/lib/common/Logging.h 2010-10-05 14:49:41 UTC (rev 2780)
+++ box/invisnet/perfhack1/lib/common/Logging.h 2010-10-05 14:58:10 UTC (rev 2781)
@@ -37,6 +37,7 @@
#define BOX_WARNING(stuff) BOX_LOG(Log::WARNING, stuff)
#define BOX_NOTICE(stuff) BOX_LOG(Log::NOTICE, stuff)
#define BOX_INFO(stuff) BOX_LOG(Log::INFO, stuff)
+#define BOX_STATS(stuff) BOX_LOG(Log::STATS, stuff)
#define BOX_TRACE(stuff) \
if (Logging::IsEnabled(Log::TRACE)) \
{ BOX_LOG(Log::TRACE, stuff) }
@@ -109,7 +110,7 @@
namespace Log
{
- enum Level
+ enum Level
{
NOTHING = 1,
FATAL,
@@ -117,7 +118,8 @@
WARNING,
NOTICE,
INFO,
- TRACE,
+ STATS,
+ TRACE,
EVERYTHING,
INVALID = -1
};
@@ -136,15 +138,15 @@
{
private:
Log::Level mCurrentLevel;
-
+
public:
Logger();
Logger(Log::Level level);
virtual ~Logger();
-
- virtual bool Log(Log::Level level, const std::string& rFile,
+
+ virtual bool Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage) = 0;
-
+
void Filter(Log::Level level)
{
mCurrentLevel = level;
@@ -152,7 +154,7 @@
virtual const char* GetType() = 0;
Log::Level GetLevel() { return mCurrentLevel; }
-
+
virtual void SetProgramName(const std::string& rProgramName) = 0;
};
@@ -175,7 +177,7 @@
static std::string sTag;
public:
- virtual bool Log(Log::Level level, const std::string& rFile,
+ virtual bool Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage);
virtual const char* GetType() { return "Console"; }
virtual void SetProgramName(const std::string& rProgramName);
@@ -204,8 +206,8 @@
public:
Syslog();
virtual ~Syslog();
-
- virtual bool Log(Log::Level level, const std::string& rFile,
+
+ virtual bool Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage);
virtual const char* GetType() { return "Syslog"; }
virtual void SetProgramName(const std::string& rProgramName);
@@ -235,7 +237,7 @@
static Log::Level sGlobalLevel;
static Logging sGlobalLogging;
static std::string sProgramName;
-
+
public:
Logging ();
~Logging();
@@ -245,9 +247,9 @@
static void FilterConsole (Log::Level level);
static void Add (Logger* pNewLogger);
static void Remove (Logger* pOldLogger);
- static void Log(Log::Level level, const std::string& rFile,
+ static void Log(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage);
- static void LogToSyslog(Log::Level level, const std::string& rFile,
+ static void LogToSyslog(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage);
static void SetContext(std::string context);
static void ClearContext();
@@ -303,16 +305,16 @@
FileStream mLogFile;
FileLogger(const FileLogger& forbidden)
: mLogFile("") { /* do not call */ }
-
+
public:
FileLogger(const std::string& rFileName, Log::Level Level)
: Logger(Level),
mLogFile(rFileName, O_WRONLY | O_CREAT | O_APPEND)
{ }
-
- virtual bool Log(Log::Level Level, const std::string& rFile,
+
+ virtual bool Log(Log::Level Level, const std::string& rFile,
int Line, std::string& rMessage);
-
+
virtual const char* GetType() { return "FileLogger"; }
virtual void SetProgramName(const std::string& rProgramName) { }
};
Modified: box/invisnet/perfhack1/lib/server/SocketStream.cpp
===================================================================
--- box/invisnet/perfhack1/lib/server/SocketStream.cpp 2010-10-05 14:49:41 UTC (rev 2780)
+++ box/invisnet/perfhack1/lib/server/SocketStream.cpp 2010-10-05 14:58:10 UTC (rev 2781)
@@ -45,7 +45,9 @@
mReadClosed(false),
mWriteClosed(false),
mBytesRead(0),
- mBytesWritten(0)
+ mBytesRead128k(0),
+ mBytesWritten(0),
+ mBytesWritten128k(0)
{
}
@@ -62,7 +64,9 @@
mReadClosed(false),
mWriteClosed(false),
mBytesRead(0),
- mBytesWritten(0)
+ mBytesRead128k(0),
+ mBytesWritten(0),
+ mBytesWritten128k(0)
{
if(socket < 0)
{
@@ -83,7 +87,9 @@
mReadClosed(rToCopy.mReadClosed),
mWriteClosed(rToCopy.mWriteClosed),
mBytesRead(rToCopy.mBytesRead),
- mBytesWritten(rToCopy.mBytesWritten)
+ mBytesRead128k(rToCopy.mBytesRead128k),
+ mBytesWritten(rToCopy.mBytesWritten),
+ mBytesWritten128k(rToCopy.mBytesWritten128k)
{
if(rToCopy.mSocketHandle < 0)
@@ -122,7 +128,7 @@
// --------------------------------------------------------------------------
void SocketStream::Attach(int socket)
{
- if(mSocketHandle != INVALID_SOCKET_VALUE)
+ if(mSocketHandle != INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, SocketAlreadyOpen)
}
@@ -145,11 +151,11 @@
// --------------------------------------------------------------------------
void SocketStream::Open(Socket::Type Type, const std::string& rName, int Port)
{
- if(mSocketHandle != INVALID_SOCKET_VALUE)
+ if(mSocketHandle != INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, SocketAlreadyOpen)
}
-
+
// Setup parameters based on type, looking up names if required
int sockDomain = 0;
SocketAllAddr addr;
@@ -164,7 +170,7 @@
BOX_LOG_SYS_ERROR("Failed to create a network socket");
THROW_EXCEPTION(ServerException, SocketOpenError)
}
-
+
// Connect it
if(::connect(mSocketHandle, &addr.sa_generic, addrLen) == -1)
{
@@ -172,7 +178,7 @@
#ifdef WIN32
DWORD err = WSAGetLastError();
::closesocket(mSocketHandle);
- BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket "
+ BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket "
"(type " << Type << ", name " << rName <<
", port " << Port << ")", err);
#else // !WIN32
@@ -202,7 +208,7 @@
// --------------------------------------------------------------------------
int SocketStream::Read(void *pBuffer, int NBytes, int Timeout)
{
- if(mSocketHandle == INVALID_SOCKET_VALUE)
+ if(mSocketHandle == INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, BadSocketHandle)
}
@@ -230,12 +236,12 @@
SocketPollError)
}
break;
-
+
case 0:
// no data
return 0;
break;
-
+
default:
// good to go!
break;
@@ -268,8 +274,14 @@
{
mReadClosed = true;
}
-
+
mBytesRead += r;
+ mBytesRead128k += r;
+ if(mBytesRead128k > 128*1024)
+ {
+ BOX_STATS("IN+" << mBytesRead128k);
+ mBytesRead128k = 0;
+ }
return r;
}
@@ -283,18 +295,18 @@
// --------------------------------------------------------------------------
void SocketStream::Write(const void *pBuffer, int NBytes)
{
- if(mSocketHandle == INVALID_SOCKET_VALUE)
+ if(mSocketHandle == INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, BadSocketHandle)
}
-
+
// Buffer in byte sized type.
ASSERT(sizeof(char) == 1);
const char *buffer = (char *)pBuffer;
-
+
// Bytes left to send
int bytesLeft = NBytes;
-
+
while(bytesLeft > 0)
{
// Try to send.
@@ -311,27 +323,33 @@
THROW_EXCEPTION(ConnectionException,
Conn_SocketWriteError);
}
-
+
// Knock off bytes sent
bytesLeft -= sent;
// Move buffer pointer
buffer += sent;
mBytesWritten += sent;
-
+ mBytesWritten128k += sent;
+ if(mBytesWritten128k > 128*1024)
+ {
+ BOX_STATS("OUT+" << mBytesWritten128k);
+ mBytesWritten128k = 0;
+ }
+
// Need to wait until it can send again?
if(bytesLeft > 0)
{
- BOX_TRACE("Waiting to send data on socket " <<
+ BOX_TRACE("Waiting to send data on socket " <<
mSocketHandle << " (" << bytesLeft <<
" of " << NBytes << " bytes left)");
-
+
// Wait for data to send.
struct pollfd p;
p.fd = mSocketHandle;
p.events = POLLOUT;
p.revents = 0;
-
+
if(::poll(&p, 1, 16000 /* 16 seconds */) == -1)
{
// Don't exception if it's just a signal
@@ -357,7 +375,7 @@
// --------------------------------------------------------------------------
void SocketStream::Close()
{
- if(mSocketHandle == INVALID_SOCKET_VALUE)
+ if(mSocketHandle == INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, BadSocketHandle)
}
@@ -372,6 +390,8 @@
// already closed or closing.
}
mSocketHandle = INVALID_SOCKET_VALUE;
+
+ BOX_STATS("IN+" << mBytesRead128k << " OUT+" << mBytesWritten128k);
}
// --------------------------------------------------------------------------
@@ -384,18 +404,18 @@
// --------------------------------------------------------------------------
void SocketStream::Shutdown(bool Read, bool Write)
{
- if(mSocketHandle == INVALID_SOCKET_VALUE)
+ if(mSocketHandle == INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, BadSocketHandle)
}
-
+
// Do anything?
if(!Read && !Write) return;
-
+
int how = SHUT_RDWR;
if(Read && !Write) how = SHUT_RD;
if(!Read && Write) how = SHUT_WR;
-
+
// Shut it down!
if(::shutdown(mSocketHandle, how) == -1)
{
@@ -443,7 +463,7 @@
// --------------------------------------------------------------------------
tOSSocketHandle SocketStream::GetSocketHandle()
{
- if(mSocketHandle == INVALID_SOCKET_VALUE)
+ if(mSocketHandle == INVALID_SOCKET_VALUE)
{
THROW_EXCEPTION(ServerException, BadSocketHandle)
}
Modified: box/invisnet/perfhack1/lib/server/SocketStream.h
===================================================================
--- box/invisnet/perfhack1/lib/server/SocketStream.h 2010-10-05 14:49:41 UTC (rev 2780)
+++ box/invisnet/perfhack1/lib/server/SocketStream.h 2010-10-05 14:58:10 UTC (rev 2781)
@@ -36,7 +36,7 @@
SocketStream(int socket);
SocketStream(const SocketStream &rToCopy);
~SocketStream();
-
+
void Open(Socket::Type Type, const std::string& rName, int Port = 0);
void Attach(int socket);
@@ -62,12 +62,14 @@
protected:
off_t mBytesRead;
+ off_t mBytesRead128k;
off_t mBytesWritten;
+ off_t mBytesWritten128k;
public:
off_t GetBytesRead() const {return mBytesRead;}
off_t GetBytesWritten() const {return mBytesWritten;}
- void ResetCounters() {mBytesRead = mBytesWritten = 0;}
+ void ResetCounters() {mBytesRead = mBytesRead128k = mBytesWritten = mBytesWritten128k = 0;}
bool IsOpened() { return mSocketHandle != INVALID_SOCKET_VALUE; }
};
Modified: box/invisnet/perfhack1/lib/server/SocketStreamTLS.cpp
===================================================================
--- box/invisnet/perfhack1/lib/server/SocketStreamTLS.cpp 2010-10-05 14:49:41 UTC (rev 2780)
+++ box/invisnet/perfhack1/lib/server/SocketStreamTLS.cpp 2010-10-05 14:58:10 UTC (rev 2781)
@@ -72,7 +72,7 @@
{
// Attempt to close to avoid problems
Close();
-
+
// And if that didn't work...
if(mpSSL)
{
@@ -81,7 +81,7 @@
mpBIO = 0; // implicity freed by the SSL_free call
}
}
-
+
// If we only got to creating that BIO.
if(mpBIO)
{
@@ -130,7 +130,7 @@
tOSSocketHandle socket = GetSocketHandle();
BIO_set_fd(mpBIO, socket, BIO_NOCLOSE);
-
+
// Then the SSL object
mpSSL = ::SSL_new(rContext.GetRawContext());
if(mpSSL == 0)
@@ -153,7 +153,7 @@
THROW_EXCEPTION(ServerException, SocketSetNonBlockingFailed)
}
#endif
-
+
// FIXME: This is less portable than the above. However, it MAY be needed
// for cygwin, which has/had bugs with fcntl
//
@@ -198,7 +198,7 @@
THROW_EXCEPTION(ConnectionException, Conn_TLSHandshakeTimedOut)
}
break;
-
+
default: // (and SSL_ERROR_ZERO_RETURN)
// Error occured
if(IsServer)
@@ -213,7 +213,7 @@
}
}
}
-
+
// And that's it
}
@@ -235,7 +235,7 @@
case SSL_ERROR_WANT_READ:
p.events = POLLIN;
break;
-
+
case SSL_ERROR_WANT_WRITE:
p.events = POLLOUT;
break;
@@ -303,7 +303,7 @@
{
return 0;
}
-
+
while(true)
{
int r = ::SSL_read(mpSSL, pBuffer, NBytes);
@@ -314,6 +314,12 @@
case SSL_ERROR_NONE:
// No error, return number of bytes read
mBytesRead += r;
+ mBytesRead128k += r;
+ if(mBytesRead128k > 128*1024)
+ {
+ BOX_STATS("IN+" << mBytesRead128k);
+ mBytesRead128k = 0;
+ }
return r;
break;
@@ -333,7 +339,7 @@
return 0;
}
break;
-
+
default:
SSLLib::LogError("reading");
THROW_EXCEPTION(ConnectionException, Conn_TLSReadFailed)
@@ -353,31 +359,37 @@
void SocketStreamTLS::Write(const void *pBuffer, int NBytes)
{
if(!mpSSL) {THROW_EXCEPTION(ServerException, TLSNoSSLObject)}
-
+
// Make sure zero byte writes work as expected
if(NBytes == 0)
{
return;
}
-
+
// from man SSL_write
//
// SSL_write() will only return with success, when the
// complete contents of buf of length num has been written.
//
// So no worries about partial writes and moving the buffer around
-
+
while(true)
{
// try the write
int r = ::SSL_write(mpSSL, pBuffer, NBytes);
-
+
int se;
switch((se = ::SSL_get_error(mpSSL, r)))
{
case SSL_ERROR_NONE:
// No error, data sent, return success
mBytesWritten += r;
+ mBytesWritten128k += r;
+ if(mBytesWritten128k > 128*1024)
+ {
+ BOX_STATS("OUT+" << mBytesWritten128k);
+ mBytesWritten128k = 0;
+ }
return;
break;
@@ -392,13 +404,13 @@
// wait for the requried data
{
#ifndef BOX_RELEASE_BUILD
- bool conditionmet =
+ bool conditionmet =
#endif
WaitWhenRetryRequired(se, IOStream::TimeOutInfinite);
ASSERT(conditionmet);
}
break;
-
+
default:
SSLLib::LogError("writing");
THROW_EXCEPTION(ConnectionException, Conn_TLSWriteFailed)
@@ -469,14 +481,14 @@
THROW_EXCEPTION(ConnectionException, Conn_TLSNoPeerCertificate)
}
- // Subject details
- X509_NAME *subject = ::X509_get_subject_name(cert);
+ // Subject details
+ X509_NAME *subject = ::X509_get_subject_name(cert);
if(subject == 0)
{
::X509_free(cert);
THROW_EXCEPTION(ConnectionException, Conn_TLSPeerCertificateInvalid)
}
-
+
// Common name
char commonName[256];
if(::X509_NAME_get_text_by_NID(subject, NID_commonName, commonName, sizeof(commonName)) <= 0)
@@ -486,7 +498,7 @@
}
// Terminate just in case
commonName[sizeof(commonName)-1] = '\0';
-
+
// Done.
return std::string(commonName);
}
More information about the Boxbackup-commit
mailing list