]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/rocksjni/memory_util.cc
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / java / rocksjni / memory_util.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #include <jni.h>
7 #include <map>
8 #include <string>
9 #include <unordered_set>
10 #include <vector>
11
12 #include "include/org_rocksdb_MemoryUtil.h"
13
14 #include "rocksjni/portal.h"
15
16 #include "rocksdb/utilities/memory_util.h"
17
18
19 /*
20 * Class: org_rocksdb_MemoryUtil
21 * Method: getApproximateMemoryUsageByType
22 * Signature: ([J[J)Ljava/util/Map;
23 */
24 jobject Java_org_rocksdb_MemoryUtil_getApproximateMemoryUsageByType(
25 JNIEnv *env, jclass /*jclazz*/, jlongArray jdb_handles, jlongArray jcache_handles) {
26
27 std::vector<rocksdb::DB*> dbs;
28 jsize db_handle_count = env->GetArrayLength(jdb_handles);
29 if(db_handle_count > 0) {
30 jlong *ptr_jdb_handles = env->GetLongArrayElements(jdb_handles, nullptr);
31 if (ptr_jdb_handles == nullptr) {
32 // exception thrown: OutOfMemoryError
33 return nullptr;
34 }
35 for (jsize i = 0; i < db_handle_count; i++) {
36 dbs.push_back(reinterpret_cast<rocksdb::DB *>(ptr_jdb_handles[i]));
37 }
38 env->ReleaseLongArrayElements(jdb_handles, ptr_jdb_handles, JNI_ABORT);
39 }
40
41 std::unordered_set<const rocksdb::Cache*> cache_set;
42 jsize cache_handle_count = env->GetArrayLength(jcache_handles);
43 if(cache_handle_count > 0) {
44 jlong *ptr_jcache_handles = env->GetLongArrayElements(jcache_handles, nullptr);
45 if (ptr_jcache_handles == nullptr) {
46 // exception thrown: OutOfMemoryError
47 return nullptr;
48 }
49 for (jsize i = 0; i < cache_handle_count; i++) {
50 auto *cache_ptr =
51 reinterpret_cast<std::shared_ptr<rocksdb::Cache> *>(ptr_jcache_handles[i]);
52 cache_set.insert(cache_ptr->get());
53 }
54 env->ReleaseLongArrayElements(jcache_handles, ptr_jcache_handles, JNI_ABORT);
55 }
56
57 std::map<rocksdb::MemoryUtil::UsageType, uint64_t> usage_by_type;
58 if(rocksdb::MemoryUtil::GetApproximateMemoryUsageByType(dbs, cache_set, &usage_by_type) != rocksdb::Status::OK()) {
59 // Non-OK status
60 return nullptr;
61 }
62
63 jobject jusage_by_type = rocksdb::HashMapJni::construct(
64 env, static_cast<uint32_t>(usage_by_type.size()));
65 if (jusage_by_type == nullptr) {
66 // exception occurred
67 return nullptr;
68 }
69 const rocksdb::HashMapJni::FnMapKV<const rocksdb::MemoryUtil::UsageType, const uint64_t, jobject, jobject>
70 fn_map_kv =
71 [env](const std::pair<rocksdb::MemoryUtil::UsageType, uint64_t>& pair) {
72 // Construct key
73 const jobject jusage_type =
74 rocksdb::ByteJni::valueOf(env, rocksdb::MemoryUsageTypeJni::toJavaMemoryUsageType(pair.first));
75 if (jusage_type == nullptr) {
76 // an error occurred
77 return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
78 }
79 // Construct value
80 const jobject jusage_value =
81 rocksdb::LongJni::valueOf(env, pair.second);
82 if (jusage_value == nullptr) {
83 // an error occurred
84 return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
85 }
86 // Construct and return pointer to pair of jobjects
87 return std::unique_ptr<std::pair<jobject, jobject>>(
88 new std::pair<jobject, jobject>(jusage_type,
89 jusage_value));
90 };
91
92 if (!rocksdb::HashMapJni::putAll(env, jusage_by_type, usage_by_type.begin(),
93 usage_by_type.end(), fn_map_kv)) {
94 // exception occcurred
95 jusage_by_type = nullptr;
96 }
97
98 return jusage_by_type;
99
100 }