1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
**
** Copyright 2017, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include "perfprofd_binder.h"
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <inttypes.h>
#include <unistd.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <binder/BinderService.h>
#include <binder/IResultReceiver.h>
#include <binder/Status.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <utils/String16.h>
#include <utils/String8.h>
#include <utils/Vector.h>
#include "android/os/BnPerfProfd.h"
#include "perfprofd_config.pb.h"
#include "perfprofd_record.pb.h"
#include "config.h"
#include "configreader.h"
#include "perfprofdcore.h"
#include "perfprofd_threaded_handler.h"
namespace android {
namespace perfprofd {
namespace binder {
namespace {
using Status = ::android::binder::Status;
class PerfProfdNativeService : public BinderService<PerfProfdNativeService>,
public ::android::os::BnPerfProfd,
public ThreadedHandler {
public:
static status_t start();
static int Main();
static char const* getServiceName() { return "perfprofd"; }
status_t dump(int fd, const Vector<String16> &args) override;
Status startProfiling(int32_t collectionInterval,
int32_t iterations,
int32_t process,
int32_t samplingPeriod,
int32_t samplingFrequency,
int32_t sampleDuration,
bool stackProfile,
bool useElfSymbolizer,
bool sendToDropbox) override;
Status startProfilingString(const String16& config) override;
Status startProfilingProtobuf(const std::vector<uint8_t>& config_proto) override;
Status stopProfiling() override;
// Override onTransact so we can handle shellCommand.
status_t onTransact(uint32_t _aidl_code,
const Parcel& _aidl_data,
Parcel* _aidl_reply,
uint32_t _aidl_flags = 0) override;
private:
status_t shellCommand(int /*in*/, int out, int err, Vector<String16>& args);
template <typename ProtoLoaderFn> Status StartProfilingProtobuf(ProtoLoaderFn fn);
Status StartProfilingProtobufFd(int fd);
};
status_t PerfProfdNativeService::start() {
IPCThreadState::self()->disableBackgroundScheduling(true);
status_t ret = BinderService<PerfProfdNativeService>::publish();
if (ret != android::OK) {
return ret;
}
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
ps->giveThreadPoolName();
return android::OK;
}
status_t PerfProfdNativeService::dump(int fd, const Vector<String16> &args) {
auto out = std::fstream(base::StringPrintf("/proc/self/fd/%d", fd));
auto print_config = [&out](bool is_profiling, const Config* config) {
if (is_profiling) {
out << "Profiling with config: " << ConfigReader::ConfigToString(*config);
} else {
out << "Not actively profiling.";
}
};
RunOnConfig(print_config);
out << std::endl;
return NO_ERROR;
}
Status PerfProfdNativeService::startProfiling(int32_t collectionInterval,
int32_t iterations,
int32_t process,
int32_t samplingPeriod,
int32_t samplingFrequency,
int32_t sampleDuration,
bool stackProfile,
bool useElfSymbolizer,
bool sendToDropbox) {
auto config_fn = [&](ThreadedConfig& config) {
config = ThreadedConfig(); // Reset to a default config.
if (collectionInterval >= 0) {
config.collection_interval_in_s = collectionInterval;
}
if (iterations >= 0) {
config.main_loop_iterations = iterations;
}
if (process >= 0) {
config.process = process;
}
if (samplingPeriod > 0) {
config.sampling_period = samplingPeriod;
}
if (samplingFrequency > 0) {
config.sampling_frequency = samplingFrequency;
}
if (sampleDuration > 0) {
config.sample_duration_in_s = sampleDuration;
}
config.stack_profile = stackProfile;
config.use_elf_symbolizer = useElfSymbolizer;
config.send_to_dropbox = sendToDropbox;
};
std::string error_msg;
if (!StartProfiling(config_fn, &error_msg)) {
return Status::fromExceptionCode(1, error_msg.c_str());
}
return Status::ok();
}
Status PerfProfdNativeService::startProfilingString(const String16& config) {
ConfigReader reader;
std::string error_msg;
// Split configuration along colon.
std::vector<std::string> args = base::Split(String8(config).string(), ":");
for (auto& arg : args) {
if (!reader.Read(arg, /* fail_on_error */ true, &error_msg)) {
std::string tmp = base::StringPrintf("Could not parse %s: %s",
arg.c_str(),
error_msg.c_str());
return Status::fromExceptionCode(1, tmp.c_str());
}
}
auto config_fn = [&](ThreadedConfig& config) {
config = ThreadedConfig(); // Reset to a default config.
reader.FillConfig(&config);
};
if (!StartProfiling(config_fn, &error_msg)) {
return Status::fromExceptionCode(1, error_msg.c_str());
}
return Status::ok();
}
Status PerfProfdNativeService::startProfilingProtobuf(const std::vector<uint8_t>& config_proto) {
auto proto_loader_fn = [&config_proto](ProfilingConfig& proto_config) {
return proto_config.ParseFromArray(config_proto.data(), config_proto.size());
};
return StartProfilingProtobuf(proto_loader_fn);
}
template <typename ProtoLoaderFn>
Status PerfProfdNativeService::StartProfilingProtobuf(ProtoLoaderFn fn) {
ProfilingConfig proto_config;
if (!fn(proto_config)) {
return binder::Status::fromExceptionCode(2, "Could not read protobuf");
}
auto config_fn = [&proto_config](ThreadedConfig& config) {
config = ThreadedConfig(); // Reset to a default config.
ConfigReader::ProtoToConfig(proto_config, &config);
};
std::string error_msg;
if (!StartProfiling(config_fn, &error_msg)) {
return Status::fromExceptionCode(1, error_msg.c_str());
}
return Status::ok();
}
Status PerfProfdNativeService::StartProfilingProtobufFd(int fd) {
auto proto_loader_fn = [fd](ProfilingConfig& proto_config) {
struct IstreamCopyingInputStream : public google::protobuf::io::CopyingInputStream {
IstreamCopyingInputStream(int fd_in)
: stream(base::StringPrintf("/proc/self/fd/%d", fd_in),
std::ios::binary | std::ios::in) {
}
int Read(void* buffer, int size) override {
stream.read(reinterpret_cast<char*>(buffer), size);
size_t count = stream.gcount();
if (count > 0) {
return count;
}
return -1;
}
std::ifstream stream;
};
std::unique_ptr<IstreamCopyingInputStream> is(new IstreamCopyingInputStream(fd));
std::unique_ptr<google::protobuf::io::CopyingInputStreamAdaptor> is_adaptor(
new google::protobuf::io::CopyingInputStreamAdaptor(is.get()));
return proto_config.ParseFromZeroCopyStream(is_adaptor.get());
};
return StartProfilingProtobuf(proto_loader_fn);
}
Status PerfProfdNativeService::stopProfiling() {
std::string error_msg;
if (!StopProfiling(&error_msg)) {
Status::fromExceptionCode(1, error_msg.c_str());
}
return Status::ok();
}
status_t PerfProfdNativeService::shellCommand(int in,
int out,
int err_fd,
Vector<String16>& args) {
if (android::base::kEnableDChecks) {
LOG(VERBOSE) << "Perfprofd::shellCommand";
for (size_t i = 0, n = args.size(); i < n; i++) {
LOG(VERBOSE) << " arg[" << i << "]: '" << String8(args[i]).string() << "'";
}
}
auto err_str = std::fstream(base::StringPrintf("/proc/self/fd/%d", err_fd));
if (args.size() >= 1) {
if (args[0] == String16("dump")) {
dump(out, args);
return OK;
} else if (args[0] == String16("startProfiling")) {
ConfigReader reader;
for (size_t i = 1; i < args.size(); ++i) {
std::string error_msg;
if (!reader.Read(String8(args[i]).string(), /* fail_on_error */ true, &error_msg)) {
err_str << "Could not parse '" << String8(args[i]).string() << "': " << error_msg
<< std::endl;
return BAD_VALUE;
}
}
auto config_fn = [&](ThreadedConfig& config) {
config = ThreadedConfig(); // Reset to a default config.
reader.FillConfig(&config);
};
std::string error_msg;
if (!StartProfiling(config_fn, &error_msg)) {
err_str << error_msg << std::endl;
return UNKNOWN_ERROR;
}
return OK;
} else if (args[0] == String16("startProfilingProto")) {
if (args.size() < 2) {
return BAD_VALUE;
}
int fd = -1;
if (args[1] == String16("-")) {
fd = in;
} else {
// TODO: Implement reading from disk?
}
if (fd < 0) {
err_str << "Bad file descriptor " << args[1] << std::endl;
return BAD_VALUE;
}
binder::Status status = StartProfilingProtobufFd(fd);
if (status.isOk()) {
return OK;
} else {
err_str << status.toString8() << std::endl;
return UNKNOWN_ERROR;
}
} else if (args[0] == String16("stopProfiling")) {
Status status = stopProfiling();
if (status.isOk()) {
return OK;
} else {
err_str << status.toString8() << std::endl;
return UNKNOWN_ERROR;
}
}
}
return BAD_VALUE;
}
status_t PerfProfdNativeService::onTransact(uint32_t _aidl_code,
const Parcel& _aidl_data,
Parcel* _aidl_reply,
uint32_t _aidl_flags) {
switch (_aidl_code) {
case IBinder::SHELL_COMMAND_TRANSACTION: {
int in = _aidl_data.readFileDescriptor();
int out = _aidl_data.readFileDescriptor();
int err = _aidl_data.readFileDescriptor();
int argc = _aidl_data.readInt32();
Vector<String16> args;
for (int i = 0; i < argc && _aidl_data.dataAvail() > 0; i++) {
args.add(_aidl_data.readString16());
}
sp<IBinder> unusedCallback;
sp<IResultReceiver> resultReceiver;
status_t status;
if ((status = _aidl_data.readNullableStrongBinder(&unusedCallback)) != OK)
return status;
if ((status = _aidl_data.readNullableStrongBinder(&resultReceiver)) != OK)
return status;
status = shellCommand(in, out, err, args);
if (resultReceiver != nullptr) {
resultReceiver->send(status);
}
return OK;
}
default:
return ::android::os::BnPerfProfd::onTransact(
_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
}
}
} // namespace
int Main() {
{
struct DummyConfig : public Config {
void Sleep(size_t seconds) override {}
bool IsProfilingEnabled() const override { return false; }
};
DummyConfig config;
GlobalInit(config.perf_path);
}
android::status_t ret;
if ((ret = PerfProfdNativeService::start()) != android::OK) {
LOG(ERROR) << "Unable to start InstalldNativeService: %d" << ret;
exit(1);
}
android::IPCThreadState::self()->joinThreadPool();
LOG(INFO) << "Exiting perfprofd";
return 0;
}
} // namespace binder
} // namespace perfprofd
} // namespace android
|