ADebug.cpp revision 9903589eacc655481acebc5b85632b3b84418bc9
1/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <stdlib.h>
19#include <ctype.h>
20
21#define LOG_TAG "ADebug"
22#include <cutils/atomic.h>
23#include <utils/Log.h>
24#include <utils/misc.h>
25
26#include <cutils/properties.h>
27
28#include <ADebug.h>
29#include <AStringUtils.h>
30#include <AUtils.h>
31
32namespace android {
33
34//static
35long ADebug::GetLevelFromSettingsString(
36        const char *name, const char *value, long def) {
37    // split on ,
38    const char *next = value, *current;
39    while (next != NULL) {
40        current = next;
41        next = strchr(current, ',');
42        if (next != NULL) {
43            ++next;  // pass ,
44        }
45
46        while (isspace(*current)) {
47            ++current;
48        }
49        // check for :
50        char *colon = strchr(current, ':');
51
52        // get level
53        char *end;
54        errno = 0;  // strtol does not clear errno, but it can be set for any return value
55        long level = strtol(current, &end, 10);
56        while (isspace(*end)) {
57            ++end;
58        }
59        if (errno != 0 || end == current || (end != colon && *end != '\0' && end != next)) {
60            // invalid level - skip
61            continue;
62        }
63        if (colon != NULL) {
64            // check if pattern matches
65            do {  // skip colon and spaces
66                ++colon;
67            } while (isspace(*colon));
68            size_t globLen = (next == NULL ? strlen(colon) : (next - 1 - colon));
69            while (globLen > 0 && isspace(colon[globLen - 1])) {
70                --globLen;  // trim glob
71            }
72
73            if (!AStringUtils::MatchesGlob(
74                    colon, globLen, name, strlen(name), true /* ignoreCase */)) {
75                continue;
76            }
77        }
78
79        // update value
80        def = level;
81    }
82    return def;
83}
84
85//static
86long ADebug::GetLevelFromProperty(
87        const char *name, const char *propertyName, long def) {
88    char value[PROPERTY_VALUE_MAX];
89    if (property_get(propertyName, value, NULL)) {
90        def = GetLevelFromSettingsString(name, value, def);
91    }
92    return def;
93}
94
95//static
96ADebug::Level ADebug::GetDebugLevelFromProperty(
97        const char *name, const char *propertyName, ADebug::Level def) {
98    long level = GetLevelFromProperty(name, propertyName, (long)def);
99    return (Level)min(max(level, (long)kDebugNone), (long)kDebugMax);
100}
101
102//static
103char *ADebug::GetDebugName(const char *name) {
104    char *debugName = strdup(name);
105    const char *terms[] = { "omx", "video", "audio" };
106    for (size_t i = 0; i < NELEM(terms) && debugName != NULL; i++) {
107        const char *term = terms[i];
108        const size_t len = strlen(term);
109        char *match = strcasestr(debugName, term);
110        if (match != NULL && (match == debugName || match[-1] == '.'
111                || match[len] == '.' || match[len] == '\0')) {
112            char *src = match + len;
113            if (match == debugName || match[-1] == '.') {
114                src += (*src == '.');  // remove trailing or double .
115            }
116            memmove(match, src, debugName + strlen(debugName) - src + 1);
117        }
118    }
119
120    return debugName;
121}
122
123//static
124bool ADebug::getExperimentFlag(
125        bool allow, const char *name, uint64_t modulo,
126        uint64_t limit, uint64_t plus, uint64_t timeDivisor) {
127    // see if this experiment should be disabled/enabled based on properties.
128    // default to 2 to allow 0/1 specification
129    const int undefined = 2;
130    long level = GetLevelFromProperty(name, "debug.stagefright.experiments", undefined);
131    if (level != undefined) {
132        ALOGI("experiment '%s': %s from property", name, level ? "ENABLED" : "disabled");
133        return level != 0;
134    }
135
136    static volatile int32_t haveSerial = 0;
137    static uint64_t serialNum;
138    if (!android_atomic_acquire_load(&haveSerial)) {
139        // calculate initial counter value based on serial number
140        static char serial[PROPERTY_VALUE_MAX];
141        property_get("ro.serialno", serial, "0");
142        uint64_t num = 0; // it is okay for this number to overflow
143        for (size_t i = 0; i < NELEM(serial) && serial[i] != '\0'; ++i) {
144            const char &c = serial[i];
145            // try to use most letters of serialno
146            if (isdigit(c)) {
147                num = num * 10 + (c - '0');
148            } else if (islower(c)) {
149                num = num * 26 + (c - 'a');
150            } else if (isupper(c)) {
151                num = num * 26 + (c - 'A');
152            } else {
153                num = num * 256 + c;
154            }
155        }
156        serialNum = num;
157        android_atomic_release_store(1, &haveSerial);
158    }
159    ALOGD("serial: %llu, time: %lld", (long long unsigned)serialNum, (long long)time(NULL));
160    // MINOR: use modulo for counter and time, so that their sum does not
161    // roll over, and mess up the correlation between related experiments.
162    // e.g. keep (a mod 2N) = 0 impl (a mod N) = 0
163    time_t counter = (time(NULL) / timeDivisor) % modulo + plus + serialNum % modulo;
164    bool enable = allow && (counter % modulo < limit);
165    ALOGI("experiment '%s': %s", name, enable ? "ENABLED" : "disabled");
166    return enable;
167}
168
169}  // namespace android
170
171