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
This commit is contained in:
Howard Ro 2018-10-22 17:37:42 -07:00
parent fa677f573a
commit d173009c74
6 changed files with 77 additions and 7 deletions

View file

@ -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

View file

@ -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|.

View file

@ -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

View file

@ -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",
],
}

View file

@ -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;

View file

@ -15,8 +15,9 @@
*/
#include "statsd_writer.h"
#include <cutils/fs.h>
#include <cutils/sockets.h>
#include <endian.h>
#include <cutils/threads.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
@ -37,6 +38,14 @@
/* branchless on many architectures. */
#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
#ifndef htole32
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htole32(x) (x)
#else
#define htole32(x) __bswap_32(x)
#endif
#endif
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
static atomic_int dropped = 0;
@ -78,7 +87,14 @@ static int statsdOpen() {
i = atomic_load(&statsdLoggerWrite.sock);
if (i < 0) {
int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
int flags = SOCK_DGRAM;
#ifdef SOCK_CLOEXEC
flags |= SOCK_CLOEXEC;
#endif
#ifdef SOCK_NONBLOCK
flags |= SOCK_NONBLOCK;
#endif
int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, flags, 0));
if (sock < 0) {
ret = -errno;
} else {