Add synchronous SurfaceFlinger tracing
We want to collect traces without impacting composition so by default
we collect traces in another thread while holding a tracing lock when
current state is copied over to drawing state. But this has some
issues. If the tracing thread is not scheduled before the next frame
is composed, we may miss a state. If we need to collect composition
state then we may access states that are currently being modified by
the drawing thread.
To counter this the caller can pass in a TRACE_SYNC flag which will
force the trace to be collected in the drawing thread after
composition. This can be used by tests which don't care about the perf
impact caused by the sync tracing.
This change also fixes threading bugs highlighted in b/154155922
Finally we add a flag TRACE_BUFFERS to capture buffer latches as
well. This is used for Blast buffer tests.
Bug: 169849887, 153739621
Fixes: 154155922
Test: enable tracing with flags, on hwasan builds
Change-Id: I58512cdc98745398b460fa8e7798eb809bd2b0ae
diff --git a/services/surfaceflinger/SurfaceTracing.h b/services/surfaceflinger/SurfaceTracing.h
index f208eb8..576bba7 100644
--- a/services/surfaceflinger/SurfaceTracing.h
+++ b/services/surfaceflinger/SurfaceTracing.h
@@ -32,25 +32,31 @@
namespace android {
class SurfaceFlinger;
-
constexpr auto operator""_MB(unsigned long long const num) {
return num * 1024 * 1024;
}
/*
- * SurfaceTracing records layer states during surface flinging.
+ * SurfaceTracing records layer states during surface flinging. Manages tracing state and
+ * configuration.
*/
class SurfaceTracing {
public:
- explicit SurfaceTracing(SurfaceFlinger& flinger);
+ SurfaceTracing(SurfaceFlinger& flinger);
bool enable();
bool disable();
status_t writeToFile();
bool isEnabled() const;
+ /*
+ * Adds a trace entry, must be called from the drawing thread or while holding the
+ * SurfaceFlinger tracing lock.
+ */
void notify(const char* where);
- void notifyLocked(const char* where) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */;
+ /*
+ * Adds a trace entry, called while holding the SurfaceFlinger tracing lock.
+ */
+ void notifyLocked(const char* where) /* REQUIRES(mSfLock) */;
- void setBufferSize(size_t bufferSizeInByte);
- void writeToFileAsync();
+ void setBufferSize(size_t bufferSizeInBytes) { mConfig.bufferSize = bufferSizeInBytes; }
void dump(std::string& result) const;
enum : uint32_t {
@@ -59,18 +65,34 @@
TRACE_COMPOSITION = 1 << 2,
TRACE_EXTRA = 1 << 3,
TRACE_HWC = 1 << 4,
- TRACE_ALL = 0xffffffff
+ // Add non-geometry composition changes to the trace.
+ TRACE_BUFFERS = 1 << 5,
+ // Add entries from the drawing thread post composition.
+ TRACE_SYNC = 1 << 6,
+ TRACE_ALL = TRACE_CRITICAL | TRACE_INPUT | TRACE_COMPOSITION | TRACE_EXTRA,
};
- void setTraceFlags(uint32_t flags);
- bool flagIsSetLocked(uint32_t flags) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */ {
- return (mTraceFlags & flags) == flags;
- }
+ void setTraceFlags(uint32_t flags) { mConfig.flags = flags; }
+ bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; }
private:
- static constexpr auto kDefaultBufferCapInByte = 5_MB;
- static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
+ class Runner;
+ static constexpr auto DEFAULT_BUFFER_SIZE = 5_MB;
+ static constexpr auto DEFAULT_FILE_NAME = "/data/misc/wmtrace/layers_trace.pb";
- class LayersTraceBuffer { // ring buffer
+ SurfaceFlinger& mFlinger;
+ mutable std::mutex mTraceLock;
+ bool mEnabled = false;
+ std::unique_ptr<Runner> runner;
+
+ struct Config {
+ uint32_t flags = TRACE_CRITICAL | TRACE_INPUT;
+ size_t bufferSize = DEFAULT_BUFFER_SIZE;
+ } mConfig;
+
+ /*
+ * ring buffer.
+ */
+ class LayersTraceBuffer {
public:
size_t size() const { return mSizeInBytes; }
size_t used() const { return mUsedInBytes; }
@@ -83,35 +105,59 @@
private:
size_t mUsedInBytes = 0U;
- size_t mSizeInBytes = 0U;
+ size_t mSizeInBytes = DEFAULT_BUFFER_SIZE;
std::queue<LayersTraceProto> mStorage;
};
- void mainLoop();
- bool addFirstEntry();
- LayersTraceProto traceWhenNotified();
- LayersTraceProto traceLayersLocked(const char* where) REQUIRES(mSfLock);
+ /*
+ * Implements a synchronous way of adding trace entries. This must be called
+ * from the drawing thread.
+ */
+ class Runner {
+ public:
+ Runner(SurfaceFlinger& flinger, SurfaceTracing::Config& config);
+ virtual ~Runner() = default;
+ virtual status_t stop();
+ virtual status_t writeToFile();
+ virtual void notify(const char* where);
+ /* Cannot be called with a synchronous runner. */
+ virtual void notifyLocked(const char* /* where */) {}
+ void dump(std::string& result) const;
- // Returns true if trace is enabled.
- bool addTraceToBuffer(LayersTraceProto& entry);
- void writeProtoFileLocked() REQUIRES(mTraceLock);
+ protected:
+ bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; }
+ SurfaceFlinger& mFlinger;
+ SurfaceTracing::Config mConfig;
+ SurfaceTracing::LayersTraceBuffer mBuffer;
+ uint32_t mMissedTraceEntries = 0;
+ LayersTraceProto traceLayers(const char* where);
+ };
- SurfaceFlinger& mFlinger;
- status_t mLastErr = NO_ERROR;
- std::thread mThread;
- std::condition_variable mCanStartTrace;
+ /*
+ * Implements asynchronous way to add trace entries called from a separate thread while holding
+ * the SurfaceFlinger tracing lock. Trace entries may be missed if the tracing thread is not
+ * scheduled in time.
+ */
+ class AsyncRunner : public Runner {
+ public:
+ AsyncRunner(SurfaceFlinger& flinger, SurfaceTracing::Config& config, std::mutex& sfLock);
+ virtual ~AsyncRunner() = default;
+ status_t stop() override;
+ status_t writeToFile() override;
+ void notify(const char* where) override;
+ void notifyLocked(const char* where);
- std::mutex& mSfLock;
- uint32_t mTraceFlags GUARDED_BY(mSfLock) = TRACE_CRITICAL | TRACE_INPUT;
- const char* mWhere GUARDED_BY(mSfLock) = "";
- uint32_t mMissedTraceEntries GUARDED_BY(mSfLock) = 0;
- bool mTracingInProgress GUARDED_BY(mSfLock) = false;
-
- mutable std::mutex mTraceLock;
- LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock);
- size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte;
- bool mEnabled GUARDED_BY(mTraceLock) = false;
- bool mWriteToFile GUARDED_BY(mTraceLock) = false;
+ private:
+ std::mutex& mSfLock;
+ std::condition_variable mCanStartTrace;
+ std::thread mThread;
+ const char* mWhere = "";
+ bool mWriteToFile = false;
+ bool mEnabled = false;
+ bool mAddEntry = false;
+ void loop();
+ bool traceWhenNotified(LayersTraceProto* outProto);
+ };
};
} // namespace android