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

Commit ab273e2e authored by Yao Chen's avatar Yao Chen
Browse files

Add a DropboxWriter in statsd.

+ The DropboxWriter keeps data in cache, and flush to files once the
  size exceeds the maximum value.

+ Different components should create their owner DropboxWriter with
  different tags, e.g., anomly detection, experiment metrics, etc.

+ Copied stats_log related protos from g3

Test: run statsd, and adb shell dumpsys dropbox
      Will add unit tests.

Change-Id: If06e9a9953be32082252b340a97124d732656b40
parent 175b19b5
Loading
Loading
Loading
Loading
+32 −5
Original line number Diff line number Diff line
@@ -14,6 +14,23 @@

LOCAL_PATH:= $(call my-dir)

# ================
# proto static lib
# ================
include $(CLEAR_VARS)

LOCAL_MODULE := statsd_proto
LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-proto-files-under, src)

LOCAL_PROTOC_FLAGS :=
LOCAL_PROTOC_OPTIMIZE_TYPE := lite

include $(BUILD_STATIC_LIBRARY)

STATSD_PROTO_INCLUDES := $(local-generated-sources-dir)/src/$(LOCAL_PATH)

# =========
# statsd
# =========
@@ -27,7 +44,12 @@ LOCAL_SRC_FILES := \
    src/StatsService.cpp \
    src/LogEntryPrinter.cpp \
    src/LogReader.cpp \
    src/main.cpp
    src/main.cpp \
    src/DropboxWriter.cpp \
    src/StatsLogProcessor.cpp \
    src/stats_log.proto \
    src/statsd_config.proto \
    src/stats_constants.proto \

LOCAL_CFLAGS += \
    -Wall \
@@ -47,7 +69,10 @@ else
endif

LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/../../core/java
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src \
	STATSD_PROTO_INCLUDES

LOCAL_STATIC_LIBRARIES := statsd_proto

LOCAL_SHARED_LIBRARIES := \
        libbase \
@@ -56,7 +81,8 @@ LOCAL_SHARED_LIBRARIES := \
        libincident \
        liblog \
        libselinux \
        libutils
        libutils \
        libservices \

LOCAL_MODULE_CLASS := EXECUTABLES

@@ -82,7 +108,8 @@ LOCAL_CFLAGS += \
    -Wno-unused-function \
    -Wno-unused-parameter

LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src \
	STATSD_PROTO_INCLUDES

LOCAL_SRC_FILES := \
    ../../core/java/android/os/IStatsManager.aidl \
@@ -93,6 +120,7 @@ LOCAL_SRC_FILES := \

LOCAL_STATIC_LIBRARIES := \
    libgmock \
    statsd_proto

LOCAL_SHARED_LIBRARIES := \
    libbase \
@@ -103,4 +131,3 @@ LOCAL_SHARED_LIBRARIES := \
    libutils

include $(BUILD_NATIVE_TEST)
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */

#include <android/os/DropBoxManager.h>
#include <cutils/log.h>

#include "DropboxWriter.h"

using android::os::DropBoxManager;
using android::binder::Status;
using android::sp;
using android::String16;
using std::vector;

DropboxWriter::DropboxWriter(const string& tag)
    : mTag(tag), mLogList(), mBufferSize(0) {
}

void DropboxWriter::addEntry(const StatsLogEntry& entry) {
    flushIfNecessary(entry);
    StatsLogEntry* newEntry = mLogList.add_stats_log_entry();
    newEntry->CopyFrom(entry);
    mBufferSize += entry.ByteSize();
}

void DropboxWriter::flushIfNecessary(const StatsLogEntry& entry) {
    // The serialized size of the StatsLogList is approximately the sum of the serialized size of
    // every StatsLogEntry inside it.
    if (entry.ByteSize() + mBufferSize > kMaxSerializedBytes) {
        flush();
    }
}

void DropboxWriter::flush() {
    // now we get an exact byte size of the output
    const int numBytes = mLogList.ByteSize();
    vector<uint8_t> buffer(numBytes);
    sp<DropBoxManager> dropbox = new DropBoxManager();
    mLogList.SerializeToArray(&buffer[0], numBytes);
    Status status = dropbox->addData(String16(mTag.c_str()), &buffer[0],
            numBytes, 0 /* no flag */);
    if (!status.isOk()) {
        ALOGE("failed to write to dropbox");
        //TODO: What to do if flush fails??
    }
    mLogList.Clear();
    mBufferSize = 0;
}
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */

