| 1 |
/* -*- C++ -*- */ |
| 2 |
|
| 3 |
#ifndef HL_LOGHEAP_H_ |
| 4 |
#define HL_LOGHEAP_H_ |
| 5 |
|
| 6 |
/** |
| 7 |
* @file logheap.h |
| 8 |
* @brief Contains the implementations of Log and LogHeap. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <assert.h> |
| 12 |
#include <math.h> |
| 13 |
#include <limits.h> |
| 14 |
#include <stdlib.h> |
| 15 |
|
| 16 |
// UNIX-specific for now... |
| 17 |
#if defined(unix) |
| 18 |
#include <unistd.h> |
| 19 |
#include <sys/types.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <fstream> |
| 23 |
#include <ios> |
| 24 |
|
| 25 |
|
| 26 |
using namespace std; |
| 27 |
|
| 28 |
namespace HL { |
| 29 |
|
| 30 |
template <class Obj, |
| 31 |
int MAX_ENTRIES = 300000> |
| 32 |
class Log { |
| 33 |
public: |
| 34 |
|
| 35 |
Log() : |
| 36 |
numEntries (0) |
| 37 |
{ |
| 38 |
#if defined(unix) |
| 39 |
sprintf (filename, "theLog-%d", getpid()); |
| 40 |
#else |
| 41 |
sprintf (filename, "theLog"); |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
void writeLog() { |
| 46 |
{ |
| 47 |
ofstream outfile (filename, ios_base::app); |
| 48 |
} |
| 49 |
write(filename); |
| 50 |
} |
| 51 |
|
| 52 |
void dump() { |
| 53 |
write (filename); |
| 54 |
numEntries = 0; |
| 55 |
} |
| 56 |
|
| 57 |
int append (const Obj& o) { |
| 58 |
if (numEntries < MAX_ENTRIES) { |
| 59 |
entries[numEntries] = o; |
| 60 |
numEntries++; |
| 61 |
return 1; |
| 62 |
} else { |
| 63 |
return 0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void write (char * fname) { |
| 68 |
ofstream outfile (fname, ios_base::app); |
| 69 |
for (int i = 0; i < numEntries; i++) { |
| 70 |
outfile << entries[i] << endl; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private: |
| 75 |
|
| 76 |
int numEntries; |
| 77 |
Obj entries[MAX_ENTRIES]; |
| 78 |
char filename[255]; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
template <class SuperHeap> |
| 83 |
class LogHeap : public SuperHeap { |
| 84 |
public: |
| 85 |
|
| 86 |
LogHeap() |
| 87 |
: allDone (false) |
| 88 |
{} |
| 89 |
|
| 90 |
inline void * malloc (size_t sz) { |
| 91 |
void * ptr = SuperHeap::malloc (sz); |
| 92 |
if (!allDone) { |
| 93 |
MemoryRequest m; |
| 94 |
m.malloc (ptr, sz); |
| 95 |
if (!log.append (m)) { |
| 96 |
allDone = true; |
| 97 |
log.dump(); |
| 98 |
log.append(m); |
| 99 |
allDone = false; |
| 100 |
} |
| 101 |
} |
| 102 |
return ptr; |
| 103 |
} |
| 104 |
|
| 105 |
void write() { |
| 106 |
allDone = true; |
| 107 |
log.writeLog(); |
| 108 |
} |
| 109 |
|
| 110 |
inline void free (void * ptr) { |
| 111 |
if (!allDone) { |
| 112 |
MemoryRequest m; |
| 113 |
m.free (ptr); |
| 114 |
log.append (m); |
| 115 |
} |
| 116 |
SuperHeap::free (ptr); |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
|
| 121 |
class MemoryRequest { |
| 122 |
public: |
| 123 |
|
| 124 |
MemoryRequest() |
| 125 |
: |
| 126 |
#if 0 |
| 127 |
_sec (LONG_MAX), |
| 128 |
_usec (LONG_MAX), |
| 129 |
#endif |
| 130 |
_size (0), |
| 131 |
_address (INVALID) |
| 132 |
{} |
| 133 |
|
| 134 |
enum { FREE_OP = 0, |
| 135 |
MALLOC_OP, |
| 136 |
REALLOC_OP, |
| 137 |
REFREE_OP, |
| 138 |
ALLOCATE_OP, |
| 139 |
DEALLOCATE_OP, |
| 140 |
INVALID |
| 141 |
}; |
| 142 |
|
| 143 |
friend std::ostream& operator<< (std::ostream& os, MemoryRequest& m) { |
| 144 |
switch (m.getType()) { |
| 145 |
case FREE_OP: |
| 146 |
os << "F\t" << (void *) m.getAddress(); |
| 147 |
break; |
| 148 |
case MALLOC_OP: |
| 149 |
os << "M\t" << m.getSize() << "\t" << (void *) m.getAddress(); |
| 150 |
break; |
| 151 |
default: |
| 152 |
abort(); |
| 153 |
} |
| 154 |
|
| 155 |
return os; |
| 156 |
} |
| 157 |
|
| 158 |
void malloc (void * addr, |
| 159 |
size_t sz) |
| 160 |
{ |
| 161 |
assert ((((unsigned long) addr) & 7) == 0); |
| 162 |
_size = sz; |
| 163 |
_address = (unsigned long) addr | MALLOC_OP; |
| 164 |
// markTime (_sec, _usec); |
| 165 |
// printf ("malloc %d (%f)\n", sz, getTime()); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
void free (void * addr) |
| 170 |
{ |
| 171 |
assert ((((unsigned long) addr) & 7) == 0); |
| 172 |
_address = (unsigned long) addr | FREE_OP; |
| 173 |
// markTime (_sec, _usec); |
| 174 |
// printf ("free %d (%f)\n", _address, getTime()); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
void allocate (int sz) |
| 179 |
{ |
| 180 |
_address = ALLOCATE_OP; |
| 181 |
_size = sz; |
| 182 |
markTime (_sec, _usec); |
| 183 |
// printf ("allocate %d (%f)\n", sz, getTime()); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
void deallocate (int sz) |
| 188 |
{ |
| 189 |
_address = DEALLOCATE_OP; |
| 190 |
_size = sz; |
| 191 |
markTime (_sec, _usec); |
| 192 |
// printf ("allocate %d (%f)\n", sz, getTime()); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
// Set sec & usec to the current time. |
| 197 |
void markTime (long& /* sec */, long& /* usec */) |
| 198 |
{ |
| 199 |
#if 0 |
| 200 |
#ifdef __SVR4 // Solaris |
| 201 |
hrtime_t t; |
| 202 |
t = gethrtime(); |
| 203 |
sec = *((long *) &t); |
| 204 |
usec = *((long *) &t + 1); |
| 205 |
#else |
| 206 |
struct timeval tv; |
| 207 |
struct timezone tz; |
| 208 |
gettimeofday (&tv, &tz); |
| 209 |
sec = tv.tv_sec; |
| 210 |
usec = tv.tv_usec; |
| 211 |
#endif |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
int getType() { |
| 216 |
return _address & 7; |
| 217 |
} |
| 218 |
|
| 219 |
int getAllocated() { |
| 220 |
return _size; |
| 221 |
} |
| 222 |
|
| 223 |
int getDeallocated() { |
| 224 |
return _size; |
| 225 |
} |
| 226 |
|
| 227 |
unsigned long getAddress() { |
| 228 |
return _address & ~7; |
| 229 |
} |
| 230 |
|
| 231 |
int getSize() { |
| 232 |
return _size; |
| 233 |
} |
| 234 |
|
| 235 |
#if 0 |
| 236 |
double getTime() { |
| 237 |
return (double) _sec + (double) _usec / 1000000.0; |
| 238 |
} |
| 239 |
|
| 240 |
long getSeconds() { |
| 241 |
return _sec; |
| 242 |
} |
| 243 |
|
| 244 |
long getUseconds() { |
| 245 |
return _usec; |
| 246 |
} |
| 247 |
|
| 248 |
friend int operator< (MemoryRequest& m, MemoryRequest& n) { |
| 249 |
return ((m._sec < n._sec) |
| 250 |
|| ((m._sec == n._sec) |
| 251 |
&& (m._usec < n._usec))); |
| 252 |
} |
| 253 |
|
| 254 |
friend int operator== (MemoryRequest& m, MemoryRequest& n) { |
| 255 |
return ((m._sec == n._sec) && (m._usec == n._usec)); |
| 256 |
} |
| 257 |
#endif |
| 258 |
|
| 259 |
private: |
| 260 |
int _size; // in bytes |
| 261 |
unsigned long _address; // The address returned by malloc/realloc |
| 262 |
#if 1 |
| 263 |
long _sec; // seconds as returned by gettimeofday |
| 264 |
long _usec; // microseconds as returned by gettimeofday |
| 265 |
#endif |
| 266 |
}; |
| 267 |
|
| 268 |
Log<MemoryRequest> log; |
| 269 |
|
| 270 |
bool allDone; |
| 271 |
|
| 272 |
|
| 273 |
}; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
#endif // HL_LOGHEAP_H_ |