xref: /aosp_15_r20/external/ruy/ruy/profiler/profiler.h (revision bb86c7ed5fb1b98a7eac808e443a46cc8b90dfc0)
1 /* Copyright 2020 Google LLC. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef RUY_RUY_PROFILER_PROFILER_H_
17 #define RUY_RUY_PROFILER_PROFILER_H_
18 
19 #include <cstdio>
20 
21 #ifdef RUY_PROFILER
22 #include <atomic>
23 #include <chrono>
24 #include <thread>
25 #include <vector>
26 #endif
27 
28 #include "ruy/profiler/instrumentation.h"
29 #include "ruy/profiler/treeview.h"
30 
31 namespace ruy {
32 namespace profiler {
33 
34 #ifdef RUY_PROFILER
35 
36 // RAII user-facing way to create a profiler and let it profile a code scope,
37 // and print out an ASCII/MarkDown treeview upon leaving the scope.
38 class ScopeProfile {
39  public:
40   // Default constructor, unconditionally profiling.
41   ScopeProfile();
42 
43   // Constructor allowing to choose at runtime whether to profile.
44   explicit ScopeProfile(bool enable);
45 
46   // Destructor. It's where the profile is reported.
47   ~ScopeProfile();
48 
49   // See treeview_ member.
SetUserTreeView(TreeView * treeview)50   void SetUserTreeView(TreeView* treeview) { user_treeview_ = treeview; }
51 
52  private:
53   void Start();
54 
55   // Thread entry point function for the profiler thread. This thread is
56   // created on construction.
57   void ThreadFunc();
58 
59   // Record a stack as a sample.
60   void Sample(const detail::ThreadStack& stack);
61 
62   // Finalize the profile. Called on destruction.
63   // If user_treeview_ is non-null, it will receive the treeview.
64   // Otherwise the treeview will just be printed.
65   void Finish();
66 
67   // Buffer where samples are recorded during profiling.
68   std::vector<char> samples_buf_;
69 
70   // Used to synchronize thread termination.
71   std::atomic<bool> finishing_;
72 
73   // Underlying profiler thread, which will perform the sampling.
74   // This profiler approach relies on a thread rather than on signals.
75   std::unique_ptr<std::thread> thread_;
76 
77   // TreeView to populate upon destruction. If left null (the default),
78   // a temporary treeview will be used and dumped on stdout. The user
79   // may override that by passing their own TreeView object for other
80   // output options or to directly inspect the TreeView.
81   TreeView* user_treeview_ = nullptr;
82 };
83 
84 #else  // no RUY_PROFILER
85 
86 struct ScopeProfile {
87   ScopeProfile() {
88 #ifdef GEMMLOWP_PROFILING
89     fprintf(
90         stderr,
91         "\n\n\n**********\n\nWARNING:\n\nLooks like you defined "
92         "GEMMLOWP_PROFILING, but this code has been ported to the new ruy "
93         "profiler replacing the old gemmlowp profiler. You should now be "
94         "defining RUY_PROFILER and not GEMMLOWP_PROFILING. When building using "
95         "Bazel, just pass --define=ruy_profiler=true.\n\n**********\n\n\n");
96 #endif
97   }
98   explicit ScopeProfile(bool) {}
99 };
100 
101 #endif
102 
103 }  // namespace profiler
104 }  // namespace ruy
105 
106 #endif  // RUY_RUY_PROFILER_PROFILER_H_
107