Merge "liblp: delete unused function and fields"
diff --git a/TEST_MAPPING b/TEST_MAPPING
index a2ed205..7933629 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -28,9 +28,6 @@
"name": "fs_mgr_vendor_overlay_test"
},
{
- "name": "libbase_test"
- },
- {
"name": "libpackagelistparser_test"
},
{
@@ -60,15 +57,6 @@
},
{
"name": "propertyinfoserializer_tests"
- },
- {
- "name": "ziparchive-tests"
- }
- ],
-
- "postsubmit": [
- {
- "name": "ziparchive_tests_large"
}
]
}
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index d2daaa1..8c2e001 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -454,7 +454,8 @@
<< entry.encryption_options;
return;
}
- if ((options.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) != 0) {
+ if ((options.flags &
+ (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) != 0) {
// We can only use this policy on ext4 if the "stable_inodes" feature
// is set on the filesystem, otherwise shrinking will break encrypted files.
if ((sb->s_feature_compat & cpu_to_le32(EXT4_FEATURE_COMPAT_STABLE_INODES)) == 0) {
diff --git a/fs_mgr/libsnapshot/Android.bp b/fs_mgr/libsnapshot/Android.bp
index 2783e4d..95301ff 100644
--- a/fs_mgr/libsnapshot/Android.bp
+++ b/fs_mgr/libsnapshot/Android.bp
@@ -248,10 +248,6 @@
cc_defaults {
name: "libsnapshot_fuzzer_defaults",
-
- // TODO(b/154633114): make host supported.
- // host_supported: true,
-
native_coverage : true,
srcs: [
// Compile the protobuf definition again with type full.
diff --git a/libcutils/Android.bp b/libcutils/Android.bp
index d7c83a2..60400c9 100644
--- a/libcutils/Android.bp
+++ b/libcutils/Android.bp
@@ -152,6 +152,7 @@
"iosched_policy.cpp",
"load_file.cpp",
"native_handle.cpp",
+ "properties.cpp",
"record_stream.cpp",
"strlcpy.c",
"threads.cpp",
@@ -187,7 +188,6 @@
"fs_config.cpp",
"klog.cpp",
"partition_utils.cpp",
- "properties.cpp",
"qtaguid.cpp",
"trace-dev.cpp",
"uevent.cpp",
@@ -268,6 +268,7 @@
name: "libcutils_test_default",
srcs: [
"native_handle_test.cpp",
+ "properties_test.cpp",
"sockets_test.cpp",
],
@@ -280,7 +281,6 @@
"fs_config_test.cpp",
"memset_test.cpp",
"multiuser_test.cpp",
- "properties_test.cpp",
"sched_policy_test.cpp",
"str_parms_test.cpp",
"trace-dev_test.cpp",
diff --git a/libcutils/include/cutils/properties.h b/libcutils/include/cutils/properties.h
index d2e0871..78d8bc6 100644
--- a/libcutils/include/cutils/properties.h
+++ b/libcutils/include/cutils/properties.h
@@ -14,27 +14,30 @@
* limitations under the License.
*/
-#ifndef __CUTILS_PROPERTIES_H
-#define __CUTILS_PROPERTIES_H
+#pragma once
#include <sys/cdefs.h>
#include <stddef.h>
-#include <sys/system_properties.h>
#include <stdint.h>
+#if __has_include(<sys/system_properties.h>)
+#include <sys/system_properties.h>
+#else
+#define PROP_VALUE_MAX 92
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
-/* System properties are *small* name value pairs managed by the
-** property service. If your data doesn't fit in the provided
-** space it is not appropriate for a system property.
-**
-** WARNING: system/bionic/include/sys/system_properties.h also defines
-** these, but with different names. (TODO: fix that)
-*/
-#define PROPERTY_KEY_MAX PROP_NAME_MAX
-#define PROPERTY_VALUE_MAX PROP_VALUE_MAX
+//
+// Deprecated.
+//
+// See <android-base/properties.h> for better API.
+//
+
+#define PROPERTY_KEY_MAX PROP_NAME_MAX
+#define PROPERTY_VALUE_MAX PROP_VALUE_MAX
/* property_get: returns the length of the value which will never be
** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
@@ -146,5 +149,3 @@
#ifdef __cplusplus
}
#endif
-
-#endif
diff --git a/libcutils/properties.cpp b/libcutils/properties.cpp
index 5dbbeba..03f0496 100644
--- a/libcutils/properties.cpp
+++ b/libcutils/properties.cpp
@@ -16,27 +16,19 @@
#include <cutils/properties.h>
-#define LOG_TAG "properties"
-// #define LOG_NDEBUG 0
-
-#include <assert.h>
-#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <cutils/sockets.h>
-#include <log/log.h>
+#include <android-base/properties.h>
-int8_t property_get_bool(const char *key, int8_t default_value) {
- if (!key) {
- return default_value;
- }
+int8_t property_get_bool(const char* key, int8_t default_value) {
+ if (!key) return default_value;
int8_t result = default_value;
- char buf[PROPERTY_VALUE_MAX] = {'\0'};
+ char buf[PROPERTY_VALUE_MAX] = {};
int len = property_get(key, buf, "");
if (len == 1) {
@@ -57,73 +49,53 @@
return result;
}
-// Convert string property to int (default if fails); return default value if out of bounds
-static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
- intmax_t default_value) {
- if (!key) {
- return default_value;
+template <typename T>
+static T property_get_int(const char* key, T default_value) {
+ if (!key) return default_value;
+
+ char value[PROPERTY_VALUE_MAX] = {};
+ if (property_get(key, value, "") < 1) return default_value;
+
+ // libcutils unwisely allows octal, which libbase doesn't.
+ T result = default_value;
+ int saved_errno = errno;
+ errno = 0;
+ char* end = nullptr;
+ intmax_t v = strtoimax(value, &end, 0);
+ if (errno != ERANGE && end != value && v >= std::numeric_limits<T>::min() &&
+ v <= std::numeric_limits<T>::max()) {
+ result = v;
}
-
- intmax_t result = default_value;
- char buf[PROPERTY_VALUE_MAX] = {'\0'};
- char *end = NULL;
-
- int len = property_get(key, buf, "");
- if (len > 0) {
- int tmp = errno;
- errno = 0;
-
- // Infer base automatically
- result = strtoimax(buf, &end, /*base*/ 0);
- if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
- // Over or underflow
- result = default_value;
- ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
- } else if (result < lower_bound || result > upper_bound) {
- // Out of range of requested bounds
- result = default_value;
- ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
- } else if (end == buf) {
- // Numeric conversion failed
- result = default_value;
- ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", __FUNCTION__, key,
- default_value);
- }
-
- errno = tmp;
- }
-
+ errno = saved_errno;
return result;
}
-int64_t property_get_int64(const char *key, int64_t default_value) {
- return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
+int64_t property_get_int64(const char* key, int64_t default_value) {
+ return property_get_int<int64_t>(key, default_value);
}
-int32_t property_get_int32(const char *key, int32_t default_value) {
- return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
+int32_t property_get_int32(const char* key, int32_t default_value) {
+ return property_get_int<int32_t>(key, default_value);
}
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-#include <sys/_system_properties.h>
-
-int property_set(const char *key, const char *value) {
+int property_set(const char* key, const char* value) {
return __system_property_set(key, value);
}
-int property_get(const char *key, char *value, const char *default_value) {
+int property_get(const char* key, char* value, const char* default_value) {
int len = __system_property_get(key, value);
- if (len > 0) {
- return len;
- }
- if (default_value) {
- len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
- memcpy(value, default_value, len);
- value[len] = '\0';
+ if (len < 1 && default_value) {
+ snprintf(value, PROPERTY_VALUE_MAX, "%s", default_value);
+ return strlen(value);
}
return len;
}
+#if __has_include(<sys/system_properties.h>)
+
+#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
+#include <sys/_system_properties.h>
+
struct callback_data {
void (*callback)(const char* name, const char* value, void* cookie);
void* cookie;
@@ -139,6 +111,8 @@
}
int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
- callback_data data = { fn, cookie };
+ callback_data data = {fn, cookie};
return __system_property_foreach(property_list_callback, &data);
}
+
+#endif
diff --git a/libcutils/properties_test.cpp b/libcutils/properties_test.cpp
index 7921972..efc0183 100644
--- a/libcutils/properties_test.cpp
+++ b/libcutils/properties_test.cpp
@@ -93,160 +93,179 @@
}
};
-TEST_F(PropertiesTest, SetString) {
-
+TEST_F(PropertiesTest, property_set_null_key) {
// Null key -> unsuccessful set
- {
- // Null key -> fails
- EXPECT_GT(0, property_set(/*key*/NULL, PROPERTY_TEST_VALUE_DEFAULT));
- }
+ EXPECT_GT(0, property_set(/*key*/ NULL, PROPERTY_TEST_VALUE_DEFAULT));
+}
- // Null value -> returns default value
- {
- // Null value -> OK , and it clears the value
- EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/NULL));
- ResetValue();
+TEST_F(PropertiesTest, property_set_null_value) {
+ // Null value -> OK, and it clears the value
+ EXPECT_OK(property_set(PROPERTY_TEST_KEY, /*value*/ NULL));
+ ResetValue();
- // Since the value is null, default value will be returned
- size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
- EXPECT_EQ(strlen(PROPERTY_TEST_VALUE_DEFAULT), len);
- EXPECT_STREQ(PROPERTY_TEST_VALUE_DEFAULT, mValue);
- }
+ // Since the value is null, default value will be returned
+ size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
+ EXPECT_EQ(strlen(PROPERTY_TEST_VALUE_DEFAULT), len);
+ EXPECT_STREQ(PROPERTY_TEST_VALUE_DEFAULT, mValue);
+}
+TEST_F(PropertiesTest, property_set) {
// Trivial case => get returns what was set
- {
- size_t len = SetAndGetProperty("hello_world");
- EXPECT_EQ(strlen("hello_world"), len) << "hello_world key";
- EXPECT_STREQ("hello_world", mValue);
- ResetValue();
- }
+ size_t len = SetAndGetProperty("hello_world");
+ EXPECT_EQ(strlen("hello_world"), len) << "hello_world key";
+ EXPECT_STREQ("hello_world", mValue);
+ ResetValue();
+}
+TEST_F(PropertiesTest, property_set_empty) {
// Set to empty string => get returns default always
- {
- const char* EMPTY_STRING_DEFAULT = "EMPTY_STRING";
- size_t len = SetAndGetProperty("", EMPTY_STRING_DEFAULT);
- EXPECT_EQ(strlen(EMPTY_STRING_DEFAULT), len) << "empty key";
- EXPECT_STREQ(EMPTY_STRING_DEFAULT, mValue);
- ResetValue();
- }
+ const char* EMPTY_STRING_DEFAULT = "EMPTY_STRING";
+ size_t len = SetAndGetProperty("", EMPTY_STRING_DEFAULT);
+ EXPECT_EQ(strlen(EMPTY_STRING_DEFAULT), len) << "empty key";
+ EXPECT_STREQ(EMPTY_STRING_DEFAULT, mValue);
+ ResetValue();
+}
+TEST_F(PropertiesTest, property_set_max_length) {
// Set to max length => get returns what was set
- {
- std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
+ std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'a');
- int len = SetAndGetProperty(maxLengthString.c_str());
- EXPECT_EQ(PROPERTY_VALUE_MAX-1, len) << "max length key";
- EXPECT_STREQ(maxLengthString.c_str(), mValue);
- ResetValue();
- }
+ int len = SetAndGetProperty(maxLengthString.c_str());
+ EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len) << "max length key";
+ EXPECT_STREQ(maxLengthString.c_str(), mValue);
+ ResetValue();
+}
+TEST_F(PropertiesTest, property_set_too_long) {
// Set to max length + 1 => set fails
- {
- const char* VALID_TEST_VALUE = "VALID_VALUE";
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, VALID_TEST_VALUE));
+ const char* VALID_TEST_VALUE = "VALID_VALUE";
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, VALID_TEST_VALUE));
- std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
+ std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
- // Expect that the value set fails since it's too long
- EXPECT_GT(0, property_set(PROPERTY_TEST_KEY, oneLongerString.c_str()));
- size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
+ // Expect that the value set fails since it's too long
+ EXPECT_GT(0, property_set(PROPERTY_TEST_KEY, oneLongerString.c_str()));
+ size_t len = property_get(PROPERTY_TEST_KEY, mValue, PROPERTY_TEST_VALUE_DEFAULT);
- EXPECT_EQ(strlen(VALID_TEST_VALUE), len) << "set should've failed";
- EXPECT_STREQ(VALID_TEST_VALUE, mValue);
- ResetValue();
- }
+ EXPECT_EQ(strlen(VALID_TEST_VALUE), len) << "set should've failed";
+ EXPECT_STREQ(VALID_TEST_VALUE, mValue);
+ ResetValue();
}
-TEST_F(PropertiesTest, GetString) {
-
+TEST_F(PropertiesTest, property_get_too_long) {
// Try to use a default value that's too long => get truncates the value
- {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
- std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'a');
- std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
+ std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'a');
+ std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
- // Expect that the value is truncated since it's too long (by 1)
- int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
- EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
- EXPECT_STREQ(maxLengthString.c_str(), mValue);
- ResetValue();
- }
-
- // Try to use a default value that's the max length => get succeeds
- {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
-
- std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'b');
-
- // Expect that the value matches maxLengthString
- int len = property_get(PROPERTY_TEST_KEY, mValue, maxLengthString.c_str());
- EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
- EXPECT_STREQ(maxLengthString.c_str(), mValue);
- ResetValue();
- }
-
- // Try to use a default value of length one => get succeeds
- {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
-
- std::string oneCharString = std::string(1, 'c');
-
- // Expect that the value matches oneCharString
- int len = property_get(PROPERTY_TEST_KEY, mValue, oneCharString.c_str());
- EXPECT_EQ(1, len);
- EXPECT_STREQ(oneCharString.c_str(), mValue);
- ResetValue();
- }
-
- // Try to use a default value of length zero => get succeeds
- {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
-
- std::string zeroCharString = std::string(0, 'd');
-
- // Expect that the value matches oneCharString
- int len = property_get(PROPERTY_TEST_KEY, mValue, zeroCharString.c_str());
- EXPECT_EQ(0, len);
- EXPECT_STREQ(zeroCharString.c_str(), mValue);
- ResetValue();
- }
-
- // Try to use a NULL default value => get returns 0
- {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
-
- // Expect a return value of 0
- int len = property_get(PROPERTY_TEST_KEY, mValue, NULL);
- EXPECT_EQ(0, len);
- ResetValue();
- }
+ // Expect that the value is truncated since it's too long (by 1)
+ int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
+ EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
+ EXPECT_STREQ(maxLengthString.c_str(), mValue);
+ ResetValue();
}
-TEST_F(PropertiesTest, GetBool) {
- /**
- * TRUE
- */
- const char *valuesTrue[] = { "1", "true", "y", "yes", "on", };
- for (size_t i = 0; i < arraysize(valuesTrue); ++i) {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesTrue[i]));
- bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/false);
- EXPECT_TRUE(val) << "Property should've been TRUE for value: '" << valuesTrue[i] << "'";
- }
+TEST_F(PropertiesTest, property_get_default_too_long) {
+ // Try to use a default value that's the max length => get succeeds
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
- /**
- * FALSE
- */
- const char *valuesFalse[] = { "0", "false", "n", "no", "off", };
- for (size_t i = 0; i < arraysize(valuesFalse); ++i) {
- ASSERT_OK(property_set(PROPERTY_TEST_KEY, valuesFalse[i]));
- bool val = property_get_bool(PROPERTY_TEST_KEY, /*default_value*/true);
- EXPECT_FALSE(val) << "Property shoud've been FALSE For string value: '" << valuesFalse[i] << "'";
- }
+ std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'b');
- /**
- * NEITHER
- */
+ // Expect that the value matches maxLengthString
+ int len = property_get(PROPERTY_TEST_KEY, mValue, maxLengthString.c_str());
+ EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
+ EXPECT_STREQ(maxLengthString.c_str(), mValue);
+ ResetValue();
+}
+
+TEST_F(PropertiesTest, property_get_default_okay) {
+ // Try to use a default value of length one => get succeeds
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
+
+ std::string oneCharString = std::string(1, 'c');
+
+ // Expect that the value matches oneCharString
+ int len = property_get(PROPERTY_TEST_KEY, mValue, oneCharString.c_str());
+ EXPECT_EQ(1, len);
+ EXPECT_STREQ(oneCharString.c_str(), mValue);
+ ResetValue();
+}
+
+TEST_F(PropertiesTest, property_get_default_empty) {
+ // Try to use a default value of length zero => get succeeds
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
+
+ std::string zeroCharString = std::string(0, 'd');
+
+ // Expect that the value matches oneCharString
+ int len = property_get(PROPERTY_TEST_KEY, mValue, zeroCharString.c_str());
+ EXPECT_EQ(0, len);
+ EXPECT_STREQ(zeroCharString.c_str(), mValue);
+ ResetValue();
+}
+
+TEST_F(PropertiesTest, property_get_default_NULL) {
+ // Try to use a NULL default value => get returns 0
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
+
+ // Expect a return value of 0
+ int len = property_get(PROPERTY_TEST_KEY, mValue, NULL);
+ EXPECT_EQ(0, len);
+ ResetValue();
+}
+
+TEST_F(PropertiesTest, property_get_bool_0) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "0"));
+ ASSERT_FALSE(property_get_bool(PROPERTY_TEST_KEY, true));
+}
+
+TEST_F(PropertiesTest, property_get_bool_1) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "1"));
+ ASSERT_TRUE(property_get_bool(PROPERTY_TEST_KEY, false));
+}
+
+TEST_F(PropertiesTest, property_get_bool_false) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "false"));
+ ASSERT_FALSE(property_get_bool(PROPERTY_TEST_KEY, true));
+}
+
+TEST_F(PropertiesTest, property_get_bool_n) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "n"));
+ ASSERT_FALSE(property_get_bool(PROPERTY_TEST_KEY, true));
+}
+
+TEST_F(PropertiesTest, property_get_bool_no) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "no"));
+ ASSERT_FALSE(property_get_bool(PROPERTY_TEST_KEY, true));
+}
+
+TEST_F(PropertiesTest, property_get_bool_off) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "off"));
+ ASSERT_FALSE(property_get_bool(PROPERTY_TEST_KEY, true));
+}
+
+TEST_F(PropertiesTest, property_get_bool_on) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "on"));
+ ASSERT_TRUE(property_get_bool(PROPERTY_TEST_KEY, false));
+}
+
+TEST_F(PropertiesTest, property_get_bool_true) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "true"));
+ ASSERT_TRUE(property_get_bool(PROPERTY_TEST_KEY, false));
+}
+
+TEST_F(PropertiesTest, property_get_bool_y) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "y"));
+ ASSERT_TRUE(property_get_bool(PROPERTY_TEST_KEY, false));
+}
+
+TEST_F(PropertiesTest, property_get_bool_yes) {
+ ASSERT_OK(property_set(PROPERTY_TEST_KEY, "yes"));
+ ASSERT_TRUE(property_get_bool(PROPERTY_TEST_KEY, false));
+}
+
+TEST_F(PropertiesTest, property_get_bool_neither) {
const char *valuesNeither[] = { "x0", "x1", "2", "-2", "True", "False", "garbage", "", " ",
"+1", " 1 ", " true", " true ", " y ", " yes", "yes ",
"+0", "-0", "00", " 00 ", " false", "false ",
@@ -263,7 +282,7 @@
}
}
-TEST_F(PropertiesTest, GetInt64) {
+TEST_F(PropertiesTest, property_get_int64) {
const int64_t DEFAULT_VALUE = INT64_C(0xDEADBEEFBEEFDEAD);
const std::string longMaxString = ToString(INT64_MAX);
@@ -310,7 +329,7 @@
}
}
-TEST_F(PropertiesTest, GetInt32) {
+TEST_F(PropertiesTest, property_get_int32) {
const int32_t DEFAULT_VALUE = INT32_C(0xDEADBEEF);
const std::string intMaxString = ToString(INT32_MAX);
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index 1d899ab..0abb861 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -189,7 +189,11 @@
int adjust = offset % mPageSize;
off64_t adjOffset = offset - adjust;
- size_t adjLength = length + adjust;
+ size_t adjLength;
+ if (__builtin_add_overflow(length, adjust, &adjLength)) {
+ ALOGE("adjusted length overflow: length %zu adjust %d", length, adjust);
+ return false;
+ }
int flags = MAP_SHARED;
int prot = PROT_READ;
diff --git a/libutils/FileMap_test.cpp b/libutils/FileMap_test.cpp
index 9f7ce85..fd1c9b0 100644
--- a/libutils/FileMap_test.cpp
+++ b/libutils/FileMap_test.cpp
@@ -52,3 +52,16 @@
ASSERT_EQ(0u, m.getDataLength());
ASSERT_EQ(offset, m.getDataOffset());
}
+
+TEST(FileMap, offset_overflow) {
+ // Make sure that an end that overflows SIZE_MAX will not abort.
+ // See http://b/156997193.
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+
+ off64_t offset = 200;
+ size_t length = SIZE_MAX;
+
+ android::FileMap m;
+ ASSERT_FALSE(m.create("test", tf.fd, offset, length, true));
+}
diff --git a/logd/Android.bp b/logd/Android.bp
index 5e591d9..1f6ab34 100644
--- a/logd/Android.bp
+++ b/logd/Android.bp
@@ -54,6 +54,7 @@
"LogStatistics.cpp",
"LogWhiteBlackList.cpp",
"LogTags.cpp",
+ "SimpleLogBuffer.cpp",
],
logtags: ["event.logtags"],
@@ -128,6 +129,7 @@
] + event_flag,
srcs: [
+ "ChattyLogBufferTest.cpp",
"logd_test.cpp",
"LogBufferTest.cpp",
],
diff --git a/logd/ChattyLogBuffer.cpp b/logd/ChattyLogBuffer.cpp
index f1305a5..0ba6025 100644
--- a/logd/ChattyLogBuffer.cpp
+++ b/logd/ChattyLogBuffer.cpp
@@ -40,59 +40,15 @@
#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
#endif
-// Default
-#define log_buffer_size(id) mMaxSize[id]
-
-void ChattyLogBuffer::Init() {
- log_id_for_each(i) {
- if (SetSize(i, __android_logger_get_buffer_size(i))) {
- SetSize(i, LOG_BUFFER_MIN_SIZE);
- }
- }
- // Release any sleeping reader threads to dump their current content.
- auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
- for (const auto& reader_thread : reader_list_->reader_threads()) {
- reader_thread->triggerReader_Locked();
- }
-}
-
ChattyLogBuffer::ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
LogStatistics* stats)
- : reader_list_(reader_list), tags_(tags), prune_(prune), stats_(stats) {
- pthread_rwlock_init(&mLogElementsLock, nullptr);
+ : SimpleLogBuffer(reader_list, tags, stats), prune_(prune) {}
- log_id_for_each(i) {
- lastLoggedElements[i] = nullptr;
- droppedElements[i] = nullptr;
- }
-
- Init();
-}
-
-ChattyLogBuffer::~ChattyLogBuffer() {
- log_id_for_each(i) {
- delete lastLoggedElements[i];
- delete droppedElements[i];
- }
-}
-
-LogBufferElementCollection::iterator ChattyLogBuffer::GetOldest(log_id_t log_id) {
- auto it = mLogElements.begin();
- if (oldest_[log_id]) {
- it = *oldest_[log_id];
- }
- while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
- it++;
- }
- if (it != mLogElements.end()) {
- oldest_[log_id] = it;
- }
- return it;
-}
+ChattyLogBuffer::~ChattyLogBuffer() {}
enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
-static enum match_type identical(LogBufferElement* elem, LogBufferElement* last) {
+static enum match_type Identical(LogBufferElement* elem, LogBufferElement* last) {
// is it mostly identical?
// if (!elem) return DIFFERENT;
ssize_t lenl = elem->getMsgLen();
@@ -147,222 +103,92 @@
return SAME;
}
-int ChattyLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
- const char* msg, uint16_t len) {
- if (log_id >= LOG_ID_MAX) {
- return -EINVAL;
- }
-
- // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
- // This prevents any chance that an outside source can request an
- // exact entry with time specified in ms or us precision.
- if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
-
- LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
-
+void ChattyLogBuffer::LogInternal(LogBufferElement&& elem) {
// b/137093665: don't coalesce security messages.
- if (log_id == LOG_ID_SECURITY) {
- wrlock();
- log(elem);
- unlock();
+ if (elem.getLogId() == LOG_ID_SECURITY) {
+ SimpleLogBuffer::LogInternal(std::move(elem));
+ return;
+ }
+ int log_id = elem.getLogId();
- return len;
+ // Initialize last_logged_elements_ to a copy of elem if logging the first element for a log_id.
+ if (!last_logged_elements_[log_id]) {
+ last_logged_elements_[log_id].emplace(elem);
+ SimpleLogBuffer::LogInternal(std::move(elem));
+ return;
}
- int prio = ANDROID_LOG_INFO;
- const char* tag = nullptr;
- size_t tag_len = 0;
- if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
- tag = tags_->tagToName(elem->getTag());
- if (tag) {
- tag_len = strlen(tag);
- }
- } else {
- prio = *msg;
- tag = msg + 1;
- tag_len = strnlen(tag, len - 1);
- }
- if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
- // Log traffic received to total
- stats_->AddTotal(elem);
- delete elem;
- return -EACCES;
- }
+ LogBufferElement& current_last = *last_logged_elements_[log_id];
+ enum match_type match = Identical(&elem, ¤t_last);
- wrlock();
- LogBufferElement* currentLast = lastLoggedElements[log_id];
- if (currentLast) {
- LogBufferElement* dropped = droppedElements[log_id];
- uint16_t count = dropped ? dropped->getDropped() : 0;
- //
- // State Init
- // incoming:
- // dropped = nullptr
- // currentLast = nullptr;
- // elem = incoming message
- // outgoing:
- // dropped = nullptr -> State 0
- // currentLast = copy of elem
- // log elem
- // State 0
- // incoming:
- // count = 0
- // dropped = nullptr
- // currentLast = copy of last message
- // elem = incoming message
- // outgoing: if match != DIFFERENT
- // dropped = copy of first identical message -> State 1
- // currentLast = reference to elem
- // break: if match == DIFFERENT
- // dropped = nullptr -> State 0
- // delete copy of last message (incoming currentLast)
- // currentLast = copy of elem
- // log elem
- // State 1
- // incoming:
- // count = 0
- // dropped = copy of first identical message
- // currentLast = reference to last held-back incoming
- // message
- // elem = incoming message
- // outgoing: if match == SAME
- // delete copy of first identical message (dropped)
- // dropped = reference to last held-back incoming
- // message set to chatty count of 1 -> State 2
- // currentLast = reference to elem
- // outgoing: if match == SAME_LIBLOG
- // dropped = copy of first identical message -> State 1
- // take sum of currentLast and elem
- // if sum overflows:
- // log currentLast
- // currentLast = reference to elem
- // else
- // delete currentLast
- // currentLast = reference to elem, sum liblog.
- // break: if match == DIFFERENT
- // delete dropped
- // dropped = nullptr -> State 0
- // log reference to last held-back (currentLast)
- // currentLast = copy of elem
- // log elem
- // State 2
- // incoming:
- // count = chatty count
- // dropped = chatty message holding count
- // currentLast = reference to last held-back incoming
- // message.
- // dropped = chatty message holding count
- // elem = incoming message
- // outgoing: if match != DIFFERENT
- // delete chatty message holding count
- // dropped = reference to last held-back incoming
- // message, set to chatty count + 1
- // currentLast = reference to elem
- // break: if match == DIFFERENT
- // log dropped (chatty message)
- // dropped = nullptr -> State 0
- // log reference to last held-back (currentLast)
- // currentLast = copy of elem
- // log elem
- //
- enum match_type match = identical(elem, currentLast);
- if (match != DIFFERENT) {
- if (dropped) {
- // Sum up liblog tag messages?
- if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
- android_log_event_int_t* event = reinterpret_cast<android_log_event_int_t*>(
- const_cast<char*>(currentLast->getMsg()));
- //
- // To unit test, differentiate with something like:
- // event->header.tag = htole32(CHATTY_LOG_TAG);
- // here, then instead of delete currentLast below,
- // log(currentLast) to see the incremental sums form.
- //
- uint32_t swab = event->payload.data;
- unsigned long long total = htole32(swab);
- event = reinterpret_cast<android_log_event_int_t*>(
- const_cast<char*>(elem->getMsg()));
- swab = event->payload.data;
-
- lastLoggedElements[LOG_ID_EVENTS] = elem;
- total += htole32(swab);
- // check for overflow
- if (total >= std::numeric_limits<int32_t>::max()) {
- log(currentLast);
- unlock();
- return len;
- }
- stats_->AddTotal(currentLast);
- delete currentLast;
- swab = total;
- event->payload.data = htole32(swab);
- unlock();
- return len;
- }
- if (count == USHRT_MAX) {
- log(dropped);
- count = 1;
- } else {
- delete dropped;
- ++count;
- }
+ if (match == DIFFERENT) {
+ if (duplicate_elements_[log_id]) {
+ // If we previously had 3+ identical messages, log the chatty message.
+ if (duplicate_elements_[log_id]->getDropped() > 0) {
+ SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
}
- if (count) {
- stats_->AddTotal(currentLast);
- currentLast->setDropped(count);
- }
- droppedElements[log_id] = currentLast;
- lastLoggedElements[log_id] = elem;
- unlock();
- return len;
+ duplicate_elements_[log_id].reset();
+ // Log the saved copy of the last identical message seen.
+ SimpleLogBuffer::LogInternal(std::move(current_last));
}
- if (dropped) { // State 1 or 2
- if (count) { // State 2
- log(dropped); // report chatty
- } else { // State 1
- delete dropped;
- }
- droppedElements[log_id] = nullptr;
- log(currentLast); // report last message in the series
- } else { // State 0
- delete currentLast;
- }
+ last_logged_elements_[log_id].emplace(elem);
+ SimpleLogBuffer::LogInternal(std::move(elem));
+ return;
}
- lastLoggedElements[log_id] = new LogBufferElement(*elem);
- log(elem);
- unlock();
+ // 2 identical message: set duplicate_elements_ appropriately.
+ if (!duplicate_elements_[log_id]) {
+ duplicate_elements_[log_id].emplace(std::move(current_last));
+ last_logged_elements_[log_id].emplace(std::move(elem));
+ return;
+ }
- return len;
+ // 3+ identical LIBLOG event messages: coalesce them into last_logged_elements_.
+ if (match == SAME_LIBLOG) {
+ const android_log_event_int_t* current_last_event =
+ reinterpret_cast<const android_log_event_int_t*>(current_last.getMsg());
+ int64_t current_last_count = current_last_event->payload.data;
+ android_log_event_int_t* elem_event =
+ reinterpret_cast<android_log_event_int_t*>(const_cast<char*>(elem.getMsg()));
+ int64_t elem_count = elem_event->payload.data;
+
+ int64_t total = current_last_count + elem_count;
+ if (total > std::numeric_limits<int32_t>::max()) {
+ SimpleLogBuffer::LogInternal(std::move(current_last));
+ last_logged_elements_[log_id].emplace(std::move(elem));
+ return;
+ }
+ stats()->AddTotal(current_last.getLogId(), current_last.getMsgLen());
+ elem_event->payload.data = total;
+ last_logged_elements_[log_id].emplace(std::move(elem));
+ return;
+ }
+
+ // 3+ identical messages (not LIBLOG) messages: increase the drop count.
+ uint16_t dropped_count = duplicate_elements_[log_id]->getDropped();
+ if (dropped_count == std::numeric_limits<uint16_t>::max()) {
+ SimpleLogBuffer::LogInternal(std::move(*duplicate_elements_[log_id]));
+ dropped_count = 0;
+ }
+ // We're dropping the current_last log so add its stats to the total.
+ stats()->AddTotal(current_last.getLogId(), current_last.getMsgLen());
+ // Use current_last for tracking the dropped count to always use the latest timestamp.
+ current_last.setDropped(dropped_count + 1);
+ duplicate_elements_[log_id].emplace(std::move(current_last));
+ last_logged_elements_[log_id].emplace(std::move(elem));
}
-// assumes ChattyLogBuffer::wrlock() held, owns elem, look after garbage collection
-void ChattyLogBuffer::log(LogBufferElement* elem) {
- mLogElements.push_back(elem);
- stats_->Add(elem);
- maybePrune(elem->getLogId());
- reader_list_->NotifyNewLog(1 << elem->getLogId());
-}
-
-// ChattyLogBuffer::wrlock() must be held when this function is called.
-void ChattyLogBuffer::maybePrune(log_id_t id) {
- unsigned long prune_rows;
- if (stats_->ShouldPrune(id, log_buffer_size(id), &prune_rows)) {
- prune(id, prune_rows);
- }
-}
-
-LogBufferElementCollection::iterator ChattyLogBuffer::erase(LogBufferElementCollection::iterator it,
+LogBufferElementCollection::iterator ChattyLogBuffer::Erase(LogBufferElementCollection::iterator it,
bool coalesce) {
- LogBufferElement* element = *it;
- log_id_t id = element->getLogId();
+ LogBufferElement& element = *it;
+ log_id_t id = element.getLogId();
// Remove iterator references in the various lists that will become stale
// after the element is erased from the main logging list.
{ // start of scope for found iterator
- int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
- : element->getUid();
+ int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.getTag()
+ : element.getUid();
LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
if ((found != mLastWorst[id].end()) && (it == found->second)) {
mLastWorst[id].erase(found);
@@ -373,33 +199,26 @@
// element->getUid() may not be AID_SYSTEM for next-best-watermark.
// will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
// long term code stability, find() check should be fast for those ids.
- LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element->getPid());
+ LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(element.getPid());
if (found != mLastWorstPidOfSystem[id].end() && it == found->second) {
mLastWorstPidOfSystem[id].erase(found);
}
}
- bool setLast[LOG_ID_MAX];
- bool doSetLast = false;
- log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
LogBufferElementCollection::iterator bad = it;
int key =
(id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag() : element->getUid();
#endif
- it = mLogElements.erase(it);
- if (doSetLast) {
- log_id_for_each(i) {
- if (setLast[i]) {
- if (__predict_false(it == mLogElements.end())) {
- oldest_[i] = std::nullopt;
- } else {
- oldest_[i] = it; // Store the next iterator even if it does not correspond to
- // the same log_id, as a starting point for GetOldest().
- }
- }
- }
+
+ if (coalesce) {
+ stats()->Erase(&element);
+ } else {
+ stats()->Subtract(&element);
}
+
+ it = SimpleLogBuffer::Erase(it);
+
#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
log_id_for_each(i) {
for (auto b : mLastWorst[i]) {
@@ -414,13 +233,6 @@
}
}
#endif
- if (coalesce) {
- stats_->Erase(element);
- } else {
- stats_->Subtract(element);
- }
- delete element;
-
return it;
}
@@ -474,27 +286,6 @@
}
};
-// If the selected reader is blocking our pruning progress, decide on
-// what kind of mitigation is necessary to unblock the situation.
-void ChattyLogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
- if (stats_->Sizes(id) > (2 * log_buffer_size(id))) { // +100%
- // A misbehaving or slow reader has its connection
- // dropped if we hit too much memory pressure.
- android::prdebug("Kicking blocked reader, %s, from ChattyLogBuffer::kickMe()\n",
- me->name().c_str());
- me->release_Locked();
- } else if (me->deadline().time_since_epoch().count() != 0) {
- // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
- me->triggerReader_Locked();
- } else {
- // tell slow reader to skip entries to catch up
- android::prdebug(
- "Skipping %lu entries from slow reader, %s, from ChattyLogBuffer::kickMe()\n",
- pruneRows, me->name().c_str());
- me->triggerSkip_Locked(id, pruneRows);
- }
-}
-
// prune "pruneRows" of type "id" from the buffer.
//
// This garbage collection task is used to expire log entries. It is called to
@@ -540,17 +331,15 @@
// The third thread is optional, and only gets hit if there was a whitelist
// and more needs to be pruned against the backstop of the region lock.
//
-// ChattyLogBuffer::wrlock() must be held when this function is called.
-//
-bool ChattyLogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
+bool ChattyLogBuffer::Prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
LogReaderThread* oldest = nullptr;
bool busy = false;
bool clearAll = pruneRows == ULONG_MAX;
- auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
+ auto reader_threads_lock = std::lock_guard{reader_list()->reader_threads_lock()};
// Region locked?
- for (const auto& reader_thread : reader_list_->reader_threads()) {
+ for (const auto& reader_thread : reader_list()->reader_threads()) {
if (!reader_thread->IsWatching(id)) {
continue;
}
@@ -567,21 +356,21 @@
// Only here if clear all request from non system source, so chatty
// filter logistics is not required.
it = GetOldest(id);
- while (it != mLogElements.end()) {
- LogBufferElement* element = *it;
+ while (it != logs().end()) {
+ LogBufferElement& element = *it;
- if (element->getLogId() != id || element->getUid() != caller_uid) {
+ if (element.getLogId() != id || element.getUid() != caller_uid) {
++it;
continue;
}
- if (oldest && oldest->start() <= element->getSequence()) {
+ if (oldest && oldest->start() <= element.getSequence()) {
busy = true;
- kickMe(oldest, id, pruneRows);
+ KickReader(oldest, id, pruneRows);
break;
}
- it = erase(it);
+ it = Erase(it);
if (--pruneRows == 0) {
break;
}
@@ -600,16 +389,16 @@
if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
// Calculate threshold as 12.5% of available storage
- size_t threshold = log_buffer_size(id) / 8;
+ size_t threshold = max_size(id) / 8;
if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
- stats_->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
+ stats()->WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
// per-pid filter for AID_SYSTEM sources is too complex
} else {
- stats_->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
+ stats()->WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
- stats_->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
+ stats()->WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
}
}
}
@@ -630,7 +419,7 @@
if (!gc && (worst != -1)) {
{ // begin scope for worst found iterator
LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
- if (found != mLastWorst[id].end() && found->second != mLogElements.end()) {
+ if (found != mLastWorst[id].end() && found->second != logs().end()) {
leading = false;
it = found->second;
}
@@ -639,8 +428,7 @@
// FYI: worstPid only set if !LOG_ID_EVENTS and
// !LOG_ID_SECURITY, not going to make that assumption ...
LogBufferPidIteratorMap::iterator found = mLastWorstPidOfSystem[id].find(worstPid);
- if (found != mLastWorstPidOfSystem[id].end() &&
- found->second != mLogElements.end()) {
+ if (found != mLastWorstPidOfSystem[id].end() && found->second != logs().end()) {
leading = false;
it = found->second;
}
@@ -651,43 +439,43 @@
}
static const log_time too_old{EXPIRE_HOUR_THRESHOLD * 60 * 60, 0};
LogBufferElementCollection::iterator lastt;
- lastt = mLogElements.end();
+ lastt = logs().end();
--lastt;
LogBufferElementLast last;
- while (it != mLogElements.end()) {
- LogBufferElement* element = *it;
+ while (it != logs().end()) {
+ LogBufferElement& element = *it;
- if (oldest && oldest->start() <= element->getSequence()) {
+ if (oldest && oldest->start() <= element.getSequence()) {
busy = true;
// Do not let chatty eliding trigger any reader mitigation
break;
}
- if (element->getLogId() != id) {
+ if (element.getLogId() != id) {
++it;
continue;
}
// below this point element->getLogId() == id
- uint16_t dropped = element->getDropped();
+ uint16_t dropped = element.getDropped();
// remove any leading drops
if (leading && dropped) {
- it = erase(it);
+ it = Erase(it);
continue;
}
- if (dropped && last.coalesce(element, dropped)) {
- it = erase(it, true);
+ if (dropped && last.coalesce(&element, dropped)) {
+ it = Erase(it, true);
continue;
}
- int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element->getTag()
- : element->getUid();
+ int key = (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) ? element.getTag()
+ : element.getUid();
- if (hasBlacklist && prune_->naughty(element)) {
- last.clear(element);
- it = erase(it);
+ if (hasBlacklist && prune_->naughty(&element)) {
+ last.clear(&element);
+ it = Erase(it);
if (dropped) {
continue;
}
@@ -702,25 +490,25 @@
if (worst_sizes < second_worst_sizes) {
break;
}
- worst_sizes -= element->getMsgLen();
+ worst_sizes -= element.getMsgLen();
}
continue;
}
- if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
- (element->getRealTime() > (*lastt)->getRealTime())) {
+ if (element.getRealTime() < (lastt->getRealTime() - too_old) ||
+ element.getRealTime() > lastt->getRealTime()) {
break;
}
if (dropped) {
- last.add(element);
- if (worstPid && ((!gc && element->getPid() == worstPid) ||
- mLastWorstPidOfSystem[id].find(element->getPid()) ==
+ last.add(&element);
+ if (worstPid && ((!gc && element.getPid() == worstPid) ||
+ mLastWorstPidOfSystem[id].find(element.getPid()) ==
mLastWorstPidOfSystem[id].end())) {
// element->getUid() may not be AID_SYSTEM, next best
// watermark if current one empty. id is not LOG_ID_EVENTS
// or LOG_ID_SECURITY because of worstPid check.
- mLastWorstPidOfSystem[id][element->getPid()] = it;
+ mLastWorstPidOfSystem[id][element.getPid()] = it;
}
if ((!gc && !worstPid && (key == worst)) ||
(mLastWorst[id].find(key) == mLastWorst[id].end())) {
@@ -730,9 +518,9 @@
continue;
}
- if (key != worst || (worstPid && element->getPid() != worstPid)) {
+ if (key != worst || (worstPid && element.getPid() != worstPid)) {
leading = false;
- last.clear(element);
+ last.clear(&element);
++it;
continue;
}
@@ -746,18 +534,18 @@
kick = true;
- uint16_t len = element->getMsgLen();
+ uint16_t len = element.getMsgLen();
// do not create any leading drops
if (leading) {
- it = erase(it);
+ it = Erase(it);
} else {
- stats_->Drop(element);
- element->setDropped(1);
- if (last.coalesce(element, 1)) {
- it = erase(it, true);
+ stats()->Drop(&element);
+ element.setDropped(1);
+ if (last.coalesce(&element, 1)) {
+ it = Erase(it, true);
} else {
- last.add(element);
+ last.add(&element);
if (worstPid && (!gc || mLastWorstPidOfSystem[id].find(worstPid) ==
mLastWorstPidOfSystem[id].end())) {
// element->getUid() may not be AID_SYSTEM, next best
@@ -786,188 +574,52 @@
bool whitelist = false;
bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
it = GetOldest(id);
- while ((pruneRows > 0) && (it != mLogElements.end())) {
- LogBufferElement* element = *it;
+ while ((pruneRows > 0) && (it != logs().end())) {
+ LogBufferElement& element = *it;
- if (element->getLogId() != id) {
+ if (element.getLogId() != id) {
it++;
continue;
}
- if (oldest && oldest->start() <= element->getSequence()) {
+ if (oldest && oldest->start() <= element.getSequence()) {
busy = true;
- if (!whitelist) kickMe(oldest, id, pruneRows);
+ if (!whitelist) KickReader(oldest, id, pruneRows);
break;
}
- if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
+ if (hasWhitelist && !element.getDropped() && prune_->nice(&element)) {
// WhiteListed
whitelist = true;
it++;
continue;
}
- it = erase(it);
+ it = Erase(it);
pruneRows--;
}
// Do not save the whitelist if we are reader range limited
if (whitelist && (pruneRows > 0)) {
it = GetOldest(id);
- while ((it != mLogElements.end()) && (pruneRows > 0)) {
- LogBufferElement* element = *it;
+ while ((it != logs().end()) && (pruneRows > 0)) {
+ LogBufferElement& element = *it;
- if (element->getLogId() != id) {
+ if (element.getLogId() != id) {
++it;
continue;
}
- if (oldest && oldest->start() <= element->getSequence()) {
+ if (oldest && oldest->start() <= element.getSequence()) {
busy = true;
- kickMe(oldest, id, pruneRows);
+ KickReader(oldest, id, pruneRows);
break;
}
- it = erase(it);
+ it = Erase(it);
pruneRows--;
}
}
return (pruneRows > 0) && busy;
}
-
-// clear all rows of type "id" from the buffer.
-bool ChattyLogBuffer::Clear(log_id_t id, uid_t uid) {
- bool busy = true;
- // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
- for (int retry = 4;;) {
- if (retry == 1) { // last pass
- // Check if it is still busy after the sleep, we say prune
- // one entry, not another clear run, so we are looking for
- // the quick side effect of the return value to tell us if
- // we have a _blocked_ reader.
- wrlock();
- busy = prune(id, 1, uid);
- unlock();
- // It is still busy, blocked reader(s), lets kill them all!
- // otherwise, lets be a good citizen and preserve the slow
- // readers and let the clear run (below) deal with determining
- // if we are still blocked and return an error code to caller.
- if (busy) {
- auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
- for (const auto& reader_thread : reader_list_->reader_threads()) {
- if (reader_thread->IsWatching(id)) {
- android::prdebug(
- "Kicking blocked reader, %s, from ChattyLogBuffer::clear()\n",
- reader_thread->name().c_str());
- reader_thread->release_Locked();
- }
- }
- }
- }
- wrlock();
- busy = prune(id, ULONG_MAX, uid);
- unlock();
- if (!busy || !--retry) {
- break;
- }
- sleep(1); // Let reader(s) catch up after notification
- }
- return busy;
-}
-
-// set the total space allocated to "id"
-int ChattyLogBuffer::SetSize(log_id_t id, unsigned long size) {
- // Reasonable limits ...
- if (!__android_logger_valid_buffer_size(size)) {
- return -1;
- }
- wrlock();
- log_buffer_size(id) = size;
- unlock();
- return 0;
-}
-
-// get the total space allocated to "id"
-unsigned long ChattyLogBuffer::GetSize(log_id_t id) {
- rdlock();
- size_t retval = log_buffer_size(id);
- unlock();
- return retval;
-}
-
-uint64_t ChattyLogBuffer::FlushTo(
- LogWriter* writer, uint64_t start, pid_t* lastTid,
- const std::function<FlushToResult(const LogBufferElement* element)>& filter) {
- LogBufferElementCollection::iterator it;
- uid_t uid = writer->uid();
-
- rdlock();
-
- if (start <= 1) {
- // client wants to start from the beginning
- it = mLogElements.begin();
- } else {
- // Client wants to start from some specified time. Chances are
- // we are better off starting from the end of the time sorted list.
- for (it = mLogElements.end(); it != mLogElements.begin();
- /* do nothing */) {
- --it;
- LogBufferElement* element = *it;
- if (element->getSequence() <= start) {
- it++;
- break;
- }
- }
- }
-
- uint64_t curr = start;
-
- for (; it != mLogElements.end(); ++it) {
- LogBufferElement* element = *it;
-
- if (!writer->privileged() && element->getUid() != uid) {
- continue;
- }
-
- if (!writer->can_read_security_logs() && element->getLogId() == LOG_ID_SECURITY) {
- continue;
- }
-
- // NB: calling out to another object with wrlock() held (safe)
- if (filter) {
- FlushToResult ret = filter(element);
- if (ret == FlushToResult::kSkip) {
- continue;
- }
- if (ret == FlushToResult::kStop) {
- break;
- }
- }
-
- bool sameTid = false;
- if (lastTid) {
- sameTid = lastTid[element->getLogId()] == element->getTid();
- // Dropped (chatty) immediately following a valid log from the
- // same source in the same log buffer indicates we have a
- // multiple identical squash. chatty that differs source
- // is due to spam filter. chatty to chatty of different
- // source is also due to spam filter.
- lastTid[element->getLogId()] =
- (element->getDropped() && !sameTid) ? 0 : element->getTid();
- }
-
- unlock();
-
- curr = element->getSequence();
- // range locking in LastLogTimes looks after us
- if (!element->FlushTo(writer, stats_, sameTid)) {
- return FLUSH_ERROR;
- }
-
- rdlock();
- }
- unlock();
-
- return curr;
-}
diff --git a/logd/ChattyLogBuffer.h b/logd/ChattyLogBuffer.h
index 29a421d..6f60272 100644
--- a/logd/ChattyLogBuffer.h
+++ b/logd/ChattyLogBuffer.h
@@ -22,6 +22,7 @@
#include <optional>
#include <string>
+#include <android-base/thread_annotations.h>
#include <android/log.h>
#include <private/android_filesystem_config.h>
#include <sysutils/SocketClient.h>
@@ -34,64 +35,37 @@
#include "LogTags.h"
#include "LogWhiteBlackList.h"
#include "LogWriter.h"
+#include "SimpleLogBuffer.h"
+#include "rwlock.h"
-typedef std::list<LogBufferElement*> LogBufferElementCollection;
+typedef std::list<LogBufferElement> LogBufferElementCollection;
-class ChattyLogBuffer : public LogBuffer {
- LogBufferElementCollection mLogElements;
- pthread_rwlock_t mLogElementsLock;
-
+class ChattyLogBuffer : public SimpleLogBuffer {
// watermark of any worst/chatty uid processing
typedef std::unordered_map<uid_t, LogBufferElementCollection::iterator> LogBufferIteratorMap;
- LogBufferIteratorMap mLastWorst[LOG_ID_MAX];
+ LogBufferIteratorMap mLastWorst[LOG_ID_MAX] GUARDED_BY(lock_);
// watermark of any worst/chatty pid of system processing
typedef std::unordered_map<pid_t, LogBufferElementCollection::iterator> LogBufferPidIteratorMap;
- LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX];
-
- unsigned long mMaxSize[LOG_ID_MAX];
-
- LogBufferElement* lastLoggedElements[LOG_ID_MAX];
- LogBufferElement* droppedElements[LOG_ID_MAX];
- void log(LogBufferElement* elem);
+ LogBufferPidIteratorMap mLastWorstPidOfSystem[LOG_ID_MAX] GUARDED_BY(lock_);
public:
ChattyLogBuffer(LogReaderList* reader_list, LogTags* tags, PruneList* prune,
LogStatistics* stats);
~ChattyLogBuffer();
- void Init() override;
- int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
- uint16_t len) override;
- uint64_t FlushTo(
- LogWriter* writer, uint64_t start, pid_t* lastTid,
- const std::function<FlushToResult(const LogBufferElement* element)>& filter) override;
-
- bool Clear(log_id_t id, uid_t uid = AID_ROOT) override;
- unsigned long GetSize(log_id_t id) override;
- int SetSize(log_id_t id, unsigned long size) override;
+ protected:
+ bool Prune(log_id_t id, unsigned long pruneRows, uid_t uid) REQUIRES(lock_) override;
+ void LogInternal(LogBufferElement&& elem) REQUIRES(lock_) override;
private:
- void wrlock() { pthread_rwlock_wrlock(&mLogElementsLock); }
- void rdlock() { pthread_rwlock_rdlock(&mLogElementsLock); }
- void unlock() { pthread_rwlock_unlock(&mLogElementsLock); }
+ LogBufferElementCollection::iterator Erase(LogBufferElementCollection::iterator it,
+ bool coalesce = false) REQUIRES(lock_);
- void maybePrune(log_id_t id);
- void kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows);
-
- bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
- LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it,
- bool coalesce = false);
-
- // Returns an iterator to the oldest element for a given log type, or mLogElements.end() if
- // there are no logs for the given log type. Requires mLogElementsLock to be held.
- LogBufferElementCollection::iterator GetOldest(log_id_t log_id);
-
- LogReaderList* reader_list_;
- LogTags* tags_;
PruneList* prune_;
- LogStatistics* stats_;
- // Keeps track of the iterator to the oldest log message of a given log type, as an
- // optimization when pruning logs. Use GetOldest() to retrieve.
- std::optional<LogBufferElementCollection::iterator> oldest_[LOG_ID_MAX];
+ // This always contains a copy of the last message logged, for deduplication.
+ std::optional<LogBufferElement> last_logged_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
+ // This contains an element if duplicate messages are seen.
+ // Its `dropped` count is `duplicates seen - 1`.
+ std::optional<LogBufferElement> duplicate_elements_[LOG_ID_MAX] GUARDED_BY(lock_);
};
diff --git a/logd/ChattyLogBufferTest.cpp b/logd/ChattyLogBufferTest.cpp
new file mode 100644
index 0000000..2e0c947
--- /dev/null
+++ b/logd/ChattyLogBufferTest.cpp
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "LogBufferTest.h"
+
+class ChattyLogBufferTest : public LogBufferTest {};
+
+TEST_P(ChattyLogBufferTest, deduplication_simple) {
+ auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
+ bool regex = false) -> LogMessage {
+ logger_entry entry = {
+ .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
+ std::string message;
+ message.push_back(ANDROID_LOG_INFO);
+ message.append(tag);
+ message.push_back('\0');
+ message.append(msg);
+ message.push_back('\0');
+ return {entry, message, regex};
+ };
+
+ // clang-format off
+ std::vector<LogMessage> log_messages = {
+ make_message(0, "test_tag", "duplicate"),
+ make_message(1, "test_tag", "duplicate"),
+ make_message(2, "test_tag", "not_same"),
+ make_message(3, "test_tag", "duplicate"),
+ make_message(4, "test_tag", "duplicate"),
+ make_message(5, "test_tag", "not_same"),
+ make_message(6, "test_tag", "duplicate"),
+ make_message(7, "test_tag", "duplicate"),
+ make_message(8, "test_tag", "duplicate"),
+ make_message(9, "test_tag", "not_same"),
+ make_message(10, "test_tag", "duplicate"),
+ make_message(11, "test_tag", "duplicate"),
+ make_message(12, "test_tag", "duplicate"),
+ make_message(13, "test_tag", "duplicate"),
+ make_message(14, "test_tag", "duplicate"),
+ make_message(15, "test_tag", "duplicate"),
+ make_message(16, "test_tag", "not_same"),
+ make_message(100, "test_tag", "duplicate"),
+ make_message(200, "test_tag", "duplicate"),
+ make_message(300, "test_tag", "duplicate"),
+ };
+ // clang-format on
+ FixupMessages(&log_messages);
+ LogMessages(log_messages);
+
+ std::vector<LogMessage> read_log_messages;
+ std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
+ log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
+
+ std::vector<LogMessage> expected_log_messages = {
+ make_message(0, "test_tag", "duplicate"),
+ make_message(1, "test_tag", "duplicate"),
+ make_message(2, "test_tag", "not_same"),
+ make_message(3, "test_tag", "duplicate"),
+ make_message(4, "test_tag", "duplicate"),
+ make_message(5, "test_tag", "not_same"),
+ // 3 duplicate logs together print the first, a 1 count chatty message, then the last.
+ make_message(6, "test_tag", "duplicate"),
+ make_message(7, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ expire 1 line", true),
+ make_message(8, "test_tag", "duplicate"),
+ make_message(9, "test_tag", "not_same"),
+ // 6 duplicate logs together print the first, a 4 count chatty message, then the last.
+ make_message(10, "test_tag", "duplicate"),
+ make_message(14, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ expire 4 lines", true),
+ make_message(15, "test_tag", "duplicate"),
+ make_message(16, "test_tag", "not_same"),
+ // duplicate logs > 1 minute apart are not deduplicated.
+ make_message(100, "test_tag", "duplicate"),
+ make_message(200, "test_tag", "duplicate"),
+ make_message(300, "test_tag", "duplicate"),
+ };
+ FixupMessages(&expected_log_messages);
+ CompareLogMessages(expected_log_messages, read_log_messages);
+};
+
+TEST_P(ChattyLogBufferTest, deduplication_overflow) {
+ auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
+ bool regex = false) -> LogMessage {
+ logger_entry entry = {
+ .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
+ std::string message;
+ message.push_back(ANDROID_LOG_INFO);
+ message.append(tag);
+ message.push_back('\0');
+ message.append(msg);
+ message.push_back('\0');
+ return {entry, message, regex};
+ };
+
+ uint32_t sec = 0;
+ std::vector<LogMessage> log_messages = {
+ make_message(sec++, "test_tag", "normal"),
+ };
+ size_t expired_per_chatty_message = std::numeric_limits<uint16_t>::max();
+ for (size_t i = 0; i < expired_per_chatty_message + 3; ++i) {
+ log_messages.emplace_back(make_message(sec++, "test_tag", "duplicate"));
+ }
+ log_messages.emplace_back(make_message(sec++, "test_tag", "normal"));
+ FixupMessages(&log_messages);
+ LogMessages(log_messages);
+
+ std::vector<LogMessage> read_log_messages;
+ std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
+ log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
+
+ std::vector<LogMessage> expected_log_messages = {
+ make_message(0, "test_tag", "normal"),
+ make_message(1, "test_tag", "duplicate"),
+ make_message(expired_per_chatty_message + 1, "chatty",
+ "uid=0\\([^\\)]+\\) [^ ]+ expire 65535 lines", true),
+ make_message(expired_per_chatty_message + 2, "chatty",
+ "uid=0\\([^\\)]+\\) [^ ]+ expire 1 line", true),
+ make_message(expired_per_chatty_message + 3, "test_tag", "duplicate"),
+ make_message(expired_per_chatty_message + 4, "test_tag", "normal"),
+ };
+ FixupMessages(&expected_log_messages);
+ CompareLogMessages(expected_log_messages, read_log_messages);
+}
+
+TEST_P(ChattyLogBufferTest, deduplication_liblog) {
+ auto make_message = [&](uint32_t sec, int32_t tag, int32_t count) -> LogMessage {
+ logger_entry entry = {
+ .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_EVENTS, .uid = 0};
+ android_log_event_int_t liblog_event = {
+ .header.tag = tag, .payload.type = EVENT_TYPE_INT, .payload.data = count};
+ return {entry, std::string(reinterpret_cast<char*>(&liblog_event), sizeof(liblog_event)),
+ false};
+ };
+
+ // LIBLOG_LOG_TAG
+ std::vector<LogMessage> log_messages = {
+ make_message(0, 1234, 1),
+ make_message(1, LIBLOG_LOG_TAG, 3),
+ make_message(2, 1234, 2),
+ make_message(3, LIBLOG_LOG_TAG, 3),
+ make_message(4, LIBLOG_LOG_TAG, 4),
+ make_message(5, 1234, 223),
+ make_message(6, LIBLOG_LOG_TAG, 2),
+ make_message(7, LIBLOG_LOG_TAG, 3),
+ make_message(8, LIBLOG_LOG_TAG, 4),
+ make_message(9, 1234, 227),
+ make_message(10, LIBLOG_LOG_TAG, 1),
+ make_message(11, LIBLOG_LOG_TAG, 3),
+ make_message(12, LIBLOG_LOG_TAG, 2),
+ make_message(13, LIBLOG_LOG_TAG, 3),
+ make_message(14, LIBLOG_LOG_TAG, 5),
+ make_message(15, 1234, 227),
+ make_message(16, LIBLOG_LOG_TAG, 2),
+ make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
+ make_message(18, LIBLOG_LOG_TAG, 3),
+ make_message(19, LIBLOG_LOG_TAG, 5),
+ make_message(20, 1234, 227),
+ };
+ FixupMessages(&log_messages);
+ LogMessages(log_messages);
+
+ std::vector<LogMessage> read_log_messages;
+ std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
+ log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
+
+ std::vector<LogMessage> expected_log_messages = {
+ make_message(0, 1234, 1),
+ make_message(1, LIBLOG_LOG_TAG, 3),
+ make_message(2, 1234, 2),
+ make_message(3, LIBLOG_LOG_TAG, 3),
+ make_message(4, LIBLOG_LOG_TAG, 4),
+ make_message(5, 1234, 223),
+ // More than 2 liblog events (3 here), sum their value into the third message.
+ make_message(6, LIBLOG_LOG_TAG, 2),
+ make_message(8, LIBLOG_LOG_TAG, 7),
+ make_message(9, 1234, 227),
+ // More than 2 liblog events (5 here), sum their value into the third message.
+ make_message(10, LIBLOG_LOG_TAG, 1),
+ make_message(14, LIBLOG_LOG_TAG, 13),
+ make_message(15, 1234, 227),
+ // int32_t max is the max for a chatty message, beyond that we must use new messages.
+ make_message(16, LIBLOG_LOG_TAG, 2),
+ make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
+ make_message(19, LIBLOG_LOG_TAG, 8),
+ make_message(20, 1234, 227),
+ };
+ FixupMessages(&expected_log_messages);
+ CompareLogMessages(expected_log_messages, read_log_messages);
+};
+
+INSTANTIATE_TEST_CASE_P(ChattyLogBufferTests, ChattyLogBufferTest, testing::Values("chatty"));
\ No newline at end of file
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
index 6274051..7f1e128 100644
--- a/logd/LogBuffer.h
+++ b/logd/LogBuffer.h
@@ -53,4 +53,6 @@
virtual bool Clear(log_id_t id, uid_t uid) = 0;
virtual unsigned long GetSize(log_id_t id) = 0;
virtual int SetSize(log_id_t id, unsigned long size) = 0;
+
+ virtual uint64_t sequence() const = 0;
};
\ No newline at end of file
diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp
index 4f5cabd..c6dbda8 100644
--- a/logd/LogBufferElement.cpp
+++ b/logd/LogBufferElement.cpp
@@ -30,14 +30,12 @@
#include "LogStatistics.h"
#include "LogUtils.h"
-atomic_int_fast64_t LogBufferElement::sequence(1);
-
LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
- pid_t tid, const char* msg, uint16_t len)
+ pid_t tid, uint64_t sequence, const char* msg, uint16_t len)
: mUid(uid),
mPid(pid),
mTid(tid),
- mSequence(sequence.fetch_add(1, memory_order_relaxed)),
+ mSequence(sequence),
mRealTime(realtime),
mMsgLen(len),
mLogId(log_id),
@@ -63,6 +61,23 @@
}
}
+LogBufferElement::LogBufferElement(LogBufferElement&& elem)
+ : mUid(elem.mUid),
+ mPid(elem.mPid),
+ mTid(elem.mTid),
+ mSequence(elem.mSequence),
+ mRealTime(elem.mRealTime),
+ mMsgLen(elem.mMsgLen),
+ mLogId(elem.mLogId),
+ mDropped(elem.mDropped) {
+ if (mDropped) {
+ mTag = elem.getTag();
+ } else {
+ mMsg = elem.mMsg;
+ elem.mMsg = nullptr;
+ }
+}
+
LogBufferElement::~LogBufferElement() {
if (!mDropped) {
delete[] mMsg;
diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h
index 2f2d70d..35252f9 100644
--- a/logd/LogBufferElement.h
+++ b/logd/LogBufferElement.h
@@ -16,7 +16,6 @@
#pragma once
-#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -51,15 +50,14 @@
const uint8_t mLogId;
bool mDropped;
- static atomic_int_fast64_t sequence;
-
// assumption: mDropped == true
size_t populateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame);
public:
- LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
- pid_t tid, const char* msg, uint16_t len);
+ LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
+ uint64_t sequence, const char* msg, uint16_t len);
LogBufferElement(const LogBufferElement& elem);
+ LogBufferElement(LogBufferElement&& elem);
~LogBufferElement();
bool isBinary(void) const {
@@ -90,7 +88,6 @@
return mDropped ? nullptr : mMsg;
}
uint64_t getSequence() const { return mSequence; }
- static uint64_t getCurrentSequence() { return sequence.load(memory_order_relaxed); }
log_time getRealTime(void) const {
return mRealTime;
}
diff --git a/logd/LogBufferTest.cpp b/logd/LogBufferTest.cpp
index b52ce8f..334d57b 100644
--- a/logd/LogBufferTest.cpp
+++ b/logd/LogBufferTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2014 The Android Open Source Project
+ * Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "LogBuffer.h"
+#include "LogBufferTest.h"
#include <unistd.h>
@@ -25,14 +25,8 @@
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
-#include <gtest/gtest.h>
-#include "ChattyLogBuffer.h"
-#include "LogReaderList.h"
#include "LogReaderThread.h"
-#include "LogStatistics.h"
-#include "LogTags.h"
-#include "LogWhiteBlackList.h"
#include "LogWriter.h"
using android::base::Join;
@@ -61,15 +55,8 @@
return nullptr;
}
-struct LogMessage {
- logger_entry entry;
- std::string message;
- bool regex_compare = false; // Only set for expected messages, when true 'message' should be
- // interpretted as a regex.
-};
-
-std::vector<std::string> CompareLoggerEntries(const logger_entry& expected,
- const logger_entry& result, bool ignore_len) {
+static std::vector<std::string> CompareLoggerEntries(const logger_entry& expected,
+ const logger_entry& result, bool ignore_len) {
std::vector<std::string> errors;
if (!ignore_len && expected.len != result.len) {
errors.emplace_back(
@@ -106,7 +93,7 @@
return errors;
}
-std::string MakePrintable(std::string in) {
+static std::string MakePrintable(std::string in) {
if (in.size() > 80) {
in = in.substr(0, 80) + "...";
}
@@ -121,7 +108,7 @@
return result;
}
-std::string CompareMessages(const std::string& expected, const std::string& result) {
+static std::string CompareMessages(const std::string& expected, const std::string& result) {
if (expected == result) {
return {};
}
@@ -145,7 +132,7 @@
result_short.c_str());
}
-std::string CompareRegexMessages(const std::string& expected, const std::string& result) {
+static std::string CompareRegexMessages(const std::string& expected, const std::string& result) {
auto expected_pieces = Split(expected, std::string("\0", 1));
auto result_pieces = Split(result, std::string("\0", 1));
@@ -197,54 +184,14 @@
EXPECT_EQ(0U, num_errors);
}
-class TestWriter : public LogWriter {
- public:
- TestWriter(std::vector<LogMessage>* msgs, bool* released)
- : LogWriter(0, true, true), msgs_(msgs), released_(released) {}
- bool Write(const logger_entry& entry, const char* message) override {
- msgs_->emplace_back(LogMessage{entry, std::string(message, entry.len), false});
- return true;
+void FixupMessages(std::vector<LogMessage>* messages) {
+ for (auto& [entry, message, _] : *messages) {
+ entry.hdr_size = sizeof(logger_entry);
+ entry.len = message.size();
}
+}
- void Release() {
- if (released_) *released_ = true;
- }
-
- std::string name() const override { return "test_writer"; }
-
- private:
- std::vector<LogMessage>* msgs_;
- bool* released_;
-};
-
-class LogBufferTest : public testing::Test {
- protected:
- void SetUp() override {
- log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_));
- }
-
- void FixupMessages(std::vector<LogMessage>* messages) {
- for (auto& [entry, message, _] : *messages) {
- entry.hdr_size = sizeof(logger_entry);
- entry.len = message.size();
- }
- }
-
- void LogMessages(const std::vector<LogMessage>& messages) {
- for (auto& [entry, message, _] : messages) {
- log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec),
- entry.uid, entry.pid, entry.tid, message.c_str(), message.size());
- }
- }
-
- LogReaderList reader_list_;
- LogTags tags_;
- PruneList prune_;
- LogStatistics stats_{false};
- std::unique_ptr<LogBuffer> log_buffer_;
-};
-
-TEST_F(LogBufferTest, smoke) {
+TEST_P(LogBufferTest, smoke) {
std::vector<LogMessage> log_messages = {
{{
.pid = 1,
@@ -266,7 +213,7 @@
CompareLogMessages(log_messages, read_log_messages);
}
-TEST_F(LogBufferTest, smoke_with_reader_thread) {
+TEST_P(LogBufferTest, smoke_with_reader_thread) {
std::vector<LogMessage> log_messages = {
{{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0},
"first"},
@@ -358,7 +305,7 @@
return {entry, message};
}
-TEST_F(LogBufferTest, random_messages) {
+TEST_P(LogBufferTest, random_messages) {
srand(1);
std::vector<LogMessage> log_messages;
for (size_t i = 0; i < 1000; ++i) {
@@ -388,183 +335,4 @@
CompareLogMessages(log_messages, read_log_messages);
}
-TEST_F(LogBufferTest, deduplication_simple) {
- auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
- bool regex = false) -> LogMessage {
- logger_entry entry = {
- .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
- std::string message;
- message.push_back(ANDROID_LOG_INFO);
- message.append(tag);
- message.push_back('\0');
- message.append(msg);
- message.push_back('\0');
- return {entry, message, regex};
- };
-
- // clang-format off
- std::vector<LogMessage> log_messages = {
- make_message(0, "test_tag", "duplicate"),
- make_message(1, "test_tag", "duplicate"),
- make_message(2, "test_tag", "not_same"),
- make_message(3, "test_tag", "duplicate"),
- make_message(4, "test_tag", "duplicate"),
- make_message(5, "test_tag", "not_same"),
- make_message(6, "test_tag", "duplicate"),
- make_message(7, "test_tag", "duplicate"),
- make_message(8, "test_tag", "duplicate"),
- make_message(9, "test_tag", "not_same"),
- make_message(10, "test_tag", "duplicate"),
- make_message(11, "test_tag", "duplicate"),
- make_message(12, "test_tag", "duplicate"),
- make_message(13, "test_tag", "duplicate"),
- make_message(14, "test_tag", "duplicate"),
- make_message(15, "test_tag", "duplicate"),
- make_message(16, "test_tag", "not_same"),
- make_message(100, "test_tag", "duplicate"),
- make_message(200, "test_tag", "duplicate"),
- make_message(300, "test_tag", "duplicate"),
- };
- // clang-format on
- FixupMessages(&log_messages);
- LogMessages(log_messages);
-
- std::vector<LogMessage> read_log_messages;
- std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
- log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
-
- std::vector<LogMessage> expected_log_messages = {
- make_message(0, "test_tag", "duplicate"),
- make_message(1, "test_tag", "duplicate"),
- make_message(2, "test_tag", "not_same"),
- make_message(3, "test_tag", "duplicate"),
- make_message(4, "test_tag", "duplicate"),
- make_message(5, "test_tag", "not_same"),
- // 3 duplicate logs together print the first, a 1 count chatty message, then the last.
- make_message(6, "test_tag", "duplicate"),
- make_message(7, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ expire 1 line", true),
- make_message(8, "test_tag", "duplicate"),
- make_message(9, "test_tag", "not_same"),
- // 6 duplicate logs together print the first, a 4 count chatty message, then the last.
- make_message(10, "test_tag", "duplicate"),
- make_message(14, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ expire 4 lines", true),
- make_message(15, "test_tag", "duplicate"),
- make_message(16, "test_tag", "not_same"),
- // duplicate logs > 1 minute apart are not deduplicated.
- make_message(100, "test_tag", "duplicate"),
- make_message(200, "test_tag", "duplicate"),
- make_message(300, "test_tag", "duplicate"),
- };
- FixupMessages(&expected_log_messages);
- CompareLogMessages(expected_log_messages, read_log_messages);
-};
-
-TEST_F(LogBufferTest, deduplication_overflow) {
- auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
- bool regex = false) -> LogMessage {
- logger_entry entry = {
- .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
- std::string message;
- message.push_back(ANDROID_LOG_INFO);
- message.append(tag);
- message.push_back('\0');
- message.append(msg);
- message.push_back('\0');
- return {entry, message, regex};
- };
-
- uint32_t sec = 0;
- std::vector<LogMessage> log_messages = {
- make_message(sec++, "test_tag", "normal"),
- };
- size_t expired_per_chatty_message = std::numeric_limits<uint16_t>::max();
- for (size_t i = 0; i < expired_per_chatty_message + 3; ++i) {
- log_messages.emplace_back(make_message(sec++, "test_tag", "duplicate"));
- }
- log_messages.emplace_back(make_message(sec++, "test_tag", "normal"));
- FixupMessages(&log_messages);
- LogMessages(log_messages);
-
- std::vector<LogMessage> read_log_messages;
- std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
- log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
-
- std::vector<LogMessage> expected_log_messages = {
- make_message(0, "test_tag", "normal"),
- make_message(1, "test_tag", "duplicate"),
- make_message(expired_per_chatty_message + 1, "chatty",
- "uid=0\\([^\\)]+\\) [^ ]+ expire 65535 lines", true),
- make_message(expired_per_chatty_message + 2, "chatty",
- "uid=0\\([^\\)]+\\) [^ ]+ expire 1 line", true),
- make_message(expired_per_chatty_message + 3, "test_tag", "duplicate"),
- make_message(expired_per_chatty_message + 4, "test_tag", "normal"),
- };
- FixupMessages(&expected_log_messages);
- CompareLogMessages(expected_log_messages, read_log_messages);
-}
-
-TEST_F(LogBufferTest, deduplication_liblog) {
- auto make_message = [&](uint32_t sec, int32_t tag, int32_t count) -> LogMessage {
- logger_entry entry = {
- .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_EVENTS, .uid = 0};
- android_log_event_int_t liblog_event = {
- .header.tag = tag, .payload.type = EVENT_TYPE_INT, .payload.data = count};
- return {entry, std::string(reinterpret_cast<char*>(&liblog_event), sizeof(liblog_event)),
- false};
- };
-
- // LIBLOG_LOG_TAG
- std::vector<LogMessage> log_messages = {
- make_message(0, 1234, 1),
- make_message(1, LIBLOG_LOG_TAG, 3),
- make_message(2, 1234, 2),
- make_message(3, LIBLOG_LOG_TAG, 3),
- make_message(4, LIBLOG_LOG_TAG, 4),
- make_message(5, 1234, 223),
- make_message(6, LIBLOG_LOG_TAG, 2),
- make_message(7, LIBLOG_LOG_TAG, 3),
- make_message(8, LIBLOG_LOG_TAG, 4),
- make_message(9, 1234, 227),
- make_message(10, LIBLOG_LOG_TAG, 1),
- make_message(11, LIBLOG_LOG_TAG, 3),
- make_message(12, LIBLOG_LOG_TAG, 2),
- make_message(13, LIBLOG_LOG_TAG, 3),
- make_message(14, LIBLOG_LOG_TAG, 5),
- make_message(15, 1234, 227),
- make_message(16, LIBLOG_LOG_TAG, 2),
- make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
- make_message(18, LIBLOG_LOG_TAG, 3),
- make_message(19, LIBLOG_LOG_TAG, 5),
- make_message(20, 1234, 227),
- };
- FixupMessages(&log_messages);
- LogMessages(log_messages);
-
- std::vector<LogMessage> read_log_messages;
- std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
- log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr);
-
- std::vector<LogMessage> expected_log_messages = {
- make_message(0, 1234, 1),
- make_message(1, LIBLOG_LOG_TAG, 3),
- make_message(2, 1234, 2),
- make_message(3, LIBLOG_LOG_TAG, 3),
- make_message(4, LIBLOG_LOG_TAG, 4),
- make_message(5, 1234, 223),
- // More than 2 liblog events (3 here), sum their value into the third message.
- make_message(6, LIBLOG_LOG_TAG, 2),
- make_message(8, LIBLOG_LOG_TAG, 7),
- make_message(9, 1234, 227),
- // More than 2 liblog events (5 here), sum their value into the third message.
- make_message(10, LIBLOG_LOG_TAG, 1),
- make_message(14, LIBLOG_LOG_TAG, 13),
- make_message(15, 1234, 227),
- // int32_t max is the max for a chatty message, beyond that we must use new messages.
- make_message(16, LIBLOG_LOG_TAG, 2),
- make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
- make_message(19, LIBLOG_LOG_TAG, 8),
- make_message(20, 1234, 227),
- };
- FixupMessages(&expected_log_messages);
- CompareLogMessages(expected_log_messages, read_log_messages);
-};
\ No newline at end of file
+INSTANTIATE_TEST_CASE_P(LogBufferTests, LogBufferTest, testing::Values("chatty", "simple"));
diff --git a/logd/LogBufferTest.h b/logd/LogBufferTest.h
new file mode 100644
index 0000000..1659f12
--- /dev/null
+++ b/logd/LogBufferTest.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "ChattyLogBuffer.h"
+#include "LogReaderList.h"
+#include "LogStatistics.h"
+#include "LogTags.h"
+#include "LogWhiteBlackList.h"
+#include "SimpleLogBuffer.h"
+
+struct LogMessage {
+ logger_entry entry;
+ std::string message;
+ bool regex_compare = false; // Only set for expected messages, when true 'message' should be
+ // interpretted as a regex.
+};
+
+// Compares the ordered list of expected and result, causing a test failure with appropriate
+// information on failure.
+void CompareLogMessages(const std::vector<LogMessage>& expected,
+ const std::vector<LogMessage>& result);
+// Sets hdr_size and len parameters appropriately.
+void FixupMessages(std::vector<LogMessage>* messages);
+
+class TestWriter : public LogWriter {
+ public:
+ TestWriter(std::vector<LogMessage>* msgs, bool* released)
+ : LogWriter(0, true, true), msgs_(msgs), released_(released) {}
+ bool Write(const logger_entry& entry, const char* message) override {
+ msgs_->emplace_back(LogMessage{entry, std::string(message, entry.len), false});
+ return true;
+ }
+
+ void Release() {
+ if (released_) *released_ = true;
+ }
+
+ std::string name() const override { return "test_writer"; }
+
+ private:
+ std::vector<LogMessage>* msgs_;
+ bool* released_;
+};
+
+class LogBufferTest : public testing::TestWithParam<std::string> {
+ protected:
+ void SetUp() override {
+ if (GetParam() == "chatty") {
+ log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_));
+ } else if (GetParam() == "simple") {
+ log_buffer_.reset(new SimpleLogBuffer(&reader_list_, &tags_, &stats_));
+ } else {
+ FAIL() << "Unknown buffer type selected for test";
+ }
+ }
+
+ void LogMessages(const std::vector<LogMessage>& messages) {
+ for (auto& [entry, message, _] : messages) {
+ log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec),
+ entry.uid, entry.pid, entry.tid, message.c_str(), message.size());
+ }
+ }
+
+ LogReaderList reader_list_;
+ LogTags tags_;
+ PruneList prune_;
+ LogStatistics stats_{false};
+ std::unique_ptr<LogBuffer> log_buffer_;
+};
diff --git a/logd/LogReader.cpp b/logd/LogReader.cpp
index 89562a4..234ddc7 100644
--- a/logd/LogReader.cpp
+++ b/logd/LogReader.cpp
@@ -200,7 +200,7 @@
if (nonBlock) {
return false;
}
- sequence = LogBufferElement::getCurrentSequence();
+ sequence = log_buffer_->sequence();
}
}
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index 14bcb63..bb7621d 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -79,12 +79,9 @@
}
}
-void LogStatistics::AddTotal(LogBufferElement* element) {
+void LogStatistics::AddTotal(log_id_t log_id, uint16_t size) {
auto lock = std::lock_guard{lock_};
- if (element->getDropped()) return;
- log_id_t log_id = element->getLogId();
- uint16_t size = element->getMsgLen();
mSizesTotal[log_id] += size;
SizesTotal += size;
++mElementsTotal[log_id];
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
index 53c9bb6..7d13ff7 100644
--- a/logd/LogStatistics.h
+++ b/logd/LogStatistics.h
@@ -666,7 +666,7 @@
public:
LogStatistics(bool enable_statistics);
- void AddTotal(LogBufferElement* entry) EXCLUDES(lock_);
+ void AddTotal(log_id_t log_id, uint16_t size) EXCLUDES(lock_);
void Add(LogBufferElement* entry) EXCLUDES(lock_);
void Subtract(LogBufferElement* entry) EXCLUDES(lock_);
// entry->setDropped(1) must follow this call
diff --git a/logd/LogTags.h b/logd/LogTags.h
index e4d165a..cce700c 100644
--- a/logd/LogTags.h
+++ b/logd/LogTags.h
@@ -14,13 +14,13 @@
* limitations under the License.
*/
-#ifndef _LOGD_LOG_TAGS_H__
-#define _LOGD_LOG_TAGS_H__
+#pragma once
#include <string>
#include <unordered_map>
#include <unordered_set>
+#include <private/android_filesystem_config.h>
#include <utils/RWLock.h>
class LogTags {
@@ -120,5 +120,3 @@
std::string formatGetEventTag(uid_t uid, const char* name,
const char* format);
};
-
-#endif // _LOGD_LOG_TAGS_H__
diff --git a/logd/SimpleLogBuffer.cpp b/logd/SimpleLogBuffer.cpp
new file mode 100644
index 0000000..1c83428
--- /dev/null
+++ b/logd/SimpleLogBuffer.cpp
@@ -0,0 +1,337 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SimpleLogBuffer.h"
+
+#include "LogBufferElement.h"
+
+SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats)
+ : reader_list_(reader_list), tags_(tags), stats_(stats) {
+ Init();
+}
+
+SimpleLogBuffer::~SimpleLogBuffer() {}
+
+void SimpleLogBuffer::Init() {
+ log_id_for_each(i) {
+ if (SetSize(i, __android_logger_get_buffer_size(i))) {
+ SetSize(i, LOG_BUFFER_MIN_SIZE);
+ }
+ }
+
+ // Release any sleeping reader threads to dump their current content.
+ auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
+ for (const auto& reader_thread : reader_list_->reader_threads()) {
+ reader_thread->triggerReader_Locked();
+ }
+}
+
+std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) {
+ auto it = logs().begin();
+ if (oldest_[log_id]) {
+ it = *oldest_[log_id];
+ }
+ while (it != logs().end() && it->getLogId() != log_id) {
+ it++;
+ }
+ if (it != logs().end()) {
+ oldest_[log_id] = it;
+ }
+ return it;
+}
+
+bool SimpleLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) {
+ if (log_id == LOG_ID_SECURITY) {
+ return true;
+ }
+
+ int prio = ANDROID_LOG_INFO;
+ const char* tag = nullptr;
+ size_t tag_len = 0;
+ if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
+ if (len < sizeof(android_event_header_t)) {
+ return false;
+ }
+ int32_t numeric_tag = reinterpret_cast<const android_event_header_t*>(msg)->tag;
+ tag = tags_->tagToName(numeric_tag);
+ if (tag) {
+ tag_len = strlen(tag);
+ }
+ } else {
+ prio = *msg;
+ tag = msg + 1;
+ tag_len = strnlen(tag, len - 1);
+ }
+ return __android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE);
+}
+
+int SimpleLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
+ const char* msg, uint16_t len) {
+ if (log_id >= LOG_ID_MAX) {
+ return -EINVAL;
+ }
+
+ if (!ShouldLog(log_id, msg, len)) {
+ // Log traffic received to total
+ stats_->AddTotal(log_id, len);
+ return -EACCES;
+ }
+
+ // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
+ // This prevents any chance that an outside source can request an
+ // exact entry with time specified in ms or us precision.
+ if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
+
+ auto lock = std::lock_guard{lock_};
+ auto sequence = sequence_.fetch_add(1, std::memory_order_relaxed);
+ LogInternal(LogBufferElement(log_id, realtime, uid, pid, tid, sequence, msg, len));
+ return len;
+}
+
+void SimpleLogBuffer::LogInternal(LogBufferElement&& elem) {
+ log_id_t log_id = elem.getLogId();
+
+ logs_.emplace_back(std::move(elem));
+ stats_->Add(&logs_.back());
+ MaybePrune(log_id);
+ reader_list_->NotifyNewLog(1 << log_id);
+}
+
+uint64_t SimpleLogBuffer::FlushTo(
+ LogWriter* writer, uint64_t start, pid_t* last_tid,
+ const std::function<FlushToResult(const LogBufferElement* element)>& filter) {
+ auto shared_lock = SharedLock{lock_};
+
+ std::list<LogBufferElement>::iterator it;
+ if (start <= 1) {
+ // client wants to start from the beginning
+ it = logs_.begin();
+ } else {
+ // Client wants to start from some specified time. Chances are
+ // we are better off starting from the end of the time sorted list.
+ for (it = logs_.end(); it != logs_.begin();
+ /* do nothing */) {
+ --it;
+ if (it->getSequence() <= start) {
+ it++;
+ break;
+ }
+ }
+ }
+
+ uint64_t curr = start;
+
+ for (; it != logs_.end(); ++it) {
+ LogBufferElement& element = *it;
+
+ if (!writer->privileged() && element.getUid() != writer->uid()) {
+ continue;
+ }
+
+ if (!writer->can_read_security_logs() && element.getLogId() == LOG_ID_SECURITY) {
+ continue;
+ }
+
+ if (filter) {
+ FlushToResult ret = filter(&element);
+ if (ret == FlushToResult::kSkip) {
+ continue;
+ }
+ if (ret == FlushToResult::kStop) {
+ break;
+ }
+ }
+
+ bool same_tid = false;
+ if (last_tid) {
+ same_tid = last_tid[element.getLogId()] == element.getTid();
+ // Dropped (chatty) immediately following a valid log from the
+ // same source in the same log buffer indicates we have a
+ // multiple identical squash. chatty that differs source
+ // is due to spam filter. chatty to chatty of different
+ // source is also due to spam filter.
+ last_tid[element.getLogId()] =
+ (element.getDropped() && !same_tid) ? 0 : element.getTid();
+ }
+
+ shared_lock.unlock();
+
+ // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
+ // `element` pointer is safe here without the lock
+ curr = element.getSequence();
+ if (!element.FlushTo(writer, stats_, same_tid)) {
+ return FLUSH_ERROR;
+ }
+
+ shared_lock.lock_shared();
+ }
+
+ return curr;
+}
+
+// clear all rows of type "id" from the buffer.
+bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
+ bool busy = true;
+ // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
+ for (int retry = 4;;) {
+ if (retry == 1) { // last pass
+ // Check if it is still busy after the sleep, we say prune
+ // one entry, not another clear run, so we are looking for
+ // the quick side effect of the return value to tell us if
+ // we have a _blocked_ reader.
+ {
+ auto lock = std::lock_guard{lock_};
+ busy = Prune(id, 1, uid);
+ }
+ // It is still busy, blocked reader(s), lets kill them all!
+ // otherwise, lets be a good citizen and preserve the slow
+ // readers and let the clear run (below) deal with determining
+ // if we are still blocked and return an error code to caller.
+ if (busy) {
+ auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
+ for (const auto& reader_thread : reader_list_->reader_threads()) {
+ if (reader_thread->IsWatching(id)) {
+ android::prdebug("Kicking blocked reader, %s, from LogBuffer::clear()\n",
+ reader_thread->name().c_str());
+ reader_thread->release_Locked();
+ }
+ }
+ }
+ }
+ {
+ auto lock = std::lock_guard{lock_};
+ busy = Prune(id, ULONG_MAX, uid);
+ }
+
+ if (!busy || !--retry) {
+ break;
+ }
+ sleep(1); // Let reader(s) catch up after notification
+ }
+ return busy;
+}
+
+// get the total space allocated to "id"
+unsigned long SimpleLogBuffer::GetSize(log_id_t id) {
+ auto lock = SharedLock{lock_};
+ size_t retval = max_size_[id];
+ return retval;
+}
+
+// set the total space allocated to "id"
+int SimpleLogBuffer::SetSize(log_id_t id, unsigned long size) {
+ // Reasonable limits ...
+ if (!__android_logger_valid_buffer_size(size)) {
+ return -1;
+ }
+
+ auto lock = std::lock_guard{lock_};
+ max_size_[id] = size;
+ return 0;
+}
+
+void SimpleLogBuffer::MaybePrune(log_id_t id) {
+ unsigned long prune_rows;
+ if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) {
+ Prune(id, prune_rows, 0);
+ }
+}
+
+bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) {
+ auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
+
+ // Don't prune logs that are newer than the point at which any reader threads are reading from.
+ LogReaderThread* oldest = nullptr;
+ for (const auto& reader_thread : reader_list_->reader_threads()) {
+ if (!reader_thread->IsWatching(id)) {
+ continue;
+ }
+ if (!oldest || oldest->start() > reader_thread->start() ||
+ (oldest->start() == reader_thread->start() &&
+ reader_thread->deadline().time_since_epoch().count() != 0)) {
+ oldest = reader_thread.get();
+ }
+ }
+
+ auto it = GetOldest(id);
+
+ while (it != logs_.end()) {
+ LogBufferElement& element = *it;
+
+ if (element.getLogId() != id) {
+ ++it;
+ continue;
+ }
+
+ if (caller_uid != 0 && element.getUid() != caller_uid) {
+ ++it;
+ continue;
+ }
+
+ if (oldest && oldest->start() <= element.getSequence()) {
+ KickReader(oldest, id, prune_rows);
+ return true;
+ }
+
+ stats_->Subtract(&element);
+ it = Erase(it);
+ if (--prune_rows == 0) {
+ return false;
+ }
+ }
+ return false;
+}
+
+std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase(
+ std::list<LogBufferElement>::iterator it) {
+ bool oldest_is_it[LOG_ID_MAX];
+ log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; }
+
+ it = logs_.erase(it);
+
+ log_id_for_each(i) {
+ if (oldest_is_it[i]) {
+ if (__predict_false(it == logs().end())) {
+ oldest_[i] = std::nullopt;
+ } else {
+ oldest_[i] = it; // Store the next iterator even if it does not correspond to
+ // the same log_id, as a starting point for GetOldest().
+ }
+ }
+ }
+
+ return it;
+}
+
+// If the selected reader is blocking our pruning progress, decide on
+// what kind of mitigation is necessary to unblock the situation.
+void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) {
+ if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
+ // A misbehaving or slow reader has its connection
+ // dropped if we hit too much memory pressure.
+ android::prdebug("Kicking blocked reader, %s, from LogBuffer::kickMe()\n",
+ reader->name().c_str());
+ reader->release_Locked();
+ } else if (reader->deadline().time_since_epoch().count() != 0) {
+ // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
+ reader->triggerReader_Locked();
+ } else {
+ // tell slow reader to skip entries to catch up
+ android::prdebug("Skipping %lu entries from slow reader, %s, from LogBuffer::kickMe()\n",
+ prune_rows, reader->name().c_str());
+ reader->triggerSkip_Locked(id, prune_rows);
+ }
+}
diff --git a/logd/SimpleLogBuffer.h b/logd/SimpleLogBuffer.h
new file mode 100644
index 0000000..9a2d01a
--- /dev/null
+++ b/logd/SimpleLogBuffer.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <atomic>
+#include <list>
+#include <mutex>
+
+#include "LogBuffer.h"
+#include "LogBufferElement.h"
+#include "LogReaderList.h"
+#include "LogStatistics.h"
+#include "LogTags.h"
+#include "rwlock.h"
+
+class SimpleLogBuffer : public LogBuffer {
+ public:
+ SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats);
+ ~SimpleLogBuffer();
+ void Init() override;
+
+ int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, const char* msg,
+ uint16_t len) override;
+ uint64_t FlushTo(
+ LogWriter* writer, uint64_t start, pid_t* lastTid,
+ const std::function<FlushToResult(const LogBufferElement* element)>& filter) override;
+
+ bool Clear(log_id_t id, uid_t uid) override;
+ unsigned long GetSize(log_id_t id) override;
+ int SetSize(log_id_t id, unsigned long size) override;
+
+ uint64_t sequence() const override { return sequence_.load(std::memory_order_relaxed); }
+
+ protected:
+ virtual bool Prune(log_id_t id, unsigned long prune_rows, uid_t uid) REQUIRES(lock_);
+ virtual void LogInternal(LogBufferElement&& elem) REQUIRES(lock_);
+
+ // Returns an iterator to the oldest element for a given log type, or logs_.end() if
+ // there are no logs for the given log type. Requires logs_lock_ to be held.
+ std::list<LogBufferElement>::iterator GetOldest(log_id_t log_id) REQUIRES(lock_);
+ std::list<LogBufferElement>::iterator Erase(std::list<LogBufferElement>::iterator it)
+ REQUIRES(lock_);
+ void KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows)
+ REQUIRES_SHARED(lock_);
+
+ LogStatistics* stats() { return stats_; }
+ LogReaderList* reader_list() { return reader_list_; }
+ unsigned long max_size(log_id_t id) REQUIRES_SHARED(lock_) { return max_size_[id]; }
+ std::list<LogBufferElement>& logs() { return logs_; }
+
+ RwLock lock_;
+
+ private:
+ bool ShouldLog(log_id_t log_id, const char* msg, uint16_t len);
+ void MaybePrune(log_id_t id) REQUIRES(lock_);
+
+ LogReaderList* reader_list_;
+ LogTags* tags_;
+ LogStatistics* stats_;
+
+ std::atomic<uint64_t> sequence_ = 1;
+
+ unsigned long max_size_[LOG_ID_MAX] GUARDED_BY(lock_);
+ std::list<LogBufferElement> logs_ GUARDED_BY(lock_);
+ // Keeps track of the iterator to the oldest log message of a given log type, as an
+ // optimization when pruning logs. Use GetOldest() to retrieve.
+ std::optional<std::list<LogBufferElement>::iterator> oldest_[LOG_ID_MAX] GUARDED_BY(lock_);
+};
diff --git a/logd/main.cpp b/logd/main.cpp
index 1deca72..c2b5a1d 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -56,6 +56,7 @@
#include "LogStatistics.h"
#include "LogTags.h"
#include "LogUtils.h"
+#include "SimpleLogBuffer.h"
#define KMSG_PRIORITY(PRI) \
'<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
@@ -287,9 +288,13 @@
// entries.
LogReaderList reader_list;
- // LogBuffer is the object which is responsible for holding all
- // log entries.
- LogBuffer* logBuf = new ChattyLogBuffer(&reader_list, &log_tags, &prune_list, &log_statistics);
+ // LogBuffer is the object which is responsible for holding all log entries.
+ LogBuffer* logBuf;
+ if (true) {
+ logBuf = new ChattyLogBuffer(&reader_list, &log_tags, &prune_list, &log_statistics);
+ } else {
+ logBuf = new SimpleLogBuffer(&reader_list, &log_tags, &log_statistics);
+ }
// LogReader listens on /dev/socket/logdr. When a client
// connects, log entries in the LogBuffer are written to the client.
diff --git a/logd/rwlock.h b/logd/rwlock.h
new file mode 100644
index 0000000..2b27ff1
--- /dev/null
+++ b/logd/rwlock.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <pthread.h>
+
+#include <android-base/macros.h>
+#include <android-base/thread_annotations.h>
+
+// As of the end of May 2020, std::shared_mutex is *not* simply a pthread_rwlock, but rather a
+// combination of std::mutex and std::condition variable, which is obviously less efficient. This
+// immitates what std::shared_mutex should be doing and is compatible with RAII thread wrappers.
+
+class SHARED_CAPABILITY("mutex") RwLock {
+ public:
+ RwLock() {}
+ ~RwLock() {}
+
+ void lock() ACQUIRE() { pthread_rwlock_wrlock(&rwlock_); }
+ void lock_shared() ACQUIRE_SHARED() { pthread_rwlock_rdlock(&rwlock_); }
+
+ void unlock() RELEASE() { pthread_rwlock_unlock(&rwlock_); }
+
+ private:
+ pthread_rwlock_t rwlock_ = PTHREAD_RWLOCK_INITIALIZER;
+};
+
+// std::shared_lock does not have thread annotations, so we need our own.
+
+class SCOPED_CAPABILITY SharedLock {
+ public:
+ SharedLock(RwLock& lock) ACQUIRE_SHARED(lock) : lock_(lock) { lock_.lock_shared(); }
+ ~SharedLock() RELEASE() { lock_.unlock(); }
+
+ void lock_shared() ACQUIRE_SHARED() { lock_.lock_shared(); }
+ void unlock() RELEASE() { lock_.unlock(); }
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(SharedLock);
+
+ private:
+ RwLock& lock_;
+};