Statsd CPU optimization.

The key change is to revamp how we parse/store/match a log event, especially how we match repeated
field and attribution nodes, and how we construct dimensions and compare them.

+ We use a integer to encode the field of a log element. And also encode the FieldMatcher into an
integer and a bit mask. The log matching becomes 2 integer operations.

+ Dimension is stored as encoded field and value pair. Checking if 2 dimensions are equal is then
  becoming checking if the underlying integers are equal. The integers are stored contiguously
  in memory, so it's much faster than previous tree structure.

Start review from FieldValue.h

Test: statsd_test + new unit tests

Bug: 72659059

Change-Id: Iec8daeacdd3f39ab297c10ab9cd7b710a9c42e86
diff --git a/cmds/statsd/src/metrics/MetricProducer.cpp b/cmds/statsd/src/metrics/MetricProducer.cpp
index 85e655b..beb9015 100644
--- a/cmds/statsd/src/metrics/MetricProducer.cpp
+++ b/cmds/statsd/src/metrics/MetricProducer.cpp
@@ -13,9 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "MetricProducer.h"
 
-#include "dimension.h"
+#define DEBUG true  // STOPSHIP if true
+#include "Log.h"
+#include "MetricProducer.h"
 
 namespace android {
 namespace os {
@@ -35,9 +36,10 @@
 
     std::unordered_set<HashableDimensionKey> dimensionKeysInCondition;
     if (mConditionSliced) {
-        for (const auto& link : mConditionLinks) {
-            getDimensionKeysForCondition(event, link, &conditionKey[link.condition()]);
+        for (const auto& link : mMetric2ConditionLinks) {
+            getDimensionForCondition(event, link, &conditionKey[link.conditionId]);
         }
+
         auto conditionState =
             mWizard->query(mConditionTrackerIndex, conditionKey, mDimensionsInCondition,
                            &dimensionKeysInCondition);
@@ -46,20 +48,19 @@
         condition = mCondition;
     }
 
-    vector<DimensionsValue> dimensionInWhatValues;
-    if (mDimensionsInWhat.has_field() && mDimensionsInWhat.child_size() > 0) {
-        getDimensionKeys(event, mDimensionsInWhat, &dimensionInWhatValues);
+    vector<HashableDimensionKey> dimensionInWhatValues;
+    if (mDimensionsInWhat.size() > 0) {
+        filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhatValues);
     }
 
     if (dimensionInWhatValues.empty() && dimensionKeysInCondition.empty()) {
         onMatchedLogEventInternalLocked(
             matcherIndex, DEFAULT_METRIC_DIMENSION_KEY, conditionKey, condition, event);
     } else if (dimensionKeysInCondition.empty()) {
-        for (const DimensionsValue& whatValue : dimensionInWhatValues) {
-            onMatchedLogEventInternalLocked(
-                matcherIndex,
-                MetricDimensionKey(HashableDimensionKey(whatValue), DEFAULT_DIMENSION_KEY),
-                conditionKey, condition, event);
+        for (const HashableDimensionKey& whatValue : dimensionInWhatValues) {
+            onMatchedLogEventInternalLocked(matcherIndex,
+                                            MetricDimensionKey(whatValue, DEFAULT_DIMENSION_KEY),
+                                            conditionKey, condition, event);
         }
     } else if (dimensionInWhatValues.empty()) {
         for (const auto& conditionDimensionKey : dimensionKeysInCondition) {
@@ -69,12 +70,11 @@
                 conditionKey, condition, event);
         }
     } else {
-        for (const DimensionsValue& whatValue : dimensionInWhatValues) {
+        for (const auto& whatValue : dimensionInWhatValues) {
             for (const auto& conditionDimensionKey : dimensionKeysInCondition) {
                 onMatchedLogEventInternalLocked(
-                    matcherIndex,
-                    MetricDimensionKey(HashableDimensionKey(whatValue), conditionDimensionKey),
-                    conditionKey, condition, event);
+                        matcherIndex, MetricDimensionKey(whatValue, conditionDimensionKey),
+                        conditionKey, condition, event);
             }
         }
     }