summaryrefslogtreecommitdiff
path: root/lib/tools/src/instrument.erl
blob: 34c5ba04cc1ba6239a5b4d67e1ff093fae40ae4a (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1998-2009. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%
-module(instrument).

-export([holes/1, mem_limits/1, memory_data/0, read_memory_data/1,
	 sort/1, store_memory_data/1, sum_blocks/1,
	 descr/1, type_descr/2, allocator_descr/2, class_descr/2,
	 type_no_range/1, block_header_size/1, store_memory_status/1,
	 read_memory_status/1, memory_status/1]).


-define(OLD_INFO_SIZE, 32). %% (sizeof(mem_link) in pre R9C utils.c)

-define(IHMARKER(H),  element(1, H)).
-define(VSN(H),       element(2, H)).
-define(INFO_SIZE(H), element(3, H)).
-define(TYPEMAP(H),   element(4, H)).

-define(IHDR(H), is_tuple(H), ?IHMARKER(H) =:= instr_hdr).
-define(IHDRVSN(H, V), ?IHDR(H), ?VSN(H) =:= V).

memory_data() ->
    case catch erlang:system_info(allocated) of
	{'EXIT',{Error,_}} ->
	    erlang:error(Error, []);
	{'EXIT',Error} ->
	    erlang:error(Error, []);
	Res ->
	    Res
    end.

store_memory_data(File) ->
    case catch erlang:system_info({allocated, File}) of
	{'EXIT',{Error,_}} ->
	    erlang:error(Error, [File]);
	{'EXIT',Error} ->
	    erlang:error(Error, [File]);
	Res ->
	    Res
    end.

memory_status(Type) when is_atom(Type) ->
    case catch erlang:system_info({allocated, status, Type}) of
	{'EXIT',{Error,_}} ->
	    erlang:error(Error, [Type]);
	{'EXIT',Error} ->
	    erlang:error(Error, [Type]);
	Res ->
	    Res
    end;
memory_status(Type) ->
    erlang:error(badarg, [Type]).

store_memory_status(File) when is_list(File) ->
    case catch erlang:system_info({allocated, status, File}) of
	{'EXIT',{Error,_}} ->
	    erlang:error(Error, [File]);
	{'EXIT',Error} ->
	    erlang:error(Error, [File]);
	Res ->
	    Res
    end;
store_memory_status(File) ->
    erlang:error(badarg, [File]).

read_memory_data(File) when is_list(File) ->
    case file:consult(File) of
	{ok, [Hdr|MD]} when ?IHDR(Hdr) ->
	    {Hdr, MD};
	{ok, [{T,A,S,undefined}|_] = MD} when is_integer(T),
					      is_integer(A),
					      is_integer(S) ->
	    {{instr_hdr, 1, ?OLD_INFO_SIZE}, MD};
	{ok, [{T,A,S,{X,Y,Z}}|_] = MD} when is_integer(T),
					    is_integer(A),
					    is_integer(S),
					    is_integer(X),
					    is_integer(Y),
					    is_integer(Z) ->
	    {{instr_hdr, 1, ?OLD_INFO_SIZE}, MD};
	{ok, _} ->
	    {error, eio};
	Error ->
	    Error
    end;
read_memory_data(File) ->
    erlang:error(badarg, [File]).

read_memory_status(File) when is_list(File) ->
    case file:consult(File) of
	{ok, [{instr_vsn, _}|Stat]} ->
	    Stat;
	{ok, _} ->
	    {error, eio};
	Error ->
	    Error
    end;
read_memory_status(File) ->
    erlang:error(badarg, [File]).

holes({Hdr, MD}) when ?IHDR(Hdr) ->
    check_holes(?INFO_SIZE(Hdr), MD).

check_holes(_ISz, []) ->
    ok;
check_holes(ISz, [E | L]) ->
    check_holes(ISz, E, L).

check_holes(_ISz, _E1, []) ->
    io:format("~n");
check_holes(ISz, E1, [E2 | Rest]) ->
    check_hole(ISz, E1, E2),
    check_holes(ISz, E2, Rest).

check_hole(ISz, {_,P1,S1,_}, {_,P2,_,_}) ->
    End = P1+S1,
    Hole = P2 - (End + ISz),
    if
	Hole =< 7 ->
	    ok;
	true ->
	    io:format(" ~p", [Hole])
    end.

sum_blocks({Hdr, L}) when ?IHDR(Hdr) ->
    lists:foldl(fun({_,_,S,_}, Sum) -> S+Sum end,
		0,
		L).

mem_limits({Hdr, L}) when ?IHDR(Hdr) ->
    {_, P1, _, _} = hd(L),
    {_, P2, S2, _} = lists:last(L),
    {P1, P2+S2}.

sort({Hdr, MD}) when ?IHDR(Hdr) ->
    {Hdr, lists:keysort(2, MD)}.

descr({Hdr, MD} = ID) when ?IHDR(Hdr) ->
    {Hdr, lists:map(fun ({TN, Addr, Sz, {0, N, S}}) ->
			    {type_descr(ID, TN),
			     Addr,
			     Sz,
			     list_to_pid("<0."
					 ++ integer_to_list(N)
					 ++ "."
					 ++ integer_to_list(S)
					 ++ ">")};
			({TN, Addr, Sz, undefined}) ->
			    {type_descr(ID, TN),
			     Addr,
			     Sz,
			     undefined}
		    end,
		    MD)}.

block_header_size({Hdr, _}) when ?IHDR(Hdr) ->
    ?INFO_SIZE(Hdr).

type_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 2),
				  is_integer(TypeNo) ->
    case catch element(1, element(TypeNo, ?TYPEMAP(Hdr))) of
	{'EXIT', _} -> invalid_type;
	Type -> Type
    end;
