-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
memsup.erl
1086 lines (942 loc) · 36.5 KB
/
memsup.erl
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2024. 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(memsup).
-moduledoc """
A Memory Supervisor Process
`memsup` is a process which supervises the memory usage for the system and for
individual processes. It is part of the OS_Mon application, see
[os_mon](os_mon_app.md). Available for Unix and Windows.
Periodically performs a memory check:
- If more than a certain amount of available system memory is allocated, as
reported by the underlying operating system, the alarm
`{system_memory_high_watermark, []}` is set.
- If any Erlang process `Pid` in the system has allocated more than a certain
amount of total system memory, the alarm
`{process_memory_high_watermark, Pid}` is set.
Alarms are reported to the SASL alarm handler, see `m:alarm_handler`. To set an
alarm, `alarm_handler:set_alarm(Alarm)` is called where `Alarm` is either of the
alarms specified above.
The alarms are cleared automatically when the alarm cause is no longer valid.
The function [get_memory_data()](`get_memory_data/0`) can be used to retrieve
the result of the latest periodic memory check.
There is also a interface to system dependent memory data,
[get_system_memory_data()](`get_system_memory_data/0`). The result is highly
dependent on the underlying operating system and the interface is targeted
primarily for systems without virtual memory. However, the output on other
systems is still valid, although sparse.
A call to `get_system_memory_data/0` is more costly than a call to
`get_memory_data/0` as data is collected synchronously when this function is
called.
The total system memory reported under UNIX is the number of physical pages of
memory times the page size, and the available memory is the number of available
physical pages times the page size. This is a reasonable measure as swapping
should be avoided anyway, but the task of defining total memory and available
memory is difficult because of virtual memory and swapping.
## Configuration
The following configuration parameters can be used to change the default values
for time intervals and thresholds:
- **`memory_check_interval = int()>0`** - The time interval, in minutes, for the
periodic memory check. The default is one minute.
- **`system_memory_high_watermark = float()`** - The threshold, as percentage of
system memory, for how much system memory can be allocated before the
corresponding alarm is set. The default is 0.80 (80%).
- **`process_memory_high_watermark = float()`** - The threshold, as percentage
of system memory, for how much system memory can be allocated by one Erlang
process before the corresponding alarm is set. The default is 0.05 (5%).
- **`memsup_helper_timeout = int()>0`** - A timeout, in seconds, for how long
the `memsup` process should wait for a result from a memory check. If the
timeout expires, a warning message `"OS_MON (memsup) timeout"` is issued via
`error_logger` and any pending, synchronous client calls will return a dummy
value. Normally, this situation should not occur. There have been cases on
Linux, however, where the pseudo file from which system data is read is
temporarily unavailable when the system is heavily loaded.
The default is 30 seconds.
- **`memsup_system_only = bool()`** - Specifies whether the `memsup` process
should only check system memory usage (`true`) or not. The default is `false`,
meaning that information regarding both system memory usage and Erlang process
memory usage is collected.
It is recommended to set this parameter to `false` on systems with many
concurrent processes, as each process memory check makes a traversal of the
entire list of processes.
See [config](`e:kernel:config.md`) for information about how to change the
value of configuration parameters.
## See Also
`m:alarm_handler`, [os_mon](os_mon_app.md)
""".
-behaviour(gen_server).
%% API
-export([start_link/0]). % for supervisor
-export([get_memory_data/0, get_system_memory_data/0,
get_check_interval/0, set_check_interval/1,
get_procmem_high_watermark/0, set_procmem_high_watermark/1,
get_sysmem_high_watermark/0, set_sysmem_high_watermark/1,
get_helper_timeout/0, set_helper_timeout/1,
get_os_wordsize/0]).
-export([dummy_reply/1, param_type/2, param_default/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2]).
%% Other exports
-export([format_status/1]).
-include("memsup.hrl").
-record(state,
{os, % {OSfamily,OSname} | OSfamily
port_mode, % bool()
mem_usage, % undefined | {Alloc, Total}
worst_mem_user, % undefined | {Pid, Alloc}
sys_only, % bool() memsup_system_only
timeout, % int() memory_check_interval, ms
helper_timeout, % int() memsup_helper_timeout, ms
sys_mem_watermark, % float() system_memory_high_watermark, %
proc_mem_watermark, % float() process_memory_high_watermark, %
pid, % undefined | pid()
wd_timer, % undefined | TimerRef
ext_wd_timer, % undefined | TimerRef
pending = [], % [reg | {reg,From} | {ext,From}]
ext_pending = [] % [{ext,From}]
}).
-define(EXT_MEM_MAP, memsup_ext_memory_type_map__).
%%----------------------------------------------------------------------
%% API
%%----------------------------------------------------------------------
-doc false.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-doc """
Returns the wordsize of the current running operating system.
""".
-spec get_os_wordsize() -> Wordsize when Wordsize :: 32 | 64 | unsupported_os.
get_os_wordsize() ->
os_mon:call(memsup, get_os_wordsize, infinity).
-doc """
Returns the result of the latest memory check, where `Total` is the total memory
size and `Allocated` the allocated memory size, in bytes.
`Worst` is the pid and number of allocated bytes of the largest Erlang process
on the node. If `memsup` should not collect process data, that is if the
configuration parameter `memsup_system_only` was set to `true`, `Worst` is
`undefined`.
The function is normally asynchronous in the sense that it does not invoke a
memory check, but returns the latest available value. The one exception if is
the function is called before a first memory check is finished, in which case it
does not return a value until the memory check is finished.
Returns `{0,0,{pid(),0}}` or `{0,0,undefined}` if `memsup` is not available, or
if all memory checks so far have timed out.
""".
-spec get_memory_data() -> {Total, Allocated, Worst} when
Total :: integer(),
Allocated :: integer(),
Worst :: {Pid, PidAllocated} | undefined,
Pid :: pid(),
PidAllocated :: integer().
get_memory_data() ->
os_mon:call(memsup, get_memory_data, infinity).
-doc """
Invokes a memory check and returns the resulting, system dependent, data as a
list of tagged tuples, where `Tag` currently can be one of the following:
- **`total_memory`** - The total amount of memory available to the Erlang
emulator, allocated and free. May or may not be equal to the amount of memory
configured in the system.
- **`available_memory`** - Informs about the amount memory that is available for
increased usage if there is an increased memory need. This value is not based
on a calculation of the other provided values and should give a better value
of the amount of memory that actually is available than calculating a value
based on the other values reported. This value is currently only present on
newer Linux kernels. If this value is not available on Linux, you can use the
sum of `cached_memory`, `buffered_memory`, and `free_memory` as an
approximation.
- **`free_memory`** - The amount of free memory available to the Erlang emulator
for allocation.
- **`system_total_memory`** - The amount of memory available to the whole
operating system. This may well be equal to `total_memory` but not
necessarily.
- **`buffered_memory`** - The amount of memory the system uses for temporary
storing raw disk blocks.
- **`cached_memory`** - The amount of memory the system uses for cached files
read from disk. On Linux, also memory marked as reclaimable in the kernel slab
allocator will be added to this value.
- **`total_swap`** - The amount of total amount of memory the system has
available for disk swap.
- **`free_swap`** - The amount of memory the system has available for disk swap.
> #### Note {: .info }
>
> Note that new tagged tuples may be introduced in the result at any time
> without prior notice
Note that the order of the tuples in the resulting list is undefined and may
change at any time.
All memory sizes are presented as number of _bytes_.
Returns the empty list [] if `memsup` is not available, or if the memory check
times out.
""".
-spec get_system_memory_data() -> MemDataList when
MemDataList :: [{Tag, Size}],
Tag :: atom(),
Size :: integer().
get_system_memory_data() ->
os_mon:call(memsup, get_system_memory_data, infinity).
-doc """
Returns the time interval, in milliseconds, for the periodic memory check.
""".
-spec get_check_interval() -> Milliseconds :: integer().
get_check_interval() ->
os_mon:call(memsup, get_check_interval, infinity).
-doc """
Changes the time interval, given in minutes, for the periodic memory check.
The change will take effect after the next memory check and is non-persistent.
That is, in case of a process restart, this value is forgotten and the default
value will be used. See [Configuration](`m:memsup#module-configuration`).
""".
-spec set_check_interval(Minutes :: non_neg_integer()) -> ok.
set_check_interval(Minutes) ->
case param_type(memory_check_interval, Minutes) of
true ->
MS = minutes_to_ms(Minutes), % for backwards compatibility
os_mon:call(memsup, {set_check_interval, MS}, infinity);
false ->
erlang:error(badarg)
end.
-doc """
Returns the threshold, in percent, for process memory allocation.
""".
-spec get_procmem_high_watermark() -> integer().
get_procmem_high_watermark() ->
os_mon:call(memsup, get_procmem_high_watermark, infinity).
-doc """
Changes the threshold, given as a float, for process memory allocation.
The change will take effect during the next periodic memory check and is
non-persistent. That is, in case of a process restart, this value is forgotten
and the default value will be used. See [Configuration](`m:memsup#module-configuration`).
""".
-spec set_procmem_high_watermark(Float :: term()) -> ok.
set_procmem_high_watermark(Float) ->
case param_type(process_memory_high_watermark, Float) of
true ->
os_mon:call(memsup, {set_procmem_high_watermark, Float},
infinity);
false ->
erlang:error(badarg)
end.
-doc """
Returns the threshold, in percent, for system memory allocation.
""".
-spec get_sysmem_high_watermark() -> integer().
get_sysmem_high_watermark() ->
os_mon:call(memsup, get_sysmem_high_watermark, infinity).
-doc """
Changes the threshold, given as a float, for system memory allocation.
The change will take effect during the next periodic memory check and is
non-persistent. That is, in case of a process restart, this value is forgotten
and the default value will be used. See [Configuration](`m:memsup#module-configuration`)
above.
""".
-spec set_sysmem_high_watermark(Float :: term()) -> ok.
set_sysmem_high_watermark(Float) ->
case param_type(system_memory_high_watermark, Float) of
true ->
os_mon:call(memsup, {set_sysmem_high_watermark, Float},
infinity);
false ->
erlang:error(badarg)
end.
-doc """
Returns the timeout value, in seconds, for memory checks.
""".
-spec get_helper_timeout() -> Seconds :: integer().
get_helper_timeout() ->
os_mon:call(memsup, get_helper_timeout, infinity).
-doc """
Changes the timeout value, given in seconds, for memory checks.
The change will take effect for the next memory check and is non-persistent.
That is, in the case of a process restart, this value is forgotten and the
default value will be used. See [Configuration](`m:memsup#module-configuration`) above.
""".
-spec set_helper_timeout(Seconds :: non_neg_integer()) -> ok.
set_helper_timeout(Seconds) ->
case param_type(memsup_helper_timeout, Seconds) of
true ->
os_mon:call(memsup, {set_helper_timeout, Seconds}, infinity);
false ->
erlang:error(badarg)
end.
-doc false.
dummy_reply(get_memory_data) ->
dummy_reply(get_memory_data,
os_mon:get_env(memsup, memsup_system_only));
dummy_reply(get_system_memory_data) ->
[];
dummy_reply(get_os_wordsize) ->
0;
dummy_reply(get_check_interval) ->
minutes_to_ms(os_mon:get_env(memsup, memory_check_interval));
dummy_reply({set_check_interval, _}) ->
ok;
dummy_reply(get_procmem_high_watermark) ->
trunc(100 * os_mon:get_env(memsup, process_memory_high_watermark));
dummy_reply({set_procmem_high_watermark, _}) ->
ok;
dummy_reply(get_sysmem_high_watermark) ->
trunc(100 * os_mon:get_env(memsup, system_memory_high_watermark));
dummy_reply({set_sysmem_high_watermark, _}) ->
ok;
dummy_reply(get_helper_timeout) ->
os_mon:get_env(memsup, memsup_helper_timeout);
dummy_reply({set_helper_timeout, _}) ->
ok.
dummy_reply(get_memory_data, true) ->
{0,0,undefined};
dummy_reply(get_memory_data, false) ->
{0,0,{self(),0}}.
-doc false.
param_type(memsup_system_only, Val) when Val==true; Val==false -> true;
param_type(memory_check_interval, Val) when is_integer(Val),
Val>0 -> true;
param_type(memsup_helper_timeout, Val) when is_integer(Val),
Val>0 -> true;
param_type(system_memory_high_watermark, Val) when is_number(Val),
0=<Val,
Val=<1 -> true;
param_type(process_memory_high_watermark, Val) when is_number(Val),
0=<Val,
Val=<1 -> true;
param_type(_Param, _Val) -> false.
-doc false.
param_default(memsup_system_only) -> false;
param_default(memory_check_interval) -> 1;
param_default(memsup_helper_timeout) -> 30;
param_default(system_memory_high_watermark) -> 0.80;
param_default(process_memory_high_watermark) -> 0.05.
%%----------------------------------------------------------------------
%% gen_server callbacks
%%----------------------------------------------------------------------
-doc false.
init([]) ->
process_flag(trap_exit, true),
process_flag(priority, low),
OS = os:type(),
PortMode = case OS of
{unix, darwin} -> true;
{unix, freebsd} -> false;
{unix, dragonfly} -> false;
% Linux supports this.
{unix, linux} -> true;
{unix, openbsd} -> true;
{unix, netbsd} -> true;
{unix, irix64} -> true;
{unix, irix} -> true;
{unix, sunos} -> true;
{win32, _OSname} -> false;
_ ->
exit({unsupported_os, OS})
end,
Pid = if
PortMode ->
spawn_link(fun() -> port_init() end);
not PortMode ->
undefined
end,
%% Read the values of some configuration parameters
SysOnly = os_mon:get_env(memsup, memsup_system_only),
Timeout = os_mon:get_env(memsup, memory_check_interval),
HelperTimeout = os_mon:get_env(memsup, memsup_helper_timeout),
SysMem = os_mon:get_env(memsup, system_memory_high_watermark),
ProcMem = os_mon:get_env(memsup, process_memory_high_watermark),
persistent_term:put(?EXT_MEM_MAP,
#{?MEM_SYSTEM_TOTAL => system_total_memory,
?MEM_TOTAL => total_memory,
?MEM_AVAIL => available_memory,
?MEM_FREE => free_memory,
?MEM_BUFFERS => buffered_memory,
?MEM_CACHED => cached_memory,
%% When included cached_x is same as cached...
?MEM_CACHED_X => cached_memory,
?MEM_SHARED => shared_memory,
?MEM_LARGEST_FREE => largest_free,
?MEM_NUMBER_OF_FREE => number_of_free,
?SWAP_TOTAL => total_swap,
?SWAP_FREE => free_swap}),
%% Initiate first data collection
self() ! time_to_collect,
{ok, #state{os=OS, port_mode=PortMode,
sys_only = SysOnly,
timeout = minutes_to_ms(Timeout),
helper_timeout = sec_to_ms(HelperTimeout),
sys_mem_watermark = SysMem,
proc_mem_watermark = ProcMem,
pid=Pid}}.
-doc false.
handle_call(get_os_wordsize, _From, State) ->
Wordsize = get_os_wordsize(State#state.os),
{reply, Wordsize, State};
handle_call(get_memory_data, From, State) ->
%% Return result of latest memory check
case State#state.mem_usage of
{Alloc, Total} ->
Worst = State#state.worst_mem_user,
{reply, {Total, Alloc, Worst}, State};
%% Special case: get_memory_data called before any memory data
%% has been collected
undefined ->
case State#state.wd_timer of
undefined ->
WDTimer = erlang:send_after(State#state.timeout,
self(),
reg_collection_timeout),
Pending = [{reg,From}],
if
State#state.port_mode ->
State#state.pid ! {self(), collect_sys},
{noreply, State#state{wd_timer=WDTimer,
pending=Pending}};
true ->
OS = State#state.os,
Self = self(),
Pid = spawn_link(fun() ->
MU = get_memory_usage(OS),
Self ! {collected_sys,MU}
end),
{noreply, State#state{pid=Pid,
wd_timer=WDTimer,
pending=Pending}}
end;
_TimerRef ->
Pending = [{reg,From} | State#state.pending],
{noreply, State#state{pending=Pending}}
end
end;
handle_call(get_system_memory_data,From,#state{port_mode=true}=State) ->
%% When using a port, the extensive memory collection is slightly
%% different than a regular one
case State#state.ext_wd_timer of
undefined ->
WDTimer = erlang:send_after(State#state.helper_timeout,
self(),
ext_collection_timeout),
State#state.pid ! {self(), collect_ext_sys},
{noreply, State#state{ext_wd_timer=WDTimer,
ext_pending=[{ext,From}]}};
_TimerRef ->
Pending = [{ext,From} | State#state.ext_pending],
{noreply, State#state{ext_pending=Pending}}
end;
handle_call(get_system_memory_data, From, State) ->
%% When not using a port, the regular memory collection is used
%% for extensive memory data as well
case State#state.wd_timer of
undefined ->
WDTimer = erlang:send_after(State#state.helper_timeout,
self(),
reg_collection_timeout),
OS = State#state.os,
Self = self(),
Pid = spawn_link(fun() ->
MemUsage = get_memory_usage(OS),
Self ! {collected_sys, MemUsage}
end),
{noreply, State#state{pid=Pid, wd_timer=WDTimer,
pending=[{ext,From}]}};
_TimerRef ->
Pending = [{ext,From} | State#state.pending],
{noreply, State#state{pending=Pending}}
end;
handle_call(get_check_interval, _From, State) ->
{reply, State#state.timeout, State};
handle_call({set_check_interval, MS}, _From, State) ->
{reply, ok, State#state{timeout=MS}};
handle_call(get_procmem_high_watermark, _From, State) ->
{reply, trunc(100 * State#state.proc_mem_watermark), State};
handle_call({set_procmem_high_watermark, Float}, _From, State) ->
{reply, ok, State#state{proc_mem_watermark=Float}};
handle_call(get_sysmem_high_watermark, _From, State) ->
{reply, trunc(100 * State#state.sys_mem_watermark), State};
handle_call({set_sysmem_high_watermark, Float}, _From, State) ->
{reply, ok, State#state{sys_mem_watermark=Float}};
handle_call(get_helper_timeout, _From, State) ->
{reply, ms_to_sec(State#state.helper_timeout), State};
handle_call({set_helper_timeout, Seconds}, _From, State) ->
{reply, ok, State#state{helper_timeout=sec_to_ms(Seconds)}};
%% The following are only for test purposes (whitebox testing).
handle_call({set_sys_hw, HW}, _From, State) ->
{reply, ok, State#state{sys_mem_watermark=HW}};
handle_call({set_pid_hw, HW}, _From, State) ->
{reply, ok, State#state{proc_mem_watermark=HW}};
handle_call(get_state, _From, State) ->
{reply, State, State}.
-doc false.
handle_cast(_Msg, State) ->
{noreply, State}.
%% It's time to check memory
-doc false.
handle_info(time_to_collect, State) ->
case State#state.wd_timer of
undefined ->
WDTimer = erlang:send_after(State#state.helper_timeout,
self(),
reg_collection_timeout),
if
State#state.port_mode ->
State#state.pid ! {self(), collect_sys},
{noreply, State#state{wd_timer=WDTimer,
pending=[reg]}};
true ->
OS = State#state.os,
Self = self(),
Pid = spawn_link(fun() ->
MU = get_memory_usage(OS),
Self ! {collected_sys,MU}
end),
{noreply, State#state{pid=Pid, wd_timer=WDTimer,
pending=[reg]}}
end;
_TimerRef ->
{noreply, State#state{pending=[reg|State#state.pending]}}
end;
%% Memory data collected
handle_info({collected_sys, {Alloc,Total}}, State) ->
%% Cancel watchdog timer (and as a security measure,
%% also flush any reg_collection_timeout message)
TimeSpent = case erlang:cancel_timer(State#state.wd_timer) of
false ->
State#state.helper_timeout;
TimeLeft ->
State#state.helper_timeout-TimeLeft
end,
flush(reg_collection_timeout),
%% First check if this is the result of a periodic memory check
%% and update alarms and State if this is the case
State2 =
case lists:member(reg, State#state.pending) of
true ->
%% Check if system alarm should be set/cleared
if
Alloc > State#state.sys_mem_watermark*Total ->
set_alarm(system_memory_high_watermark, []);
true ->
clear_alarm(system_memory_high_watermark)
end,
%% Check if process data should be collected
case State#state.sys_only of
false ->
{Pid, Bytes} = get_worst_memory_user(),
Threshold= State#state.proc_mem_watermark*Total,
%% Check if process alarm should be set/cleared
if
Bytes > Threshold ->
set_alarm(process_memory_high_watermark,
Pid);
true ->
clear_alarm(process_memory_high_watermark)
end,
State#state{mem_usage={Alloc, Total},
worst_mem_user={Pid, Bytes}};
true ->
State#state{mem_usage={Alloc, Total}}
end;
false ->
State
end,
%% Then send a reply to all waiting clients, in preserved time order
Worst = State2#state.worst_mem_user,
SysMemUsage = get_ext_memory_usage(State2#state.os, {Alloc,Total}),
reply(State2#state.pending, {Total,Alloc,Worst}, SysMemUsage),
%% Last, if this was a periodic check, start a timer for the next
%% one. New timeout = interval-time spent collecting,
_ = case lists:member(reg, State#state.pending) of
true ->
Time = case State2#state.timeout - TimeSpent of
MS when MS<0 ->
0;
MS ->
MS
end,
erlang:send_after(Time, self(), time_to_collect);
false ->
ignore
end,
{noreply, State2#state{wd_timer=undefined, pending=[]}};
handle_info({'EXIT', Pid, normal}, State) when is_pid(Pid) ->
%% Temporary pid terminating when job is done
{noreply, State};
%% Timeout during data collection
handle_info(reg_collection_timeout, State) ->
%% Cancel memory collection (and as a security measure,
%% also flush any collected_sys message)
if
State#state.port_mode -> State#state.pid ! cancel;
true -> exit(State#state.pid, cancel)
end,
flush(collected_sys),
%% Issue a warning message
Str = "OS_MON (memsup) timeout, no data collected~n",
error_logger:warning_msg(Str),
%% Send a dummy reply to all waiting clients, preserving time order
reply(State#state.pending,
dummy_reply(get_memory_data, State#state.sys_only),
dummy_reply(get_system_memory_data)),
%% If it is a periodic check which has timed out, start a timer for
%% the next one
%% New timeout = interval-helper timeout
_ = case lists:member(reg, State#state.pending) of
true ->
Time =
case State#state.timeout-State#state.helper_timeout of
MS when MS<0 -> 0;
MS -> MS
end,
erlang:send_after(Time, self(), time_to_collect);
false ->
ignore
end,
{noreply, State#state{wd_timer=undefined, pending=[]}};
handle_info({'EXIT', Pid, cancel}, State) when is_pid(Pid) ->
%% Temporary pid terminating as ordered
{noreply, State};
%% Extensive memory data collected (port_mode==true only)
handle_info({collected_ext_sys, SysMemUsage}, State) ->
%% Cancel watchdog timer (and as a security mearure,
%% also flush any ext_collection_timeout message)
ok = erlang:cancel_timer(State#state.ext_wd_timer, [{async,true}]),
flush(ext_collection_timeout),
%% Send the reply to all waiting clients, preserving time order
reply(State#state.ext_pending, undef, SysMemUsage),
{noreply, State#state{ext_wd_timer=undefined, ext_pending=[]}};
%% Timeout during ext memory data collection (port_mode==true only)
handle_info(ext_collection_timeout, State) ->
%% Cancel memory collection (and as a security measure,
%% also flush any collected_ext_sys message)
State#state.pid ! ext_cancel,
flush(collected_ext_sys),
%% Issue a warning message
Str = "OS_MON (memsup) timeout, no data collected~n",
error_logger:warning_msg(Str),
%% Send a dummy reply to all waiting clients, preserving time order
SysMemUsage = dummy_reply(get_system_memory_data),
reply(State#state.ext_pending, undef, SysMemUsage),
{noreply, State#state{ext_wd_timer=undefined, ext_pending=[]}};
%% Error in data collecting (port connected or temporary) process
handle_info({'EXIT', Pid, Reason}, State) when is_pid(Pid) ->
{stop, Reason, State};
handle_info(_Info, State) ->
{noreply, State}.
-doc false.
terminate(_Reason, State) ->
if
State#state.port_mode -> State#state.pid ! close;
true -> ok
end,
clear_alarms(),
ok.
%%----------------------------------------------------------------------
%% Other exports
%%----------------------------------------------------------------------
-doc false.
format_status(#{ state := #state{timeout=Timeout, mem_usage=MemUsage,
worst_mem_user=WorstMemUser} } = Status) ->
{Allocated, Total} = MemUsage,
WorstMemFormat = case WorstMemUser of
{Pid, Mem} ->
[{"Pid", Pid}, {"Memory", Mem}];
undefined ->
undefined
end,
Status#{ state :=
[{"Timeout", Timeout},
{"Memory Usage", [{"Allocated", Allocated},
{"Total", Total}]},
{"Worst Memory User", WorstMemFormat}] }.
%%----------------------------------------------------------------------
%% Internal functions
%%----------------------------------------------------------------------
%%-- Fetching kernel bit support ---------------------------------------
get_os_wordsize({unix, sunos}) ->
String = clean_string(os:cmd("isainfo -b")),
erlang:list_to_integer(String);
get_os_wordsize({unix, irix64}) -> 64;
get_os_wordsize({unix, irix}) -> 32;
get_os_wordsize({unix, linux}) -> get_os_wordsize_with_uname();
get_os_wordsize({unix, darwin}) -> get_os_wordsize_with_uname();
get_os_wordsize({unix, netbsd}) -> get_os_wordsize_with_uname();
get_os_wordsize({unix, freebsd}) -> get_os_wordsize_with_uname();
get_os_wordsize({unix, dragonfly}) -> get_os_wordsize_with_uname();
get_os_wordsize({unix, openbsd}) -> get_os_wordsize_with_uname();
get_os_wordsize(_) -> unsupported_os.
get_os_wordsize_with_uname() ->
String = clean_string(os:cmd("uname -m")),
case String of
"x86_64" -> 64;
"sparc64" -> 64;
"amd64" -> 64;
"ppc64" -> 64;
"ppc64le" -> 64;
"s390x" -> 64;
"aarch64" -> 64; %Linux
"arm64" -> 64; %macOS
_ -> 32
end.
clean_string(String) -> lists:flatten(string:lexemes(String,[[$\r,$\n]|"\n\t "])).
%%--Replying to pending clients-----------------------------------------
reply(Pending, MemUsage, SysMemUsage) ->
lists:foreach(fun(reg) ->
ignore;
({reg, From}) ->
gen_server:reply(From, MemUsage);
({ext, From}) ->
gen_server:reply(From, SysMemUsage)
end,
lists:reverse(Pending)).
%%--Collect memory data, no port----------------------------------------
%% get_memory_usage(OS) -> {Alloc, Total}
%% FreeBSD: Look in /usr/include/sys/vmmeter.h for the format of struct
%% vmmeter
get_memory_usage({unix,OSname}) when OSname == freebsd; OSname == dragonfly ->
PageSize = freebsd_sysctl("vm.stats.vm.v_page_size"),
PageCount = freebsd_sysctl("vm.stats.vm.v_page_count"),
FreeCount = freebsd_sysctl("vm.stats.vm.v_free_count"),
NMemUsed = (PageCount - FreeCount) * PageSize,
NMemTotal = PageCount * PageSize,
{NMemUsed, NMemTotal};
%% Win32: Find out how much memory is in use by asking
%% the os_mon_sysinfo process.
get_memory_usage({win32,_OSname}) ->
[Result|_] = os_mon_sysinfo:get_mem_info(),
{ok, [_MemLoad, TotPhys, AvailPhys,
_TotPage, _AvailPage, _TotV, _AvailV], _RestStr} =
io_lib:fread("~d~d~d~d~d~d~d", Result),
{TotPhys-AvailPhys, TotPhys}.
freebsd_sysctl(Def) ->
list_to_integer(os:cmd("/sbin/sysctl -n " ++ Def) -- "\n").
%% get_ext_memory_usage(OS, {Alloc, Total}) -> [{Tag, Bytes}]
get_ext_memory_usage(OS, {Alloc, Total}) ->
case OS of
{win32, _} ->
[{total_memory, Total}, {free_memory, Total-Alloc},
{system_total_memory, Total}];
{unix, linux} ->
[{total_memory, Total}, {free_memory, Total-Alloc},
%% corr. unless setrlimit() set
{system_total_memory, Total}];
{unix, freebsd} ->
[{total_memory, Total}, {free_memory, Total-Alloc},
{system_total_memory, Total}];
{unix, dragonfly} ->
[{total_memory, Total}, {free_memory, Total-Alloc},
{system_total_memory, Total}];
_ -> % OSs using a port
dummy % not sent anyway
end.
%%--Collect memory data, using port-------------------------------------
port_init() ->
process_flag(trap_exit, true),
Port = start_portprogram(),
port_idle(Port).
start_portprogram() ->
os_mon:open_port("memsup",[{packet,1}]).
port_shutdown(Port) ->
Port ! {self(), {command, [?EXIT]}},
Port ! {self(), close}.
%% The connected process loops are a bit awkward (several different
%% functions doing almost the same thing) as
%% a) strategies for receiving regular memory data and extensive
%% memory data are different
%% b) memory collection can be cancelled, in which case the process
%% should still wait for port response (which should come
%% eventually!) but not receive any requests or cancellations
%% meanwhile to prevent getting out of synch.
port_idle(Port) ->
receive
{Memsup, collect_sys} ->
Port ! {self(), {command, [?SHOW_MEM]}},
get_memory_usage(Port, undefined, Memsup);
{Memsup, collect_ext_sys} ->
Port ! {self(), {command, [?SHOW_SYSTEM_MEM]}},
get_ext_memory_usage(Port, #{}, Memsup);
cancel ->
%% Received after reply already has been delivered...
port_idle(Port);
ext_cancel ->
%% Received after reply already has been delivered...
port_idle(Port);
close ->
port_shutdown(Port);
{Port, {data, Data}} ->
exit({port_error, Data});
{'EXIT', Port, Reason} ->
exit({port_died, Reason});
{'EXIT', _Memsup, _Reason} ->
port_shutdown(Port)
end.
get_memory_usage(Port, Alloc, Memsup) ->
receive
{Port, {data, Data}} when Alloc==undefined ->
get_memory_usage(Port, erlang:list_to_integer(Data, 16), Memsup);
{Port, {data, Data}} ->
Total = erlang:list_to_integer(Data, 16),
Memsup ! {collected_sys, {Alloc, Total}},
port_idle(Port);
cancel ->
get_memory_usage_cancelled(Port, Alloc);
close ->
port_shutdown(Port);
{'EXIT', Port, Reason} ->
exit({port_died, Reason});
{'EXIT', _Memsup, _Reason} ->
port_shutdown(Port)
end.
get_memory_usage_cancelled(Port, Alloc) ->
receive
{Port, {data, _Data}} when Alloc==undefined ->
get_memory_usage_cancelled(Port, 0);
{Port, {data, _Data}} ->
port_idle(Port);
close ->
port_shutdown(Port);
{'EXIT', Port, Reason} ->
exit({port_died, Reason});
{'EXIT', _Memsup, _Reason} ->
port_shutdown(Port)
end.
tag2atag(Port, Tag) ->
Tab = persistent_term:get(?EXT_MEM_MAP),
try
maps:get(Tag, Tab)
catch
error:_ ->
exit({memsup_port_error, {Port,[Tag]}})
end.
get_ext_memory_usage(Port, Accum, Memsup) ->
receive
{Port, {data, [?SHOW_SYSTEM_MEM_END]}} ->
Memsup ! {collected_ext_sys, maps:to_list(Accum)},
port_idle(Port);
{Port, {data, [Tag]}} ->
get_ext_memory_usage(tag2atag(Port, Tag), Port, Accum,
Memsup);
ext_cancel ->
get_ext_memory_usage_cancelled(Port);
close ->
port_shutdown(Port);
{'EXIT', Port, Reason} ->
exit({port_died, Reason});
{'EXIT', _Memsup, _Reason} ->
port_shutdown(Port)
end.
get_ext_memory_usage_cancelled(Port) ->
receive
{Port, {data, [?SHOW_SYSTEM_MEM_END]}} ->
port_idle(Port);
{Port, {data, [Tag]}} ->
get_ext_memory_usage_cancelled(tag2atag(Port, Tag),
Port);
close ->
port_shutdown(Port);
{'EXIT', Port, Reason} ->
exit({port_died, Reason});
{'EXIT', _Memsup, _Reason} ->
port_shutdown(Port)
end.
get_ext_memory_usage(ATag, Port, Accum0, Memsup) ->
receive
{Port, {data, Data}} ->
Value = erlang:list_to_integer(Data, 16),
Accum = case maps:get(ATag, Accum0, undefined) of
undefined ->
maps:put(ATag, Value, Accum0);
PrevValue ->
maps:put(ATag, Value + PrevValue, Accum0)
end,
get_ext_memory_usage(Port, Accum, Memsup);
cancel ->
get_ext_memory_usage_cancelled(ATag, Port);
close ->
port_shutdown(Port);
{'EXIT', Port, Reason} ->