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_FTRACE_READER_FTRACE_PROCFS_H_ 18 #define SRC_FTRACE_READER_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 // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|. 51 std::string ReadCpuStats(size_t cpu) const; 52 53 // Set ftrace buffer size in pages. 54 // This size is *per cpu* so for the total size you have to multiply 55 // by the number of CPUs. 56 bool SetCpuBufferSizeInPages(size_t pages); 57 58 // Returns the number of CPUs. 59 // This will match the number of tracing/per_cpu/cpuXX directories. 60 size_t virtual NumberOfCpus() const; 61 62 // Clears the trace buffers for all CPUs. Blocks until this is done. 63 void ClearTrace(); 64 65 // Writes the string |str| as an event into the trace buffer. 66 bool WriteTraceMarker(const std::string& str); 67 68 // Enable tracing. 69 bool EnableTracing(); 70 71 // Disables tracing, does not clear the buffer. 72 bool DisableTracing(); 73 74 // Enabls/disables tracing, does not clear the buffer. 75 bool SetTracingOn(bool enable); 76 77 // Returns true iff tracing is enabled. 78 // Necessarily racy: another program could enable/disable tracing at any 79 // point. 80 bool IsTracingEnabled(); 81 82 // Set the clock. |clock_name| should be one of the names returned by 83 // AvailableClocks. Setting the clock clears the buffer. 84 bool SetClock(const std::string& clock_name); 85 86 // Get the currently set clock. 87 std::string GetClock(); 88 89 // Get all the avaiable clocks. 90 std::set<std::string> AvailableClocks(); 91 92 // Open the raw pipe for |cpu|. 93 virtual base::ScopedFile OpenPipeForCpu(size_t cpu); 94 95 protected: 96 // virtual and public for testing. 97 virtual bool WriteToFile(const std::string& path, const std::string& str); 98 virtual bool ClearFile(const std::string& path); 99 virtual char ReadOneCharFromFile(const std::string& path); 100 virtual std::string ReadFileIntoString(const std::string& path) const; 101 102 private: 103 // Checks the trace file is present at the given root path. 104 static bool CheckRootPath(const std::string& root); 105 106 bool WriteNumberToFile(const std::string& path, size_t value); 107 108 const std::string root_; 109 }; 110 111 } // namespace perfetto 112 113 #endif // SRC_FTRACE_READER_FTRACE_PROCFS_H_ 114