Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dac8b8c authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Eliminate lambdas from tracing helper macro

In the change with ID Ibee1e7dc5021296bdb5871dec59d8d4978fcf0c9,
we introduced lambdas at the tracing sites so that we could
conditionally execute the code to generate the tracing string only when
tracing was enabled. However, it seems like the introduction of the
lambdas, especially ones that capture everything by reference, have
added a performance burden, causing ~5% increase in latency in the input
pipeline.

In this CL, we remove the lambdas from the tracing sites, and instead
rely on the expression evaluation guarantees of the ternary to ensure
that the expression to generate the trace message will only be evaluated
when the condition is true.

Bug: 297462790
Test: Will evaluate perf metric after submitting
Change-Id: I69b37551c4b23256c64544e0ddf4b5b39a9403fb
parent 0376c639
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -19,7 +19,11 @@
#include <utils/Trace.h>
#include <optional>

#define ATRACE_NAME_IF(condition, messageProvider)                                            \
// A macro for tracing when the given condition is true.
// This macro relies on the fact that only one branch of the ternary operator is evaluated. That
// means if `message` is an expression that evaluates to a std::string value, the value will
// not be computed unless the condition is true.
#define ATRACE_NAME_IF(condition, message)                                            \
    const auto _trace_token = condition                                               \
            ? std::make_optional<android::ScopedTrace>(ATRACE_TAG, messageProvider().c_str()) \
            ? std::make_optional<android::ScopedTrace>(ATRACE_TAG, (message).c_str()) \
            : std::nullopt
+26 −33
Original line number Diff line number Diff line
@@ -433,10 +433,10 @@ status_t InputChannel::openInputChannelPair(const std::string& name,
}

status_t InputChannel::sendMessage(const InputMessage* msg) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("sendMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=0x%" PRIx32 ")",
                            mName.c_str(), msg->header.seq, msg->header.type);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("sendMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=0x%" PRIx32
                                ")",
                                mName.c_str(), msg->header.seq, msg->header.type));
    const size_t msgLength = msg->size();
    InputMessage cleanMsg;
    msg->getSanitizedCopy(&cleanMsg);
@@ -472,9 +472,8 @@ status_t InputChannel::sendMessage(const InputMessage* msg) {
}

status_t InputChannel::receiveMessage(InputMessage* msg) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("receiveMessage(inputChannel=%s)", mName.c_str());
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("receiveMessage(inputChannel=%s)", mName.c_str()));
    ssize_t nRead;
    do {
        nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
@@ -580,11 +579,10 @@ status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t
                                         int32_t flags, int32_t keyCode, int32_t scanCode,
                                         int32_t metaState, int32_t repeatCount, nsecs_t downTime,
                                         nsecs_t eventTime) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
                                mChannel->getName().c_str(), KeyEvent::actionToString(action),
                            KeyEvent::getLabel(keyCode));
    });
                                KeyEvent::getLabel(keyCode)));
    ALOGD_IF(debugTransportPublisher(),
             "channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
             "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d,"
@@ -626,11 +624,10 @@ status_t InputPublisher::publishMotionEvent(
        const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
        uint32_t pointerCount, const PointerProperties* pointerProperties,
        const PointerCoords* pointerCoords) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
                                mChannel->getName().c_str(),
                            MotionEvent::actionToString(action).c_str());
    });
                                MotionEvent::actionToString(action).c_str()));
    if (verifyEvents()) {
        Result<void> result =
                mInputVerifier.processMovement(deviceId, action, pointerCount, pointerProperties,
@@ -709,10 +706,9 @@ status_t InputPublisher::publishMotionEvent(
}

status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
                            mChannel->getName().c_str(), toString(hasFocus));
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
                                mChannel->getName().c_str(), toString(hasFocus)));
    ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: seq=%u, id=%d, hasFocus=%s",
             mChannel->getName().c_str(), __func__, seq, eventId, toString(hasFocus));

@@ -726,10 +722,9 @@ status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool h

