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