Added system usage class and uptime command.

git-svn-id: http://voip.null.ro/svn/yate@807 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2006-05-24 10:04:22 +00:00
parent f6c646fd3f
commit 0f2d3fb03f
5 changed files with 188 additions and 4 deletions

View File

@ -479,6 +479,7 @@ int Engine::run()
#else
::signal(SIGPIPE,SIG_IGN);
#endif
SysUsage::init();
s_runid = Time::secNow();
s_cfg = configFile(s_cfgfile);
s_cfg.load();

View File

@ -605,6 +605,17 @@ String& String::append(const char* value, const char* separator, bool force)
return *this;
}
String& String::append(double value, unsigned int decimals)
{
if (decimals > 12)
decimals = 12;
char fmt[8];
::sprintf(fmt,"%%0.%uf",decimals);
char buf[80];
::sprintf(buf,fmt,value);
return operator+=(buf);
}
bool String::operator==(const char* value) const
{
if (!m_string)

View File

@ -40,6 +40,8 @@ void srandom(unsigned int seed)
{ s_randSeed = seed % RAND_MAX; }
}
#else
#include <sys/resource.h>
#endif
@ -55,6 +57,7 @@ static int s_indent = 0;
static bool s_debugging = true;
static bool s_abort = false;
static u_int64_t s_timestamp = 0;
static u_int64_t s_startTime = 0;
static const char* const s_colors[11] = {
"\033[5;41;1;33m\033[K",// DebugFail - blinking yellow on red
@ -433,7 +436,7 @@ u_int32_t Time::secNow()
#endif
}
u_int64_t Time::fromTimeval(struct timeval* tv)
u_int64_t Time::fromTimeval(const struct timeval* tv)
{
u_int64_t rval = 0;
if (tv) {
@ -533,6 +536,75 @@ void RefPointerBase::assign(RefObject* oldptr, RefObject* newptr, void* pointer)
oldptr->deref();
}
void SysUsage::init()
{
if (!s_startTime)
s_startTime = Time::now();
}
u_int64_t SysUsage::startTime()
{
init();
return s_startTime;
}
u_int64_t SysUsage::usecRunTime(Type type)
{
switch (type) {
case WallTime:
return Time::now() - startTime();
case UserTime:
{
#ifdef _WINDOWS
FILETIME ft;
if (GetProcessTimes(GetCurrentProcess(),NULL,NULL,NULL,&ft)) {
u_int64_t t = ft.dwLowDateTime | (((u_int64_t)ft.dwHighDateTime) << 32);
return t / 10;
}
#else
struct rusage usage;
// FIXME: this is broken, may not sum all threads
if (!::getrusage(RUSAGE_SELF,&usage))
return Time::fromTimeval(usage.ru_utime);
#endif
}
break;
case KernelTime:
{
#ifdef _WINDOWS
FILETIME ft;
if (GetProcessTimes(GetCurrentProcess(),NULL,NULL,&ft,NULL)) {
u_int64_t t = ft.dwLowDateTime | (((u_int64_t)ft.dwHighDateTime) << 32);
return t / 10;
}
#else
struct rusage usage;
// FIXME: this is broken, may not sum all threads
if (!::getrusage(RUSAGE_SELF,&usage))
return Time::fromTimeval(usage.ru_stime);
#endif
}
break;
}
return 0;
}
u_int64_t SysUsage::msecRunTime(Type type)
{
return usecRunTime(type) / 1000;
}
u_int32_t SysUsage::secRunTime(Type type)
{
return (u_int32_t)(usecRunTime(type) / 1000000);
}
double SysUsage::runTime(Type type)
{
return 0.000001 * usecRunTime(type);
}
};
/* vi: set ts=8 sw=4 sts=4 noet: */

View File

@ -29,6 +29,7 @@
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
using namespace TelEngine;
@ -38,6 +39,7 @@ static const char s_helpmsg[] =
" quit\n"
" help [command]\n"
" status [module]\n"
" uptime\n"
" machine [on|off]\n"
" output [on|off]\n"
" color [on|off]\n"
@ -316,6 +318,26 @@ bool Connection::processLine(const char *line)
writeStr(str);
return false;
}
else if (str.startSkip("uptime"))
{
str.clear();
u_int32_t t = SysUsage::secRunTime();
if (m_machine) {
str << "%%=uptime:" << t;
(str << ":").append(SysUsage::runTime(SysUsage::UserTime));
(str << ":").append(SysUsage::runTime(SysUsage::KernelTime));
}
else {
char buf[64];
::sprintf(buf,"%u:%02u:%02u (%u)",t / 3600,(t / 60) % 60,t % 60,t);
str << "Uptime: " << buf;
(str << " user: ").append(SysUsage::runTime(SysUsage::UserTime));
(str << " kernel: ").append(SysUsage::runTime(SysUsage::KernelTime));
}
str << "\n";
writeStr(str);
return false;
}
else if (str.startSkip("quit"))
{
writeStr(m_machine ? "%%=quit\n" : "Goodbye!\n");

View File

@ -1421,6 +1421,13 @@ public:
*/
String& append(const char* value, const char* separator = 0, bool force = false);
/**
* Explicit double append
* @param value Value to append
* @param decimals Number of decimals
*/
String& append(double value, unsigned int decimals = 3);
/**
* Locate the first instance of a character in the string
* @param what Character to search for
@ -2051,10 +2058,18 @@ public:
{ }
/**
* Constructs a Time object from a timeval structure
* Constructs a Time object from a timeval structure pointer
* @param tv Pointer to the timeval structure
*/
inline Time(struct timeval* tv)
inline Time(const struct timeval* tv)
: m_time(fromTimeval(tv))
{ }
/**
* Constructs a Time object from a timeval structure
* @param tv Reference of the timeval structure
*/
inline Time(const struct timeval& tv)
: m_time(fromTimeval(tv))
{ }
@ -2129,7 +2144,15 @@ public:
* @param tv Pointer to the timeval structure
* @return Corresponding time in microseconds or zero if tv is NULL
*/
static u_int64_t fromTimeval(struct timeval* tv);
static u_int64_t fromTimeval(const struct timeval* tv);
/**
* Convert time in a timeval struct to microseconds
* @param tv Reference of the timeval structure
* @return Corresponding time in microseconds
*/
inline static u_int64_t fromTimeval(const struct timeval& tv)
{ return fromTimeval(&tv); }
/**
* Get the current system time in microseconds
@ -3911,6 +3934,61 @@ protected:
SOCKET m_handle;
};
/**
* The SysUsage class allows collecting some statistics about engine's usage
* of system resources
* @short A class exposing system resources usage
*/
class YATE_API SysUsage
{
public:
enum Type {
WallTime,
UserTime,
KernelTime
};
/**
* Initialize the system start variable
*/
static void init();
/**
* Get the wall time used as start for the usage time
* @return Time of the first direct or implicit call of @ref init()
*/
static u_int64_t startTime();
/**
* Get the program's running time in microseconds
* @param type Type of running time requested
* @return Time in microseconds since the start of the program
*/
static u_int64_t usecRunTime(Type type = WallTime);
/**
* Get the program's running time in milliseconds
* @param type Type of running time requested
* @return Time in milliseconds since the start of the program
*/
static u_int64_t msecRunTime(Type type = WallTime);
/**
* Get the program's running time in seconds
* @param type Type of running time requested
* @return Time in seconds since the start of the program
*/
static u_int32_t secRunTime(Type type = WallTime);
/**
* Get the program's running time in seconds
* @param type Type of running time requested
* @return Time in seconds since the start of the program
*/
static double runTime(Type type = WallTime);
};
}; // namespace TelEngine
#endif /* __YATECLASS_H */