summaryrefslogtreecommitdiff
path: root/statsd/src/condition/SimpleConditionTracker.cpp
blob: 66ce961f6243714b770df31364b5898443423e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define STATSD_DEBUG false  // STOPSHIP if true
#include "Log.h"

#include "SimpleConditionTracker.h"
#include "guardrail/StatsdStats.h"

namespace android {
namespace os {
namespace statsd {

using std::unordered_map;

SimpleConditionTracker::SimpleConditionTracker(
        const ConfigKey& key, const int64_t& id, const uint64_t protoHash, const int index,
        const SimplePredicate& simplePredicate,
        const unordered_map<int64_t, int>& atomMatchingTrackerMap)
    : ConditionTracker(id, index, protoHash),
      mConfigKey(key),
      mContainANYPositionInInternalDimensions(false) {
    VLOG("creating SimpleConditionTracker %lld", (long long)mConditionId);
    mCountNesting = simplePredicate.count_nesting();

    setMatcherIndices(simplePredicate, atomMatchingTrackerMap);

    if (simplePredicate.has_dimensions()) {
        translateFieldMatcher(simplePredicate.dimensions(), &mOutputDimensions);
        if (mOutputDimensions.size() > 0) {
            mSliced = true;
        }
        mContainANYPositionInInternalDimensions = HasPositionANY(simplePredicate.dimensions());
    }
    // If an initial value isn't specified, default to false if sliced and unknown if not sliced.
    mInitialValue = simplePredicate.has_initial_value()
                            ? convertInitialValue(simplePredicate.initial_value())
                            : mSliced ? ConditionState::kFalse : ConditionState::kUnknown;
    mInitialized = true;
}

SimpleConditionTracker::~SimpleConditionTracker() {
    VLOG("~SimpleConditionTracker()");
}

bool SimpleConditionTracker::init(const vector<Predicate>& allConditionConfig,
                                  const vector<sp<ConditionTracker>>& allConditionTrackers,
                                  const unordered_map<int64_t, int>& conditionIdIndexMap,
                                  vector<bool>& stack, vector<ConditionState>& conditionCache) {
    // SimpleConditionTracker does not have dependency on other conditions, thus we just return
    // if the initialization was successful.
    ConditionKey conditionKey;
    if (mSliced) {
        conditionKey[mConditionId] = DEFAULT_DIMENSION_KEY;
    }
    isConditionMet(conditionKey, allConditionTrackers, mSliced, conditionCache);
    return mInitialized;
}

bool SimpleConditionTracker::onConfigUpdated(
        const vector<Predicate>& allConditionProtos, const int index,
        const vector<sp<ConditionTracker>>& allConditionTrackers,
        const unordered_map<int64_t, int>& atomMatchingTrackerMap,
        const unordered_map<int64_t, int>& conditionTrackerMap) {
    ConditionTracker::onConfigUpdated(allConditionProtos, index, allConditionTrackers,
                                      atomMatchingTrackerMap, conditionTrackerMap);
    setMatcherIndices(allConditionProtos[index].simple_predicate(), atomMatchingTrackerMap);
    return true;
}

void SimpleConditionTracker::setMatcherIndices(
        const SimplePredicate& simplePredicate,
        const unordered_map<int64_t, int>& atomMatchingTrackerMap) {
    mTrackerIndex.clear();
    if (simplePredicate.has_start()) {
        auto pair = atomMatchingTrackerMap.find(simplePredicate.start());
        if (pair == atomMatchingTrackerMap.end()) {
            ALOGW("Start matcher %lld not found in the config", (long long)simplePredicate.start());
            return;
        }
        mStartLogMatcherIndex = pair->second;
        mTrackerIndex.insert(mStartLogMatcherIndex);
    } else {
        mStartLogMatcherIndex = -1;
    }

    if (simplePredicate.has_stop()) {
        auto pair = atomMatchingTrackerMap.find(simplePredicate.stop());
        if (pair == atomMatchingTrackerMap.end()) {
            ALOGW("Stop matcher %lld not found in the config", (long long)simplePredicate.stop());
            return;
        }
        mStopLogMatcherIndex = pair->second;
        mTrackerIndex.insert(mStopLogMatcherIndex);
    } else {
        mStopLogMatcherIndex = -1;
    }

    if (simplePredicate.has_stop_all()) {
        auto pair = atomMatchingTrackerMap.find(simplePredicate.stop_all());
        if (pair == atomMatchingTrackerMap.end()) {
            ALOGW("Stop all matcher %lld found in the config",
                  (long long)simplePredicate.stop_all());
            return;
        }
        mStopAllLogMatcherIndex = pair->second;
        mTrackerIndex.insert(mStopAllLogMatcherIndex);
    } else {
        mStopAllLogMatcherIndex = -1;
    }
}

void SimpleConditionTracker::dumpState() {
    VLOG("%lld DUMP:", (long long)mConditionId);
    for (const auto& pair : mSlicedConditionState) {
        VLOG("\t%s : %d", pair.first.toString().c_str(), pair.second);
    }

    VLOG("Changed to true keys: \n");
    for (const auto& key : mLastChangedToTrueDimensions) {
        VLOG("%s", key.toString().c_str());
    }
    VLOG("Changed to false keys: \n");
    for (const auto& key : mLastChangedToFalseDimensions) {
        VLOG("%s", key.toString().c_str());
    }
}

void SimpleConditionTracker::handleStopAll(std::vector<ConditionState>& conditionCache,
                                           std::vector<bool>& conditionChangedCache) {
    // Unless the default condition is false, and there was nothing started, otherwise we have
    // triggered a condition change.
    conditionChangedCache[mIndex] =
            (mInitialValue == ConditionState::kFalse && mSlicedConditionState.empty()) ? false
                                                                                           : true;

    for (const auto& cond : mSlicedConditionState) {
        if (cond.second > 0) {
            mLastChangedToFalseDimensions.insert(cond.first);
        }
    }

    // After StopAll, we know everything has stopped. From now on, default condition is false.
    mInitialValue = ConditionState::kFalse;
    mSlicedConditionState.clear();
    conditionCache[mIndex] = ConditionState::kFalse;
}

bool SimpleConditionTracker::hitGuardRail(const HashableDimensionKey& newKey) {
    if (!mSliced || mSlicedConditionState.find(newKey) != mSlicedConditionState.end()) {
        // if the condition is not sliced or the key is not new, we are good!
        return false;
    }
    // 1. Report the tuple count if the tuple count > soft limit
    if (mSlicedConditionState.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
        size_t newTupleCount = mSlicedConditionState.size() + 1;
        StatsdStats::getInstance().noteConditionDimensionSize(mConfigKey, mConditionId, newTupleCount);
        // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
        if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
            ALOGE("Predicate %lld dropping data for dimension key %s",
                (long long)mConditionId, newKey.toString().c_str());
            return true;
        }
    }
    return false;
}

void SimpleConditionTracker::handleConditionEvent(const HashableDimensionKey& outputKey,
                                                  bool matchStart, ConditionState* conditionCache,
                                                  bool* conditionChangedCache) {
    bool changed = false;
    auto outputIt = mSlicedConditionState.find(outputKey);
    ConditionState newCondition;
    if (hitGuardRail(outputKey)) {
        (*conditionChangedCache) = false;
        // Tells the caller it's evaluated.
        (*conditionCache) = ConditionState::kUnknown;
        return;
    }
    if (outputIt == mSlicedConditionState.end()) {
        // We get a new output key.
        newCondition = matchStart ? ConditionState::kTrue : ConditionState::kFalse;
        if (matchStart && mInitialValue != ConditionState::kTrue) {
            mSlicedConditionState[outputKey] = 1;
            changed = true;
            mLastChangedToTrueDimensions.insert(outputKey);
        } else if (mInitialValue != ConditionState::kFalse) {
            // it's a stop and we don't have history about it.
            // If the default condition is not false, it means this stop is valuable to us.
            mSlicedConditionState[outputKey] = 0;
            mLastChangedToFalseDimensions.insert(outputKey);
            changed = true;
        }
    } else {
        // we have history about this output key.
        auto& startedCount = outputIt->second;
        // assign the old value first.
        newCondition = startedCount > 0 ? ConditionState::kTrue : ConditionState::kFalse;
        if (matchStart) {
            if (startedCount == 0) {
                mLastChangedToTrueDimensions.insert(outputKey);
                // This condition for this output key will change from false -> true
                changed = true;
            }

            // it's ok to do ++ here, even if we don't count nesting. The >1 counts will be treated
            // as 1 if not counting nesting.
            startedCount++;
            newCondition = ConditionState::kTrue;
        } else {
            // This is a stop event.
            if (startedCount > 0) {
                if (mCountNesting) {
                    startedCount--;
                    if (startedCount == 0) {
                        newCondition = ConditionState::kFalse;
                    }
                } else {
                    // not counting nesting, so ignore the number of starts, stop now.
                    startedCount = 0;
                    newCondition = ConditionState::kFalse;
                }
                // if everything has stopped for this output key, condition true -> false;
                if (startedCount == 0) {
                    mLastChangedToFalseDimensions.insert(outputKey);
                    changed = true;
                }
            }

            // if default condition is false, it means we don't need to keep the false values.
            if (mInitialValue == ConditionState::kFalse && startedCount == 0) {
                mSlicedConditionState.erase(outputIt);
                VLOG("erase key %s", outputKey.toString().c_str());
            }
        }
    }

    // dump all dimensions for debugging
    if (STATSD_DEBUG) {
        dumpState();
    }

    (*conditionChangedCache) = changed;
    (*conditionCache) = newCondition;

    VLOG("SimplePredicate %lld nonSlicedChange? %d", (long long)mConditionId,
         conditionChangedCache[mIndex] == true);
}

void SimpleConditionTracker::evaluateCondition(
        const LogEvent& event,
        const vector<MatchingState>& eventMatcherValues,
        const vector<sp<ConditionTracker>>& mAllConditions,
        vector<ConditionState>& conditionCache,
        vector<bool>& conditionChangedCache) {
    if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
        // it has been evaluated.
        VLOG("Yes, already evaluated, %lld %d",
            (long long)mConditionId, conditionCache[mIndex]);
        return;
    }
    mLastChangedToTrueDimensions.clear();
    mLastChangedToFalseDimensions.clear();

