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

Commit ca77672a authored by Howard Ro's avatar Howard Ro Committed by android-build-merger
Browse files

Merge "Make native metrics logger write to statsd socket" am: d301f1bc am: b6f5ec7c

am: f40e791c

Change-Id: I3c2b2a32478ec41712e841ca746869f410b01242
parents ec247e10 f40e791c
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -11,7 +11,11 @@ cc_defaults {


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


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


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


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


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


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


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


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


#include <cstdlib>
#include <cstdlib>


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

using namespace android;


namespace {
namespace {


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


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

}  // namespace
}  // namespace


namespace android {
namespace android {
@@ -42,6 +52,12 @@ void LogHistogram(const std::string& event, int32_t data) {
    android_log_event_list log(kSysuiMultiActionTag);
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
    log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
        << LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS;
        << 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().
// 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);
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
    log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
        << val << LOG_ID_EVENTS;
        << 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().
// 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);
    android_log_event_list log(kSysuiMultiActionTag);
    log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
    log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
        << field << value << LOG_ID_EVENTS;
        << 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;
    logger << LOGBUILDER_CATEGORY << category;
    stats_logger << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
                 << category;
}
}


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


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


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


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


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


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


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


#include <cutils/fs.h>
#include <cutils/sockets.h>
#include <cutils/sockets.h>
#include <cutils/threads.h>
#include <endian.h>
#include <endian.h>
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>