Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kenlig committed Oct 19, 2024
1 parent f66e70d commit 5c4f296
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions runtime/src/bpf_map/userspace/var_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class var_size_hash_map_impl {
int map_get_next_key(const void *key, void *next_key);

int lookup_and_delete(const void *key, void *value_out);
uint32_t get_value_size() const
{
return _value_size;
}
};

} // namespace bpftime
Expand Down
68 changes: 68 additions & 0 deletions runtime/unit-test/maps/kernel_unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,71 @@ TEST_CASE("test_hashmap_percpu (kernel)")
REQUIRE((map.map_get_next_key(NULL, &next_key) < 0 && errno == ENOENT));
REQUIRE((map.map_get_next_key(&key, &next_key) < 0 && errno == ENOENT));
}

#define VALUE_SIZE 3
static inline var_size_hash_map_impl
helper_fill_hashmap(int max_entries, managed_shared_memory &mem)
{
int i, fd, ret;
long long key, value[VALUE_SIZE] = {};

var_size_hash_map_impl map(mem, sizeof(key), sizeof(value),
max_entries);

for (i = 0; i < max_entries; i++) {
key = i;
value[0] = key;
ret = map.elem_update(&key, value, BPF_NOEXIST);
REQUIRE((ret == 0 && "Unable to update hash map"));
}

return map;
}

Check warning on line 272 in runtime/unit-test/maps/kernel_unit_tests.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/unit-test/maps/kernel_unit_tests.cpp#L272

Added line #L272 was not covered by tests

TEST_CASE("test_hashmap_walk (kernel)")
{
shm_remove remover(SHM_NAME);
managed_shared_memory mem(boost::interprocess::create_only, SHM_NAME,
20 << 20);
int i, max_entries = 10000;
long long key, value[VALUE_SIZE], next_key;
bool next_key_valid = true;

auto map = helper_fill_hashmap(max_entries, mem);

const auto lookup_helper = [&](const void *key, void *value) -> long {
auto returned_value = map.elem_lookup(key);
if (!returned_value)
return -1;

Check warning on line 288 in runtime/unit-test/maps/kernel_unit_tests.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/unit-test/maps/kernel_unit_tests.cpp#L288

Added line #L288 was not covered by tests
memcpy(value, returned_value, map.get_value_size());
return 0;
};

for (i = 0; map.map_get_next_key(!i ? NULL : &key, &next_key) == 0;
i++) {
key = next_key;
REQUIRE(lookup_helper(&key, value) == 0);
}

REQUIRE(i == max_entries);

REQUIRE(map.map_get_next_key(NULL, &key) == 0);
for (i = 0; next_key_valid; i++) {
next_key_valid = map.map_get_next_key(&key, &next_key) == 0;
REQUIRE(lookup_helper(&key, value) == 0);
value[0]++;
REQUIRE(map.elem_update(&key, value, BPF_EXIST) == 0);
key = next_key;
}

REQUIRE(i == max_entries);

for (i = 0; map.map_get_next_key(!i ? NULL : &key, &next_key) == 0;
i++) {
key = next_key;
REQUIRE(lookup_helper(&key, value) == 0);
REQUIRE(value[0] - 1 == key);
}

REQUIRE(i == max_entries);
}

0 comments on commit 5c4f296

Please sign in to comment.