Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4eb93dd952
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 81 lines (64 sloc) 2.217 kb
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
%%==============================================================================
%% Copyright 2010 Erlang Solutions Ltd.
%%
%% 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.
%%==============================================================================

-module(jobs_sampler_mnesia).

-behaviour(jobs_sampler).

-export([init/2,
         sample/2,
         handle_msg/3,
         calc/2]).

-record(st, {levels = [],
subscriber}).



init(Name, Opts) ->
    Pid = spawn_link(fun() ->
mnesia:subscribe(system),
subscriber_loop(Name)
end),
    Levels = proplists:get_value(levels, Opts, default_levels()),
    {ok, #st{levels = Levels, subscriber = Pid}}.

default_levels() ->
    {seconds, [{0,1}, {30,2}, {45,3}, {60,4}]}.

handle_msg({mnesia_system_event, {mnesia,{dump_log,_}}}, _T, S) ->
    {log, true, S};
handle_msg({mnesia_system_event, {mnesia_tm, message_queue_len, _}}, _T, S) ->
    {log, true, S};
handle_msg(_, _T, S) ->
    {ignore, S}.

sample(_T, S) ->
    {is_overload(), S}.

calc(History, #st{levels = Levels} = S) ->
    {[{mnesia,jobs_sampler:calc(time, Levels, History)}], S}.


subscriber_loop(Name) ->
    receive
Msg ->
case jobs_sampler:tell_sampler(Name, Msg) of
ok ->
subscriber_loop(Name);
{error, _} ->
%% sampler likely removed
exit(normal)
end
    end.


is_overload() ->
    %% e.g: [{mnesia_tm,true},{mnesia_dump_log,false}]
    lists:keymember(true, 2, mnesia_overload_read()).

mnesia_overload_read() ->
    %% This function is not present in mnesia versions older than R14A
    case erlang:function_exported(mnesia_lib,overload_read,0) of
false ->
            [];
true ->
mnesia_lib:overload_read()
    end.
Something went wrong with that request. Please try again.