aboutsummaryrefslogtreecommitdiffstats
path: root/erts/preloaded/src/counters.erl
blob: 67354f648dcfe2eb59ca8c57c5780c85ed2bf80f (plain) (blame)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2018. 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%
%%

%% Purpose : Main atomics API module.

-module(counters).

-export([new/2,
         get/2,
         add/3,
         sub/3,
         info/1]).

-export_type([counters_ref/0]).

-opaque counters_ref() :: {atomics, reference()} | {write_concurrency, reference()}.

-spec new(Size, Opts) -> counters_ref() when
      Size :: pos_integer(),
      Opts :: [Opt],
      Opt :: atomics | write_concurrency.
new(Size, [atomics]) ->
    {atomics, atomics:new(Size, [{signed, true}])};
new(Size, [write_concurrency]) ->
    {write_concurrency, erts_internal:counters_new(Size)};
new(Size, []) ->
    new(Size, [atomics]);
new(_, _) ->
    erlang:error(badarg).

-spec get(Ref, Ix) -> integer() when
      Ref  :: counters_ref(),
      Ix :: integer().
get({atomics,Ref}, Ix) ->
    atomics:get(Ref, Ix);
get({write_concurrency, Ref}, Ix) ->
    erts_internal:counters_get(Ref, Ix);
get(_, _) ->
    erlang:error(badarg).



-spec add(Ref, Ix, Incr) -> ok when
      Ref  :: counters_ref(),
      Ix :: integer(),
      Incr :: integer().
add({atomics, Ref}, Ix, Incr) ->
    atomics:add(Ref, Ix, Incr);
add({write_concurrency, Ref}, Ix, Incr) ->
    erts_internal:counters_add(Ref, Ix, Incr);
add(_, _, _) ->
    erlang:error(badarg).


-spec sub(Ref, Ix, Decr) -> ok when
      Ref  :: counters_ref(),
      Ix :: integer(),
      Decr :: integer().
sub(Ref, Ix, Decr) ->
    add(Ref, Ix, -Decr).

-spec info(Ref) -> Info when
      Ref  :: counters_ref(),
      Info :: #{'size':=Size, 'memory':=Memory},
      Size :: non_neg_integer(),
      Memory :: non_neg_integer().
info({atomics, Ref}) ->
    atomics:info(Ref);
info({write_concurrency, Ref}) ->
    erts_internal:counters_info(Ref);
info(_) ->
    erlang:error(badarg).