123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openLwt.SyntaxmoduleInttbl=Hashtbl.Make(structtypet=intletequal(x:int)(y:int)=x=ylethash=Hashtbl.hashend)typet={id:int;alpha:int;mutabletotal:int64;mutablecurrent:int;mutableaverage:int;}typestate={refresh_interval:float;counters:tInttbl.t;updated:unitLwt_condition.t;mutableupdate_hook:(unit->unit)list;}letworker_loopstate()=letrecinnersleeptime_at_entry=let*()=sleepinletsleep=Lwt_unix.sleepstate.refresh_intervalinletnow=Mtime_clock.elapsed()inletelapsed=int_of_floatMtime.Span.(to_msnow-.to_mstime_at_entry)inInttbl.iter(fun_c->c.average<-(c.alpha*c.current/elapsed)+((1000-c.alpha)*c.average/1000);c.current<-0)state.counters;List.iter(funf->f())state.update_hook;Lwt_condition.broadcaststate.updated();innersleepnowininner(Lwt_unix.sleepstate.refresh_interval)(Mtime_clock.elapsed())letfresh_state~id~refresh_interval=ifrefresh_interval<=0.0thenraise(Invalid_argument"Moving_average.fresh_state");letstate={refresh_interval;counters=Inttbl.create41;updated=Lwt_condition.create();update_hook=[];}inLwt.ignore_result(Lwt_utils.worker(Format.asprintf"counter(%s)"id)~on_event:Internal_event.Lwt_worker_logger.on_event~run:(worker_loopstate)~cancel:(fun_->Lwt.return_unit));stateleton_updatestatef=state.update_hook<-f::state.update_hookletcreate=letcpt=ref0infunstate~init~alpha->ifnot(0.<alpha&&alpha<=1.)thenraise(Invalid_argument"Moving_average.create");letid=!cptinincrcpt;letalpha=int_of_float(1000.*.alpha)inletc={id;alpha;total=0L;current=0;average=init}inInttbl.addstate.countersidc;cletaddcx=c.total<-Int64.(addc.total(of_intx));c.current<-c.current+xletdestroystatec=Inttbl.removestate.countersc.idletupdated{updated;_}=updatedtypestat={total:int64;average:int}letstat({total;average;_}:t):stat={total;average}