status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
                                             bool pointerCaptureEnabled) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
                            mChannel->getName().c_str(), toString(pointerCaptureEnabled));
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
                                mChannel->getName().c_str(), toString(pointerCaptureEnabled)));
    ALOGD_IF(debugTransportPublisher(),
             "channel '%s' publisher ~ %s: seq=%u, id=%d, pointerCaptureEnabled=%s",
             mChannel->getName().c_str(), __func__, seq, eventId, toString(pointerCaptureEnabled));
@@ -744,10 +739,9 @@ status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,

status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
                                          bool isExiting) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
                            mChannel->getName().c_str(), x, y, toString(isExiting));
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
                                mChannel->getName().c_str(), x, y, toString(isExiting)));
    ALOGD_IF(debugTransportPublisher(),
             "channel '%s' publisher ~ %s: seq=%u, id=%d, x=%f, y=%f, isExiting=%s",
             mChannel->getName().c_str(), __func__, seq, eventId, x, y, toString(isExiting));
@@ -763,10 +757,9 @@ status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x
}

status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
                            mChannel->getName().c_str(), toString(isInTouchMode));
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
                                mChannel->getName().c_str(), toString(isInTouchMode)));
    ALOGD_IF(debugTransportPublisher(),
             "channel '%s' publisher ~ %s: seq=%u, id=%d, isInTouchMode=%s",
             mChannel->getName().c_str(), __func__, seq, eventId, toString(isInTouchMode));
+18 −27
Original line number Diff line number Diff line
@@ -114,73 +114,64 @@ TracedInputListener::TracedInputListener(const char* name, InputListenerInterfac

void TracedInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyKey(const NotifyKeyArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyMotion(const NotifyMotionArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifySwitch(const NotifySwitchArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifySensor(const NotifySensorArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

void TracedInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
    constexpr static auto& fnName = __func__;
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("%s::%s(id=0x%" PRIx32 ")", mName, fnName, args.id));
    mInnerListener.notify(args);
}

+13 −16
Original line number Diff line number Diff line
@@ -3183,10 +3183,9 @@ void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
                                                 const std::shared_ptr<Connection>& connection,
                                                 std::shared_ptr<EventEntry> eventEntry,
                                                 const InputTarget& inputTarget) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
                            connection->getInputChannelName().c_str(), eventEntry->id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
                                connection->getInputChannelName().c_str(), eventEntry->id));
    if (DEBUG_DISPATCH_CYCLE) {
        ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, "
              "globalScaleFactor=%f, pointerIds=%s %s",
@@ -3251,10 +3250,9 @@ void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
                                                   const std::shared_ptr<Connection>& connection,
                                                   std::shared_ptr<EventEntry> eventEntry,
                                                   const InputTarget& inputTarget) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")",
                            connection->getInputChannelName().c_str(), eventEntry->id);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")",
                                connection->getInputChannelName().c_str(), eventEntry->id));
    LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK),
                        "No dispatch flags are set for %s", eventEntry->getDescription().c_str());

@@ -3570,10 +3568,9 @@ status_t InputDispatcher::publishMotionEvent(Connection& connection,

void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
                                               const std::shared_ptr<Connection>& connection) {
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
                            connection->getInputChannelName().c_str());
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
                                connection->getInputChannelName().c_str()));
    if (DEBUG_DISPATCH_CYCLE) {
        ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str());
    }
@@ -4147,10 +4144,10 @@ std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent(
    }

    int32_t newId = mIdGenerator.nextId();
    ATRACE_NAME_IF(ATRACE_ENABLED(), [&]() {
        return StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 ").",
                            originalMotionEntry.id, newId);
    });
    ATRACE_NAME_IF(ATRACE_ENABLED(),
                   StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32
                                ").",
                                originalMotionEntry.id, newId));
    std::unique_ptr<MotionEntry> splitMotionEntry =
            std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime,
                                          originalMotionEntry.deviceId, originalMotionEntry.source,