Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 64e90163 authored by Tom Cherry's avatar Tom Cherry
Browse files

logd: separate LogStatistics from LogBuffer

LogStatistics is intertwined with LogBuffer, even relying on it for
thread safety.  This needs to change to have a proper
LogBufferInterface, so this CL separates them.  Specifically:

1) Adding a lock to LogStatistics and adding thread annotations to
   ensure that data structures are protected appropriately.
2) Moving prune_rows calculation into LogStatistics so it is done
   while holding this lock.
3) Using LogStatistics instead of LogBuffer where appropriate.

Note that there should not be a significant performance regression
with this lock, as it will almost always been uncontended.  If
anything, it should alleviate pressure from LogBuffer's lock.

Test: logging unit tests
Change-Id: I9d6dde2c96c9f024fa0341711c7bc63379e8e406
parent b6b78e9b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ cc_library_static {

    cflags: [
        "-Wextra",
        "-Wthread-safety",
    ] + event_flag,
}

+7 −6
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@
 * limitations under the License.
 */

#include "CommandListener.h"

#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
@@ -35,12 +37,11 @@
#include <private/android_filesystem_config.h>
#include <sysutils/SocketClient.h>

#include "CommandListener.h"
#include "LogCommand.h"
#include "LogUtils.h"

CommandListener::CommandListener(LogBuffer* buf, LogTags* tags, PruneList* prune)
    : FrameworkListener(getLogSocket()), buf_(buf), tags_(tags), prune_(prune) {
CommandListener::CommandListener(LogBuffer* buf, LogTags* tags, PruneList* prune,
                                 LogStatistics* stats)
    : FrameworkListener(getLogSocket()), buf_(buf), tags_(tags), prune_(prune), stats_(stats) {
    registerCmd(new ClearCmd(this));
    registerCmd(new GetBufSizeCmd(this));
    registerCmd(new SetBufSizeCmd(this));
@@ -148,7 +149,7 @@ int CommandListener::GetBufSizeUsedCmd::runCommand(SocketClient* cli, int argc,
        return 0;
    }

    unsigned long size = buf()->getSizeUsed((log_id_t)id);
    unsigned long size = stats()->Sizes((log_id_t)id);
    char buf[512];
    snprintf(buf, sizeof(buf), "%lu", size);
    cli->sendMsg(buf);
@@ -209,7 +210,7 @@ int CommandListener::GetStatisticsCmd::runCommand(SocketClient* cli, int argc,
        }
    }

    cli->sendMsg(PackageString(buf()->formatStatistics(uid, pid, logMask)).c_str());
    cli->sendMsg(PackageString(stats()->Format(uid, pid, logMask)).c_str());
    return 0;
}

+17 −14
Original line number Diff line number Diff line
@@ -22,12 +22,13 @@
#include "LogCommand.h"
#include "LogListener.h"
#include "LogReader.h"
#include "LogStatistics.h"
#include "LogTags.h"
#include "LogWhiteBlackList.h"

class CommandListener : public FrameworkListener {
  public:
    CommandListener(LogBuffer* buf, LogTags* tags, PruneList* prune);
    CommandListener(LogBuffer* buf, LogTags* tags, PruneList* prune, LogStatistics* log_statistics);
    virtual ~CommandListener() {}

  private:
@@ -36,6 +37,7 @@ class CommandListener : public FrameworkListener {
    LogBuffer* buf_;
    LogTags* tags_;
    PruneList* prune_;
    LogStatistics* stats_;

#define LogCmd(name, command_string)                             \
    class name##Cmd : public LogCommand {                        \
@@ -49,6 +51,7 @@ class CommandListener : public FrameworkListener {
        LogBuffer* buf() const { return parent_->buf_; }         \
        LogTags* tags() const { return parent_->tags_; }         \
        PruneList* prune() const { return parent_->prune_; }     \
        LogStatistics* stats() const { return parent_->stats_; } \
        CommandListener* parent_;                                \
    }

+9 −14
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@
 * limitations under the License.
 */

#include "LogAudit.h"

#include <ctype.h>
#include <endian.h>
#include <errno.h>
@@ -34,8 +36,6 @@
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>

#include "LogAudit.h"
#include "LogBuffer.h"
#include "LogKlog.h"
#include "LogReader.h"
#include "LogUtils.h"
@@ -45,16 +45,15 @@
    '<', '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
        '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, '>'

LogAudit::LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg)
LogAudit::LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg, LogStatistics* stats)
    : SocketListener(getLogSocket(), false),
      logbuf(buf),
      reader(reader),
      fdDmesg(fdDmesg),
      main(__android_logger_property_get_bool("ro.logd.auditd.main",
                                              BOOL_DEFAULT_TRUE)),
      events(__android_logger_property_get_bool("ro.logd.auditd.events",
                                                BOOL_DEFAULT_TRUE)),
      initialized(false) {
      main(__android_logger_property_get_bool("ro.logd.auditd.main", BOOL_DEFAULT_TRUE)),
      events(__android_logger_property_get_bool("ro.logd.auditd.events", BOOL_DEFAULT_TRUE)),
      initialized(false),
      stats_(stats) {
    static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
                                           'l',
                                           'o',
@@ -211,9 +210,7 @@ int LogAudit::logPrint(const char* fmt, ...) {
            ++cp;
        }
        tid = pid;
        logbuf->wrlock();
        uid = logbuf->pidToUid(pid);
        logbuf->unlock();
        uid = stats_->PidToUid(pid);
        memmove(pidptr, cp, strlen(cp) + 1);
    }

@@ -301,9 +298,7 @@ int LogAudit::logPrint(const char* fmt, ...) {
        pid = tid;
        comm = "auditd";
    } else {
        logbuf->wrlock();
        comm = commfree = logbuf->pidToName(pid);
        logbuf->unlock();
        comm = commfree = stats_->PidToName(pid);
        if (!comm) {
            comm = "unknown";
        }
+8 −8
Original line number Diff line number Diff line
@@ -14,14 +14,14 @@
 * limitations under the License.
 */

#ifndef _LOGD_LOG_AUDIT_H__
#define _LOGD_LOG_AUDIT_H__
#pragma once

#include <map>

#include <sysutils/SocketListener.h>

#include "LogBuffer.h"
#include "LogStatistics.h"

class LogReader;

@@ -34,7 +34,7 @@ class LogAudit : public SocketListener {
    bool initialized;

  public:
    LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg);
    LogAudit(LogBuffer* buf, LogReader* reader, int fdDmesg, LogStatistics* stats);
    int log(char* buf, size_t len);

  protected:
@@ -48,6 +48,6 @@ class LogAudit : public SocketListener {
    void auditParse(const std::string& string, uid_t uid, std::string* bug_num);
    int logPrint(const char* fmt, ...)
        __attribute__((__format__(__printf__, 2, 3)));
};

#endif
    LogStatistics* stats_;
};
Loading