Skip to content

Commit 187b585

Browse files
committed
Add metrics for tracking futures
1 parent 71a996e commit 187b585

File tree

8 files changed

+163
-5
lines changed

8 files changed

+163
-5
lines changed

c_src/atom_names.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ ATOM_MAP(write);
143143
ATOM_MAP(retryable);
144144
ATOM_MAP(maybe_committed);
145145
ATOM_MAP(retryable_not_committed);
146+
147+
// Metrics
148+
ATOM_MAP(futures_created);
149+
ATOM_MAP(futures_destroyed);
150+
ATOM_MAP(futures_fired);
151+
ATOM_MAP(futures_read);

c_src/main.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "fdb.h"
1919

2020
#include "atoms.h"
21+
#include "metrics.h"
2122
#include "resources.h"
2223
#include "util.h"
2324

@@ -95,6 +96,8 @@ erlfdb_future_cb(FDBFuture* fdb_future, void* data)
9596

9697
enif_release_resource(future);
9798

99+
erlfdb_future_fired();
100+
98101
return;
99102
}
100103

@@ -159,6 +162,8 @@ erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureType ftype)
159162
// thread has a reference. If its 1 then only
160163
// Erlang has a reference.
161164

165+
erlfdb_future_created();
166+
162167
return T3(env, ATOM_erlfdb_future, ref, ret);
163168
}
164169

@@ -173,6 +178,8 @@ erlfdb_future_get_void(ErlNifEnv* env, ErlFDBFuture* f)
173178
return erlfdb_erlang_error(env, err);
174179
}
175180

181+
erlfdb_future_read();
182+
176183
return ATOM_ok;
177184
}
178185

@@ -191,6 +198,8 @@ erlfdb_future_get_int64(ErlNifEnv* env, ErlFDBFuture* f)
191198

192199
nif_res = fdb_res;
193200

201+
erlfdb_future_read();
202+
194203
return enif_make_int64(env, nif_res);
195204
}
196205

@@ -212,6 +221,8 @@ erlfdb_future_get_key(ErlNifEnv* env, ErlFDBFuture* f)
212221
buf = enif_make_new_binary(env, len, &ret);
213222
memcpy(buf, key, len);
214223

224+
erlfdb_future_read();
225+
215226
return ret;
216227
}
217228

@@ -238,6 +249,8 @@ erlfdb_future_get_value(ErlNifEnv* env, ErlFDBFuture* f)
238249
buf = enif_make_new_binary(env, len, &ret);
239250
memcpy(buf, val, len);
240251

252+
erlfdb_future_read();
253+
241254
return ret;
242255
}
243256

@@ -266,6 +279,8 @@ erlfdb_future_get_string_array(ErlNifEnv* env, ErlFDBFuture* f)
266279
ret = enif_make_list_cell(env, bin, ret);
267280
}
268281

282+
erlfdb_future_read();
283+
269284
return ret;
270285
}
271286

@@ -298,6 +313,8 @@ erlfdb_future_get_keyvalue_array(ErlNifEnv* env, ErlFDBFuture* f)
298313
ret = enif_make_list_cell(env, T2(env, key, val), ret);
299314
}
300315

316+
erlfdb_future_read();
317+
301318
if(more) {
302319
return T3(env, ret, enif_make_int(env, count), ATOM_true);
303320
} else {
@@ -2145,6 +2162,23 @@ erlfdb_error_predicate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
21452162
}
21462163
}
21472164

2165+
static ERL_NIF_TERM
2166+
erlfdb_get_metrics(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
2167+
{
2168+
ERL_NIF_TERM metrics[4];
2169+
2170+
ERL_NIF_TERM created = enif_make_uint64(env, erlfdb_num_futures_created());
2171+
ERL_NIF_TERM destroyed = enif_make_uint64(env, erlfdb_num_futures_destroyed());
2172+
ERL_NIF_TERM fired = enif_make_uint64(env, erlfdb_num_futures_fired());
2173+
ERL_NIF_TERM read = enif_make_uint64(env, erlfdb_num_futures_read());
2174+
2175+
metrics[0] = T2(env, ATOM_futures_created, created);
2176+
metrics[1] = T2(env, ATOM_futures_destroyed, destroyed);
2177+
metrics[2] = T2(env, ATOM_futures_fired, fired);
2178+
metrics[3] = T2(env, ATOM_futures_read, read);
2179+
2180+
return enif_make_list_from_array(env, metrics, 4);
2181+
}
21482182

21492183
#define NIF_FUNC(name, arity) {#name, arity, name}
21502184
static ErlNifFunc funcs[] =
@@ -2192,7 +2226,9 @@ static ErlNifFunc funcs[] =
21922226
NIF_FUNC(erlfdb_transaction_get_writes_allowed, 1),
21932227

21942228
NIF_FUNC(erlfdb_get_error, 1),
2195-
NIF_FUNC(erlfdb_error_predicate, 2)
2229+
NIF_FUNC(erlfdb_error_predicate, 2),
2230+
2231+
NIF_FUNC(erlfdb_get_metrics, 0)
21962232
};
21972233
#undef NIF_FUNC
21982234

