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*/true,
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
StateChangedSubject(const::perfetto::protos::TraceConfig & trace_config,rxcpp::subscriber<PerfettoStateChange> destination,std::shared_ptr<PerfettoConsumer> perfetto_consumer)202 StateChangedSubject(const ::perfetto::protos::TraceConfig& trace_config,
203 rxcpp::subscriber<PerfettoStateChange> destination,
204 std::shared_ptr<PerfettoConsumer> perfetto_consumer)
205 : deferred_start(trace_config.deferred_start()),
206 dest(std::move(destination)),
207 perfetto_consumer_(std::move(perfetto_consumer)) {
208 DCHECK(perfetto_consumer_ != nullptr);
209 }
210
211 private:
212 struct StateChangedError : public std::runtime_error {
StateChangedErroriorap::perfetto::StateChangedSubject::StateChangedError213 explicit StateChangedError(const std::string& what_arg) : std::runtime_error(what_arg) {}
214 };
215
216 std::shared_ptr<PerfettoConsumerHandle> handle_; // non-null after bound_ == true.
217 std::atomic<bool> bound_{false}; // synchronize-with for BindHandle -> OnStateChanged.
218
219 State last_state{State::kIdle};
220 bool deferred_start{false};
221
222 rxcpp::subscriber<PerfettoStateChange> dest;
223 std::shared_ptr<PerfettoConsumer> perfetto_consumer_; // This is never null.
224
DcheckBadStateTransition(State state,bool fail_unless=false) const225 void DcheckBadStateTransition(State state, bool fail_unless = false) const {
226 DCHECK(fail_unless) << "Invalid state transition to " << state << " from " << last_state;
227 }
228
DcheckValidStateTransition(State state)229 void DcheckValidStateTransition(State state) {
230 // State must not be out of range.
231 DCHECK_GE(state, State::kTraceFailed);
232 DCHECK_LE(state, State::kTraceEnded);
233
234 // Internal state that should never leak out into public perfetto API:
235 DCHECK_NE(state, State::kIdle);
236 // These can only be returned by PollState:
237 DCHECK_NE(state, State::kSessionNotFound);
238
239 // Validate state transitions as per the perfetto API contract.
240 // See the 'state diagram' in consumer_api.h
241 switch (last_state) {
242 case State::kTraceFailed: // Final and unrecoverable.
243 // b/122548195: this can transition to 'kConnectionError' if selinux is disabled.
244 if (state == State::kConnectionError) {
245 LOG(WARNING) << "b/122548195: kTraceFailed is non-terminal, ignoring.";
246 // This is a bit awkward: rxcpp will drop the #on_error calls if its more than once.
247 break;
248 }
249 DcheckBadStateTransition(state);
250 break;
251 case State::kConnectionError: // Final and unrecoverable.
252 DcheckBadStateTransition(state);
253 break;
254 case State::kSessionNotFound:
255 DcheckBadStateTransition(state);
256 break;
257 case State::kIdle:
258 // OK: we initialized our own state to idle prior to the first callback.
259 break;
260 case State::kConnecting:
261 switch (state) {
262 case State::kConfigured:
263 // kConfigured, if |deferred_start| == true in the trace config.
264 DcheckBadStateTransition(state, deferred_start);
265 break;
266 case State::kTracing:
267 // kTracing, if |deferred_start| == false.
268 DcheckBadStateTransition(state, !deferred_start);
269 break;
270 case State::kConnectionError:
271 // An error state, e.g. if cannot reach the traced daemon.
272 break;
273 default:
274 // Unconditionally invalid state transitions from kConnecting to anything else.
275 DcheckBadStateTransition(state);
276 }
277 break;
278 case State::kConfigured:
279 DCHECK(deferred_start);
280 if (state != State::kTracing // OK: this is documented.
281 && state != State::kTraceFailed) { // Undocumented selinux failure.
282 // Undocumented, but it appears to go directly from Configured->TraceEnded
283 // it can also go to kTraceFailed if e.g. there's an selinux violation
284 // however this appears to be underdocumented.
285 // b/122607276 #2
286
287 if (state != State::kTraceEnded) { // b/122607276 #1
288 DcheckBadStateTransition(state);
289 }
290 }
291 break;
292 case State::kTracing:
293 switch (state) {
294 case State::kTraceEnded:
295 break;
296 case State::kTraceFailed:
297 break;
298 default:
299 DcheckBadStateTransition(state);
300 }
301 break;
302 case State::kTraceEnded:
303 // Cannot transition from terminal state to another state.
304 DcheckBadStateTransition(state);
305 break;
306
307 // default: This list is exhaustive
308 }
309 }
310
IsTerminalState() const311 constexpr bool IsTerminalState() const {
312 switch (last_state) {
313 case State::kTraceFailed:
314 case State::kConnectionError:
315 case State::kTraceEnded:
316 return true;
317 default:
318 return false;
319 }
320 }
321
322 // Returns true for non-terminal states (i.e. this callback will be invoked again).
323 // Returns false otherwise.
OnStateChanged(Handle handle,State state)324 bool OnStateChanged(Handle handle, State state) {
325 using namespace ::perfetto::consumer;
326
327 // Block until 'BoundHandle' is called by the other thread.
328 while (!bound_.load()) {} // seq_cst acquire.
329
330 std::shared_ptr<PerfettoConsumerHandle> handle_ptr = handle_;
331 DCHECK(handle_ptr != nullptr);
332
333 DCHECK_EQ(handle_ptr->GetHandle(), handle);
334 DcheckValidStateTransition(state);
335
336 switch (state) {
337 // Error states (terminal).
338 case State::kTraceFailed:
339 EmitError("kTraceFailed");
340 break;
341 case State::kConnectionError:
342 EmitError("kConnectionError");
343 break;
344
345 // Regular transitions (non-terminal).
346 case State::kConnecting:
347 case State::kConfigured:
348 case State::kTracing:
349 EmitNext(state);
350 break;
351 // Regular transitions (terminal).
352 case State::kTraceEnded: // XX: do we even need to emit the 'TraceEnded' state?
353 EmitNext(state);
354 dest.on_completed();
355 break;
356 default:
357 DcheckBadStateTransition(state);
358 }
359
360 bool force_non_terminal = false;
361
362 if (last_state == State::kConfigured && state == State::kConnectionError) {
363 // b/122548195: this can transition to 'kConnectionError' if selinux is disabled.
364 force_non_terminal = true;
365 // This function must 'return true' in this buggy case, otherwise we will
366 // call the destructor too early and subsequent callbacks will crash.
367 }
368
369 // Remember the state to validate prior state transitions.
370 last_state = state;
371
372 // The owner of this class should avoid leaking memory once we reach a terminal state.
373 return !IsTerminalState() || force_non_terminal;
374 }
375
376 public:
377 // Thread safety: Called by main thread, terminates the rx stream.
378 // When this function is invoked, no calls to this class from other threads can occur.
OnCreateFailed()379 void OnCreateFailed() {
380 // returned when an invalid handle is passed to PollState().
381 last_state = State::kSessionNotFound;
382 EmitError("Create returned kInvalidHandle");
383 }
384
385 // Thread safety: Called by main thread, this could be concurrent to
386 // 'CallbackOnStateChanged'.
BindHandle(const std::shared_ptr<PerfettoConsumerHandle> & handle)387 void BindHandle(const std::shared_ptr<PerfettoConsumerHandle>& handle) {
388 handle_ = handle;
389
390 // Unblock OnStateChanged.
391 bound_.store(true); // seq_cst release.
392 }
393
394 // Thread safety: Called by libperfetto background thread (same one every time).
CallbackOnStateChanged(Handle handle,State state,void * callback_arg)395 static void CallbackOnStateChanged(Handle handle, State state, void* callback_arg) {
396 LOG(VERBOSE) << "CallbackOnStateChanged(handle=" << handle << ",state=" << state
397 << ",callback_arg=" << callback_arg << ")";
398
399 // Validate OnStateChanged callback invariants, guaranteed by libperfetto.
400 DCHECK_NE(handle, ::perfetto::consumer::kInvalidHandle);
401
402 // Note: Perfetto guarantees this callback always occurs on the same thread,
403 // so we don't need to do any extra thread synchronization here since we are only mutating
404 // StateChangedSubject from within this function.
405
406 // TODO: the memory ordering guarantees should be explicitly specified in consumer_api.h:
407 // This isn't specific enough:
408 // "The callback will be invoked on an internal thread and must not block."
409 // However looking at the implementation it posts onto a single-thread task runner,
410 // so this must be the case.
411
412 StateChangedSubject* state_subject = reinterpret_cast<StateChangedSubject*>(callback_arg);
413 // This current thread owns 'StateChangedSubject', no other threads must access it.
414 // Explicit synchronization is not necessary.
415
416 if (!state_subject->OnStateChanged(handle, state)) {
417 // Clean up the state tracker when we reach a terminal state.
418 // This means that no future callbacks will occur anymore.
419 delete state_subject;
420 }
421 }
422
423 private:
EmitError(const std::string & msg)424 void EmitError(const std::string& msg) {
425 // Sidenote: Exact error class does not matter, rxcpp only lets us access the error
426 // as a string (rxcpp::util::what).
427 //
428 // Either way, the recovery strategy is identical (log then try and restart).
429 dest.on_error(rxcpp::util::make_error_ptr(StateChangedError{msg}));
430 }
431
EmitNext(State state)432 void EmitNext(State state) {
433 if (WOULD_LOG(VERBOSE) && !dest.is_subscribed()) {
434 // This is purely for logging: #on_next already filters out items after unsubscription.
435 LOG(VERBOSE) << "StateChangedSubject#EmitNext(" << state << ") - drop due to unsubscribe";
436 }
437
438 auto handle_ptr = handle_;
439 DCHECK(handle_ptr != nullptr);
440
441 // Non-null guarantee for the items emitted into this stream.
442 PerfettoStateChange state_change{state, handle_ptr};
443 dest.on_next(std::move(state_change));
444 }
445
446 // TODO: inherit from rx subject and handle #unsubscribe explicitly, instead
447 // of just being subject-like?
448 };
449
450 // Note: The states will be emitted on a separate thread, so e.g. #as_blocking()
451 // needs to be used to avoid dropping everything on the floor.
452 //
453 // Important: The #on_error case must be handled explicitly by the observable,
454 // because the default behavior is to 'throw' which will cause an std::terminate with -fno-except.
455 static auto /*[observable<State>, shared_ptr<PerfettoConsumerHandle>]*/
CreatePerfettoStateStream(::perfetto::protos::TraceConfig perfetto_config,std::shared_ptr<PerfettoConsumer> perfetto_consumer)456 CreatePerfettoStateStream(::perfetto::protos::TraceConfig perfetto_config,
457 std::shared_ptr<PerfettoConsumer> perfetto_consumer) {
458 auto obs = rxcpp::observable<>::create<PerfettoStateChange>(
459 [perfetto_config = std::move(perfetto_config), perfetto_consumer = std::move(perfetto_consumer)]
460 (rxcpp::subscriber<PerfettoStateChange> subscriber) {
461 std::unique_ptr<StateChangedSubject> state_subject{
462 new StateChangedSubject{perfetto_config, subscriber, perfetto_consumer}};
463
464 // Perfetto API requires a pointer to a serialized protobuf, it doesn't accept
465 // the code-generated object.
466 std::string perfetto_config_str = perfetto_config.SerializeAsString();
467
468 ::perfetto::consumer::Handle handle =
469 perfetto_consumer->Create(perfetto_config_str.data(),
470 perfetto_config_str.size(),
471 // executes on the same background thread repeatedly.
472 &StateChangedSubject::CallbackOnStateChanged,
473 // inter-thread-move
474 reinterpret_cast<void*>(state_subject.get()));
475 // perfetto::consumer::Create synchronizes-with OnStateChanged callback, this means
476 // we don't need to explicitly synchronize state_subject here so long as we don't access
477 // it on this thread again.
478 LOG(DEBUG) << "Create Perfetto handle " << handle;
479
480 if (handle == ::perfetto::consumer::kInvalidHandle) {
481 LOG(ERROR) << "Failed to create Perfetto handle";
482 // No callbacks will occur, so our thread still owns the state subject.
483 state_subject->OnCreateFailed();
484 return;
485 }
486
487 std::shared_ptr<PerfettoConsumerHandle> safe_handle{
488 new PerfettoConsumerHandle{perfetto_consumer, handle}};
489
490 // Share ownership of the Handle with the StateSubject.
491 // This way we defer calling 'Destroy' until the callback reaches a terminal state
492 // *and* all users of the stream are done with the handle.
493 state_subject->BindHandle(safe_handle);
494
495 // state_subject ownership is taken over by OnStateChanged.
496 // It will also be touched in a separate thread, so we must never access it here again.
497 state_subject.release();
498
499 // 'subscriber#add' is actually a call to register an on_unsubscribe listener.
500 subscriber.add([safe_handle]() {
501 LOG(VERBOSE) << "PerfettoStateChange#unsubscribe";
502
503 // Release our ref-count to the handle.
504 // safe_handle.reset(); // This happens implicitly.
505
506 // TODO: I think this won't handle the case where we need to shut down early.
507 // Need to use the explicit kShutdown for that?
508 });
509
510 // TODO: this would be an excellent place to shuffle the perfetto config protobuf
511 // into a global debug state for dumpsys.
512 });
513
514 return obs;
515 }
516
517 template <typename T>
WriteFullyToFile(const std::string & path,bool follow_symlinks) const518 bool BinaryWireProtobuf<T>::WriteFullyToFile(const std::string& path,
519 bool follow_symlinks) const {
520 // TODO: it would be great if android::base had a string_view overload to avoid copying
521 // data into an std::string.
522
523 // u g o
524 // rw-rw----
525 //
526 // Protobufs can be read/written but not executed.
527 static constexpr const mode_t kMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
528
529 int flags =
530 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
531 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, kMode)));
532
533 if (fd == -1) {
534 PLOG(ERROR) << "BinaryWireProtobuf::WriteFullyToFile open failed";
535 return false;
536 }
537
538 if (!::android::base::WriteFully(fd, data_.data(), size())) {
539 PLOG(ERROR) << "BinaryWireProtobuf::WriteFullyToFile write failed";
540 return CleanUpAfterFailedWrite(path);
541 }
542
543 return true;
544 }
545
546 template <typename T>
CleanUpAfterFailedWrite(const std::string & path)547 bool BinaryWireProtobuf<T>::CleanUpAfterFailedWrite(const std::string& path) {
548 // Something went wrong. Let's not leave a corrupt file lying around.
549 int saved_errno = errno;
550 unlink(path.c_str());
551 errno = saved_errno;
552 return false;
553 }
554
555 template <typename T>
WriteStringToFd(int fd) const556 bool BinaryWireProtobuf<T>::WriteStringToFd(int fd) const {
557 const char* p = reinterpret_cast<const char*>(data_.data());
558 size_t left = size();
559 while (left > 0) {
560 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
561 if (n == -1) {
562 return false;
563 }
564 p += n;
565 left -= n;
566 }
567 return true;
568 }
569
570 // explicit template instantiation.
571 template struct BinaryWireProtobuf<::google::protobuf::MessageLite>;
572 // TODO: refactor this not to need the template instantiation.
573
574 #if defined(__ANDROID__)
575 // Copy of the 2.6.18 kernel header (linux/ioprio.h)
576
577 #define IOPRIO_WHO_PROCESS (1)
578 #define IOPRIO_CLASS_IDLE (3)
579
580 #define IOPRIO_BITS (16)
581 #define IOPRIO_CLASS_SHIFT (13)
582 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
583
584 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
585 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
586 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
587 #endif
588
ioprio_get(int which,int who)589 static int ioprio_get(int which, int who) {
590 return syscall(SYS_ioprio_get, which, who);
591 }
592
ioprio_set(int which,int who,int ioprio)593 static int ioprio_set(int which, int who, int ioprio) {
594 return syscall(SYS_ioprio_set, which, who, ioprio);
595 }
596
597 // An rx Coordination, which will cause a new thread to spawn for each new Worker.
598 //
599 // Idle-class priority is set for the CPU and IO priorities on the new thread.
ObserveOnNewIoThread()600 rxcpp::observe_on_one_worker ObserveOnNewIoThread() {
601 // IO thread factory for idle-priority threads.
602 // Both the CPU scheduler and the IO scheduler are set to idle.
603 //
604 // Use this when needing to schedule disk access from a normal-priority thread onto a
605 // very low priority thread, but not so low that we need to use a BackgroundJobScheduler.
606 struct io_thread_factory {
607 std::thread operator()(std::function<void()> start) const {
608 return std::thread{
609 [start=std::move(start)]() {
610 // Set IO priority to idle.
611 do {
612 int value = ioprio_get(IOPRIO_WHO_PROCESS, /*pid*/0);
613 if (value == -1) {
614 PLOG(ERROR) << "io_thread_factory failed ioprio_get";
615 break; // Can't set the ioprio, we don't know what data to use.
616 }
617
618 int data = IOPRIO_PRIO_DATA(value); // priority level
619 // This appears to be '4' in practice. We may want to raise to
620 // be the highest-priority within the idle class.
621
622 // idle scheduling class. only access disk when nobody else needs disk.
623 int res = ioprio_set(IOPRIO_WHO_PROCESS,
624 /*pid*/0,
625 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, data));
626 if (res < 0) {
627 PLOG(ERROR) << "io_thread_factory failed ioprio_set";
628 break;
629 }
630
631 // Changing the IO priority only has any effect with cfq scheduler:
632 // $> cat /sys/block/sda/queue/scheduler
633 LOG(VERBOSE) << "ioprio_set(WHO_PROCESS, class=IDLE, data=" << data << ")";
634 } while (false);
635
636 // Set CPU priority to idle.
637 do {
638 struct sched_param param{};
639 param.sched_priority = 0; // Required to be statically 0 when used with SCHED_IDLE.
640
641 if (sched_setscheduler(/*pid*/0, // current thread,
642 SCHED_IDLE,
643 /*in*/¶m) != 0) {
644 PLOG(ERROR) << "io_thread_factory failed sched_setscheduler";
645 break;
646 }
647
648 LOG(VERBOSE) << "sched_setscheduler(self, IDLE)";
649 } while (false);
650
651 // XX: if changing the scheduling is too aggressive (i.e. it causes starvation),
652 // we may want to stick with the default class and change the nice (priority) levels
653 // to the minimum.
654
655 // TODO: future work, maybe use cgroups configuration file instead?
656
657 // Call the rxcpp-supplied code.
658 start();
659 }
660 };
661 }
662 };
663
664 static rxcpp::schedulers::scheduler thread_scheduler =
665 rxcpp::schedulers::make_new_thread(io_thread_factory{});
666
667 static rxcpp::observe_on_one_worker observe_on_io_thread{thread_scheduler};
668
669 return observe_on_io_thread;
670 }
671
672 static auto/*observable<PerfettoTraceProto>*/
CreatePerfettoStream(rxcpp::observable<PerfettoStreamCommand> input,std::shared_ptr<PerfettoConsumer> perfetto_consumer,const::perfetto::protos::TraceConfig & trace_config)673 CreatePerfettoStream(rxcpp::observable<PerfettoStreamCommand> input,
674 std::shared_ptr<PerfettoConsumer> perfetto_consumer,
675 const ::perfetto::protos::TraceConfig& trace_config) {
676 // XX: should I also take a scheduler for input here???
677
678 auto /*observable<PerfettoStateChange>*/ perfetto_states =
679 CreatePerfettoStateStream(trace_config, perfetto_consumer);
680
681 using State = ::perfetto::consumer::State;
682
683 auto/*coordinator*/ serialize_coordinator = rxcpp::observe_on_new_thread();
684 // Rx note:
685 // The optimal thing to do would be to have a lock/unlock for an entire subset of a chain.
686 // This would avoid creating new threads, and could also be used to intentionally block
687 // the regular C-callback perfetto thread.
688 //
689 // It seems possible to create a coordinator to lock a single operator in a chain, but this
690 // appears to be unsound. In particular, it doesn't even make life any simpler below because
691 // it would only apply the synchronization to 'zip' but not 'flat_map' which is unsound.
692 //
693 // There is also the built-in 'serialize_new_thread' which seems to create a new thread but
694 // then never actually uses it, that seems unfortunate and wasteful.
695 //
696 // Instead, do the simple thing which is create a new thread and always queue on there.
697 // Execution an action on that worker is itself unsynchronized, but this doesn't matter since
698 // the worker is only backed by 1 thread (no 2 schedulables can be executed concurrently
699 // on the 'observe_new_thread' worker).
700 return input
701 .tap([](PerfettoStreamCommand command) {
702 LOG(VERBOSE) << "CreatePerfettoStreamCommand#tap(command=" << command << ")";
703 })
704 // Input A, thread tA. Input B, thread tB. Continue execution with (A,B) on thread tC.
705 .zip(serialize_coordinator, // rest of chain is also executed on the same thread.
706 perfetto_states)
707 // Note: zip terminates when either of the streams complete.
708 .flat_map(
709 [](std::tuple<PerfettoStreamCommand, PerfettoStateChange> p) {
710 auto& [command, state_change] = p;
711 LOG(VERBOSE) << "CreatePerfettoStream#combine("
712 << command << "," << state_change << ")";
713 if (command == PerfettoStreamCommand::kShutdown) {
714 // Perfetto: Always safe to call ::perfetto::consumer::Destroy
715 // at any time.
716 //
717 // XX: How do we clean up the StateChangedSubject without racing
718 // against the callback? It strikes me that we may need a 'kDestroyed'
719 // state that perfetto can transition to from kConfigured.
720 LOG(VERBOSE) << "Call Perfetto_Consumer->Destroy";
721 state_change.GetConsumer()->Destroy(state_change.GetHandle());
722
723 // XX: Do we even have any guarantees about not getting more callbacks?
724 // We could just say 'there can still be spurious output after Shutdown'
725 // and just ignore it (e.g. Shutdown and immediately unsubscribe).
726 } else if (command == PerfettoStreamCommand::kStartTracing
727 && state_change.state == State::kConfigured) {
728 LOG(VERBOSE) << "Call Perfetto_Consumer->StartTracing";
729 state_change.GetConsumer()->StartTracing(state_change.GetHandle());
730 } else if (command == PerfettoStreamCommand::kStopTracing &&
731 state_change.state == State::kTraceEnded) {
732 // TODO: if perfetto actually had a 'StopTracing' we could call that here.
733 // right now we just pretend it exists, but rely on the config timer instead.
734 ::perfetto::consumer::TraceBuffer trace_buffer =
735 state_change.GetConsumer()->ReadTrace(state_change.GetHandle());
736
737 LOG(VERBOSE) << "Perfetto Trace ended"
738 << ", addr=" << reinterpret_cast<void*>(trace_buffer.begin)
739 << ",size= " << trace_buffer.size;
740
741 PerfettoTraceProto wire_proto{trace_buffer.begin, trace_buffer.size};
742 return rxcpp::observable<>::just(std::move(wire_proto)).as_dynamic();
743 }
744 return rxcpp::observable<>::empty<PerfettoTraceProto>().as_dynamic();
745 }
746 );
747 }
748
operator <<(std::ostream & os,PerfettoStreamCommand c)749 std::ostream& operator<<(std::ostream& os, PerfettoStreamCommand c) {
750 switch (c) {
751 case PerfettoStreamCommand::kStartTracing:
752 os << "kStartTracing";
753 break;
754 case PerfettoStreamCommand::kStopTracing:
755 os << "kStopTracing";
756 break;
757 case PerfettoStreamCommand::kShutdown:
758 os << "kShutdown";
759 break;
760 default:
761 os << "(unknown)";
762 break;
763 }
764 return os;
765 }
766
RxProducerFactory(PerfettoDependencies::Injector & injector)767 RxProducerFactory::RxProducerFactory(PerfettoDependencies::Injector& injector)
768 : injector_(injector) {
769 }
770
771 // TODO: (fruit) maybe this could be streamlined further by avoiding this boilerplate?
CreateTraceStream(rxcpp::observable<PerfettoStreamCommand> commands)772 rxcpp::observable<PerfettoTraceProto> RxProducerFactory::CreateTraceStream(
773 rxcpp::observable<PerfettoStreamCommand> commands) {
774 std::shared_ptr<PerfettoConsumer> perfetto_consumer =
775 injector_.get<std::shared_ptr<PerfettoConsumer>>();
776 const ::perfetto::protos::TraceConfig& trace_config =
777 injector_.get<::perfetto::protos::TraceConfig>();
778
779 DCHECK(perfetto_consumer != nullptr);
780 DCHECK(reinterpret_cast<volatile const void*>(&trace_config) != nullptr);
781
782 return CreatePerfettoStream(commands,
783 perfetto_consumer,
784 trace_config);
785 }
786
787 // For testing/debugging only.
788 //
789 // Saves protobuf results in file name specified by 'arg_output_proto'.
CollectPerfettoTraceBufferImmediately(RxProducerFactory & producer_factory,const std::string & arg_output_proto)790 void CollectPerfettoTraceBufferImmediately(
791 RxProducerFactory& producer_factory,
792 const std::string& arg_output_proto) {
793 LOG(VERBOSE) << "CollectPerfettoTraceBufferImmediately";
794
795 std::shared_ptr<PerfettoConsumer> perfetto_consumer =
796 producer_factory.injector_.get<std::shared_ptr<PerfettoConsumer>>();
797 const ::perfetto::protos::TraceConfig& trace_config =
798 producer_factory.injector_.get<const ::perfetto::protos::TraceConfig&>();
799
800 auto /*observable<PerfettoStateChange>*/ perfetto_states =
801 CreatePerfettoStateStream(trace_config, perfetto_consumer);
802
803 perfetto_states
804 .as_blocking() // Wait for observable to terminate with on_completed or on_error.
805 .subscribe(/*on_next*/[&](auto state_change) {
806 LOG(VERBOSE) << "Perfetto post-processed State change: " << state_change;
807
808 using State = ::perfetto::consumer::State;
809 switch (state_change.state) {
810 case State::kConnecting:
811 LOG(VERBOSE) << "Perfetto Tracing is Connecting";
812 // Transitional state. No-op.
813 break;
814 case State::kConfigured:
815 state_change.GetConsumer()->StartTracing(state_change.GetHandle());
816 break;
817 case State::kTracing:
818 LOG(VERBOSE) << "Perfetto Tracing started";
819 // Transitional state. No-op.
820 break;
821 case State::kTraceEnded: {
822 ::perfetto::consumer::TraceBuffer trace_buffer =
823 state_change.GetConsumer()->ReadTrace(state_change.GetHandle());
824
825 LOG(VERBOSE) << "Perfetto Trace ended"
826 << ", addr=" << reinterpret_cast<void*>(trace_buffer.begin)
827 << ",size= " << trace_buffer.size;
828
829 if (!arg_output_proto.empty()) {
830 std::string trace_buffer_str;
831 trace_buffer_str.resize(trace_buffer.size);
832 std::copy(trace_buffer.begin,
833 trace_buffer.begin + trace_buffer.size,
834 trace_buffer_str.data());
835 if (!android::base::WriteStringToFile(trace_buffer_str, arg_output_proto)) {
836 LOG(ERROR) << "Failed to save TraceBuffer to " << arg_output_proto;
837 } else {
838 LOG(INFO) << "TraceBuffer saved to file: " << arg_output_proto;
839 LOG(INFO);
840 LOG(INFO) << "To print this in a human readable form, execute these commands:";
841 LOG(INFO) << "$> adb pull '" << arg_output_proto << "'";
842 LOG(INFO) << "$> trace_to_text systrace <filename.pb>";
843 }
844 }
845
846 // TODO: something more useful with this TraceBuffer, such as saving it to a file
847 // and printing the output.
848 break;
849 }
850 default:
851 // No other states are possible, because they go to #on_error or cause a dcheck.
852 DCHECK(false) << "Invalid state: " << state_change;
853 }
854
855 //INTENTIONAL_COMPILER_ERROR_HERE // lets make sure this code actually does a trace.
856
857 }, /*on_error*/[](rxcpp::util::error_ptr err) {
858 LOG(ERROR) << "Perfetto post-processed state change failed: " << rxcpp::util::what(err);
859 }, /*on_completed*/[]() {
860 LOG(VERBOSE) << "Perfetto post-processed State #on_completed";
861 });
862 }
863
864
865 } // namespace iorap::perfetto
866