diff options
| author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-04-18 17:56:04 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-04-18 17:56:07 +0000 |
| commit | 7b37fa9384cb9ca9a95928af45b03b2bc9fe815c (patch) | |
| tree | 9bddbd819628612f36b7f9e5981fa19ea4645b38 | |
| parent | 5dc0565c1b2413719c35843051f911639e964a96 (diff) | |
| parent | e4ddb3c587d07a8b8459d8202b31c0d9340ce8a1 (diff) | |
Merge "Move bionic_systrace.cpp over to CachedProperty."
| -rw-r--r-- | libc/bionic/bionic_systrace.cpp | 46 | ||||
| -rw-r--r-- | libc/private/CachedProperty.h | 8 |
2 files changed, 16 insertions, 38 deletions
diff --git a/libc/bionic/bionic_systrace.cpp b/libc/bionic/bionic_systrace.cpp index 8e8ff768c..5699f6e7f 100644 --- a/libc/bionic/bionic_systrace.cpp +++ b/libc/bionic/bionic_systrace.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include <cutils/trace.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -23,55 +22,26 @@ #include "private/bionic_lock.h" #include "private/bionic_systrace.h" +#include "private/CachedProperty.h" #include "private/libc_logging.h" -#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ -#include <sys/_system_properties.h> +#include <cutils/trace.h> // For ATRACE_TAG_BIONIC. #define WRITE_OFFSET 32 -constexpr char SYSTRACE_PROPERTY_NAME[] = "debug.atrace.tags.enableflags"; - static Lock g_lock; -static const prop_info* g_pinfo; -static uint32_t g_property_serial = -1; -static uint32_t g_property_area_serial = -1; -static uint64_t g_tags; static int g_trace_marker_fd = -1; static bool should_trace() { - bool result = false; - g_lock.lock(); - // debug.atrace.tags.enableflags is set to a safe non-tracing value during property - // space initialization, so it should only be null in two cases, if there are - // insufficient permissions for this process to access the property, in which - // case an audit will be logged, and during boot before the property server has - // been started, in which case we store the global property_area serial to prevent - // the costly find operation until we see a changed property_area. - if (g_pinfo == nullptr && g_property_area_serial != __system_property_area_serial()) { - g_property_area_serial = __system_property_area_serial(); - g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME); - } + static CachedProperty g_debug_atrace_tags_enableflags("debug.atrace.tags.enableflags"); + static uint64_t g_tags; - if (g_pinfo) { - // Find out which tags have been enabled on the command line and set - // the value of tags accordingly. If the value of the property changes, - // the serial will also change, so the costly system_property_read function - // can be avoided by calling the much cheaper system_property_serial - // first. The values within pinfo may change, but its location is guaranteed - // not to move. - uint32_t cur_serial = __system_property_serial(g_pinfo); - if (cur_serial != g_property_serial) { - __system_property_read_callback(g_pinfo, - [] (void*, const char*, const char* value, uint32_t serial) { - g_property_serial = serial; - g_tags = strtoull(value, nullptr, 0); - }, nullptr); - } - result = ((g_tags & ATRACE_TAG_BIONIC) != 0); + g_lock.lock(); + if (g_debug_atrace_tags_enableflags.DidChange()) { + g_tags = strtoull(g_debug_atrace_tags_enableflags.Get(), nullptr, 0); } g_lock.unlock(); - return result; + return ((g_tags & ATRACE_TAG_BIONIC) != 0); } static int get_trace_marker_fd() { diff --git a/libc/private/CachedProperty.h b/libc/private/CachedProperty.h index f0c81c9d8..417a8553b 100644 --- a/libc/private/CachedProperty.h +++ b/libc/private/CachedProperty.h @@ -46,6 +46,14 @@ class CachedProperty { cached_value_[0] = '\0'; } + // Returns true if the property has been updated (based on the serial rather than the value) + // since the last call to Get. + bool DidChange() { + uint32_t initial_property_serial_ = cached_property_serial_; + Get(); + return (cached_property_serial_ != initial_property_serial_); + } + // Returns the current value of the underlying system property as cheaply as possible. // The returned pointer is valid until the next call to Get. It is the caller's responsibility // to provide a lock for thread-safety. |
