memory_util.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "rocksdb/utilities/memory_util.h"
  6. #include <jni.h>
  7. #include <map>
  8. #include <string>
  9. #include <unordered_set>
  10. #include <vector>
  11. #include "include/org_rocksdb_MemoryUtil.h"
  12. #include "rocksjni/portal.h"
  13. /*
  14. * Class: org_rocksdb_MemoryUtil
  15. * Method: getApproximateMemoryUsageByType
  16. * Signature: ([J[J)Ljava/util/Map;
  17. */
  18. jobject Java_org_rocksdb_MemoryUtil_getApproximateMemoryUsageByType(
  19. JNIEnv *env, jclass, jlongArray jdb_handles, jlongArray jcache_handles) {
  20. jboolean has_exception = JNI_FALSE;
  21. std::vector<ROCKSDB_NAMESPACE::DB *> dbs =
  22. ROCKSDB_NAMESPACE::JniUtil::fromJPointers<ROCKSDB_NAMESPACE::DB>(
  23. env, jdb_handles, &has_exception);
  24. if (has_exception == JNI_TRUE) {
  25. // exception thrown: OutOfMemoryError
  26. return nullptr;
  27. }
  28. std::unordered_set<const ROCKSDB_NAMESPACE::Cache *> cache_set;
  29. jsize cache_handle_count = env->GetArrayLength(jcache_handles);
  30. if (cache_handle_count > 0) {
  31. jlong *ptr_jcache_handles =
  32. env->GetLongArrayElements(jcache_handles, nullptr);
  33. if (ptr_jcache_handles == nullptr) {
  34. // exception thrown: OutOfMemoryError
  35. return nullptr;
  36. }
  37. for (jsize i = 0; i < cache_handle_count; i++) {
  38. auto *cache_ptr =
  39. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache> *>(
  40. ptr_jcache_handles[i]);
  41. cache_set.insert(cache_ptr->get());
  42. }
  43. env->ReleaseLongArrayElements(jcache_handles, ptr_jcache_handles,
  44. JNI_ABORT);
  45. }
  46. std::map<ROCKSDB_NAMESPACE::MemoryUtil::UsageType, uint64_t> usage_by_type;
  47. if (ROCKSDB_NAMESPACE::MemoryUtil::GetApproximateMemoryUsageByType(
  48. dbs, cache_set, &usage_by_type) != ROCKSDB_NAMESPACE::Status::OK()) {
  49. // Non-OK status
  50. return nullptr;
  51. }
  52. jobject jusage_by_type = ROCKSDB_NAMESPACE::HashMapJni::construct(
  53. env, static_cast<uint32_t>(usage_by_type.size()));
  54. if (jusage_by_type == nullptr) {
  55. // exception occurred
  56. return nullptr;
  57. }
  58. const ROCKSDB_NAMESPACE::HashMapJni::FnMapKV<
  59. const ROCKSDB_NAMESPACE::MemoryUtil::UsageType, const uint64_t, jobject,
  60. jobject>
  61. fn_map_kv = [env](
  62. const std::pair<ROCKSDB_NAMESPACE::MemoryUtil::UsageType,
  63. uint64_t> &pair) {
  64. // Construct key
  65. const jobject jusage_type = ROCKSDB_NAMESPACE::ByteJni::valueOf(
  66. env, ROCKSDB_NAMESPACE::MemoryUsageTypeJni::toJavaMemoryUsageType(
  67. pair.first));
  68. if (jusage_type == nullptr) {
  69. // an error occurred
  70. return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
  71. }
  72. // Construct value
  73. const jobject jusage_value =
  74. ROCKSDB_NAMESPACE::LongJni::valueOf(env, pair.second);
  75. if (jusage_value == nullptr) {
  76. // an error occurred
  77. return std::unique_ptr<std::pair<jobject, jobject>>(nullptr);
  78. }
  79. // Construct and return pointer to pair of jobjects
  80. return std::unique_ptr<std::pair<jobject, jobject>>(
  81. new std::pair<jobject, jobject>(jusage_type, jusage_value));
  82. };
  83. if (!ROCKSDB_NAMESPACE::HashMapJni::putAll(env, jusage_by_type,
  84. usage_by_type.begin(),
  85. usage_by_type.end(), fn_map_kv)) {
  86. // exception occcurred
  87. jusage_by_type = nullptr;
  88. }
  89. return jusage_by_type;
  90. }