Skip to content

Commit bc5d939

Browse files
committed
Add tiledb_stats_is_enabled to C and C++ APIs
1 parent d766b3a commit bc5d939

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ if (TILEDB_CPP_API)
179179
src/cpp-integration-query-condition.cc
180180
src/unit-cppapi-schema.cc
181181
src/unit-cppapi-schema-evolution.cc
182+
src/unit-cppapi-stats.cc
182183
src/unit-cppapi-string-dims.cc
183184
src/unit-cppapi-subarray.cc
184185
src/unit-cppapi-type.cc

test/src/unit-cppapi-stats.cc

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file unit-cppapi-stats.cc
3+
*
4+
* @section LICENSE
5+
*
6+
* The MIT License
7+
*
8+
* @copyright Copyright (c) 2023-2024 TileDB Inc.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*
28+
* @section DESCRIPTION
29+
*
30+
* Tests the C++ API for stats related functions.
31+
*/
32+
33+
#include "test/support/src/stats.h"
34+
#include "tiledb/sm/cpp_api/stats.h"
35+
36+
#include <test/support/tdb_catch.h>
37+
38+
using namespace tiledb;
39+
40+
TEST_CASE("stats gathering default", "[stats]") {
41+
CHECK(!Stats::is_enabled());
42+
}
43+
44+
TEST_CASE("stats disabled, scoped enable", "[stats]") {
45+
CHECK(!Stats::is_enabled());
46+
{
47+
test::ScopedStats scoped;
48+
CHECK(Stats::is_enabled());
49+
}
50+
CHECK(!Stats::is_enabled());
51+
}
52+
53+
TEST_CASE("stats enabled, scoped enable", "[stats]") {
54+
CHECK(!Stats::is_enabled());
55+
56+
// outer scope disables when exiting
57+
{
58+
test::ScopedStats outer;
59+
CHECK(Stats::is_enabled());
60+
61+
// inner scope does not disable since stats was enabled when entering
62+
{
63+
test::ScopedStats inner;
64+
CHECK(Stats::is_enabled());
65+
}
66+
67+
CHECK(Stats::is_enabled());
68+
}
69+
70+
CHECK(!Stats::is_enabled());
71+
}

test/support/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ set(TILEDB_TEST_SUPPORT_SOURCES
4545
src/mem_helpers.h
4646
src/mem_helpers.cc
4747
src/serialization_wrappers.cc
48+
src/stats.cc
4849
src/temporary_local_directory.cc
4950
src/vfs_helpers.cc
5051
)

test/support/src/stats.cc

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file array_schema_helpers.cc
3+
*
4+
* @section LICENSE
5+
*
6+
* The MIT License
7+
*
8+
* @copyright Copyright (c) 2017-2024 TileDB, Inc.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*
28+
* @section DESCRIPTION
29+
*
30+
* This file defines some helpers for tests which use stats gathering.
31+
*/
32+
33+
#include "tiledb/sm/cpp_api/stats.h"
34+
#include "test/support/src/stats.h"
35+
36+
using namespace tiledb;
37+
38+
namespace tiledb::test {
39+
40+
ScopedStats::ScopedStats()
41+
: enabled_(Stats::is_enabled()) {
42+
Stats::enable();
43+
}
44+
45+
ScopedStats::~ScopedStats() {
46+
if (!enabled_) {
47+
Stats::disable();
48+
}
49+
}
50+
51+
} // namespace tiledb::test

test/support/src/stats.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @file stats.h
3+
*
4+
* @section LICENSE
5+
*
6+
* The MIT License
7+
*
8+
* @copyright Copyright (c) 2017-2024 TileDB, Inc.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*
28+
* @section DESCRIPTION
29+
*
30+
* This file declares some helpers for tests which use stats gathering.
31+
*/
32+
33+
#ifndef TILEDB_TEST_STATS_HELPERS_H
34+
#define TILEDB_TEST_STATS_HELPERS_H
35+
36+
namespace tiledb::test {
37+
38+
class ScopedStats {
39+
public:
40+
ScopedStats();
41+
~ScopedStats();
42+
43+
private:
44+
bool enabled_;
45+
};
46+
47+
} // namespace tiledb::test
48+
49+
#endif

tiledb/sm/c_api/tiledb.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,11 @@ int32_t tiledb_stats_disable() {
12661266
return TILEDB_OK;
12671267
}
12681268

1269+
int32_t tiledb_stats_is_enabled(uint8_t* enabled) {
1270+
*enabled = static_cast<int8_t>(tiledb::sm::stats::all_stats.enabled());
1271+
return TILEDB_OK;
1272+
}
1273+
12691274
int32_t tiledb_stats_reset() {
12701275
tiledb::sm::stats::all_stats.reset();
12711276
return TILEDB_OK;
@@ -3077,6 +3082,10 @@ CAPI_INTERFACE_NULL(stats_disable) {
30773082
return api_entry_plain<tiledb::api::tiledb_stats_disable>();
30783083
}
30793084

3085+
CAPI_INTERFACE(stats_is_enabled, uint8_t* enabled) {
3086+
return api_entry_plain<tiledb::api::tiledb_stats_is_enabled>(enabled);
3087+
}
3088+
30803089
CAPI_INTERFACE_NULL(stats_reset) {
30813090
return api_entry_plain<tiledb::api::tiledb_stats_reset>();
30823091
}

tiledb/sm/c_api/tiledb.h

+8
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,14 @@ TILEDB_EXPORT int32_t tiledb_stats_enable(void) TILEDB_NOEXCEPT;
14121412
*/
14131413
TILEDB_EXPORT int32_t tiledb_stats_disable(void) TILEDB_NOEXCEPT;
14141414

1415+
/**
1416+
* Returns whether internal statistics gathering is enabled.
1417+
*
1418+
* @param enabled Output argument, non-zero for enabled and zero for disabled.
1419+
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error
1420+
*/
1421+
TILEDB_EXPORT int32_t tiledb_stats_is_enabled(uint8_t* enabled) TILEDB_NOEXCEPT;
1422+
14151423
/**
14161424
* Reset all internal statistics counters to 0.
14171425
*

tiledb/sm/cpp_api/stats.h

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define TILEDB_CPP_API_STATS_H
3535

3636
#include "tiledb.h"
37+
#include "tiledb/sm/cpp_api/exception.h"
38+
39+
#include <string>
3740

3841
namespace tiledb {
3942

@@ -64,6 +67,12 @@ class Stats {
6467
check_error(tiledb_stats_disable(), "error disabling stats");
6568
}
6669

70+
static bool is_enabled() {
71+
uint8_t enabled;
72+
check_error(tiledb_stats_is_enabled(&enabled), "error checking stats");
73+
return static_cast<bool>(enabled);
74+
}
75+
6776
/** Reset all internal statistics counters to 0. */
6877
static void reset() {
6978
check_error(tiledb_stats_reset(), "error resetting stats");

0 commit comments

Comments
 (0)