2019-01-04 16:29:57 +01:00
|
|
|
// Copyright 2004, Ondra Kamenik
|
|
|
|
|
|
|
|
// Resource usage journal
|
|
|
|
|
|
|
|
#ifndef JOURNAL_H
|
|
|
|
#define JOURNAL_H
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
#include "int_sequence.hh"
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <cstdio>
|
2019-02-06 18:39:46 +01:00
|
|
|
#include <cstring>
|
2019-01-04 16:29:57 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
class SystemResources
|
|
|
|
{
|
|
|
|
timeval start;
|
|
|
|
public:
|
|
|
|
SystemResources();
|
|
|
|
static long int pageSize();
|
|
|
|
static long int physicalPages();
|
|
|
|
static long int onlineProcessors();
|
|
|
|
static long int availableMemory();
|
|
|
|
void getRUS(double &load_avg, long int &pg_avail, double &utime,
|
|
|
|
double &stime, double &elapsed, long int &idrss,
|
|
|
|
long int &majflt);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SystemResourcesFlash
|
|
|
|
{
|
|
|
|
double load_avg;
|
|
|
|
long int pg_avail;
|
|
|
|
double utime;
|
|
|
|
double stime;
|
|
|
|
double elapsed;
|
|
|
|
long int idrss;
|
|
|
|
long int majflt;
|
|
|
|
SystemResourcesFlash();
|
|
|
|
void diff(const SystemResourcesFlash &pre);
|
|
|
|
};
|
|
|
|
|
2019-02-06 15:50:01 +01:00
|
|
|
class Journal : public std::ofstream
|
2019-01-04 16:29:57 +01:00
|
|
|
{
|
|
|
|
int ord;
|
|
|
|
int depth;
|
|
|
|
public:
|
|
|
|
Journal(const char *fname)
|
2019-02-06 15:50:01 +01:00
|
|
|
: std::ofstream(fname), ord(0), depth(0)
|
2019-01-04 16:29:57 +01:00
|
|
|
{
|
|
|
|
printHeader();
|
|
|
|
}
|
2019-01-09 16:26:42 +01:00
|
|
|
~Journal() override
|
2019-01-04 16:29:57 +01:00
|
|
|
{
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
void printHeader();
|
|
|
|
void
|
|
|
|
incrementOrd()
|
|
|
|
{
|
|
|
|
ord++;
|
|
|
|
}
|
|
|
|
int
|
|
|
|
getOrd() const
|
|
|
|
{
|
|
|
|
return ord;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
incrementDepth()
|
|
|
|
{
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
decrementDepth()
|
|
|
|
{
|
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
int
|
|
|
|
getDepth() const
|
|
|
|
{
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXLEN 1000
|
|
|
|
|
|
|
|
class JournalRecord;
|
|
|
|
JournalRecord&endrec(JournalRecord &);
|
|
|
|
|
|
|
|
class JournalRecord
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
char recChar;
|
|
|
|
int ord;
|
|
|
|
public:
|
|
|
|
Journal &journal;
|
|
|
|
char prefix[MAXLEN];
|
|
|
|
char mes[MAXLEN];
|
|
|
|
SystemResourcesFlash flash;
|
2019-01-09 17:21:14 +01:00
|
|
|
using _Tfunc = JournalRecord &(*)(JournalRecord &);
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
JournalRecord(Journal &jr, char rc = 'M')
|
|
|
|
: recChar(rc), ord(jr.getOrd()), journal(jr)
|
|
|
|
{
|
|
|
|
prefix[0] = '\0'; mes[0] = '\0'; writePrefix(flash);
|
|
|
|
}
|
|
|
|
virtual ~JournalRecord()
|
2019-01-09 16:16:29 +01:00
|
|
|
= default;
|
2019-01-04 16:29:57 +01:00
|
|
|
JournalRecord &operator<<(const IntSequence &s);
|
|
|
|
JournalRecord &
|
|
|
|
operator<<(_Tfunc f)
|
|
|
|
{
|
|
|
|
(*f)(*this); return *this;
|
|
|
|
}
|
|
|
|
JournalRecord &
|
|
|
|
operator<<(const char *s)
|
|
|
|
{
|
|
|
|
strcat(mes, s); return *this;
|
|
|
|
}
|
|
|
|
JournalRecord &
|
|
|
|
operator<<(int i)
|
|
|
|
{
|
|
|
|
sprintf(mes+strlen(mes), "%d", i); return *this;
|
|
|
|
}
|
|
|
|
JournalRecord &
|
|
|
|
operator<<(double d)
|
|
|
|
{
|
|
|
|
sprintf(mes+strlen(mes), "%f", d); return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
void writePrefix(const SystemResourcesFlash &f);
|
|
|
|
};
|
|
|
|
|
|
|
|
class JournalRecordPair : public JournalRecord
|
|
|
|
{
|
|
|
|
char prefix_end[MAXLEN];
|
|
|
|
public:
|
|
|
|
JournalRecordPair(Journal &jr)
|
|
|
|
: JournalRecord(jr, 'S')
|
|
|
|
{
|
|
|
|
prefix_end[0] = '\0'; journal.incrementDepth();
|
|
|
|
}
|
2019-01-09 16:26:42 +01:00
|
|
|
~JournalRecordPair() override;
|
2019-01-04 16:29:57 +01:00
|
|
|
private:
|
|
|
|
void writePrefixForEnd(const SystemResourcesFlash &f);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|