summaryrefslogtreecommitdiff
path: root/src/file_handle_cache_stats.erl
blob: b1fbb3f4edd7a393f4b4cef144e235ca543835e6 (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
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (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.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2014 GoPivotal, Inc.  All rights reserved.
%%

-module(file_handle_cache_stats).

%% stats about read / write operations that go through the fhc.

-export([init/0, update/3, update/2, update/1, get/0]).

-define(TABLE, ?MODULE).

init() ->
    ets:new(?TABLE, [public, named_table]),
    [ets:insert(?TABLE, {{Op, Counter}, 0}) || Op      <- [read, write],
                                               Counter <- [count, bytes, time]],
    [ets:insert(?TABLE, {{Op, Counter}, 0}) || Op      <- [sync, seek],
                                               Counter <- [count, time]],
    [ets:insert(?TABLE, {{Op, Counter}, 0}) || Op      <- [reopen],
                                               Counter <- [count]].

update(Op, Bytes, Thunk) ->
    {Time, Res} = timer:tc(Thunk),
    ets:update_counter(?TABLE, {Op, count}, 1),
    ets:update_counter(?TABLE, {Op, bytes}, Bytes),
    ets:update_counter(?TABLE, {Op, time}, Time),
    Res.

update(Op, Thunk) ->
    {Time, Res} = timer:tc(Thunk),
    ets:update_counter(?TABLE, {Op, count}, 1),
    ets:update_counter(?TABLE, {Op, time}, Time),
    Res.

update(Op) ->
    ets:update_counter(?TABLE, {Op, count}, 1),
    ok.

get() ->
    lists:sort(ets:tab2list(?TABLE)).