memstatdump.c
7.79 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* memstatdump.c
*
* reads the detail output files into something that is perl-able
*
* Author: bugnion
* Date: nov. 94 -...
*
* Disclaimer: this is the grossest code ever written. Although it
* does the job, I am sometimes not quite sure how and mainly what
* possessed it to evolve in this direction, independently of my
* will.
*
*******************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#ifdef __alpha
#include <string.h>
#else
#include <bstring.h>
#endif
#include <malloc.h>
#include "memstatdump.h"
struct Context{
int fd;
VA addr;
int remaining;
int eof;
};
static void GetNextBucket( struct Context *cnt,uint *bucket);
static void CalcDiff(uint *buck1,uint *buck2, uint *buck3);
int fd;
VA simRecHighRes;
VA simRecLowRes;
int numCPUs;
SimRecHeader msHeader;
static int GetLog2(long x);
#ifdef __alpha
int VASizeLog2 = 44;
#else
int VASizeLog2 = 32;
#endif
int MemStatFileOpen(char *filename)
{
char *fieldNames;
char *start,*end;
int count=0;
fd = open(filename,O_RDONLY,0);
if( fd < 0 ) {
return fd;
}
if( read(fd,(char *) &msHeader,sizeof(msHeader))!=(int)sizeof(msHeader)) {
fprintf(stderr,"read failed -bailing \n");
exit(1);
}
if( msHeader.magic != SIMREC_MAGIC ) {
fprintf(stderr,"wrong magic number!\n");
exit(1);
}
if (msHeader.version< 6) {
fprintf(stderr,"wrong version number %d. Expecting 6 or greater \n",
msHeader.version);
exit(1);
}
fieldNames = (char *)malloc(msHeader.fieldNamesLen);
if (read(fd,fieldNames,msHeader.fieldNamesLen)!=msHeader.fieldNamesLen) {
fprintf(stderr,"read failed -bailing \n");
exit(1);
}
simRecHighRes = msHeader.highRes;
simRecLowRes = msHeader.lowRes;
if (1) {
fprintf(stderr,"MemStat file: ");
fprintf(stderr,"version=%i name=%s ",
msHeader.version,msHeader.tableName);
fprintf(stderr," highRes=0x%lx lowRes=0x%lx fieldSize=%i bucketSize=%i\n",
msHeader.highRes,
msHeader.lowRes,msHeader.entrySize,msHeader.bucketSize);
start = fieldNames;
while (1) {
end = start;
while (*end && *end != ',') {
end++;
}
if (!(*end)) {
end = 0;
} else {
*end = '\0';
}
printf("FIELD %d %s\n",count,start);
if (!end) { break;}
start = end+1;
count++;
}
}
return fd;
}
void MemStatFileIterate( void (*process)(VA,void *,Resolution))
{
char bucket[1024];
VA a;
long rd,i,numChunks;
int startEnd[2];
Resolution resolution;
numChunks = 1<<(VASizeLog2 - GetLog2(msHeader.lowRes)) ;
/* lowRes */
resolution = Low;
for(a=0;a<numChunks;++a) {
if( read(fd,bucket,msHeader.bucketSize)!=msHeader.bucketSize) {
fprintf(stderr,"read failed at lowRes\n");
exit(1);
}
process(a*msHeader.lowRes,bucket,resolution);
}
/* out of range */
if( read(fd,bucket,(uint)msHeader.bucketSize)!=msHeader.bucketSize) {
fprintf(stderr,"read failed at lowRes\n");
process(0,bucket,resolution);
exit(1);
}
process(0,bucket,resolution);
/* highRes and detailRes*/
while(( rd = read(fd,startEnd,8))==8 ) {
if( !startEnd[0] && !startEnd[1] ) /* end of High Res Area */
break;
if( startEnd[1] < 0 ) { /* detail resolution */
resolution = Detail;
startEnd[1] *= -1;
} else
resolution = High;
for(a=0;a<(VA)startEnd[1];++a) {
rd = read(fd,bucket,(uint)msHeader.bucketSize);
if( rd != msHeader.bucketSize ) {
fprintf(stderr,"read failed at %08x %i out of %i\n",startEnd[0],
a,startEnd[1]);
}
for(i=0;i<msHeader.bucketSize;++i)
if( bucket[i] )
break;
if( i !=msHeader.bucketSize ) {
if( resolution==High )
process((VA)startEnd[0]+a*simRecHighRes,bucket,resolution);
else
process((VA)startEnd[0]+a*4,bucket,resolution);
}
}
}
close(fd);
}
void MemStatDiff(char *file1, char *file2,char *file3)
{
char tableName1[24];
struct Context cnt1,cnt2;
int out[2],i;
long numChunks;
uint b1[1024];
uint b2[1024];
uint b3[1024];
struct SimRecHeader hdr;
if( MemStatFileOpen(file1)<0 ) {
fprintf(stderr,"could not open %s\n",file1);
return;
}
cnt1.fd = fd;
if( MemStatFileOpen(file2)<0) {
fprintf(stderr,"could not open %s\n",file2);
return;
}
cnt2.fd = fd;
fd = open(file3, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
printf("Error in MemStatDiff\n");
return;
}
/* for now, assume full compatibility */
cnt1.eof = 0;
cnt2.eof = 0;
cnt1.remaining = 0;
cnt2.remaining = 0;
hdr.magic = SIMREC_MAGIC;
hdr.version = 2;
strncpy(hdr.tableName, tableName1, sizeof(hdr.tableName)-1);
hdr.addrStart = 0;
hdr.lowRes = simRecLowRes;
hdr.highRes = simRecHighRes;
hdr.bucketSize = msHeader.bucketSize;
numChunks = 1<<(VASizeLog2 - GetLog2(msHeader.lowRes)) ;
if (write(fd, (char *) &hdr, sizeof(hdr)) != sizeof(hdr))
goto err;
/* lowRes bucket + out of bounds bucket */
for( i=0;i<numChunks+ 1;++i) {
if( read(cnt1.fd,b1,msHeader.bucketSize)!=msHeader.bucketSize)
goto err;
if( read(cnt2.fd,b2,msHeader.bucketSize) != msHeader.bucketSize)
goto err;
CalcDiff(b1,b2,b3);
if( write(fd,b3,msHeader.bucketSize) != msHeader.bucketSize)
goto err;
}
GetNextBucket(&cnt1,b1);
GetNextBucket(&cnt2,b2);
while( !cnt2.eof ) {
while( !cnt2.eof && (cnt1.eof || cnt1.addr > cnt2.addr) ) {
/* printf("writing %08x %08x\n",cnt1.addr,cnt2.addr); */
out[0] = cnt2.addr;
out[1] = (cnt2.remaining < 0 ? -1 : 1);
if( write(fd,out,8) !=8)
goto err;
if( write(fd,b2,msHeader.bucketSize) != msHeader.bucketSize)
goto err;
GetNextBucket(&cnt2,b2);
}
if( (!cnt1.eof) && (!cnt2.eof) ) {
if( cnt1.addr != cnt2.addr )
printf("Address mismatch\n");
CalcDiff(b1,b2,b3);
out[0] = cnt2.addr;
out[1] = (cnt2.remaining < 0 ? -1 : 1);
/* printf("writing %08x %08x\n",cnt1.addr,cnt2.addr); */
if( write(fd,out,8) !=8)
goto err;
if( write(fd,b3,msHeader.bucketSize) != msHeader.bucketSize)
goto err;
GetNextBucket(&cnt1,b1);
GetNextBucket(&cnt2,b2);
}
}
out[0] = 0;
out[1] = 0;
if( write(fd,out,8)!=8)
goto err;
close(fd);
close(cnt1.fd);
close(cnt2.fd);
return;
err:
printf("Errro in MemStatDiff");
}
int
GetLineFromFile(FILE * f, char *buffer, int bufsize)
{
int i;
int n;
for (i = 0; i < bufsize-1; i++) {
n = fgetc(f);
if (n ==EOF) {
return 0;
}
buffer[i] = n;
if (n == '\n') break;
}
buffer[i] = '\000';
return i+1;
}
static void
GetNextBucket(struct Context *cnt, uint *bucket)
{
if( cnt->eof )
printf("EOF, %i\n",cnt->fd);
if( cnt->remaining > 0 )
--cnt->remaining;
if( cnt->remaining < 0 )
++cnt->remaining;
if( !cnt->remaining ) {
if( read(cnt->fd,&cnt->addr,8)!=8)
goto err;
if( !cnt->addr) {
cnt->eof = 1;
return;
}
} else {
if( cnt->remaining < 0 )
cnt->addr += 4;
else
cnt->addr += simRecHighRes;
}
/* printf("Getting %08x for %i\n",cnt->addr,cnt->fd); */
if( read(cnt->fd,bucket,msHeader.bucketSize)!=msHeader.bucketSize)
goto err;
return;
err:
printf("Error Getnextbucket %08x %i\n",cnt->addr,cnt->fd);
exit(1);
}
static void CalcDiff(uint *buck1,uint *buck2, uint *buck3)
{
fprintf(stderr,"calcdiff not implemented \n");
}
static int GetLog2(long x)
{
long i;
int r=0;
for(i=1;i<x;i*=2)
r++;
if( i != x ) {
fprintf(stderr,"Error: GetLog2(%i) is undefined \n",x);
exit(1);
}
return r;
}