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
diff --git a/include/input/TraceTools.h b/include/input/TraceTools.h
index 70b23c5..81fc7af 100644
--- a/include/input/TraceTools.h
+++ b/include/input/TraceTools.h
@@ -19,7 +19,11 @@
#include <utils/Trace.h>
#include <optional>
-#define ATRACE_NAME_IF(condition, messageProvider) \
- const auto _trace_token = condition \
- ? std::make_optional<android::ScopedTrace>(ATRACE_TAG, messageProvider().c_str()) \
+// 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, (message).c_str()) \
: std::nullopt
diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp
index 5fec1a9..bbbebf7 100644
--- a/libs/input/InputTransport.cpp
+++ b/libs/input/InputTransport.cpp
@@ -433,10 +433,10 @@
}
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::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 @@
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)",
- mChannel->getName().c_str(), KeyEvent::actionToString(action),
- KeyEvent::getLabel(keyCode));
- });
+ ATRACE_NAME_IF(ATRACE_ENABLED(),
+ StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
+ mChannel->getName().c_str(), KeyEvent::actionToString(action),
+ 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 @@
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)",
- mChannel->getName().c_str(),
- MotionEvent::actionToString(action).c_str());
- });
+ ATRACE_NAME_IF(ATRACE_ENABLED(),
+ StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
+ mChannel->getName().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::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::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::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::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));
diff --git a/services/inputflinger/InputListener.cpp b/services/inputflinger/InputListener.cpp
index d319d6d..1f17c16 100644
--- a/services/inputflinger/InputListener.cpp
+++ b/services/inputflinger/InputListener.cpp
@@ -114,73 +114,64 @@
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);
}
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 8b7a99b..d98641e 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -3183,10 +3183,9 @@
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 @@
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 @@
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 @@
}
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,