Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory tracking for Domain. #4758

Merged
merged 16 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/src/unit-Reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ TEST_CASE_METHOD(
auto d2{make_shared<Dimension>(HERE(), "d2", Datatype::INT32)};
CHECK(d2->set_domain(&domain_vec[2]).ok());
CHECK(d2->set_tile_extent(&tile_extents_vec[1]).ok());
auto dom{make_shared<Domain>(HERE())};
auto dom{make_shared<Domain>(HERE(), tracker_)};
CHECK(dom->add_dimension(d1).ok());
CHECK(dom->add_dimension(d2).ok());

Expand Down
9 changes: 6 additions & 3 deletions test/src/unit-capi-array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ using namespace tiledb::common;
using namespace tiledb::sm;

struct ArrayFx {
// The memory tracker
shared_ptr<tiledb::sm::MemoryTracker> memory_tracker_;

// TileDB context
tiledb_ctx_t* ctx_;
tiledb_vfs_t* vfs_;
Expand Down Expand Up @@ -116,7 +119,8 @@ static const std::string test_ca_file =
std::string(TILEDB_TEST_INPUTS_DIR) + "/test_certs/public.crt";

ArrayFx::ArrayFx()
: fs_vec_(vfs_test_get_fs_vec()) {
: memory_tracker_(tiledb::test::create_test_memory_tracker())
, fs_vec_(vfs_test_get_fs_vec()) {
// Initialize vfs test
REQUIRE(vfs_test_init(fs_vec_, &ctx_, &vfs_).ok());
}
Expand Down Expand Up @@ -2586,13 +2590,12 @@ TEST_CASE_METHOD(
1,
&buff);
REQUIRE(rc == TILEDB_OK);
auto memory_tracker = ctx_->resources().create_memory_tracker();
auto st = tiledb::sm::serialization::array_deserialize(
array->array_.get(),
tiledb::sm::SerializationType::CAPNP,
buff->buffer(),
ctx_->storage_manager(),
memory_tracker);
memory_tracker_);
REQUIRE(st.ok());

// 6. Server: Close array and clean up
Expand Down
3 changes: 2 additions & 1 deletion test/src/unit-cppapi-schema-evolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ TEST_CASE(
int range[2] = {0, 1000};
throw_if_not_ok(dim->set_domain(range));

auto dom = make_shared<tiledb::sm::Domain>(HERE());
auto dom = make_shared<tiledb::sm::Domain>(
HERE(), tiledb::test::get_test_memory_tracker());
throw_if_not_ok(dom->add_dimension(dim));
throw_if_not_ok(schema->set_domain(dom));

Expand Down
10 changes: 5 additions & 5 deletions test/src/unit-enumerations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ struct EnumerationFx {

void rm_array();

shared_ptr<MemoryTracker> memory_tracker_;
URI uri_;
Config cfg_;
Context ctx_;
EncryptionKey enc_key_;
shared_ptr<MemoryTracker> memory_tracker_;
};

template <typename T>
Expand Down Expand Up @@ -2463,9 +2463,9 @@ struct TypeParams {
};

EnumerationFx::EnumerationFx()
: uri_("enumeration_test_array")
, ctx_(cfg_)
, memory_tracker_(tiledb::test::create_test_memory_tracker()) {
: memory_tracker_(tiledb::test::create_test_memory_tracker())
, uri_("enumeration_test_array")
, ctx_(cfg_) {
rm_array();
throw_if_not_ok(enc_key_.set_key(EncryptionType::NO_ENCRYPTION, nullptr, 0));
memory_tracker_ = tiledb::test::create_test_memory_tracker();
Expand Down Expand Up @@ -2758,7 +2758,7 @@ shared_ptr<ArraySchema> EnumerationFx::create_schema() {
int range[2] = {0, 1000};
throw_if_not_ok(dim->set_domain(range));

auto dom = make_shared<Domain>(HERE());
auto dom = make_shared<Domain>(HERE(), memory_tracker_);
throw_if_not_ok(dom->add_dimension(dim));
throw_if_not_ok(schema->set_domain(dom));

Expand Down
26 changes: 13 additions & 13 deletions test/src/unit-request-handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct RequestHandlerFx {

shared_ptr<Array> get_array(QueryType type);

shared_ptr<MemoryTracker> memory_tracker_;
URI uri_;
Config cfg_;
Context ctx_;
Expand Down Expand Up @@ -96,12 +97,12 @@ struct HandleConsolidationPlanRequestFx : RequestHandlerFx {
}

virtual shared_ptr<ArraySchema> create_schema() override {
auto schema = make_shared<ArraySchema>(
HERE(), ArrayType::SPARSE, tiledb::test::create_test_memory_tracker());
auto schema =
make_shared<ArraySchema>(HERE(), ArrayType::SPARSE, memory_tracker_);
auto dim = make_shared<Dimension>(HERE(), "dim1", Datatype::INT32);
int range[2] = {0, 1000};
throw_if_not_ok(dim->set_domain(range));
auto dom = make_shared<Domain>(HERE());
auto dom = make_shared<Domain>(HERE(), memory_tracker_);
throw_if_not_ok(dom->add_dimension(dim));
throw_if_not_ok(schema->set_domain(dom));
return schema;
Expand Down Expand Up @@ -336,7 +337,8 @@ TEST_CASE_METHOD(
/* ********************************* */

RequestHandlerFx::RequestHandlerFx(const std::string uri)
: uri_(uri)
: memory_tracker_(tiledb::test::create_test_memory_tracker())
, uri_(uri)
, ctx_(cfg_) {
delete_array();
throw_if_not_ok(enc_key_.set_key(EncryptionType::NO_ENCRYPTION, nullptr, 0));
Expand Down Expand Up @@ -398,13 +400,13 @@ HandleLoadArraySchemaRequestFx::create_string_enumeration(

shared_ptr<ArraySchema> HandleLoadArraySchemaRequestFx::create_schema() {
// Create a schema to serialize
auto schema = make_shared<ArraySchema>(
HERE(), ArrayType::SPARSE, tiledb::test::create_test_memory_tracker());
auto schema =
make_shared<ArraySchema>(HERE(), ArrayType::SPARSE, memory_tracker_);
auto dim = make_shared<Dimension>(HERE(), "dim1", Datatype::INT32);
int range[2] = {0, 1000};
throw_if_not_ok(dim->set_domain(range));

auto dom = make_shared<Domain>(HERE());
auto dom = make_shared<Domain>(HERE(), memory_tracker_);
throw_if_not_ok(dom->add_dimension(dim));
throw_if_not_ok(schema->set_domain(dom));

Expand Down Expand Up @@ -440,15 +442,13 @@ shared_ptr<ArraySchema> HandleLoadArraySchemaRequestFx::call_handler(
resp_buf);
REQUIRE(rval == TILEDB_OK);

auto memory_tracker =
ctx.ptr()->context().resources().create_memory_tracker();
return serialization::deserialize_load_array_schema_response(
stype, resp_buf->buffer(), memory_tracker);
stype, resp_buf->buffer(), memory_tracker_);
}

shared_ptr<ArraySchema> HandleQueryPlanRequestFx::create_schema() {
auto schema = make_shared<ArraySchema>(
HERE(), ArrayType::DENSE, tiledb::test::create_test_memory_tracker());
auto schema =
make_shared<ArraySchema>(HERE(), ArrayType::DENSE, memory_tracker_);
schema->set_capacity(10000);
throw_if_not_ok(schema->set_cell_order(Layout::ROW_MAJOR));
throw_if_not_ok(schema->set_tile_order(Layout::ROW_MAJOR));
Expand All @@ -459,7 +459,7 @@ shared_ptr<ArraySchema> HandleQueryPlanRequestFx::create_schema() {
auto dim2 = make_shared<Dimension>(HERE(), "dim2", Datatype::INT32);
throw_if_not_ok(dim2->set_domain(&dim_domain[2]));

auto dom = make_shared<Domain>(HERE());
auto dom = make_shared<Domain>(HERE(), memory_tracker_);
throw_if_not_ok(dom->add_dimension(dim1));
throw_if_not_ok(dom->add_dimension(dim2));
throw_if_not_ok(schema->set_domain(dom));
Expand Down
11 changes: 8 additions & 3 deletions tiledb/api/c_api/domain/domain_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
#include "../dimension/dimension_api_internal.h"
#include "domain_api_external.h"
#include "domain_api_internal.h"
#include "tiledb/common/memory_tracker.h"

namespace tiledb::api {

int32_t tiledb_domain_alloc(tiledb_domain_handle_t** domain) {
int32_t tiledb_domain_alloc(
tiledb_ctx_t* ctx, tiledb_domain_handle_t** domain) {
ensure_output_pointer_is_valid(domain);
*domain = tiledb_domain_handle_t::make_handle();
auto memory_tracker = ctx->resources().create_memory_tracker();
memory_tracker->set_type(tiledb::sm::MemoryTrackerType::ARRAY_CREATE);
*domain = tiledb_domain_handle_t::make_handle(memory_tracker);
return TILEDB_OK;
}

Expand Down Expand Up @@ -147,7 +151,8 @@ int32_t tiledb_domain_dump(const tiledb_domain_t* domain, FILE* out) {
using tiledb::api::api_entry_context;

CAPI_INTERFACE(domain_alloc, tiledb_ctx_t* ctx, tiledb_domain_t** domain) {
return api_entry_context<tiledb::api::tiledb_domain_alloc>(ctx, domain);
return tiledb::api::api_entry_with_context<tiledb::api::tiledb_domain_alloc>(
ctx, domain);
}

CAPI_INTERFACE_VOID(domain_free, tiledb_domain_t** domain) {
Expand Down
9 changes: 7 additions & 2 deletions tiledb/api/c_api/domain/domain_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include "tiledb/api/c_api_support/handle/handle.h"
#include "tiledb/sm/array_schema/domain.h"

namespace tiledb::sm {
class MemoryTracker;
}

struct tiledb_domain_handle_t
: public tiledb::api::CAPIHandle<tiledb_domain_handle_t> {
private:
Expand All @@ -54,8 +58,9 @@ struct tiledb_domain_handle_t
* `class Domain` is principally a container for `Dimension` objects. Domain
* handles are first constructed as empty containers.
*/
tiledb_domain_handle_t()
: domain_{make_shared<tiledb::sm::Domain>(HERE())} {
explicit tiledb_domain_handle_t(
shared_ptr<tiledb::sm::MemoryTracker> memory_tracker)
: domain_{make_shared<tiledb::sm::Domain>(HERE(), memory_tracker)} {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "../domain_api_internal.h"

int main() {
// Domain is easy to deal with because it has a default constructor.
tiledb_domain_handle_t x{};
tiledb_domain_handle_t x{shared_ptr<tiledb::sm::Domain>()};
return 0;
}
1 change: 1 addition & 0 deletions tiledb/common/memory_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ enum class MemoryType {
ATTRIBUTES,
DIMENSION_LABELS,
DIMENSIONS,
DOMAINS,
shaunrd0 marked this conversation as resolved.
Show resolved Hide resolved
TILE_SUMS,
TILE_WRITER_DATA
};
Expand Down
13 changes: 10 additions & 3 deletions tiledb/sm/array/test/unit_consistency.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "../array.h"
#include "../consistency.h"
#include "test/support/src/mem_helpers.h"
#include "tiledb/sm/array_schema/dimension.h"
#include "tiledb/sm/enums/array_type.h"
#include "tiledb/sm/enums/encryption_type.h"
Expand All @@ -63,8 +64,14 @@ using array_entry = std::tuple<Array&, const QueryType>;
using entry_type = std::multimap<const URI, array_entry>::const_iterator;

class WhiteboxConsistencyController : public ConsistencyController {
/** The memory tracker to use for the fixture */
shared_ptr<MemoryTracker> memory_tracker_;

public:
WhiteboxConsistencyController() = default;
WhiteboxConsistencyController()
: memory_tracker_(tiledb::test::create_test_memory_tracker()) {
}

~WhiteboxConsistencyController() = default;

entry_type register_array(
Expand Down Expand Up @@ -103,8 +110,8 @@ class WhiteboxConsistencyController : public ConsistencyController {
throw_if_not_ok(dim->set_tile_extent(&tile_extent));

std::vector<shared_ptr<Dimension>> dims = {dim};
shared_ptr<Domain> domain =
make_shared<Domain>(HERE(), Layout::ROW_MAJOR, dims, Layout::ROW_MAJOR);
shared_ptr<Domain> domain = make_shared<Domain>(
HERE(), Layout::ROW_MAJOR, dims, Layout::ROW_MAJOR, memory_tracker_);

// Create the ArraySchema
shared_ptr<ArraySchema> schema = make_shared<ArraySchema>(
Expand Down
7 changes: 6 additions & 1 deletion tiledb/sm/array_schema/array_schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,12 @@ shared_ptr<ArraySchema> ArraySchema::deserialize(
// Note: Security validation delegated to invoked API
// #TODO Add security validation
auto domain{Domain::deserialize(
deserializer, version, cell_order, tile_order, coords_filters)};
deserializer,
version,
cell_order,
tile_order,
coords_filters,
memory_tracker)};

// Load attributes
// Note: Security validation delegated to invoked API
Expand Down
1 change: 1 addition & 0 deletions tiledb/sm/array_schema/array_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ class ArraySchema {
*
* @param deserializer The deserializer to deserialize from.
* @param uri The uri of the Array.
* @param memory_tracker The memory tracker to use.
* @return A new ArraySchema.
*/
static shared_ptr<ArraySchema> deserialize(
Expand Down
7 changes: 6 additions & 1 deletion tiledb/sm/array_schema/dimension_label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "tiledb/sm/array_schema/dimension_label.h"
#include "tiledb/common/common.h"
#include "tiledb/common/memory_tracker.h"
#include "tiledb/sm/array_schema/array_schema.h"
#include "tiledb/sm/array_schema/dimension.h"
#include "tiledb/sm/array_schema/domain.h"
Expand Down Expand Up @@ -176,7 +177,11 @@ DimensionLabel::DimensionLabel(
throw_if_not_ok(
index_dims.back()->set_tile_extent(dim->tile_extent().data()));
throw_if_not_ok(schema_->set_domain(make_shared<Domain>(
HERE(), Layout::ROW_MAJOR, index_dims, Layout::ROW_MAJOR)));
HERE(),
Layout::ROW_MAJOR,
index_dims,
Layout::ROW_MAJOR,
memory_tracker)));

// Create and set dimension label attribute.
auto label_attr = make_shared<Attribute>(
Expand Down
Loading
Loading