LCOV - code coverage report
Current view: top level - source/ps - Profile.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 4 50.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /*
      19             :  * GPG3-style hierarchical profiler
      20             :  */
      21             : 
      22             : #ifndef INCLUDED_PROFILE
      23             : #define INCLUDED_PROFILE
      24             : 
      25             : #include "lib/adts/ring_buf.h"
      26             : #include "lib/code_annotation.h"
      27             : #include "ps/Profiler2.h"
      28             : #include "ps/Singleton.h"
      29             : 
      30             : #include <vector>
      31             : 
      32             : class CProfileNodeTable;
      33             : 
      34             : #define PROFILE_AMORTIZE_FRAMES 30
      35             : #define PROFILE_AMORTIZE_TURNS 5
      36             : 
      37             : // To profile scripts usefully, we use a call hook that's called on every enter/exit,
      38             : // and need to find the function name. But most functions are anonymous so we make do
      39             : // with filename plus line number instead.
      40             : // Computing the names is fairly expensive, and we need to return an interned char*
      41             : // for the profiler to hold a copy of, so we use boost::flyweight to construct interned
      42             : // strings per call location.
      43             : //
      44             : // TODO: Check again how much the overhead for getting filename and line really is and if
      45             : // it has increased with the new approach after the SpiderMonkey 31 upgrade.
      46             : 
      47             : class CProfileNode
      48             : {
      49             :     NONCOPYABLE(CProfileNode);
      50             : public:
      51             :     typedef std::vector<CProfileNode*>::iterator profile_iterator;
      52             :     typedef std::vector<CProfileNode*>::const_iterator const_profile_iterator;
      53             : 
      54             :     CProfileNode( const char* name, CProfileNode* parent );
      55             :     ~CProfileNode();
      56             : 
      57       16653 :     const char* GetName() const { return name; }
      58             : 
      59             :     double GetFrameCalls() const;
      60             :     double GetFrameTime() const;
      61             :     double GetTurnCalls() const;
      62             :     double GetTurnTime() const;
      63             : 
      64             :     const CProfileNode* GetChild( const char* name ) const;
      65             :     const CProfileNode* GetScriptChild( const char* name ) const;
      66           0 :     const std::vector<CProfileNode*>* GetChildren() const { return( &children ); }
      67           0 :     const std::vector<CProfileNode*>* GetScriptChildren() const { return( &script_children ); }
      68             : 
      69             :     bool CanExpand();
      70             : 
      71             :     CProfileNode* GetChild( const char* name );
      72             :     CProfileNode* GetScriptChild( const char* name );
      73       16653 :     CProfileNode* GetParent() const { return( parent ); }
      74             : 
      75             :     // Resets timing information for this node and all its children
      76             :     void Reset();
      77             :     // Resets frame timings for this node and all its children
      78             :     void Frame();
      79             :     // Resets turn timings for this node and all its children
      80             :     void Turn();
      81             :     // Enters the node
      82             :     void Call();
      83             :     // Leaves the node. Returns true if the node has actually been left
      84             :     bool Return();
      85             : 
      86             : private:
      87             :     friend class CProfileManager;
      88             :     friend class CProfileNodeTable;
      89             : 
      90             :     const char* name;
      91             : 
      92             :     int calls_frame_current;
      93             :     int calls_turn_current;
      94             :     RingBuf<int, PROFILE_AMORTIZE_FRAMES> calls_per_frame;
      95             :     RingBuf<int, PROFILE_AMORTIZE_TURNS> calls_per_turn;
      96             : 
      97             :     double time_frame_current;
      98             :     double time_turn_current;
      99             :     RingBuf<double, PROFILE_AMORTIZE_FRAMES> time_per_frame;
     100             :     RingBuf<double, PROFILE_AMORTIZE_TURNS> time_per_turn;
     101             : 
     102             :     double start;
     103             :     int recursion;
     104             : 
     105             :     CProfileNode* parent;
     106             :     std::vector<CProfileNode*> children;
     107             :     std::vector<CProfileNode*> script_children;
     108             :     CProfileNodeTable* display_table;
     109             : };
     110             : 
     111             : class CProfileManager : public Singleton<CProfileManager>
     112             : {
     113             : public:
     114             :     CProfileManager();
     115             :     ~CProfileManager();
     116             : 
     117             :     // Begins timing for a named subsection
     118             :     void Start( const char* name );
     119             :     void StartScript( const char* name );
     120             : 
     121             :     // Ends timing for the current subsection
     122             :     void Stop();
     123             : 
     124             :     // Resets all timing information
     125             :     void Reset();
     126             :     // Resets frame timing information
     127             :     void Frame();
     128             :     // Resets turn timing information
     129             :     // (Must not be called before Frame)
     130             :     void Turn();
     131             :     // Resets absolutely everything, at the end of this frame
     132             :     void StructuralReset();
     133             : 
     134             :     inline const CProfileNode* GetCurrent() { return( current ); }
     135             :     inline const CProfileNode* GetRoot() { return( root ); }
     136             : 
     137             : private:
     138             :     CProfileNode* root;
     139             :     CProfileNode* current;
     140             : 
     141             :     bool needs_structural_reset;
     142             : 
     143             :     void PerformStructuralReset();
     144             : };
     145             : 
     146             : #define g_Profiler CProfileManager::GetSingleton()
     147             : 
     148             : class CProfileSample
     149             : {
     150             : public:
     151             :     CProfileSample(const char* name);
     152             :     ~CProfileSample();
     153             : };
     154             : 
     155             : // Put a PROFILE("xyz") block at the start of all code to be profiled.
     156             : // Profile blocks last until the end of the containing scope.
     157             : #define PROFILE(name) CProfileSample __profile(name)
     158             : // Cheat a bit to make things slightly easier on the user
     159             : #define PROFILE_START(name) { CProfileSample __profile(name)
     160             : #define PROFILE_END(name) }
     161             : 
     162             : // Do both old and new profilers simultaneously (1+2=3), for convenience.
     163             : #define PROFILE3(name) PROFILE(name); PROFILE2(name)
     164             : 
     165             : // Also do GPU
     166             : #define PROFILE3_GPU(deviceCommandContext, name) PROFILE(name); PROFILE2(name); PROFILE2_GPU(deviceCommandContext, name)
     167             : 
     168             : #endif // INCLUDED_PROFILE

Generated by: LCOV version 1.16