Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42bf7a98 authored by Vova Sharaienko's avatar Vova Sharaienko Committed by Gerrit Code Review
Browse files

Merge "[TeX] Introduced Telemetry Express Logging APIs"

parents 80ceccc4 ff1ed5d7
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */

package com.android.internal.expresslog;

import android.annotation.NonNull;

import com.android.internal.util.FrameworkStatsLog;

/** Counter encapsulates StatsD write API calls */
public final class Counter {

    // Not instantiable.
    private Counter() {}

    /**
     * Increments Telemetry Express Counter metric by 1
     * @hide
     */
    public static void logIncrement(@NonNull String metricId) {
        logIncrement(metricId, 1);
    }

    /**
     * Increments Telemetry Express Counter metric by arbitrary value
     * @hide
     */
    public static void logIncrement(@NonNull String metricId, long amount) {
        final long metricIdHash = hashString(metricId);
        FrameworkStatsLog.write(FrameworkStatsLog.EXPRESS_EVENT_REPORTED, metricIdHash, amount);
    }

    private static native long hashString(String stringToHash);
}
+5 −0
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ cc_library_shared {
        "-Wno-error=deprecated-declarations",
        "-Wunused",
        "-Wunreachable-code",

        "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
    ],

    cppflags: ["-Wno-conversion-null"],
@@ -211,6 +213,7 @@ cc_library_shared {
                "android_content_res_Configuration.cpp",
                "android_security_Scrypt.cpp",
                "com_android_internal_content_om_OverlayConfig.cpp",
                "com_android_internal_expresslog_Counter.cpp",
                "com_android_internal_net_NetworkUtilsInternal.cpp",
                "com_android_internal_os_ClassLoaderFactory.cpp",
                "com_android_internal_os_FuseAppLoop.cpp",
@@ -244,6 +247,7 @@ cc_library_shared {
                "libscrypt_static",
                "libstatssocket_lazy",
                "libskia",
                "libtextclassifier_hash_static",
            ],

            shared_libs: [
@@ -326,6 +330,7 @@ cc_library_shared {
            header_libs: [
                "bionic_libc_platform_headers",
                "dnsproxyd_protocol_headers",
                "libtextclassifier_hash_headers",
            ],
        },
        host: {
+2 −0
Original line number Diff line number Diff line
@@ -193,6 +193,7 @@ extern int register_android_security_Scrypt(JNIEnv *env);
extern int register_com_android_internal_content_F2fsUtils(JNIEnv* env);
extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
extern int register_com_android_internal_content_om_OverlayConfig(JNIEnv *env);
extern int register_com_android_internal_expresslog_Counter(JNIEnv* env);
extern int register_com_android_internal_net_NetworkUtilsInternal(JNIEnv* env);
extern int register_com_android_internal_os_ClassLoaderFactory(JNIEnv* env);
extern int register_com_android_internal_os_FuseAppLoop(JNIEnv* env);
@@ -1584,6 +1585,7 @@ static const RegJNIRec gRegJNI[] = {
        REG_JNI(register_android_os_SharedMemory),
        REG_JNI(register_android_os_incremental_IncrementalManager),
        REG_JNI(register_com_android_internal_content_om_OverlayConfig),
        REG_JNI(register_com_android_internal_expresslog_Counter),
        REG_JNI(register_com_android_internal_net_NetworkUtilsInternal),
        REG_JNI(register_com_android_internal_os_ClassLoaderFactory),
        REG_JNI(register_com_android_internal_os_LongArrayMultiStateCounter),
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 <nativehelper/JNIHelp.h>
#include <utils/hash/farmhash.h>

#include "core_jni_helpers.h"

// ----------------------------------------------------------------------------
// JNI Glue
// ----------------------------------------------------------------------------

static jclass g_stringClass = nullptr;

/**
 * Class:     com_android_internal_expresslog_Counter
 * Method:    hashString
 * Signature: (Ljava/lang/String;)J
 */
static jlong hashString(JNIEnv* env, jclass /*class*/, jstring metricNameObj) {
    ScopedUtfChars name(env, metricNameObj);
    if (name.c_str() == nullptr) {
        return 0;
    }

    return static_cast<jlong>(farmhash::Fingerprint64(name.c_str(), name.size()));
}

static const JNINativeMethod g_methods[] = {
        {"hashString", "(Ljava/lang/String;)J", (void*)hashString},
};

static const char* const kCounterPathName = "com/android/internal/expresslog/Counter";

namespace android {

int register_com_android_internal_expresslog_Counter(JNIEnv* env) {
    jclass stringClass = FindClassOrDie(env, "java/lang/String");
    g_stringClass = MakeGlobalRefOrDie(env, stringClass);

    return RegisterMethodsOrDie(env, kCounterPathName, g_methods, NELEM(g_methods));
}

} // namespace android