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

Commit 88e01661 authored by Mark Salyzyn's avatar Mark Salyzyn Committed by Gerrit Code Review
Browse files

Merge "logd: optimize code hotspots"

parents 72146b93 ddda212f
Loading
Loading
Loading
Loading
+20 −15
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <sys/uio.h>
#include <syslog.h>

#include <log/logger.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>

@@ -153,15 +154,16 @@ int LogAudit::logPrint(const char *fmt, ...) {

    // log to events

    size_t l = strlen(str);
    size_t l = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
    size_t n = l + sizeof(android_log_event_string_t);

    bool notify = false;

    android_log_event_string_t *event = static_cast<android_log_event_string_t *>(malloc(n));
    if (!event) {
        rc = -ENOMEM;
    } else {
    {   // begin scope for event buffer
        uint32_t buffer[(n + sizeof(uint32_t) - 1) / sizeof(uint32_t)];

        android_log_event_string_t *event
            = reinterpret_cast<android_log_event_string_t *>(buffer);
        event->header.tag = htole32(AUDITD_LOG_TAG);
        event->type = EVENT_TYPE_STRING;
        event->length = htole32(l);
@@ -170,11 +172,10 @@ int LogAudit::logPrint(const char *fmt, ...) {
        rc = logbuf->log(LOG_ID_EVENTS, now, uid, pid, tid,
                         reinterpret_cast<char *>(event),
                         (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
        free(event);

        if (rc >= 0) {
            notify = true;
        }
        // end scope for event buffer
    }

    // log to main
@@ -206,24 +207,28 @@ int LogAudit::logPrint(const char *fmt, ...) {
        l = strlen(comm) + 1;
        ecomm = "";
    }
    n = (estr - str) + strlen(ecomm) + l + 2;
    size_t b = estr - str;
    if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
        b = LOGGER_ENTRY_MAX_PAYLOAD;
    }
    size_t e = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - b);
    n = b + e + l + 2;

    {   // begin scope for main buffer
        char newstr[n];

    char *newstr = static_cast<char *>(malloc(n));
    if (!newstr) {
        rc = -ENOMEM;
    } else {
        *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN;
        strlcpy(newstr + 1, comm, l);
        strncpy(newstr + 1 + l, str, estr - str);
        strcpy(newstr + 1 + l + (estr - str), ecomm);
        strncpy(newstr + 1 + l, str, b);
        strncpy(newstr + 1 + l + b, ecomm, e);

        rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
                         (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
        free(newstr);

        if (rc >= 0) {
            notify = true;
        }
        // end scope for main buffer
    }

    free(commfree);
+15 −12
Original line number Diff line number Diff line
@@ -91,7 +91,8 @@ char *android::tidToName(pid_t tid) {
        size_t retval_len = strlen(retval);
        size_t name_len = strlen(name);
        // KISS: ToDo: Only checks prefix truncated, not suffix, or both
        if ((retval_len < name_len) && !strcmp(retval, name + name_len - retval_len)) {
        if ((retval_len < name_len)
                && !fast<strcmp>(retval, name + name_len - retval_len)) {
            free(retval);
            retval = name;
        } else {
@@ -123,9 +124,10 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer,
        commName = parent->pidToName(mPid);
        parent->unlock();
    }
    size_t len = name ? strlen(name) : 0;
    if (len && commName && !strncmp(name, commName, len)) {
        if (commName[len] == '\0') {
    if (name && name[0] && commName && (name[0] == commName[0])) {
        size_t len = strlen(name + 1);
        if (!strncmp(name + 1, commName + 1, len)) {
            if (commName[len + 1] == '\0') {
                free(commName);
                commName = NULL;
            } else {
@@ -133,6 +135,7 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer,
                name = NULL;
            }
        }
    }
    if (name) {
        char *p = NULL;
        asprintf(&p, "(%s)", name);
@@ -150,7 +153,7 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer,
        }
    }
    // identical to below to calculate the buffer size required
    len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
    size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
                          commName ? commName : "",
                          mDropped, (mDropped > 1) ? "s" : "");

+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ public:
    unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
    unsigned short setDropped(unsigned short value) {
        if (mMsg) {
            free(mMsg);
            delete [] mMsg;
            mMsg = NULL;
        }
        return mDropped = value;
+14 −17
Original line number Diff line number Diff line
@@ -306,7 +306,7 @@ static const char *strnstr(const char *s, size_t len, const char *needle) {
                }
                --len;
            } while (*s++ != c);
        } while (memcmp(s, needle, needleLen) != 0);
        } while (fast<memcmp>(s, needle, needleLen));
        s--;
    }
    return s;
@@ -588,7 +588,7 @@ int LogKlog::log(const char *buf, size_t len) {
        const char *bt, *et, *cp;

        bt = p;
        if (!strncmp(p, "[INFO]", 6)) {
        if (!fast<strncmp>(p, "[INFO]", 6)) {
            // <PRI>[<TIME>] "[INFO]"<tag> ":" message
            bt = p + 6;
            taglen -= 6;
@@ -612,10 +612,10 @@ int LogKlog::log(const char *buf, size_t len) {
            p = cp + 1;
        } else if (taglen) {
            size = et - bt;
            if (strncmp(bt, cp, size)) {
            if ((*bt == *cp) && fast<strncmp>(bt + 1, cp + 1, size - 1)) {
                // <PRI>[<TIME>] <tag>_host '<tag>.<num>' : message
                if (!strncmp(bt + size - 5, "_host", 5)
                        && !strncmp(bt, cp, size - 5)) {
                if (!fast<strncmp>(bt + size - 5, "_host", 5)
                        && !fast<strncmp>(bt + 1, cp + 1, size - 6)) {
                    const char *b = cp;
                    cp += size - 5;
                    taglen -= size - 5;
@@ -694,10 +694,10 @@ int LogKlog::log(const char *buf, size_t len) {
            // register names like x18 but not driver names like en0
                || ((size == 3) && (isdigit(tag[1]) && isdigit(tag[2])))
            // blacklist
                || ((size == 3) && !strncmp(tag, "CPU", 3))
                || ((size == 7) && !strncasecmp(tag, "WARNING", 7))
                || ((size == 5) && !strncasecmp(tag, "ERROR", 5))
                || ((size == 4) && !strncasecmp(tag, "INFO", 4))) {
                || ((size == 3) && !fast<strncmp>(tag, "CPU", 3))
                || ((size == 7) && !fast<strncasecmp>(tag, "WARNING", 7))
                || ((size == 5) && !fast<strncasecmp>(tag, "ERROR", 5))
                || ((size == 4) && !fast<strncasecmp>(tag, "INFO", 4))) {
            p = start;
            etag = tag = "";
        }
@@ -709,7 +709,7 @@ int LogKlog::log(const char *buf, size_t len) {
    const char *mp = strnrchr(tag, ']', taglen);
    if (mp && (++mp < etag)) {
        size_t s = etag - mp;
        if (((s + s) < taglen) && !memcmp(mp, mp - 1 - s, s)) {
        if (((s + s) < taglen) && !fast<memcmp>(mp, mp - 1 - s, s)) {
            taglen = mp - tag;
        }
    }
@@ -727,6 +727,9 @@ int LogKlog::log(const char *buf, size_t len) {
        p = " ";
        b = 1;
    }
    if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
        b = LOGGER_ENTRY_MAX_PAYLOAD;
    }
    size_t n = 1 + taglen + 1 + b + 1;
    int rc = n;
    if ((taglen > n) || (b > n)) { // Can not happen ...
@@ -734,12 +737,7 @@ int LogKlog::log(const char *buf, size_t len) {
        return rc;
    }

    // Allocate a buffer to hold the interpreted log message
    char *newstr = reinterpret_cast<char *>(malloc(n));
    if (!newstr) {
        rc = -ENOMEM;
        return rc;
    }
    char newstr[n];
    char *np = newstr;

    // Convert priority into single-byte Android logger priority
@@ -759,7 +757,6 @@ int LogKlog::log(const char *buf, size_t len) {
    // Log message
    rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr,
                     (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
    free(newstr);

    // notify readers
    if (!rc) {
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ bool LogReader::onDataAvailable(SocketClient *cli) {
    }

    bool nonBlock = false;
    if (strncmp(buffer, "dumpAndClose", 12) == 0) {
    if (!fast<strncmp>(buffer, "dumpAndClose", 12)) {
        // Allow writer to get some cycles, and wait for pending notifications
        sched_yield();
        LogTimeEntry::lock();
Loading