1 /*
2 * Copyright (C) 2019, 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 <jni.h>
18 #include <log/log.h>
19 #include <stats_event.h>
20 #include <stats_pull_atom_callback.h>
21
22 #include <chrono>
23 #include <thread>
24
25 using std::this_thread::sleep_for;
26
27 namespace {
28 static int32_t sAtomTag;
29 static int32_t sPullReturnVal;
30 static int64_t sLatencyMillis;
31 static int32_t sAtomsPerPull;
32 static int32_t sNumPulls = 0;
33
pullAtomCallback(int32_t atomTag,AStatsEventList * data,void *)34 static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag, AStatsEventList* data,
35 void* /*cookie*/) {
36 sNumPulls++;
37 sleep_for(std::chrono::milliseconds(sLatencyMillis));
38 for (int i = 0; i < sAtomsPerPull; i++) {
39 AStatsEvent* event = AStatsEventList_addStatsEvent(data);
40 AStatsEvent_setAtomId(event, atomTag);
41 AStatsEvent_writeInt64(event, (int64_t) sNumPulls);
42 AStatsEvent_build(event);
43 }
44 return sPullReturnVal;
45 }
46
47 extern "C" JNIEXPORT void JNICALL
Java_com_android_internal_os_statsd_libstats_LibStatsPullTests_setStatsPuller(JNIEnv *,jobject,jint atomTag,jlong timeoutMillis,jlong coolDownMillis,jint pullRetVal,jlong latencyMillis,int atomsPerPull)48 Java_com_android_internal_os_statsd_libstats_LibStatsPullTests_setStatsPuller(
49 JNIEnv* /*env*/, jobject /* this */, jint atomTag, jlong timeoutMillis,
50 jlong coolDownMillis, jint pullRetVal, jlong latencyMillis, int atomsPerPull) {
51 sAtomTag = atomTag;
52 sPullReturnVal = pullRetVal;
53 sLatencyMillis = latencyMillis;
54 sAtomsPerPull = atomsPerPull;
55 sNumPulls = 0;
56 AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
57 AStatsManager_PullAtomMetadata_setCoolDownMillis(metadata, coolDownMillis);
58 AStatsManager_PullAtomMetadata_setTimeoutMillis(metadata, timeoutMillis);
59
60 AStatsManager_setPullAtomCallback(sAtomTag, metadata, &pullAtomCallback, nullptr);
61 AStatsManager_PullAtomMetadata_release(metadata);
62 }
63
64 extern "C" JNIEXPORT void JNICALL
Java_com_android_internal_os_statsd_libstats_LibStatsPullTests_clearStatsPuller(JNIEnv *,jobject,jint)65 Java_com_android_internal_os_statsd_libstats_LibStatsPullTests_clearStatsPuller(JNIEnv* /*env*/,
66 jobject /* this */,
67 jint /*atomTag*/) {
68 AStatsManager_clearPullAtomCallback(sAtomTag);
69 }
70 } // namespace
71