blob: 332d9934e32532e2f9015a3e9107b1e54bf24e9b [file] [log] [blame]
Hector Dearman0d300332017-11-22 11:05:34 +00001/*
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#include "ftrace_procfs.h"
18
19#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Hector Dearman8d8ccd32017-11-27 16:06:34 +000023#include <fstream>
24#include <sstream>
25#include <string>
26
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080027#include "perfetto_base/logging.h"
28#include "perfetto_base/utils.h"
Hector Dearman0d300332017-11-22 11:05:34 +000029
30namespace perfetto {
31namespace {
32
33// Reading /trace produces human readable trace output.
34// Writing to this file clears all trace buffers for all CPUS.
35
36// Writing to /trace_marker file injects an event into the trace buffer.
37
38// Reading /tracing_on returns 1/0 if tracing is enabled/disabled.
39// Writing 1/0 to this file enables/disables tracing.
40// Disabling tracing with this file prevents further writes but
41// does not clear the buffer.
42
43char ReadOneCharFromFile(const std::string& path) {
44 base::ScopedFile fd(open(path.c_str(), O_RDONLY));
45 PERFETTO_CHECK(fd);
46 char result = '\0';
47 ssize_t bytes = PERFETTO_EINTR(read(fd.get(), &result, 1));
48 PERFETTO_CHECK(bytes == 1 || bytes == -1);
49 return result;
50}
51
Hector Dearman8d8ccd32017-11-27 16:06:34 +000052std::string ReadFileIntoString(std::string path) {
53 std::ifstream fin(path, std::ios::in);
54 if (!fin) {
55 PERFETTO_DLOG("Could not read '%s'", path.c_str());
56 return "";
57 }
58
59 std::string str;
60 // You can't seek or stat the procfs files on Android.
61 // The vast majority (884/886) of format files are under 4k.
62 str.reserve(4096);
63 str.assign(std::istreambuf_iterator<char>(fin),
64 std::istreambuf_iterator<char>());
65
66 return str;
67}
68
Hector Dearman0d300332017-11-22 11:05:34 +000069} // namespace
70
71FtraceProcfs::FtraceProcfs(const std::string& root) : root_(root) {}
72FtraceProcfs::~FtraceProcfs() = default;
73
74bool FtraceProcfs::EnableEvent(const std::string& group,
75 const std::string& name) {
76 std::string path = root_ + "events/" + group + "/" + name + "/enable";
77 return WriteToFile(path, "1");
78}
79
80bool FtraceProcfs::DisableEvent(const std::string& group,
81 const std::string& name) {
82 std::string path = root_ + "events/" + group + "/" + name + "/enable";
83 return WriteToFile(path, "0");
84}
85
Hector Dearman8d8ccd32017-11-27 16:06:34 +000086std::string FtraceProcfs::ReadEventFormat(const std::string& group,
87 const std::string& name) const {
88 std::string path = root_ + "events/" + group + "/" + name + "/format";
89 return ReadFileIntoString(path);
90}
91
92std::string FtraceProcfs::ReadAvailableEvents() const {
93 std::string path = root_ + "available_events";
94 return ReadFileIntoString(path);
95}
96
Hector Dearman0d300332017-11-22 11:05:34 +000097size_t FtraceProcfs::NumberOfCpus() const {
98 static size_t num_cpus = sysconf(_SC_NPROCESSORS_CONF);
99 return num_cpus;
100}
101
102void FtraceProcfs::ClearTrace() {
103 std::string path = root_ + "trace";
104 base::ScopedFile fd(open(path.c_str(), O_WRONLY | O_TRUNC));
105 PERFETTO_CHECK(fd); // Could not clear.
106}
107
108bool FtraceProcfs::WriteTraceMarker(const std::string& str) {
109 std::string path = root_ + "trace_marker";
110 return WriteToFile(path, str);
111}
112
113bool FtraceProcfs::EnableTracing() {
114 std::string path = root_ + "tracing_on";
115 return WriteToFile(path, "1");
116}
117
118bool FtraceProcfs::DisableTracing() {
119 std::string path = root_ + "tracing_on";
120 return WriteToFile(path, "0");
121}
122
123bool FtraceProcfs::IsTracingEnabled() {
124 std::string path = root_ + "tracing_on";
125 return ReadOneCharFromFile(path) == '1';
126}
127
128bool FtraceProcfs::WriteToFile(const std::string& path,
129 const std::string& str) {
130 base::ScopedFile fd(open(path.c_str(), O_WRONLY));
131 if (!fd)
132 return false;
133 ssize_t written = PERFETTO_EINTR(write(fd.get(), str.c_str(), str.length()));
134 ssize_t length = static_cast<ssize_t>(str.length());
135 // This should either fail or write fully.
136 PERFETTO_CHECK(written == length || written == -1);
137 return written == length;
138}
139
140base::ScopedFile FtraceProcfs::OpenPipeForCpu(size_t cpu) {
141 std::string path =
Hector Dearman8d8ccd32017-11-27 16:06:34 +0000142 root_ + "per_cpu/cpu" + std::to_string(cpu) + "/trace_pipe_raw";
Hector Dearman0d300332017-11-22 11:05:34 +0000143 return base::ScopedFile(open(path.c_str(), O_RDONLY));
144}
145
146} // namespace perfetto