1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information.
// You should have received a copy of these files along with MAdLib.
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------
#include "MAdResourceManager.h"
#include "MAdMessage.h"
#include <fstream>
#include <unistd.h>
#ifdef PARALLEL
#include <mpi.h>
#else
#ifndef _WIN_
#include <sys/time.h>
#else
#include <time.h>
#include <windows.h>
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
struct timezone
{
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
int get_time_of_day(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
// converting file time
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tmpres /= 10; // convert into microseconds
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
#endif
#endif
#if defined(__linux) || defined(linux)
void process_mem_usage(double& vm_usage, double& resident_set)
{
using std::ios_base;
using std::ifstream;
using std::string;
vm_usage = 0.0;
resident_set = 0.0;
// 'file' stat seems to give the most reliable results
//
ifstream stat_stream("/proc/self/stat",ios_base::in);
// dummy vars for leading entries in stat that we don't care about
//
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
// the two fields we want
//
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
>> utime >> stime >> cutime >> cstime >> priority >> nice
>> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
vm_usage = vsize / 1024.0 / 1024;
resident_set = rss * page_size_kb / 1024;
}
#endif
// -------------------------------------------------------------------
namespace MAd {
// -------------------------------------------------------------------
void MAdResourceManager::initialize()
{
}
// -------------------------------------------------------------------
void MAdResourceManager::finalize()
{
}
// -------------------------------------------------------------------
double MAdResourceManager::getTime() const {
#ifdef PARALLEL
return MPI_Wtime();
#else
struct timeval tp;
struct timezone tz;
#ifdef _WIN_
get_time_of_day(&tp,&tz);
#else
gettimeofday(&tp,&tz);
#endif
return ((double) tp.tv_sec +
(double) ((double) .000001 * (double) tp.tv_usec));
#endif
}
// -------------------------------------------------------------------
double MAdResourceManager::elapsedTime() const {
return ( getTime() - initialTime );
}
// -------------------------------------------------------------------
bool MAdResourceManager::getMemoryUsage(double& resident_size,
double& virtual_size) const
{
#if defined(__linux) || defined(linux)
process_mem_usage(virtual_size,resident_size);
#ifdef PARALLEL
double send[2] = {virtual_size,resident_size};
double recv[2];
MPI_Allreduce(send,recv,2,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
virtual_size = recv[0];
resident_size = recv[1];
#endif
return true;
#else
MAdMsgSgl::instance().info(__LINE__,__FILE__,
"Process memory statistics not reliable on this platform");
return false;
#endif
}
// -------------------------------------------------------------------
void MAdResourceManager::printMemoryUsage(std::string step,
std::ostream& out) const
{
double ram, virt;
getMemoryUsage(ram,virt);
out << "Memory usage at step \'"<<step<<"\': "
<< ram << " Mb (resident), "
<< virt << " Mb (virtual)\n";
}
}
// -------------------------------------------------------------------
|