diff options
author | Simon MacMullen <simon@rabbitmq.com> | 2012-09-27 15:45:48 +0100 |
---|---|---|
committer | Simon MacMullen <simon@rabbitmq.com> | 2012-09-27 15:45:48 +0100 |
commit | 30f0965f25789fac24bbee6f6e35a3d256ba32cd (patch) | |
tree | 00c3f5b40ecad5aa113b7a22080f4069923a9cdc | |
parent | e92bbcbb0602aaa87e3aea0c5f514219c6122f6a (diff) | |
download | rabbitmq-server-30f0965f25789fac24bbee6f6e35a3d256ba32cd.tar.gz |
interval_operation/3
-rw-r--r-- | src/rabbit_vm.erl | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/rabbit_vm.erl b/src/rabbit_vm.erl index cc9d9635..9f9b9fbe 100644 --- a/src/rabbit_vm.erl +++ b/src/rabbit_vm.erl @@ -16,7 +16,7 @@ -module(rabbit_vm). --export([memory/0]). +-export([memory/0, interval_operation/3]). -define(MAGIC_PLUGINS, [mochiweb, webmachine, cowboy, sockjs, rfc4627_jsonrpc]). @@ -25,7 +25,9 @@ -ifdef(use_specs). -spec(memory/0 :: () -> rabbit_types:infos()). - +-spec(interval_operation/3 :: + (fun (() -> any()), non_neg_integer(), non_neg_integer()) + -> {any(), non_neg_integer()}). -endif. %%---------------------------------------------------------------------------- @@ -71,6 +73,17 @@ memory() -> %% claims about negative memory. See %% http://erlang.org/pipermail/erlang-questions/2012-September/069320.html + +%% Ideally, you'd want Fun to run every IdealInterval. but you don't +%% want it to take more than MaxTime per IdealInterval. So if it takes +%% more then you want to run it less often. So we time how long it +%% takes to run, and then suggest how long you should wait before +%% running it again. Times are in millis. +interval_operation(Fun, MaxTime, IdealInterval) -> + {Micros, Res} = timer:tc(Fun), + Ratio = lists:max([1, Micros / MaxTime / 1000]), + {Res, round(IdealInterval * Ratio)}. + %%---------------------------------------------------------------------------- sup_memory(Sup) -> |