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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%% @doc Utility functions to operate on percept data. These functions should
%% be considered experimental. Behaviour may change in future releases.
-module(percept_analyzer).
-export([
minmax/1,
waiting_activities/1,
activities2count/2,
activities2count/3,
activities2count2/2,
analyze_activities/2,
runnable_count/1,
runnable_count/2,
seconds2ts/2,
minmax_activities/2,
mean/1
]).
-include("percept.hrl").
%%==========================================================================
%%
%% Interface functions
%%
%%==========================================================================
%% @spec minmax([{X, Y}]) -> {MinX, MinY, MaxX, MaxY}
%% X = number()
%% Y = number()
%% MinX = number()
%% MinY = number()
%% MaxX = number()
%% MaxY = number()
%% @doc Returns the min and max of a set of 2-dimensional numbers.
minmax(Data) ->
Xs = [ X || {X,_Y} <- Data],
Ys = [ Y || {_X, Y} <- Data],
{lists:min(Xs), lists:min(Ys), lists:max(Xs), lists:max(Ys)}.
%% @spec mean([number()]) -> {Mean, StdDev, N}
%% Mean = float()
%% StdDev = float()
%% N = integer()
%% @doc Calculates the mean and the standard deviation of a set of
%% numbers.
mean([]) -> {0, 0, 0};
mean([Value]) -> {Value, 0, 1};
mean(List) -> mean(List, {0, 0, 0}).
mean([], {Sum, SumSquare, N}) ->
Mean = Sum / N,
StdDev = math:sqrt((SumSquare - Sum*Sum/N)/(N - 1)),
{Mean, StdDev, N};
mean([Value | List], {Sum, SumSquare, N}) ->
mean(List, {Sum + Value, SumSquare + Value*Value, N + 1}).
activities2count2(Acts, StartTs) ->
Start = inactive_start_states(Acts),
activities2count2(Acts, StartTs, Start, []).
activities2count2([], _, _, Out) -> lists:reverse(Out);
activities2count2([#activity{ id = Id, timestamp = Ts, state = active} | Acts], StartTs, {Proc,Port}, Out) when is_pid(Id) ->
activities2count2(Acts, StartTs, {Proc + 1, Port}, [{?seconds(Ts, StartTs), Proc + 1, Port}|Out]);
activities2count2([#activity{ id = Id, timestamp = Ts, state = inactive} | Acts], StartTs, {Proc,Port}, Out) when is_pid(Id) ->
activities2count2(Acts, StartTs, {Proc - 1, Port}, [{?seconds(Ts, StartTs), Proc - 1, Port}|Out]);
activities2count2([#activity{ id = Id, timestamp = Ts, state = active} | Acts], StartTs, {Proc,Port}, Out) when is_port(Id) ->
activities2count2(Acts, StartTs, {Proc, Port + 1}, [{?seconds(Ts, StartTs), Proc, Port + 1}|Out]);
activities2count2([#activity{ id = Id, timestamp = Ts, state = inactive} | Acts], StartTs, {Proc,Port}, Out) when is_port(Id) ->
activities2count2(Acts, StartTs, {Proc, Port - 1}, [{?seconds(Ts, StartTs), Proc, Port - 1}|Out]).
inactive_start_states(Acts) ->
D = activity_start_states(Acts, dict:new()),
dict:fold(fun
(K, inactive, {Procs, Ports}) when is_pid(K) -> {Procs + 1, Ports};
(K, inactive, {Procs, Ports}) when is_port(K) -> {Procs, Ports + 1};
(_, _, {Procs, Ports}) -> {Procs, Ports}
end, {0,0}, D).
activity_start_states([], D) -> D;
activity_start_states([#activity{id = Id, state = State}|Acts], D) ->
case dict:is_key(Id, D) of
true -> activity_start_states(Acts, D);
false -> activity_start_states(Acts, dict:store(Id, State, D))
end.
%% @spec activities2count(#activity{}, timestamp()) -> Result
%% Result = [{Time, ProcessCount, PortCount}]
%% Time = float()
%% ProcessCount = integer()
%% PortCount = integer()
%% @doc Calculate the resulting active processes and ports during
%% the activity interval.
%% Also checks active/inactive consistency.
%% A task will always begin with an active state and end with an inactive state.
activities2count(Acts, StartTs) when is_list(Acts) -> activities2count(Acts, StartTs, separated).
activities2count(Acts, StartTs, Type) when is_list(Acts) -> activities2count_loop(Acts, {StartTs, {0,0}}, Type, []).
activities2count_loop([], _, _, Out) -> lists:reverse(Out);
activities2count_loop(
[#activity{ timestamp = Ts, id = Id, runnable_count = Rc} | Acts],
{StartTs, {Procs, Ports}}, separated, Out) ->
Time = ?seconds(Ts, StartTs),
case Id of
Id when is_port(Id) ->
Entry = {Time, Procs, Rc},
activities2count_loop(Acts, {StartTs, {Procs, Rc}}, separated, [Entry | Out]);
Id when is_pid(Id) ->
Entry = {Time, Rc, Ports},
activities2count_loop(Acts, {StartTs, {Rc, Ports}}, separated, [Entry | Out]);
_ ->
activities2count_loop(Acts, {StartTs,{Procs, Ports}}, separated, Out)
end;
activities2count_loop(
[#activity{ timestamp = Ts, id = Id, runnable_count = Rc} | Acts],
{StartTs, {Procs, Ports}}, summated, Out) ->
Time = ?seconds(Ts, StartTs),
case Id of
Id when is_port(Id) ->
Entry = {Time, Procs + Rc},
activities2count_loop(Acts, {StartTs, {Procs, Rc}}, summated, [Entry | Out]);
Id when is_pid(Id) ->
Entry = {Time, Rc + Ports},
activities2count_loop(Acts, {StartTs, {Rc, Ports}}, summated, [Entry | Out])
end.
%% @spec waiting_activities([#activity{}]) -> FunctionList
%% FunctionList = [{Seconds, Mfa, {Mean, StdDev, N}}]
%% Seconds = float()
%% Mfa = mfa()
%% Mean = float()
%% StdDev = float()
%% N = integer()
%% @doc Calculates the time, both average and total, that a process has spent
%% in a receive state at specific function. However, if there are multiple receives
%% in a function it cannot differentiate between them.
waiting_activities(Activities) ->
ListedMfas = waiting_activities_mfa_list(Activities, []),
Unsorted = lists:foldl(
fun (Mfa, MfaList) ->
{Total, WaitingTimes} = get({waiting_mfa, Mfa}),
% cleanup
erlang:erase({waiting_mfa, Mfa}),
% statistics of receive waiting places
Stats = mean(WaitingTimes),
[{Total, Mfa, Stats} | MfaList]
end, [], ListedMfas),
lists:sort(fun ({A,_,_},{B,_,_}) ->
if
A > B -> true;
true -> false
end
end, Unsorted).
%% Generate lists of receive waiting times per mfa
%% Out:
%% ListedMfas = [mfa()]
%% Intrisnic:
%% get({waiting, mfa()}) ->
%% [{waiting, mfa()}, {Total, [WaitingTime]})
%% WaitingTime = float()
waiting_activities_mfa_list([], ListedMfas) -> ListedMfas;
waiting_activities_mfa_list([Activity|Activities], ListedMfas) ->
#activity{id = Pid, state = Act, timestamp = Time, where = MFA} = Activity,
case Act of
active ->
waiting_activities_mfa_list(Activities, ListedMfas);
inactive ->
% Want to know how long the wait is in a receive,
% it is given via the next activity
case Activities of
[] ->
[Info] = percept_db:select(information, Pid),
case Info#information.stop of
undefined ->
% get profile end time
Waited = ?seconds(
percept_db:select({system,stop_ts}),
Time);
Time2 ->
Waited = ?seconds(Time2, Time)
end,
case get({waiting_mfa, MFA}) of
undefined ->
put({waiting_mfa, MFA}, {Waited, [Waited]}),
[MFA | ListedMfas];
{Total, TimedMfa} ->
put({waiting_mfa, MFA}, {Total + Waited, [Waited | TimedMfa]}),
ListedMfas
end;
[#activity{timestamp=Time2, id = Pid, state = active} | _ ] ->
% Calculate waiting time
Waited = ?seconds(Time2, Time),
% Get previous entry
case get({waiting_mfa, MFA}) of
undefined ->
% add entry to list
put({waiting_mfa, MFA}, {Waited, [Waited]}),
waiting_activities_mfa_list(Activities, [MFA|ListedMfas]);
{Total, TimedMfa} ->
put({waiting_mfa, MFA}, {Total + Waited, [Waited | TimedMfa]}),
waiting_activities_mfa_list(Activities, ListedMfas)
end;
_ -> error
end
end.
%% seconds2ts(Seconds, StartTs) -> TS
%% In:
%% Seconds = float()
%% StartTs = timestamp()
%% Out:
%% TS = timestamp()
%% @spec seconds2ts(float(), StartTs::{integer(),integer(),integer()}) -> timestamp()
%% @doc Calculates a timestamp given a duration in seconds and a starting timestamp.
seconds2ts(Seconds, {Ms, S, Us}) ->
% Calculate mega seconds integer
MsInteger = trunc(Seconds) div 1000000 ,
% Calculate the reminder for seconds
SInteger = trunc(Seconds),
% Calculate the reminder for micro seconds
UsInteger = trunc((Seconds - SInteger) * 1000000),
% Wrap overflows
UsOut = (UsInteger + Us) rem 1000000,
SOut = ((SInteger + S) + (UsInteger + Us) div 1000000) rem 1000000,
MsOut = (MsInteger+ Ms) + ((SInteger + S) + (UsInteger + Us) div 1000000) div 1000000,
{MsOut, SOut, UsOut}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Analyze interval for concurrency
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @spec analyze_activities(integer(), [#activity{}]) -> [{integer(),#activity{}}]
%% @hidden
analyze_activities(Threshold, Activities) ->
RunnableCount = runnable_count(Activities, 0),
analyze_runnable_activities(Threshold, RunnableCount).
%% runnable_count(Activities, StartValue) -> RunnableCount
%% In:
%% Activities = [activity()]
%% StartValue = integer()
%% Out:
%% RunnableCount = [{integer(), activity()}]
%% Purpose:
%% Calculate the runnable count of a given interval of generic
%% activities.
%% @spec runnable_count([#activity{}]) -> [{integer(),#activity{}}]
%% @hidden
runnable_count(Activities) ->
Threshold = runnable_count_threshold(Activities),
runnable_count(Activities, Threshold, []).
runnable_count_threshold(Activities) ->
CountedActs = runnable_count(Activities, 0),
Counts = [C || {C, _} <- CountedActs],
Min = lists:min(Counts),
0 - Min.
%% @spec runnable_count([#activity{}],integer()) -> [{integer(),#activity{}}]
%% @hidden
runnable_count(Activities, StartCount) when is_integer(StartCount) ->
runnable_count(Activities, StartCount, []).
runnable_count([], _ , Out) ->
lists:reverse(Out);
runnable_count([A | As], PrevCount, Out) ->
case A#activity.state of
active ->
runnable_count(As, PrevCount + 1, [{PrevCount + 1, A} | Out]);
inactive ->
runnable_count(As, PrevCount - 1, [{PrevCount - 1, A} | Out])
end.
%% In:
%% Threshold = integer(),
%% RunnableActivities = [{Rc, activity()}]
%% Rc = integer()
analyze_runnable_activities(Threshold, RunnableActivities) ->
analyze_runnable_activities(Threshold, RunnableActivities, []).
analyze_runnable_activities( _z, [], Out) ->
lists:reverse(Out);
analyze_runnable_activities(Threshold, [{Rc, Act} | RunnableActs], Out) ->
if
Rc =< Threshold ->
analyze_runnable_activities(Threshold, RunnableActs, [{Rc,Act} | Out]);
true ->
analyze_runnable_activities(Threshold, RunnableActs, Out)
end.
%% minmax_activity(Activities, Count) -> {Min, Max}
%% In:
%% Activities = [activity()]
%% InitialCount = non_neg_integer()
%% Out:
%% {Min, Max}
%% Min = non_neg_integer()
%% Max = non_neg_integer()
%% Purpose:
%% Minimal and maximal activity during an activity interval.
%% Initial activity count needs to be supplied.
%% @spec minmax_activities([#activity{}], integer()) -> {integer(), integer()}
%% @doc Calculates the minimum and maximum of runnable activites (processes
% and ports) during the interval of reffered by the activity list.
minmax_activities(Activities, Count) ->
minmax_activities(Activities, Count, {Count, Count}).
minmax_activities([], _, Out) ->
Out;
minmax_activities([A|Acts], Count, {Min, Max}) ->
case A#activity.state of
active ->
minmax_activities(Acts, Count + 1, {Min, lists:max([Count + 1, Max])});
inactive ->
minmax_activities(Acts, Count - 1, {lists:min([Count - 1, Min]), Max})
end.
|