type_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 1),
				  is_integer(TypeNo) ->
    type_string(TypeNo).


allocator_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 2), is_integer(TypeNo) ->
    case catch element(2, element(TypeNo, ?TYPEMAP(Hdr))) of
	{'EXIT', _} -> invalid_type;
	Type -> Type
    end;
allocator_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 1), is_integer(TypeNo) ->
    "unknown".

class_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 2), is_integer(TypeNo) ->
    case catch element(3, element(TypeNo, ?TYPEMAP(Hdr))) of
	{'EXIT', _} -> invalid_type;
	Type -> Type
    end;
class_descr({Hdr, _}, TypeNo) when ?IHDRVSN(Hdr, 1), is_integer(TypeNo) ->
    "unknown".

type_no_range({Hdr, _}) when ?IHDRVSN(Hdr, 2) ->
    {1, tuple_size(?TYPEMAP(Hdr))};
type_no_range({Hdr, _}) when ?IHDRVSN(Hdr, 1) ->
    {-1, 1000}.

type_string(-1) ->
    "unknown";
type_string(1) ->
    "atom text";
type_string(11) ->
    "atom desc";
type_string(2) ->
    "bignum (big_to_list)";
type_string(31) ->
    "fixalloc";
type_string(32) ->
    "unknown fixalloc block";
type_string(33) ->
    "message buffer";
type_string(34) ->
    "message link";
type_string(4) ->
    "estack";
type_string(40) ->
    "db table vec";
type_string(41) ->
    "db tree select buffer";
type_string(43) ->
    "db hash select buffer";
type_string(44) ->
    "db hash select list";
type_string(45) ->
    "db match prog stack";
type_string(46) ->
    "db match prog heap data";
type_string(47) ->
    "db temp buffer";
type_string(48) ->
    "db error";
type_string(49) ->
    "db error info";
type_string(50) ->
    "db trans tab";
type_string(51) ->
    "db segment";
type_string(52) ->
    "db term";
type_string(53) ->
    "db add_counter";
type_string(54) ->
    "db segment table";
type_string(55) ->
    "db table (fix)";
type_string(56) ->
    "db bindings";
type_string(57) ->
    "db counter";
type_string(58) ->
    "db trace vec";
