1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2019-2019. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21-module(megaco_test_sys_monitor).
22
23-export([start/0, stop/0,
24         init/1]).
25
26-define(NAME, ?MODULE).
27-define(GSM,  megaco_test_global_sys_monitor).
28
29
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31
32start() ->
33    Parent = self(),
34    proc_lib:start(?MODULE, init, [Parent]).
35
36stop() ->
37    case whereis(?NAME) of
38        Pid when is_pid(Pid) ->
39            Pid ! {?MODULE, self(), stop},
40            receive
41                {?MODULE, Pid, stop} ->
42                    ok
43            end;
44        _ ->
45            ok
46    end.
47
48
49
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51
52init(Parent) ->
53    process_flag(priority, high),
54    try register(?NAME, self()) of
55        true ->
56            global:sync(),
57            MonSettings = [
58                           busy_port,
59                           busy_dist_port,
60                           {long_gc, 1000},
61                           {long_schedule, 1000},
62                           {large_heap, 8*1024*1024} % 8 MB
63                          ],
64            erlang:system_monitor(self(), MonSettings),
65            ?GSM:log({erlang:timestamp(), starting}),
66            proc_lib:init_ack(Parent, {ok, self()}),
67            loop(#{parent => Parent})
68    catch
69        _:_:_ ->
70            ?GSM:log({erlang:timestamp(), already_started}),
71            proc_lib:init_ack(Parent, {error, already_started}),
72            exit(normal)
73    end.
74
75
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78loop(State) ->
79    receive
80        {monitor, Pid, Tag, Info} ->
81            ?GSM:log({Pid, erlang:timestamp(), Tag, Info}),
82            loop(State);
83
84        {?MODULE, From, stop} ->
85            ?GSM:log({erlang:timestamp(), stopping}),
86            From ! {?MODULE, self(), stop},
87            exit(normal);
88
89        _ ->
90            loop(State)
91    end.
92
93
94
95