1f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar/*
2f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * Copyright 2014 The Android Open Source Project
3f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar *
4f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * Licensed under the Apache License, Version 2.0 (the "License");
5f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * you may not use this file except in compliance with the License.
6f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * You may obtain a copy of the License at
7f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar *
8f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar *      http://www.apache.org/licenses/LICENSE-2.0
9f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar *
10f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * Unless required by applicable law or agreed to in writing, software
11f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * distributed under the License is distributed on an "AS IS" BASIS,
12f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * See the License for the specific language governing permissions and
14f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar * limitations under the License.
15f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar */
16f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
17f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <errno.h>
18f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <stdlib.h>
19f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <ctype.h>
20f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
21f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define LOG_TAG "ADebug"
22011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar#include <cutils/atomic.h>
23f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <utils/Log.h>
24f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <utils/misc.h>
25f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
26f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <cutils/properties.h>
27f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
28f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <ADebug.h>
29f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <AStringUtils.h>
30f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <AUtils.h>
31f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
327c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar#define UNUSED(x) ((void)(x))
337c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar
34f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarnamespace android {
35f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
36f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar//static
379903589eacc655481acebc5b85632b3b84418bc9Lajos Molnarlong ADebug::GetLevelFromSettingsString(
389903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        const char *name, const char *value, long def) {
39f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // split on ,
40f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    const char *next = value, *current;
41f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    while (next != NULL) {
42f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        current = next;
43f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        next = strchr(current, ',');
44f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        if (next != NULL) {
45f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            ++next;  // pass ,
46f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
47f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
48f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        while (isspace(*current)) {
49f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            ++current;
50f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
51f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        // check for :
52f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        char *colon = strchr(current, ':');
53f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
54f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        // get level
55f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        char *end;
569903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        errno = 0;  // strtol does not clear errno, but it can be set for any return value
579903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        long level = strtol(current, &end, 10);
58f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        while (isspace(*end)) {
59f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            ++end;
60f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
61f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        if (errno != 0 || end == current || (end != colon && *end != '\0' && end != next)) {
62f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            // invalid level - skip
63f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            continue;
64f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
65f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        if (colon != NULL) {
66f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            // check if pattern matches
67f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            do {  // skip colon and spaces
68f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                ++colon;
69f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            } while (isspace(*colon));
70f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            size_t globLen = (next == NULL ? strlen(colon) : (next - 1 - colon));
71f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            while (globLen > 0 && isspace(colon[globLen - 1])) {
72f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                --globLen;  // trim glob
73f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            }
74f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
75f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            if (!AStringUtils::MatchesGlob(
76f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    colon, globLen, name, strlen(name), true /* ignoreCase */)) {
77f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                continue;
78f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            }
79f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
80f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
819903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        // update value
829903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        def = level;
83f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
84f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return def;
85f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}
86f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
87f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar//static
889903589eacc655481acebc5b85632b3b84418bc9Lajos Molnarlong ADebug::GetLevelFromProperty(
899903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        const char *name, const char *propertyName, long def) {
90f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    char value[PROPERTY_VALUE_MAX];
91f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (property_get(propertyName, value, NULL)) {
929903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        def = GetLevelFromSettingsString(name, value, def);
93f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
94f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return def;
95f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}
96f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
97f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar//static
989903589eacc655481acebc5b85632b3b84418bc9Lajos MolnarADebug::Level ADebug::GetDebugLevelFromProperty(
999903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        const char *name, const char *propertyName, ADebug::Level def) {
1009903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    long level = GetLevelFromProperty(name, propertyName, (long)def);
1019903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    return (Level)min(max(level, (long)kDebugNone), (long)kDebugMax);
1029903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar}
1039903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar
1049903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar//static
105f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarchar *ADebug::GetDebugName(const char *name) {
106f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    char *debugName = strdup(name);
107f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    const char *terms[] = { "omx", "video", "audio" };
108f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    for (size_t i = 0; i < NELEM(terms) && debugName != NULL; i++) {
109f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        const char *term = terms[i];
110f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        const size_t len = strlen(term);
111f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        char *match = strcasestr(debugName, term);
112f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        if (match != NULL && (match == debugName || match[-1] == '.'
113f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                || match[len] == '.' || match[len] == '\0')) {
114f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            char *src = match + len;
115f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            if (match == debugName || match[-1] == '.') {
116f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                src += (*src == '.');  // remove trailing or double .
117f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            }
118f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            memmove(match, src, debugName + strlen(debugName) - src + 1);
119f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
120f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
121f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
122f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return debugName;
123f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}
124f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
125011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar//static
126011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnarbool ADebug::getExperimentFlag(
127011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        bool allow, const char *name, uint64_t modulo,
128011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        uint64_t limit, uint64_t plus, uint64_t timeDivisor) {
1299903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    // see if this experiment should be disabled/enabled based on properties.
1309903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    // default to 2 to allow 0/1 specification
1319903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    const int undefined = 2;
1329903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    long level = GetLevelFromProperty(name, "debug.stagefright.experiments", undefined);
1339903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    if (level != undefined) {
1349903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar        ALOGI("experiment '%s': %s from property", name, level ? "ENABLED" : "disabled");
1357c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar        return allow && (level != 0);
1367c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    }
1377c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar
1387c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar#ifndef ENABLE_STAGEFRIGHT_AUTO_EXPERIMENTS
1397c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    UNUSED(modulo);
1407c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    UNUSED(limit);
1417c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    UNUSED(plus);
1427c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    UNUSED(timeDivisor);
1437c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    return false;
1447c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar#else
1457c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    // Disable automatic experiments in "non-experimental" builds (that is, _all_ builds
1467c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    // as there is no "experimental" build).
1477c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    // TODO: change build type to enable automatic experiments in the future for some builds
1487c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    char value[PROPERTY_VALUE_MAX];
1497c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar    if (property_get("ro.build.type", value, NULL)) {
1507c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar        if (strcmp(value, "experimental")) {
1517c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar            return false;
1527c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar        }
1539903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    }
1549903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar
155011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    static volatile int32_t haveSerial = 0;
156011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    static uint64_t serialNum;
157011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    if (!android_atomic_acquire_load(&haveSerial)) {
158011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        // calculate initial counter value based on serial number
159011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        static char serial[PROPERTY_VALUE_MAX];
160011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        property_get("ro.serialno", serial, "0");
161011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        uint64_t num = 0; // it is okay for this number to overflow
162011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        for (size_t i = 0; i < NELEM(serial) && serial[i] != '\0'; ++i) {
163011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            const char &c = serial[i];
164011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            // try to use most letters of serialno
165011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            if (isdigit(c)) {
166011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar                num = num * 10 + (c - '0');
167011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            } else if (islower(c)) {
168011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar                num = num * 26 + (c - 'a');
169011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            } else if (isupper(c)) {
170011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar                num = num * 26 + (c - 'A');
171011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            } else {
172011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar                num = num * 256 + c;
173011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar            }
174011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        }
175011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        serialNum = num;
176011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar        android_atomic_release_store(1, &haveSerial);
177011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    }
1789903589eacc655481acebc5b85632b3b84418bc9Lajos Molnar    ALOGD("serial: %llu, time: %lld", (long long unsigned)serialNum, (long long)time(NULL));
179011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    // MINOR: use modulo for counter and time, so that their sum does not
180011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    // roll over, and mess up the correlation between related experiments.
181011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    // e.g. keep (a mod 2N) = 0 impl (a mod N) = 0
182011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    time_t counter = (time(NULL) / timeDivisor) % modulo + plus + serialNum % modulo;
183011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    bool enable = allow && (counter % modulo < limit);
184011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    ALOGI("experiment '%s': %s", name, enable ? "ENABLED" : "disabled");
185011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar    return enable;
1867c3c275b4d0100eec36f7b66637a7aace7971466Lajos Molnar#endif
187011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar}
188011778fd4cb9606b68bfea1ed483d73c04fc6dcdLajos Molnar
189f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}  // namespace android
190f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
191