#ifndef DROPBOX_WRITER_H
#define DROPBOX_WRITER_H

#include <frameworks/base/cmds/statsd/src/stats_log.pb.h>

using std::string;
using android::os::statsd::StatsLogEntry;
using android::os::statsd::StatsLogList;

class DropboxWriter {
public:
    /* tag will be part of the file name, and used as the key to build the file index inside
       DropBoxManagerService.
     */
    DropboxWriter(const string& tag);

    void addEntry(const StatsLogEntry& entry);

    /* Request a flush to dropbox. */
    void flush();

private:
    /* Max *serialized* size of the logs kept in memory before flushing to dropbox.
       Proto lite does not implement the SpaceUsed() function which gives the in memory byte size.
       So we cap memory usage by limiting the serialized size. Note that protobuf's in memory size
       is higher than its serialized size. DropboxManager will compress the file when the data is
       larger than 4KB. So the final file size is less than this number.
     */
    static const size_t kMaxSerializedBytes = 16 * 1024;

    const string mTag;

    /* StatsLogList is a wrapper for storing a list of StatsLogEntry */
    StatsLogList mLogList;

    /* Current *serialized* size of the logs kept in memory.
       To save computation, we will not calculate the size of the StatsLogList every time when a new
       entry is added, which would recursively call ByteSize() on every log entry. Instead, we keep
       the sum of all individual stats log entry sizes. The size of a proto is approximately the sum
       of the size of all member protos.
     */
    size_t mBufferSize = 0;

    /* Check if the buffer size exceeds the max buffer size when the new entry is added, and flush
       the logs to dropbox if true. */
    void flushIfNecessary(const StatsLogEntry& entry);

};

#endif //DROPBOX_WRITER_H
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */

#include <StatsLogProcessor.h>

#include <log/event_tag_map.h>
#include <log/logprint.h>
#include <utils/Errors.h>

#include <frameworks/base/cmds/statsd/src/stats_log.pb.h>

using namespace android;
using android::os::statsd::StatsLogEntry;

StatsLogProcessor::StatsLogProcessor() : m_dropbox_writer("all-logs")
{
    // Initialize the EventTagMap, which is how we know the names of the numeric event tags.
    // If this fails, we can't print well, but something will print.
    m_tags = android_openEventTagMap(NULL);

    // Printing format
    m_format = android_log_format_new();
    android_log_setPrintFormat(m_format, FORMAT_THREADTIME);
}

StatsLogProcessor::~StatsLogProcessor()
{
    if (m_tags != NULL) {
        android_closeEventTagMap(m_tags);
    }
    android_log_format_free(m_format);
}

void
StatsLogProcessor::OnLogEvent(const log_msg& msg)
{
    status_t err;
    AndroidLogEntry entry;
    char buf[1024];

    err = android_log_processBinaryLogBuffer(&(const_cast<log_msg*>(&msg)->entry_v1),
                &entry, m_tags, buf, sizeof(buf));

    // dump all statsd logs to dropbox for now.
    // TODO: Add filtering, aggregation, etc.
    if (err == NO_ERROR) {
        StatsLogEntry logEntry;
        logEntry.set_uid(entry.uid);
        logEntry.set_pid(entry.pid);
        logEntry.set_start_report_millis(entry.tv_sec / 1000 + entry.tv_nsec / 1000 / 1000);
        logEntry.add_pairs()->set_value_str(entry.message, entry.messageLen);
        m_dropbox_writer.addEntry(logEntry);
    }
}
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */
#ifndef STATS_LOG_PROCESSOR_H
#define STATS_LOG_PROCESSOR_H

#include "LogReader.h"
#include "DropboxWriter.h"

#include <log/logprint.h>
#include <stdio.h>

class StatsLogProcessor : public LogListener
{
public:
    StatsLogProcessor();
    virtual ~StatsLogProcessor();

    virtual void OnLogEvent(const log_msg& msg);

private:
    /**
     * Numeric to string tag name mapping.
     */
    EventTagMap* m_tags;

    /**
     * Pretty printing format.
     */
    AndroidLogFormat* m_format;

    DropboxWriter m_dropbox_writer;
};
#endif //STATS_LOG_PROCESSOR_H
Loading