-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add valgrind memory leaks check (#584)
* added pub sub test for memory leaks * added valgrind test * format * ci fix * ci fix * add queryable-get memory leaks test * do not copy memory leaks script * fixed memory leak in handlers * docs update * format * format
- Loading branch information
1 parent
320faf6
commit 5a82ecf
Showing
14 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
||
function check_leaks { | ||
echo "Checking $1 for memory leaks" | ||
valgrind --leak-check=full --num-callers=50 --log-file="$1.leaks.log" $1 | ||
num_leaks=$(grep 'ERROR SUMMARY: [0-9]+' -Eo "$1.leaks.log" | grep '[0-9]+' -Eo) | ||
echo "Detected $num_leaks memory leaks" | ||
if (( num_leaks == 0 )) | ||
then | ||
return 0 | ||
else | ||
cat $1.leaks.log | ||
return -1 | ||
fi | ||
} | ||
|
||
check_leaks $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
|
||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "zenoh.h" | ||
|
||
#undef NDEBUG | ||
#include <assert.h> | ||
|
||
const char *PUB_KEY_EXPR = "test/valgrind/data"; | ||
const char *SUB_KEY_EXPR = "test/valgrind/**"; | ||
|
||
void data_handler(const z_loaned_sample_t *sample, void *context) { | ||
(void)context; | ||
z_view_string_t key_string; | ||
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string); | ||
|
||
z_owned_string_t payload_string; | ||
z_bytes_deserialize_into_string(z_sample_payload(sample), &payload_string); | ||
|
||
printf(">> [Subscriber] Received ('%.*s': '%.*s')\n", (int)z_string_len(z_loan(key_string)), | ||
z_string_data(z_loan(key_string)), (int)z_string_len(z_loan(payload_string)), | ||
z_string_data(z_loan(payload_string))); | ||
z_drop(z_move(payload_string)); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
printf("Declaring Publisher on %s\n", PUB_KEY_EXPR); | ||
|
||
z_owned_keyexpr_t pub_keyexpr; | ||
z_keyexpr_from_str(&pub_keyexpr, PUB_KEY_EXPR); | ||
|
||
z_owned_config_t pub_config; | ||
z_config_default(&pub_config); | ||
|
||
z_owned_session_t pub_session; | ||
z_open(&pub_session, z_move(pub_config)); | ||
|
||
z_owned_publisher_t publisher; | ||
z_declare_publisher(&publisher, z_loan(pub_session), z_loan(pub_keyexpr), NULL); | ||
|
||
printf("Declaring Subscriber on %s\n", SUB_KEY_EXPR); | ||
|
||
z_view_keyexpr_t sub_keyexpr; | ||
z_view_keyexpr_from_str(&sub_keyexpr, SUB_KEY_EXPR); | ||
|
||
z_owned_config_t sub_config; | ||
z_config_default(&sub_config); | ||
|
||
z_owned_session_t sub_session; | ||
z_open(&sub_session, z_move(sub_config)); | ||
|
||
z_owned_closure_sample_t callback; | ||
z_closure(&callback, data_handler, NULL, NULL); | ||
|
||
z_owned_subscriber_t subscriber; | ||
z_declare_subscriber(&subscriber, z_loan(sub_session), z_loan(sub_keyexpr), z_move(callback), NULL); | ||
|
||
z_sleep_s(1); | ||
|
||
char buf[32] = {0}; | ||
for (int i = 0; i < 5; ++i) { | ||
sprintf(buf, "data [%4d]", i); | ||
printf("Putting Data ('%s': '%s')...\n", PUB_KEY_EXPR, buf); | ||
z_publisher_put_options_t options; | ||
z_publisher_put_options_default(&options); | ||
|
||
z_owned_bytes_t payload; | ||
z_bytes_serialize_from_str(&payload, buf); | ||
|
||
z_publisher_put(z_loan(publisher), z_move(payload), &options); | ||
z_sleep_s(1); | ||
} | ||
|
||
z_undeclare_publisher(z_move(publisher)); | ||
z_undeclare_subscriber(z_move(subscriber)); | ||
z_close(z_move(pub_session)); | ||
z_close(z_move(sub_session)); | ||
z_drop(z_move(pub_keyexpr)); | ||
|
||
zc_stop_z_runtime(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
|
||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "zenoh.h" | ||
|
||
#undef NDEBUG | ||
#include <assert.h> | ||
|
||
const char *GET_KEY_EXPR = "test/valgrind/data"; | ||
const char *QUERYABLE_KEY_EXPR = "test/valgrind/**"; | ||
|
||
void query_handler(const z_loaned_query_t *query, void *context) { | ||
(void)context; | ||
z_view_string_t key_string; | ||
z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string); | ||
|
||
z_view_string_t params; | ||
z_query_parameters(query, ¶ms); | ||
|
||
const z_loaned_bytes_t *payload = z_query_payload(query); | ||
|
||
z_owned_string_t payload_string; | ||
z_bytes_deserialize_into_string(payload, &payload_string); | ||
|
||
printf(">> [Queryable ] Received Query '%.*s' with value '%.*s'\n", (int)z_string_len(z_loan(key_string)), | ||
z_string_data(z_loan(key_string)), (int)z_string_len(z_loan(payload_string)), | ||
z_string_data(z_loan(payload_string))); | ||
z_drop(z_move(payload_string)); | ||
z_query_reply_options_t options; | ||
z_query_reply_options_default(&options); | ||
|
||
z_owned_bytes_t reply_payload; | ||
z_bytes_clone(&reply_payload, payload); | ||
|
||
z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
printf("Declaring Queryable on %s\n", QUERYABLE_KEY_EXPR); | ||
|
||
z_owned_keyexpr_t queryable_keyexpr; | ||
z_keyexpr_from_str(&queryable_keyexpr, QUERYABLE_KEY_EXPR); | ||
|
||
z_owned_config_t queryable_config; | ||
z_config_default(&queryable_config); | ||
|
||
z_owned_session_t queryable_session; | ||
z_open(&queryable_session, z_move(queryable_config)); | ||
|
||
z_owned_closure_query_t callback; | ||
z_closure(&callback, query_handler, NULL, NULL); | ||
z_owned_queryable_t queryable; | ||
z_declare_queryable(&queryable, z_loan(queryable_session), z_loan(queryable_keyexpr), z_move(callback), NULL); | ||
|
||
z_view_keyexpr_t get_keyexpr; | ||
z_view_keyexpr_from_str(&get_keyexpr, GET_KEY_EXPR); | ||
|
||
z_owned_config_t get_config; | ||
z_config_default(&get_config); | ||
|
||
z_owned_session_t get_session; | ||
z_open(&get_session, z_move(get_config)); | ||
|
||
z_sleep_s(1); | ||
|
||
size_t received_replies = 0; | ||
char buf[32] = {0}; | ||
for (int i = 0; i < 5; ++i) { | ||
sprintf(buf, "data [%4d]", i); | ||
printf("Get with Data ('%s': '%s')...\n", GET_KEY_EXPR, buf); | ||
z_get_options_t options; | ||
z_get_options_default(&options); | ||
|
||
z_owned_bytes_t payload; | ||
z_bytes_serialize_from_str(&payload, buf); | ||
|
||
options.payload = z_move(payload); | ||
|
||
z_owned_fifo_handler_reply_t handler; | ||
z_owned_closure_reply_t closure; | ||
z_fifo_channel_reply_new(&closure, &handler, 16); | ||
|
||
z_get(z_loan(get_session), z_loan(get_keyexpr), "", z_move(closure), &options); | ||
|
||
z_owned_reply_t reply; | ||
while (z_recv(z_loan(handler), &reply) == Z_OK) { | ||
received_replies++; | ||
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); | ||
assert(sample != NULL); | ||
|
||
z_view_string_t key_str; | ||
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_str); | ||
|
||
z_owned_string_t reply_str; | ||
z_bytes_deserialize_into_string(z_sample_payload(sample), &reply_str); | ||
|
||
printf(">> Received ('%.*s': '%.*s')\n", (int)z_string_len(z_loan(key_str)), z_string_data(z_loan(key_str)), | ||
(int)z_string_len(z_loan(reply_str)), z_string_data(z_loan(reply_str))); | ||
z_drop(z_move(reply_str)); | ||
z_drop(z_move(reply)); | ||
} | ||
|
||
z_drop(z_move(handler)); | ||
z_sleep_s(1); | ||
} | ||
assert(received_replies == 5); | ||
|
||
z_undeclare_queryable(z_move(queryable)); | ||
z_close(z_move(get_session)); | ||
z_close(z_move(queryable_session)); | ||
z_drop(z_move(queryable_keyexpr)); | ||
|
||
zc_stop_z_runtime(); | ||
|
||
return 0; | ||
} |