2014-02-06 23:48:50 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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.
|
|
|
|
*/
|
|
|
|
|
2020-05-07 18:13:12 +02:00
|
|
|
#pragma once
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2016-12-19 23:51:15 +01:00
|
|
|
#include <ctype.h>
|
2017-04-19 23:39:21 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
2015-03-16 16:26:05 +01:00
|
|
|
#include <stdlib.h>
|
2017-04-19 23:39:21 +02:00
|
|
|
#include <string.h>
|
2014-02-06 23:48:50 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
#include <algorithm> // std::max
|
2020-05-07 18:13:12 +02:00
|
|
|
#include <array>
|
2016-12-19 23:51:15 +01:00
|
|
|
#include <memory>
|
2020-05-07 23:44:43 +02:00
|
|
|
#include <mutex>
|
2017-12-01 01:31:35 +01:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2015-05-19 18:12:30 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2020-05-07 23:44:43 +02:00
|
|
|
#include <android-base/thread_annotations.h>
|
2017-03-10 23:31:54 +01:00
|
|
|
#include <android/log.h>
|
2017-04-14 18:46:57 +02:00
|
|
|
#include <log/log_time.h>
|
2015-08-22 01:44:30 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2017-04-19 23:39:21 +02:00
|
|
|
#include <utils/FastStrcmp.h>
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
#include "LogUtils.h"
|
2014-02-06 23:48:50 +01:00
|
|
|
|
|
|
|
#define log_id_for_each(i) \
|
2017-03-10 23:31:54 +01:00
|
|
|
for (log_id_t i = LOG_ID_MIN; (i) < LOG_ID_MAX; (i) = (log_id_t)((i) + 1))
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2015-08-22 01:44:30 +02:00
|
|
|
class LogStatistics;
|
2020-06-03 22:49:24 +02:00
|
|
|
class UidEntry;
|
|
|
|
class PidEntry;
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
struct LogStatisticsElement {
|
|
|
|
uid_t uid;
|
|
|
|
pid_t pid;
|
|
|
|
pid_t tid;
|
|
|
|
uint32_t tag;
|
|
|
|
log_time realtime;
|
|
|
|
const char* msg;
|
|
|
|
uint16_t msg_len;
|
|
|
|
uint16_t dropped_count;
|
|
|
|
log_id_t log_id;
|
|
|
|
};
|
|
|
|
|
2015-03-16 16:26:05 +01:00
|
|
|
template <typename TKey, typename TEntry>
|
2015-05-19 18:12:30 +02:00
|
|
|
class LogHashtable {
|
|
|
|
std::unordered_map<TKey, TEntry> map;
|
|
|
|
|
2016-10-06 18:55:21 +02:00
|
|
|
size_t bucket_size() const {
|
|
|
|
size_t count = 0;
|
|
|
|
for (size_t idx = 0; idx < map.bucket_count(); ++idx) {
|
|
|
|
size_t bucket_size = map.bucket_size(idx);
|
|
|
|
if (bucket_size == 0) bucket_size = 1;
|
|
|
|
count += bucket_size;
|
|
|
|
}
|
|
|
|
float load_factor = map.max_load_factor();
|
|
|
|
if (load_factor < 1.0) return count;
|
|
|
|
return count * load_factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t unordered_map_per_entry_overhead = sizeof(void*);
|
|
|
|
static const size_t unordered_map_bucket_overhead = sizeof(void*);
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
public:
|
2017-03-10 23:31:54 +01:00
|
|
|
size_t size() const {
|
|
|
|
return map.size();
|
|
|
|
}
|
2016-10-06 19:09:24 +02:00
|
|
|
|
2016-10-06 18:55:21 +02:00
|
|
|
// Estimate unordered_map memory usage.
|
|
|
|
size_t sizeOf() const {
|
|
|
|
return sizeof(*this) +
|
2016-10-06 19:09:24 +02:00
|
|
|
(size() * (sizeof(TEntry) + unordered_map_per_entry_overhead)) +
|
2016-10-06 18:55:21 +02:00
|
|
|
(bucket_size() * sizeof(size_t) + unordered_map_bucket_overhead);
|
|
|
|
}
|
|
|
|
|
2015-05-19 18:12:30 +02:00
|
|
|
typedef typename std::unordered_map<TKey, TEntry>::iterator iterator;
|
2017-03-10 23:31:54 +01:00
|
|
|
typedef
|
|
|
|
typename std::unordered_map<TKey, TEntry>::const_iterator const_iterator;
|
2015-05-19 18:12:30 +02:00
|
|
|
|
2020-05-07 18:13:12 +02:00
|
|
|
// 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.
|
|
|
|
template <size_t len>
|
2020-06-03 22:49:24 +02:00
|
|
|
void MaxEntries(uid_t uid, pid_t pid, std::array<const TKey*, len>& out_keys,
|
|
|
|
std::array<const TEntry*, len>& out_entries) const {
|
|
|
|
out_keys.fill(nullptr);
|
|
|
|
out_entries.fill(nullptr);
|
|
|
|
for (const auto& [key, entry] : map) {
|
|
|
|
uid_t entry_uid = 0;
|
|
|
|
if constexpr (std::is_same_v<TEntry, UidEntry>) {
|
|
|
|
entry_uid = key;
|
|
|
|
} else {
|
|
|
|
entry_uid = entry.uid();
|
|
|
|
}
|
|
|
|
if (uid != AID_ROOT && uid != entry_uid) {
|
2015-12-17 18:58:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-03 22:49:24 +02:00
|
|
|
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) {
|
2015-12-17 18:58:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-22 01:44:30 +02:00
|
|
|
size_t sizes = entry.getSizes();
|
|
|
|
ssize_t index = len - 1;
|
2020-06-03 22:49:24 +02:00
|
|
|
while ((!out_entries[index] || sizes > out_entries[index]->getSizes()) && --index >= 0)
|
2015-03-16 16:26:05 +01:00
|
|
|
;
|
2015-08-22 01:44:30 +02:00
|
|
|
if (++index < (ssize_t)len) {
|
|
|
|
size_t num = len - index - 1;
|
|
|
|
if (num) {
|
2020-06-03 22:49:24 +02:00
|
|
|
memmove(&out_keys[index + 1], &out_keys[index], num * sizeof(out_keys[0]));
|
|
|
|
memmove(&out_entries[index + 1], &out_entries[index],
|
|
|
|
num * sizeof(out_entries[0]));
|
2015-03-16 16:26:05 +01:00
|
|
|
}
|
2020-06-03 22:49:24 +02:00
|
|
|
out_keys[index] = &key;
|
|
|
|
out_entries[index] = &entry;
|
2015-03-16 16:26:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
iterator Add(const TKey& key, const LogStatisticsElement& element) {
|
2015-05-19 18:12:30 +02:00
|
|
|
iterator it = map.find(key);
|
|
|
|
if (it == map.end()) {
|
2015-08-22 01:44:30 +02:00
|
|
|
it = map.insert(std::make_pair(key, TEntry(element))).first;
|
2015-05-19 18:12:30 +02:00
|
|
|
} else {
|
2020-05-20 04:01:16 +02:00
|
|
|
it->second.Add(element);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2015-05-19 18:12:30 +02:00
|
|
|
return it;
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
iterator Add(const TKey& key) {
|
2015-05-19 18:12:30 +02:00
|
|
|
iterator it = map.find(key);
|
|
|
|
if (it == map.end()) {
|
|
|
|
it = map.insert(std::make_pair(key, TEntry(key))).first;
|
|
|
|
} else {
|
2020-05-20 04:01:16 +02:00
|
|
|
it->second.Add(key);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2015-05-19 18:12:30 +02:00
|
|
|
return it;
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Subtract(const TKey& key, const LogStatisticsElement& element) {
|
2015-05-19 18:12:30 +02:00
|
|
|
iterator it = map.find(key);
|
2020-05-20 04:01:16 +02:00
|
|
|
if (it != map.end() && it->second.Subtract(element)) {
|
2015-05-19 18:12:30 +02:00
|
|
|
map.erase(it);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Drop(const TKey& key, const LogStatisticsElement& element) {
|
2015-05-19 18:12:30 +02:00
|
|
|
iterator it = map.find(key);
|
|
|
|
if (it != map.end()) {
|
2020-05-20 04:01:16 +02:00
|
|
|
it->second.Drop(element);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
iterator begin() { return map.begin(); }
|
|
|
|
const_iterator begin() const { return map.begin(); }
|
|
|
|
iterator end() { return map.end(); }
|
|
|
|
const_iterator end() const { return map.end(); }
|
2015-03-16 16:26:05 +01:00
|
|
|
};
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class EntryBase {
|
|
|
|
public:
|
|
|
|
EntryBase() : size_(0) {}
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit EntryBase(const LogStatisticsElement& element) : size_(element.msg_len) {}
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
size_t getSizes() const { return size_; }
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) { size_ += element.msg_len; }
|
|
|
|
bool Subtract(const LogStatisticsElement& element) {
|
|
|
|
size_ -= element.msg_len;
|
2020-05-20 04:01:16 +02:00
|
|
|
return !size_;
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
static constexpr size_t PRUNED_LEN = 14;
|
|
|
|
static constexpr size_t TOTAL_LEN = 80;
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
static std::string formatLine(const std::string& name,
|
|
|
|
const std::string& size,
|
|
|
|
const std::string& pruned) {
|
2020-05-20 04:01:16 +02:00
|
|
|
ssize_t drop_len = std::max(pruned.length() + 1, PRUNED_LEN);
|
|
|
|
ssize_t size_len = std::max(size.length() + 1, TOTAL_LEN - name.length() - drop_len - 1);
|
2017-03-10 23:31:54 +01:00
|
|
|
|
|
|
|
std::string ret = android::base::StringPrintf(
|
|
|
|
"%s%*s%*s", name.c_str(), (int)size_len, size.c_str(),
|
|
|
|
(int)drop_len, pruned.c_str());
|
2016-12-19 23:51:15 +01:00
|
|
|
// remove any trailing spaces
|
|
|
|
size_t pos = ret.size();
|
|
|
|
size_t len = 0;
|
|
|
|
while (pos && isspace(ret[--pos])) ++len;
|
|
|
|
if (len) ret.erase(pos + 1, len);
|
|
|
|
return ret + "\n";
|
2015-08-22 01:44:30 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size_;
|
2015-04-13 23:24:45 +02:00
|
|
|
};
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class EntryBaseDropped : public EntryBase {
|
|
|
|
public:
|
|
|
|
EntryBaseDropped() : dropped_(0) {}
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit EntryBaseDropped(const LogStatisticsElement& element)
|
|
|
|
: EntryBase(element), dropped_(element.dropped_count) {}
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
size_t dropped_count() const { return dropped_; }
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
dropped_ += element.dropped_count;
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBase::Add(element);
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
bool Subtract(const LogStatisticsElement& element) {
|
|
|
|
dropped_ -= element.dropped_count;
|
2020-05-20 04:01:16 +02:00
|
|
|
return EntryBase::Subtract(element) && !dropped_;
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
void Drop(const LogStatisticsElement& element) {
|
2020-05-20 04:01:16 +02:00
|
|
|
dropped_ += 1;
|
|
|
|
EntryBase::Subtract(element);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t dropped_;
|
2015-04-13 23:24:45 +02:00
|
|
|
};
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class UidEntry : public EntryBaseDropped {
|
|
|
|
public:
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit UidEntry(const LogStatisticsElement& element)
|
2020-06-03 22:49:24 +02:00
|
|
|
: EntryBaseDropped(element), pid_(element.pid) {}
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_t pid() const { return pid_; }
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
if (pid_ != element.pid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_ = -1;
|
2015-12-17 18:58:43 +01:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBaseDropped::Add(element);
|
2015-12-17 18:58:43 +01:00
|
|
|
}
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
std::string formatHeader(const std::string& name, log_id_t id) const;
|
2020-06-03 22:49:24 +02:00
|
|
|
std::string format(const LogStatistics& stat, log_id_t id, uid_t uid) const;
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
pid_t pid_;
|
2014-02-06 23:48:50 +01:00
|
|
|
};
|
|
|
|
|
2019-06-28 22:21:27 +02:00
|
|
|
namespace android {
|
|
|
|
uid_t pidToUid(pid_t pid);
|
|
|
|
}
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class PidEntry : public EntryBaseDropped {
|
|
|
|
public:
|
2017-03-10 23:31:54 +01:00
|
|
|
explicit PidEntry(pid_t pid)
|
|
|
|
: EntryBaseDropped(),
|
2020-05-20 04:01:16 +02:00
|
|
|
uid_(android::pidToUid(pid)),
|
|
|
|
name_(android::pidToName(pid)) {}
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit PidEntry(const LogStatisticsElement& element)
|
2020-06-03 22:49:24 +02:00
|
|
|
: EntryBaseDropped(element), uid_(element.uid), name_(android::pidToName(element.pid)) {}
|
2017-03-10 23:31:54 +01:00
|
|
|
PidEntry(const PidEntry& element)
|
|
|
|
: EntryBaseDropped(element),
|
2020-05-20 04:01:16 +02:00
|
|
|
uid_(element.uid_),
|
|
|
|
name_(element.name_ ? strdup(element.name_) : nullptr) {}
|
|
|
|
~PidEntry() { free(name_); }
|
|
|
|
|
|
|
|
uid_t uid() const { return uid_; }
|
|
|
|
const char* name() const { return name_; }
|
|
|
|
|
|
|
|
void Add(pid_t new_pid) {
|
|
|
|
if (name_ && !fastcmp<strncmp>(name_, "zygote", 6)) {
|
|
|
|
free(name_);
|
|
|
|
name_ = nullptr;
|
2015-04-20 19:27:38 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
if (!name_) {
|
|
|
|
name_ = android::pidToName(new_pid);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
uid_t incoming_uid = element.uid;
|
2020-05-20 04:01:16 +02:00
|
|
|
if (uid() != incoming_uid) {
|
|
|
|
uid_ = incoming_uid;
|
|
|
|
free(name_);
|
2020-06-03 00:39:21 +02:00
|
|
|
name_ = android::pidToName(element.pid);
|
2015-04-13 23:24:45 +02:00
|
|
|
} else {
|
2020-06-03 00:39:21 +02:00
|
|
|
Add(element.pid);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBaseDropped::Add(element);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
std::string formatHeader(const std::string& name, log_id_t id) const;
|
2020-06-03 22:49:24 +02:00
|
|
|
std::string format(const LogStatistics& stat, log_id_t id, pid_t pid) const;
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
private:
|
|
|
|
uid_t uid_;
|
|
|
|
char* name_;
|
|
|
|
};
|
2017-03-10 23:31:54 +01:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class TidEntry : public EntryBaseDropped {
|
|
|
|
public:
|
2017-03-10 23:31:54 +01:00
|
|
|
TidEntry(pid_t tid, pid_t pid)
|
|
|
|
: EntryBaseDropped(),
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_(pid),
|
|
|
|
uid_(android::pidToUid(tid)),
|
|
|
|
name_(android::tidToName(tid)) {}
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit TidEntry(const LogStatisticsElement& element)
|
2017-03-10 23:31:54 +01:00
|
|
|
: EntryBaseDropped(element),
|
2020-06-03 00:39:21 +02:00
|
|
|
pid_(element.pid),
|
|
|
|
uid_(element.uid),
|
2020-06-03 22:49:24 +02:00
|
|
|
name_(android::tidToName(element.tid)) {}
|
2017-03-10 23:31:54 +01:00
|
|
|
TidEntry(const TidEntry& element)
|
|
|
|
: EntryBaseDropped(element),
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_(element.pid_),
|
|
|
|
uid_(element.uid_),
|
|
|
|
name_(element.name_ ? strdup(element.name_) : nullptr) {}
|
|
|
|
~TidEntry() { free(name_); }
|
|
|
|
|
|
|
|
pid_t pid() const { return pid_; }
|
|
|
|
uid_t uid() const { return uid_; }
|
|
|
|
const char* name() const { return name_; }
|
|
|
|
|
|
|
|
void Add(pid_t incomingTid) {
|
|
|
|
if (name_ && !fastcmp<strncmp>(name_, "zygote", 6)) {
|
|
|
|
free(name_);
|
|
|
|
name_ = nullptr;
|
2015-04-20 22:35:15 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
if (!name_) {
|
|
|
|
name_ = android::tidToName(incomingTid);
|
2015-04-20 22:35:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
uid_t incoming_uid = element.uid;
|
|
|
|
pid_t incoming_pid = element.pid;
|
2020-05-20 04:01:16 +02:00
|
|
|
if (uid() != incoming_uid || pid() != incoming_pid) {
|
|
|
|
uid_ = incoming_uid;
|
|
|
|
pid_ = incoming_pid;
|
|
|
|
free(name_);
|
2020-06-03 00:39:21 +02:00
|
|
|
name_ = android::tidToName(element.tid);
|
2015-04-20 22:35:15 +02:00
|
|
|
} else {
|
2020-06-03 00:39:21 +02:00
|
|
|
Add(element.tid);
|
2015-04-20 22:35:15 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBaseDropped::Add(element);
|
2015-04-20 22:35:15 +02:00
|
|
|
}
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
std::string formatHeader(const std::string& name, log_id_t id) const;
|
2020-06-03 22:49:24 +02:00
|
|
|
std::string format(const LogStatistics& stat, log_id_t id, pid_t pid) const;
|
2015-04-20 22:35:15 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
private:
|
|
|
|
pid_t pid_;
|
|
|
|
uid_t uid_;
|
|
|
|
char* name_;
|
|
|
|
};
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class TagEntry : public EntryBaseDropped {
|
|
|
|
public:
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit TagEntry(const LogStatisticsElement& element)
|
|
|
|
: EntryBaseDropped(element), tag_(element.tag), pid_(element.pid), uid_(element.uid) {}
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
uint32_t key() const { return tag_; }
|
|
|
|
pid_t pid() const { return pid_; }
|
|
|
|
uid_t uid() const { return uid_; }
|
|
|
|
const char* name() const { return android::tagToName(tag_); }
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
if (uid_ != element.uid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
uid_ = -1;
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
if (pid_ != element.pid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_ = -1;
|
2015-12-17 18:58:43 +01:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBaseDropped::Add(element);
|
2015-04-13 23:24:45 +02:00
|
|
|
}
|
2015-08-22 01:44:30 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
std::string formatHeader(const std::string& name, log_id_t id) const;
|
2020-06-03 22:49:24 +02:00
|
|
|
std::string format(const LogStatistics& stat, log_id_t id, uint32_t) const;
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
const uint32_t tag_;
|
|
|
|
pid_t pid_;
|
|
|
|
uid_t uid_;
|
2015-03-16 16:26:05 +01:00
|
|
|
};
|
|
|
|
|
2017-04-19 23:39:21 +02:00
|
|
|
struct TagNameKey {
|
|
|
|
std::string* alloc;
|
2017-12-01 01:31:35 +01:00
|
|
|
std::string_view name; // Saves space if const char*
|
2017-04-19 23:39:21 +02:00
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit TagNameKey(const LogStatisticsElement& element)
|
|
|
|
: alloc(nullptr), name("", strlen("")) {
|
|
|
|
if (IsBinary(element.log_id)) {
|
|
|
|
uint32_t tag = element.tag;
|
2017-04-19 23:39:21 +02:00
|
|
|
if (tag) {
|
|
|
|
const char* cp = android::tagToName(tag);
|
|
|
|
if (cp) {
|
2017-12-01 01:31:35 +01:00
|
|
|
name = std::string_view(cp, strlen(cp));
|
2017-04-19 23:39:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alloc = new std::string(
|
|
|
|
android::base::StringPrintf("[%" PRIu32 "]", tag));
|
|
|
|
if (!alloc) return;
|
2017-12-01 01:31:35 +01:00
|
|
|
name = std::string_view(alloc->c_str(), alloc->size());
|
2017-04-19 23:39:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
const char* msg = element.msg;
|
2017-04-19 23:39:21 +02:00
|
|
|
if (!msg) {
|
2017-12-01 01:31:35 +01:00
|
|
|
name = std::string_view("chatty", strlen("chatty"));
|
2017-04-19 23:39:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
++msg;
|
2020-06-03 00:39:21 +02:00
|
|
|
uint16_t len = element.msg_len;
|
2017-04-19 23:39:21 +02:00
|
|
|
len = (len <= 1) ? 0 : strnlen(msg, len - 1);
|
|
|
|
if (!len) {
|
2017-12-01 01:31:35 +01:00
|
|
|
name = std::string_view("<NULL>", strlen("<NULL>"));
|
2017-04-19 23:39:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
alloc = new std::string(msg, len);
|
|
|
|
if (!alloc) return;
|
2017-12-01 01:31:35 +01:00
|
|
|
name = std::string_view(alloc->c_str(), alloc->size());
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 20:16:22 +02:00
|
|
|
explicit TagNameKey(TagNameKey&& rval) noexcept
|
2017-04-19 23:39:21 +02:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
2017-12-01 01:31:35 +01:00
|
|
|
operator const std::string_view() const {
|
2017-04-19 23:39:21 +02:00
|
|
|
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;
|
2017-12-01 01:31:35 +01:00
|
|
|
return std::hash<std::string_view>()(std::string_view(__t));
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-20 04:01:16 +02:00
|
|
|
class TagNameEntry : public EntryBase {
|
|
|
|
public:
|
2020-06-03 00:39:21 +02:00
|
|
|
explicit TagNameEntry(const LogStatisticsElement& element)
|
2020-06-03 22:49:24 +02:00
|
|
|
: EntryBase(element), tid_(element.tid), pid_(element.pid), uid_(element.uid) {}
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
pid_t tid() const { return tid_; }
|
|
|
|
pid_t pid() const { return pid_; }
|
|
|
|
uid_t uid() const { return uid_; }
|
|
|
|
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& element) {
|
|
|
|
if (uid_ != element.uid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
uid_ = -1;
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
if (pid_ != element.pid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
pid_ = -1;
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
2020-06-03 00:39:21 +02:00
|
|
|
if (tid_ != element.tid) {
|
2020-05-20 04:01:16 +02:00
|
|
|
tid_ = -1;
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
2020-05-20 04:01:16 +02:00
|
|
|
EntryBase::Add(element);
|
2017-04-19 23:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatHeader(const std::string& name, log_id_t id) const;
|
2020-06-03 22:49:24 +02:00
|
|
|
std::string format(const LogStatistics& stat, log_id_t id, const TagNameKey& key_name) const;
|
2020-05-20 04:01:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
pid_t tid_;
|
|
|
|
pid_t pid_;
|
|
|
|
uid_t uid_;
|
2017-04-19 23:39:21 +02:00
|
|
|
};
|
|
|
|
|
2014-02-06 23:48:50 +01:00
|
|
|
// Log Statistics
|
|
|
|
class LogStatistics {
|
2015-08-24 20:08:00 +02:00
|
|
|
friend UidEntry;
|
2020-05-07 23:44:43 +02:00
|
|
|
friend PidEntry;
|
|
|
|
friend TidEntry;
|
|
|
|
|
|
|
|
size_t mSizes[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
size_t mElements[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
size_t mDroppedElements[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
size_t mSizesTotal[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
size_t mElementsTotal[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
log_time mOldest[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
log_time mNewest[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
log_time mNewestDropped[LOG_ID_MAX] GUARDED_BY(lock_);
|
|
|
|
static std::atomic<size_t> SizesTotal;
|
2015-03-16 16:26:05 +01:00
|
|
|
bool enable;
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
// uid to size list
|
2015-03-16 16:26:05 +01:00
|
|
|
typedef LogHashtable<uid_t, UidEntry> uidTable_t;
|
2020-05-07 23:44:43 +02:00
|
|
|
uidTable_t uidTable[LOG_ID_MAX] GUARDED_BY(lock_);
|
2014-02-20 02:18:31 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
// pid of system to size list
|
|
|
|
typedef LogHashtable<pid_t, PidEntry> pidSystemTable_t;
|
2020-05-07 23:44:43 +02:00
|
|
|
pidSystemTable_t pidSystemTable[LOG_ID_MAX] GUARDED_BY(lock_);
|
2015-08-28 17:02:59 +02:00
|
|
|
|
2015-03-16 16:26:05 +01:00
|
|
|
// pid to uid list
|
|
|
|
typedef LogHashtable<pid_t, PidEntry> pidTable_t;
|
2020-05-07 23:44:43 +02:00
|
|
|
pidTable_t pidTable GUARDED_BY(lock_);
|
2015-03-16 16:26:05 +01:00
|
|
|
|
2015-04-20 22:35:15 +02:00
|
|
|
// tid to uid list
|
|
|
|
typedef LogHashtable<pid_t, TidEntry> tidTable_t;
|
2020-05-07 23:44:43 +02:00
|
|
|
tidTable_t tidTable GUARDED_BY(lock_);
|
2015-04-20 22:35:15 +02:00
|
|
|
|
2015-04-13 23:24:45 +02:00
|
|
|
// tag list
|
|
|
|
typedef LogHashtable<uint32_t, TagEntry> tagTable_t;
|
2020-05-07 23:44:43 +02:00
|
|
|
tagTable_t tagTable GUARDED_BY(lock_);
|
2015-04-13 23:24:45 +02:00
|
|
|
|
2015-12-04 19:59:45 +01:00
|
|
|
// security tag list
|
2020-05-07 23:44:43 +02:00
|
|
|
tagTable_t securityTagTable GUARDED_BY(lock_);
|
2015-12-04 19:59:45 +01:00
|
|
|
|
2017-04-19 23:39:21 +02:00
|
|
|
// global tag list
|
|
|
|
typedef LogHashtable<TagNameKey, TagNameEntry> tagNameTable_t;
|
|
|
|
tagNameTable_t tagNameTable;
|
|
|
|
|
2020-05-07 23:44:43 +02:00
|
|
|
size_t sizeOf() const REQUIRES(lock_) {
|
2016-10-06 18:55:21 +02:00
|
|
|
size_t size = sizeof(*this) + pidTable.sizeOf() + tidTable.sizeOf() +
|
2016-10-06 19:09:24 +02:00
|
|
|
tagTable.sizeOf() + securityTagTable.sizeOf() +
|
2017-04-19 23:39:21 +02:00
|
|
|
tagNameTable.sizeOf() +
|
2016-10-06 19:09:24 +02:00
|
|
|
(pidTable.size() * sizeof(pidTable_t::iterator)) +
|
|
|
|
(tagTable.size() * sizeof(tagTable_t::iterator));
|
2017-03-10 23:31:54 +01:00
|
|
|
for (auto it : pidTable) {
|
2020-05-20 04:01:16 +02:00
|
|
|
const char* name = it.second.name();
|
2016-10-06 18:55:21 +02:00
|
|
|
if (name) size += strlen(name) + 1;
|
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
for (auto it : tidTable) {
|
2020-05-20 04:01:16 +02:00
|
|
|
const char* name = it.second.name();
|
2016-10-06 18:55:21 +02:00
|
|
|
if (name) size += strlen(name) + 1;
|
|
|
|
}
|
2020-06-03 22:49:24 +02:00
|
|
|
for (auto it : tagNameTable) size += it.first.getAllocLength();
|
2016-10-06 18:55:21 +02:00
|
|
|
log_id_for_each(id) {
|
|
|
|
size += uidTable[id].sizeOf();
|
2016-10-06 19:09:24 +02:00
|
|
|
size += uidTable[id].size() * sizeof(uidTable_t::iterator);
|
2016-10-06 18:55:21 +02:00
|
|
|
size += pidSystemTable[id].sizeOf();
|
2017-03-10 23:31:54 +01:00
|
|
|
size +=
|
|
|
|
pidSystemTable[id].size() * sizeof(pidSystemTable_t::iterator);
|
2016-10-06 18:55:21 +02:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:44:43 +02:00
|
|
|
public:
|
|
|
|
LogStatistics(bool enable_statistics);
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-05-20 02:48:42 +02:00
|
|
|
void AddTotal(log_id_t log_id, uint16_t size) EXCLUDES(lock_);
|
2020-06-03 00:39:21 +02:00
|
|
|
void Add(const LogStatisticsElement& entry) EXCLUDES(lock_);
|
|
|
|
void Subtract(const LogStatisticsElement& entry) EXCLUDES(lock_);
|
2015-03-16 20:04:09 +01:00
|
|
|
// entry->setDropped(1) must follow this call
|
2020-06-03 00:39:21 +02:00
|
|
|
void Drop(const LogStatisticsElement& entry) EXCLUDES(lock_);
|
2015-09-30 16:40:09 +02:00
|
|
|
// Correct for coalescing two entries referencing dropped content
|
2020-06-03 00:39:21 +02:00
|
|
|
void Erase(const LogStatisticsElement& element) EXCLUDES(lock_) {
|
2020-05-07 23:44:43 +02:00
|
|
|
auto lock = std::lock_guard{lock_};
|
2020-06-03 00:39:21 +02:00
|
|
|
log_id_t log_id = element.log_id;
|
2015-09-30 16:40:09 +02:00
|
|
|
--mElements[log_id];
|
|
|
|
--mDroppedElements[log_id];
|
|
|
|
}
|
2014-02-20 02:18:31 +01:00
|
|
|
|
2020-05-07 18:13:12 +02:00
|
|
|
void WorstTwoUids(log_id id, size_t threshold, int* worst, size_t* worst_sizes,
|
2020-05-07 23:44:43 +02:00
|
|
|
size_t* second_worst_sizes) const EXCLUDES(lock_);
|
2020-05-07 18:13:12 +02:00
|
|
|
void WorstTwoTags(size_t threshold, int* worst, size_t* worst_sizes,
|
2020-05-07 23:44:43 +02:00
|
|
|
size_t* second_worst_sizes) const EXCLUDES(lock_);
|
2020-05-07 18:13:12 +02:00
|
|
|
void WorstTwoSystemPids(log_id id, size_t worst_uid_sizes, int* worst,
|
2020-05-07 23:44:43 +02:00
|
|
|
size_t* second_worst_sizes) const EXCLUDES(lock_);
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-05-07 23:44:43 +02:00
|
|
|
bool ShouldPrune(log_id id, unsigned long max_size, unsigned long* prune_rows) const
|
|
|
|
EXCLUDES(lock_);
|
|
|
|
|
|
|
|
// Snapshot of the sizes for a given log buffer.
|
|
|
|
size_t Sizes(log_id_t id) const EXCLUDES(lock_) {
|
|
|
|
auto lock = std::lock_guard{lock_};
|
2017-03-10 23:31:54 +01:00
|
|
|
return mSizes[id];
|
|
|
|
}
|
2020-05-07 23:44:43 +02:00
|
|
|
// TODO: Get rid of this entirely.
|
2017-03-10 23:31:54 +01:00
|
|
|
static size_t sizesTotal() {
|
|
|
|
return SizesTotal;
|
|
|
|
}
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-05-07 23:44:43 +02:00
|
|
|
std::string Format(uid_t uid, pid_t pid, unsigned int logMask) const EXCLUDES(lock_);
|
2014-04-07 16:05:40 +02:00
|
|
|
|
2020-05-07 23:44:43 +02:00
|
|
|
const char* PidToName(pid_t pid) const EXCLUDES(lock_);
|
|
|
|
uid_t PidToUid(pid_t pid) EXCLUDES(lock_);
|
|
|
|
const char* UidToName(uid_t uid) const EXCLUDES(lock_);
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2020-05-07 18:13:12 +02:00
|
|
|
private:
|
|
|
|
template <typename TKey, typename TEntry>
|
|
|
|
void WorstTwoWithThreshold(const LogHashtable<TKey, TEntry>& table, size_t threshold,
|
2020-05-07 23:44:43 +02:00
|
|
|
int* worst, size_t* worst_sizes, size_t* second_worst_sizes) const;
|
|
|
|
template <typename TKey, typename TEntry>
|
|
|
|
std::string FormatTable(const LogHashtable<TKey, TEntry>& table, uid_t uid, pid_t pid,
|
|
|
|
const std::string& name = std::string(""),
|
|
|
|
log_id_t id = LOG_ID_MAX) const REQUIRES(lock_);
|
|
|
|
void FormatTmp(const char* nameTmp, uid_t uid, std::string& name, std::string& size,
|
|
|
|
size_t nameLen) const REQUIRES(lock_);
|
|
|
|
const char* UidToNameLocked(uid_t uid) const REQUIRES(lock_);
|
|
|
|
|
|
|
|
mutable std::mutex lock_;
|
2020-05-07 18:13:12 +02:00
|
|
|
};
|