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

Commit d173009c authored by Howard Ro's avatar Howard Ro
Browse files

Make native metrics logger write to statsd socket

Previous submission of this change, aosp/790068, caused a failure in
sdk_mac for CLOCK_REALTIME and <endian.h> not being available.

Bug: 110537511
Test: compiles without failures and verified the correct metric
Change-Id: Iba1dc920ad82e88a4bcdd2feaee9a06202a440c2
parent fa677f57
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -11,7 +11,11 @@ cc_defaults {

    export_include_dirs: ["include"],
    local_include_dirs: ["include"],
    shared_libs: ["liblog"],
    shared_libs: [
        "libbase",
        "liblog",
        "libstatssocket",
    ],
    whole_static_libs: ["libgtest_prod"],

    cflags: [
@@ -23,17 +27,20 @@ cc_defaults {

// metricslogger shared library
// -----------------------------------------------------------------------------
cc_library_shared {
cc_library {
    name: "libmetricslogger",
    srcs: metricslogger_lib_src_files,
    defaults: ["metricslogger_defaults"],
    export_shared_lib_headers: ["libstatssocket"],
}

// static version of libmetricslogger, needed by a few art static binaries
// TODO(b/117829226): Remove once dependencies are cleaned up.
cc_library_static {
    name: "libmetricslogger_static",
    srcs: metricslogger_lib_src_files,
    defaults: ["metricslogger_defaults"],
    export_shared_lib_headers: ["libstatssocket"],
}

// metricslogger shared library, debug
+2 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */

#include <log/log_event_list.h>
#include <stats_event_list.h>
#include <cstdint>
#include <string>

@@ -43,6 +44,7 @@ void LogMultiAction(int32_t category, int32_t field, const std::string& value);
class ComplexEventLogger {
  private:
    android_log_event_list logger;
    stats_event_list stats_logger;

  public:
    // Create a complex event with category|category|.
+37 −2
Original line number Diff line number Diff line
@@ -18,11 +18,15 @@

#include <cstdlib>

#include <android-base/chrono_utils.h>
#include <log/event_tag_map.h>
#include <log/log_event_list.h>

using namespace android;

namespace {

const static int kStatsEventTag = 1937006964;
const static int kKeyValuePairAtomId = 83;
#ifdef __ANDROID__
EventTagMap* kEventTagMap = android_openEventTagMap(nullptr);
const int kSysuiMultiActionTag = android_lookupEventTagNum(
@@ -32,6 +36,12 @@ const int kSysuiMultiActionTag = android_lookupEventTagNum(
const int kSysuiMultiActionTag = 0;
#endif

int64_t getElapsedTimeNanoSinceBoot() {
    return std::chrono::duration_cast<std::chrono::nanoseconds>(
                   android::base::boot_clock::now().time_since_epoch())
            .count();
}

}  // namespace

namespace android {
@@ -42,6 +52,12 @@ void LogHistogram(const std::string& event, int32_t data) {
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
        << LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS;

    stats_event_list stats_log(kStatsEventTag);
    stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
              << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event << LOGBUILDER_BUCKET << data
              << LOGBUILDER_VALUE << 1;
    stats_log.write(LOG_ID_STATS);
}

// Mirror com.android.internal.logging.MetricsLogger#count().
@@ -49,6 +65,11 @@ void LogCounter(const std::string& name, int32_t val) {
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
        << val << LOG_ID_EVENTS;

    stats_event_list stats_log(kStatsEventTag);
    stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
              << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE << val;
    stats_log.write(LOG_ID_STATS);
}

// Mirror com.android.internal.logging.MetricsLogger#action().
@@ -56,34 +77,48 @@ void LogMultiAction(int32_t category, int32_t field, const std::string& value) {
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
        << field << value << LOG_ID_EVENTS;

    stats_event_list stats_log(kStatsEventTag);
    stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
              << category << LOGBUILDER_TYPE << TYPE_ACTION << field << value;
    stats_log.write(LOG_ID_STATS);
}

ComplexEventLogger::ComplexEventLogger(int category) : logger(kSysuiMultiActionTag) {
ComplexEventLogger::ComplexEventLogger(int category)
    : logger(kSysuiMultiActionTag), stats_logger(kStatsEventTag) {
    logger << LOGBUILDER_CATEGORY << category;
    stats_logger << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
                 << category;
}

void ComplexEventLogger::SetPackageName(const std::string& package_name) {
    logger << LOGBUILDER_PACKAGENAME << package_name;
    stats_logger << LOGBUILDER_PACKAGENAME << package_name;
}

void ComplexEventLogger::AddTaggedData(int tag, int32_t value) {
    logger << tag << value;
    stats_logger << tag << value;
}

void ComplexEventLogger::AddTaggedData(int tag, const std::string& value) {
    logger << tag << value;
    stats_logger << tag << value;
}

void ComplexEventLogger::AddTaggedData(int tag, int64_t value) {
    logger << tag << value;
    stats_logger << tag << value;
}

void ComplexEventLogger::AddTaggedData(int tag, float value) {
    logger << tag << value;
    stats_logger << tag << value;
}

void ComplexEventLogger::Record() {
    logger << LOG_ID_EVENTS;
    stats_logger.write(LOG_ID_STATS);
}

}  // namespace metricslogger
+3 −1
Original line number Diff line number Diff line
@@ -17,12 +17,13 @@
// ==========================================================
// Native library to write stats log to statsd socket
// ==========================================================
cc_library_static {
cc_library {
    name: "libstatssocket",
    srcs: [
        "stats_event_list.c",
        "statsd_writer.c",
    ],
    host_supported: true,
    cflags: [
        "-Wall",
        "-Werror",
@@ -32,6 +33,7 @@ cc_library_static {
    ],
    export_include_dirs: ["include"],
    shared_libs: [
        "libcutils",
        "liblog",
    ],
}
+8 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "include/stats_event_list.h"

#include <string.h>
#include <sys/time.h>
#include "statsd_writer.h"

#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
@@ -156,7 +157,14 @@ static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
    }

    save_errno = errno;
#if defined(__ANDROID__)
    clock_gettime(CLOCK_REALTIME, &ts);
#else
    struct timeval tv;
    gettimeofday(&tv, NULL);
    ts.tv_sec = tv.tv_sec;
    ts.tv_nsec = tv.tv_usec * 1000;
#endif

    int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
    errno = save_errno;
Loading