LCOV - code coverage report
Current view: top level - source/lib/file/common - trace.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 6 6 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * IO event recording
      25             :  */
      26             : 
      27             : // traces are useful for determining the optimal ordering of archived files
      28             : // and can also serve as a repeatable IO benchmark.
      29             : 
      30             : // note: since FileContents are smart pointers, the trace can't easily
      31             : // be notified when they are released (relevant for cache simulation).
      32             : // we have to assume that users process one file at a time -- as they
      33             : // should.
      34             : 
      35             : #ifndef INCLUDED_TRACE
      36             : #define INCLUDED_TRACE
      37             : 
      38             : #include "lib/os_path.h"
      39             : #include "lib/path.h"
      40             : #include "lib/status.h"
      41             : 
      42             : #include <cstddef>
      43             : #include <memory>
      44             : #include <string>
      45             : 
      46             : // stores information about an IO event.
      47           7 : class TraceEntry
      48             : {
      49             : public:
      50             :     enum EAction
      51             :     {
      52             :         Load = 'L',
      53             :         Store = 'S'
      54             :     };
      55             : 
      56             :     TraceEntry(EAction action, const Path& pathname, size_t size);
      57             :     TraceEntry(const std::wstring& text);
      58             : 
      59           4 :     EAction Action() const
      60             :     {
      61           4 :         return m_action;
      62             :     }
      63             : 
      64             :     const Path& Pathname() const
      65             :     {
      66             :         return m_pathname;
      67             :     }
      68             : 
      69           4 :     size_t Size() const
      70             :     {
      71           4 :         return m_size;
      72             :     }
      73             : 
      74             :     std::wstring EncodeAsText() const;
      75             : 
      76             : private:
      77             :     // note: keep an eye on the class size because all instances are kept
      78             :     // in memory (see ITrace)
      79             : 
      80             :     // time (as returned by timer_Time) after the operation completes.
      81             :     // rationale: when loading, the VFS doesn't know file size until
      82             :     // querying the cache or retrieving file information.
      83             :     float m_timestamp;
      84             : 
      85             :     EAction m_action;
      86             : 
      87             :     Path m_pathname;
      88             : 
      89             :     // size of file.
      90             :     // rationale: other applications using this trace format might not
      91             :     // have access to the VFS and its file information.
      92             :     size_t m_size;
      93             : };
      94             : 
      95             : 
      96             : // note: to avoid interfering with measurements, this trace container
      97             : // does not cause any IOs (except of course in Load/Store)
      98         143 : struct ITrace
      99             : {
     100             :     virtual ~ITrace();
     101             : 
     102             :     virtual void NotifyLoad(const Path& pathname, size_t size) = 0;
     103             :     virtual void NotifyStore(const Path& pathname, size_t size) = 0;
     104             : 
     105             :     /**
     106             :      * store all entries into a file.
     107             :      *
     108             :      * @param pathname (native, absolute)
     109             :      *
     110             :      * note: the file format is text-based to allow human inspection and
     111             :      * because storing filename strings in a binary format would be a
     112             :      * bit awkward.
     113             :      **/
     114             :     virtual Status Store(const OsPath& pathname) const = 0;
     115             : 
     116             :     /**
     117             :      * load entries from file.
     118             :      *
     119             :      * @param pathname (native, absolute)
     120             :      *
     121             :      * replaces any existing entries.
     122             :      **/
     123             :     virtual Status Load(const OsPath& pathname) = 0;
     124             : 
     125             :     virtual const TraceEntry* Entries() const = 0;
     126             :     virtual size_t NumEntries() const = 0;
     127             : };
     128             : 
     129             : typedef std::shared_ptr<ITrace> PITrace;
     130             : 
     131             : extern PITrace CreateDummyTrace(size_t maxSize);
     132             : extern PITrace CreateTrace(size_t maxSize);
     133             : 
     134             : #endif  // #ifndef INCLUDED_TRACE

Generated by: LCOV version 1.16