aboutsummaryrefslogtreecommitdiff
path: root/malloc/trace_dump.c
blob: f0349f3e6f7d4c28dcd1a393b262c4bffa99d3b7 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

// The trace file looks like an array of struct __malloc_trace_buffer_s
#include "mtrace.h"

typedef long long BIG;

static BIG
get_int (unsigned char **ptr)
{
  BIG rv = 0;
  while (1)
  {
    unsigned char c = *(*ptr)++;
    rv |= (c & 0x7f);
    if (c & 0x80)
      rv <<= 7;
    else
      return rv;
  }
}


int
data_looks_like_raw_trace (unsigned char *data, long n_data)
{
  long lim = n_data > 1024 ? 1020 : (n_data - 4);
  long i;

  // free and malloc calls will have a NULL we can look for
  for (i=0; i<lim; i++)
    if (memcmp (data+i, "\0\0\0\0", 4) == 0)
	return 1;

  return 0;
}

const char * const typenames[] = {
  "unused",
  "malloc",
  "calloc",
  "free",
  "realloc",
  "memalign",
  "valloc",
  "pvalloc",
  "posix_memalign",
};

void
dump_raw_trace (unsigned char *data, long n_data)
{
  unsigned char *edata = data + n_data;
  long head;

  head = n_data / sizeof (struct __malloc_trace_buffer_s);

  printf ("%ld out of %ld events captured (I think)\n", head, head);

  printf ("%8s %8s %8s %16s %16s %16s %16s %16s\n",
	  "threadid", "type", "path", "ptr1", "size", "ptr2", "size2", "size3");

  while (data <= edata - sizeof (struct __malloc_trace_buffer_s))
    {
      struct __malloc_trace_buffer_s *t = (struct __malloc_trace_buffer_s *)data;

      switch (t->type)
	{
	case __MTB_TYPE_UNUSED:
	  break;
	default:
	  /* Consider 'memalign' to be the largest API word we want to align
	     on so make the name 8 chars wide at a minimum.  */
	  printf ("%08x %8s %c%c%c%c%c%c%c%c%c%c%c%c%c%c %016llx %016llx %016llx %016llx %016llx\n",
		  t->thread,
		  t->type == __MTB_TYPE_MAGIC ? "magic" : typenames[t->type],
		  t->path_thread_cache ? 'T' : '-',
		  t->path_cpu_cache ? 'c' : '-',
		  t->path_cpu_cache2 ? 'C' : '-',
		  t->path_sbrk ? 's' : '-',
		  t->path_mmap ? 'M' : '-',
		  t->path_munmap ? 'U' : '-',
		  t->path_m_f_realloc ? 'R' : '-',
		  t->path_hook ? 'H' : '-',
		  t->path_unsorted_add ? 'U' : '-',
		  t->path_unsorted_remove ? 'u' : '-',
		  t->path_unsorted_empty ? 'E' : '-',
		  t->path_fastbin_add ? 'F' : '-',
		  t->path_fastbin_remove ? 'f' : '-',
		  t->path_malloc_consolidate ? 'C' : '-',
		  (long long unsigned int) (size_t) t->ptr1,
		  (long long unsigned int) t->size,
		  (long long unsigned int) (size_t) t->ptr2,
		  (long long unsigned int) t->size2,
		  (long long unsigned int) t->size3);
	  break;
	}

      data += sizeof (struct __malloc_trace_buffer_s);
    }
}

void
dump_workload (unsigned char *data, long n_data)
{
  unsigned char *orig_data = data;
  unsigned char *edata = data + n_data;
  BIG thread_idx = 0;
  BIG n_ptrs, n_syncs, n_threads, idx, p1, p2, sz;

  while (data < edata)
    {
      printf("%016lx: %4lld: ", data - orig_data, thread_idx);
      switch (*data++)
	{
	case C_NOP:
	  break;
	case C_ALLOC_PTRS:
	  n_ptrs = get_int(&data);
	  printf("AllocPtrs: %lld\n", n_ptrs);
	  break;
	case C_ALLOC_SYNCS:
	  n_syncs = get_int(&data);
	  printf("AllocSyncs: %lld\n", n_syncs);
	  break;
	case C_NTHREADS:
	  n_threads = get_int (&data);
	  printf("NThreads: %lld\n", n_threads);
	  break;
	case C_START_THREAD:
	  idx = get_int (&data);
	  printf("StartThread: 0x%llx\n", idx);
	  break;
	case C_DONE:
	  printf("Done\n");
	  thread_idx ++;
	  break;

	case C_MALLOC:
	  p2 = get_int (&data);
	  sz = get_int (&data);
	  printf("Malloc (%lld) -> %lld\n", sz, p2);
	  break;

	case C_CALLOC:
	  p2 = get_int (&data);
	  sz = get_int (&data);
	  printf("Calloc (%lld) -> %lld\n", sz, p2);
	  break;

	case C_REALLOC:
	  p2 = get_int (&data);
	  p1 = get_int (&data);
	  sz =