123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Hook to profile loops iterations *)openMopsaopenHookopenAstmoduleHook=struct(** {2 Hook header} *)(** *************** *)letname="loop_profiler"letdebugfmt=Debug.debug~channel:"loop_profiler"fmt(** {2 Profiling records} *)(** ********************* *)(** A loop is identified by its range and its body. The information of body
is necessary to compute the number of iterations *)typeloop={range:range;body:stmt;}moduleLoopMap=MapExt.Make(structtypet=loopletcomparel1l2=compare_rangel1.rangel2.rangeend)(** For each loop, and for each time we hit the loop, we compute the number
of iterations until we exit the loop *)typeiterations=intlist(** Statistics table mapping loops to iterations stats *)letstats:iterationsLoopMap.tref=refLoopMap.empty(** Print statistics table *)letprintfmt()=Format.fprintffmt"Loops profiling:@.";LoopMap.iter(funloopiterations->letsum=List.fold_left(+)(0)iterationsinletnb=List.lengthiterationsinletavg=(float_of_intsum)/.(float_of_intnb)inFormat.fprintffmt" %a: %d time%a, %.2f avg. iterations (%a)@."pp_relative_rangeloop.rangenbDebug.plurial_intnbavgFormat.(pp_print_list~pp_sep:(funfmt()->pp_print_stringfmt", ")pp_print_int)iterations)!stats(** A frame in the stack of still-executing loops, for tracking the number
iterations *)typeframe={loop:loop;iterations:int;inside:bool;(* Flag indicating whether we are inside the loop body *)}(** Stack of still-executing loops *)letstack:framelistref=ref[](** Check whether a statement is the body of the top loop *)letis_loop_bodystmt=match!stackwith|[]->false|hd::_->hd.loop.body==stmt(** Event raised when we enter a loop *)leton_enter_looprangebody=ifis_loop_bodybodythen()elseletframe={loop={range;body};iterations=0;inside=false;}instack:=frame::!stack(** Event raised when we start a new iteration *)leton_enter_body()=match!stackwith|hd::tlwhennothd.inside->stack:={hdwithiterations=hd.iterations+1;inside=true}::tl|_->()(** Event raised when we terminate an iteration *)leton_exit_body()=match!stackwith|hd::tlwhenhd.inside->stack:={hdwithinside=false}::tl|_->()(** Event raised when we exit a loop *)leton_exit_looprangebody=match!stackwith|hd::tlwhenis_loop_bodybody->(* Save the number of iterations in the stats table *)letold=tryLoopMap.findhd.loop!statswithNot_found->[]instats:=LoopMap.addhd.loop(hd.iterations::old)!stats;(* Pop the frame from the stack *)stack:=tl|_->()(** {2 Events handlers} *)(** ******************* *)letinitctx=()leton_before_execroutestmtmanflow=matchskindstmtwith|S_while(_,body)->on_enter_loopstmt.srangebody|_whenis_loop_bodystmt->on_enter_body()|_->()leton_after_execroutestmtmanflowpost=matchskindstmtwith|S_while(_,body)->on_exit_loopstmt.srangebody|_whenis_loop_bodystmt->on_exit_body()|_->()leton_before_evalroutesemanticexpmanflow=()leton_after_evalroutesemanticexpmanfloweval=()leton_finishmanflow=printFormat.std_formatter()endlet()=register_stateless_hook(moduleHook)