This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
outfile-writer.c
319 lines (274 loc) · 9.59 KB
/
outfile-writer.c
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
* outfile-writer.c
*
* Copyright (C) 2005 Novell, Inc.
*
*/
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
#include <glib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include "outfile-writer.h"
#define MAGIC_NUMBER 0x4eabbdd1
#define FILE_FORMAT_VERSION 6
#define FILE_LABEL "heap-buddy logfile"
#define TAG_TYPE 0x01
#define TAG_METHOD 0x02
#define TAG_CONTEXT 0x03
#define TAG_GC 0x04
#define TAG_RESIZE 0x05
#define TAG_EOS 0xff
static void
write_byte (FILE *out, guint8 x)
{
fwrite (&x, sizeof (guint8), 1, out);
}
static void
write_pointer (FILE *out, gpointer x)
{
guint32 y = GPOINTER_TO_UINT (x);
fwrite (&y, sizeof (guint32), 1, out);
}
static void
write_int16 (FILE *out, gint16 x)
{
fwrite (&x, sizeof (gint16), 1, out);
}
static void
write_uint16 (FILE *out, guint16 x)
{
fwrite (&x, sizeof (guint16), 1, out);
}
static void
write_int32 (FILE *out, gint32 x)
{
fwrite (&x, sizeof (gint32), 1, out);
}
static void
write_uint32 (FILE *out, guint32 x)
{
fwrite (&x, sizeof (guint32), 1, out);
}
static void
write_int64 (FILE *out, gint64 x)
{
fwrite (&x, sizeof (gint64), 1, out);
}
static void
write_vint (FILE *out, guint32 x)
{
guint8 y;
do {
y = (guint8) (x & 0x7f);
x = x >> 7;
if (x != 0)
y |= 0x80;
write_byte (out, y);
} while (x != 0);
}
static void
write_string (FILE *out, const char *str)
{
int len = strlen (str);
write_vint (out, (guint32) len);
fwrite (str, sizeof (char), len, out);
}
OutfileWriter *
outfile_writer_open (const char *filename)
{
OutfileWriter *ofw;
ofw = g_new0 (OutfileWriter, 1);
ofw->out = fopen (filename, "w");
ofw->seen_items = g_hash_table_new (NULL, NULL);
ofw->gc_count = 0;
write_uint32 (ofw->out, MAGIC_NUMBER);
write_int32 (ofw->out, FILE_FORMAT_VERSION);
write_string (ofw->out, FILE_LABEL);
ofw->saved_outfile_offset = ftell (ofw->out);
// we update these after every GC
write_byte (ofw->out, 0); // is the log fully written out?
write_int32 (ofw->out, -1); // total # of GCs
write_int32 (ofw->out, -1); // total # of types
write_int32 (ofw->out, -1); // total # of methods
write_int32 (ofw->out, -1); // total # of contexts/backtraces
write_int32 (ofw->out, -1); // total # of resizes
write_int64 (ofw->out, -1); // total # of allocated bytes
write_int32 (ofw->out, -1); // total # of allocated objects
return ofw;
}
static void
outfile_writer_update_totals (OutfileWriter *ofw,
gint64 total_allocated_bytes,
gint32 total_allocated_objects)
{
// Seek back up to the right place in the header
fseek (ofw->out, ofw->saved_outfile_offset, SEEK_SET);
// Update our counts and totals
write_byte (ofw->out, 0); // we still might terminate abnormally
write_int32 (ofw->out, ofw->gc_count);
write_int32 (ofw->out, ofw->type_count);
write_int32 (ofw->out, ofw->method_count);
write_int32 (ofw->out, ofw->context_count);
write_int32 (ofw->out, ofw->resize_count);
if (total_allocated_bytes >= 0) {
write_int64 (ofw->out, total_allocated_bytes);
write_int32 (ofw->out, total_allocated_objects);
}
// Seek back to the end of the outfile
fseek (ofw->out, 0, SEEK_END);
}
void
outfile_writer_close (OutfileWriter *ofw)
{
// Write out the end-of-stream tag.
write_byte (ofw->out, TAG_EOS);
// Seek back up to the right place in the header
fseek (ofw->out, ofw->saved_outfile_offset, SEEK_SET);
// Indicate that we terminated normally.
write_byte (ofw->out, 1);
fclose (ofw->out);
}
void
outfile_writer_add_accountant (OutfileWriter *ofw,
Accountant *acct)
{
int i, frame_count;
char *name;
/* First, add this type if we haven't seen it before. */
if (g_hash_table_lookup (ofw->seen_items, acct->klass) == NULL) {
name = mono_type_full_name (mono_class_get_type (acct->klass));
write_byte (ofw->out, TAG_TYPE);
write_pointer (ofw->out, acct->klass);
write_string (ofw->out, name);
g_free (name);
g_hash_table_insert (ofw->seen_items, acct->klass, acct->klass);
++ofw->type_count;
}
/* Next, walk across the backtrace and add any previously-unseen
methods. */
frame_count = 0;
for (i = 0; acct->backtrace [i] != NULL; ++i) {
++frame_count;
MonoMethod *method = acct->backtrace [i]->method;
if (g_hash_table_lookup (ofw->seen_items, method) == NULL) {
char *name = mono_method_full_name (method, TRUE);
write_byte (ofw->out, TAG_METHOD);
write_pointer (ofw->out, method);
write_string (ofw->out, name);
g_free (name);
g_hash_table_insert (ofw->seen_items, method, method);
++ofw->method_count;
}
}
write_byte (ofw->out, TAG_CONTEXT);
write_pointer (ofw->out, acct);
write_pointer (ofw->out, acct->klass);
write_int16 (ofw->out, frame_count);
for (i = 0; acct->backtrace [i] != NULL; ++i) {
write_pointer (ofw->out, acct->backtrace [i]->method);
write_uint32 (ofw->out, acct->backtrace [i]->il_offset);
}
++ofw->context_count;
fflush (ofw->out);
}
void
type_writer_start_types (FILE * file, GHashTable *g)
{
// time_t timestamp;
// time (×tamp);
struct timeval time;
gettimeofday (&time, NULL);
//fprintf (file, "%d elements\n", (int)g_hash_table_size (g));
write_int64 (file, (gint64)time.tv_sec);
write_int64 (file, (gint64)time.tv_usec);
write_uint32 (file, (guint32)g_hash_table_size (g));
}
void
type_writer_update_types (FILE * file, const char *name, gint32 bytes)
{
//fprintf (file, "\t%d bytes of %s\n", (int)bytes, name);
write_int32 (file, bytes);
write_string (file, name);
}
// total_live_bytes is the total size of all of the live objects
// before the GC
void
outfile_writer_gc_begin (OutfileWriter *ofw,
gboolean is_final,
gint64 total_live_bytes,
gint32 total_live_objects,
gint32 n_accountants)
{
//time_t timestamp;
//time (×tamp);
struct timeval time;
gettimeofday (&time, NULL);
write_byte (ofw->out, TAG_GC);
write_int32 (ofw->out, is_final ? -1 : ofw->gc_count);
write_int64 (ofw->out, (gint64) time.tv_sec);
write_int64 (ofw->out, (gint64) time.tv_usec);
write_int64 (ofw->out, total_live_bytes);
write_int32 (ofw->out, total_live_objects);
write_int32 (ofw->out, n_accountants);
++ofw->gc_count;
}
void
outfile_writer_gc_log_stats (OutfileWriter *ofw,
Accountant *acct)
{
write_pointer (ofw->out, acct);
write_uint32 (ofw->out, acct->n_allocated_objects);
write_uint32 (ofw->out, acct->n_allocated_bytes);
write_uint32 (ofw->out, acct->allocated_total_age);
write_uint32 (ofw->out, acct->allocated_total_weight);
write_uint32 (ofw->out, acct->n_live_objects);
write_uint32 (ofw->out, acct->n_live_bytes);
write_uint32 (ofw->out, acct->live_total_age);
write_uint32 (ofw->out, acct->live_total_weight);
}
// total_live_bytes is the total size of all live objects
// after the GC is finished
void
outfile_writer_gc_end (OutfileWriter *ofw,
gint64 total_allocated_bytes,
gint32 total_allocated_objects,
gint64 total_live_bytes,
gint32 total_live_objects)
{
write_int64 (ofw->out, total_live_bytes);
write_int32 (ofw->out, total_live_objects);
outfile_writer_update_totals (ofw, total_allocated_bytes, total_allocated_objects);
fflush (ofw->out);
}
void
outfile_writer_resize (OutfileWriter *ofw,
gint64 new_size,
gint64 total_live_bytes)
{
time_t timestamp;
time (×tamp);
write_byte (ofw->out, TAG_RESIZE);
write_int64 (ofw->out, (gint64) timestamp);
write_int64 (ofw->out, new_size);
write_int64 (ofw->out, total_live_bytes);
++ofw->resize_count;
outfile_writer_update_totals (ofw, -1, -1);
fflush (ofw->out);
}