statistics.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // This file implements the "bridge" between Java and C++ and enables
  7. // calling c++ ROCKSDB_NAMESPACE::Statistics methods from Java side.
  8. #include "rocksdb/statistics.h"
  9. #include <jni.h>
  10. #include <memory>
  11. #include <set>
  12. #include "include/org_rocksdb_Statistics.h"
  13. #include "rocksjni/cplusplus_to_java_convert.h"
  14. #include "rocksjni/portal.h"
  15. #include "rocksjni/statisticsjni.h"
  16. /*
  17. * Class: org_rocksdb_Statistics
  18. * Method: newStatistics
  19. * Signature: ()J
  20. */
  21. jlong Java_org_rocksdb_Statistics_newStatistics__(JNIEnv* env, jclass jcls) {
  22. return Java_org_rocksdb_Statistics_newStatistics___3BJ(env, jcls, nullptr, 0);
  23. }
  24. /*
  25. * Class: org_rocksdb_Statistics
  26. * Method: newStatistics
  27. * Signature: (J)J
  28. */
  29. jlong Java_org_rocksdb_Statistics_newStatistics__J(
  30. JNIEnv* env, jclass jcls, jlong jother_statistics_handle) {
  31. return Java_org_rocksdb_Statistics_newStatistics___3BJ(
  32. env, jcls, nullptr, jother_statistics_handle);
  33. }
  34. /*
  35. * Class: org_rocksdb_Statistics
  36. * Method: newStatistics
  37. * Signature: ([B)J
  38. */
  39. jlong Java_org_rocksdb_Statistics_newStatistics___3B(JNIEnv* env, jclass jcls,
  40. jbyteArray jhistograms) {
  41. return Java_org_rocksdb_Statistics_newStatistics___3BJ(env, jcls, jhistograms,
  42. 0);
  43. }
  44. /*
  45. * Class: org_rocksdb_Statistics
  46. * Method: newStatistics
  47. * Signature: ([BJ)J
  48. */
  49. jlong Java_org_rocksdb_Statistics_newStatistics___3BJ(
  50. JNIEnv* env, jclass, jbyteArray jhistograms,
  51. jlong jother_statistics_handle) {
  52. std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>* pSptr_other_statistics =
  53. nullptr;
  54. if (jother_statistics_handle > 0) {
  55. pSptr_other_statistics =
  56. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  57. jother_statistics_handle);
  58. }
  59. std::set<uint32_t> histograms;
  60. if (jhistograms != nullptr) {
  61. const jsize len = env->GetArrayLength(jhistograms);
  62. if (len > 0) {
  63. jbyte* jhistogram = env->GetByteArrayElements(jhistograms, nullptr);
  64. if (jhistogram == nullptr) {
  65. // exception thrown: OutOfMemoryError
  66. return 0;
  67. }
  68. for (jsize i = 0; i < len; i++) {
  69. const ROCKSDB_NAMESPACE::Histograms histogram =
  70. ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram[i]);
  71. histograms.emplace(histogram);
  72. }
  73. env->ReleaseByteArrayElements(jhistograms, jhistogram, JNI_ABORT);
  74. }
  75. }
  76. std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> sptr_other_statistics =
  77. nullptr;
  78. if (pSptr_other_statistics != nullptr) {
  79. sptr_other_statistics = *pSptr_other_statistics;
  80. }
  81. auto* pSptr_statistics =
  82. new std::shared_ptr<ROCKSDB_NAMESPACE::StatisticsJni>(
  83. new ROCKSDB_NAMESPACE::StatisticsJni(sptr_other_statistics,
  84. histograms));
  85. return GET_CPLUSPLUS_POINTER(pSptr_statistics);
  86. }
  87. /*
  88. * Class: org_rocksdb_Statistics
  89. * Method: disposeInternal
  90. * Signature: (J)V
  91. */
  92. void Java_org_rocksdb_Statistics_disposeInternalJni(JNIEnv*, jclass,
  93. jlong jhandle) {
  94. if (jhandle > 0) {
  95. auto* pSptr_statistics =
  96. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  97. jhandle);
  98. delete pSptr_statistics;
  99. }
  100. }
  101. /*
  102. * Class: org_rocksdb_Statistics
  103. * Method: statsLevel
  104. * Signature: (J)B
  105. */
  106. jbyte Java_org_rocksdb_Statistics_statsLevel(JNIEnv*, jclass, jlong jhandle) {
  107. auto* pSptr_statistics =
  108. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  109. jhandle);
  110. assert(pSptr_statistics != nullptr);
  111. return ROCKSDB_NAMESPACE::StatsLevelJni::toJavaStatsLevel(
  112. pSptr_statistics->get()->get_stats_level());
  113. }
  114. /*
  115. * Class: org_rocksdb_Statistics
  116. * Method: setStatsLevel
  117. * Signature: (JB)V
  118. */
  119. void Java_org_rocksdb_Statistics_setStatsLevel(JNIEnv*, jclass, jlong jhandle,
  120. jbyte jstats_level) {
  121. auto* pSptr_statistics =
  122. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  123. jhandle);
  124. assert(pSptr_statistics != nullptr);
  125. auto stats_level =
  126. ROCKSDB_NAMESPACE::StatsLevelJni::toCppStatsLevel(jstats_level);
  127. pSptr_statistics->get()->set_stats_level(stats_level);
  128. }
  129. /*
  130. * Class: org_rocksdb_Statistics
  131. * Method: getTickerCount
  132. * Signature: (JB)J
  133. */
  134. jlong Java_org_rocksdb_Statistics_getTickerCount(JNIEnv*, jclass, jlong jhandle,
  135. jbyte jticker_type) {
  136. auto* pSptr_statistics =
  137. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  138. jhandle);
  139. assert(pSptr_statistics != nullptr);
  140. auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type);
  141. uint64_t count = pSptr_statistics->get()->getTickerCount(ticker);
  142. return static_cast<jlong>(count);
  143. }
  144. /*
  145. * Class: org_rocksdb_Statistics
  146. * Method: getAndResetTickerCount
  147. * Signature: (JB)J
  148. */
  149. jlong Java_org_rocksdb_Statistics_getAndResetTickerCount(JNIEnv*, jclass,
  150. jlong jhandle,
  151. jbyte jticker_type) {
  152. auto* pSptr_statistics =
  153. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  154. jhandle);
  155. assert(pSptr_statistics != nullptr);
  156. auto ticker = ROCKSDB_NAMESPACE::TickerTypeJni::toCppTickers(jticker_type);
  157. return pSptr_statistics->get()->getAndResetTickerCount(ticker);
  158. }
  159. /*
  160. * Class: org_rocksdb_Statistics
  161. * Method: getHistogramData
  162. * Signature: (JB)Lorg/rocksdb/HistogramData;
  163. */
  164. jobject Java_org_rocksdb_Statistics_getHistogramData(JNIEnv* env, jclass,
  165. jlong jhandle,
  166. jbyte jhistogram_type) {
  167. auto* pSptr_statistics =
  168. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  169. jhandle);
  170. assert(pSptr_statistics != nullptr);
  171. // TODO(AR) perhaps better to construct a Java Object Wrapper that
  172. // uses ptr to C++ `new HistogramData`
  173. ROCKSDB_NAMESPACE::HistogramData data;
  174. auto histogram =
  175. ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type);
  176. pSptr_statistics->get()->histogramData(
  177. static_cast<ROCKSDB_NAMESPACE::Histograms>(histogram), &data);
  178. jclass jclazz = ROCKSDB_NAMESPACE::HistogramDataJni::getJClass(env);
  179. if (jclazz == nullptr) {
  180. // exception occurred accessing class
  181. return nullptr;
  182. }
  183. jmethodID mid =
  184. ROCKSDB_NAMESPACE::HistogramDataJni::getConstructorMethodId(env);
  185. if (mid == nullptr) {
  186. // exception occurred accessing method
  187. return nullptr;
  188. }
  189. return env->NewObject(jclazz, mid, data.median, data.percentile95,
  190. data.percentile99, data.average,
  191. data.standard_deviation, data.max, data.count, data.sum,
  192. data.min);
  193. }
  194. /*
  195. * Class: org_rocksdb_Statistics
  196. * Method: getHistogramString
  197. * Signature: (JB)Ljava/lang/String;
  198. */
  199. jstring Java_org_rocksdb_Statistics_getHistogramString(JNIEnv* env, jclass,
  200. jlong jhandle,
  201. jbyte jhistogram_type) {
  202. auto* pSptr_statistics =
  203. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  204. jhandle);
  205. assert(pSptr_statistics != nullptr);
  206. auto histogram =
  207. ROCKSDB_NAMESPACE::HistogramTypeJni::toCppHistograms(jhistogram_type);
  208. auto str = pSptr_statistics->get()->getHistogramString(histogram);
  209. return env->NewStringUTF(str.c_str());
  210. }
  211. /*
  212. * Class: org_rocksdb_Statistics
  213. * Method: reset
  214. * Signature: (J)V
  215. */
  216. void Java_org_rocksdb_Statistics_reset(JNIEnv* env, jclass, jlong jhandle) {
  217. auto* pSptr_statistics =
  218. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  219. jhandle);
  220. assert(pSptr_statistics != nullptr);
  221. ROCKSDB_NAMESPACE::Status s = pSptr_statistics->get()->Reset();
  222. if (!s.ok()) {
  223. ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
  224. }
  225. }
  226. /*
  227. * Class: org_rocksdb_Statistics
  228. * Method: toString
  229. * Signature: (J)Ljava/lang/String;
  230. */
  231. jstring Java_org_rocksdb_Statistics_toString(JNIEnv* env, jclass,
  232. jlong jhandle) {
  233. auto* pSptr_statistics =
  234. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Statistics>*>(
  235. jhandle);
  236. assert(pSptr_statistics != nullptr);
  237. auto str = pSptr_statistics->get()->ToString();
  238. return env->NewStringUTF(str.c_str());
  239. }