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