logd: use coalesce instead of merge (cleanup)

(cherry pick from commit aaad42f47c)

- switch to coalesce instead of merge in naming of functions
  and variables. Confusing since we also to merge-sorts and
  other activities in the logger.
- define maxPrune rather than using a number in the code path.

Bug: 24511000
This commit is contained in:
Mark Salyzyn 2015-09-30 07:40:09 -07:00 committed by Rom Lemarchand
parent 9bc35ef4c9
commit 6326bb8fd2
3 changed files with 19 additions and 18 deletions

View file

@ -217,7 +217,7 @@ int LogBuffer::log(log_id_t log_id, log_time realtime,
return len; return len;
} }
// Prune at most 10% of the log entries or 256, whichever is less. // Prune at most 10% of the log entries or maxPrune, whichever is less.
// //
// mLogElementsLock must be held when this function is called. // mLogElementsLock must be held when this function is called.
void LogBuffer::maybePrune(log_id_t id) { void LogBuffer::maybePrune(log_id_t id) {
@ -228,18 +228,18 @@ void LogBuffer::maybePrune(log_id_t id) {
size_t elements = stats.elements(id); size_t elements = stats.elements(id);
size_t minElements = elements / 10; size_t minElements = elements / 10;
unsigned long pruneRows = elements * sizeOver / sizes; unsigned long pruneRows = elements * sizeOver / sizes;
if (pruneRows <= minElements) { if (pruneRows < minElements) {
pruneRows = minElements; pruneRows = minElements;
} }
if (pruneRows > 256) { if (pruneRows > maxPrune) {
pruneRows = 256; pruneRows = maxPrune;
} }
prune(id, pruneRows); prune(id, pruneRows);
} }
} }
LogBufferElementCollection::iterator LogBuffer::erase( LogBufferElementCollection::iterator LogBuffer::erase(
LogBufferElementCollection::iterator it, bool engageStats) { LogBufferElementCollection::iterator it, bool coalesce) {
LogBufferElement *e = *it; LogBufferElement *e = *it;
log_id_t id = e->getLogId(); log_id_t id = e->getLogId();
@ -248,10 +248,10 @@ LogBufferElementCollection::iterator LogBuffer::erase(
mLastWorstUid[id].erase(f); mLastWorstUid[id].erase(f);
} }
it = mLogElements.erase(it); it = mLogElements.erase(it);
if (engageStats) { if (coalesce) {
stats.subtract(e);
} else {
stats.erase(e); stats.erase(e);
} else {
stats.subtract(e);
} }
delete e; delete e;
@ -286,7 +286,7 @@ class LogBufferElementLast {
public: public:
bool merge(LogBufferElement *e, unsigned short dropped) { bool coalesce(LogBufferElement *e, unsigned short dropped) {
LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid()); LogBufferElementKey key(e->getUid(), e->getPid(), e->getTid());
LogBufferElementMap::iterator it = map.find(key.getKey()); LogBufferElementMap::iterator it = map.find(key.getKey());
if (it != map.end()) { if (it != map.end()) {
@ -457,7 +457,7 @@ bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
it = mLogElements.begin(); it = mLogElements.begin();
// Perform at least one mandatory garbage collection cycle in following // Perform at least one mandatory garbage collection cycle in following
// - clear leading chatty tags // - clear leading chatty tags
// - merge chatty tags // - coalesce chatty tags
// - check age-out of preserved logs // - check age-out of preserved logs
bool gc = pruneRows <= 1; bool gc = pruneRows <= 1;
if (!gc && (worst != (uid_t) -1)) { if (!gc && (worst != (uid_t) -1)) {
@ -496,9 +496,8 @@ bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
continue; continue;
} }
// merge any drops if (dropped && last.coalesce(e, dropped)) {
if (dropped && last.merge(e, dropped)) { it = erase(it, true);
it = erase(it, false);
continue; continue;
} }
@ -529,7 +528,6 @@ bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
break; break;
} }
// unmerged drop message
if (dropped) { if (dropped) {
last.add(e); last.add(e);
if ((!gc && (e->getUid() == worst)) if ((!gc && (e->getUid() == worst))
@ -563,8 +561,8 @@ bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
} else { } else {
stats.drop(e); stats.drop(e);
e->setDropped(1); e->setDropped(1);
if (last.merge(e, 1)) { if (last.coalesce(e, 1)) {
it = erase(it, false); it = erase(it, true);
} else { } else {
last.add(e); last.add(e);
if (!gc || (mLastWorstUid[id].find(worst) if (!gc || (mLastWorstUid[id].find(worst)

View file

@ -85,10 +85,13 @@ public:
void unlock() { pthread_mutex_unlock(&mLogElementsLock); } void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
private: private:
static constexpr size_t maxPrune = 256;
void maybePrune(log_id_t id); void maybePrune(log_id_t id);
bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
LogBufferElementCollection::iterator erase( LogBufferElementCollection::iterator erase(
LogBufferElementCollection::iterator it, bool engageStats = true); LogBufferElementCollection::iterator it, bool coalesce = false);
}; };
#endif // _LOGD_LOG_BUFFER_H__ #endif // _LOGD_LOG_BUFFER_H__

View file

@ -403,7 +403,7 @@ public:
void subtract(LogBufferElement *entry); void subtract(LogBufferElement *entry);
// entry->setDropped(1) must follow this call // entry->setDropped(1) must follow this call
void drop(LogBufferElement *entry); void drop(LogBufferElement *entry);
// Correct for merging two entries referencing dropped content // Correct for coalescing two entries referencing dropped content
void erase(LogBufferElement *e) { --mElements[e->getLogId()]; } void erase(LogBufferElement *e) { --mElements[e->getLogId()]; }
std::unique_ptr<const UidEntry *[]> sort(size_t len, log_id id) { std::unique_ptr<const UidEntry *[]> sort(size_t len, log_id id) {