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 // Read the printk formats file. 62 std::string ReadPrintkFormats() const; 63 64 // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|. 65 std::string ReadCpuStats(size_t cpu) const; 66 67 // Set ftrace buffer size in pages. 68 // This size is *per cpu* so for the total size you have to multiply 69 // by the number of CPUs. 70 bool SetCpuBufferSizeInPages(size_t pages); 71 72 // Returns the number of CPUs. 73 // This will match the number of tracing/per_cpu/cpuXX directories. 74 size_t virtual NumberOfCpus() const; 75 76 // Clears the trace buffers for all CPUs. Blocks until this is done. 77 void ClearTrace(); 78 79 // Clears the trace buffer for cpu. Blocks until this is done. 80 void ClearPerCpuTrace(size_t cpu); 81 82 // Writes the string |str| as an event into the trace buffer. 83 bool WriteTraceMarker(const std::string& str); 84 85 // Enable tracing. 86 bool EnableTracing(); 87 88 // Disables tracing, does not clear the buffer. 89 bool DisableTracing(); 90 91 // Enables/disables tracing, does not clear the buffer. 92 bool SetTracingOn(bool enable); 93 94 // Returns true iff tracing is enabled. 95 // Necessarily racy: another program could enable/disable tracing at any 96 // point. 97 bool IsTracingEnabled(); 98 99 // Set the clock. |clock_name| should be one of the names returned by 100 // AvailableClocks. Setting the clock clears the buffer. 101 bool SetClock(const std::string& clock_name); 102 103 // Get the currently set clock. 104 std::string GetClock(); 105 106 // Get all the available clocks. 107 std::set<std::string> AvailableClocks(); 108 109 // Get all the enabled events. 110 virtual std::vector<std::string> ReadEnabledEvents(); 111 112 // Open the raw pipe for |cpu|. 113 virtual base::ScopedFile OpenPipeForCpu(size_t cpu); 114 115 virtual const std::set<std::string> GetEventNamesForGroup( 116 const std::string& path) const; 117 118 // Returns the |id| for event with the given |group| and |name|. Returns 0 if 119 // the event doesn't exist, or its /id file could not be read. Not typically 120 // needed if already parsing the format file. 121 uint32_t ReadEventId(const std::string& group, const std::string& name) const; 122 GetRootPath()123 std::string GetRootPath() const { return root_; } 124 125 protected: 126 // virtual and protected for testing. 127 virtual bool WriteToFile(const std::string& path, const std::string& str); 128 virtual bool AppendToFile(const std::string& path, const std::string& str); 129 virtual bool ClearFile(const std::string& path); 130 virtual char ReadOneCharFromFile(const std::string& path); 131 virtual std::string ReadFileIntoString(const std::string& path) const; 132 133 private: 134 // Checks the trace file is present at the given root path. 135 static bool CheckRootPath(const std::string& root); 136 137 bool WriteNumberToFile(const std::string& path, size_t value); 138 139 const std::string root_; 140 }; 141 142 } // namespace perfetto 143 144 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_ 145