• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_
18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_
19 
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "perfetto/ext/base/scoped_file.h"
26 
27 namespace perfetto {
28 
29 class FtraceProcfs {
30  public:
31   static const char* const kTracingPaths[];
32 
33   // Tries creating an |FtraceProcfs| at the standard tracefs mount points.
34   // Takes an optional |instance_path| such as "instances/wifi/", in which case
35   // the returned object will be for that ftrace instance path.
36   static std::unique_ptr<FtraceProcfs> CreateGuessingMountPoint(
37       const std::string& instance_path = "");
38 
39   static std::unique_ptr<FtraceProcfs> Create(const std::string& root);
40 
41   static int g_kmesg_fd;
42 
43   explicit FtraceProcfs(const std::string& root);
44   virtual ~FtraceProcfs();
45 
46   // Set the filter for syscall events. If empty, clear the filter.
47   bool SetSyscallFilter(const std::set<size_t>& filter);
48 
49   // Enable the event under with the given |group| and |name|.
50   bool EnableEvent(const std::string& group, const std::string& name);
51 
52   // Disable the event under with the given |group| and |name|.
53   bool DisableEvent(const std::string& group, const std::string& name);
54 
55   // Returns true if the event under the given |group| and |name| exists and its
56   // enable file is writeable.
57   bool IsEventAccessible(const std::string& group, const std::string& name);
58 
59   // Disable all events by writing to the global enable file.
60   bool DisableAllEvents();
61 
62   // Read the format for event with the given |group| and |name|.
63   // virtual for testing.
64   virtual std::string ReadEventFormat(const std::string& group,
65                                       const std::string& name) const;
66 
67   virtual std::string ReadPageHeaderFormat() const;
68 
69   std::string GetCurrentTracer();
70   // Sets the "current_tracer". Might fail with EBUSY if tracing pipes have
71   // already been opened for reading.
72   bool SetCurrentTracer(const std::string& tracer);
73   // Resets the "current_tracer" to "nop".
74   bool ResetCurrentTracer();
75   bool AppendFunctionFilters(const std::vector<std::string>& filters);
76   bool ClearFunctionFilters();
77   bool AppendFunctionGraphFilters(const std::vector<std::string>& filters);
78   bool ClearFunctionGraphFilters();
79 
80   // Get all triggers for event with the given |group| and |name|.
81   std::vector<std::string> ReadEventTriggers(const std::string& group,
82                                              const std::string& name) const;
83 
84   // Create an event trigger for the given |group| and |name|.
85   bool CreateEventTrigger(const std::string& group,
86                           const std::string& name,
87                           const std::string& trigger);
88 
89   // Remove an event trigger for the given |group| and |name|.
90   bool RemoveEventTrigger(const std::string& group,
91                           const std::string& name,
92                           const std::string& trigger);
93 
94   // Remove all event trigger for the given |group| and |name|.
95   bool RemoveAllEventTriggers(const std::string& group,
96                               const std::string& name);
97 
98   // Sets up any associated event trigger before enabling the event
99   bool MaybeSetUpEventTriggers(const std::string& group,
100                                const std::string& name);
101 
102   // Tears down any associated event trigger after disabling the event
103   bool MaybeTearDownEventTriggers(const std::string& group,
104                                   const std::string& name);
105 
106   // Returns true if rss_stat_throttled synthetic event is supported
107   bool SupportsRssStatThrottled();
108 
109   // Read the printk formats file.
110   std::string ReadPrintkFormats() const;
111 
112   // Opens the "/per_cpu/cpuXX/stats" file for the given |cpu|.
113   base::ScopedFile OpenCpuStats(size_t cpu) const;
114 
115   // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|.
116   std::string ReadCpuStats(size_t cpu) const;
117 
118   // Set ftrace buffer size in pages.
119   // This size is *per cpu* so for the total size you have to multiply
120   // by the number of CPUs.
121   bool SetCpuBufferSizeInPages(size_t pages);
122 
123   // Returns the number of CPUs.
124   // This will match the number of tracing/per_cpu/cpuXX directories.
125   size_t virtual NumberOfCpus() const;
126 
127   // Clears the trace buffers for all CPUs. Blocks until this is done.
128   void ClearTrace();
129 
130   // Clears the trace buffer for cpu. Blocks until this is done.
131   void ClearPerCpuTrace(size_t cpu);
132 
133   // Writes the string |str| as an event into the trace buffer.
134   bool WriteTraceMarker(const std::string& str);
135 
136   // Read tracing_on and return true if tracing_on is 1, otherwise return false.
137   bool GetTracingOn();
138 
139   // Write 1 to tracing_on if |on| is true, otherwise write 0.
140   bool SetTracingOn(bool on);
141 
142   // Returns true if ftrace tracing is available.
143   // Ftrace tracing is available iff "/current_tracer" is "nop", indicates
144   // function tracing is not in use. Necessarily
145   // racy: another program could enable/disable tracing at any point.
146   bool IsTracingAvailable();
147 
148   // Set the clock. |clock_name| should be one of the names returned by
149   // AvailableClocks. Setting the clock clears the buffer.
150   bool SetClock(const std::string& clock_name);
151 
152   // Get the currently set clock.
153   std::string GetClock();
154 
155   // Get all the available clocks.
156   std::set<std::string> AvailableClocks();
157 
158   // Get all the enabled events.
159   virtual std::vector<std::string> ReadEnabledEvents();
160 
161   // Open the raw pipe for |cpu|.
162   virtual base::ScopedFile OpenPipeForCpu(size_t cpu);
163 
164   virtual const std::set<std::string> GetEventNamesForGroup(
165       const std::string& path) const;
166 
167   // Returns the |id| for event with the given |group| and |name|. Returns 0 if
168   // the event doesn't exist, or its /id file could not be read. Not typically
169   // needed if already parsing the format file.
170   uint32_t ReadEventId(const std::string& group, const std::string& name) const;
171 
GetRootPath()172   std::string GetRootPath() const { return root_; }
173 
174  protected:
175   // virtual and protected for testing.
176   virtual bool WriteToFile(const std::string& path, const std::string& str);
177   virtual bool AppendToFile(const std::string& path, const std::string& str);
178   virtual bool ClearFile(const std::string& path);
179   virtual bool IsFileWriteable(const std::string& path);
180   virtual char ReadOneCharFromFile(const std::string& path);
181   virtual std::string ReadFileIntoString(const std::string& path) const;
182 
183  private:
184   // Checks the trace file is present at the given root path.
185   static bool CheckRootPath(const std::string& root);
186 
187   bool WriteNumberToFile(const std::string& path, size_t value);
188 
189   const std::string root_;
190 };
191 
192 }  // namespace perfetto
193 
194 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_
195