Skip to content

vtysh: Add JSON output support for show memory - #20605

Merged
riw777 merged 3 commits into
FRRouting:masterfrom
opensourcerouting:feature/show_memory_json
Feb 10, 2026
Merged

vtysh: Add JSON output support for show memory#20605
riw777 merged 3 commits into
FRRouting:masterfrom
opensourcerouting:feature/show_memory_json

Conversation

@ton31337

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented Jan 27, 2026

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR adds JSON output support to the show memory command in both the library and vtysh layers.

Key Changes:

  • Created new qmem_walker_json() function in lib/lib_vty.c that transforms memory statistics into JSON format, mirroring the existing qmem_walker() for text output
  • Added JSON handling to the show_memory command definition to accept optional json parameter
  • Implemented show_memory_send() helper function in vtysh/vtysh.c to coordinate JSON output across multiple daemons
  • Updated vtysh command handler to properly route JSON requests through the new code path while preserving backward compatibility for non-JSON commands

Implementation Notes:

  • The JSON walker properly handles conditional compilation with HAVE_MALLOC_USABLE_SIZE guards
  • Memory types with zero max allocations are filtered out (matching text output behavior)
  • Variable-size memory types are represented as "size": "variable" in JSON
  • JSON output wraps daemon responses in a top-level object with daemon names as keys
  • Mallinfo statistics are not included in JSON output (only in text mode)

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation follows established patterns in the codebase, properly handles argument parsing, maintains backward compatibility, and includes appropriate conditional compilation guards. The code is clean, well-structured, and mirrors existing JSON implementations for similar commands.
  • No files require special attention

Important Files Changed

Filename Overview
lib/lib_vty.c Added JSON output support for memory statistics with new qmem_walker_json function that mirrors the existing text walker
vtysh/vtysh.c Extended show memory command to support JSON output with new show_memory_send helper function following existing patterns

Sequence Diagram

sequenceDiagram
    participant User
    participant vtysh as vtysh (CLI)
    participant daemon as Daemon (bgpd, zebra, etc.)
    participant lib_vty as lib_vty.c
    
    User->>vtysh: show memory [daemon] json
    
    alt JSON output requested
        vtysh->>vtysh: argv_find("json")
        vtysh->>vtysh: show_memory_send(daemon, true)
        vtysh->>User: Output "{"
        
        loop For each connected daemon
            vtysh->>vtysh: Check if daemon matches filter
            vtysh->>vtysh: Check if daemon is connected
            vtysh->>User: Output "daemon_name":
            vtysh->>daemon: vtysh_client_execute_name("do show memory json")
            daemon->>lib_vty: show_memory with json flag
            lib_vty->>lib_vty: use_json(argc, argv) returns true
            lib_vty->>lib_vty: json_object_new_object()
            lib_vty->>lib_vty: qmem_walk(qmem_walker_json, &jarg)
            
            loop For each memory group & type
                lib_vty->>lib_vty: qmem_walker_json()
                alt Memory group
                    lib_vty->>lib_vty: Create JSON array for group
                else Memory type
                    lib_vty->>lib_vty: Add memory stats to JSON
                end
            end
            
            lib_vty->>daemon: vty_json(vty, json)
            daemon->>vtysh: Return JSON output
            vtysh->>User: Output daemon's JSON data
        end
        
        vtysh->>User: Output "}"
    else Non-JSON output
        vtysh->>vtysh: show_one_daemon() or show_per_daemon()
        Note over vtysh,User: Existing flow unchanged
    end
Loading

Comment thread vtysh/vtysh.c Outdated
@ton31337
ton31337 force-pushed the feature/show_memory_json branch from 4e0f3c5 to e5bc9c8 Compare January 27, 2026 17:15
@Jafaral

Jafaral commented Jan 27, 2026

Copy link
Copy Markdown
Member

Not sure why Verify Source is complaining about a line that isn't part of this PR. https://github.com/FRRouting/frr/pull/20605/files#diff-223013206495788884d7c3f9d02f603f4ea7e4fbbc15e76b10f3df66996f64b2R219

Comment thread vtysh/vtysh.c Outdated
@ton31337
ton31337 force-pushed the feature/show_memory_json branch from e5bc9c8 to 3d7668f Compare January 28, 2026 16:41
@frrbot frrbot Bot added documentation tests Topotests, make check, etc labels Jan 28, 2026
@ton31337
ton31337 force-pushed the feature/show_memory_json branch from 3d7668f to 0eaa67c Compare January 28, 2026 16:42
Comment thread lib/lib_vty.c Outdated
Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
@ton31337
ton31337 force-pushed the feature/show_memory_json branch 2 times, most recently from 42f6200 to 42db15a Compare January 28, 2026 16:54
Comment thread lib/lib_vty.c Outdated
E.g.:

donatas# show memory bgpd json
{"bgpd":{
  "libfrr":[
    {
      "name":"Buffer",
      "currentAllocations":7,
      "size":24,
      "sizeVariable":false,
      "totalBytes":168,
      "maxAllocations":7,
      "maxBytes":168
    },
...

Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
@ton31337
ton31337 force-pushed the feature/show_memory_json branch from 42db15a to 69f7fbc Compare January 29, 2026 07:55

@mjstapp mjstapp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good to me!

@mjstapp

mjstapp commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

ci:rerun

@riw777 riw777 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@riw777
riw777 merged commit 3eb03a9 into FRRouting:master Feb 10, 2026
19 checks passed
@ton31337
ton31337 deleted the feature/show_memory_json branch February 10, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants