Generated on 2009-08-09 21:22:54 with etap 0.3.4.
| Name | Total lines | Lines of code | Total coverage | Code coverage | ||||
| couch_stats_collector | ?? | ?? | ?? |
|
....1 % Licensed under the Apache License, Version 2.0 (the "License"); you may not ....2 % use this file except in compliance with the License. You may obtain a copy of ....3 % the License at ....4 % ....5 % http://www.apache.org/licenses/LICENSE-2.0 ....6 % ....7 % Unless required by applicable law or agreed to in writing, software ....8 % distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ....9 % WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the ...10 % License for the specific language governing permissions and limitations under ...11 % the License. ...12 ...13 % todo ...14 % - remove existance check on increment(), decrement() and record(). have ...15 % modules initialize counters on startup. ...16 ...17 -module(couch_stats_collector). ...18 ...19 -behaviour(gen_server). ...20 ...21 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, ...22 terminate/2, code_change/3]). ...23 ...24 ...25 -export([start/0, stop/0, get/1, ...26 increment/1, decrement/1, ...27 track_process_count/1, track_process_count/2, ...28 record/2, clear/1, ...29 all/0, all/1]). ...30 ...31 -record(state, {}). ...32 ...33 -define(ABSOLUTE_VALUE_COUNTER_TABLE, abs_table). ...34 -define(HIT_COUNTER_TABLE, hit_table). ...35 ...36 ...37 % PUBLIC API ...38 ...39 start() -> ...40 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). ...41 ...42 stop() -> ...43 gen_server:call(?MODULE, stop). ...44 ...45 get(Key) -> ...46 case ets:lookup(?HIT_COUNTER_TABLE, Key) of ...47 [] -> ...48 case ets:lookup(?ABSOLUTE_VALUE_COUNTER_TABLE, Key) of ...49 [] -> ...50 0; ...51 Result2 -> extract_value_from_ets_result(Key, Result2) ...52 end; ...53 [{_,Result1}] -> Result1 ...54 end. ...55 ...56 increment({Module, Key}) when is_integer(Key) -> ...57 increment({Module, list_to_atom(integer_to_list(Key))}); ...58 increment(Key) -> ...59 case catch ets:update_counter(?HIT_COUNTER_TABLE, Key, 1) of ...60 {'EXIT', {badarg, _}} -> ...61 true = ets:insert(?HIT_COUNTER_TABLE, {Key, 1}), ...62 ok; ...63 _ -> ok ...64 end. ...65 ...66 decrement(Key) -> ...67 case catch ets:update_counter(?HIT_COUNTER_TABLE, Key, -1) of ...68 {'EXIT', {badarg, _}} -> ...69 true = ets:insert(?HIT_COUNTER_TABLE, {Key, -1}), ...70 ok; ...71 _ -> ok ...72 end. ...73 ...74 record(Key, Value) -> ...75 ets:insert(?ABSOLUTE_VALUE_COUNTER_TABLE, {Key, Value}). ...76 ...77 clear(Key) -> ...78 true = ets:delete(?ABSOLUTE_VALUE_COUNTER_TABLE, Key). ...79 ...80 all() -> ...81 lists:append(ets:tab2list(?HIT_COUNTER_TABLE), ...82 ets:tab2list(?ABSOLUTE_VALUE_COUNTER_TABLE)). ...83 ...84 all(Type) -> ...85 case Type of ...86 incremental -> ets:tab2list(?HIT_COUNTER_TABLE); ...87 absolute -> ets:tab2list(?ABSOLUTE_VALUE_COUNTER_TABLE) ...88 end. ...89 ...90 track_process_count(Stat) -> ...91 track_process_count(self(), Stat). ...92 ...93 track_process_count(Pid, Stat) -> ...94 case (catch couch_stats_collector:increment(Stat)) of ...95 ok -> ...96 spawn( ...97 fun() -> ...98 erlang:monitor(process, Pid), ...99 receive {'DOWN', _, _, _, _} -> ok end, ..100 couch_stats_collector:decrement(Stat) ..101 end); ..102 _ -> ok ..103 end. ..104 ..105 ..106 % GEN_SERVER ..107 ..108 ..109 init(_) -> ..110 ets:new(?HIT_COUNTER_TABLE, [named_table, set, public]), ..111 ets:new(?ABSOLUTE_VALUE_COUNTER_TABLE, [named_table, duplicate_bag, public]), ..112 {ok, #state{}}. ..113 ..114 ..115 handle_call(stop, _, State) -> ..116 {stop, normal, stopped, State}. ..117 ..118 ..119 % PRIVATE API ..120 ..121 extract_value_from_ets_result(_Key, Result) -> ..122 lists:map(fun({_, Value}) -> Value end, Result). ..123 ..124 ..125 % Unused gen_server behaviour API functions that we need to declare. ..126 ..127 %% @doc Unused ..128 handle_cast(foo, State) -> ..129 {noreply, State}. ..130 ..131 handle_info(_Info, State) -> ..132 {noreply, State}. ..133 ..134 %% @doc Unused ..135 terminate(_Reason, _State) -> ok. ..136 ..137 %% @doc Unused ..138 code_change(_OldVersion, State, _Extra) -> {ok, State}. ..139 ..140 ..141 %% Tests ..142 ..143 -ifdef(TEST). ..144 % Internal API unit tests go here ..145 ..146 ..147 -endif.
Generated using etap 0.3.4.