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