Merge changes I978ac321,I5675a80c,I56ef3cc4,I1cdaf002
* changes: Add --supportQ flag to java StatsLog codegen Support new socket schema in native codegen Move statsd metadata from statslog to atoms_info Fix StatsEvent memory usage for pulled events
This commit is contained in:
@@ -138,11 +138,69 @@ cc_defaults {
|
||||
"android.hardware.power@1.1",
|
||||
"android.hardware.power.stats@1.0",
|
||||
"libpackagelistparser",
|
||||
"libstatsmetadata",
|
||||
"libsysutils",
|
||||
"libcutils",
|
||||
],
|
||||
}
|
||||
|
||||
// ================
|
||||
// libstatsmetadata
|
||||
// ================
|
||||
|
||||
genrule {
|
||||
name: "atoms_info.h",
|
||||
tools: ["stats-log-api-gen"],
|
||||
cmd: "$(location stats-log-api-gen) --atomsInfoHeader $(genDir)/atoms_info.h",
|
||||
out: [
|
||||
"atoms_info.h",
|
||||
],
|
||||
}
|
||||
|
||||
genrule {
|
||||
name: "atoms_info.cpp",
|
||||
tools: ["stats-log-api-gen"],
|
||||
cmd: "$(location stats-log-api-gen) --atomsInfoCpp $(genDir)/atoms_info.cpp",
|
||||
out: [
|
||||
"atoms_info.cpp",
|
||||
],
|
||||
}
|
||||
|
||||
cc_library_shared {
|
||||
name: "libstatsmetadata",
|
||||
host_supported: true,
|
||||
generated_sources: [
|
||||
"atoms_info.cpp",
|
||||
],
|
||||
generated_headers: [
|
||||
"atoms_info.h",
|
||||
],
|
||||
cflags: [
|
||||
"-Wall",
|
||||
"-Werror",
|
||||
],
|
||||
export_generated_headers: [
|
||||
"atoms_info.h",
|
||||
],
|
||||
shared_libs: [
|
||||
"libcutils",
|
||||
"libstatslog",
|
||||
],
|
||||
target: {
|
||||
android: {
|
||||
shared_libs: [
|
||||
"libutils",
|
||||
],
|
||||
},
|
||||
host: {
|
||||
static_libs: [
|
||||
"libutils",
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
// =========
|
||||
// statsd
|
||||
// =========
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
#include "Log.h"
|
||||
#include "FieldValue.h"
|
||||
#include "HashableDimensionKey.h"
|
||||
#include "atoms_info.h"
|
||||
#include "math.h"
|
||||
#include "statslog.h"
|
||||
|
||||
namespace android {
|
||||
namespace os {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
|
||||
#include "StatsLogProcessor.h"
|
||||
#include "android-base/stringprintf.h"
|
||||
#include "atoms_info.h"
|
||||
#include "external/StatsPullerManager.h"
|
||||
#include "guardrail/StatsdStats.h"
|
||||
#include "metrics/CountMetricProducer.h"
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "PowerStatsPuller.h"
|
||||
#include "statslog.h"
|
||||
#include "stats_log_util.h"
|
||||
|
||||
using android::hardware::hidl_vec;
|
||||
|
||||
2
cmds/statsd/src/external/puller_util.cpp
vendored
2
cmds/statsd/src/external/puller_util.cpp
vendored
@@ -18,8 +18,8 @@
|
||||
#include "Log.h"
|
||||
|
||||
#include "StatsPullerManager.h"
|
||||
#include "atoms_info.h"
|
||||
#include "puller_util.h"
|
||||
#include "statslog.h"
|
||||
|
||||
namespace android {
|
||||
namespace os {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "config/ConfigKey.h"
|
||||
#include "statslog.h"
|
||||
#include "atoms_info.h"
|
||||
|
||||
#include <gtest/gtest_prod.h>
|
||||
#include <log/log_time.h>
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "statslog.h"
|
||||
|
||||
#include "CountMetricProducer.h"
|
||||
#include "atoms_info.h"
|
||||
#include "condition/CombinationConditionTracker.h"
|
||||
#include "condition/SimpleConditionTracker.h"
|
||||
#include "guardrail/StatsdStats.h"
|
||||
|
||||
@@ -32,8 +32,8 @@
|
||||
#include "../metrics/GaugeMetricProducer.h"
|
||||
#include "../metrics/ValueMetricProducer.h"
|
||||
|
||||
#include "atoms_info.h"
|
||||
#include "stats_util.h"
|
||||
#include "statslog.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -19,9 +19,9 @@
|
||||
#include <android/util/ProtoOutputStream.h>
|
||||
#include "FieldValue.h"
|
||||
#include "HashableDimensionKey.h"
|
||||
#include "atoms_info.h"
|
||||
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
|
||||
#include "guardrail/StatsdStats.h"
|
||||
#include "statslog.h"
|
||||
|
||||
namespace android {
|
||||
namespace os {
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <log/log.h>
|
||||
|
||||
#include "src/external/GpuStatsPuller.h"
|
||||
#include "statslog.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include "statslog.h"
|
||||
#include "../metrics/metrics_test_helper.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
@@ -31,14 +31,23 @@ import com.android.internal.annotations.VisibleForTesting;
|
||||
*
|
||||
* <p>Usage:</p>
|
||||
* <pre>
|
||||
* // Pushed event
|
||||
* StatsEvent statsEvent = StatsEvent.newBuilder()
|
||||
* .setAtomId(atomId)
|
||||
* .writeBoolean(false)
|
||||
* .writeString("annotated String field")
|
||||
* .addBooleanAnnotation(annotationId, true)
|
||||
* .usePooledBuffer()
|
||||
* .build();
|
||||
* StatsLog.write(statsEvent);
|
||||
*
|
||||
* // Pulled event
|
||||
* StatsEvent statsEvent = StatsEvent.newBuilder()
|
||||
* .setAtomId(atomId)
|
||||
* .writeBoolean(false)
|
||||
* .writeString("annotated String field")
|
||||
* .addBooleanAnnotation(annotationId, true)
|
||||
* .build();
|
||||
*
|
||||
* StatsLog.write(statsEvent);
|
||||
* </pre>
|
||||
* @hide
|
||||
**/
|
||||
@@ -210,12 +219,15 @@ public final class StatsEvent {
|
||||
private static final int MAX_PAYLOAD_SIZE = LOGGER_ENTRY_MAX_PAYLOAD - 4;
|
||||
|
||||
private final int mAtomId;
|
||||
private final Buffer mBuffer;
|
||||
private final byte[] mPayload;
|
||||
private Buffer mBuffer;
|
||||
private final int mNumBytes;
|
||||
|
||||
private StatsEvent(final int atomId, @NonNull final Buffer buffer, final int numBytes) {
|
||||
private StatsEvent(final int atomId, @Nullable final Buffer buffer,
|
||||
@NonNull final byte[] payload, final int numBytes) {
|
||||
mAtomId = atomId;
|
||||
mBuffer = buffer;
|
||||
mPayload = payload;
|
||||
mNumBytes = numBytes;
|
||||
}
|
||||
|
||||
@@ -243,7 +255,7 @@ public final class StatsEvent {
|
||||
**/
|
||||
@NonNull
|
||||
public byte[] getBytes() {
|
||||
return mBuffer.getBytes();
|
||||
return mPayload;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,10 +268,14 @@ public final class StatsEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Recycle this StatsEvent object.
|
||||
* Recycle resources used by this StatsEvent object.
|
||||
* No actions should be taken on this StatsEvent after release() is called.
|
||||
**/
|
||||
public void release() {
|
||||
mBuffer.release();
|
||||
if (mBuffer != null) {
|
||||
mBuffer.release();
|
||||
mBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -280,7 +296,18 @@ public final class StatsEvent {
|
||||
* optional string field3 = 3 [(annotation1) = true];
|
||||
* }
|
||||
*
|
||||
* // StatsEvent construction.
|
||||
* // StatsEvent construction for pushed event.
|
||||
* StatsEvent.newBuilder()
|
||||
* StatsEvent statsEvent = StatsEvent.newBuilder()
|
||||
* .setAtomId(atomId)
|
||||
* .writeInt(3) // field1
|
||||
* .writeLong(8L) // field2
|
||||
* .writeString("foo") // field 3
|
||||
* .addBooleanAnnotation(annotation1Id, true)
|
||||
* .usePooledBuffer()
|
||||
* .build();
|
||||
*
|
||||
* // StatsEvent construction for pulled event.
|
||||
* StatsEvent.newBuilder()
|
||||
* StatsEvent statsEvent = StatsEvent.newBuilder()
|
||||
* .setAtomId(atomId)
|
||||
@@ -306,6 +333,7 @@ public final class StatsEvent {
|
||||
private byte mLastType;
|
||||
private int mNumElements;
|
||||
private int mErrorMask;
|
||||
private boolean mUsePooledBuffer = false;
|
||||
|
||||
private Builder(final Buffer buffer) {
|
||||
mBuffer = buffer;
|
||||
@@ -568,6 +596,17 @@ public final class StatsEvent {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates to reuse Buffer's byte array as the underlying payload in StatsEvent.
|
||||
* This should be called for pushed events to reduce memory allocations and garbage
|
||||
* collections.
|
||||
**/
|
||||
@NonNull
|
||||
public Builder usePooledBuffer() {
|
||||
mUsePooledBuffer = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a StatsEvent object with values entered in this Builder.
|
||||
**/
|
||||
@@ -599,7 +638,18 @@ public final class StatsEvent {
|
||||
size = mPos;
|
||||
}
|
||||
|
||||
return new StatsEvent(mAtomId, mBuffer, size);
|
||||
if (mUsePooledBuffer) {
|
||||
return new StatsEvent(mAtomId, mBuffer, mBuffer.getBytes(), size);
|
||||
} else {
|
||||
// Create a copy of the buffer with the required number of bytes.
|
||||
final byte[] payload = new byte[size];
|
||||
System.arraycopy(mBuffer.getBytes(), 0, payload, 0, size);
|
||||
|
||||
// Return Buffer instance to the pool.
|
||||
mBuffer.release();
|
||||
|
||||
return new StatsEvent(mAtomId, null, payload, size);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeTypeId(final byte typeId) {
|
||||
|
||||
@@ -246,12 +246,15 @@ public final class StatsLog extends StatsLogInternal {
|
||||
|
||||
/**
|
||||
* Write an event to stats log using the raw format encapsulated in StatsEvent.
|
||||
* After writing to stats log, release() is called on the StatsEvent object.
|
||||
* No further action should be taken on the StatsEvent object following this call.
|
||||
*
|
||||
* @param statsEvent The StatsEvent object containing the encoded buffer of data to write.
|
||||
* @hide
|
||||
*/
|
||||
public static void write(@NonNull final StatsEvent statsEvent) {
|
||||
writeImpl(statsEvent.getBytes(), statsEvent.getNumBytes(), statsEvent.getAtomId());
|
||||
statsEvent.release();
|
||||
}
|
||||
|
||||
private static void enforceDumpCallingPermission(Context context) {
|
||||
|
||||
@@ -44,7 +44,7 @@ public class StatsEventTest {
|
||||
@Test
|
||||
public void testNoFields() {
|
||||
final long minTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
final StatsEvent statsEvent = StatsEvent.newBuilder().build();
|
||||
final StatsEvent statsEvent = StatsEvent.newBuilder().usePooledBuffer().build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
final int expectedAtomId = 0;
|
||||
@@ -99,6 +99,7 @@ public class StatsEventTest {
|
||||
.writeBoolean(field2)
|
||||
.writeInt(field3)
|
||||
.writeInt(field4)
|
||||
.usePooledBuffer()
|
||||
.build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
@@ -167,6 +168,7 @@ public class StatsEventTest {
|
||||
.writeString(field1)
|
||||
.writeFloat(field2)
|
||||
.writeByteArray(field3)
|
||||
.usePooledBuffer()
|
||||
.build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
@@ -230,6 +232,7 @@ public class StatsEventTest {
|
||||
.setAtomId(expectedAtomId)
|
||||
.writeAttributionChain(uids, tags)
|
||||
.writeLong(field2)
|
||||
.usePooledBuffer()
|
||||
.build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
@@ -299,6 +302,7 @@ public class StatsEventTest {
|
||||
final StatsEvent statsEvent = StatsEvent.newBuilder()
|
||||
.setAtomId(expectedAtomId)
|
||||
.writeKeyValuePairs(intMap, longMap, stringMap, floatMap)
|
||||
.usePooledBuffer()
|
||||
.build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
@@ -392,6 +396,7 @@ public class StatsEventTest {
|
||||
.addBooleanAnnotation(field1AnnotationId, field1AnnotationValue)
|
||||
.writeBoolean(field2)
|
||||
.addIntAnnotation(field2AnnotationId, field2AnnotationValue)
|
||||
.usePooledBuffer()
|
||||
.build();
|
||||
final long maxTimestamp = SystemClock.elapsedRealtimeNanos();
|
||||
|
||||
|
||||
@@ -21,9 +21,12 @@ cc_binary_host {
|
||||
name: "stats-log-api-gen",
|
||||
srcs: [
|
||||
"Collation.cpp",
|
||||
"atoms_info_writer.cpp",
|
||||
"java_writer.cpp",
|
||||
"java_writer_q.cpp",
|
||||
"main.cpp",
|
||||
"native_writer.cpp",
|
||||
"native_writer_q.cpp",
|
||||
"utils.cpp",
|
||||
],
|
||||
cflags: [
|
||||
@@ -102,13 +105,19 @@ genrule {
|
||||
cc_library {
|
||||
name: "libstatslog",
|
||||
host_supported: true,
|
||||
generated_sources: ["statslog.cpp"],
|
||||
generated_headers: ["statslog.h"],
|
||||
generated_sources: [
|
||||
"statslog.cpp",
|
||||
],
|
||||
generated_headers: [
|
||||
"statslog.h"
|
||||
],
|
||||
cflags: [
|
||||
"-Wall",
|
||||
"-Werror",
|
||||
],
|
||||
export_generated_headers: ["statslog.h"],
|
||||
export_generated_headers: [
|
||||
"statslog.h"
|
||||
],
|
||||
shared_libs: [
|
||||
"liblog",
|
||||
"libcutils",
|
||||
@@ -127,3 +136,4 @@ cc_library {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -379,6 +379,7 @@ int collate_atoms(const Descriptor *descriptor, Atoms *atoms) {
|
||||
int errorCount = 0;
|
||||
const bool dbg = false;
|
||||
|
||||
int maxPushedAtomId = 2;
|
||||
for (int i = 0; i < descriptor->field_count(); i++) {
|
||||
const FieldDescriptor *atomField = descriptor->field(i);
|
||||
|
||||
@@ -447,8 +448,14 @@ int collate_atoms(const Descriptor *descriptor, Atoms *atoms) {
|
||||
}
|
||||
atoms->non_chained_decls.insert(nonChainedAtomDecl);
|
||||
}
|
||||
|
||||
if (atomDecl.code < PULL_ATOM_START_ID && atomDecl.code > maxPushedAtomId) {
|
||||
maxPushedAtomId = atomDecl.code;
|
||||
}
|
||||
}
|
||||
|
||||
atoms->maxPushedAtomId = maxPushedAtomId;
|
||||
|
||||
if (dbg) {
|
||||
printf("signatures = [\n");
|
||||
for (map<vector<java_type_t>, set<string>>::const_iterator it =
|
||||
|
||||
@@ -111,6 +111,7 @@ struct Atoms {
|
||||
set<AtomDecl> decls;
|
||||
set<AtomDecl> non_chained_decls;
|
||||
map<vector<java_type_t>, set<string>> non_chained_signatures_to_modules;
|
||||
int maxPushedAtomId;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -123,4 +124,4 @@ int collate_atom(const Descriptor *atom, AtomDecl *atomDecl, vector<java_type_t>
|
||||
} // namespace android
|
||||
|
||||
|
||||
#endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H
|
||||
#endif // ANDROID_STATS_LOG_API_GEN_COLLATION_H
|
||||
|
||||
227
tools/stats_log_api_gen/atoms_info_writer.cpp
Normal file
227
tools/stats_log_api_gen/atoms_info_writer.cpp
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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 "atoms_info_writer.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
static void write_atoms_info_header_body(FILE* out, const Atoms& atoms) {
|
||||
fprintf(out, "struct StateAtomFieldOptions {\n");
|
||||
fprintf(out, " std::vector<int> primaryFields;\n");
|
||||
fprintf(out, " int exclusiveField;\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "struct AtomsInfo {\n");
|
||||
fprintf(out,
|
||||
" const static std::set<int> "
|
||||
"kTruncatingTimestampAtomBlackList;\n");
|
||||
fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
|
||||
fprintf(out,
|
||||
" const static std::set<int> kAtomsWithAttributionChain;\n");
|
||||
fprintf(out,
|
||||
" const static std::map<int, StateAtomFieldOptions> "
|
||||
"kStateAtomsFieldOptions;\n");
|
||||
fprintf(out,
|
||||
" const static std::map<int, std::vector<int>> "
|
||||
"kBytesFieldAtoms;\n");
|
||||
fprintf(out,
|
||||
" const static std::set<int> kWhitelistedAtoms;\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", atoms.maxPushedAtomId);
|
||||
|
||||
}
|
||||
|
||||
static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) {
|
||||
std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
|
||||
"audio_state_changed",
|
||||
"call_state_changed",
|
||||
"phone_signal_strength_changed",
|
||||
"mobile_bytes_transfer_by_fg_bg",
|
||||
"mobile_bytes_transfer"};
|
||||
fprintf(out,
|
||||
"const std::set<int> "
|
||||
"AtomsInfo::kTruncatingTimestampAtomBlackList = {\n");
|
||||
for (set<string>::const_iterator blacklistedAtom = kTruncatingAtomNames.begin();
|
||||
blacklistedAtom != kTruncatingAtomNames.end(); blacklistedAtom++) {
|
||||
fprintf(out, " %s,\n", make_constant_name(*blacklistedAtom).c_str());
|
||||
}
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
for (vector<AtomField>::const_iterator field = atom->fields.begin();
|
||||
field != atom->fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, " %s,\n", constant.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->whitelisted) {
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, " %s,\n", constant.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
|
||||
fprintf(out, " std::map<int, int> uidField;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->uidField == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding uid field for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
|
||||
make_constant_name(atom->name).c_str(), atom->uidField);
|
||||
}
|
||||
|
||||
fprintf(out, " return uidField;\n");
|
||||
fprintf(out, "};\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
|
||||
"getAtomUidField();\n");
|
||||
|
||||
fprintf(out,
|
||||
"static std::map<int, StateAtomFieldOptions> "
|
||||
"getStateAtomFieldOptions() {\n");
|
||||
fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
|
||||
fprintf(out, " StateAtomFieldOptions opt;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding primary and exclusive fields for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
fprintf(out, " opt.primaryFields.clear();\n");
|
||||
for (const auto& field : atom->primaryFields) {
|
||||
fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
|
||||
}
|
||||
|
||||
fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
|
||||
fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
|
||||
make_constant_name(atom->name).c_str());
|
||||
}
|
||||
|
||||
fprintf(out, " return options;\n");
|
||||
fprintf(out, "}\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, StateAtomFieldOptions> "
|
||||
"AtomsInfo::kStateAtomsFieldOptions = "
|
||||
"getStateAtomFieldOptions();\n");
|
||||
|
||||
fprintf(out,
|
||||
"static std::map<int, std::vector<int>> "
|
||||
"getBinaryFieldAtoms() {\n");
|
||||
fprintf(out, " std::map<int, std::vector<int>> options;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->binaryFields.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding binary fields for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
|
||||
for (const auto& field : atom->binaryFields) {
|
||||
fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
|
||||
make_constant_name(atom->name).c_str(), field);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, " return options;\n");
|
||||
fprintf(out, "}\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, std::vector<int>> "
|
||||
"AtomsInfo::kBytesFieldAtoms = "
|
||||
"getBinaryFieldAtoms();\n");
|
||||
|
||||
}
|
||||
|
||||
int write_atoms_info_header(FILE* out, const Atoms &atoms, const string& namespaceStr) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#pragma once\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#include <vector>\n");
|
||||
fprintf(out, "#include <map>\n");
|
||||
fprintf(out, "#include <set>\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_namespace(out, namespaceStr);
|
||||
|
||||
write_atoms_info_header_body(out, atoms);
|
||||
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, namespaceStr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_atoms_info_cpp(FILE *out, const Atoms &atoms, const string& namespaceStr,
|
||||
const string& importHeader, const string& statslogHeader) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#include <%s>\n", importHeader.c_str());
|
||||
fprintf(out, "#include <%s>\n", statslogHeader.c_str());
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_namespace(out, namespaceStr);
|
||||
|
||||
write_atoms_info_cpp_body(out, atoms);
|
||||
|
||||
// Print footer
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, namespaceStr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
35
tools/stats_log_api_gen/atoms_info_writer.h
Normal file
35
tools/stats_log_api_gen/atoms_info_writer.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Collation.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
using namespace std;
|
||||
|
||||
int write_atoms_info_cpp(FILE* out, const Atoms& atoms, const string& namespaceStr,
|
||||
const string& importHeader, const string& statslogHeader);
|
||||
|
||||
int write_atoms_info_header(FILE* out, const Atoms& atoms, const string& namespaceStr);
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
@@ -48,7 +48,8 @@ static int write_java_methods(
|
||||
FILE* out,
|
||||
const map<vector<java_type_t>, set<string>>& signatures_to_modules,
|
||||
const AtomDecl &attributionDecl,
|
||||
const string& moduleName
|
||||
const string& moduleName,
|
||||
const bool supportQ
|
||||
) {
|
||||
for (auto signature_to_modules_it = signatures_to_modules.begin();
|
||||
signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
@@ -82,8 +83,10 @@ static int write_java_methods(
|
||||
|
||||
// Print method body.
|
||||
string indent("");
|
||||
if (DEFAULT_MODULE_NAME != moduleName) {
|
||||
fprintf(out, " if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {\n");
|
||||
if (supportQ) {
|
||||
// TODO(b/146235828): Use just SDK_INT check once it is incremented from Q.
|
||||
fprintf(out, " if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q ||\n");
|
||||
fprintf(out, " Build.VERSION.CODENAME.equals(\"R\")) {\n");
|
||||
indent = " ";
|
||||
}
|
||||
|
||||
@@ -189,10 +192,11 @@ static int write_java_methods(
|
||||
}
|
||||
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "%s builder.usePooledBuffer();\n", indent.c_str());
|
||||
fprintf(out, "%s StatsLog.write(builder.build());\n", indent.c_str());
|
||||
|
||||
// Add support for writing using Q schema if this is not the default module.
|
||||
if (DEFAULT_MODULE_NAME != moduleName) {
|
||||
if (supportQ) {
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " QLogger.write(code");
|
||||
argIndex = 1;
|
||||
@@ -224,15 +228,17 @@ static int write_java_methods(
|
||||
|
||||
int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& javaClass,
|
||||
const string& javaPackage) {
|
||||
const string& javaPackage, const bool supportQ) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "package %s;\n", javaPackage.c_str());
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "import android.os.Build;\n");
|
||||
fprintf(out, "import android.os.SystemClock;\n");
|
||||
if (supportQ) {
|
||||
fprintf(out, "import android.os.Build;\n");
|
||||
fprintf(out, "import android.os.SystemClock;\n");
|
||||
}
|
||||
|
||||
if (DEFAULT_MODULE_NAME == moduleName) {
|
||||
// Mainline modules don't use WorkSource logging.
|
||||
@@ -270,12 +276,15 @@ int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attribut
|
||||
|
||||
// Print write methods.
|
||||
fprintf(out, " // Write methods\n");
|
||||
errors += write_java_methods(out, atoms.signatures_to_modules, attributionDecl, moduleName);
|
||||
errors += write_java_methods(
|
||||
out, atoms.signatures_to_modules, attributionDecl, moduleName, supportQ);
|
||||
errors += write_java_non_chained_methods(
|
||||
out, atoms.non_chained_signatures_to_modules, moduleName);
|
||||
if (DEFAULT_MODULE_NAME == moduleName) {
|
||||
errors += write_java_work_source_methods(out, atoms.signatures_to_modules, moduleName);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (supportQ) {
|
||||
errors += write_java_q_logger_class(
|
||||
out, atoms.signatures_to_modules, attributionDecl, moduleName);
|
||||
}
|
||||
|
||||
@@ -32,8 +32,7 @@ using namespace std;
|
||||
|
||||
int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& javaClass,
|
||||
const string& javaPackage
|
||||
);
|
||||
const string& javaPackage, const bool supportQ);
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
|
||||
@@ -37,22 +37,20 @@ int write_java_methods_q_schema(
|
||||
const map<vector<java_type_t>, set<string>>& signatures_to_modules,
|
||||
const AtomDecl &attributionDecl,
|
||||
const string& moduleName,
|
||||
const string& indent
|
||||
);
|
||||
const string& indent);
|
||||
|
||||
void write_java_helpers_for_q_schema_methods(
|
||||
FILE * out,
|
||||
const AtomDecl &attributionDecl,
|
||||
const int requiredHelpers,
|
||||
const string& indent
|
||||
);
|
||||
const string& indent);
|
||||
|
||||
#if defined(STATS_SCHEMA_LEGACY)
|
||||
int write_stats_log_java_q(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl);
|
||||
|
||||
int write_stats_log_java_q_for_module(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& javaClass,
|
||||
const string& javaPackage);
|
||||
int write_stats_log_java_q_for_module(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl &attributionDecl, const string& moduleName, const string& javaClass,
|
||||
const string& javaPackage);
|
||||
#endif
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
|
||||
|
||||
#include "Collation.h"
|
||||
#include "atoms_info_writer.h"
|
||||
#if !defined(STATS_SCHEMA_LEGACY)
|
||||
#include "java_writer.h"
|
||||
#endif
|
||||
#include "java_writer_q.h"
|
||||
#include "native_writer.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
|
||||
@@ -18,8 +20,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "android-base/strings.h"
|
||||
|
||||
using namespace google::protobuf;
|
||||
using namespace std;
|
||||
|
||||
@@ -28,750 +28,6 @@ namespace stats_log_api_gen {
|
||||
|
||||
using android::os::statsd::Atom;
|
||||
|
||||
static void write_atoms_info_cpp(FILE *out, const Atoms &atoms) {
|
||||
std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
|
||||
"audio_state_changed",
|
||||
"call_state_changed",
|
||||
"phone_signal_strength_changed",
|
||||
"mobile_bytes_transfer_by_fg_bg",
|
||||
"mobile_bytes_transfer"};
|
||||
fprintf(out,
|
||||
"const std::set<int> "
|
||||
"AtomsInfo::kTruncatingTimestampAtomBlackList = {\n");
|
||||
for (set<string>::const_iterator blacklistedAtom = kTruncatingAtomNames.begin();
|
||||
blacklistedAtom != kTruncatingAtomNames.end(); blacklistedAtom++) {
|
||||
fprintf(out, " %s,\n", make_constant_name(*blacklistedAtom).c_str());
|
||||
}
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
for (vector<AtomField>::const_iterator field = atom->fields.begin();
|
||||
field != atom->fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, " %s,\n", constant.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->whitelisted) {
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, " %s,\n", constant.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "static std::map<int, int> getAtomUidField() {\n");
|
||||
fprintf(out, " std::map<int, int> uidField;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->uidField == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding uid field for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
|
||||
make_constant_name(atom->name).c_str(), atom->uidField);
|
||||
}
|
||||
|
||||
fprintf(out, " return uidField;\n");
|
||||
fprintf(out, "};\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
|
||||
"getAtomUidField();\n");
|
||||
|
||||
fprintf(out,
|
||||
"static std::map<int, StateAtomFieldOptions> "
|
||||
"getStateAtomFieldOptions() {\n");
|
||||
fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
|
||||
fprintf(out, " StateAtomFieldOptions opt;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding primary and exclusive fields for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
fprintf(out, " opt.primaryFields.clear();\n");
|
||||
for (const auto& field : atom->primaryFields) {
|
||||
fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
|
||||
}
|
||||
|
||||
fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
|
||||
fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
|
||||
make_constant_name(atom->name).c_str());
|
||||
}
|
||||
|
||||
fprintf(out, " return options;\n");
|
||||
fprintf(out, "}\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, StateAtomFieldOptions> "
|
||||
"AtomsInfo::kStateAtomsFieldOptions = "
|
||||
"getStateAtomFieldOptions();\n");
|
||||
|
||||
fprintf(out,
|
||||
"static std::map<int, std::vector<int>> "
|
||||
"getBinaryFieldAtoms() {\n");
|
||||
fprintf(out, " std::map<int, std::vector<int>> options;\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
if (atom->binaryFields.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
fprintf(out,
|
||||
"\n // Adding binary fields for atom "
|
||||
"(%d)%s\n",
|
||||
atom->code, atom->name.c_str());
|
||||
|
||||
for (const auto& field : atom->binaryFields) {
|
||||
fprintf(out, " options[static_cast<int>(%s)].push_back(%d);\n",
|
||||
make_constant_name(atom->name).c_str(), field);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, " return options;\n");
|
||||
fprintf(out, "}\n");
|
||||
|
||||
fprintf(out,
|
||||
"const std::map<int, std::vector<int>> "
|
||||
"AtomsInfo::kBytesFieldAtoms = "
|
||||
"getBinaryFieldAtoms();\n");
|
||||
}
|
||||
|
||||
// Writes namespaces for the cpp and header files, returning the number of namespaces written.
|
||||
void write_namespace(FILE* out, const string& cppNamespaces) {
|
||||
vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
|
||||
for (string cppNamespace : cppNamespaceVec) {
|
||||
fprintf(out, "namespace %s {\n", cppNamespace.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Writes namespace closing brackets for cpp and header files.
|
||||
void write_closing_namespace(FILE* out, const string& cppNamespaces) {
|
||||
vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
|
||||
for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) {
|
||||
fprintf(out, "} // namespace %s\n", it->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace,
|
||||
const string& importHeader) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "#include <mutex>\n");
|
||||
fprintf(out, "#include <chrono>\n");
|
||||
fprintf(out, "#include <thread>\n");
|
||||
fprintf(out, "#ifdef __ANDROID__\n");
|
||||
fprintf(out, "#include <cutils/properties.h>\n");
|
||||
fprintf(out, "#endif\n");
|
||||
fprintf(out, "#include <stats_event_list.h>\n");
|
||||
fprintf(out, "#include <log/log.h>\n");
|
||||
fprintf(out, "#include <%s>\n", importHeader.c_str());
|
||||
fprintf(out, "#include <utils/SystemClock.h>\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_namespace(out, cppNamespace);
|
||||
fprintf(out, "// the single event tag id for all stats logs\n");
|
||||
fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
|
||||
fprintf(out, "#ifdef __ANDROID__\n");
|
||||
fprintf(out, "const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
|
||||
fprintf(out, "#else\n");
|
||||
fprintf(out, "const static bool kStatsdEnabled = false;\n");
|
||||
fprintf(out, "#endif\n");
|
||||
|
||||
// AtomsInfo is only used by statsd internally and is not needed for other modules.
|
||||
if (moduleName == DEFAULT_MODULE_NAME) {
|
||||
write_atoms_info_cpp(out, atoms);
|
||||
}
|
||||
|
||||
fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
|
||||
fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
|
||||
fprintf(out, "static std::mutex mLogdRetryMutex;\n");
|
||||
|
||||
// Print write methods
|
||||
fprintf(out, "\n");
|
||||
for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
|
||||
signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
int argIndex;
|
||||
|
||||
fprintf(out, "int\n");
|
||||
fprintf(out, "try_stats_write(int32_t code");
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
|
||||
"const std::map<int, int64_t>& arg%d_2, "
|
||||
"const std::map<int, char const*>& arg%d_3, "
|
||||
"const std::map<int, float>& arg%d_4",
|
||||
argIndex, argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ")\n");
|
||||
|
||||
fprintf(out, "{\n");
|
||||
argIndex = 1;
|
||||
fprintf(out, " if (kStatsdEnabled) {\n");
|
||||
fprintf(out, " stats_event_list event(kStatsEventTag);\n");
|
||||
fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
|
||||
fprintf(out, " event << code;\n\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (const auto &chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (%s_length != %s.size()) {\n",
|
||||
attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
|
||||
fprintf(out, " return -EINVAL;\n");
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
}
|
||||
fprintf(out, "\n event.begin();\n");
|
||||
fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
|
||||
attributionDecl.fields.front().name.c_str());
|
||||
fprintf(out, " event.begin();\n");
|
||||
for (const auto &chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
|
||||
fprintf(out, " event << %s[i];\n", chainField.name.c_str());
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " event << \"\";\n");
|
||||
fprintf(out, " }\n");
|
||||
} else {
|
||||
fprintf(out, " event << %s[i];\n", chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " event.end();\n\n");
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, " event.begin();\n\n");
|
||||
fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " event.end();\n\n");
|
||||
} else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
|
||||
fprintf(out,
|
||||
" event.AppendCharArray(arg%d.arg, "
|
||||
"arg%d.arg_length);\n",
|
||||
argIndex, argIndex);
|
||||
} else {
|
||||
if (*arg == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (arg%d == NULL) {\n", argIndex);
|
||||
fprintf(out, " arg%d = \"\";\n", argIndex);
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
fprintf(out, " event << arg%d;\n", argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
|
||||
fprintf(out, " return event.write(LOG_ID_STATS);\n");
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " return 1;\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
|
||||
signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
int argIndex;
|
||||
|
||||
fprintf(out, "int\n");
|
||||
fprintf(out, "stats_write(int32_t code");
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out,
|
||||
", const std::map<int, int32_t>& arg%d_1, "
|
||||
"const std::map<int, int64_t>& arg%d_2, "
|
||||
"const std::map<int, char const*>& arg%d_3, "
|
||||
"const std::map<int, float>& arg%d_4",
|
||||
argIndex, argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ")\n");
|
||||
|
||||
fprintf(out, "{\n");
|
||||
fprintf(out, " int ret = 0;\n");
|
||||
|
||||
fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
|
||||
fprintf(out, " ret = try_stats_write(code");
|
||||
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", %s",
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", %s, %s_length",
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
|
||||
argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", arg%d", argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
fprintf(out, " if (ret >= 0) { break; }\n");
|
||||
|
||||
fprintf(out, " {\n");
|
||||
fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
|
||||
fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
|
||||
"kMinRetryIntervalNs) break;\n");
|
||||
fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " if (ret < 0) {\n");
|
||||
fprintf(out, " note_log_drop(ret, code);\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " return ret;\n");
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
|
||||
signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
|
||||
if (!signature_needed_for_module(signature_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_it->first;
|
||||
int argIndex;
|
||||
|
||||
fprintf(out, "int\n");
|
||||
fprintf(out, "try_stats_write_non_chained(int32_t code");
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ")\n");
|
||||
|
||||
fprintf(out, "{\n");
|
||||
argIndex = 1;
|
||||
fprintf(out, " if (kStatsdEnabled) {\n");
|
||||
fprintf(out, " stats_event_list event(kStatsEventTag);\n");
|
||||
fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
|
||||
fprintf(out, " event << code;\n\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (argIndex == 1) {
|
||||
fprintf(out, " event.begin();\n\n");
|
||||
fprintf(out, " event.begin();\n");
|
||||
}
|
||||
if (*arg == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (arg%d == NULL) {\n", argIndex);
|
||||
fprintf(out, " arg%d = \"\";\n", argIndex);
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
if (*arg == JAVA_TYPE_BYTE_ARRAY) {
|
||||
fprintf(out,
|
||||
" event.AppendCharArray(arg%d.arg, "
|
||||
"arg%d.arg_length);",
|
||||
argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, " event << arg%d;\n", argIndex);
|
||||
}
|
||||
if (argIndex == 2) {
|
||||
fprintf(out, " event.end();\n\n");
|
||||
fprintf(out, " event.end();\n\n");
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
|
||||
fprintf(out, " return event.write(LOG_ID_STATS);\n");
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " return 1;\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
|
||||
signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
|
||||
if (!signature_needed_for_module(signature_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_it->first;
|
||||
int argIndex;
|
||||
|
||||
fprintf(out, "int\n");
|
||||
fprintf(out, "stats_write_non_chained(int32_t code");
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ")\n");
|
||||
|
||||
fprintf(out, "{\n");
|
||||
|
||||
fprintf(out, " int ret = 0;\n");
|
||||
fprintf(out, " for(int retry = 0; retry < 2; ++retry) {\n");
|
||||
fprintf(out, " ret = try_stats_write_non_chained(code");
|
||||
|
||||
argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
fprintf(out, ", arg%d", argIndex);
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
fprintf(out, " if (ret >= 0) { break; }\n");
|
||||
|
||||
fprintf(out, " {\n");
|
||||
fprintf(out, " std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n");
|
||||
fprintf(out, " if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
|
||||
"kMinRetryIntervalNs) break;\n");
|
||||
fprintf(out, " lastRetryTimestampNs = android::elapsedRealtimeNano();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " std::this_thread::sleep_for(std::chrono::milliseconds(10));\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " if (ret < 0) {\n");
|
||||
fprintf(out, " note_log_drop(ret, code);\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " return ret;\n\n");
|
||||
fprintf(out, "}\n");
|
||||
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
|
||||
// Print footer
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, cppNamespace);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void write_cpp_usage(
|
||||
FILE* out, const string& method_name, const string& atom_code_name,
|
||||
const AtomDecl& atom, const AtomDecl &attributionDecl) {
|
||||
fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
|
||||
atom_code_name.c_str());
|
||||
|
||||
for (vector<AtomField>::const_iterator field = atom.fields.begin();
|
||||
field != atom.fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", const std::map<int, int32_t>& %s_int"
|
||||
", const std::map<int, int64_t>& %s_long"
|
||||
", const std::map<int, char const*>& %s_str"
|
||||
", const std::map<int, float>& %s_float",
|
||||
field->name.c_str(),
|
||||
field->name.c_str(),
|
||||
field->name.c_str(),
|
||||
field->name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
|
||||
}
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
}
|
||||
|
||||
static void write_cpp_method_header(
|
||||
FILE* out,
|
||||
const string& method_name,
|
||||
const map<vector<java_type_t>, set<string>>& signatures_to_modules,
|
||||
const AtomDecl &attributionDecl, const string& moduleName) {
|
||||
|
||||
for (auto signature_to_modules_it = signatures_to_modules.begin();
|
||||
signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
// Skip if this signature is not needed for the module.
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
fprintf(out, "int %s(int32_t code", method_name.c_str());
|
||||
int argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType), chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
|
||||
"const std::map<int, int64_t>& arg%d_2, "
|
||||
"const std::map<int, char const*>& arg%d_3, "
|
||||
"const std::map<int, float>& arg%d_4",
|
||||
argIndex, argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace)
|
||||
{
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#pragma once\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#include <stdint.h>\n");
|
||||
fprintf(out, "#include <vector>\n");
|
||||
fprintf(out, "#include <map>\n");
|
||||
fprintf(out, "#include <set>\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_namespace(out, cppNamespace);
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "/*\n");
|
||||
fprintf(out, " * API For logging statistics events.\n");
|
||||
fprintf(out, " */\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "/**\n");
|
||||
fprintf(out, " * Constants for atom codes.\n");
|
||||
fprintf(out, " */\n");
|
||||
fprintf(out, "enum {\n");
|
||||
|
||||
std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
|
||||
build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
|
||||
|
||||
size_t i = 0;
|
||||
int maxPushedAtomId = 2;
|
||||
// Print atom constants
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
// Skip if the atom is not needed for the module.
|
||||
if (!atom_needed_for_module(*atom, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, " /**\n");
|
||||
fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
|
||||
write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
|
||||
|
||||
auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
|
||||
if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
|
||||
write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
|
||||
attributionDecl);
|
||||
}
|
||||
fprintf(out, " */\n");
|
||||
char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
|
||||
fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
|
||||
if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
|
||||
maxPushedAtomId = atom->code;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
// Print constants for the enum values.
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Constants for enum values\n");
|
||||
fprintf(out, "//\n\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
// Skip if the atom is not needed for the module.
|
||||
if (!atom_needed_for_module(*atom, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (vector<AtomField>::const_iterator field = atom->fields.begin();
|
||||
field != atom->fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ENUM) {
|
||||
fprintf(out, "// Values for %s.%s\n", atom->message.c_str(),
|
||||
field->name.c_str());
|
||||
for (map<int, string>::const_iterator value = field->enumValues.begin();
|
||||
value != field->enumValues.end(); value++) {
|
||||
fprintf(out, "const int32_t %s__%s__%s = %d;\n",
|
||||
make_constant_name(atom->message).c_str(),
|
||||
make_constant_name(field->name).c_str(),
|
||||
make_constant_name(value->second).c_str(),
|
||||
value->first);
|
||||
}
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "struct BytesField {\n");
|
||||
fprintf(out,
|
||||
" BytesField(char const* array, size_t len) : arg(array), "
|
||||
"arg_length(len) {}\n");
|
||||
fprintf(out, " char const* arg;\n");
|
||||
fprintf(out, " size_t arg_length;\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
// This metadata is only used by statsd, which uses the default libstatslog.
|
||||
if (moduleName == DEFAULT_MODULE_NAME) {
|
||||
|
||||
fprintf(out, "struct StateAtomFieldOptions {\n");
|
||||
fprintf(out, " std::vector<int> primaryFields;\n");
|
||||
fprintf(out, " int exclusiveField;\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "struct AtomsInfo {\n");
|
||||
fprintf(out,
|
||||
" const static std::set<int> "
|
||||
"kTruncatingTimestampAtomBlackList;\n");
|
||||
fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
|
||||
fprintf(out,
|
||||
" const static std::set<int> kAtomsWithAttributionChain;\n");
|
||||
fprintf(out,
|
||||
" const static std::map<int, StateAtomFieldOptions> "
|
||||
"kStateAtomsFieldOptions;\n");
|
||||
fprintf(out,
|
||||
" const static std::map<int, std::vector<int>> "
|
||||
"kBytesFieldAtoms;");
|
||||
fprintf(out,
|
||||
" const static std::set<int> kWhitelistedAtoms;\n");
|
||||
fprintf(out, "};\n");
|
||||
|
||||
fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
|
||||
maxPushedAtomId);
|
||||
}
|
||||
|
||||
// Print write methods
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Write methods\n");
|
||||
fprintf(out, "//\n");
|
||||
write_cpp_method_header(out, "stats_write", atoms.signatures_to_modules, attributionDecl,
|
||||
moduleName);
|
||||
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Write flattened methods\n");
|
||||
fprintf(out, "//\n");
|
||||
write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures_to_modules,
|
||||
attributionDecl, moduleName);
|
||||
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, cppNamespace);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Hide the JNI write helpers that are not used in the new schema.
|
||||
// TODO(b/145100015): Remove this and other JNI related functionality once StatsEvent migration is
|
||||
// complete.
|
||||
@@ -1235,20 +491,28 @@ print_usage()
|
||||
fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "OPTIONS\n");
|
||||
fprintf(stderr, " --cpp FILENAME the header file to output\n");
|
||||
fprintf(stderr, " --header FILENAME the cpp file to output\n");
|
||||
fprintf(stderr, " --cpp FILENAME the header file to output for write helpers\n");
|
||||
fprintf(stderr, " --header FILENAME the cpp file to output for write helpers\n");
|
||||
fprintf(stderr,
|
||||
" --atomsInfoCpp FILENAME the header file to output for statsd metadata\n");
|
||||
fprintf(stderr, " --atomsInfoHeader FILENAME the cpp file to output for statsd metadata\n");
|
||||
fprintf(stderr, " --help this message\n");
|
||||
fprintf(stderr, " --java FILENAME the java file to output\n");
|
||||
fprintf(stderr, " --jni FILENAME the jni file to output\n");
|
||||
fprintf(stderr, " --module NAME optional, module name to generate outputs for\n");
|
||||
fprintf(stderr, " --namespace COMMA,SEP,NAMESPACE required for cpp/header with module\n");
|
||||
fprintf(stderr, " comma separated namespace of the files\n");
|
||||
fprintf(stderr, " --importHeader NAME required for cpp/jni to say which header to import\n");
|
||||
fprintf(stderr," --importHeader NAME required for cpp/jni to say which header to import "
|
||||
"for write helpers\n");
|
||||
fprintf(stderr," --atomsInfoImportHeader NAME required for cpp to say which header to import "
|
||||
"for statsd metadata\n");
|
||||
fprintf(stderr, " --javaPackage PACKAGE the package for the java file.\n");
|
||||
fprintf(stderr, " required for java with module\n");
|
||||
fprintf(stderr, " --javaClass CLASS the class name of the java class.\n");
|
||||
fprintf(stderr, " Optional for Java with module.\n");
|
||||
fprintf(stderr, " Default is \"StatsLogInternal\"\n");}
|
||||
fprintf(stderr, " Default is \"StatsLogInternal\"\n");
|
||||
fprintf(stderr, " --supportQ Include support for Android Q.\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the argument parsing and execute the tasks.
|
||||
@@ -1260,12 +524,16 @@ run(int argc, char const*const* argv)
|
||||
string headerFilename;
|
||||
string javaFilename;
|
||||
string jniFilename;
|
||||
string atomsInfoCppFilename;
|
||||
string atomsInfoHeaderFilename;
|
||||
|
||||
string moduleName = DEFAULT_MODULE_NAME;
|
||||
string cppNamespace = DEFAULT_CPP_NAMESPACE;
|
||||
string cppHeaderImport = DEFAULT_CPP_HEADER_IMPORT;
|
||||
string atomsInfoCppHeaderImport = DEFAULT_ATOMS_INFO_CPP_HEADER_IMPORT;
|
||||
string javaPackage = DEFAULT_JAVA_PACKAGE;
|
||||
string javaClass = DEFAULT_JAVA_CLASS;
|
||||
bool supportQ = false;
|
||||
|
||||
int index = 1;
|
||||
while (index < argc) {
|
||||
@@ -1335,18 +603,50 @@ run(int argc, char const*const* argv)
|
||||
return 1;
|
||||
}
|
||||
javaClass = argv[index];
|
||||
} else if (0 == strcmp("--atomsInfoHeader", argv[index])) {
|
||||
index++;
|
||||
if (index >= argc) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
atomsInfoHeaderFilename = argv[index];
|
||||
} else if (0 == strcmp("--atomsInfoCpp", argv[index])) {
|
||||
index++;
|
||||
if (index >= argc) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
atomsInfoCppFilename = argv[index];
|
||||
} else if (0 == strcmp("--atomsInfoImportHeader", argv[index])) {
|
||||
index++;
|
||||
if (index >= argc) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
atomsInfoCppHeaderImport = argv[index];
|
||||
} else if (0 == strcmp("--supportQ", argv[index])) {
|
||||
supportQ = true;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (cppFilename.size() == 0
|
||||
&& headerFilename.size() == 0
|
||||
&& javaFilename.size() == 0
|
||||
&& jniFilename.size() == 0) {
|
||||
&& jniFilename.size() == 0
|
||||
&& atomsInfoHeaderFilename.size() == 0
|
||||
&& atomsInfoCppFilename.size() == 0) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (DEFAULT_MODULE_NAME == moduleName && supportQ) {
|
||||
// Support for Q schema is not needed for default module.
|
||||
fprintf(stderr, "%s cannot support Q schema\n", moduleName.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Collate the parameters
|
||||
Atoms atoms;
|
||||
int errorCount = collate_atoms(Atom::descriptor(), &atoms);
|
||||
@@ -1359,6 +659,30 @@ run(int argc, char const*const* argv)
|
||||
collate_atom(android::os::statsd::AttributionNode::descriptor(),
|
||||
&attributionDecl, &attributionSignature);
|
||||
|
||||
// Write the atoms info .cpp file
|
||||
if (atomsInfoCppFilename.size() != 0) {
|
||||
FILE* out = fopen(atomsInfoCppFilename.c_str(), "w");
|
||||
if (out == NULL) {
|
||||
fprintf(stderr, "Unable to open file for write: %s\n", atomsInfoCppFilename.c_str());
|
||||
return 1;
|
||||
}
|
||||
errorCount = android::stats_log_api_gen::write_atoms_info_cpp(
|
||||
out, atoms, cppNamespace, atomsInfoCppHeaderImport, cppHeaderImport);
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
// Write the atoms info .h file
|
||||
if (atomsInfoHeaderFilename.size() != 0) {
|
||||
FILE* out = fopen(atomsInfoHeaderFilename.c_str(), "w");
|
||||
if (out == NULL) {
|
||||
fprintf(stderr, "Unable to open file for write: %s\n", atomsInfoHeaderFilename.c_str());
|
||||
return 1;
|
||||
}
|
||||
errorCount = android::stats_log_api_gen::write_atoms_info_header(out, atoms, cppNamespace);
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
|
||||
// Write the .cpp file
|
||||
if (cppFilename.size() != 0) {
|
||||
FILE* out = fopen(cppFilename.c_str(), "w");
|
||||
@@ -1377,7 +701,7 @@ run(int argc, char const*const* argv)
|
||||
return 1;
|
||||
}
|
||||
errorCount = android::stats_log_api_gen::write_stats_log_cpp(
|
||||
out, atoms, attributionDecl, moduleName, cppNamespace, cppHeaderImport);
|
||||
out, atoms, attributionDecl, moduleName, cppNamespace, cppHeaderImport, supportQ);
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
@@ -1425,7 +749,7 @@ run(int argc, char const*const* argv)
|
||||
javaPackage = "android.util";
|
||||
}
|
||||
errorCount = android::stats_log_api_gen::write_stats_log_java(
|
||||
out, atoms, attributionDecl, moduleName, javaClass, javaPackage);
|
||||
out, atoms, attributionDecl, moduleName, javaClass, javaPackage, supportQ);
|
||||
#endif
|
||||
|
||||
fclose(out);
|
||||
|
||||
341
tools/stats_log_api_gen/native_writer.cpp
Normal file
341
tools/stats_log_api_gen/native_writer.cpp
Normal file
@@ -0,0 +1,341 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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 "native_writer.h"
|
||||
#include "native_writer_q.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
#if !defined(STATS_SCHEMA_LEGACY)
|
||||
static void write_native_key_value_pairs_for_type(FILE* out, const int argIndex,
|
||||
const int typeIndex, const string& type, const string& valueFieldName) {
|
||||
fprintf(out, " for (const auto& it : arg%d_%d) {\n", argIndex, typeIndex);
|
||||
fprintf(out, " pairs.push_back("
|
||||
"{ .key = it.first, .valueType = %s, .%s = it.second });\n",
|
||||
type.c_str(), valueFieldName.c_str());
|
||||
fprintf(out, " }\n");
|
||||
|
||||
}
|
||||
|
||||
static int write_native_stats_write_methods(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName, const bool supportQ) {
|
||||
fprintf(out, "\n");
|
||||
for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
|
||||
signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
|
||||
write_native_method_signature(out, "int stats_write", signature,
|
||||
attributionDecl, " {");
|
||||
|
||||
int argIndex = 1;
|
||||
if (supportQ) {
|
||||
fprintf(out, " StatsEventCompat event;\n");
|
||||
fprintf(out, " event.setAtomId(code);\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
switch (*arg) {
|
||||
case JAVA_TYPE_ATTRIBUTION_CHAIN: {
|
||||
const char* uidName = attributionDecl.fields.front().name.c_str();
|
||||
const char* tagName = attributionDecl.fields.back().name.c_str();
|
||||
fprintf(out, " event.writeAttributionChain(%s, %s_length, %s);\n",
|
||||
uidName, uidName, tagName);
|
||||
break;
|
||||
}
|
||||
case JAVA_TYPE_KEY_VALUE_PAIR:
|
||||
fprintf(out, " event.writeKeyValuePairs("
|
||||
"arg%d_1, arg%d_2, arg%d_3, arg%d_4);\n",
|
||||
argIndex, argIndex, argIndex, argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_BYTE_ARRAY:
|
||||
fprintf(out, " event.writeByteArray(arg%d.arg, arg%d.arg_length);\n",
|
||||
argIndex, argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_BOOLEAN:
|
||||
fprintf(out, " event.writeBool(arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_INT: // Fall through.
|
||||
case JAVA_TYPE_ENUM:
|
||||
fprintf(out, " event.writeInt32(arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_FLOAT:
|
||||
fprintf(out, " event.writeFloat(arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_LONG:
|
||||
fprintf(out, " event.writeInt64(arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_STRING:
|
||||
fprintf(out, " event.writeString(arg%d);\n", argIndex);
|
||||
break;
|
||||
default:
|
||||
// Unsupported types: OBJECT, DOUBLE.
|
||||
fprintf(stderr, "Encountered unsupported type.");
|
||||
return 1;
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, " return event.writeToSocket();\n");
|
||||
} else {
|
||||
fprintf(out, " struct stats_event* event = stats_event_obtain();\n");
|
||||
fprintf(out, " stats_event_set_atom_id(event, code);\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
switch (*arg) {
|
||||
case JAVA_TYPE_ATTRIBUTION_CHAIN: {
|
||||
const char* uidName = attributionDecl.fields.front().name.c_str();
|
||||
const char* tagName = attributionDecl.fields.back().name.c_str();
|
||||
fprintf(out,
|
||||
" stats_event_write_attribution_chain(event, "
|
||||
"reinterpret_cast<const uint32_t*>(%s), %s.data(), "
|
||||
"static_cast<uint8_t>(%s_length));\n",
|
||||
uidName, tagName, uidName);
|
||||
break;
|
||||
}
|
||||
case JAVA_TYPE_KEY_VALUE_PAIR:
|
||||
fprintf(out, " std::vector<key_value_pair> pairs;\n");
|
||||
write_native_key_value_pairs_for_type(
|
||||
out, argIndex, 1, "INT32_TYPE", "int32Value");
|
||||
write_native_key_value_pairs_for_type(
|
||||
out, argIndex, 2, "INT64_TYPE", "int64Value");
|
||||
write_native_key_value_pairs_for_type(
|
||||
out, argIndex, 3, "STRING_TYPE", "stringValue");
|
||||
write_native_key_value_pairs_for_type(
|
||||
out, argIndex, 4, "FLOAT_TYPE", "floatValue");
|
||||
fprintf(out,
|
||||
" stats_event_write_key_value_pairs(event, pairs.data(), "
|
||||
"static_cast<uint8_t>(pairs.size()));\n");
|
||||
break;
|
||||
case JAVA_TYPE_BYTE_ARRAY:
|
||||
fprintf(out,
|
||||
" stats_event_write_byte_array(event, "
|
||||
"reinterpret_cast<const uint8_t*>(arg%d.arg), arg%d.arg_length);\n",
|
||||
argIndex, argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_BOOLEAN:
|
||||
fprintf(out, " stats_event_write_bool(event, arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_INT: // Fall through.
|
||||
case JAVA_TYPE_ENUM:
|
||||
fprintf(out, " stats_event_write_int32(event, arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_FLOAT:
|
||||
fprintf(out, " stats_event_write_float(event, arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_LONG:
|
||||
fprintf(out, " stats_event_write_int64(event, arg%d);\n", argIndex);
|
||||
break;
|
||||
case JAVA_TYPE_STRING:
|
||||
fprintf(out, " stats_event_write_string8(event, arg%d);\n", argIndex);
|
||||
break;
|
||||
default:
|
||||
// Unsupported types: OBJECT, DOUBLE.
|
||||
fprintf(stderr, "Encountered unsupported type.");
|
||||
return 1;
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, " const int ret = stats_event_write(event);\n");
|
||||
fprintf(out, " stats_event_release(event);\n");
|
||||
fprintf(out, " return ret;\n");
|
||||
}
|
||||
fprintf(out, "}\n\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void write_native_stats_write_non_chained_methods(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName) {
|
||||
fprintf(out, "\n");
|
||||
for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
|
||||
signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
|
||||
if (!signature_needed_for_module(signature_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_it->first;
|
||||
|
||||
write_native_method_signature(out, "int stats_write_non_chained", signature,
|
||||
attributionDecl, " {");
|
||||
|
||||
vector<java_type_t> newSignature;
|
||||
|
||||
// First two args form the attribution node so size goes down by 1.
|
||||
newSignature.reserve(signature.size() - 1);
|
||||
|
||||
// First arg is Attribution Chain.
|
||||
newSignature.push_back(JAVA_TYPE_ATTRIBUTION_CHAIN);
|
||||
|
||||
// Followed by the originial signature except the first 2 args.
|
||||
newSignature.insert(newSignature.end(), signature.begin() + 2, signature.end());
|
||||
|
||||
const char* uidName = attributionDecl.fields.front().name.c_str();
|
||||
const char* tagName = attributionDecl.fields.back().name.c_str();
|
||||
fprintf(out, " const int32_t* %s = &arg1;\n", uidName);
|
||||
fprintf(out, " const size_t %s_length = 1;\n", uidName);
|
||||
fprintf(out, " const std::vector<char const*> %s(1, arg2);\n", tagName);
|
||||
fprintf(out, " return ");
|
||||
write_native_method_call(out, "stats_write", newSignature, attributionDecl, 2);
|
||||
|
||||
fprintf(out, "}\n\n");
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
static void write_native_method_header(
|
||||
FILE* out,
|
||||
const string& methodName,
|
||||
const map<vector<java_type_t>, set<string>>& signatures_to_modules,
|
||||
const AtomDecl &attributionDecl, const string& moduleName) {
|
||||
|
||||
for (auto signature_to_modules_it = signatures_to_modules.begin();
|
||||
signature_to_modules_it != signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
// Skip if this signature is not needed for the module.
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
write_native_method_signature(out, methodName, signature, attributionDecl, ";");
|
||||
}
|
||||
}
|
||||
|
||||
int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace,
|
||||
const string& importHeader, const bool supportQ) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
fprintf(out, "#include <%s>\n", importHeader.c_str());
|
||||
#if defined(STATS_SCHEMA_LEGACY)
|
||||
(void)supportQ; // Workaround for unused parameter error.
|
||||
write_native_cpp_includes_q(out);
|
||||
#else
|
||||
if (supportQ) {
|
||||
fprintf(out, "#include <StatsEventCompat.h>\n");
|
||||
} else {
|
||||
fprintf(out, "#include <stats_event.h>\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
fprintf(out, "\n");
|
||||
write_namespace(out, cppNamespace);
|
||||
|
||||
#if defined(STATS_SCHEMA_LEGACY)
|
||||
write_native_stats_log_cpp_globals_q(out);
|
||||
write_native_try_stats_write_methods_q(out, atoms, attributionDecl, moduleName);
|
||||
write_native_stats_write_methods_q(out, "int stats_write", atoms, attributionDecl, moduleName,
|
||||
"try_stats_write");
|
||||
write_native_try_stats_write_non_chained_methods_q(out, atoms, attributionDecl, moduleName);
|
||||
write_native_stats_write_non_chained_methods_q(out, "int stats_write_non_chained", atoms,
|
||||
attributionDecl, moduleName, "try_stats_write_non_chained");
|
||||
#else
|
||||
write_native_stats_write_methods(out, atoms, attributionDecl, moduleName, supportQ);
|
||||
write_native_stats_write_non_chained_methods(out, atoms, attributionDecl, moduleName);
|
||||
#endif
|
||||
|
||||
// Print footer
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, cppNamespace);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace) {
|
||||
// Print prelude
|
||||
fprintf(out, "// This file is autogenerated\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#pragma once\n");
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "#include <stdint.h>\n");
|
||||
fprintf(out, "#include <vector>\n");
|
||||
fprintf(out, "#include <map>\n");
|
||||
fprintf(out, "#include <set>\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_namespace(out, cppNamespace);
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "/*\n");
|
||||
fprintf(out, " * API For logging statistics events.\n");
|
||||
fprintf(out, " */\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
write_native_atom_constants(out, atoms, attributionDecl, moduleName);
|
||||
|
||||
// Print constants for the enum values.
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Constants for enum values\n");
|
||||
fprintf(out, "//\n\n");
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
// Skip if the atom is not needed for the module.
|
||||
if (!atom_needed_for_module(*atom, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (vector<AtomField>::const_iterator field = atom->fields.begin();
|
||||
field != atom->fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ENUM) {
|
||||
fprintf(out, "// Values for %s.%s\n", atom->message.c_str(),
|
||||
field->name.c_str());
|
||||
for (map<int, string>::const_iterator value = field->enumValues.begin();
|
||||
value != field->enumValues.end(); value++) {
|
||||
fprintf(out, "const int32_t %s__%s__%s = %d;\n",
|
||||
make_constant_name(atom->message).c_str(),
|
||||
make_constant_name(field->name).c_str(),
|
||||
make_constant_name(value->second).c_str(),
|
||||
value->first);
|
||||
}
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "struct BytesField {\n");
|
||||
fprintf(out,
|
||||
" BytesField(char const* array, size_t len) : arg(array), "
|
||||
"arg_length(len) {}\n");
|
||||
fprintf(out, " char const* arg;\n");
|
||||
fprintf(out, " size_t arg_length;\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
|
||||
// Print write methods
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Write methods\n");
|
||||
fprintf(out, "//\n");
|
||||
write_native_method_header(out, "int stats_write", atoms.signatures_to_modules, attributionDecl,
|
||||
moduleName);
|
||||
|
||||
fprintf(out, "//\n");
|
||||
fprintf(out, "// Write flattened methods\n");
|
||||
fprintf(out, "//\n");
|
||||
write_native_method_header(out, "int stats_write_non_chained",
|
||||
atoms.non_chained_signatures_to_modules, attributionDecl, moduleName);
|
||||
|
||||
fprintf(out, "\n");
|
||||
write_closing_namespace(out, cppNamespace);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
37
tools/stats_log_api_gen/native_writer.h
Normal file
37
tools/stats_log_api_gen/native_writer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Collation.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
using namespace std;
|
||||
|
||||
int write_stats_log_cpp(FILE *out, const Atoms &atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace, const string& importHeader,
|
||||
const bool supportQ);
|
||||
|
||||
int write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl,
|
||||
const string& moduleName, const string& cppNamespace);
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
266
tools/stats_log_api_gen/native_writer_q.cpp
Normal file
266
tools/stats_log_api_gen/native_writer_q.cpp
Normal file
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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 "native_writer_q.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
static void write_native_stats_write_body_q(FILE* out, const vector<java_type_t>& signature,
|
||||
const AtomDecl& attributionDecl, const string& indent, const string& tryMethodName) {
|
||||
fprintf(out, "%sint ret = 0;\n", indent.c_str());
|
||||
|
||||
fprintf(out, "%sfor(int retry = 0; retry < 2; ++retry) {\n", indent.c_str());
|
||||
fprintf(out, "%s ret = ", indent.c_str());
|
||||
write_native_method_call(out, tryMethodName, signature, attributionDecl);
|
||||
fprintf(out, "%s if (ret >= 0) { break; }\n", indent.c_str());
|
||||
|
||||
fprintf(out, "%s {\n", indent.c_str());
|
||||
fprintf(out, "%s std::lock_guard<std::mutex> lock(mLogdRetryMutex);\n", indent.c_str());
|
||||
fprintf(out, "%s if ((android::elapsedRealtimeNano() - lastRetryTimestampNs) <= "
|
||||
"kMinRetryIntervalNs) break;\n", indent.c_str());
|
||||
fprintf(out, "%s lastRetryTimestampNs = android::elapsedRealtimeNano();\n",
|
||||
indent.c_str());
|
||||
fprintf(out, "%s }\n", indent.c_str());
|
||||
fprintf(out, "%s std::this_thread::sleep_for(std::chrono::milliseconds(10));\n",
|
||||
indent.c_str());
|
||||
fprintf(out, "%s}\n", indent.c_str());
|
||||
fprintf(out, "%sif (ret < 0) {\n", indent.c_str());
|
||||
fprintf(out, "%s note_log_drop(ret, code);\n", indent.c_str());
|
||||
fprintf(out, "%s}\n", indent.c_str());
|
||||
fprintf(out, "%sreturn ret;\n", indent.c_str());
|
||||
}
|
||||
|
||||
void write_native_cpp_includes_q(FILE* out) {
|
||||
fprintf(out, "#include <mutex>\n");
|
||||
fprintf(out, "#include <chrono>\n");
|
||||
fprintf(out, "#include <thread>\n");
|
||||
fprintf(out, "#ifdef __ANDROID__\n");
|
||||
fprintf(out, "#include <cutils/properties.h>\n");
|
||||
fprintf(out, "#endif\n");
|
||||
fprintf(out, "#include <stats_event_list.h>\n");
|
||||
fprintf(out, "#include <log/log.h>\n");
|
||||
fprintf(out, "#include <utils/SystemClock.h>\n");
|
||||
}
|
||||
|
||||
void write_native_stats_log_cpp_globals_q(FILE* out) {
|
||||
fprintf(out, "// the single event tag id for all stats logs\n");
|
||||
fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
|
||||
fprintf(out, "#ifdef __ANDROID__\n");
|
||||
fprintf(out,
|
||||
"const static bool kStatsdEnabled = property_get_bool(\"ro.statsd.enable\", true);\n");
|
||||
fprintf(out, "#else\n");
|
||||
fprintf(out, "const static bool kStatsdEnabled = false;\n");
|
||||
fprintf(out, "#endif\n");
|
||||
|
||||
fprintf(out, "int64_t lastRetryTimestampNs = -1;\n");
|
||||
fprintf(out, "const int64_t kMinRetryIntervalNs = NS_PER_SEC * 60 * 20; // 20 minutes\n");
|
||||
fprintf(out, "static std::mutex mLogdRetryMutex;\n");
|
||||
}
|
||||
|
||||
void write_native_try_stats_write_methods_q(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName) {
|
||||
fprintf(out, "\n");
|
||||
for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
|
||||
signature_to_modules_it != atoms.signatures_to_modules.end(); signature_to_modules_it++) {
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
|
||||
write_native_method_signature(out, "static int try_stats_write", signature,
|
||||
attributionDecl, " {");
|
||||
|
||||
int argIndex = 1;
|
||||
fprintf(out, " if (kStatsdEnabled) {\n");
|
||||
fprintf(out, " stats_event_list event(kStatsEventTag);\n");
|
||||
fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
|
||||
fprintf(out, " event << code;\n\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (const auto &chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (%s_length != %s.size()) {\n",
|
||||
attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
|
||||
fprintf(out, " return -EINVAL;\n");
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
}
|
||||
fprintf(out, "\n event.begin();\n");
|
||||
fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
|
||||
attributionDecl.fields.front().name.c_str());
|
||||
fprintf(out, " event.begin();\n");
|
||||
for (const auto &chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
|
||||
fprintf(out, " event << %s[i];\n", chainField.name.c_str());
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " event << \"\";\n");
|
||||
fprintf(out, " }\n");
|
||||
} else {
|
||||
fprintf(out, " event << %s[i];\n", chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, " event.end();\n\n");
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, " event.begin();\n\n");
|
||||
fprintf(out, " for (const auto& it : arg%d_1) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_2) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_3) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " for (const auto& it : arg%d_4) {\n", argIndex);
|
||||
fprintf(out, " event.begin();\n");
|
||||
fprintf(out, " event << it.first;\n");
|
||||
fprintf(out, " event << it.second;\n");
|
||||
fprintf(out, " event.end();\n");
|
||||
fprintf(out, " }\n");
|
||||
|
||||
fprintf(out, " event.end();\n\n");
|
||||
} else if (*arg == JAVA_TYPE_BYTE_ARRAY) {
|
||||
fprintf(out,
|
||||
" event.AppendCharArray(arg%d.arg, "
|
||||
"arg%d.arg_length);\n",
|
||||
argIndex, argIndex);
|
||||
} else {
|
||||
if (*arg == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (arg%d == NULL) {\n", argIndex);
|
||||
fprintf(out, " arg%d = \"\";\n", argIndex);
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
fprintf(out, " event << arg%d;\n", argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
|
||||
fprintf(out, " return event.write(LOG_ID_STATS);\n");
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " return 1;\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void write_native_stats_write_methods_q(FILE* out, const string& methodName, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName, const string& tryMethodName) {
|
||||
for (auto signature_to_modules_it = atoms.signatures_to_modules.begin();
|
||||
signature_to_modules_it != atoms.signatures_to_modules.end();
|
||||
signature_to_modules_it++) {
|
||||
if (!signature_needed_for_module(signature_to_modules_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_to_modules_it->first;
|
||||
|
||||
write_native_method_signature(out, methodName, signature, attributionDecl, " {");
|
||||
|
||||
write_native_stats_write_body_q(out, signature, attributionDecl, " ", tryMethodName);
|
||||
fprintf(out, "}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
void write_native_stats_write_non_chained_methods_q(FILE* out, const string& methodName,
|
||||
const Atoms& atoms, const AtomDecl& attributionDecl, const string& moduleName,
|
||||
const string& tryMethodName) {
|
||||
for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
|
||||
signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
|
||||
if (!signature_needed_for_module(signature_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_it->first;
|
||||
|
||||
write_native_method_signature(out, methodName, signature, attributionDecl, " {");
|
||||
|
||||
write_native_stats_write_body_q(out, signature, attributionDecl, " ", tryMethodName);
|
||||
fprintf(out, "}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
void write_native_try_stats_write_non_chained_methods_q(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName) {
|
||||
for (auto signature_it = atoms.non_chained_signatures_to_modules.begin();
|
||||
signature_it != atoms.non_chained_signatures_to_modules.end(); signature_it++) {
|
||||
if (!signature_needed_for_module(signature_it->second, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
vector<java_type_t> signature = signature_it->first;
|
||||
|
||||
write_native_method_signature(out, "static int try_stats_write_non_chained", signature,
|
||||
attributionDecl, " {");
|
||||
|
||||
int argIndex = 1;
|
||||
fprintf(out, " if (kStatsdEnabled) {\n");
|
||||
fprintf(out, " stats_event_list event(kStatsEventTag);\n");
|
||||
fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
|
||||
fprintf(out, " event << code;\n\n");
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (argIndex == 1) {
|
||||
fprintf(out, " event.begin();\n\n");
|
||||
fprintf(out, " event.begin();\n");
|
||||
}
|
||||
if (*arg == JAVA_TYPE_STRING) {
|
||||
fprintf(out, " if (arg%d == NULL) {\n", argIndex);
|
||||
fprintf(out, " arg%d = \"\";\n", argIndex);
|
||||
fprintf(out, " }\n");
|
||||
}
|
||||
if (*arg == JAVA_TYPE_BYTE_ARRAY) {
|
||||
fprintf(out,
|
||||
" event.AppendCharArray(arg%d.arg, "
|
||||
"arg%d.arg_length);\n",
|
||||
argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, " event << arg%d;\n", argIndex);
|
||||
}
|
||||
if (argIndex == 2) {
|
||||
fprintf(out, " event.end();\n\n");
|
||||
fprintf(out, " event.end();\n\n");
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
|
||||
fprintf(out, " return event.write(LOG_ID_STATS);\n");
|
||||
fprintf(out, " } else {\n");
|
||||
fprintf(out, " return 1;\n");
|
||||
fprintf(out, " }\n");
|
||||
fprintf(out, "}\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
47
tools/stats_log_api_gen/native_writer_q.h
Normal file
47
tools/stats_log_api_gen/native_writer_q.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2019, 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Collation.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
using namespace std;
|
||||
|
||||
void write_native_cpp_includes_q(FILE* out);
|
||||
|
||||
void write_native_stats_log_cpp_globals_q(FILE* out);
|
||||
|
||||
void write_native_try_stats_write_methods_q(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName);
|
||||
|
||||
void write_native_stats_write_methods_q(FILE* out, const string& methodName, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName, const string& tryMethodName);
|
||||
|
||||
void write_native_try_stats_write_non_chained_methods_q(FILE* out, const Atoms& atoms,
|
||||
const AtomDecl& attributionDecl, const string& moduleName);
|
||||
|
||||
void write_native_stats_write_non_chained_methods_q(FILE* out, const string& methodName,
|
||||
const Atoms& atoms, const AtomDecl& attributionDecl, const string& moduleName,
|
||||
const string& tryMethodName);
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
@@ -16,9 +16,19 @@
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include "android-base/strings.h"
|
||||
|
||||
namespace android {
|
||||
namespace stats_log_api_gen {
|
||||
|
||||
static void build_non_chained_decl_map(const Atoms& atoms,
|
||||
std::map<int, set<AtomDecl>::const_iterator>* decl_map) {
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
|
||||
atom != atoms.non_chained_decls.end(); atom++) {
|
||||
decl_map->insert(std::make_pair(atom->code, atom));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn lower and camel case into upper case with underscores.
|
||||
*/
|
||||
@@ -102,14 +112,157 @@ bool signature_needed_for_module(const set<string>& modules, const string& modul
|
||||
return modules.find(moduleName) != modules.end();
|
||||
}
|
||||
|
||||
void build_non_chained_decl_map(const Atoms& atoms,
|
||||
std::map<int, set<AtomDecl>::const_iterator>* decl_map) {
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
|
||||
atom != atoms.non_chained_decls.end(); atom++) {
|
||||
decl_map->insert(std::make_pair(atom->code, atom));
|
||||
// Native
|
||||
// Writes namespaces for the cpp and header files, returning the number of namespaces written.
|
||||
void write_namespace(FILE* out, const string& cppNamespaces) {
|
||||
vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
|
||||
for (string cppNamespace : cppNamespaceVec) {
|
||||
fprintf(out, "namespace %s {\n", cppNamespace.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Writes namespace closing brackets for cpp and header files.
|
||||
void write_closing_namespace(FILE* out, const string& cppNamespaces) {
|
||||
vector<string> cppNamespaceVec = android::base::Split(cppNamespaces, ",");
|
||||
for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) {
|
||||
fprintf(out, "} // namespace %s\n", it->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static void write_cpp_usage(
|
||||
FILE* out, const string& method_name, const string& atom_code_name,
|
||||
const AtomDecl& atom, const AtomDecl &attributionDecl) {
|
||||
fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(),
|
||||
atom_code_name.c_str());
|
||||
|
||||
for (vector<AtomField>::const_iterator field = atom.fields.begin();
|
||||
field != atom.fields.end(); field++) {
|
||||
if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (field->javaType == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", const std::map<int, int32_t>& %s_int"
|
||||
", const std::map<int, int64_t>& %s_long"
|
||||
", const std::map<int, char const*>& %s_str"
|
||||
", const std::map<int, float>& %s_float",
|
||||
field->name.c_str(),
|
||||
field->name.c_str(),
|
||||
field->name.c_str(),
|
||||
field->name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
|
||||
}
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
}
|
||||
|
||||
void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
|
||||
const string& moduleName) {
|
||||
fprintf(out, "/**\n");
|
||||
fprintf(out, " * Constants for atom codes.\n");
|
||||
fprintf(out, " */\n");
|
||||
fprintf(out, "enum {\n");
|
||||
|
||||
std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
|
||||
build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
|
||||
|
||||
size_t i = 0;
|
||||
// Print atom constants
|
||||
for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
|
||||
atom != atoms.decls.end(); atom++) {
|
||||
// Skip if the atom is not needed for the module.
|
||||
if (!atom_needed_for_module(*atom, moduleName)) {
|
||||
continue;
|
||||
}
|
||||
string constant = make_constant_name(atom->name);
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, " /**\n");
|
||||
fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
|
||||
write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
|
||||
|
||||
auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
|
||||
if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
|
||||
write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
|
||||
attributionDecl);
|
||||
}
|
||||
fprintf(out, " */\n");
|
||||
char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
|
||||
fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
|
||||
i++;
|
||||
}
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "};\n");
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
|
||||
void write_native_method_signature(FILE* out, const string& methodName,
|
||||
const vector<java_type_t>& signature, const AtomDecl& attributionDecl,
|
||||
const string& closer) {
|
||||
fprintf(out, "%s(int32_t code", methodName.c_str());
|
||||
int argIndex = 1;
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", const std::vector<%s>& %s",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", const %s* %s, size_t %s_length",
|
||||
cpp_type_name(chainField.javaType),
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", const std::map<int, int32_t>& arg%d_1, "
|
||||
"const std::map<int, int64_t>& arg%d_2, "
|
||||
"const std::map<int, char const*>& arg%d_3, "
|
||||
"const std::map<int, float>& arg%d_4",
|
||||
argIndex, argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ")%s\n", closer.c_str());
|
||||
}
|
||||
|
||||
void write_native_method_call(FILE* out, const string& methodName,
|
||||
const vector<java_type_t>& signature, const AtomDecl& attributionDecl, int argIndex) {
|
||||
fprintf(out, "%s(code", methodName.c_str());
|
||||
for (vector<java_type_t>::const_iterator arg = signature.begin();
|
||||
arg != signature.end(); arg++) {
|
||||
if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
|
||||
for (auto chainField : attributionDecl.fields) {
|
||||
if (chainField.javaType == JAVA_TYPE_STRING) {
|
||||
fprintf(out, ", %s",
|
||||
chainField.name.c_str());
|
||||
} else {
|
||||
fprintf(out, ", %s, %s_length",
|
||||
chainField.name.c_str(), chainField.name.c_str());
|
||||
}
|
||||
}
|
||||
} else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) {
|
||||
fprintf(out, ", arg%d_1, arg%d_2, arg%d_3, arg%d_4", argIndex,
|
||||
argIndex, argIndex, argIndex);
|
||||
} else {
|
||||
fprintf(out, ", arg%d", argIndex);
|
||||
}
|
||||
argIndex++;
|
||||
}
|
||||
fprintf(out, ");\n");
|
||||
}
|
||||
|
||||
// Java
|
||||
void write_java_atom_codes(FILE* out, const Atoms& atoms, const string& moduleName) {
|
||||
fprintf(out, " // Constants for atom codes.\n");
|
||||
|
||||
@@ -33,6 +33,7 @@ using namespace std;
|
||||
const string DEFAULT_MODULE_NAME = "DEFAULT";
|
||||
const string DEFAULT_CPP_NAMESPACE = "android,util";
|
||||
const string DEFAULT_CPP_HEADER_IMPORT = "statslog.h";
|
||||
const string DEFAULT_ATOMS_INFO_CPP_HEADER_IMPORT = "atoms_info.h";
|
||||
const string DEFAULT_JAVA_PACKAGE = "android.util";
|
||||
const string DEFAULT_JAVA_CLASS = "StatsLogInternal";
|
||||
|
||||
@@ -49,8 +50,20 @@ bool atom_needed_for_module(const AtomDecl& atomDecl, const string& moduleName);
|
||||
|
||||
bool signature_needed_for_module(const set<string>& modules, const string& moduleName);
|
||||
|
||||
void build_non_chained_decl_map(const Atoms& atoms,
|
||||
std::map<int, set<AtomDecl>::const_iterator>* decl_map);
|
||||
// Common Native helpers
|
||||
void write_namespace(FILE* out, const string& cppNamespaces);
|
||||
|
||||
void write_closing_namespace(FILE* out, const string& cppNamespaces);
|
||||
|
||||
void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
|
||||
const string& moduleName);
|
||||
|
||||
void write_native_method_signature(FILE* out, const string& methodName,
|
||||
const vector<java_type_t>& signature, const AtomDecl& attributionDecl,
|
||||
const string& closer);
|
||||
|
||||
void write_native_method_call(FILE* out, const string& methodName,
|
||||
const vector<java_type_t>& signature, const AtomDecl& attributionDecl, int argIndex = 1);
|
||||
|
||||
// Common Java helpers.
|
||||
void write_java_atom_codes(FILE* out, const Atoms& atoms, const string& moduleName);
|
||||
@@ -62,14 +75,12 @@ void write_java_usage(FILE* out, const string& method_name, const string& atom_c
|
||||
|
||||
int write_java_non_chained_methods(FILE* out, const map<vector<java_type_t>,
|
||||
set<string>>& signatures_to_modules,
|
||||
const string& moduleName
|
||||
);
|
||||
const string& moduleName);
|
||||
|
||||
int write_java_work_source_methods(
|
||||
FILE* out,
|
||||
const map<vector<java_type_t>, set<string>>& signatures_to_modules,
|
||||
const string& moduleName
|
||||
);
|
||||
const string& moduleName);
|
||||
|
||||
} // namespace stats_log_api_gen
|
||||
} // namespace android
|
||||
|
||||
Reference in New Issue
Block a user