ligo/src/lib_p2p/moving_average.mli

51 lines
1.8 KiB
OCaml
Raw Normal View History

2016-11-29 00:54:32 +04:00
(**************************************************************************)
(* *)
2018-02-06 00:17:03 +04:00
(* Copyright (c) 2014 - 2018. *)
2016-11-29 00:54:32 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
2018-02-20 21:28:05 +04:00
(** Moving averages.
This module implements bandwidth counters based on (cumulative)
exponential moving average. Each counter is identified by an
integer. They are stored in an internal hash table.
See i.e.
https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
for the algorithm.
*)
2017-01-14 16:13:59 +04:00
type t
2018-02-20 21:28:05 +04:00
(** Type of one bandwidth counter. *)
2016-11-29 00:54:32 +04:00
2017-01-14 16:13:59 +04:00
val create: init:int -> alpha:float -> t
2018-02-20 21:28:05 +04:00
(** [create ~init ~alpha] is a counter with initial value [init] and
factor [alpha]. *)
2017-01-14 16:13:59 +04:00
val destroy: t -> unit
2018-02-20 21:28:05 +04:00
(** [destroy t] removes counter [t] from the internal hash table. *)
2016-11-29 00:54:32 +04:00
2017-01-14 16:13:59 +04:00
val add: t -> int -> unit
2018-02-20 21:28:05 +04:00
(** [add t id] adds [t] in the internal hash table under identifies
[id]. *)
2016-11-29 00:54:32 +04:00
2017-01-14 16:13:59 +04:00
val on_update: (unit -> unit) -> unit
2018-02-20 21:28:05 +04:00
(** [of_update f] registers [f] to be called on each update of the
internal worker (currently every 1s). *)
2017-01-14 16:13:59 +04:00
val updated: unit Lwt_condition.t
2018-02-20 21:28:05 +04:00
(** [updated] is a condition variable that gets signaled on each
update of the internal worker (currently every 1s). *)
2017-01-14 16:13:59 +04:00
type stat = {
total: int64 ;
2017-01-14 16:13:59 +04:00
average: int ;
}
2018-02-20 21:28:05 +04:00
2017-01-14 16:13:59 +04:00
val stat: t -> stat
2018-02-20 21:28:05 +04:00
(** [stat t] is a stat record reflecting the state of [t] at the time
of the call. *)