summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/foundation/ADebug.cpp
blob: 2c5f544014ba4be61b8ddeb7f16586e74ffeb681 (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
/*
 * Copyright 2014 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 <errno.h>
#include <stdlib.h>
#include <ctype.h>

#define LOG_TAG "ADebug"
#include <cutils/atomic.h>
#include <utils/Log.h>
#include <utils/misc.h>

#include <cutils/properties.h>

#include <ADebug.h>
#include <AStringUtils.h>
#include <AUtils.h>

#define UNUSED(x) ((void)(x))

namespace android {

//static
long ADebug::GetLevelFromSettingsString(
        const char *name, const char *value, long def) {
    // split on ,
    const char *next = value, *current;
    while (next != NULL) {
        current = next;
        next = strchr(current, ',');
        if (next != NULL) {
            ++next;  // pass ,
        }

        while (isspace(*current)) {
            ++current;
        }
        // check for :
        char *colon = strchr(current, ':');

        // get level
        char *end;
        errno = 0;  // strtol does not clear errno, but it can be set for any return value
        long level = strtol(current, &end, 10);
        while (isspace(*end)) {
            ++end;
        }
        if (errno != 0 || end == current || (end != colon && *end != '\0' && end != next)) {
            // invalid level - skip
            continue;
        }
        if (colon != NULL) {
            // check if pattern matches
            do {  // skip colon and spaces
                ++colon;
            } while (isspace(*colon));
            size_t globLen = (next == NULL ? strlen(colon) : (next - 1 - colon));
            while (globLen > 0 && isspace(colon[globLen - 1])) {
                --globLen;  // trim glob
            }

            if (!AStringUtils::MatchesGlob(
                    colon, globLen, name, strlen(name), true /* ignoreCase */)) {
                continue;
            }
        }

        // update value
        def = level;
    }
    return def;
}

//static
long ADebug::GetLevelFromProperty(
        const char *name, const char *propertyName, long def) {
    char value[PROPERTY_VALUE_MAX];
    if (property_get(propertyName, value, NULL)) {
        def = GetLevelFromSettingsString(name, value, def);
    }
    return def;
}

//static
ADebug::Level ADebug::GetDebugLevelFromProperty(
        const char *name, const char *propertyName, ADebug::Level def) {
    long level = GetLevelFromProperty(name, propertyName, (long)def);
    return (Level)min(max(level, (long)kDebugNone), (long)kDebugMax);
}

//static
char *ADebug::GetDebugName(const char *name) {
    char *debugName = strdup(name);
    const char *terms[] = { "omx", "video", "audio" };
    for (size_t i = 0; i < NELEM(terms) && debugName != NULL; i++) {
        const char *term = terms[i];
        const size_t len = strlen(term);
        char *match = strcasestr(debugName, term);
        if (match != NULL && (match == debugName || match[-1] == '.'
                || match[len] == '.' || match[len] == '\0')) {
            char *src = match + len;
            if (match == debugName || match[-1] == '.') {
                src += (*src == '.');  // remove trailing or double .
            }
            memmove(match, src, debugName + strlen(debugName) - src + 1);
        }
    }

    return debugName;
}

//static
bool ADebug::getExperimentFlag(
        bool allow, const char *name, uint64_t modulo,
        uint64_t limit, uint64_t plus, uint64_t timeDivisor) {
    // see if this experiment should be disabled/enabled based on properties.
    // default to 2 to allow 0/1 specification
    const int undefined = 2;
    long level = GetLevelFromProperty(name, "debug.stagefright.experiments", undefined);
    if (level != undefined) {
        ALOGI("experiment '%s': %s from property", name, level ? "ENABLED" : "disabled");
        return allow && (level != 0);
    }

#ifndef ENABLE_STAGEFRIGHT_AUTO_EXPERIMENTS
    UNUSED(modulo);
    UNUSED(limit);
    UNUSED(plus);
    UNUSED(timeDivisor);
    return false;
#else
    // Disable automatic experiments in "non-experimental" builds (that is, _all_ builds
    // as there is no "experimental" build).
    // TODO: change build type to enable automatic experiments in the future for some builds
    char value[PROPERTY_VALUE_MAX];
    if (property_get("ro.build.type", value, NULL)) {
        if (strcmp(value, "experimental")) {
            return false;
        }
    }

    static volatile int32_t haveSerial = 0;
    static uint64_t serialNum;
    if (!android_atomic_acquire_load(&haveSerial)) {
        // calculate initial counter value based on serial number
        static char serial[PROPERTY_VALUE_MAX];
        property_get("ro.serialno", serial, "0");
        uint64_t num = 0; // it is okay for this number to overflow
        for (size_t i = 0; i < NELEM(serial) && serial[i] != '\0'; ++i) {
            const char &c = serial[i];
            // try to use most letters of serialno
            if (isdigit(c)) {
                num = num * 10 + (c - '0');
            } else if (islower(c)) {
                num = num * 26 + (c - 'a');
            } else if (isupper(c)) {
                num = num * 26 + (c - 'A');
            } else {
                num = num * 256 + c;
            }
        }
        serialNum = num;
        android_atomic_release_store(1, &haveSerial);
    }
    ALOGD("serial: %llu, time: %lld", (long long unsigned)serialNum, (long long)time(NULL));
    // MINOR: use modulo for counter and time, so that their sum does not
    // roll over, and mess up the correlation between related experiments.
    // e.g. keep (a mod 2N) = 0 impl (a mod N) = 0
    time_t counter = (time(NULL) / timeDivisor) % modulo + plus + serialNum % modulo;
    bool enable = allow && (counter % modulo < limit);
    ALOGI("experiment '%s': %s", name, enable ? "ENABLED" : "disabled");
    return enable;
#endif
}

}  // namespace android