2018-10-08 18:03:34 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
2022-10-25 16:26:51 +00:00
|
|
|
#include "rocksdb/utilities/memory_util.h"
|
|
|
|
|
2018-10-08 18:03:34 +00:00
|
|
|
#include <jni.h>
|
2022-10-25 16:26:51 +00:00
|
|
|
|
2018-10-08 18:03:34 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_MemoryUtil.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_MemoryUtil
|
|
|
|
* Method: getApproximateMemoryUsageByType
|
|
|
|
* Signature: ([J[J)Ljava/util/Map;
|
|
|
|
*/
|
|
|
|
jobject Java_org_rocksdb_MemoryUtil_getApproximateMemoryUsageByType(
|
2020-10-15 05:22:01 +00:00
|
|
|
JNIEnv *env, jclass, jlongArray jdb_handles, jlongArray jcache_handles) {
|
|
|
|
jboolean has_exception = JNI_FALSE;
|
|
|
|
std::vector<ROCKSDB_NAMESPACE::DB *> dbs =
|
|
|
|
ROCKSDB_NAMESPACE::JniUtil::fromJPointers<ROCKSDB_NAMESPACE::DB>(
|
|
|
|
env, jdb_handles, &has_exception);
|
|
|
|
if (has_exception == JNI_TRUE) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
2018-10-08 18:03:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
std::unordered_set<const ROCKSDB_NAMESPACE::Cache *> cache_set;
|
2018-10-08 18:03:34 +00:00
|
|
|
jsize cache_handle_count = env->GetArrayLength(jcache_handles);
|
2022-10-25 16:26:51 +00:00
|
|
|
if (cache_handle_count > 0) {
|
|
|
|
jlong *ptr_jcache_handles =
|
|
|
|
env->GetLongArrayElements(jcache_handles, nullptr);
|
2018-10-08 18:03:34 +00:00
|
|
|
if (ptr_jcache_handles == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (jsize i = 0; i < cache_handle_count; i++) {
|
|
|
|
auto *cache_ptr =
|
2020-02-20 20:07:53 +00:00
|
|
|
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
|
|
|
|
ptr_jcache_handles[i]);
|
2018-10-08 18:03:34 +00:00
|
|
|
cache_set.insert(cache_ptr->get());
|
|
|
|
}
|
2022-10-25 16:26:51 +00:00
|
|
|
env->ReleaseLongArrayElements(jcache_handles, ptr_jcache_handles,
|
|
|
|
JNI_ABORT);
|
2018-10-08 18:03:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
std::map<ROCKSDB_NAMESPACE::MemoryUtil::UsageType, uint64_t> usage_by_type;
|
|
|
|
if (ROCKSDB_NAMESPACE::MemoryUtil::GetApproximateMemoryUsageByType(
|
|
|
|
dbs, cache_set, &usage_by_type) != ROCKSDB_NAMESPACE::Status::OK()) {
|
2018-10-08 18:03:34 +00:00
|
|
|
// Non-OK status
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
jobject jusage_by_type = ROCKSDB_NAMESPACE::HashMapJni::construct(
|
2018-10-08 18:03:34 +00:00
|
|
|
env, static_cast<uint32_t>(usage_by_type.size()));
|
|
|
|
if (jusage_by_type == nullptr) {
|
|
|
|
// exception occurred
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
const ROCKSDB_NAMESPACE::HashMapJni::FnMapKV<
|
|
|
|
const ROCKSDB_NAMESPACE::MemoryUtil::UsageType, const uint64_t, jobject,
|
|
|
|
jobject>
|
|
|
|
fn_map_kv = [env](
|
|
|
|
const std::pair<ROCKSDB_NAMESPACE::MemoryUtil::UsageType,
|
|
|
|
uint64_t> &pair) {
|
2018-10-08 18:03:34 +00:00
|
|
|
// Construct key
|
2020-02-20 20:07:53 +00:00
|
|
|
const jobject jusage_type = ROCKSDB_NAMESPACE::ByteJni::valueOf(
|
|
|
|
env, ROCKSDB_NAMESPACE::MemoryUsageTypeJni::toJavaMemoryUsageType(
|
|
|
|
pair.first));
|
2018-10-08 18:03:34 +00:00
|
|
|
if (jusage_type == nullptr) {
|
|
|
|
// an error occurred
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
|
|
|
|
}
|
|
|
|
// Construct value
|
|
|
|
const jobject jusage_value =
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::LongJni::valueOf(env, pair.second);
|
2018-10-08 18:03:34 +00:00
|
|
|
if (jusage_value == nullptr) {
|
|
|
|
// an error occurred
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
|
|
|
|
}
|
|
|
|
// Construct and return pointer to pair of jobjects
|
|
|
|
return std::unique_ptr<std::pair<jobject, jobject>>(
|
2022-10-25 16:26:51 +00:00
|
|
|
new std::pair<jobject, jobject>(jusage_type, jusage_value));
|
2018-10-08 18:03:34 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
if (!ROCKSDB_NAMESPACE::HashMapJni::putAll(env, jusage_by_type,
|
|
|
|
usage_by_type.begin(),
|
|
|
|
usage_by_type.end(), fn_map_kv)) {
|
2018-10-08 18:03:34 +00:00
|
|
|
// exception occcurred
|
|
|
|
jusage_by_type = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return jusage_by_type;
|
|
|
|
}
|