Merge changes I957c519b,Iaf4e389e,I92f8056b

* changes:
  logd: remove faulty optimization
  logd: don't store the map key in its value
  logd: use libbase logging
This commit is contained in:
Tom Cherry 2020-06-05 15:21:13 +00:00 committed by Gerrit Code Review
commit a1f1775172
11 changed files with 153 additions and 242 deletions

View file

@ -217,12 +217,12 @@ LogBufferElementCollection::iterator ChattyLogBuffer::Erase(LogBufferElementColl
log_id_for_each(i) { log_id_for_each(i) {
for (auto b : mLastWorst[i]) { for (auto b : mLastWorst[i]) {
if (bad == b.second) { if (bad == b.second) {
android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i, b.first, key); LOG(ERROR) << StringPrintf("stale mLastWorst[%d] key=%d mykey=%d", i, b.first, key);
} }
} }
for (auto b : mLastWorstPidOfSystem[i]) { for (auto b : mLastWorstPidOfSystem[i]) {
if (bad == b.second) { if (bad == b.second) {
android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i, b.first); LOG(ERROR) << StringPrintf("stale mLastWorstPidOfSystem[%d] pid=%d", i, b.first);
} }
} }
} }

View file

@ -31,6 +31,7 @@
#include <string> #include <string>
#include <android-base/logging.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <log/log_properties.h> #include <log/log_properties.h>
@ -298,7 +299,7 @@ int CommandListener::ReinitCmd::runCommand(SocketClient* cli, int /*argc*/,
char** /*argv*/) { char** /*argv*/) {
setname(); setname();
android::prdebug("logd reinit"); LOG(INFO) << "logd reinit";
buf()->Init(); buf()->Init();
prune()->init(nullptr); prune()->init(nullptr);

View file

@ -43,14 +43,6 @@ bool __android_logger_valid_buffer_size(unsigned long) {
} }
#endif #endif
void android::prdebug(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
char* android::uidToName(uid_t) { char* android::uidToName(uid_t) {
return nullptr; return nullptr;
} }

View file

@ -23,6 +23,7 @@
#include <chrono> #include <chrono>
#include <android-base/logging.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
@ -201,9 +202,9 @@ bool LogReader::onDataAvailable(SocketClient* cli) {
} }
} }
android::prdebug( LOG(INFO) << android::base::StringPrintf(
"logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d " "logdr: UID=%d GID=%d PID=%d %c tail=%lu logMask=%x pid=%d "
"start=%" PRIu64 "ns deadline=%" PRIi64 "ns\n", "start=%" PRIu64 "ns deadline=%" PRIi64 "ns",
cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail, logMask, cli->getUid(), cli->getGid(), cli->getPid(), nonBlock ? 'n' : 'b', tail, logMask,
(int)pid, start.nsec(), static_cast<int64_t>(deadline.time_since_epoch().count())); (int)pid, start.nsec(), static_cast<int64_t>(deadline.time_since_epoch().count()));

View file

