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 static int g_kmesg_fd; 41 42 explicit FtraceProcfs(const std::string& root); 43 virtual ~FtraceProcfs(); 44 45 // Enable the event under with the given |group| and |name|. 46 bool EnableEvent(const std::string& group, const std::string& name); 47 48 // Disable the event under with the given |group| and |name|. 49 bool DisableEvent(const std::string& group, const std::string& name); 50 51 // Disable all events by writing to the global enable file. 52 bool DisableAllEvents(); 53 54 // Read the format for event with the given |group| and |name|. 55 // virtual for testing. 56 virtual std::string ReadEventFormat(const std::string& group, 57 const std::string& name) const; 58 59 virtual std::string ReadPageHeaderFormat() const; 60 61 // Get all triggers for event with the given |group| and |name|. 62 std::vector<std::string> ReadEventTriggers(const std::string& group, 63 const std::string& name) const; 64 65 // Create an event trigger for the given |group| and |name|. 66 bool CreateEventTrigger(const std::string& group, 67 const std::string& name, 68 const std::string& trigger); 69 70 // Remove an event trigger for the given |group| and |name|. 71 bool RemoveEventTrigger(const std::string& group, 72 const std::string& name, 73 const std::string& trigger); 74 75 // Remove all event trigger for the given |group| and |name|. 76 bool RemoveAllEventTriggers(const std::string& group, 77 const std::string& name); 78 79 // Sets up any associated event trigger before enabling the event 80 bool MaybeSetUpEventTriggers(const std::string& group, 81 const std::string& name); 82 83 // Tears down any associated event trigger after disabling the event 84 bool MaybeTearDownEventTriggers(const std::string& group, 85 const std::string& name); 86 87 // Returns true if rss_stat_throttled synthetic event is supported 88 bool SupportsRssStatThrottled(); 89 90 // Read the printk formats file. 91 std::string ReadPrintkFormats() const; 92 93 // Opens the "/per_cpu/cpuXX/stats" file for the given |cpu|. 94 base::ScopedFile OpenCpuStats(size_t cpu) const; 95 96 // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|. 97 std::string ReadCpuStats(size_t cpu) const; 98 99 // Set ftrace buffer size in pages. 100 // This size is *per cpu* so for the total size you have to multiply 101 // by the number of CPUs. 102 bool SetCpuBufferSizeInPages(size_t pages); 103 104 // Returns the number of CPUs. 105 // This will match the number of tracing/per_cpu/cpuXX directories. 106 size_t virtual NumberOfCpus() const; 107 108 // Clears the trace buffers for all CPUs. Blocks until this is done. 109 void ClearTrace(); 110 111 // Clears the trace buffer for cpu. Blocks until this is done. 112 void ClearPerCpuTrace(size_t cpu); 113 114 // Writes the string |str| as an event into the trace buffer. 115 bool WriteTraceMarker(const std::string& str); 116 117 // Enable tracing. 118 bool EnableTracing(); 119 120 // Disables tracing, does not clear the buffer. 121 bool DisableTracing(); 122 123 // Enables/disables tracing, does not clear the buffer. 124 bool SetTracingOn(bool enable); 125 126 // Returns true iff tracing is enabled. 127 // Necessarily racy: another program could enable/disable tracing at any 128 // point. 129 bool IsTracingEnabled(); 130 131 // Set the clock. |clock_name| should be one of the names returned by 132 // AvailableClocks. Setting the clock clears the buffer. 133 bool SetClock(const std::string& clock_name); 134 135 // Get the currently set clock. 136 std::string GetClock(); 137 138 // Get all the available clocks. 139 std::set<std::string> AvailableClocks(); 140 141 // Get all the enabled events. 142 virtual std::vector<std::string> ReadEnabledEvents(); 143 144 // Open the raw pipe for |cpu|. 145 virtual base::ScopedFile OpenPipeForCpu(size_t cpu); 146 147 virtual const std::set<std::string> GetEventNamesForGroup( 148 const std::string& path) const; 149 150 // Returns the |id| for event with the given |group| and |name|. Returns 0 if 151 // the event doesn't exist, or its /id file could not be read. Not typically 152 // needed if already parsing the format file. 153 uint32_t ReadEventId(const std::string& group, const std::string& name) const; 154 GetRootPath()155 std::string GetRootPath() const { return root_; } 156 157 protected: 158 // virtual and protected for testing. 159 virtual bool WriteToFile(const std::string& path, const std::string& str); 160 virtual bool AppendToFile(const std::string& path, const std::string& str); 161 virtual bool ClearFile(const std::string& path); 162 virtual char ReadOneCharFromFile(const std::string& path); 163 virtual std::string ReadFileIntoString(const std::string& path) const; 164 165 private: 166 // Checks the trace file is present at the given root path. 167 static bool CheckRootPath(const std::string& root); 168 169 bool WriteNumberToFile(const std::string& path, size_t value); 170 171 const std::string root_; 172 }; 173 174 } // namespace perfetto 175 176 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_ 177