• 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 std::unique_ptr<FtraceProcfs> Create(const std::string& root);
32   static int g_kmesg_fd;
33 
34   explicit FtraceProcfs(const std::string& root);
35   virtual ~FtraceProcfs();
36 
37   // Enable the event under with the given |group| and |name|.
38   bool EnableEvent(const std::string& group, const std::string& name);
39 
40   // Disable the event under with the given |group| and |name|.
41   bool DisableEvent(const std::string& group, const std::string& name);
42 
43   // Disable all events by writing to the global enable file.
44   bool DisableAllEvents();
45 
46   // Read the format for event with the given |group| and |name|.
47   // virtual for testing.
48   virtual std::string ReadEventFormat(const std::string& group,
49                                       const std::string& name) const;
50 
51   virtual std::string ReadPageHeaderFormat() const;
52 
53   // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|.
54   std::string ReadCpuStats(size_t cpu) const;
55 
56   // Set ftrace buffer size in pages.
57   // This size is *per cpu* so for the total size you have to multiply
58   // by the number of CPUs.
59   bool SetCpuBufferSizeInPages(size_t pages);
60 
61   // Returns the number of CPUs.
62   // This will match the number of tracing/per_cpu/cpuXX directories.
63   size_t virtual NumberOfCpus() const;
64 
65   // Clears the trace buffers for all CPUs. Blocks until this is done.
66   void ClearTrace();
67 
68   // Writes the string |str| as an event into the trace buffer.
69   bool WriteTraceMarker(const std::string& str);
70 
71   // Enable tracing.
72   bool EnableTracing();
73 
74   // Disables tracing, does not clear the buffer.
75   bool DisableTracing();
76 
77   // Enables/disables tracing, does not clear the buffer.
78   bool SetTracingOn(bool enable);
79 
80   // Returns true iff tracing is enabled.
81   // Necessarily racy: another program could enable/disable tracing at any
82   // point.
83   bool IsTracingEnabled();
84 
85   // Set the clock. |clock_name| should be one of the names returned by
86   // AvailableClocks. Setting the clock clears the buffer.
87   bool SetClock(const std::string& clock_name);
88 
89   // Get the currently set clock.
90   std::string GetClock();
91 
92   // Get all the available clocks.
93   std::set<std::string> AvailableClocks();
94 
95   // Get all the enabled events.
96   virtual std::vector<std::string> ReadEnabledEvents();
97 
98   // Open the raw pipe for |cpu|.
99   virtual base::ScopedFile OpenPipeForCpu(size_t cpu);
100 
101   virtual const std::set<std::string> GetEventNamesForGroup(
102       const std::string& path) const;
103 
104  protected:
105   // virtual and protected for testing.
106   virtual bool WriteToFile(const std::string& path, const std::string& str);
107   virtual bool AppendToFile(const std::string& path, const std::string& str);
108   virtual bool ClearFile(const std::string& path);
109   virtual char ReadOneCharFromFile(const std::string& path);
110   virtual std::string ReadFileIntoString(const std::string& path) const;
111 
112  private:
113   // Checks the trace file is present at the given root path.
114   static bool CheckRootPath(const std::string& root);
115 
116   bool WriteNumberToFile(const std::string& path, size_t value);
117 
118   const std::string root_;
119 };
120 
121 }  // namespace perfetto
122 
123 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_
124