    if (mStopAllLogMatcherIndex >= 0 && mStopAllLogMatcherIndex < int(eventMatcherValues.size()) &&
        eventMatcherValues[mStopAllLogMatcherIndex] == MatchingState::kMatched) {
        handleStopAll(conditionCache, conditionChangedCache);
        return;
    }

    int matchedState = -1;
    // Note: The order to evaluate the following start, stop, stop_all matters.
    // The priority of overwrite is stop_all > stop > start.
    if (mStartLogMatcherIndex >= 0 &&
        eventMatcherValues[mStartLogMatcherIndex] == MatchingState::kMatched) {
        matchedState = 1;
    }

    if (mStopLogMatcherIndex >= 0 &&
        eventMatcherValues[mStopLogMatcherIndex] == MatchingState::kMatched) {
        matchedState = 0;
    }

    if (matchedState < 0) {
        // The event doesn't match this condition. So we just report existing condition values.
        conditionChangedCache[mIndex] = false;
        if (mSliced) {
            // if the condition result is sliced. The overall condition is true if any of the sliced
            // condition is true
            conditionCache[mIndex] = mInitialValue;
            for (const auto& slicedCondition : mSlicedConditionState) {
                if (slicedCondition.second > 0) {
                    conditionCache[mIndex] = ConditionState::kTrue;
                    break;
                }
            }
        } else {
            const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
            if (itr == mSlicedConditionState.end()) {
                // condition not sliced, but we haven't seen the matched start or stop yet. so
                // return initial value.
                conditionCache[mIndex] = mInitialValue;
            } else {
                // return the cached condition.
                conditionCache[mIndex] =
                        itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
            }
        }
        return;
    }

