1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "common/debug.h"
16 #include "common/expected.h"
17 #include "perfetto/rx_producer.h"
18
19 #include <android-base/file.h>
20 #include <android-base/properties.h>
21 #include <android-base/unique_fd.h>
22
23 #include <iostream>
24
25 #include <sched.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <syscall.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 // TODO: move to perfetto code
33 namespace perfetto {
34 namespace consumer {
35
operator <<(std::ostream & os,State state)36 std::ostream& operator<<(std::ostream& os, State state) {
37 switch (state) {
38 case State::kTraceFailed:
39 os << "kTraceFailed";
40 break;
41 case State::kConnectionError:
42 os << "kConnectionError";
43 break;
44 case State::kSessionNotFound:
45 os << "kSessionNotFound";
46 break;
47 case State::kIdle:
48 os << "kIdle";
49 break;
50 case State::kConnecting:
51 os << "kConnecting";
52 break;
53 case State::kConfigured:
54 os << "kConfigured";
55 break;
56 case State::kTracing:
57 os << "kTracing";
58 break;
59 case State::kTraceEnded:
60 os << "kTraceEnded";
61 break;
62 default:
63 os << "(unknown)"; // did someone forget to update this code?
64 break;
65 }
66 return os;
67 }
68
69 } // namespace consumer
70 } // namespace perfetto
71
72 namespace iorap::perfetto {
73
CreateComponent()74 PerfettoDependencies::Component PerfettoDependencies::CreateComponent() {
75 // TODO: read from config.
76 static const uint32_t kTraceDurationMs =
77 ::android::base::GetUintProperty("iorapd.perfetto.trace_duration_ms", /*default*/5000U);
78
79 static const uint32_t kBufferSize =
80 ::android::base::GetUintProperty("iorapd.perfetto.buffer_size", /*default*/4096U);
81
82 return fruit::createComponent()
83 .bind<PerfettoConsumer, PerfettoConsumerImpl>()
84 .registerProvider([]() /* -> TraceConfig */ {
85 return CreateConfig(kTraceDurationMs,
86 /*deferred_start*/false,
87 kBufferSize);
88 });
89 }
90
CreateConfig(uint32_t duration_ms,bool deferred_start,uint32_t buffer_size)91 ::perfetto::protos::TraceConfig PerfettoDependencies::CreateConfig(uint32_t duration_ms,
92 bool deferred_start,
93 uint32_t buffer_size) {
94 ::perfetto::protos::TraceConfig trace_config;
95
96 trace_config.set_duration_ms(duration_ms);
97 trace_config.add_buffers()->set_size_kb(buffer_size);
98 trace_config.set_deferred_start(deferred_start);
99
100 auto* ds_config = trace_config.add_data_sources()->mutable_config();
101 ds_config->set_name("linux.ftrace");
102 ds_config->mutable_ftrace_config()->add_ftrace_events(
103 "mm_filemap_add_to_page_cache");
104 ds_config->mutable_ftrace_config()->add_ftrace_events(
105 "mm_filemap_delete_from_page_cache");
106 ds_config->set_target_buffer(0);
107
108 return trace_config;
109 }
110
111 // RAII-style wrapper around a perfetto handle that calls Destroy
112 // in a thread-safe manner.
113 struct PerfettoConsumerHandle {
114 private:
115 std::shared_ptr<PerfettoConsumer> consumer_;
116 PerfettoConsumer::Handle handle_;
117
118 public:
119 // Takes over ownership of the 'handle'.
120 //
121 // Consumer must not be null.
PerfettoConsumerHandleiorap::perfetto::PerfettoConsumerHandle122 PerfettoConsumerHandle(std::shared_ptr<PerfettoConsumer> consumer,
123 PerfettoConsumer::Handle handle)
124 : consumer_{std::move(consumer)},
125 handle_{std::move(handle)} {
126 DCHECK(consumer_ != nullptr);
127 }
128
GetConsumeriorap::perfetto::PerfettoConsumerHandle129 std::shared_ptr<PerfettoConsumer> GetConsumer() const {
130 return consumer_;
131 }
132
GetHandleiorap::perfetto::PerfettoConsumerHandle133 PerfettoConsumer::Handle GetHandle() const {
134 return handle_;
135 }
136
~PerfettoConsumerHandleiorap::perfetto::PerfettoConsumerHandle137 ~PerfettoConsumerHandle() {
138 LOG(VERBOSE) << "PerfettoConsumerHandle::Destroy(" << handle_ << ")";
139 consumer_->Destroy(handle_);
140 }
141
operator ==iorap::perfetto::PerfettoConsumerHandle142 bool operator==(const PerfettoConsumerHandle& other) const {
143 return handle_ == other.handle_ && consumer_ == other.consumer_;
144 }
145
operator !=iorap::perfetto::PerfettoConsumerHandle146 bool operator!=(const PerfettoConsumerHandle& other) const {
147 return !(*this == other);
148 }
149 };
150
151
152 // Snapshot of a single perfetto OnStateChanged callback.
153 //
154 // Operate on the PerfettoConsumer to further change the state.
155 //
156 // The Handle is kept 'valid' until all references to the PerfettoConsumerHandle
157 // are dropped to 0. This ensures the Handle is not destroyed too early. All
158 // direct usages of 'Handle' must be scoped by the PerfettoConsumerHandle.
159 struct PerfettoStateChange {
160 public:
161 using State = ::perfetto::consumer::State;
162 using Handle = ::perfetto::consumer::Handle;
163
164 State state; // Never invalid.
165 std::shared_ptr<PerfettoConsumerHandle> perfetto_consumer_and_handle; // Never null.
166
167 // Safety: Use only within scope of the PerfettoStateChange.
GetHandleiorap::perfetto::PerfettoStateChange168 Handle GetHandle() const {
169 // TODO: it would be even safer to wrap all the calls to the handle inside a class,
170 // instead of exposing this raw Handle.
171 return perfetto_consumer_and_handle->GetHandle();
172 }
173
GetConsumeriorap::perfetto::PerfettoStateChange174 std::shared_ptr<PerfettoConsumer> GetConsumer() const {
175 return perfetto_consumer_and_handle->GetConsumer();
176 }
177 };
178
operator <<(std::ostream & os,const PerfettoStateChange & state_change)179 std::ostream& operator<<(std::ostream& os, const PerfettoStateChange& state_change) {
180 os << "PerfettoStateChange{" << state_change.state << ","
181 << state_change.GetHandle() << ","
182 << state_change.GetConsumer().get() << "}";
183 return os;
184 }
185
186 // Once created, this acts as a hot observable, emitting 'PerfettoStateChange' transition items.
187 // Only the 'state' will vary, the handle and perfetto_consumer are always the same value.
188 //
189 // Clients only need to handle the success states in #on_next, all failure states will go to
190 // #on_error.
191 //
192 // Upon reaching the appropriate terminal states, either #on_completed or #on_error is called.
193 // No future callbacks will then occur, so this object should be subsequently deleted.
194 //
195 // The Handle is destroyed automatically after the last item is emitted, so it must only be
196 // manipulated from the #on_next callbacks. Do not save the Handle and use it at other times.
197 class StateChangedSubject {
198 public:
199 using State = ::perfetto::consumer::State;
200 using Handle = ::perfetto::consumer::Handle;
201
202 // Static members to solve use-after-free bug.
203 // The object is accessed from not only perfetto thread, but also iorap
204 // thread. Use this global map to manage it.
205 static std::mutex state_subject_mutex_;
206 static std::unordered_map<Handle, StateChangedSubject*> state_subject_map_;
207
StateChangedSubject(const::perfetto::protos::TraceConfig & trace_config,rxcpp::subscriber<PerfettoStateChange> destination,std::shared_ptr<PerfettoConsumer> perfetto_consumer)208 StateChangedSubject(const ::perfetto::protos::TraceConfig& trace_config,
209 rxcpp::subscriber<PerfettoStateChange> destination,
210 std::shared_ptr<PerfettoConsumer> perfetto_consumer)
211 : deferred_start(trace_config.deferred_start()),
212 dest(std::move(destination)),
213 perfetto_consumer_(std::move(perfetto_consumer)) {
214 DCHECK(perfetto_consumer_ != nullptr);
215 }
216
217 private:
218 struct StateChangedError : public std::runtime_error {
StateChangedErroriorap::perfetto::StateChangedSubject::StateChangedError219 explicit StateChangedError(const std::string& what_arg) : std::runtime_error(what_arg) {}
220 };
221
222 std::shared_ptr<PerfettoConsumerHandle> handle_; // non-null after bound_ == true.
223 std::atomic<bool> bound_{false}; // synchronize-with for BindHandle -> OnStateChanged.
224
225 State last_state{State::kIdle};
226 bool deferred_start{false};
227
228 rxcpp::subscriber<PerfettoStateChange> dest;
229 std::shared_ptr<PerfettoConsumer> perfetto_consumer_; // This is never null.
230
DcheckBadStateTransition(State state,bool fail_unless=false) const231 void DcheckBadStateTransition(State state, bool fail_unless = false) const {
232 DCHECK(fail_unless) << "Invalid state transition to " << state << " from " << last_state;
233 }
234
DcheckValidStateTransition(State state)235 void DcheckValidStateTransition(State state) {
236 // State must not be out of range.
237 DCHECK_GE(state, State::kTraceFailed);
238 DCHECK_LE(state, State::kTraceEnded);
239
240 // Internal state that should never leak out into public perfetto API:
241 DCHECK_NE(state, State::kIdle);
242 // These can only be returned by PollState:
243 DCHECK_NE(state, State::kSessionNotFound);
244
245 // Validate state transitions as per the perfetto API contract.
246 // See the 'state diagram' in consumer_api.h
247 switch (last_state) {
248 case State::kTraceFailed: // Final and unrecoverable.
249 // b/122548195: this can transition to 'kConnectionError' if selinux is disabled.
250 if (state == State::kConnectionError) {
251 LOG(WARNING) << "b/122548195: kTraceFailed is non-terminal, ignoring.";
252 // This is a bit awkward: rxcpp will drop the #on_error calls if its more than once.
253 break;
254 }
255 DcheckBadStateTransition(state);
256 break;
257 case State::kConnectionError: // Final and unrecoverable.
258 DcheckBadStateTransition(state);
259 break;
260 case State::kSessionNotFound:
261 DcheckBadStateTransition(state);
262 break;
263 case State::kIdle:
264 // OK: we initialized our own state to idle prior to the first callback.
265 break;
266 case State::kConnecting:
267 switch (state) {
268 case State::kConfigured:
269 // kConfigured, if |deferred_start| == true in the trace config.
270 DcheckBadStateTransition(state, deferred_start);
271 break;
272 case State::kTracing:
273 // kTracing, if |deferred_start| == false.
274 DcheckBadStateTransition(state, !deferred_start);
275 break;
276 case State::kConnectionError:
277 // An error state, e.g. if cannot reach the traced daemon.
278 break;
279 default:
280 // Unconditionally invalid state transitions from kConnecting to anything else.
281 DcheckBadStateTransition(state);
282 }
283 break;
284 case State::kConfigured:
285 DCHECK(deferred_start);
286 if (state != State::kTracing // OK: this is documented.
287 && state != State::kTraceFailed) { // Undocumented selinux failure.
288 // Undocumented, but it appears to go directly from Configured->TraceEnded
289 // it can also go to kTraceFailed if e.g. there's an selinux violation
290 // however this appears to be underdocumented.
291 // b/122607276 #2
292
293 if (state != State::kTraceEnded) { // b/122607276 #1
294 DcheckBadStateTransition(state);
295 }
296 }
297 break;
298 case State::kTracing:
299 switch (state) {
300 case State::kTraceEnded:
301 break;
302 case State::kTraceFailed:
303 break;
304 default:
305 DcheckBadStateTransition(state);
306 }
307 break;
308 case State::kTraceEnded:
309 // Cannot transition from terminal state to another state.
310 DcheckBadStateTransition(state);
311 break;
312
313 // default: This list is exhaustive
314 }
315 }
316
IsTerminalState() const317 constexpr bool IsTerminalState() const {
318 switch (last_state) {
319 case State::kTraceFailed:
320 case State::kConnectionError:
321 case State::kTraceEnded:
322 return true;
323 default:
324 return false;
325 }
326 }
327
328 // Returns true for non-terminal states (i.e. this callback will be invoked again).
329 // Returns false otherwise.
OnStateChanged(Handle handle,State state)330 bool OnStateChanged(Handle handle, State state) {
331 using namespace ::perfetto::consumer;
332
333 // Block until 'BoundHandle' is called by the other thread.
334 while (!bound_.load()) {} // seq_cst acquire.
335
336 std::shared_ptr<PerfettoConsumerHandle> handle_ptr = handle_;
337 DCHECK(handle_ptr != nullptr);
338
339 DCHECK_EQ(handle_ptr->GetHandle(), handle);
340 DcheckValidStateTransition(state);
341
342 switch (state) {
343 // Error states (terminal).
344 case State::kTraceFailed:
345 EmitError("kTraceFailed");
346 break;
347 case State::kConnectionError:
348 EmitError("kConnectionError");
349 break;
350
351 // Regular transitions (non-terminal).
352 case State::kConnecting:
353 case State::kConfigured:
354 case State::kTracing:
355 EmitNext(state);
356 break;
357 // Regular transitions (terminal).
358 case State::kTraceEnded: // XX: do we even need to emit the 'TraceEnded' state?
359 EmitNext(state);
360 dest.on_completed();
361 break;
362 default:
363 DcheckBadStateTransition(state);
364 }
365
366 bool force_non_terminal = false;
367
368 if (last_state == State::kConfigured && state == State::kConnectionError) {
369 // b/122548195: this can transition to 'kConnectionError' if selinux is disabled.
370 force_non_terminal = true;
371 // This function must 'return true' in this buggy case, otherwise we will
372 // call the destructor too early and subsequent callbacks will crash.
373 }
374
375 // Remember the state to validate prior state transitions.
376 last_state = state;
377
378 // The owner of this class should avoid leaking memory once we reach a terminal state.
379 return !IsTerminalState() || force_non_terminal;
380 }
381
382 public:
383 // Thread safety: Called by main thread, terminates the rx stream.
384 // When this function is invoked, no calls to this class from other threads can occur.
OnCreateFailed()385 void OnCreateFailed() {
386 // returned when an invalid handle is passed to PollState().
387 last_state = State::kSessionNotFound;
388 EmitError("Create returned kInvalidHandle");
389 }
390
391 // Thread safety: Called by main thread, this could be concurrent to
392 // 'CallbackOnStateChanged'.
BindHandle(const std::shared_ptr<PerfettoConsumerHandle> & handle)393 void BindHandle(const std::shared_ptr<PerfettoConsumerHandle>& handle) {
394 handle_ = handle;
395
396 // Unblock OnStateChanged.
397 bound_.store(true); // seq_cst release.
398 }
399
400
401 // Called by libperfetto background thread (same one every time) and iorap
402 // thread.
CallbackOnStateChanged(Handle handle,State state,void * callback_arg)403 static void CallbackOnStateChanged(Handle handle, State state, void* callback_arg) {
404 LOG(VERBOSE) << "CallbackOnStateChanged(handle=" << handle << ",state=" << state
405 << ",callback_arg=" << callback_arg << ")";
406
407 // Validate OnStateChanged callback invariants, guaranteed by libperfetto.
408 DCHECK_NE(handle, ::perfetto::consumer::kInvalidHandle);
409
410 // TODO: the memory ordering guarantees should be explicitly specified in consumer_api.h:
411 // This isn't specific enough:
412 // "The callback will be invoked on an internal thread and must not block."
413 // However looking at the implementation it posts onto a single-thread task runner,
414 // so this must be the case.
415
416 // This current thread owns 'StateChangedSubject', no other threads must access it.
417 // Explicit synchronization is not necessary.
418
419 {
420 std::lock_guard<std::mutex> guard(StateChangedSubject::state_subject_mutex_);
421 auto it = StateChangedSubject::state_subject_map_.find(handle);
422 // If the object is already deleted, do nothing.
423 if (it == StateChangedSubject::state_subject_map_.end()) {
424 return;
425 }
426
427 StateChangedSubject* state_subject = it->second;
428 if (!state_subject->OnStateChanged(handle, state)) {
429 // Clean up the state tracker when we reach a terminal state.
430 // This means that no future callbacks will occur anymore.
431 StateChangedSubject::state_subject_map_.erase(it);
432 delete state_subject;
433 }
434 }
435 }
436
437 private:
EmitError(const std::string & msg)438 void EmitError(const std::string& msg) {
439 // Sidenote: Exact error class does not matter, rxcpp only lets us access the error
440 // as a string (rxcpp::util::what).
441 //
442 // Either way, the recovery strategy is identical (log then try and restart).
443 dest.on_error(rxcpp::util::make_error_ptr(StateChangedError{msg}));
444 }
445
EmitNext(State state)446 void EmitNext(State state) {
447 if (WOULD_LOG(VERBOSE) && !dest.is_subscribed()) {
448 // This is purely for logging: #on_next already filters out items after unsubscription.
449 LOG(VERBOSE) << "StateChangedSubject#EmitNext(" << state << ") - drop due to unsubscribe";
450 }
451
452 auto handle_ptr = handle_;
453 DCHECK(handle_ptr != nullptr);
454
455 // Non-null guarantee for the items emitted into this stream.
456 PerfettoStateChange state_change{state, handle_ptr};
457 dest.on_next(std::move(state_change));
458 }
459
460 // TODO: inherit from rx subject and handle #unsubscribe explicitly, instead
461 // of just being subject-like?
462 };
463
464 std::mutex StateChangedSubject::state_subject_mutex_;
465 std::unordered_map<::perfetto::consumer::Handle,
466 StateChangedSubject*> StateChangedSubject::state_subject_map_;
467
468 // Note: The states will be emitted on a separate thread, so e.g. #as_blocking()
469 // needs to be used to avoid dropping everything on the floor.
470 //
471 // Important: The #on_error case must be handled explicitly by the observable,
472 // because the default behavior is to 'throw' which will cause an std::terminate with -fno-except.
473 static auto /*[observable<State>, shared_ptr<PerfettoConsumerHandle>]*/
CreatePerfettoStateStream(::perfetto::protos::TraceConfig perfetto_config,std::shared_ptr<PerfettoConsumer> perfetto_consumer)474 CreatePerfettoStateStream(::perfetto::protos::TraceConfig perfetto_config,
475 std::shared_ptr<PerfettoConsumer> perfetto_consumer) {
476 auto obs = rxcpp::observable<>::create<PerfettoStateChange>(
477 [perfetto_config = std::move(perfetto_config), perfetto_consumer = std::move(perfetto_consumer)]
478 (rxcpp::subscriber<PerfettoStateChange> subscriber) {
479 std::unique_ptr<StateChangedSubject> state_subject{
480 new StateChangedSubject{perfetto_config, subscriber, perfetto_consumer}};
481
482 // Perfetto API requires a pointer to a serialized protobuf, it doesn't accept
483 // the code-generated object.
484 std::string perfetto_config_str = perfetto_config.SerializeAsString();
485
486 ::perfetto::consumer::Handle handle =
487 perfetto_consumer->Create(perfetto_config_str.data(),
488 perfetto_config_str.size(),
489 // executes on the same background thread repeatedly.
490 &StateChangedSubject::CallbackOnStateChanged,
491 // inter-thread-move
492 reinterpret_cast<void*>(state_subject.get()));
493 // perfetto::consumer::Create synchronizes-with OnStateChanged callback, this means
494 // we don't need to explicitly synchronize state_subject here so long as we don't access
495 // it on this thread again.
496 LOG(DEBUG) << "Create Perfetto handle " << handle;
497
498 if (handle == ::perfetto::consumer::kInvalidHandle) {
499 LOG(ERROR) << "Failed to create Perfetto handle";
500 // No callbacks will occur, so our thread still owns the state subject.
501 state_subject->OnCreateFailed();
502 return;
503 }
504
505 {
506 std::lock_guard<std::mutex> guard(StateChangedSubject::state_subject_mutex_);
507 StateChangedSubject::state_subject_map_[handle] = state_subject.get();
508 }
509
510 std::shared_ptr<PerfettoConsumerHandle> safe_handle{
511 new PerfettoConsumerHandle{perfetto_consumer, handle}};
512
513 // Share ownership of the Handle with the StateSubject.
514 // This way we defer calling 'Destroy' until the callback reaches a terminal state
515 // *and* all users of the stream are done with the handle.
516 state_subject->BindHandle(safe_handle);
517
518 // state_subject ownership is taken over by OnStateChanged.
519 // It will also be touched in a separate thread, so we must never access it here again.
520 state_subject.release();
521
522 // 'subscriber#add' is actually a call to register an on_unsubscribe listener.
523 subscriber.add([safe_handle]() {
524 LOG(VERBOSE) << "PerfettoStateChange#unsubscribe";
525
526 // Release our ref-count to the handle.
527 // safe_handle.reset(); // This happens implicitly.
528
529 // TODO: I think this won't handle the case where we need to shut down early.
530 // Need to use the explicit kShutdown for that?
531 });
532
533 // TODO: this would be an excellent place to shuffle the perfetto config protobuf
534 // into a global debug state for dumpsys.
535 });
536
537 return obs;
538 }
539
540 template <typename T>
WriteFullyToFile(const std::string & path,bool follow_symlinks) const541 bool BinaryWireProtobuf<T>::WriteFullyToFile(const std::string& path,
542 bool follow_symlinks) const {
543 // TODO: it would be great if android::base had a string_view overload to avoid copying
544 // data into an std::string.
545
546 // u g o
547 // rw-rw----
548 //
549 // Protobufs can be read/written but not executed.
550 static constexpr const mode_t kMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
551
552 int flags =
553 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
554 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, kMode)));
555
556 if (fd == -1) {
557 PLOG(ERROR) << "BinaryWireProtobuf::WriteFullyToFile open failed";
558 return false;
559 }
560
561 if (!::android::base::WriteFully(fd, data_.data(), size())) {
562 PLOG(ERROR) << "BinaryWireProtobuf::WriteFullyToFile write failed";
563 return CleanUpAfterFailedWrite(path);
564 }
565
566 return true;
567 }
568
569 template <typename T>
CleanUpAfterFailedWrite(const std::string & path)570 bool BinaryWireProtobuf<T>::CleanUpAfterFailedWrite(const std::string& path) {
571 // Something went wrong. Let's not leave a corrupt file lying around.
572 int saved_errno = errno;
573 unlink(path.c_str());
574 errno = saved_errno;
575 return false;
576 }
577
578 template <typename T>
WriteStringToFd(int fd) const579 bool BinaryWireProtobuf<T>::WriteStringToFd(int fd) const {
580 const char* p = reinterpret_cast<const char*>(data_.data());
581 size_t left = size();
582 while (left > 0) {
583 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
584 if (n == -1) {
585 return false;
586 }
587 p += n;
588 left -= n;
589 }
590 return true;
591 }
592
593 template <typename T>
ReadFullyFromFile(const std::string & path,bool follow_symlinks)594 std::optional<BinaryWireProtobuf<T>> BinaryWireProtobuf<T>::ReadFullyFromFile(
595 const std::string& path,
596 bool follow_symlinks) {
597 std::vector<std::byte> data;
598
599 int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
600 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags)));
601 if (fd == -1) {
602 return std::nullopt;
603 }
604
605 if (ReadFdToString(fd.get(), /*out*/&data)) {
606 return BinaryWireProtobuf<T>{std::move(data)};
607 } else {
608 return std::nullopt;
609 }
610 }
611
612 template <typename T>
operator ==(const BinaryWireProtobuf<T> & other) const613 bool BinaryWireProtobuf<T>::operator==(const BinaryWireProtobuf<T>& other) const {
614 if (data_.size() != other.data_.size()) {
615 return false;
616 }
617 return std::equal(data_.begin(), data_.end(), other.data_.begin());
618 }
619
620 template <typename T>
ReadFdToString(int fd,std::vector<std::byte> * content)621 bool BinaryWireProtobuf<T>::ReadFdToString(int fd, /*out*/std::vector<std::byte>* content) {
622 DCHECK(content != nullptr);
623
624 content->clear();
625
626 struct stat sb;
627 if (fstat(fd, /*out*/&sb) != -1 && sb.st_size > 0) {
628 content->reserve(sb.st_size);
629 }
630
631 char buf[BUFSIZ];
632 auto it = content->begin();
633 ssize_t n;
634 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
635 content->insert(it,
636 reinterpret_cast<std::byte*>(&buf[0]),
637 reinterpret_cast<std::byte*>(&buf[n]));
638
639 std::advance(/*inout*/it, static_cast<size_t>(n));
640
641 static_assert(sizeof(char) == sizeof(std::byte), "sanity check for reinterpret cast");
642 }
643 return (n == 0) ? true : false;
644 }
645
646 // explicit template instantiation.
647 template struct BinaryWireProtobuf<::google::protobuf::MessageLite>;
648 // TODO: refactor this not to need the template instantiation.
649
650 // Copy of the 2.6.18 kernel header (linux/ioprio.h)
651
652 #define IOPRIO_WHO_PROCESS (1)
653 #define IOPRIO_CLASS_IDLE (3)
654
655 #define IOPRIO_BITS (16)
656 #define IOPRIO_CLASS_SHIFT (13)
657 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
658
659 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
660 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
661 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
662
ioprio_get(int which,int who)663 static int ioprio_get(int which, int who) {
664 return syscall(SYS_ioprio_get, which, who);
665 }
666
ioprio_set(int which,int who,int ioprio)667 static int ioprio_set(int which, int who, int ioprio) {
668 return syscall(SYS_ioprio_set, which, who, ioprio);
669 }
670
671 // An rx Coordination, which will cause a new thread to spawn for each new Worker.
672 //
673 // Idle-class priority is set for the CPU and IO priorities on the new thread.
ObserveOnNewIoThread()674 rxcpp::observe_on_one_worker ObserveOnNewIoThread() {
675 // IO thread factory for idle-priority threads.
676 // Both the CPU scheduler and the IO scheduler are set to idle.
677 //
678 // Use this when needing to schedule disk access from a normal-priority thread onto a
679 // very low priority thread, but not so low that we need to use a BackgroundJobScheduler.
680 struct io_thread_factory {
681 std::thread operator()(std::function<void()> start) const {
682 return std::thread{
683 [start=std::move(start)]() {
684 // Set IO priority to idle.
685 do {
686 int value = ioprio_get(IOPRIO_WHO_PROCESS, /*pid*/0);
687 if (value == -1) {
688 PLOG(ERROR) << "io_thread_factory failed ioprio_get";
689 break; // Can't set the ioprio, we don't know what data to use.
690 }
691
692 int data = IOPRIO_PRIO_DATA(value); // priority level
693 // This appears to be '4' in practice. We may want to raise to
694 // be the highest-priority within the idle class.
695
696 // idle scheduling class. only access disk when nobody else needs disk.
697 int res = ioprio_set(IOPRIO_WHO_PROCESS,
698 /*pid*/0,
699 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, data));
700 if (res < 0) {
701 PLOG(ERROR) << "io_thread_factory failed ioprio_set";
702 break;
703 }
704
705 // Changing the IO priority only has any effect with cfq scheduler:
706 // $> cat /sys/block/sda/queue/scheduler
707 LOG(VERBOSE) << "ioprio_set(WHO_PROCESS, class=IDLE, data=" << data << ")";
708 } while (false);
709
710 // Set CPU priority to idle.
711 do {
712 struct sched_param param{};
713 param.sched_priority = 0; // Required to be statically 0 when used with SCHED_IDLE.
714
715 if (sched_setscheduler(/*pid*/0, // current thread,
716 SCHED_IDLE,
717 /*in*/¶m) != 0) {
718 PLOG(ERROR) << "io_thread_factory failed sched_setscheduler";
719 break;
720 }
721
722 LOG(VERBOSE) << "sched_setscheduler(self, IDLE)";
723 } while (false);
724
725 // XX: if changing the scheduling is too aggressive (i.e. it causes starvation),
726 // we may want to stick with the default class and change the nice (priority) levels
727 // to the minimum.
728
729 // TODO: future work, maybe use cgroups configuration file instead?
730
731 // Call the rxcpp-supplied code.
732 start();
733 }
734 };
735 }
736 };
737
738 static rxcpp::schedulers::scheduler thread_scheduler =
739 rxcpp::schedulers::make_new_thread(io_thread_factory{});
740
741 static rxcpp::observe_on_one_worker observe_on_io_thread{thread_scheduler};
742
743 return observe_on_io_thread;
744 }
745
746 static auto/*observable<PerfettoTraceProto>*/
CreatePerfettoStream(rxcpp::observable<PerfettoStreamCommand> input,std::shared_ptr<PerfettoConsumer> perfetto_consumer,const::perfetto::protos::TraceConfig & trace_config)747 CreatePerfettoStream(rxcpp::observable<PerfettoStreamCommand> input,
748 std::shared_ptr<PerfettoConsumer> perfetto_consumer,
749 const ::perfetto::protos::TraceConfig& trace_config) {
750 // XX: should I also take a scheduler for input here???
751
752 auto /*observable<PerfettoStateChange>*/ perfetto_states =
753 CreatePerfettoStateStream(trace_config, perfetto_consumer);
754
755 using State = ::perfetto::consumer::State;
756
757 auto/*coordinator*/ serialize_coordinator = rxcpp::observe_on_new_thread();
758 // Rx note:
759 // The optimal thing to do would be to have a lock/unlock for an entire subset of a chain.
760 // This would avoid creating new threads, and could also be used to intentionally block
761 // the regular C-callback perfetto thread.
762 //
763 // It seems possible to create a coordinator to lock a single operator in a chain, but this
764 // appears to be unsound. In particular, it doesn't even make life any simpler below because
765 // it would only apply the synchronization to 'zip' but not 'flat_map' which is unsound.
766 //
767 // There is also the built-in 'serialize_new_thread' which seems to create a new thread but
768 // then never actually uses it, that seems unfortunate and wasteful.
769 //
770 // Instead, do the simple thing which is create a new thread and always queue on there.
771 // Execution an action on that worker is itself unsynchronized, but this doesn't matter since
772 // the worker is only backed by 1 thread (no 2 schedulables can be executed concurrently
773 // on the 'observe_new_thread' worker).
774 return input
775 .tap([](PerfettoStreamCommand command) {
776 LOG(VERBOSE) << "CreatePerfettoStreamCommand#tap(command=" << command << ")";
777 })
778 // Input A, thread tA. Input B, thread tB. Continue execution with (A,B) on thread tC.
779 .zip(serialize_coordinator, // rest of chain is also executed on the same thread.
780 perfetto_states)
781 // Note: zip terminates when either of the streams complete.
782 .flat_map(
783 [](std::tuple<PerfettoStreamCommand, PerfettoStateChange> p) {
784 auto& [command, state_change] = p;
785 LOG(VERBOSE) << "CreatePerfettoStream#combine("
786 << command << "," << state_change << ")";
787 if (command == PerfettoStreamCommand::kShutdown) {
788 // Perfetto: Always safe to call ::perfetto::consumer::Destroy
789 // at any time.
790 //
791 // XX: How do we clean up the StateChangedSubject without racing
792 // against the callback? It strikes me that we may need a 'kDestroyed'
793 // state that perfetto can transition to from kConfigured.
794 LOG(VERBOSE) << "Call Perfetto_Consumer->Destroy";
795 state_change.GetConsumer()->Destroy(state_change.GetHandle());
796
797 // XX: Do we even have any guarantees about not getting more callbacks?
798 // We could just say 'there can still be spurious output after Shutdown'
799 // and just ignore it (e.g. Shutdown and immediately unsubscribe).
800 } else if (command == PerfettoStreamCommand::kStartTracing
801 && state_change.state == State::kConfigured) {
802 LOG(VERBOSE) << "Call Perfetto_Consumer->StartTracing";
803 state_change.GetConsumer()->StartTracing(state_change.GetHandle());
804 } else if (command == PerfettoStreamCommand::kStopTracing &&
805 state_change.state == State::kTraceEnded) {
806 // TODO: if perfetto actually had a 'StopTracing' we could call that here.
807 // right now we just pretend it exists, but rely on the config timer instead.
808 ::perfetto::consumer::TraceBuffer trace_buffer =
809 state_change.GetConsumer()->ReadTrace(state_change.GetHandle());
810
811 LOG(VERBOSE) << "Perfetto Trace ended"
812 << ", addr=" << reinterpret_cast<void*>(trace_buffer.begin)
813 << ",size= " << trace_buffer.size;
814
815 PerfettoTraceProto wire_proto{trace_buffer.begin, trace_buffer.size};
816 return rxcpp::observable<>::just(std::move(wire_proto)).as_dynamic();
817 }
818 return rxcpp::observable<>::empty<PerfettoTraceProto>().as_dynamic();
819 }
820 );
821 }
822
operator <<(std::ostream & os,PerfettoStreamCommand c)823 std::ostream& operator<<(std::ostream& os, PerfettoStreamCommand c) {
824 switch (c) {
825 case PerfettoStreamCommand::kStartTracing:
826 os << "kStartTracing";
827 break;
828 case PerfettoStreamCommand::kStopTracing:
829 os << "kStopTracing";
830 break;
831 case PerfettoStreamCommand::kShutdown:
832 os << "kShutdown";
833 break;
834 default:
835 os << "(unknown)";
836 break;
837 }
838 return os;
839 }
840
RxProducerFactory(PerfettoDependencies::Injector & injector)841 RxProducerFactory::RxProducerFactory(PerfettoDependencies::Injector& injector)
842 : injector_(injector) {
843 }
844
845 // TODO: (fruit) maybe this could be streamlined further by avoiding this boilerplate?
CreateTraceStream(rxcpp::observable<PerfettoStreamCommand> commands)846 rxcpp::observable<PerfettoTraceProto> RxProducerFactory::CreateTraceStream(
847 rxcpp::observable<PerfettoStreamCommand> commands) {
848 std::shared_ptr<PerfettoConsumer> perfetto_consumer =
849 injector_.get<std::shared_ptr<PerfettoConsumer>>();
850 const ::perfetto::protos::TraceConfig& trace_config =
851 injector_.get<::perfetto::protos::TraceConfig>();
852
853 DCHECK(perfetto_consumer != nullptr);
854 DCHECK(reinterpret_cast<volatile const void*>(&trace_config) != nullptr);
855
856 return CreatePerfettoStream(commands,
857 perfetto_consumer,
858 trace_config);
859 }
860
861 // For testing/debugging only.
862 //
863 // Saves protobuf results in file name specified by 'arg_output_proto'.
CollectPerfettoTraceBufferImmediately(RxProducerFactory & producer_factory,const std::string & arg_output_proto)864 void CollectPerfettoTraceBufferImmediately(
865 RxProducerFactory& producer_factory,
866 const std::string& arg_output_proto) {
867 LOG(VERBOSE) << "CollectPerfettoTraceBufferImmediately";
868
869 std::shared_ptr<PerfettoConsumer> perfetto_consumer =
870 producer_factory.injector_.get<std::shared_ptr<PerfettoConsumer>>();
871 const ::perfetto::protos::TraceConfig& trace_config =
872 producer_factory.injector_.get<const ::perfetto::protos::TraceConfig&>();
873
874 auto /*observable<PerfettoStateChange>*/ perfetto_states =
875 CreatePerfettoStateStream(trace_config, perfetto_consumer);
876
877 perfetto_states
878 .as_blocking() // Wait for observable to terminate with on_completed or on_error.
879 .subscribe(/*on_next*/[&](auto state_change) {
880 LOG(VERBOSE) << "Perfetto post-processed State change: " << state_change;
881
882 using State = ::perfetto::consumer::State;
883 switch (state_change.state) {
884 case State::kConnecting:
885 LOG(VERBOSE) << "Perfetto Tracing is Connecting";
886 // Transitional state. No-op.
887 break;
888 case State::kConfigured:
889 state_change.GetConsumer()->StartTracing(state_change.GetHandle());
890 break;
891 case State::kTracing:
892 LOG(VERBOSE) << "Perfetto Tracing started";
893 // Transitional state. No-op.
894 break;
895 case State::kTraceEnded: {
896 ::perfetto::consumer::TraceBuffer trace_buffer =
897 state_change.GetConsumer()->ReadTrace(state_change.GetHandle());
898
899 LOG(VERBOSE) << "Perfetto Trace ended"
900 << ", addr=" << reinterpret_cast<void*>(trace_buffer.begin)
901 << ",size= " << trace_buffer.size;
902
903 if (!arg_output_proto.empty()) {
904 std::string trace_buffer_str;
905 trace_buffer_str.resize(trace_buffer.size);
906 std::copy(trace_buffer.begin,
907 trace_buffer.begin + trace_buffer.size,
908 trace_buffer_str.data());
909 if (!android::base::WriteStringToFile(trace_buffer_str, arg_output_proto)) {
910 LOG(ERROR) << "Failed to save TraceBuffer to " << arg_output_proto;
911 } else {
912 LOG(INFO) << "TraceBuffer saved to file: " << arg_output_proto;
913 LOG(INFO);
914 LOG(INFO) << "To print this in a human readable form, execute these commands:";
915 LOG(INFO) << "$> adb pull '" << arg_output_proto << "'";
916 LOG(INFO) << "$> trace_to_text systrace <filename.pb>";
917 }
918 }
919
920 // TODO: something more useful with this TraceBuffer, such as saving it to a file
921 // and printing the output.
922 break;
923 }
924 default:
925 // No other states are possible, because they go to #on_error or cause a dcheck.
926 DCHECK(false) << "Invalid state: " << state_change;
927 }
928
929 //INTENTIONAL_COMPILER_ERROR_HERE // lets make sure this code actually does a trace.
930
931 }, /*on_error*/[](rxcpp::util::error_ptr err) {
932 LOG(ERROR) << "Perfetto post-processed state change failed: " << rxcpp::util::what(err);
933 }, /*on_completed*/[]() {
934 LOG(VERBOSE) << "Perfetto post-processed State #on_completed";
935 });
936 }
937
938
939 } // namespace iorap::perfetto
940