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