GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
profiler.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_profiler_h)
27 #define octave_profiler_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstddef>
32 #include <map>
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 class octave_value;
38 
40 
41 class
42 OCTINTERP_API
44 {
45 public:
46 
47  // This is a utility class that can be used to call the enter/exit
48  // functions in a manner protected from stack unwinding.
49  template <typename T> class enter
50  {
51  private:
52 
53  profiler& m_profiler;
54  std::string m_fcn;
55  bool m_enabled;
56 
57  public:
58 
59  enter (profiler& p, const T& t) : m_profiler (p)
60  {
61  // A profiling block cannot be active if the profiler is not
62  m_enabled = m_profiler.enabled ();
63 
64  if (m_enabled)
65  {
66  m_fcn = t.profiler_name ();
67 
68  // NOTE: The test f != "" must be kept to prevent a blank
69  // line showing up in profiler statistics. See bug
70  // #39524. The root cause is that the function name is
71  // not set for the recurring readline hook function.
72  if (m_fcn == "")
73  m_enabled = false; // Inactive profiling block
74  else
75  m_profiler.enter_function (m_fcn);
76  }
77  }
78 
79  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (enter)
80 
81  ~enter ()
82  {
83  if (m_enabled)
84  m_profiler.exit_function (m_fcn);
85  }
86  };
87 
88  profiler ();
89 
90  OCTAVE_DISABLE_COPY_MOVE (profiler)
91 
92  virtual ~profiler ();
93 
94  bool enabled () const { return m_enabled; }
95  void set_active (bool);
96 
97  void reset ();
98 
99  octave_value get_flat () const;
100  octave_value get_hierarchical () const;
101 
102 private:
103 
104  // One entry in the flat profile (i.e., a collection of data for a single
105  // function). This is filled in when building the flat profile from the
106  // hierarchical call tree.
107  struct stats
108  {
109  public:
110 
111  stats ();
112 
113  OCTAVE_DEFAULT_COPY_MOVE_DELETE (stats)
114 
115  typedef std::set<octave_idx_type> function_set;
116 
117  // Convert a function_set list to an Octave array of indices.
118  static octave_value function_set_value (const function_set&);
119 
120  //--------
121 
122  double m_time;
123  std::size_t m_calls;
124 
125  bool m_recursive;
126 
127  function_set m_parents;
128  function_set m_children;
129  };
130 
131  typedef std::vector<stats> flat_profile;
132 
133  // Store data for one node in the call-tree of the hierarchical profiler
134  // data we collect.
135  class tree_node
136  {
137  public:
138 
139  tree_node (tree_node *, octave_idx_type);
140 
141  virtual ~tree_node ();
142 
143  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_node)
144 
145  void add_time (double dt) { m_time += dt; }
146 
147  // Enter a child function. It is created in the list of children if it
148  // wasn't already there. The now-active child node is returned.
149  tree_node * enter (octave_idx_type);
150 
151  // Exit function. As a sanity-check, it is verified that the currently
152  // active function actually is the one handed in here. Returned is the
153  // then-active node, which is our parent.
154  tree_node * exit (octave_idx_type);
155 
156  void build_flat (flat_profile&) const;
157 
158  // Get the hierarchical profile for this node and its children. If total
159  // is set, accumulate total time of the subtree in that variable as
160  // additional return value.
161  octave_value get_hierarchical (double *total = nullptr) const;
162 
163  private:
164 
165  tree_node *m_parent;
166  octave_idx_type m_fcn_id;
167 
168  typedef std::map<octave_idx_type, tree_node *> child_map;
169  child_map m_children;
170 
171  // This is only time spent *directly* on this level, excluding children!
172  double m_time;
173 
174  std::size_t m_calls;
175  };
176 
177  // Each function we see in the profiler is given a unique index (which
178  // simply counts starting from 1). We thus have to map profiler-names to
179  // those indices. For all other stuff, we identify functions by their
180  // index.
181 
182  typedef std::vector<std::string> function_set;
183  typedef std::map<std::string, octave_idx_type> fcn_index_map;
184 
185  function_set m_known_functions;
186  fcn_index_map m_fcn_index;
187 
188  bool m_enabled;
189 
190  tree_node *m_call_tree;
191  tree_node *m_active_fcn;
192 
193  // Store last timestamp we had, when the currently active function was
194  // called.
195  double m_last_time;
196 
197  // These are private as only the unwind-protecting inner class enter
198  // should be allowed to call them.
199  void enter_function (const std::string&);
200  void exit_function (const std::string&);
201 
202  // Query a timestamp, used for timing calls (obviously).
203  // This is not static because in the future, maybe we want a flag
204  // in the profiler or something to choose between cputime, wall-time,
205  // user-time, system-time, ...
206  double query_time () const;
207 
208  // Add the time elapsed since last_time to the function we're currently in.
209  // This is called from two different positions, thus it is useful to have
210  // it as a separate function.
211  void add_current_time ();
212 };
213 
214 OCTAVE_END_NAMESPACE(octave)
215 
216 #endif
enter(profiler &p, const T &t)
Definition: profiler.h:59
bool enabled() const
Definition: profiler.h:94
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn