json: collapse set element commands from parser
Pablo Neira Ayuso
maintainerPATCH 1/1 json: collapse set element commands from parser
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}
Add CTX_F_COLLAPSED flag to report that command has been collapsed.
This patch reduces memory consumption by ~32% this case.
---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add a json_t pointer to struct location to relate expressions and json objects.
src/parser_json.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
src/parser_json.c +16 -0
@@ -18,6 +18,7 @@#include <netlink.h> #include <parser.h> #include <rule.h> +#include <cmd.h> #include <sctp_chunk.h> #include <socket.h>
@@ -49,6 +50,7 @@#define CTX_F_SES (1 << 6)/* set_elem_expr_stmt */ #define CTX_F_MAP (1 << 7)/* LHS of map_expr */ #define CTX_F_CONCAT (1 << 8)/* inside concat_expr */ +#define CTX_F_COLLAPSED (1 << 9)struct json_ctx {struct nft_ctx *nft;
@@ -3490,6 +3492,15 @@ static struct cmd *json_parse_cmd_add_element(struct json_ctx *ctx,handle_free (&h);return NULL ; } + +if ((op ==CMD_CREATE || op ==CMD_ADD ) && +nft_cmd_collapse_elems (op, ctx->cmds, &h, expr)) { +handle_free (&h); +expr_free (expr); + ctx->flags |=CTX_F_COLLAPSED ; +return NULL ; + } +return cmd_alloc (op, cmd_obj, &h, int_loc, expr); }
@@ -4319,6 +4330,11 @@ static int __json_parse(struct json_ctx *ctx) cmd =json_parse_cmd (ctx, value);if (!cmd) { +if (ctx->flags &CTX_F_COLLAPSED ) { + ctx->flags &= ~CTX_F_COLLAPSED ; +continue ; + } +json_error (ctx,"Parsing command array at index %zd failed." , index);return -1 ; }
Signature · 1 line
2.30.2
Quoted 7 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}
Quoted 7 lines
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
Thanks Pablo!
Phil Sutter
reviewerQuoted 6 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}
Thanks for the fix!
Quoted 1 line
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.
I had come up with a similar solution (but did not find time to submit it last week). My solution to the "what to return" problem was to introduce a 'static struct cmd cmd_nop' and return its address. Your flag way is fine, too from my PoV.
Quoted 11 lines
This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add ajson_tpointer to struct location to relate expressions and json objects.
I can have a look at mem use if I find spare time (TM).
We already record links between struct cmd and json_t objects for echo mode (and only then). The problem with error reporting in my opinion is the lack of location data in json_t. You might remember, I tried to extend libjansson to our needs but my MR[1] is being ignored for more than a year now. Should we just ship an extended copy in nftables?
Cheers, Phil
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 8 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}
Quoted 8 lines
Thanks for the fix!
Quoted 1 line
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.I had come up with a similar solution (but did not find time to submit it last week). My solution to the "what to return" problem was to introduce a 'static struct cmd
cmd_nop' and return its address. Your flag way is fine, too from my PoV.
OK, I'm going to push it out then.
Quoted 12 lines
Quoted 11 lines
This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add ajson_tpointer to struct location to relate expressions and json objects.
Quoted 1 line
I can have a look at mem use if I find spare time (TM).
I understand, that is always the issue.
Quoted 5 lines
We already record links between struct cmd and
json_tobjects for echo mode (and only then). The problem with error reporting in my opinion is the lack of location data injson_t. You might remember, I tried to extend libjansson to our needs but my MR[1] is being ignored for more than a year now. Should we just ship an extended copy in nftables?
Do you still have the link with your proposal around? I don't find it in my notes anymore.
IIRC the rejection came from concerns about increasing memory usage for our specific usecase, that was an extra pointer to store location, correct?
Thanks.
Phil Sutter
reviewerQuoted 34 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 8 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}Quoted 8 lines
Thanks for the fix!
Quoted 1 line
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.I had come up with a similar solution (but did not find time to submit it last week). My solution to the "what to return" problem was to introduce a 'static struct cmd
cmd_nop' and return its address. Your flag way is fine, too from my PoV.OK, I'm going to push it out then.
Quoted 12 lines
Quoted 11 lines
This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add ajson_tpointer to struct location to relate expressions and json objects.
Quoted 3 lines
Quoted 1 line
I can have a look at mem use if I find spare time (TM).
I understand, that is always the issue.
It's on my TODO at least, let's hope for the best.
Quoted 8 lines
Quoted 5 lines
We already record links between struct cmd and
json_tobjects for echo mode (and only then). The problem with error reporting in my opinion is the lack of location data injson_t. You might remember, I tried to extend libjansson to our needs but my MR[1] is being ignored for more than a year now. Should we just ship an extended copy in nftables?Do you still have the link with your proposal around? I don't find it in my notes anymore.
Ah, prolly forgot to resolve that [1] above:
https://github.com/akheron/jansson/pull/662
Quoted 3 lines
IIRC the rejection came from concerns about increasing memory usage for our specific usecase, that was an extra pointer to store location, correct?
That and lack of interest in the feature in general. See the linked !461 for some feedback. The uncommented implementation in !662 hides everything behind a decoder flag and avoids any memory overhead if not enabled. The only remaining concern I can't address is: "we don't see this as an important feature that should be included in Jansson."
Cheers, Phil
Pablo Neira Ayuso
maintainerimproving json error reporting [was Re: [PATCH nft] json: collapse set element commands from parser]
Hi Phil,
Quoted 35 lines · Phil Sutter
Quoted 34 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 8 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}Quoted 8 lines
Thanks for the fix!
Quoted 1 line
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.I had come up with a similar solution (but did not find time to submit it last week). My solution to the "what to return" problem was to introduce a 'static struct cmd
cmd_nop' and return its address. Your flag way is fine, too from my PoV.OK, I'm going to push it out then.
Quoted 12 lines
Quoted 11 lines
This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add ajson_tpointer to struct location to relate expressions and json objects.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I can have a look at mem use if I find spare time (TM).
I understand, that is always the issue.
It's on my TODO at least, let's hope for the best.
Thanks, we are already discussing this in a different thread.
Quoted 17 lines
Quoted 8 lines
Quoted 5 lines
We already record links between struct cmd and
json_tobjects for echo mode (and only then). The problem with error reporting in my opinion is the lack of location data injson_t. You might remember, I tried to extend libjansson to our needs but my MR[1] is being ignored for more than a year now. Should we just ship an extended copy in nftables?Do you still have the link with your proposal around? I don't find it in my notes anymore.
Ah, prolly forgot to resolve that [1] above:
https://github.com/akheron/jansson/pull/662
Quoted 3 lines
IIRC the rejection came from concerns about increasing memory usage for our specific usecase, that was an extra pointer to store location, correct?
Quoted 5 lines
That and lack of interest in the feature in general. See the linked !461 for some feedback. The uncommented implementation in !662 hides everything behind a decoder flag and avoids any memory overhead if not enabled. The only remaining concern I can't address is: "we don't see this as an important feature that should be included in Jansson."
Your changeset for jansson look relative small, coding style look also correct, and you have addressed the memory consumption issue.
It is a pity, I wonder what people do when json in this case? They have to scavenge in the json soup to find for non-syntactic errors as we have to do with it?
Having said this, I like jansson implementation, it is neat.
Caching a copy of libjansson in the nftables tree does not look like a way to go IMO.
Is there a chance to poke them again / refresh your pull request?
Otherwise, we will have to search for alternative path to improve the existing situation.
Thanks!
Phil Sutter
reviewerRe: improving json error reporting [was Re: [PATCH nft] json: collapse set element commands from parser]
Quoted 76 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 35 lines · Phil Sutter
Quoted 34 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 8 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Update json parser to collapse {add,create} element commands to reduce memory consumption in the case of large sets defined by one element per command:
{"nftables": [{"add": {"element": {"family": "ip", "table": "x", "name":
"y", "elem": [{"set": ["1.1.0.0"]}]}}},...]}Quoted 8 lines
Thanks for the fix!
Quoted 1 line
Add
CTX_F_COLLAPSEDflag to report that command has been collapsed.I had come up with a similar solution (but did not find time to submit it last week). My solution to the "what to return" problem was to introduce a 'static struct cmd
cmd_nop' and return its address. Your flag way is fine, too from my PoV.OK, I'm going to push it out then.
Quoted 12 lines
Quoted 11 lines
This patch reduces memory consumption by ~32% this case.
Fixes 20f1c60ac8c8 ("src: collapse set element commands from parser")Reported-by Eric Garver <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>---
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add ajson_tpointer to struct location to relate expressions and json objects.Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I can have a look at mem use if I find spare time (TM).
I understand, that is always the issue.
It's on my TODO at least, let's hope for the best.
Thanks, we are already discussing this in a different thread.
Quoted 17 lines
Quoted 8 lines
Quoted 5 lines
We already record links between struct cmd and
json_tobjects for echo mode (and only then). The problem with error reporting in my opinion is the lack of location data injson_t. You might remember, I tried to extend libjansson to our needs but my MR[1] is being ignored for more than a year now. Should we just ship an extended copy in nftables?Do you still have the link with your proposal around? I don't find it in my notes anymore.
Ah, prolly forgot to resolve that [1] above:
https://github.com/akheron/jansson/pull/662
Quoted 3 lines
IIRC the rejection came from concerns about increasing memory usage for our specific usecase, that was an extra pointer to store location, correct?
Quoted 5 lines
That and lack of interest in the feature in general. See the linked !461 for some feedback. The uncommented implementation in !662 hides everything behind a decoder flag and avoids any memory overhead if not enabled. The only remaining concern I can't address is: "we don't see this as an important feature that should be included in Jansson."
Your changeset for jansson look relative small, coding style look also correct, and you have addressed the memory consumption issue.
It is a pity, I wonder what people do when json in this case? They have to scavenge in the json soup to find for non-syntactic errors as we have to do with it?
Quoted 6 lines
Having said this, I like jansson implementation, it is neat.
Caching a copy of libjansson in the nftables tree does not look like a way to go IMO.
Is there a chance to poke them again / refresh your pull request?
I rebased the merge request and added a comment. Let's see if someone reacts.
Quoted 2 lines
Otherwise, we will have to search for alternative path to improve the existing situation.
If they don't accept, I see no way other than forking their lib or reimplementing functionality in another way.
Cheers, Phil
Phil Sutter
reviewerHi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add a
json_tpointer to struct location to relate expressions and json objects.
I can't quite reproduce this. When restoring a ruleset with ~12.7k elements in individual standard syntax commands, valgrind prints:
| HEAP SUMMARY: | in use at exit: 59,802 bytes in 582 blocks | total heap usage: 954,970 allocs, | 954,388 frees, | 18,300,874 bytes allocated
Repeating the same in JSON syntax, I get:
| HEAP SUMMARY: | in use at exit: 61,592 bytes in 647 blocks | total heap usage: 1,200,164 allocs, | 1,199,517 frees, | 38,612,257 bytes allocated
So this is 38MB vs 18MB? At least far from the mentioned 5 times. Would you mind sharing how you got to that number?
Please kindly find my reproducers attached for reference.
Thanks, Phil
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 26 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add a
json_tpointer to struct location to relate expressions and json objects.I can't quite reproduce this. When restoring a ruleset with ~12.7k elements in individual standard syntax commands, valgrind prints:
| HEAP SUMMARY: | in use at exit: 59,802 bytes in 582 blocks | total heap usage: 954,970 allocs, | 954,388 frees, | 18,300,874 bytes allocatedRepeating the same in JSON syntax, I get:
| HEAP SUMMARY: | in use at exit: 61,592 bytes in 647 blocks | total heap usage: 1,200,164 allocs, | 1,199,517 frees, | 38,612,257 bytes allocated
Quoted 4 lines
So this is 38MB vs 18MB? At least far from the mentioned 5 times. Would you mind sharing how you got to that number?
Please kindly find my reproducers attached for reference.
I am using valgrind --tool=massif to measure memory consumption in userspace.
I used these two files:
- set-init.json-nft, to create the table and set.
- set-65535.nft-json, to create a small set with 64K elements.
then I run:
valgrind --tool=massif nft -f set-65535.nft-json
there is a tool:
ms_print massif.out.XYZ
At "peak time" in heap memory consumption, I can see 60% is consumed in json objects.
I am looking at the commands and expressions to reduce memory consumption there. The result of that work will also help json support.
Pablo Neira Ayuso
maintainerQuoted 34 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 26 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add a
json_tpointer to struct location to relate expressions and json objects.I can't quite reproduce this. When restoring a ruleset with ~12.7k elements in individual standard syntax commands, valgrind prints:
| HEAP SUMMARY: | in use at exit: 59,802 bytes in 582 blocks | total heap usage: 954,970 allocs, | 954,388 frees, | 18,300,874 bytes allocatedRepeating the same in JSON syntax, I get:
| HEAP SUMMARY: | in use at exit: 61,592 bytes in 647 blocks | total heap usage: 1,200,164 allocs, | 1,199,517 frees, | 38,612,257 bytes allocatedQuoted 4 lines
So this is 38MB vs 18MB? At least far from the mentioned 5 times. Would you mind sharing how you got to that number?
Please kindly find my reproducers attached for reference.
Quoted 7 lines
I am using valgrind --tool=massif to measure memory consumption in userspace.
I used these two files:
- set-init.json-nft, to create the table and set.
- set-65535.nft-json, to create a small set with 64K elements.
Forgot attachments.
Quoted 6 lines
then I run:
valgrind --tool=massif nft -f set-65535.nft-json
there is a tool:
Quoted 9 lines
ms_printmassif.out.XYZAt "peak time" in heap memory consumption, I can see 60% is consumed in json objects.
I am looking at the commands and expressions to reduce memory consumption there. The result of that work will also help json support.
2 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Phil Sutter
reviewerQuoted 42 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 26 lines · Phil Sutter
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Side note: While profiling, I can still see lots json objects, this results in memory consumption that is 5 times than native representation. Error reporting is also lagging behind, it should be possible to add a
json_tpointer to struct location to relate expressions and json objects.I can't quite reproduce this. When restoring a ruleset with ~12.7k elements in individual standard syntax commands, valgrind prints:
| HEAP SUMMARY: | in use at exit: 59,802 bytes in 582 blocks | total heap usage: 954,970 allocs, | 954,388 frees, | 18,300,874 bytes allocatedRepeating the same in JSON syntax, I get:
| HEAP SUMMARY: | in use at exit: 61,592 bytes in 647 blocks | total heap usage: 1,200,164 allocs, | 1,199,517 frees, | 38,612,257 bytes allocatedQuoted 4 lines
So this is 38MB vs 18MB? At least far from the mentioned 5 times. Would you mind sharing how you got to that number?
Please kindly find my reproducers attached for reference.
I am using valgrind --tool=massif to measure memory consumption in userspace.
I used these two files:
- set-init.json-nft, to create the table and set.
- set-65535.nft-json, to create a small set with 64K elements.
Quoted 7 lines
then I run:
valgrind --tool=massif nft -f set-65535.nft-json
there is a tool:
ms_printmassif.out.XYZ
Thanks! I see it now. Interestingly, I had tried feeding the ruleset on stdin and that makes standard syntax use more memory, as well. With the rulesets being read from a file, standard syntax indeed requires just 7MB while JSON uses 35MB.
Quoted 2 lines
At "peak time" in heap memory consumption, I can see 60% is consumed in json objects.
The problem with jansson in that regard is that it parses the whole thing recursively. In theory it would be possible to parse just the outer object and continue parsing array elements by the time they are accessed.
Interestingly, I managed to reduce memory consumption by 30% by inserting a json_decref() call here:
| @@ -3496,6 +3498,7 @@ static struct cmd *json_parse_cmd_add_element(struct json_ctx *ctx,
| h.set.name = xstrdup(h.set.name);
|
| expr = json_parse_set_expr(ctx, "elem", tmp);
| + json_decref(tmp);
| if (!expr) {
| json_error(ctx, "Invalid set.");
| handle_free(&h);This does not fix a memleak, though: 'tmp' is assigned by a call to json_unpack(... "s:o" ...) and thus does not have its reference incremented. So AIUI, we're causing parts of the JSON object tree to be freed and later accesses are problematic: e.g. --echo mode will abort with "corrupted double-linked list" error.
Quoted 3 lines
I am looking at the commands and expressions to reduce memory consumption there. The result of that work will also help json support.
Cheers, Phil