1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <LogSize.h>
18
19 #include <array>
20 #include <optional>
21 #include <string>
22
23 #include <android-base/parseint.h>
24 #include <android-base/properties.h>
25
IsValidBufferSize(size_t value)26 bool IsValidBufferSize(size_t value) {
27 return kLogBufferMinSize <= value && value <= kLogBufferMaxSize;
28 }
29
GetBufferSizeProperty(const std::string & key)30 static std::optional<size_t> GetBufferSizeProperty(const std::string& key) {
31 std::string value = android::base::GetProperty(key, "");
32 if (value.empty()) {
33 return {};
34 }
35
36 uint32_t size;
37 if (!android::base::ParseByteCount(value, &size)) {
38 return {};
39 }
40
41 if (!IsValidBufferSize(size)) {
42 return {};
43 }
44
45 return size;
46 }
47
GetBufferSizePropertyOverride(log_id_t log_id)48 static std::optional<size_t> GetBufferSizePropertyOverride(log_id_t log_id) {
49 std::string buffer_name = android_log_id_to_name(log_id);
50 std::array<std::string, 4> properties = {
51 "persist.logd.size." + buffer_name,
52 "ro.logd.size." + buffer_name,
53 "persist.logd.size",
54 "ro.logd.size",
55 };
56
57 for (const auto& property : properties) {
58 if (auto size = GetBufferSizeProperty(property)) {
59 return size;
60 }
61 }
62 return {};
63 }
64
65 /* This method should only be used for debuggable devices. */
isAllowedToOverrideBufferSize()66 static bool isAllowedToOverrideBufferSize() {
67 const auto hwType = android::base::GetProperty("ro.hardware.type", "");
68 /* We allow automotive devices to optionally override the default. */
69 return (hwType == "automotive");
70 }
71
GetBufferSizeFromProperties(log_id_t log_id)72 size_t GetBufferSizeFromProperties(log_id_t log_id) {
73 /*
74 * http://b/196856709
75 *
76 * We've been seeing timeouts from logcat in bugreports for years, but the
77 * rate has gone way up lately. The suspicion is that this is because we
78 * have a lot of dogfooders who still have custom (large) log sizes but
79 * the new compressed logging is cramming way more in. The bugreports I've seen
80 * have had 1,000,000+ lines, so taking 10s to collect that much logging seems
81 * plausible. Of course, it's also possible that logcat is timing out because
82 * the log is being *spammed* as it's being read. But temporarily disabling
83 * custom log sizes like this should help us confirm (or deny) whether the
84 * problem really is this simple.
85 */
86 static const bool isDebuggable = android::base::GetBoolProperty("ro.debuggable", false);
87 if (isDebuggable) {
88 static const bool mayOverride = isAllowedToOverrideBufferSize();
89 if (mayOverride) {
90 if (auto size = GetBufferSizePropertyOverride(log_id)) {
91 return *size;
92 }
93 }
94 } else {
95 static const bool isLowRam = android::base::GetBoolProperty("ro.config.low_ram", false);
96 /*
97 * For non-debuggable low_ram devices, we want to save memory here and
98 * use the minimum size.
99 */
100 if (isLowRam) {
101 return kLogBufferMinSize;
102 }
103 }
104
105 return kDefaultLogBufferSize;
106 }
107