c_src/metrics.cc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
2+
// use this file except in compliance with the License. You may obtain a copy of
3+
// the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
// License for the specific language governing permissions and limitations under
11+
// the License.
12+
13+
#include <atomic>
14+
15+
#define BEGIN_C extern "C" {
16+
#define END_C }
17+
18+
std::atomic<uint64_t> ERLFDB_FUTURES_CREATED{0};
19+
std::atomic<uint64_t> ERLFDB_FUTURES_DESTROYED{0};
20+
std::atomic<uint64_t> ERLFDB_FUTURES_FIRED{0};
21+
std::atomic<uint64_t> ERLFDB_FUTURES_READ{0};
22+
23+
BEGIN_C
24+
25+
26+
void
27+
erlfdb_future_created()
28+
{
29+
ERLFDB_FUTURES_CREATED++;
30+
}
31+
32+
void
33+
erlfdb_future_destroyed()
34+
{
35+
ERLFDB_FUTURES_DESTROYED++;
36+
}
37+
38+
void
39+
erlfdb_future_fired()
40+
{
41+
ERLFDB_FUTURES_FIRED++;
42+
}
43+
44+
void
45+
erlfdb_future_read()
46+
{
47+
ERLFDB_FUTURES_READ++;
48+
}
49+
50+
uint64_t
51+
erlfdb_num_futures_created()
52+
{
53+
return ERLFDB_FUTURES_CREATED;
54+
}
55+
56+
uint64_t
57+
erlfdb_num_futures_destroyed()
58+
{
59+
return ERLFDB_FUTURES_DESTROYED;
60+
}
61+
62+
uint64_t
63+
erlfdb_num_futures_fired()
64+
{
65+
return ERLFDB_FUTURES_FIRED;
66+
}
67+
68+
uint64_t
69+
erlfdb_num_futures_read()
70+
{
71+
return ERLFDB_FUTURES_READ;
72+
}
73+
74+
END_C

c_src/metrics.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
2+
// use this file except in compliance with the License. You may obtain a copy of
3+
// the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
// License for the specific language governing permissions and limitations under
11+
// the License.
12+
13+
#ifndef ERLFDB_METRICS_H
14+
#define ERLFDB_METRICS_H
15+
16+
#include <stdint.h>
17+
18+
void erlfdb_future_created();
19+
void erlfdb_future_destroyed();
20+
void erlfdb_future_fired();
21+
void erlfdb_future_read();
22+
23+
uint64_t erlfdb_num_futures_created();
24+
uint64_t erlfdb_num_futures_destroyed();
25+
uint64_t erlfdb_num_futures_fired();
26+
uint64_t erlfdb_num_futures_read();
27+
28+
#endif // Included metrics.h

c_src/resources.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// License for the specific language governing permissions and limitations under
1111
// the License.
1212

13+
#include "metrics.h"
1314
#include "resources.h"
1415

1516

@@ -74,6 +75,8 @@ erlfdb_future_dtor(ErlNifEnv* env, void* obj)
7475
if(f->msg_env != NULL) {
7576
enif_free_env(f->msg_env);
7677
}
78+
79+
erlfdb_future_destroyed();
7780
}
7881

7982

rebar.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{port_specs, [
2-
{"priv/erlfdb_nif.so", ["c_src/*.c"]}
2+
{"priv/erlfdb_nif.so", ["c_src/*.c", "c_src/*.cc"]}
33
]}.
44

55
{plugins, [rebar_gdb_plugin]}.
@@ -8,7 +8,7 @@
88
{"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)",
99
"CFLAGS", "$CFLAGS -Ic_src/ -g -Wall -Werror"},
1010
{"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)",
11-
"CXXFLAGS", "$CXXFLAGS -Ic_src/ -g -Wall -Werror"},
11+
"CXXFLAGS", "$CXXFLAGS -Ic_src/ -g -Wall -Werror -std=c++14"},
1212

1313
{"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)",
1414
"LDFLAGS", "$LDFLAGS -lfdb_c"}

src/erlfdb.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
on_error/2,
123123
error_predicate/2,
124124
get_last_error/0,
125-
get_error_string/1
125+
get_error_string/1,
126+
get_metrics/0
126127
]).
127128

128129

@@ -669,6 +670,10 @@ get_error_string(ErrorCode) when is_integer(ErrorCode) ->
669670
erlfdb_nif:get_error(ErrorCode).
670671

671672

673+
get_metrics() ->
674+
erlfdb_nif:get_metrics().
675+
676+
672677
clear_erlfdb_error() ->
673678
put(?ERLFDB_ERROR, undefined).
674679

src/erlfdb_nif.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
transaction_get_approximate_size/1,
5858

5959
get_error/1,
60-
error_predicate/2
60+
error_predicate/2,
61+
62+
get_metrics/0
6163
]).
6264

6365

@@ -449,6 +451,9 @@ get_error(Error) ->
449451
error_predicate(Predicate, Error) ->
450452
erlfdb_error_predicate(Predicate, Error).
451453

454+
-spec get_metrics() -> [{atom(), non_neg_integer()}].
455+
get_metrics() ->
456+
erlfdb_get_metrics().
452457

453458
-spec option_val_to_binary(binary() | integer()) -> binary().
454459
option_val_to_binary(Val) when is_binary(Val) ->
@@ -595,3 +600,4 @@ erlfdb_transaction_get_approximate_size(_Transaction) -> ?NOT_LOADED.
595600
% Misc
596601
erlfdb_get_error(_Error) -> ?NOT_LOADED.
597602
erlfdb_error_predicate(_Predicate, _Error) -> ?NOT_LOADED.
603+
erlfdb_get_metrics() -> ?NOT_LOADED.

0 commit comments

Comments
 (0)