@ -36,6 +36,30 @@ static const uint64_t monthSec = 31 * 24 * hourSec;
std::atomic<size_t> LogStatistics::SizesTotal; std::atomic<size_t> LogStatistics::SizesTotal;
static std::string TagNameKey(const LogStatisticsElement& element) {
if (IsBinary(element.log_id)) {
uint32_t tag = element.tag;
if (tag) {
const char* cp = android::tagToName(tag);
if (cp) {
return std::string(cp);
}
}
return android::base::StringPrintf("[%" PRIu32 "]", tag);
}
const char* msg = element.msg;
if (!msg) {
return "chatty";
}
++msg;
uint16_t len = element.msg_len;
len = (len <= 1) ? 0 : strnlen(msg, len - 1);
if (!len) {
return "<NULL>";
}
return std::string(msg, len);
}
LogStatistics::LogStatistics(bool enable_statistics) : enable(enable_statistics) { LogStatistics::LogStatistics(bool enable_statistics) : enable(enable_statistics) {
log_time now(CLOCK_REALTIME); log_time now(CLOCK_REALTIME);
log_id_for_each(id) { log_id_for_each(id) {
@ -308,8 +332,9 @@ template <typename TKey, typename TEntry>
void LogStatistics::WorstTwoWithThreshold(const LogHashtable<TKey, TEntry>& table, size_t threshold, void LogStatistics::WorstTwoWithThreshold(const LogHashtable<TKey, TEntry>& table, size_t threshold,
int* worst, size_t* worst_sizes, int* worst, size_t* worst_sizes,
size_t* second_worst_sizes) const { size_t* second_worst_sizes) const {
std::array<const TKey*, 2> max_keys;
std::array<const TEntry*, 2> max_entries; std::array<const TEntry*, 2> max_entries;
table.MaxEntries(AID_ROOT, 0, &max_entries); table.MaxEntries(AID_ROOT, 0, max_keys, max_entries);
if (max_entries[0] == nullptr || max_entries[1] == nullptr) { if (max_entries[0] == nullptr || max_entries[1] == nullptr) {
return; return;
} }
@ -317,7 +342,7 @@ void LogStatistics::WorstTwoWithThreshold(const LogHashtable<TKey, TEntry>& tabl
// b/24782000: Allow time horizon to extend roughly tenfold, assume average entry length is // b/24782000: Allow time horizon to extend roughly tenfold, assume average entry length is
// 100 characters. // 100 characters.
if (*worst_sizes > threshold && *worst_sizes > (10 * max_entries[0]->dropped_count())) { if (*worst_sizes > threshold && *worst_sizes > (10 * max_entries[0]->dropped_count())) {
*worst = max_entries[0]->key(); *worst = *max_keys[0];
*second_worst_sizes = max_entries[1]->getSizes(); *second_worst_sizes = max_entries[1]->getSizes();
if (*second_worst_sizes < threshold) { if (*second_worst_sizes < threshold) {
*second_worst_sizes = threshold; *second_worst_sizes = threshold;
@ -340,13 +365,14 @@ void LogStatistics::WorstTwoTags(size_t threshold, int* worst, size_t* worst_siz
void LogStatistics::WorstTwoSystemPids(log_id id, size_t worst_uid_sizes, int* worst, void LogStatistics::WorstTwoSystemPids(log_id id, size_t worst_uid_sizes, int* worst,
size_t* second_worst_sizes) const { size_t* second_worst_sizes) const {
auto lock = std::lock_guard{lock_}; auto lock = std::lock_guard{lock_};
std::array<const pid_t*, 2> max_keys;
std::array<const PidEntry*, 2> max_entries; std::array<const PidEntry*, 2> max_entries;
pidSystemTable[id].MaxEntries(AID_SYSTEM, 0, &max_entries); pidSystemTable[id].MaxEntries(AID_SYSTEM, 0, max_keys, max_entries);
if (max_entries[0] == nullptr || max_entries[1] == nullptr) { if (max_entries[0] == nullptr || max_entries[1] == nullptr) {
return; return;
} }
*worst = max_entries[0]->key(); *worst = *max_keys[0];
*second_worst_sizes = worst_uid_sizes - max_entries[0]->getSizes() + max_entries[1]->getSizes(); *second_worst_sizes = worst_uid_sizes - max_entries[0]->getSizes() + max_entries[1]->getSizes();
} }
@ -412,11 +438,12 @@ void LogStatistics::FormatTmp(const char* nameTmp, uid_t uid, std::string& name,
} }
} }
std::string UidEntry::format(const LogStatistics& stat, log_id_t id) const REQUIRES(stat.lock_) { std::string UidEntry::format(const LogStatistics& stat, log_id_t id, uid_t uid) const
std::string name = android::base::StringPrintf("%u", uid_); REQUIRES(stat.lock_) {
std::string name = android::base::StringPrintf("%u", uid);
std::string size = android::base::StringPrintf("%zu", getSizes()); std::string size = android::base::StringPrintf("%zu", getSizes());
stat.FormatTmp(nullptr, uid_, name, size, 6); stat.FormatTmp(nullptr, uid, name, size, 6);
std::string pruned = ""; std::string pruned = "";
if (worstUidEnabledForLogid(id)) { if (worstUidEnabledForLogid(id)) {
@ -474,19 +501,20 @@ std::string UidEntry::format(const LogStatistics& stat, log_id_t id) const REQUI
std::string output = formatLine(name, size, pruned); std::string output = formatLine(name, size, pruned);
if (uid_ != AID_SYSTEM) { if (uid != AID_SYSTEM) {
return output; return output;
} }
static const size_t maximum_sorted_entries = 32; static const size_t maximum_sorted_entries = 32;
std::array<const PidEntry*, maximum_sorted_entries> sorted; std::array<const pid_t*, maximum_sorted_entries> sorted_pids;
stat.pidSystemTable[id].MaxEntries(uid_, 0, &sorted); std::array<const PidEntry*, maximum_sorted_entries> sorted_entries;
stat.pidSystemTable[id].MaxEntries(uid, 0, sorted_pids, sorted_entries);
std::string byPid; std::string byPid;
size_t index; size_t index;
bool hasDropped = false; bool hasDropped = false;
for (index = 0; index < maximum_sorted_entries; ++index) { for (index = 0; index < maximum_sorted_entries; ++index) {
const PidEntry* entry = sorted[index]; const PidEntry* entry = sorted_entries[index];
if (!entry) { if (!entry) {
break; break;
} }
@ -496,7 +524,7 @@ std::string UidEntry::format(const LogStatistics& stat, log_id_t id) const REQUI
if (entry->dropped_count()) { if (entry->dropped_count()) {
hasDropped = true; hasDropped = true;
} }
byPid += entry->format(stat, id); byPid += entry->format(stat, id, *sorted_pids[index]);
} }
if (index > 1) { // print this only if interesting if (index > 1) { // print this only if interesting
std::string ditto("\" "); std::string ditto("\" ");
@ -515,9 +543,9 @@ std::string PidEntry::formatHeader(const std::string& name,
std::string("BYTES"), std::string("NUM")); std::string("BYTES"), std::string("NUM"));
} }
std::string PidEntry::format(const LogStatistics& stat, log_id_t /* id */) const std::string PidEntry::format(const LogStatistics& stat, log_id_t, pid_t pid) const
REQUIRES(stat.lock_) { REQUIRES(stat.lock_) {
std::string name = android::base::StringPrintf("%5u/%u", pid_, uid_); std::string name = android::base::StringPrintf("%5u/%u", pid, uid_);
std::string size = android::base::StringPrintf("%zu", getSizes()); std::string size = android::base::StringPrintf("%zu", getSizes());
stat.FormatTmp(name_, uid_, name, size, 12); stat.FormatTmp(name_, uid_, name, size, 12);
@ -538,9 +566,9 @@ std::string TidEntry::formatHeader(const std::string& name,
std::string("NUM")); std::string("NUM"));
} }
std::string TidEntry::format(const LogStatistics& stat, log_id_t /* id */) const std::string TidEntry::format(const LogStatistics& stat, log_id_t, pid_t tid) const
REQUIRES(stat.lock_) { REQUIRES(stat.lock_) {
std::string name = android::base::StringPrintf("%5u/%u", tid(), uid_); std::string name = android::base::StringPrintf("%5u/%u", tid, uid_);
std::string size = android::base::StringPrintf("%zu", getSizes()); std::string size = android::base::StringPrintf("%zu", getSizes());
stat.FormatTmp(name_, uid_, name, size, 12); stat.FormatTmp(name_, uid_, name, size, 12);
@ -562,8 +590,7 @@ std::string TagEntry::formatHeader(const std::string& name, log_id_t id) const {
std::string("BYTES"), std::string(isprune ? "NUM" : "")); std::string("BYTES"), std::string(isprune ? "NUM" : ""));
} }
std::string TagEntry::format(const LogStatistics& /* stat */, std::string TagEntry::format(const LogStatistics&, log_id_t, uint32_t) const {
log_id_t /* id */) const {
std::string name; std::string name;
if (uid_ == (uid_t)-1) { if (uid_ == (uid_t)-1) {
name = android::base::StringPrintf("%7u", key()); name = android::base::StringPrintf("%7u", key());
@ -594,8 +621,8 @@ std::string TagNameEntry::formatHeader(const std::string& name,
std::string("BYTES"), std::string("")); std::string("BYTES"), std::string(""));
} }
std::string TagNameEntry::format(const LogStatistics& /* stat */, std::string TagNameEntry::format(const LogStatistics&, log_id_t,
log_id_t /* id */) const { const std::string& key_name) const {
std::string name; std::string name;
std::string pidstr; std::string pidstr;
if (pid_ != (pid_t)-1) { if (pid_ != (pid_t)-1) {
@ -616,7 +643,7 @@ std::string TagNameEntry::format(const LogStatistics& /* stat */,
std::string size = android::base::StringPrintf("%zu", getSizes()); std::string size = android::base::StringPrintf("%zu", getSizes());
const char* nameTmp = this->name(); const char* nameTmp = key_name.data();
if (nameTmp) { if (nameTmp) {
size_t lenSpace = std::max(16 - name.length(), (size_t)1); size_t lenSpace = std::max(16 - name.length(), (size_t)1);
size_t len = EntryBase::TOTAL_LEN - EntryBase::PRUNED_LEN - size.length() - name.length() - size_t len = EntryBase::TOTAL_LEN - EntryBase::PRUNED_LEN - size.length() - name.length() -
@ -684,15 +711,16 @@ std::string LogStatistics::FormatTable(const LogHashtable<TKey, TEntry>& table,
REQUIRES(lock_) { REQUIRES(lock_) {
static const size_t maximum_sorted_entries = 32; static const size_t maximum_sorted_entries = 32;
std::string output; std::string output;
std::array<const TEntry*, maximum_sorted_entries> sorted; std::array<const TKey*, maximum_sorted_entries> sorted_keys;
table.MaxEntries(uid, pid, &sorted); std::array<const TEntry*, maximum_sorted_entries> sorted_entries;
table.MaxEntries(uid, pid, sorted_keys, sorted_entries);
bool header_printed = false; bool header_printed = false;
for (size_t index = 0; index < maximum_sorted_entries; ++index) { for (size_t index = 0; index < maximum_sorted_entries; ++index) {
const TEntry* entry = sorted[index]; const TEntry* entry = sorted_entries[index];
if (!entry) { if (!entry) {
break; break;
} }
if (entry->getSizes() <= (sorted[0]->getSizes() / 100)) { if (entry->getSizes() <= (sorted_entries[0]->getSizes() / 100)) {
break; break;
} }
if (!header_printed) { if (!header_printed) {
@ -700,7 +728,7 @@ std::string LogStatistics::FormatTable(const LogHashtable<TKey, TEntry>& table,
output += entry->formatHeader(name, id); output += entry->formatHeader(name, id);
header_printed = true; header_printed = true;
} }
output += entry->format(*this, id); output += entry->format(*this, id, *sorted_keys[index]);
} }
return output; return output;
} }

View file

@ -44,6 +44,8 @@
for (log_id_t i = LOG_ID_MIN; (i) < LOG_ID_MAX; (i) = (log_id_t)((i) + 1)) for (log_id_t i = LOG_ID_MIN; (i) < LOG_ID_MAX; (i) = (log_id_t)((i) + 1))
class LogStatistics; class LogStatistics;
class UidEntry;
class PidEntry;
struct LogStatisticsElement { struct LogStatisticsElement {
uid_t uid; uid_t uid;
@ -95,31 +97,43 @@ class LogHashtable {
// Returns a sorted array of up to len highest entries sorted by size. If fewer than len // Returns a sorted array of up to len highest entries sorted by size. If fewer than len
// entries are found, their positions are set to nullptr. // entries are found, their positions are set to nullptr.
template <size_t len> template <size_t len>
void MaxEntries(uid_t uid, pid_t pid, std::array<const TEntry*, len>* out) const { void MaxEntries(uid_t uid, pid_t pid, std::array<const TKey*, len>& out_keys,
auto& retval = *out; std::array<const TEntry*, len>& out_entries) const {
retval.fill(nullptr); out_keys.fill(nullptr);
for (const_iterator it = map.begin(); it != map.end(); ++it) { out_entries.fill(nullptr);
const TEntry& entry = it->second; for (const auto& [key, entry] : map) {
uid_t entry_uid = 0;
if (uid != AID_ROOT && uid != entry.uid()) { if constexpr (std::is_same_v<TEntry, UidEntry>) {
entry_uid = key;
} else {
entry_uid = entry.uid();
}
if (uid != AID_ROOT && uid != entry_uid) {
continue; continue;
} }
if (pid && entry.pid() && pid != entry.pid()) { pid_t entry_pid = 0;
if constexpr (std::is_same_v<TEntry, PidEntry>) {
entry_pid = key;
} else {
entry_pid = entry.pid();
}
if (pid && entry_pid && pid != entry_pid) {
continue; continue;
} }
size_t sizes = entry.getSizes(); size_t sizes = entry.getSizes();
ssize_t index = len - 1; ssize_t index = len - 1;
while ((!retval[index] || (sizes > retval[index]->getSizes())) && while ((!out_entries[index] || sizes > out_entries[index]->getSizes()) && --index >= 0)
(--index >= 0))
; ;
if (++index < (ssize_t)len) { if (++index < (ssize_t)len) {
size_t num = len - index - 1; size_t num = len - index - 1;
if (num) { if (num) {
memmove(&retval[index + 1], &retval[index], memmove(&out_keys[index + 1], &out_keys[index], num * sizeof(out_keys[0]));
num * sizeof(retval[0])); memmove(&out_entries[index + 1], &out_entries[index],
num * sizeof(out_entries[0]));
} }
retval[index] = &entry; out_keys[index] = &key;
out_entries[index] = &entry;
} }
} }
} }
@ -229,10 +243,8 @@ class EntryBaseDropped : public EntryBase {
class UidEntry : public EntryBaseDropped { class UidEntry : public EntryBaseDropped {
public: public:
explicit UidEntry(const LogStatisticsElement& element) explicit UidEntry(const LogStatisticsElement& element)
: EntryBaseDropped(element), uid_(element.uid), pid_(element.pid) {} : EntryBaseDropped(element), pid_(element.pid) {}
uid_t key() const { return uid_; }
uid_t uid() const { return key(); }
pid_t pid() const { return pid_; } pid_t pid() const { return pid_; }
void Add(const LogStatisticsElement& element) { void Add(const LogStatisticsElement& element) {
@ -243,10 +255,9 @@ class UidEntry : public EntryBaseDropped {
} }
std::string formatHeader(const std::string& name, log_id_t id) const; std::string formatHeader(const std::string& name, log_id_t id) const;
std::string format(const LogStatistics& stat, log_id_t id) const; std::string format(const LogStatistics& stat, log_id_t id, uid_t uid) const;
private: private:
const uid_t uid_;
pid_t pid_; pid_t pid_;
}; };
@ -258,23 +269,16 @@ class PidEntry : public EntryBaseDropped {
public: public:
explicit PidEntry(pid_t pid) explicit PidEntry(pid_t pid)
: EntryBaseDropped(), : EntryBaseDropped(),
pid_(pid),
uid_(android::pidToUid(pid)), uid_(android::pidToUid(pid)),
name_(android::pidToName(pid)) {} name_(android::pidToName(pid)) {}
explicit PidEntry(const LogStatisticsElement& element) explicit PidEntry(const LogStatisticsElement& element)
: EntryBaseDropped(element), : EntryBaseDropped(element), uid_(element.uid), name_(android::pidToName(element.pid)) {}
pid_(element.pid),
uid_(element.uid),
name_(android::pidToName(pid_)) {}
PidEntry(const PidEntry& element) PidEntry(const PidEntry& element)
: EntryBaseDropped(element), : EntryBaseDropped(element),
pid_(element.pid_),
uid_(element.uid_), uid_(element.uid_),
name_(element.name_ ? strdup(element.name_) : nullptr) {} name_(element.name_ ? strdup(element.name_) : nullptr) {}
~PidEntry() { free(name_); } ~PidEntry() { free(name_); }
pid_t key() const { return pid_; }
pid_t pid() const { return key(); }
uid_t uid() const { return uid_; } uid_t uid() const { return uid_; }
const char* name() const { return name_; } const char* name() const { return name_; }
@ -301,10 +305,9 @@ class PidEntry : public EntryBaseDropped {
} }
std::string formatHeader(const std::string& name, log_id_t id) const; std::string formatHeader(const std::string& name, log_id_t id) const;
std::string format(const LogStatistics& stat, log_id_t id) const; std::string format(const LogStatistics& stat, log_id_t id, pid_t pid) const;
private: private:
const pid_t pid_;
uid_t uid_; uid_t uid_;
char* name_; char* name_;
}; };
@ -313,26 +316,21 @@ class TidEntry : public EntryBaseDropped {
public: public:
TidEntry(pid_t tid, pid_t pid) TidEntry(pid_t tid, pid_t pid)
: EntryBaseDropped(), : EntryBaseDropped(),
tid_(tid),
pid_(pid), pid_(pid),
uid_(android::pidToUid(tid)), uid_(android::pidToUid(tid)),
name_(android::tidToName(tid)) {} name_(android::tidToName(tid)) {}
explicit TidEntry(const LogStatisticsElement& element) explicit TidEntry(const LogStatisticsElement& element)
: EntryBaseDropped(element), : EntryBaseDropped(element),
tid_(element.tid),
pid_(element.pid), pid_(element.pid),
uid_(element.uid), uid_(element.uid),
name_(android::tidToName(tid_)) {} name_(android::tidToName(element.tid)) {}
TidEntry(const TidEntry& element) TidEntry(const TidEntry& element)
: EntryBaseDropped(element), : EntryBaseDropped(element),
tid_(element.tid_),
pid_(element.pid_), pid_(element.pid_),
uid_(element.uid_), uid_(element.uid_),
name_(element.name_ ? strdup(element.name_) : nullptr) {} name_(element.name_ ? strdup(element.name_) : nullptr) {}
~TidEntry() { free(name_); } ~TidEntry() { free(name_); }
pid_t key() const { return tid_; }
pid_t tid() const { return key(); }
pid_t pid() const { return pid_; } pid_t pid() const { return pid_; }
uid_t uid() const { return uid_; } uid_t uid() const { return uid_; }
const char* name() const { return name_; } const char* name() const { return name_; }
@ -362,10 +360,9 @@ class TidEntry : public EntryBaseDropped {
} }
std::string formatHeader(const std::string& name, log_id_t id) const; std::string formatHeader(const std::string& name, log_id_t id) const;
std::string format(const LogStatistics& stat, log_id_t id) const; std::string format(const LogStatistics& stat, log_id_t id, pid_t pid) const;
private: private:
const pid_t tid_;
pid_t pid_; pid_t pid_;
uid_t uid_; uid_t uid_;
char* name_; char* name_;
@ -392,7 +389,7 @@ class TagEntry : public EntryBaseDropped {
} }
std::string formatHeader(const std::string& name, log_id_t id) const; std::string formatHeader(const std::string& name, log_id_t id) const;
std::string format(const LogStatistics& stat, log_id_t id) const; std::string format(const LogStatistics& stat, log_id_t id, uint32_t) const;
private: private:
const uint32_t tag_; const uint32_t tag_;
@ -400,108 +397,14 @@ class TagEntry : public EntryBaseDropped {
uid_t uid_; uid_t uid_;
}; };
struct TagNameKey {
std::string* alloc;
std::string_view name; // Saves space if const char*
explicit TagNameKey(const LogStatisticsElement& element)
: alloc(nullptr), name("", strlen("")) {
if (IsBinary(element.log_id)) {
uint32_t tag = element.tag;
if (tag) {
const char* cp = android::tagToName(tag);
if (cp) {
name = std::string_view(cp, strlen(cp));
return;
}
}
alloc = new std::string(
android::base::StringPrintf("[%" PRIu32 "]", tag));
if (!alloc) return;
name = std::string_view(alloc->c_str(), alloc->size());
return;
}
const char* msg = element.msg;
if (!msg) {
name = std::string_view("chatty", strlen("chatty"));
return;
}
++msg;
uint16_t len = element.msg_len;
len = (len <= 1) ? 0 : strnlen(msg, len - 1);
if (!len) {
name = std::string_view("<NULL>", strlen("<NULL>"));
return;
}
alloc = new std::string(msg, len);
if (!alloc) return;
name = std::string_view(alloc->c_str(), alloc->size());
}
explicit TagNameKey(TagNameKey&& rval) noexcept
: alloc(rval.alloc), name(rval.name.data(), rval.name.length()) {
rval.alloc = nullptr;
}
explicit TagNameKey(const TagNameKey& rval)
: alloc(rval.alloc ? new std::string(*rval.alloc) : nullptr),
name(alloc ? alloc->data() : rval.name.data(), rval.name.length()) {
}
~TagNameKey() {
if (alloc) delete alloc;
}
operator const std::string_view() const {
return name;
}
const char* data() const {
return name.data();
}
size_t length() const {
return name.length();
}
bool operator==(const TagNameKey& rval) const {
if (length() != rval.length()) return false;
if (length() == 0) return true;
return fastcmp<strncmp>(data(), rval.data(), length()) == 0;
}
bool operator!=(const TagNameKey& rval) const {
return !(*this == rval);
}
size_t getAllocLength() const {
return alloc ? alloc->length() + 1 + sizeof(std::string) : 0;
}
};
// Hash for TagNameKey
template <>
struct std::hash<TagNameKey>
: public std::unary_function<const TagNameKey&, size_t> {
size_t operator()(const TagNameKey& __t) const noexcept {
if (!__t.length()) return 0;
return std::hash<std::string_view>()(std::string_view(__t));
}
};
class TagNameEntry : public EntryBase { class TagNameEntry : public EntryBase {
public: public:
explicit TagNameEntry(const LogStatisticsElement& element) explicit TagNameEntry(const LogStatisticsElement& element)
: EntryBase(element), : EntryBase(element), tid_(element.tid), pid_(element.pid), uid_(element.uid) {}
tid_(element.tid),
pid_(element.pid),
uid_(element.uid),
name_(element) {}
const TagNameKey& key() const { return name_; }
pid_t tid() const { return tid_; } pid_t tid() const { return tid_; }
pid_t pid() const { return pid_; } pid_t pid() const { return pid_; }
uid_t uid() const { return uid_; } uid_t uid() const { return uid_; }
const char* name() const { return name_.data(); }
size_t getNameAllocLength() const { return name_.getAllocLength(); }
void Add(const LogStatisticsElement& element) { void Add(const LogStatisticsElement& element) {
if (uid_ != element.uid) { if (uid_ != element.uid) {
@ -517,16 +420,14 @@ class TagNameEntry : public EntryBase {
} }
std::string formatHeader(const std::string& name, log_id_t id) const; std::string formatHeader(const std::string& name, log_id_t id) const;
std::string format(const LogStatistics& stat, log_id_t id) const; std::string format(const LogStatistics& stat, log_id_t id, const std::string& key_name) const;
private: private:
pid_t tid_; pid_t tid_;
pid_t pid_; pid_t pid_;
uid_t uid_; uid_t uid_;
TagNameKey name_;
}; };
// Log Statistics
class LogStatistics { class LogStatistics {
friend UidEntry; friend UidEntry;
friend PidEntry; friend PidEntry;
@ -567,7 +468,7 @@ class LogStatistics {
tagTable_t securityTagTable GUARDED_BY(lock_); tagTable_t securityTagTable GUARDED_BY(lock_);
// global tag list // global tag list
typedef LogHashtable<TagNameKey, TagNameEntry> tagNameTable_t; typedef LogHashtable<std::string, TagNameEntry> tagNameTable_t;
tagNameTable_t tagNameTable; tagNameTable_t tagNameTable;
size_t sizeOf() const REQUIRES(lock_) { size_t sizeOf() const REQUIRES(lock_) {
@ -584,13 +485,21 @@ class LogStatistics {
const char* name = it.second.name(); const char* name = it.second.name();
if (name) size += strlen(name) + 1; if (name) size += strlen(name) + 1;
} }
for (auto it : tagNameTable) size += it.second.getNameAllocLength(); for (auto it : tagNameTable) {
size += sizeof(std::string);
size_t len = it.first.size();
// Account for short string optimization: if the string's length is <= 22 bytes for 64
// bit or <= 10 bytes for 32 bit, then there is no additional allocation.
if ((sizeof(std::string) == 24 && len > 22) ||
(sizeof(std::string) != 24 && len > 10)) {
size += len;
}
}
log_id_for_each(id) { log_id_for_each(id) {
size += uidTable[id].sizeOf(); size += uidTable[id].sizeOf();
size += uidTable[id].size() * sizeof(uidTable_t::iterator); size += uidTable[id].size() * sizeof(uidTable_t::iterator);
size += pidSystemTable[id].sizeOf(); size += pidSystemTable[id].sizeOf();
size += size += pidSystemTable[id].size() * sizeof(pidSystemTable_t::iterator);
pidSystemTable[id].size() * sizeof(pidSystemTable_t::iterator);
} }
return size; return size;
} }

View file

@ -29,6 +29,7 @@
#include <string> #include <string>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h> #include <android-base/macros.h>
#include <android-base/scopeguard.h> #include <android-base/scopeguard.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
@ -115,10 +116,11 @@ bool LogTags::RebuildFileEventLogTags(const char* filename, bool warn) {
} }
if (warn) { if (warn) {
android::prdebug( if (fd < 0) {
((fd < 0) ? "%s failed to rebuild" LOG(ERROR) << filename << " failed to rebuild";
: "%s missing, damaged or truncated; rebuilt"), } else {
filename); LOG(ERROR) << filename << " missing, damaged or truncated; rebuilt";
}
} }
if (fd >= 0) { if (fd >= 0) {
@ -182,8 +184,7 @@ void LogTags::AddEventLogTags(uint32_t tag, uid_t uid, const std::string& Name,
WritePersistEventLogTags(tag, uid, source); WritePersistEventLogTags(tag, uid, source);
} else if (warn && !newOne && source) { } else if (warn && !newOne && source) {
// For the files, we want to report dupes. // For the files, we want to report dupes.
android::prdebug("Multiple tag %" PRIu32 " %s %s %s", tag, Name.c_str(), LOG(DEBUG) << "Multiple tag " << tag << " " << Name << " " << Format << " " << source;
Format.c_str(), source);
} }
} }
@ -216,7 +217,7 @@ void LogTags::ReadFileEventLogTags(const char* filename, bool warn) {
} else if (isdigit(*cp)) { } else if (isdigit(*cp)) {
unsigned long Tag = strtoul(cp, &cp, 10); unsigned long Tag = strtoul(cp, &cp, 10);
if (warn && (Tag > emptyTag)) { if (warn && (Tag > emptyTag)) {
android::prdebug("tag too large %lu", Tag); LOG(WARNING) << "tag too large " << Tag;
} }
while ((cp < endp) && (*cp != '\n') && isspace(*cp)) ++cp; while ((cp < endp) && (*cp != '\n') && isspace(*cp)) ++cp;
if (cp >= endp) break; if (cp >= endp) break;
@ -231,9 +232,8 @@ void LogTags::ReadFileEventLogTags(const char* filename, bool warn) {
std::string Name(name, cp - name); std::string Name(name, cp - name);
#ifdef ALLOW_NOISY_LOGGING_OF_PROBLEM_WITH_LOTS_OF_TECHNICAL_DEBT #ifdef ALLOW_NOISY_LOGGING_OF_PROBLEM_WITH_LOTS_OF_TECHNICAL_DEBT
static const size_t maximum_official_tag_name_size = 24; static const size_t maximum_official_tag_name_size = 24;
if (warn && if (warn && (Name.length() > maximum_official_tag_name_size)) {
(Name.length() > maximum_official_tag_name_size)) { LOG(WARNING) << "tag name too long " << Name;
android::prdebug("tag name too long %s", Name.c_str());
} }
#endif #endif
if (hasAlpha && if (hasAlpha &&
@ -264,8 +264,8 @@ void LogTags::ReadFileEventLogTags(const char* filename, bool warn) {
filename, warn); filename, warn);
} else { } else {
if (warn) { if (warn) {
android::prdebug("tag name invalid %.*s", LOG(ERROR) << android::base::StringPrintf("tag name invalid %.*s",
(int)(cp - name + 1), name); (int)(cp - name + 1), name);
} }
lineStart = nullptr; lineStart = nullptr;
} }
@ -276,7 +276,7 @@ void LogTags::ReadFileEventLogTags(const char* filename, bool warn) {
cp++; cp++;
} }
} else if (warn) { } else if (warn) {
android::prdebug("Cannot read %s", filename); LOG(ERROR) << "Cannot read " << filename;
} }
} }
@ -479,8 +479,8 @@ uint32_t LogTags::nameToTag_locked(const std::string& name, const char* format,
static int openFile(const char* name, int mode, bool warning) { static int openFile(const char* name, int mode, bool warning) {
int fd = TEMP_FAILURE_RETRY(open(name, mode)); int fd = TEMP_FAILURE_RETRY(open(name, mode));
if ((fd < 0) && warning) { if (fd < 0 && warning) {
android::prdebug("Failed open %s (%d)", name, errno); PLOG(ERROR) << "Failed to open " << name;
} }
return fd; return fd;
} }

View file

@ -30,7 +30,6 @@ namespace android {
// Furnished in main.cpp. Caller must own and free returned value // Furnished in main.cpp. Caller must own and free returned value
char* uidToName(uid_t uid); char* uidToName(uid_t uid);
void prdebug(const char* fmt, ...) __attribute__((__format__(printf, 1, 2)));
// Caller must own and free returned value // Caller must own and free returned value
char* pidToName(pid_t pid); char* pidToName(pid_t pid);

View file

@ -16,6 +16,8 @@
#include "SimpleLogBuffer.h" #include "SimpleLogBuffer.h"
#include <android-base/logging.h>
#include "LogBufferElement.h" #include "LogBufferElement.h"
SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats) SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats)
@ -232,8 +234,8 @@ bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()};
for (const auto& reader_thread : reader_list_->reader_threads()) { for (const auto& reader_thread : reader_list_->reader_threads()) {
if (reader_thread->IsWatching(id)) { if (reader_thread->IsWatching(id)) {
android::prdebug("Kicking blocked reader, %s, from LogBuffer::clear()\n", LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
reader_thread->name().c_str()); << ", from LogBuffer::clear()";
reader_thread->release_Locked(); reader_thread->release_Locked();
} }
} }
@ -350,16 +352,16 @@ void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned
if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100% if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
// A misbehaving or slow reader has its connection // A misbehaving or slow reader has its connection
// dropped if we hit too much memory pressure. // dropped if we hit too much memory pressure.
android::prdebug("Kicking blocked reader, %s, from LogBuffer::kickMe()\n", LOG(WARNING) << "Kicking blocked reader, " << reader->name()
reader->name().c_str()); << ", from LogBuffer::kickMe()";
reader->release_Locked(); reader->release_Locked();
} else if (reader->deadline().time_since_epoch().count() != 0) { } else if (reader->deadline().time_since_epoch().count() != 0) {
// Allow a blocked WRAP deadline reader to trigger and start reporting the log data. // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
reader->triggerReader_Locked(); reader->triggerReader_Locked();
} else { } else {
// tell slow reader to skip entries to catch up // tell slow reader to skip entries to catch up
android::prdebug("Skipping %lu entries from slow reader, %s, from LogBuffer::kickMe()\n", LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
prune_rows, reader->name().c_str()); << ", from LogBuffer::kickMe()";
reader->triggerSkip_Locked(id, prune_rows); reader->triggerSkip_Locked(id, prune_rows);
} }
} }

View file

@ -79,13 +79,6 @@ int write_log_messages(const uint8_t** pdata, size_t* data_left, LogBuffer* log_
return 1; return 1;
} }
// Because system/core/logd/main.cpp redefines these.
void prdebug(char const* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
char* uidToName(uid_t) { char* uidToName(uid_t) {
return strdup("fake"); return strdup("fake");
} }

View file

@ -36,7 +36,9 @@
#include <memory> #include <memory>
#include <android-base/logging.h>
#include <android-base/macros.h> #include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <cutils/android_get_control_file.h> #include <cutils/android_get_control_file.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <log/event_tag_map.h> #include <log/event_tag_map.h>
@ -70,19 +72,18 @@ static int drop_privs(bool klogd, bool auditd) {
sched_param param = {}; sched_param param = {};
if (set_sched_policy(0, SP_BACKGROUND) < 0) { if (set_sched_policy(0, SP_BACKGROUND) < 0) {
android::prdebug("failed to set background scheduling policy"); PLOG(ERROR) << "failed to set background scheduling policy";
return -1; return -1;
} }
if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) { if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
android::prdebug("failed to set batch scheduler"); PLOG(ERROR) << "failed to set batch scheduler";
return -1; return -1;
} }
if (!__android_logger_property_get_bool("ro.debuggable", if (!__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) &&
BOOL_DEFAULT_FALSE) &&
prctl(PR_SET_DUMPABLE, 0) == -1) { prctl(PR_SET_DUMPABLE, 0) == -1) {
android::prdebug("failed to clear PR_SET_DUMPABLE"); PLOG(ERROR) << "failed to clear PR_SET_DUMPABLE";
return -1; return -1;
} }
@ -105,40 +106,13 @@ static int drop_privs(bool klogd, bool auditd) {
return -1; return -1;
} }
if (cap_set_proc(caps.get()) < 0) { if (cap_set_proc(caps.get()) < 0) {
android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno); PLOG(ERROR) << "failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL";
return -1; return -1;
} }
return 0; return 0;
} }
static int fdDmesg = -1;
void android::prdebug(const char* fmt, ...) {
if (fdDmesg < 0) {
return;
}
static const char message[] = {
KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
};
char buffer[256];
memcpy(buffer, message, sizeof(message));
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(buffer + sizeof(message),
sizeof(buffer) - sizeof(message), fmt, ap);
va_end(ap);
if (n > 0) {
buffer[sizeof(buffer) - 1] = '\0';
if (!strchr(buffer, '\n')) {
buffer[sizeof(buffer) - 2] = '\0';
strlcat(buffer, "\n", sizeof(buffer));
}
write(fdDmesg, buffer, strlen(buffer));
}
}
char* android::uidToName(uid_t u) { char* android::uidToName(uid_t u) {
struct Userdata { struct Userdata {
uid_t uid; uid_t uid;
@ -246,8 +220,20 @@ int main(int argc, char* argv[]) {
return issueReinit(); return issueReinit();
} }
android::base::InitLogging(
argv, [](android::base::LogId log_id, android::base::LogSeverity severity,
const char* tag, const char* file, unsigned int line, const char* message) {
if (tag && strcmp(tag, "logd") != 0) {
auto prefixed_message = android::base::StringPrintf("%s: %s", tag, message);
android::base::KernelLogger(log_id, severity, "logd", file, line,
prefixed_message.c_str());
} else {
android::base::KernelLogger(log_id, severity, "logd", file, line, message);
}
});
static const char dev_kmsg[] = "/dev/kmsg"; static const char dev_kmsg[] = "/dev/kmsg";
fdDmesg = android_get_control_file(dev_kmsg); int fdDmesg = android_get_control_file(dev_kmsg);
if (fdDmesg < 0) { if (fdDmesg < 0) {
fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC)); fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
} }
@ -263,7 +249,7 @@ int main(int argc, char* argv[]) {
fdPmesg = TEMP_FAILURE_RETRY( fdPmesg = TEMP_FAILURE_RETRY(
open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC)); open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
} }
if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg); if (fdPmesg < 0) PLOG(ERROR) << "Failed to open " << proc_kmsg;
} }
bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE); bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);