type_string(59) ->
    "db fixed deletion";
type_string(60) ->
    "binary (external.c)";
type_string(61) ->
    "binary";
type_string(62) ->
    "procbin (fix)";
type_string(70) ->
    "driver alloc (io.c)";
type_string(71) ->
    "binary (io.c)";
type_string(72) ->
    "binary vec (io.c)";
type_string(73) ->
    "binary vec 2 (io.c)";
type_string(74) ->
    "io vec (io.c)";
type_string(75) ->
    "io vec 2 (io.c)";
type_string(76) ->
    "temp io buffer (io.c)";
type_string(77) ->
    "temp io buffer 2 (io.c)";
type_string(78) ->
    "line buffer (io.c)";
type_string(8) ->
    "heap";
type_string(801) ->
    "heap (1)";
type_string(802) ->
    "heap (2)";
type_string(803) ->
    "heap (3)";
type_string(804) ->
    "heap (4)";
type_string(805) ->
    "heap (5)";
type_string(821) ->
    "heap fragment (1)";
type_string(822) ->
    "heap fragment (2)";
type_string(830) ->
    "sequential store buffer (for vectors)";
type_string(91) ->
    "process table";
type_string(92) ->
    "process desc";
type_string(110) ->
    "hash buckets";
type_string(111) ->
    "hash table";
type_string(120) ->
    "index init";
type_string(121) ->
    "index table";
type_string(130) ->
    "temp buffer";
type_string(140) ->
    "timer wheel";
type_string(150) ->
    "distribution cache";
type_string(151) ->
    "dmem";
type_string(152) ->
    "distribution table";
type_string(153) ->
    "distribution table buckets";
type_string(154) ->
    "distribution table entry";
type_string(155) ->
    "node table";
type_string(156) ->
    "node table buckets";
type_string(157) ->
    "node table entry";
type_string(160) ->
    "port table";
type_string(161) ->
    "driver entry";
type_string(162) ->
    "port setup";
type_string(163) ->
    "port wait";
type_string(170) ->
    "module";
type_string(171) ->
    "fundef";
type_string(180) ->
    "file table";
type_string(181) ->
    "driver table";
type_string(182) ->
    "poll struct";
type_string(190) ->
    "inet driver";
type_string(200) ->
    "efile driver";
type_string(210) ->
    "gc root set";
type_string(220) ->
    "breakpoint data";
type_string(230) ->
    "async queue";
type_string(231) ->
    "async (exit)";
type_string(232) ->
    "async (driver)";
type_string(240) ->
    "bits buffer";
type_string(241) ->
    "bits temp buffer";
type_string(250) ->
    "modules (loader)";
type_string(251) ->
    "code (loader)";
type_string(252) ->
    "atom tab (loader)";
type_string(253) ->
    "import tab (loader)";
type_string(254) ->
    "export tab (loader)";
type_string(255) ->
    "lable tab (loader)";
type_string(256) ->
    "gen op (loader)";
type_string(257) ->
    "gen op args (loader)";
type_string(258) ->
    "gen op args 2 (loader)";
type_string(259) ->
    "gen op args 3 (loader)";
type_string(260) ->
    "lambdas (loader)";
type_string(261) ->
    "temp int buffer (loader)";
type_string(262) ->
    "temp heap (loader)";
type_string(280) ->
    "dist ctrl msg buffer";
type_string(281) ->
    "dist_buf";
type_string(290) ->
    "call trace buffer";
type_string(300) ->
    "bif timer rec";
type_string(310) ->
    "argument registers";
type_string(320) ->
    "compressed binary temp buffer";
type_string(330) ->
    "term_to_binary temp buffer";
type_string(340) ->
    "proc dict";
type_string(350) ->
    "trace to port temp buffer";
type_string(360) ->
    "lists subtract temp buffer";
type_string(370) ->
    "link (lh)";
type_string(380) ->
    "port call buffer";
type_string(400) ->
    "definite_alloc block";
type_string(_) ->
    invalid_type.