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
GetBufferSizeFromProperties(log_id_t log_id)48 size_t GetBufferSizeFromProperties(log_id_t log_id) {
49 /*
50 * http://b/196856709
51 *
52 * We've been seeing timeouts from logcat in bugreports for years, but the
53 * rate has gone way up lately. The suspicion is that this is because we
54 * have a lot of dogfooders who still have custom (large) log sizes but
55 * the new compressed logging is cramming way more in. The bugreports I've seen
56 * have had 1,000,000+ lines, so taking 10s to collect that much logging seems
57 * plausible. Of course, it's also possible that logcat is timing out because
58 * the log is being *spammed* as it's being read. But temporarily disabling
59 * custom log sizes like this should help us confirm (or deny) whether the
60 * problem really is this simple.
61 */
62 const auto hwType = android::base::GetProperty("ro.hardware.type", "");
63 const auto isDebuggable = android::base::GetBoolProperty("ro.debuggable", false);
64 if (hwType == "automotive" && isDebuggable) {
65 std::string buffer_name = android_log_id_to_name(log_id);
66 std::array<std::string, 4> properties = {
67 "persist.logd.size." + buffer_name,
68 "ro.logd.size." + buffer_name,
69 "persist.logd.size",
70 "ro.logd.size",
71 };
72
73 for (const auto& property : properties) {
74 if (auto size = GetBufferSizeProperty(property)) {
75 return *size;
76 }
77 }
78 }
79
80 if (android::base::GetBoolProperty("ro.config.low_ram", false)) {
81 return kLogBufferMinSize;
82 }
83
84 return kDefaultLogBufferSize;
85 }
86