----------------------------------------------------------------------- -- measure -- Benchmark tools -- Copyright (C) 2008 - 2019 Stephane Carrez -- Written by Stephane Carrez (Stephane.Carrez@gmail.com) -- -- 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. ----------------------------------------------------------------------- with Ada.Task_Attributes; with Ada.Strings.Hash; with Ada.Unchecked_Deallocation; with GNAT.Calendar.Time_IO; with Util.Streams.Buffered; with Util.Streams.Texts.TR; package body Util.Measures is ISO_DATE_TIME : constant GNAT.Calendar.Time_IO.Picture_String := "%Y-%m-%d %H:%M:%S"; procedure Free is new Ada.Unchecked_Deallocation (Buckets_Type, Buckets_Access); procedure Free is new Ada.Unchecked_Deallocation (Measure, Measure_Access); procedure Free is new Ada.Unchecked_Deallocation (String, String_Access); package Task_Context is new Ada.Task_Attributes (Measure_Set_Access, null); function Format (D : in Duration) return String; -- Format the duration in a time in 'ns', 'us', 'ms' or seconds. function Format (D : in Duration; Unit : in Unit_Type) return String; -- ------------------------------ -- Disable collecting measures on the measure set. -- ------------------------------ procedure Disable (Measures : in out Measure_Set) is begin Measures.Enabled := False; end Disable; -- ------------------------------ -- Enable collecting measures on the measure set. -- ------------------------------ procedure Enable (Measures : in out Measure_Set) is begin Measures.Enabled := True; end Enable; -- ------------------------------ -- Set the per-thread measure set. -- ------------------------------ procedure Set_Current (Measures : in Measure_Set_Access) is begin Task_Context.Set_Value (Measures); end Set_Current; -- ------------------------------ -- Get the per-thread measure set. -- ------------------------------ function Get_Current return Measure_Set_Access is begin return Task_Context.Value; end Get_Current; -- ------------------------------ -- Dump an XML result with the measures collected by the measure set. -- When writing the measures, the measure set is cleared. It is safe -- to write measures while other measures are being collected. -- ------------------------------ procedure Write (Measures : in out Measure_Set; Title : in String; Stream : in out Util.Streams.Texts.Print_Stream'Class) is procedure Dump_XML (Item : in Measure_Access); procedure Dump_XML (Item : in Measure_Access) is Total : constant String := Format (Item.Time); Time : constant String := Format (Item.Time / Item.Count); begin Stream.Write ("