2014-02-26 18:50:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-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.
|
|
|
|
*/
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
#include <ctype.h>
|
2015-02-09 17:21:05 +01:00
|
|
|
#include <errno.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-05-10 02:44:18 +02:00
|
|
|
#include <sys/user.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-05-19 18:12:30 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
#include <cutils/properties.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <log/logger.h>
|
|
|
|
|
|
|
|
#include "LogBuffer.h"
|
2015-09-08 17:56:32 +02:00
|
|
|
#include "LogKlog.h"
|
2014-05-06 16:34:59 +02:00
|
|
|
#include "LogReader.h"
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
// Default
|
2016-01-05 17:49:44 +01:00
|
|
|
#define LOG_BUFFER_SIZE (256 * 1024) // Tuned with ro.logd.size per-platform
|
2014-02-11 21:29:31 +01:00
|
|
|
#define log_buffer_size(id) mMaxSize[id]
|
2014-05-10 02:44:18 +02:00
|
|
|
#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
|
|
|
|
#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
|
|
|
|
|
|
|
|
static bool valid_size(unsigned long value) {
|
|
|
|
if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
long pages = sysconf(_SC_PHYS_PAGES);
|
|
|
|
if (pages < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
long pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
if (pagesize <= 1) {
|
|
|
|
pagesize = PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// maximum memory impact a somewhat arbitrary ~3%
|
|
|
|
pages = (pages + 31) / 32;
|
|
|
|
unsigned long maximum = pages * pagesize;
|
|
|
|
|
|
|
|
if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value <= maximum;
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
static unsigned long property_get_size(const char *key) {
|
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
property_get(key, property, "");
|
|
|
|
|
|
|
|
char *cp;
|
|
|
|
unsigned long value = strtoul(property, &cp, 10);
|
|
|
|
|
|
|
|
switch(*cp) {
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
value *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
value *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!valid_size(value)) {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-11 00:45:17 +01:00
|
|
|
void LogBuffer::init() {
|
2014-05-10 02:44:18 +02:00
|
|
|
static const char global_tuneable[] = "persist.logd.size"; // Settings App
|
|
|
|
static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
unsigned long default_size = property_get_size(global_tuneable);
|
|
|
|
if (!default_size) {
|
|
|
|
default_size = property_get_size(global_default);
|
2015-12-14 22:07:12 +01:00
|
|
|
if (!default_size) {
|
2015-12-15 01:40:12 +01:00
|
|
|
default_size = property_get_bool("ro.config.low_ram",
|
|
|
|
BOOL_DEFAULT_FALSE)
|
|
|
|
? LOG_BUFFER_MIN_SIZE // 64K
|
|
|
|
: LOG_BUFFER_SIZE; // 256K
|
2015-12-14 22:07:12 +01:00
|
|
|
}
|
2014-05-10 02:44:18 +02:00
|
|
|
}
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
log_id_for_each(i) {
|
2014-05-06 16:34:59 +02:00
|
|
|
char key[PROP_NAME_MAX];
|
2014-05-10 02:44:18 +02:00
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
snprintf(key, sizeof(key), "%s.%s",
|
2014-05-10 02:44:18 +02:00
|
|
|
global_tuneable, android_log_id_to_name(i));
|
|
|
|
unsigned long property_size = property_get_size(key);
|
|
|
|
|
|
|
|
if (!property_size) {
|
|
|
|
snprintf(key, sizeof(key), "%s.%s",
|
|
|
|
global_default, android_log_id_to_name(i));
|
|
|
|
property_size = property_get_size(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!property_size) {
|
|
|
|
property_size = default_size;
|
|
|
|
}
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!property_size) {
|
|
|
|
property_size = LOG_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setSize(i, property_size)) {
|
|
|
|
setSize(i, LOG_BUFFER_MIN_SIZE);
|
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-09-08 17:56:32 +02:00
|
|
|
bool lastMonotonic = monotonic;
|
2015-12-02 00:57:25 +01:00
|
|
|
monotonic = android_log_clockid() == CLOCK_MONOTONIC;
|
2015-11-30 20:35:56 +01:00
|
|
|
if (lastMonotonic != monotonic) {
|
|
|
|
//
|
|
|
|
// Fixup all timestamps, may not be 100% accurate, but better than
|
|
|
|
// throwing what we have away when we get 'surprised' by a change.
|
|
|
|
// In-place element fixup so no need to check reader-lock. Entries
|
|
|
|
// should already be in timestamp order, but we could end up with a
|
|
|
|
// few out-of-order entries if new monotonics come in before we
|
|
|
|
// are notified of the reinit change in status. A Typical example would
|
|
|
|
// be:
|
|
|
|
// --------- beginning of system
|
|
|
|
// 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
|
|
|
|
// --------- beginning of kernel
|
|
|
|
// 0.000000 0 0 I : Initializing cgroup subsys
|
|
|
|
// as the act of mounting /data would trigger persist.logd.timestamp to
|
|
|
|
// be corrected. 1/30 corner case YMMV.
|
|
|
|
//
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
LogBufferElementCollection::iterator it = mLogElements.begin();
|
|
|
|
while((it != mLogElements.end())) {
|
|
|
|
LogBufferElement *e = *it;
|
|
|
|
if (monotonic) {
|
|
|
|
if (!android::isMonotonic(e->mRealTime)) {
|
|
|
|
LogKlog::convertRealToMonotonic(e->mRealTime);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (android::isMonotonic(e->mRealTime)) {
|
|
|
|
LogKlog::convertMonotonicToReal(e->mRealTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
2015-09-08 17:56:32 +02:00
|
|
|
}
|
|
|
|
|
2015-11-30 20:35:56 +01:00
|
|
|
// We may have been triggered by a SIGHUP. Release any sleeping reader
|
|
|
|
// threads to dump their current content.
|
2015-09-08 17:56:32 +02:00
|
|
|
//
|
2015-11-30 20:35:56 +01:00
|
|
|
// NB: this is _not_ performed in the context of a SIGHUP, it is
|
|
|
|
// performed during startup, and in context of reinit administrative thread
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
|
|
|
LastLogTimes::iterator times = mTimes.begin();
|
|
|
|
while(times != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*times);
|
|
|
|
if (entry->owned_Locked()) {
|
|
|
|
entry->triggerReader_Locked();
|
2015-09-08 17:56:32 +02:00
|
|
|
}
|
2015-11-30 20:35:56 +01:00
|
|
|
times++;
|
2015-09-08 17:56:32 +02:00
|
|
|
}
|
2015-11-30 20:35:56 +01:00
|
|
|
|
|
|
|
LogTimeEntry::unlock();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-09-08 17:56:32 +02:00
|
|
|
LogBuffer::LogBuffer(LastLogTimes *times):
|
2015-12-02 00:57:25 +01:00
|
|
|
monotonic(android_log_clockid() == CLOCK_MONOTONIC),
|
2015-09-08 17:56:32 +02:00
|
|
|
mTimes(*times) {
|
2015-03-11 00:45:17 +01:00
|
|
|
pthread_mutex_init(&mLogElementsLock, NULL);
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:21:05 +01:00
|
|
|
int LogBuffer::log(log_id_t log_id, log_time realtime,
|
|
|
|
uid_t uid, pid_t pid, pid_t tid,
|
|
|
|
const char *msg, unsigned short len) {
|
2014-02-26 18:50:16 +01:00
|
|
|
if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
|
2015-02-09 17:21:05 +01:00
|
|
|
return -EINVAL;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2014-10-02 22:07:05 +02:00
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
LogBufferElement *elem = new LogBufferElement(log_id, realtime,
|
2014-03-21 00:09:38 +01:00
|
|
|
uid, pid, tid, msg, len);
|
2016-01-06 22:17:43 +01:00
|
|
|
if (log_id != LOG_ID_SECURITY) {
|
2015-12-04 19:59:45 +01:00
|
|
|
int prio = ANDROID_LOG_INFO;
|
2016-01-06 22:17:43 +01:00
|
|
|
const char *tag = NULL;
|
2015-12-04 19:59:45 +01:00
|
|
|
if (log_id == LOG_ID_EVENTS) {
|
2016-01-06 22:17:43 +01:00
|
|
|
tag = android::tagToName(elem->getTag());
|
2015-12-04 19:59:45 +01:00
|
|
|
} else {
|
|
|
|
prio = *msg;
|
|
|
|
tag = msg + 1;
|
|
|
|
}
|
2016-01-06 22:17:43 +01:00
|
|
|
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
|
2015-12-04 19:59:45 +01:00
|
|
|
// Log traffic received to total
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
stats.add(elem);
|
|
|
|
stats.subtract(elem);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
delete elem;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
2014-10-02 22:07:05 +02:00
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
|
|
|
|
// Insert elements in time sorted order if possible
|
|
|
|
// NB: if end is region locked, place element at end of list
|
|
|
|
LogBufferElementCollection::iterator it = mLogElements.end();
|
|
|
|
LogBufferElementCollection::iterator last = it;
|
2014-10-14 01:49:47 +02:00
|
|
|
while (last != mLogElements.begin()) {
|
|
|
|
--it;
|
2014-02-18 20:23:53 +01:00
|
|
|
if ((*it)->getRealTime() <= realtime) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = it;
|
|
|
|
}
|
2014-02-18 20:23:53 +01:00
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (last == mLogElements.end()) {
|
|
|
|
mLogElements.push_back(elem);
|
|
|
|
} else {
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t end = 1;
|
2014-02-26 18:50:16 +01:00
|
|
|
bool end_set = false;
|
|
|
|
bool end_always = false;
|
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
LastLogTimes::iterator times = mTimes.begin();
|
|
|
|
while(times != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*times);
|
2014-02-26 18:50:16 +01:00
|
|
|
if (entry->owned_Locked()) {
|
|
|
|
if (!entry->mNonBlock) {
|
|
|
|
end_always = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!end_set || (end <= entry->mEnd)) {
|
|
|
|
end = entry->mEnd;
|
|
|
|
end_set = true;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
times++;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end_always
|
2015-03-03 22:39:37 +01:00
|
|
|
|| (end_set && (end >= (*last)->getSequence()))) {
|
2014-02-26 18:50:16 +01:00
|
|
|
mLogElements.push_back(elem);
|
|
|
|
} else {
|
|
|
|
mLogElements.insert(last,elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
stats.add(elem);
|
2014-02-26 18:50:16 +01:00
|
|
|
maybePrune(log_id);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
2015-02-09 17:21:05 +01:00
|
|
|
|
|
|
|
return len;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 16:40:09 +02:00
|
|
|
// Prune at most 10% of the log entries or maxPrune, whichever is less.
|
2014-02-26 18:50:16 +01:00
|
|
|
//
|
|
|
|
// mLogElementsLock must be held when this function is called.
|
|
|
|
void LogBuffer::maybePrune(log_id_t id) {
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t sizes = stats.sizes(id);
|
2015-08-10 19:23:56 +02:00
|
|
|
unsigned long maxSize = log_buffer_size(id);
|
|
|
|
if (sizes > maxSize) {
|
2015-08-19 21:20:36 +02:00
|
|
|
size_t sizeOver = sizes - ((maxSize * 9) / 10);
|
2015-09-30 16:40:09 +02:00
|
|
|
size_t elements = stats.realElements(id);
|
|
|
|
size_t minElements = elements / 100;
|
|
|
|
if (minElements < minPrune) {
|
|
|
|
minElements = minPrune;
|
|
|
|
}
|
2015-08-10 19:23:56 +02:00
|
|
|
unsigned long pruneRows = elements * sizeOver / sizes;
|
2015-09-30 16:40:09 +02:00
|
|
|
if (pruneRows < minElements) {
|
2015-08-10 19:23:56 +02:00
|
|
|
pruneRows = minElements;
|
2014-01-14 01:37:51 +01:00
|
|
|
}
|
2015-09-30 16:40:09 +02:00
|
|
|
if (pruneRows > maxPrune) {
|
|
|
|
pruneRows = maxPrune;
|
2015-08-19 21:20:36 +02:00
|
|
|
}
|
2014-01-14 01:37:51 +01:00
|
|
|
prune(id, pruneRows);
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:08:50 +02:00
|
|
|
LogBufferElementCollection::iterator LogBuffer::erase(
|
2015-09-30 16:40:09 +02:00
|
|
|
LogBufferElementCollection::iterator it, bool coalesce) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *element = *it;
|
|
|
|
log_id_t id = element->getLogId();
|
|
|
|
|
|
|
|
{ // start of scope for uid found iterator
|
|
|
|
LogBufferIteratorMap::iterator found =
|
|
|
|
mLastWorstUid[id].find(element->getUid());
|
|
|
|
if ((found != mLastWorstUid[id].end())
|
|
|
|
&& (it == found->second)) {
|
|
|
|
mLastWorstUid[id].erase(found);
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (element->getUid() == AID_SYSTEM) {
|
|
|
|
// start of scope for pid found iterator
|
|
|
|
LogBufferPidIteratorMap::iterator found =
|
|
|
|
mLastWorstPidOfSystem[id].find(element->getPid());
|
|
|
|
if ((found != mLastWorstPidOfSystem[id].end())
|
|
|
|
&& (it == found->second)) {
|
|
|
|
mLastWorstPidOfSystem[id].erase(found);
|
|
|
|
}
|
2015-08-20 02:06:11 +02:00
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
it = mLogElements.erase(it);
|
2015-09-30 16:40:09 +02:00
|
|
|
if (coalesce) {
|
2015-08-28 17:02:59 +02:00
|
|
|
stats.erase(element);
|
2015-09-30 16:40:09 +02:00
|
|
|
} else {
|
2015-08-28 17:02:59 +02:00
|
|
|
stats.subtract(element);
|
2015-09-04 01:08:50 +02:00
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
delete element;
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2015-04-18 00:38:04 +02:00
|
|
|
// Define a temporary mechanism to report the last LogBufferElement pointer
|
|
|
|
// for the specified uid, pid and tid. Used below to help merge-sort when
|
|
|
|
// pruning for worst UID.
|
|
|
|
class LogBufferElementKey {
|
|
|
|
const union {
|
|
|
|
struct {
|
|
|
|
uint16_t uid;
|
|
|
|
uint16_t pid;
|
|
|
|
uint16_t tid;
|
|
|
|
uint16_t padding;
|
|
|
|
} __packed;
|
|
|
|
uint64_t value;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
public:
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
|
|
|
|
uid(uid),
|
|
|
|
pid(pid),
|
|
|
|
tid(tid),
|
|
|
|
padding(0) {
|
|
|
|
}
|
|
|
|
LogBufferElementKey(uint64_t key):value(key) { }
|
2015-04-18 00:38:04 +02:00
|
|
|
|
|
|
|
uint64_t getKey() { return value; }
|
|
|
|
};
|
|
|
|
|
2015-05-19 18:12:30 +02:00
|
|
|
class LogBufferElementLast {
|
2015-04-18 00:38:04 +02:00
|
|
|
|
2015-05-19 18:12:30 +02:00
|
|
|
typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
|
|
|
|
LogBufferElementMap map;
|
2015-04-18 00:38:04 +02:00
|
|
|
|
2015-04-20 23:08:56 +02:00
|
|
|
public:
|
2015-05-19 18:12:30 +02:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
bool coalesce(LogBufferElement *element, unsigned short dropped) {
|
|
|
|
LogBufferElementKey key(element->getUid(),
|
|
|
|
element->getPid(),
|
|
|
|
element->getTid());
|
2015-05-19 18:12:30 +02:00
|
|
|
LogBufferElementMap::iterator it = map.find(key.getKey());
|
|
|
|
if (it != map.end()) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *found = it->second;
|
|
|
|
unsigned short moreDropped = found->getDropped();
|
|
|
|
if ((dropped + moreDropped) > USHRT_MAX) {
|
2015-05-19 18:12:30 +02:00
|
|
|
map.erase(it);
|
2015-04-18 00:38:04 +02:00
|
|
|
} else {
|
2015-08-28 17:02:59 +02:00
|
|
|
found->setDropped(dropped + moreDropped);
|
2015-04-18 00:38:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
void add(LogBufferElement *element) {
|
|
|
|
LogBufferElementKey key(element->getUid(),
|
|
|
|
element->getPid(),
|
|
|
|
element->getTid());
|
|
|
|
map[key.getKey()] = element;
|
2015-04-18 00:38:04 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 23:08:56 +02:00
|
|
|
inline void clear() {
|
2015-05-19 18:12:30 +02:00
|
|
|
map.clear();
|
2015-04-20 23:08:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
void clear(LogBufferElement *element) {
|
|
|
|
uint64_t current = element->getRealTime().nsec()
|
2015-06-04 22:35:30 +02:00
|
|
|
- (EXPIRE_RATELIMIT * NS_PER_SEC);
|
2015-05-19 18:12:30 +02:00
|
|
|
for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *mapElement = it->second;
|
|
|
|
if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
|
|
|
|
&& (current > mapElement->getRealTime().nsec())) {
|
2015-05-19 18:12:30 +02:00
|
|
|
it = map.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
2015-04-20 23:08:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 00:38:04 +02:00
|
|
|
};
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
// prune "pruneRows" of type "id" from the buffer.
|
|
|
|
//
|
2015-09-08 18:12:51 +02:00
|
|
|
// This garbage collection task is used to expire log entries. It is called to
|
|
|
|
// remove all logs (clear), all UID logs (unprivileged clear), or every
|
|
|
|
// 256 or 10% of the total logs (whichever is less) to prune the logs.
|
|
|
|
//
|
|
|
|
// First there is a prep phase where we discover the reader region lock that
|
|
|
|
// acts as a backstop to any pruning activity to stop there and go no further.
|
|
|
|
//
|
|
|
|
// There are three major pruning loops that follow. All expire from the oldest
|
|
|
|
// entries. Since there are multiple log buffers, the Android logging facility
|
|
|
|
// will appear to drop entries 'in the middle' when looking at multiple log
|
|
|
|
// sources and buffers. This effect is slightly more prominent when we prune
|
|
|
|
// the worst offender by logging source. Thus the logs slowly loose content
|
|
|
|
// and value as you move back in time. This is preferred since chatty sources
|
|
|
|
// invariably move the logs value down faster as less chatty sources would be
|
|
|
|
// expired in the noise.
|
|
|
|
//
|
|
|
|
// The first loop performs blacklisting and worst offender pruning. Falling
|
|
|
|
// through when there are no notable worst offenders and have not hit the
|
|
|
|
// region lock preventing further worst offender pruning. This loop also looks
|
|
|
|
// after managing the chatty log entries and merging to help provide
|
|
|
|
// statistical basis for blame. The chatty entries are not a notification of
|
|
|
|
// how much logs you may have, but instead represent how much logs you would
|
|
|
|
// have had in a virtual log buffer that is extended to cover all the in-memory
|
|
|
|
// logs without loss. They last much longer than the represented pruned logs
|
|
|
|
// since they get multiplied by the gains in the non-chatty log sources.
|
|
|
|
//
|
|
|
|
// The second loop get complicated because an algorithm of watermarks and
|
|
|
|
// history is maintained to reduce the order and keep processing time
|
|
|
|
// down to a minimum at scale. These algorithms can be costly in the face
|
|
|
|
// of larger log buffers, or severly limited processing time granted to a
|
|
|
|
// background task at lowest priority.
|
|
|
|
//
|
|
|
|
// This second loop does straight-up expiration from the end of the logs
|
|
|
|
// (again, remember for the specified log buffer id) but does some whitelist
|
|
|
|
// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
|
|
|
|
// spam filtration all take priority. This second loop also checks if a region
|
|
|
|
// lock is causing us to buffer too much in the logs to help the reader(s),
|
|
|
|
// and will tell the slowest reader thread to skip log entries, and if
|
|
|
|
// persistent and hits a further threshold, kill the reader thread.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2014-02-26 18:50:16 +01:00
|
|
|
// mLogElementsLock must be held when this function is called.
|
2015-09-08 18:12:51 +02:00
|
|
|
//
|
2015-09-17 00:34:00 +02:00
|
|
|
bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry *oldest = NULL;
|
2015-09-17 00:34:00 +02:00
|
|
|
bool busy = false;
|
2015-09-17 00:34:00 +02:00
|
|
|
bool clearAll = pruneRows == ULONG_MAX;
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
|
|
|
// Region locked?
|
2015-08-28 17:02:59 +02:00
|
|
|
LastLogTimes::iterator times = mTimes.begin();
|
|
|
|
while(times != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*times);
|
2014-12-17 09:53:41 +01:00
|
|
|
if (entry->owned_Locked() && entry->isWatching(id)
|
2015-11-30 20:35:56 +01:00
|
|
|
&& (!oldest ||
|
|
|
|
(oldest->mStart > entry->mStart) ||
|
|
|
|
((oldest->mStart == entry->mStart) &&
|
|
|
|
(entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
|
2014-02-26 18:50:16 +01:00
|
|
|
oldest = entry;
|
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
times++;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:11:13 +01:00
|
|
|
LogBufferElementCollection::iterator it;
|
|
|
|
|
2014-06-12 20:16:16 +02:00
|
|
|
if (caller_uid != AID_ROOT) {
|
2015-09-17 00:34:00 +02:00
|
|
|
// Only here if clearAll condition (pruneRows == ULONG_MAX)
|
2014-06-12 20:16:16 +02:00
|
|
|
for(it = mLogElements.begin(); it != mLogElements.end();) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *element = *it;
|
2014-06-12 20:16:16 +02:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
|
2015-09-17 00:34:00 +02:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (oldest && (oldest->mStart <= element->getSequence())) {
|
2015-09-17 00:34:00 +02:00
|
|
|
busy = true;
|
2015-11-30 20:35:56 +01:00
|
|
|
if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
|
|
|
|
oldest->triggerReader_Locked();
|
|
|
|
} else {
|
|
|
|
oldest->triggerSkip_Locked(id, pruneRows);
|
|
|
|
}
|
2014-06-12 20:16:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:34:00 +02:00
|
|
|
it = erase(it);
|
|
|
|
pruneRows--;
|
2014-06-12 20:16:16 +02:00
|
|
|
}
|
|
|
|
LogTimeEntry::unlock();
|
2015-09-17 00:34:00 +02:00
|
|
|
return busy;
|
2014-06-12 20:16:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
// prune by worst offenders; by blacklist, UID, and by PID of system UID
|
2015-12-04 19:59:45 +01:00
|
|
|
bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
|
2015-09-17 00:34:00 +02:00
|
|
|
while (!clearAll && (pruneRows > 0)) {
|
2014-02-07 03:11:13 +01:00
|
|
|
// recalculate the worst offender on every batched pass
|
|
|
|
uid_t worst = (uid_t) -1;
|
|
|
|
size_t worst_sizes = 0;
|
|
|
|
size_t second_worst_sizes = 0;
|
2015-08-28 17:02:59 +02:00
|
|
|
pid_t worstPid = 0; // POSIX guarantees PID != 0
|
2014-02-07 03:11:13 +01:00
|
|
|
|
2015-03-18 01:17:25 +01:00
|
|
|
if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
|
2015-08-28 17:02:59 +02:00
|
|
|
{ // begin scope for UID sorted list
|
|
|
|
std::unique_ptr<const UidEntry *[]> sorted = stats.sort(
|
|
|
|
AID_ROOT, (pid_t)0, 2, id);
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (sorted.get() && sorted[0] && sorted[1]) {
|
2015-03-10 21:51:35 +01:00
|
|
|
worst_sizes = sorted[0]->getSizes();
|
2015-04-21 00:36:12 +02:00
|
|
|
// Calculate threshold as 12.5% of available storage
|
|
|
|
size_t threshold = log_buffer_size(id) / 8;
|
2015-10-12 22:45:51 +02:00
|
|
|
if ((worst_sizes > threshold)
|
|
|
|
// Allow time horizon to extend roughly tenfold, assume
|
|
|
|
// average entry length is 100 characters.
|
|
|
|
&& (worst_sizes > (10 * sorted[0]->getDropped()))) {
|
2015-04-21 00:36:12 +02:00
|
|
|
worst = sorted[0]->getKey();
|
|
|
|
second_worst_sizes = sorted[1]->getSizes();
|
|
|
|
if (second_worst_sizes < threshold) {
|
|
|
|
second_worst_sizes = threshold;
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
|
|
|
|
if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
|
|
|
|
// begin scope of PID sorted list
|
|
|
|
std::unique_ptr<const PidEntry *[]> sorted = stats.sort(
|
|
|
|
worst, (pid_t)0, 2, id, worst);
|
|
|
|
if (sorted.get() && sorted[0] && sorted[1]) {
|
|
|
|
worstPid = sorted[0]->getKey();
|
|
|
|
second_worst_sizes = worst_sizes
|
|
|
|
- sorted[0]->getSizes()
|
|
|
|
+ sorted[1]->getSizes();
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
// skip if we have neither worst nor naughty filters
|
|
|
|
if ((worst == (uid_t) -1) && !hasBlacklist) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-07 03:11:13 +01:00
|
|
|
bool kick = false;
|
2015-03-16 20:04:09 +01:00
|
|
|
bool leading = true;
|
2015-08-20 02:06:11 +02:00
|
|
|
it = mLogElements.begin();
|
2015-09-08 18:12:51 +02:00
|
|
|
// Perform at least one mandatory garbage collection cycle in following
|
|
|
|
// - clear leading chatty tags
|
2015-09-30 16:40:09 +02:00
|
|
|
// - coalesce chatty tags
|
2015-09-08 18:12:51 +02:00
|
|
|
// - check age-out of preserved logs
|
|
|
|
bool gc = pruneRows <= 1;
|
|
|
|
if (!gc && (worst != (uid_t) -1)) {
|
2015-08-28 17:02:59 +02:00
|
|
|
{ // begin scope for uid worst found iterator
|
|
|
|
LogBufferIteratorMap::iterator found = mLastWorstUid[id].find(worst);
|
|
|
|
if ((found != mLastWorstUid[id].end())
|
|
|
|
&& (found->second != mLogElements.end())) {
|
|
|
|
leading = false;
|
|
|
|
it = found->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (worstPid) {
|
|
|
|
// begin scope for pid worst found iterator
|
|
|
|
LogBufferPidIteratorMap::iterator found
|
|
|
|
= mLastWorstPidOfSystem[id].find(worstPid);
|
|
|
|
if ((found != mLastWorstPidOfSystem[id].end())
|
|
|
|
&& (found->second != mLogElements.end())) {
|
|
|
|
leading = false;
|
|
|
|
it = found->second;
|
|
|
|
}
|
2015-08-20 02:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 22:43:27 +02:00
|
|
|
static const timespec too_old = {
|
|
|
|
EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
|
|
|
|
};
|
|
|
|
LogBufferElementCollection::iterator lastt;
|
|
|
|
lastt = mLogElements.end();
|
|
|
|
--lastt;
|
2015-04-18 00:38:04 +02:00
|
|
|
LogBufferElementLast last;
|
2015-08-20 02:06:11 +02:00
|
|
|
while (it != mLogElements.end()) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *element = *it;
|
2014-02-07 03:11:13 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (oldest && (oldest->mStart <= element->getSequence())) {
|
2015-09-17 00:34:00 +02:00
|
|
|
busy = true;
|
2015-11-30 20:35:56 +01:00
|
|
|
if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
|
|
|
|
oldest->triggerReader_Locked();
|
|
|
|
}
|
2014-02-07 03:11:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (element->getLogId() != id) {
|
2014-02-11 21:29:31 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
unsigned short dropped = element->getDropped();
|
2014-02-11 21:29:31 +01:00
|
|
|
|
2015-03-16 20:04:09 +01:00
|
|
|
// remove any leading drops
|
|
|
|
if (leading && dropped) {
|
|
|
|
it = erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (dropped && last.coalesce(element, dropped)) {
|
2015-09-30 16:40:09 +02:00
|
|
|
it = erase(it, true);
|
2015-03-16 20:04:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (hasBlacklist && mPrune.naughty(element)) {
|
|
|
|
last.clear(element);
|
2015-03-16 20:04:09 +01:00
|
|
|
it = erase(it);
|
|
|
|
if (dropped) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pruneRows--;
|
|
|
|
if (pruneRows == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (element->getUid() == worst) {
|
2015-03-16 20:04:09 +01:00
|
|
|
kick = true;
|
|
|
|
if (worst_sizes < second_worst_sizes) {
|
|
|
|
break;
|
|
|
|
}
|
2015-08-28 17:02:59 +02:00
|
|
|
worst_sizes -= element->getMsgLen();
|
2015-03-16 20:04:09 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
|
|
|
|
|| (element->getRealTime() > (*lastt)->getRealTime())) {
|
2015-08-24 22:43:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:04:09 +01:00
|
|
|
if (dropped) {
|
2015-08-28 17:02:59 +02:00
|
|
|
last.add(element);
|
|
|
|
if (worstPid
|
|
|
|
&& ((!gc && (element->getPid() == worstPid))
|
|
|
|
|| (mLastWorstPidOfSystem[id].find(element->getPid())
|
|
|
|
== mLastWorstPidOfSystem[id].end()))) {
|
|
|
|
mLastWorstPidOfSystem[id][element->getUid()] = it;
|
|
|
|
}
|
|
|
|
if ((!gc && !worstPid && (element->getUid() == worst))
|
|
|
|
|| (mLastWorstUid[id].find(element->getUid())
|
2015-08-24 22:43:27 +02:00
|
|
|
== mLastWorstUid[id].end())) {
|
2015-08-28 17:02:59 +02:00
|
|
|
mLastWorstUid[id][element->getUid()] = it;
|
2015-08-24 22:43:27 +02:00
|
|
|
}
|
2015-03-16 20:04:09 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if ((element->getUid() != worst)
|
|
|
|
|| (worstPid && (element->getPid() != worstPid))) {
|
2015-06-01 18:41:19 +02:00
|
|
|
leading = false;
|
2015-08-28 17:02:59 +02:00
|
|
|
last.clear(element);
|
2014-02-07 03:11:13 +01:00
|
|
|
++it;
|
2015-03-10 21:51:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pruneRows--;
|
|
|
|
if (pruneRows == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
kick = true;
|
2015-03-16 20:04:09 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
unsigned short len = element->getMsgLen();
|
logd: deal with sloppy leading expire messages
The odds of chatty content also leading the logs is pretty high eg:
1799 12017 I logd: uid=10007 chatty comm=Binder_B, expire 4 lines
1799 1829 I logd: uid=10007 chatty comm=Binder_2, expire 4 lines
1919 20637 I logd: uid=10007 chatty comm=m.sersistent, expire 1 line
1919 20638 I logd: uid=10007 chatty comm=s.persistent, expire 1 line
1919 2316 I logd: uid=10007 chatty comm=UlrDispatch, expire 4 lines
19379 20634 I logd: uid=10045 chatty, expire 14 lines
19379 19388 I logd: uid=10045 chatty comm=lizerDaemon, expire 4 lines
591 4396 I logd: uid=1000 chatty comm=Thread-220, expire 5 lines
591 1377 I logd: uid=1000 chatty comm=Thread-92, expire 4 lines
1919 2267 I logd: uid=10007 chatty comm=WifiScanner, expire 4 lines
591 4397 I logd: uid=1000 chatty comm=DhcpClient, expire 4 lines
591 4398 I logd: uid=1000 chatty comm=Thread-222, expire 4 lines
226 580 D CommandListener: Setting iface cfg
Change-Id: I5ab24bc7bf5d2690bae7e789831b07f23ff8bcc6
2015-05-22 19:03:31 +02:00
|
|
|
|
|
|
|
// do not create any leading drops
|
|
|
|
if (leading) {
|
|
|
|
it = erase(it);
|
2015-03-16 20:04:09 +01:00
|
|
|
} else {
|
2015-08-28 17:02:59 +02:00
|
|
|
stats.drop(element);
|
|
|
|
element->setDropped(1);
|
|
|
|
if (last.coalesce(element, 1)) {
|
2015-09-30 16:40:09 +02:00
|
|
|
it = erase(it, true);
|
logd: deal with sloppy leading expire messages
The odds of chatty content also leading the logs is pretty high eg:
1799 12017 I logd: uid=10007 chatty comm=Binder_B, expire 4 lines
1799 1829 I logd: uid=10007 chatty comm=Binder_2, expire 4 lines
1919 20637 I logd: uid=10007 chatty comm=m.sersistent, expire 1 line
1919 20638 I logd: uid=10007 chatty comm=s.persistent, expire 1 line
1919 2316 I logd: uid=10007 chatty comm=UlrDispatch, expire 4 lines
19379 20634 I logd: uid=10045 chatty, expire 14 lines
19379 19388 I logd: uid=10045 chatty comm=lizerDaemon, expire 4 lines
591 4396 I logd: uid=1000 chatty comm=Thread-220, expire 5 lines
591 1377 I logd: uid=1000 chatty comm=Thread-92, expire 4 lines
1919 2267 I logd: uid=10007 chatty comm=WifiScanner, expire 4 lines
591 4397 I logd: uid=1000 chatty comm=DhcpClient, expire 4 lines
591 4398 I logd: uid=1000 chatty comm=Thread-222, expire 4 lines
226 580 D CommandListener: Setting iface cfg
Change-Id: I5ab24bc7bf5d2690bae7e789831b07f23ff8bcc6
2015-05-22 19:03:31 +02:00
|
|
|
} else {
|
2015-08-28 17:02:59 +02:00
|
|
|
last.add(element);
|
|
|
|
if (worstPid && (!gc
|
|
|
|
|| (mLastWorstPidOfSystem[id].find(worstPid)
|
|
|
|
== mLastWorstPidOfSystem[id].end()))) {
|
|
|
|
mLastWorstPidOfSystem[id][worstPid] = it;
|
|
|
|
}
|
|
|
|
if ((!gc && !worstPid) || (mLastWorstUid[id].find(worst)
|
2015-09-08 18:12:51 +02:00
|
|
|
== mLastWorstUid[id].end())) {
|
|
|
|
mLastWorstUid[id][worst] = it;
|
|
|
|
}
|
logd: deal with sloppy leading expire messages
The odds of chatty content also leading the logs is pretty high eg:
1799 12017 I logd: uid=10007 chatty comm=Binder_B, expire 4 lines
1799 1829 I logd: uid=10007 chatty comm=Binder_2, expire 4 lines
1919 20637 I logd: uid=10007 chatty comm=m.sersistent, expire 1 line
1919 20638 I logd: uid=10007 chatty comm=s.persistent, expire 1 line
1919 2316 I logd: uid=10007 chatty comm=UlrDispatch, expire 4 lines
19379 20634 I logd: uid=10045 chatty, expire 14 lines
19379 19388 I logd: uid=10045 chatty comm=lizerDaemon, expire 4 lines
591 4396 I logd: uid=1000 chatty comm=Thread-220, expire 5 lines
591 1377 I logd: uid=1000 chatty comm=Thread-92, expire 4 lines
1919 2267 I logd: uid=10007 chatty comm=WifiScanner, expire 4 lines
591 4397 I logd: uid=1000 chatty comm=DhcpClient, expire 4 lines
591 4398 I logd: uid=1000 chatty comm=Thread-222, expire 4 lines
226 580 D CommandListener: Setting iface cfg
Change-Id: I5ab24bc7bf5d2690bae7e789831b07f23ff8bcc6
2015-05-22 19:03:31 +02:00
|
|
|
++it;
|
|
|
|
}
|
2015-03-16 20:04:09 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
if (worst_sizes < second_worst_sizes) {
|
|
|
|
break;
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
worst_sizes -= len;
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
2015-04-18 00:38:04 +02:00
|
|
|
last.clear();
|
2014-02-07 03:11:13 +01:00
|
|
|
|
2014-04-02 02:19:47 +02:00
|
|
|
if (!kick || !mPrune.worstUidEnabled()) {
|
2014-02-07 03:11:13 +01:00
|
|
|
break; // the following loop will ask bad clients to skip/drop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
bool whitelist = false;
|
2015-12-04 19:59:45 +01:00
|
|
|
bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
|
2014-02-07 03:11:13 +01:00
|
|
|
it = mLogElements.begin();
|
2014-02-26 18:50:16 +01:00
|
|
|
while((pruneRows > 0) && (it != mLogElements.end())) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *element = *it;
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (element->getLogId() != id) {
|
2015-03-10 21:51:35 +01:00
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (oldest && (oldest->mStart <= element->getSequence())) {
|
2015-09-17 00:34:00 +02:00
|
|
|
busy = true;
|
2015-03-10 21:51:35 +01:00
|
|
|
if (whitelist) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-02 02:19:47 +02:00
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
if (stats.sizes(id) > (2 * log_buffer_size(id))) {
|
|
|
|
// kick a misbehaving log reader client off the island
|
|
|
|
oldest->release_Locked();
|
2015-11-30 20:35:56 +01:00
|
|
|
} else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
|
|
|
|
oldest->triggerReader_Locked();
|
2015-03-10 21:51:35 +01:00
|
|
|
} else {
|
|
|
|
oldest->triggerSkip_Locked(id, pruneRows);
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-02 02:19:47 +02:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
|
|
|
|
// WhiteListed
|
2015-03-10 21:51:35 +01:00
|
|
|
whitelist = true;
|
2014-02-26 18:50:16 +01:00
|
|
|
it++;
|
2015-03-10 21:51:35 +01:00
|
|
|
continue;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
it = erase(it);
|
|
|
|
pruneRows--;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
// Do not save the whitelist if we are reader range limited
|
2014-02-11 21:29:31 +01:00
|
|
|
if (whitelist && (pruneRows > 0)) {
|
|
|
|
it = mLogElements.begin();
|
|
|
|
while((it != mLogElements.end()) && (pruneRows > 0)) {
|
2015-08-28 17:02:59 +02:00
|
|
|
LogBufferElement *element = *it;
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (element->getLogId() != id) {
|
2015-03-10 21:51:35 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:02:59 +02:00
|
|
|
if (oldest && (oldest->mStart <= element->getSequence())) {
|
2015-09-17 00:34:00 +02:00
|
|
|
busy = true;
|
2015-03-10 21:51:35 +01:00
|
|
|
if (stats.sizes(id) > (2 * log_buffer_size(id))) {
|
|
|
|
// kick a misbehaving log reader client off the island
|
|
|
|
oldest->release_Locked();
|
2015-11-30 20:35:56 +01:00
|
|
|
} else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
|
|
|
|
oldest->triggerReader_Locked();
|
2015-03-10 21:51:35 +01:00
|
|
|
} else {
|
|
|
|
oldest->triggerSkip_Locked(id, pruneRows);
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
break;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
it = erase(it);
|
|
|
|
pruneRows--;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry::unlock();
|
2015-09-17 00:34:00 +02:00
|
|
|
|
|
|
|
return (pruneRows > 0) && busy;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear all rows of type "id" from the buffer.
|
2015-09-17 00:34:00 +02:00
|
|
|
bool LogBuffer::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.
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
busy = prune(id, 1, uid);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
// 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) {
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
LastLogTimes::iterator times = mTimes.begin();
|
|
|
|
while (times != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*times);
|
|
|
|
// Killer punch
|
|
|
|
if (entry->owned_Locked() && entry->isWatching(id)) {
|
|
|
|
entry->release_Locked();
|
|
|
|
}
|
|
|
|
times++;
|
|
|
|
}
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
busy = prune(id, ULONG_MAX, uid);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
if (!busy || !--retry) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep (1); // Let reader(s) catch up after notification
|
|
|
|
}
|
|
|
|
return busy;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the used space associated with "id".
|
|
|
|
unsigned long LogBuffer::getSizeUsed(log_id_t id) {
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t retval = stats.sizes(id);
|
2014-02-26 18:50:16 +01:00
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
// set the total space allocated to "id"
|
|
|
|
int LogBuffer::setSize(log_id_t id, unsigned long size) {
|
|
|
|
// Reasonable limits ...
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!valid_size(size)) {
|
2014-02-11 21:29:31 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
log_buffer_size(id) = size;
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the total space allocated to "id"
|
|
|
|
unsigned long LogBuffer::getSize(log_id_t id) {
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
size_t retval = log_buffer_size(id);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t LogBuffer::flushTo(
|
|
|
|
SocketClient *reader, const uint64_t start, bool privileged,
|
|
|
|
int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogBufferElementCollection::iterator it;
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t max = start;
|
2014-02-26 18:50:16 +01:00
|
|
|
uid_t uid = reader->getUid();
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
2015-01-15 16:29:43 +01:00
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (start <= 1) {
|
2015-01-15 16:29:43 +01:00
|
|
|
// 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;
|
2015-03-03 22:39:37 +01:00
|
|
|
if (element->getSequence() <= start) {
|
2015-01-15 16:29:43 +01:00
|
|
|
it++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != mLogElements.end(); ++it) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogBufferElement *element = *it;
|
|
|
|
|
|
|
|
if (!privileged && (element->getUid() != uid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (element->getSequence() <= start) {
|
2014-02-26 18:50:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: calling out to another object with mLogElementsLock held (safe)
|
2015-03-03 22:39:37 +01:00
|
|
|
if (filter) {
|
|
|
|
int ret = (*filter)(element, arg);
|
|
|
|
if (ret == false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ret != true) {
|
|
|
|
break;
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
|
|
|
|
// range locking in LastLogTimes looks after us
|
2015-12-04 00:38:35 +01:00
|
|
|
max = element->flushTo(reader, this, privileged);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
if (max == element->FLUSH_ERROR) {
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2015-12-17 18:58:43 +01:00
|
|
|
std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
|
|
|
|
unsigned int logMask) {
|
2014-02-06 23:48:50 +01:00
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
|
2015-12-17 18:58:43 +01:00
|
|
|
std::string ret = stats.format(uid, pid, logMask);
|
2014-02-06 23:48:50 +01:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
2015-08-20 19:01:44 +02:00
|
|
|
|
|
|
|
return ret;
|
2014-02-06 23:48:50 +01:00
|
|
|
}
|