summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_stats_collector.erl
blob: 327a37c1334a4728bd7b9593a0e5398cf920f30b (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
% 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.

% todo
% - remove existance check on increment(), decrement() and record(). have
%   modules initialize counters on startup.

-module(couch_stats_collector).

-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
        terminate/2, code_change/3]).


-export([start/0, stop/0, get/1,
        increment/1, decrement/1,
        track_process_count/1, track_process_count/2,
        record/2, clear/1,
        all/0, all/1]).

-record(state, {}).

-define(ABSOLUTE_VALUE_COUNTER_TABLE, abs_table).
-define(HIT_COUNTER_TABLE, hit_table).


% PUBLIC API

start() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

stop() ->
    gen_server:call(?MODULE, stop).

get(Key) ->
    case ets:lookup(?HIT_COUNTER_TABLE, Key) of
        [] ->
            case ets:lookup(?ABSOLUTE_VALUE_COUNTER_TABLE, Key) of
                [] ->
                    0;
                Result2 -> extract_value_from_ets_result(Key, Result2)
            end;
        [{_,Result1}] -> Result1
    end.

increment({Module, Key}) when is_integer(Key) ->
    increment({Module, list_to_atom(integer_to_list(Key))});
increment(Key) ->
    case catch ets:update_counter(?HIT_COUNTER_TABLE, Key, 1) of
        {'EXIT', {badarg, _}} ->
            true = ets:insert(?HIT_COUNTER_TABLE, {Key, 1}),
            ok;
        _ -> ok
    end.

decrement(Key) ->
    case catch ets:update_counter(?HIT_COUNTER_TABLE, Key, -1) of
        {'EXIT', {badarg, _}} ->
            true = ets:insert(?HIT_COUNTER_TABLE, {Key, -1}),
            ok;
        _ -> ok
    end.

record(Key, Value) ->
    ets:insert(?ABSOLUTE_VALUE_COUNTER_TABLE, {Key, Value}).

clear(Key) ->
    true = ets:delete(?ABSOLUTE_VALUE_COUNTER_TABLE, Key).

all() ->
    lists:append(ets:tab2list(?HIT_COUNTER_TABLE),
        ets:tab2list(?ABSOLUTE_VALUE_COUNTER_TABLE)).

all(Type) ->
    case Type of
        incremental -> ets:tab2list(?HIT_COUNTER_TABLE);
        absolute -> ets:tab2list(?ABSOLUTE_VALUE_COUNTER_TABLE)
    end.

track_process_count(Stat) ->
    track_process_count(self(), Stat).

track_process_count(Pid, Stat) ->
    case (catch couch_stats_collector:increment(Stat)) of
    ok ->
        spawn(
            fun() ->
                erlang:monitor(process, Pid),
                receive {'DOWN', _, _, _, _} -> ok end,
                couch_stats_collector:decrement(Stat)
            end);
     _ -> ok
    end.


% GEN_SERVER


init(_) ->
    ets:new(?HIT_COUNTER_TABLE, [named_table, set, public]),
    ets:new(?ABSOLUTE_VALUE_COUNTER_TABLE, [named_table, duplicate_bag, public]),
    {ok, #state{}}.


handle_call(stop, _, State) ->
    {stop, normal, stopped, State}.


% PRIVATE API

extract_value_from_ets_result(_Key, Result) ->
    lists:map(fun({_, Value}) -> Value end, Result).


% Unused gen_server behaviour API functions that we need to declare.

%% @doc Unused
handle_cast(foo, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

%% @doc Unused
terminate(_Reason, _State) -> ok.

%% @doc Unused
code_change(_OldVersion, State, _Extra) -> {ok, State}.


%% Tests

-ifdef(TEST).
% Internal API unit tests go here


-endif.