    ConditionState overallState = mInitialValue;
    bool overallChanged = false;

    if (mOutputDimensions.size() == 0) {
        handleConditionEvent(DEFAULT_DIMENSION_KEY, matchedState == 1, &overallState,
                             &overallChanged);
    } else if (!mContainANYPositionInInternalDimensions) {
        HashableDimensionKey outputValue;
        filterValues(mOutputDimensions, event.getValues(), &outputValue);

        // If this event has multiple nodes in the attribution chain,  this log event probably will
        // generate multiple dimensions. If so, we will find if the condition changes for any
        // dimension and ask the corresponding metric producer to verify whether the actual sliced
        // condition has changed or not.
        // A high level assumption is that a predicate is either sliced or unsliced. We will never
        // have both sliced and unsliced version of a predicate.
        handleConditionEvent(outputValue, matchedState == 1, &overallState, &overallChanged);
    } else {
        ALOGE("The condition tracker should not be sliced by ANY position matcher.");
    }
    conditionCache[mIndex] = overallState;
    conditionChangedCache[mIndex] = overallChanged;
}

void SimpleConditionTracker::isConditionMet(
        const ConditionKey& conditionParameters, const vector<sp<ConditionTracker>>& allConditions,
        const bool isPartialLink,
        vector<ConditionState>& conditionCache) const {

    if (conditionCache[mIndex] != ConditionState::kNotEvaluated) {
        // it has been evaluated.
        VLOG("Yes, already evaluated, %lld %d",
            (long long)mConditionId, conditionCache[mIndex]);
        return;
    }
    const auto pair = conditionParameters.find(mConditionId);

    if (pair == conditionParameters.end()) {
        ConditionState conditionState = ConditionState::kNotEvaluated;
        conditionState = conditionState | mInitialValue;
        if (!mSliced) {
            const auto& itr = mSlicedConditionState.find(DEFAULT_DIMENSION_KEY);
            if (itr != mSlicedConditionState.end()) {
                ConditionState sliceState =
                    itr->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
                conditionState = conditionState | sliceState;
            }
        }
        conditionCache[mIndex] = conditionState;
        return;
    }

    ConditionState conditionState = ConditionState::kNotEvaluated;
    const HashableDimensionKey& key = pair->second;
    if (isPartialLink) {
        // For unseen key, check whether the require dimensions are subset of sliced condition
        // output.
        conditionState = conditionState | mInitialValue;
        for (const auto& slice : mSlicedConditionState) {
            ConditionState sliceState =
                slice.second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
            if (slice.first.contains(key)) {
                conditionState = conditionState | sliceState;
            }
        }
    } else {
        auto startedCountIt = mSlicedConditionState.find(key);
        conditionState = conditionState | mInitialValue;
        if (startedCountIt != mSlicedConditionState.end()) {
            ConditionState sliceState =
                startedCountIt->second > 0 ? ConditionState::kTrue : ConditionState::kFalse;
            conditionState = conditionState | sliceState;
        }

    }
    conditionCache[mIndex] = conditionState;
    VLOG("Predicate %lld return %d", (long long)mConditionId, conditionCache[mIndex]);
}

}  // namespace statsd
}  // namespace os
}  // namespace android