diff options
author | Artyom Palvelev <artyompp@google.com> | 2023-09-07 16:03:53 +0100 |
---|---|---|
committer | Artyom Palvelev <artyompp@google.com> | 2023-09-08 14:49:43 +0100 |
commit | 6af5fd85f8d0069e9cd5cd96eb75a0ce07c6c8a6 (patch) | |
tree | fda8caac165bc137da3e8b9c2db570464d91f64d | |
parent | 6450e4f780b66195a39a8fee0e76ba259155ddef (diff) | |
download | gamesdk-6af5fd85f8d0069e9cd5cd96eb75a0ce07c6c8a6.tar.gz |
attach MemoryAdvice thread to JVM
MemoryAdvice functions can be potentially called from any thread.
As they need to perform JNI calls, it's necessary to make JVM aware of
the context of the new thread. This commit adds AttachCurrentThread()
call when necessary.
Bug: 277551258
Test: run either AGDKTunnel or Hogger sample
Change-Id: I09d1f266655c038e444d2587d86890d33f98a34c
-rw-r--r-- | games-memory-advice/core/memory_advice_impl.cpp | 6 | ||||
-rw-r--r-- | include/memory_advice/memory_advice.h | 4 | ||||
-rw-r--r-- | src/common/jni/jnictx.cpp | 7 |
3 files changed, 15 insertions, 2 deletions
diff --git a/games-memory-advice/core/memory_advice_impl.cpp b/games-memory-advice/core/memory_advice_impl.cpp index f24299bc..6bb66796 100644 --- a/games-memory-advice/core/memory_advice_impl.cpp +++ b/games-memory-advice/core/memory_advice_impl.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include "jni/jnictx.h" #include "memory_advice_impl.h" #include <algorithm> @@ -126,6 +127,11 @@ Json::object MemoryAdviceImpl::GetAdvice() { CheckCancelledWatchers(); std::lock_guard<std::mutex> lock(advice_mutex_); + + // Make sure current thread is attached to the JVM. + // This is important because we perform many JNI calls here to get system metrics. + gamesdk::jni::Ctx::Instance()->Env(); + double start_time = MillisecondsSinceEpoch(); Json::object advice; Json::object data; diff --git a/include/memory_advice/memory_advice.h b/include/memory_advice/memory_advice.h index 9b4c07fb..f86fc670 100644 --- a/include/memory_advice/memory_advice.h +++ b/include/memory_advice/memory_advice.h @@ -95,7 +95,9 @@ MemoryAdvice_ErrorCode MemoryAdvice_init(JNIEnv *env, jobject context); * other functions. * * This version of the init function will read the given params instead of - * using the library provided default params. + * using the library provided default params. The format of params is a JSON + * string with a semantic like default.json in the root directory of this + * library. * * @param env a JNIEnv * @param context the app context diff --git a/src/common/jni/jnictx.cpp b/src/common/jni/jnictx.cpp index a3871f4c..b4599079 100644 --- a/src/common/jni/jnictx.cpp +++ b/src/common/jni/jnictx.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include <unistd.h> #include "jnictx.h" #include <memory> @@ -60,13 +61,17 @@ Ctx::~Ctx() { } JNIEnv* Ctx::Env() const { if (theEnv == nullptr && jvm_ != nullptr) { + ALOGI("AttachCurrentThread for thread %d", static_cast<int>(gettid())); jvm_->AttachCurrentThread(&theEnv, NULL); } return theEnv; } void Ctx::DetachThread() const { - if (jvm_ != nullptr) jvm_->DetachCurrentThread(); + if (jvm_ != nullptr) { + ALOGI("DetachCurrentThread for thread %d", static_cast<int>(gettid())); + jvm_->DetachCurrentThread(); + } theEnv = nullptr; } |