Remove dead code and shared lib.
And fix a bug in ValueMetricProducer
Test: statsd_test & manual
Change-Id: Ie76bfc02e14ae6a0fa9f8933751fd06397de9411
src/metrics/MetricsManager.cpp \
src/metrics/metrics_manager_util.cpp \
src/packages/UidMap.cpp \
- src/storage/DropboxReader.cpp \
- src/storage/DropboxWriter.cpp \
src/storage/StorageManager.cpp \
src/StatsLogProcessor.cpp \
src/StatsService.cpp \
libselinux \
libutils \
libservices \
- libandroidfw \
libprotoutil \
libstatslog \
libhardware \
#include "config/ConfigManager.h"
#include "guardrail/MemoryLeakTrackUtil.h"
#include "guardrail/StatsdStats.h"
-#include "storage/DropboxReader.h"
#include "storage/StorageManager.h"
#include <android-base/file.h>
return cmd_config(in, out, err, args);
}
- // adb shell cmd stats print-stats-log
- if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
- return cmd_print_stats_log(out, args);
- }
-
if (!args[0].compare(String8("print-uid-map"))) {
return cmd_print_uid_map(out);
}
return NO_ERROR;
}
-status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
- long msec = 0;
-
- if (args.size() > 2) {
- msec = strtol(args[2].string(), NULL, 10);
- }
- return DropboxReader::readStatsLogs(out, args[1].string(), msec);
-}
-
status_t StatsService::cmd_print_uid_map(FILE* out) {
mUidMap->printUidMap(out);
return NO_ERROR;
void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
mCondition = condition;
+ if (eventTime < mCurrentBucketStartTimeNs) {
+ return;
+ }
+
if (mPullTagId != -1) {
if (mCondition == true) {
mStatsPullerManager->RegisterReceiver(mPullTagId, this,
*/
#include "stats_util.h"
-#include <log/log_event_list.h>
namespace android {
namespace os {
namespace statsd {
-static inline uint32_t get4LE(const char* src) {
- return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
-}
-
-int getTagId(log_msg msg) {
- return get4LE(msg.msg());
-}
-
-EventMetricData parse(log_msg msg) {
- // dump all statsd logs to dropbox for now.
- // TODO: Add filtering, aggregation, etc.
- EventMetricData eventMetricData;
-
- // set tag.
- int tag = getTagId(msg);
- // TODO: Replace the following line when we can serialize on the fly.
- // eventMetricData.set_tag(tag);
-
- // set timestamp of the event.
- eventMetricData.set_timestamp_nanos(msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec);
-
- // start iterating k,v pairs.
- android_log_context context =
- create_android_log_parser(const_cast<log_msg*>(&msg)->msg() + sizeof(uint32_t),
- const_cast<log_msg*>(&msg)->len() - sizeof(uint32_t));
- android_log_list_element elem;
-
- if (context) {
- memset(&elem, 0, sizeof(elem));
- size_t index = 0;
- int32_t key = -1;
-
- do {
- elem = android_log_read_next(context);
- switch ((int)elem.type) {
- case EVENT_TYPE_INT:
- if (index % 2 == 0) {
- key = elem.data.int32;
- } else {
- // TODO: Fix the following lines when we can serialize on the fly.
- /*
- int32_t val = elem.data.int32;
- KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
- keyValuePair->set_key(key);
- keyValuePair->set_value_int(val);
- */
- }
- index++;
- break;
- case EVENT_TYPE_FLOAT:
- if (index % 2 == 1) {
- // TODO: Fix the following lines when we can serialize on the fly.
- /*
- float val = elem.data.float32;
- KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
- keyValuePair->set_key(key);
- keyValuePair->set_value_float(val);
- */
- }
- index++;
- break;
- case EVENT_TYPE_STRING:
- if (index % 2 == 1) {
- // TODO: Fix the following lines when we can serialize on the fly.
- /*
- char* val = elem.data.string;
- KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
- keyValuePair->set_key(key);
- keyValuePair->set_value_str(val);
- */
- }
- index++;
- break;
- case EVENT_TYPE_LONG:
- if (index % 2 == 1) {
- // TODO: Fix the following lines when we can serialize on the fly.
- /*
- int64_t val = elem.data.int64;
- KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
- keyValuePair->set_key(key);
- keyValuePair->set_value_int(val);
- */
- }
- index++;
- break;
- case EVENT_TYPE_LIST:
- break;
- case EVENT_TYPE_LIST_STOP:
- break;
- case EVENT_TYPE_UNKNOWN:
- break;
- default:
- elem.complete = true;
- break;
- }
-
- if (elem.complete) {
- break;
- }
- } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
-
- android_log_destroy(&context);
- }
-
- return eventMetricData;
-}
-
// There is no existing hash function for the dimension key ("repeated KeyValuePair").
// Temporarily use a string concatenation as the hashable key.
// TODO: Find a better hash function for std::vector<KeyValuePair>.
#pragma once
-#include "logd/LogReader.h"
-#include "storage/DropboxWriter.h"
-
-#include <log/logprint.h>
-#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
+#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
#include <unordered_map>
namespace statsd {
#define DEFAULT_DIMENSION_KEY ""
-#define MATCHER_NOT_FOUND -2
typedef std::string HashableDimensionKey;
typedef std::unordered_map<HashableDimensionKey, int64_t> DimToValMap;
-EventMetricData parse(log_msg msg);
-
-int getTagId(log_msg msg);
-
std::string getHashableKey(std::vector<KeyValuePair> key);
} // namespace statsd
+++ /dev/null
-/*
- * 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-base/file.h>
-#include <android/os/DropBoxManager.h>
-#include <androidfw/ZipUtils.h>
-
-#include "storage/DropboxReader.h"
-
-using android::base::unique_fd;
-using android::binder::Status;
-using android::os::DropBoxManager;
-using android::sp;
-using android::String16;
-using android::ZipUtils;
-using std::vector;
-
-namespace android {
-namespace os {
-namespace statsd {
-
-status_t DropboxReader::readStatsLogs(FILE* out, const string& tag, long msec) {
- sp<DropBoxManager> dropbox = new DropBoxManager();
- StatsLogReport logReport;
-
- long timestamp = msec;
- // instead of while(true), put a hard limit 1000. Dropbox won't have more than 1000 files.
- for (int i = 0; i < 1000; i++) {
- DropBoxManager::Entry entry;
- Status status = dropbox->getNextEntry(String16(tag.c_str()), timestamp, &entry);
- if (!status.isOk()) {
- ALOGD("No more entries, or failed to read. We can't tell unfortunately.");
- return android::OK;
- }
-
- const unique_fd& fd = entry.getFd();
-
- // use this timestamp for next query.
- timestamp = entry.getTimestamp();
-
- if (entry.getFlags() & DropBoxManager::IS_GZIPPED) {
- if (!parseFromGzipFile(fd, logReport)) {
- // Failed to parse from the file. Continue to fetch the next entry.
- continue;
- }
- } else {
- if (!parseFromFile(fd, logReport)) {
- // Failed to parse from the file. Continue to fetch the next entry.
- continue;
- }
- }
-
- printLog(out, logReport);
- }
- return android::OK;
-}
-
-bool DropboxReader::parseFromGzipFile(const unique_fd& fd, StatsLogReport& logReport) {
- FILE* file = fdopen(fd, "r");
- bool result = false;
- bool scanResult;
- int method;
- long compressedLen;
- long uncompressedLen;
- unsigned long crc32;
- scanResult = ZipUtils::examineGzip(file, &method, &uncompressedLen, &compressedLen, &crc32);
- if (scanResult && method == kCompressDeflated) {
- vector<uint8_t> buf(uncompressedLen);
- if (ZipUtils::inflateToBuffer(file, &buf[0], uncompressedLen, compressedLen)) {
- if (logReport.ParseFromArray(&buf[0], uncompressedLen)) {
- result = true;
- }
- }
- } else {
- ALOGE("This isn't a valid deflated gzip file");
- }
- fclose(file);
- return result;
-}
-
-// parse a non zipped file.
-bool DropboxReader::parseFromFile(const unique_fd& fd, StatsLogReport& logReport) {
- string content;
- if (!android::base::ReadFdToString(fd, &content)) {
- ALOGE("Failed to read file");
- return false;
- }
- if (!logReport.ParseFromString(content)) {
- ALOGE("failed to parse log entry from data");
- return false;
- }
- return true;
-}
-
-void DropboxReader::printLog(FILE* out, const StatsLogReport& logReport) {
- fprintf(out, "start_time_ns=%lld, end_time_ns=%lld, ", logReport.start_report_nanos(),
- logReport.end_report_nanos());
- for (int i = 0; i < logReport.event_metrics().data_size(); i++) {
- EventMetricData eventMetricData = logReport.event_metrics().data(i);
- // TODO: Pretty-print the proto.
- // fprintf(out, "EventMetricData=%s", eventMetricData.SerializeAsString().c_str());
- }
- fprintf(out, "\n");
-}
-
-} // namespace statsd
-} // namespace os
-} // namespace android
+++ /dev/null
-/*
- * 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_READER_H
-#define DROPBOX_READER_H
-
-#include <frameworks/base/cmds/statsd/src/stats_log.pb.h>
-
-#include <stdint.h>
-#include <stdio.h>
-
-using android::base::unique_fd;
-using android::status_t;
-using std::string;
-
-namespace android {
-namespace os {
-namespace statsd {
-
-class DropboxReader {
-public:
- // msec is the start timestamp.
- static status_t readStatsLogs(FILE* out, const string& tag, long msec);
-
-private:
- static bool parseFromFile(const unique_fd& fd, StatsLogReport& logReport);
- static bool parseFromGzipFile(const unique_fd& fd, StatsLogReport& logReport);
- static void printLog(FILE* out, const StatsLogReport& logReport);
- enum {
- kCompressStored = 0, // no compression
- kCompressDeflated = 8, // standard deflate
- };
-};
-
-} // namespace statsd
-} // namespace os
-} // namespace android
-
-#endif // DROPBOX_READER_H
+++ /dev/null
-/*
- * 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 "storage/DropboxWriter.h"
-
-using android::binder::Status;
-using android::os::DropBoxManager;
-using android::sp;
-using android::String16;
-using std::vector;
-
-namespace android {
-namespace os {
-namespace statsd {
-
-DropboxWriter::DropboxWriter(const string& tag) : mTag(tag), mLogReport(), mBufferSize(0) {
-}
-
-void DropboxWriter::addEventMetricData(const EventMetricData& eventMetricData) {
- flushIfNecessary(eventMetricData);
- EventMetricData* newEntry = mLogReport.mutable_event_metrics()->add_data();
- newEntry->CopyFrom(eventMetricData);
- mBufferSize += eventMetricData.ByteSize();
-}
-
-void DropboxWriter::flushIfNecessary(const EventMetricData& eventMetricData) {
- if (eventMetricData.ByteSize() + mBufferSize > kMaxSerializedBytes) {
- flush();
- }
-}
-
-void DropboxWriter::flush() {
- // now we get an exact byte size of the output
- const int numBytes = mLogReport.ByteSize();
- vector<uint8_t> buffer(numBytes);
- sp<DropBoxManager> dropbox = new DropBoxManager();
- mLogReport.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??
- }
- mLogReport.Clear();
- mBufferSize = 0;
-}
-
-} // namespace statsd
-} // namespace os
-} // namespace android
+++ /dev/null
-/*
- * 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 <utils/RefBase.h>
-#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
-
-using std::string;
-
-namespace android {
-namespace os {
-namespace statsd {
-
-class DropboxWriter : public virtual RefBase {
-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 addEventMetricData(const EventMetricData& eventMetricData);
-
- /* 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;
-
- /* Data that was captured for a single metric over a given interval of time. */
- StatsLogReport mLogReport;
-
- /* Current *serialized* size of the logs kept in memory.
- To save computation, we will not calculate the size of the StatsLogReport 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 EventMetricData& eventMetricData);
-};
-
-} // namespace statsd
-} // namespace os
-} // namespace android
-
-#endif // DROPBOX_WRITER_H