Skip to content

Move user-facing output from libraries to CLI callers#111

Merged
sgopinath1 merged 2 commits intoROCm:mainfrom
shiv-tyagi:remove-printf-from-library
Apr 8, 2026
Merged

Move user-facing output from libraries to CLI callers#111
sgopinath1 merged 2 commits intoROCm:mainfrom
shiv-tyagi:remove-printf-from-library

Conversation

@shiv-tyagi
Copy link
Copy Markdown
Member

@shiv-tyagi shiv-tyagi commented Apr 7, 2026

This PR does the following fixes:-

  • Add IsEnabled() to the gpu-tracker Interface as a read-only state query, enabling CLI callers to print context-appropriate messages ("already enabled", "is disabled") before calling mutating methods
  • Change ShowStatus to return []GPUStatusEntry instead of printing a table directly
  • Change MakeGPUsExclusive/MakeGPUsShared to return *AccessibilityResult instead of printing results directly
  • Export Accessibility type with SHARED_ACCESS/EXCLUSIVE_ACCESS constants; keep internal int representation for backward-compatible JSON serialization
  • Change CDI PrintSpec to return (string, error), remove prints from WriteSpec and ValidateSpec
  • Move all user-facing fmt.Printf out of internal/ libraries into cmd/ CLI callers
  • Libraries now return structured data and errors only — they do not print to stdout. CLI callers own all display formatting.

Test plan

  • Run amd-ctk gpu-tracker enable (fresh) — should print "GPU Tracker has been enabled"
  • Run amd-ctk gpu-tracker enable (again) — should print "GPU Tracker is already enabled"
  • Run amd-ctk gpu-tracker disable (disabled) — should print "GPU Tracker has been disabled"
  • Run amd-ctk gpu-tracker disable (already disabled) — should print "GPU Tracker is already disabled"
  • Run amd-ctk gpu-tracker status (disabled) — should print "GPU Tracker is disabled"
  • Run amd-ctk gpu-tracker status (enabled) — should print table with GPU info
  • Run amd-ctk gpu-tracker 0,1 exclusive — should print which GPUs changed
  • Run amd-ctk gpu-tracker reset (was enabled) — should print reset message + restart recommendation
  • Run amd-ctk cdi generate — should print "Generated CDI spec: ..."
  • Run amd-ctk cdi validate (valid) — should print "CDI spec is valid"
  • Run amd-ctk cdi validate (invalid) — should print "CDI spec is invalid"
  • Verify existing gpu-tracker.json files still deserialize correctly (backward-compatible JSON)

I have tested for all the above cases and the behaviour looks as expected.

Add IsEnabled() (bool, error) to the gpu-tracker Interface as a read-only
query that checks tracker state without side effects. CLI callers use it
to print context-appropriate messages (e.g. "already enabled", "disabled")
before calling the mutating methods.

This moves all user-facing fmt.Printf messages out of the library into
the CLI layer, where they belong. The library now only returns errors and
data — it does not print to stdout.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the GPU Tracker and CDI components so that internal libraries return structured data (and errors) while CLI commands handle all user-facing output/formatting.

Changes:

  • Added IsEnabled() to the GPU Tracker interface and updated CLI commands to print context-appropriate status messages.
  • Refactored GPU Tracker APIs to return structured results ([]GPUStatusEntry, *AccessibilityResult) instead of printing tables/messages.
  • Updated CDI PrintSpec to return a formatted JSON string and moved generation/validation messaging into CLI commands.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
internal/gpu-tracker/gpu-tracker.go Adds structured types/results, IsEnabled(), and changes status/mode methods to return data instead of printing.
internal/gpu-tracker/gpu-tracker_test.go Updates tests to accommodate new return values from refactored GPU Tracker APIs.
internal/cdi/cdi.go Changes PrintSpec signature and removes some direct printing from CDI operations.
cmd/amd-ctk/gpu-tracker/status/status.go Moves GPU status table formatting/output into the CLI.
cmd/amd-ctk/gpu-tracker/reset/reset.go Prints reset output in CLI, using IsEnabled() to add conditional guidance.
cmd/amd-ctk/gpu-tracker/gpu-tracker.go Moves exclusive/shared operation result output into the CLI based on structured return values.
cmd/amd-ctk/gpu-tracker/enable/enable.go Adds pre-check via IsEnabled() and moves enable messaging into CLI.
cmd/amd-ctk/gpu-tracker/disable/disable.go Adds pre-check via IsEnabled() and moves disable messaging into CLI.
cmd/amd-ctk/cdi/validate/validate.go Moves validation messaging into CLI and prints valid/invalid results.
cmd/amd-ctk/cdi/generate/generate.go Prints “Generated CDI spec …” from CLI after writing spec.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@shiv-tyagi shiv-tyagi force-pushed the remove-printf-from-library branch from beaaebf to e7f5727 Compare April 7, 2026 13:27
@shiv-tyagi
Copy link
Copy Markdown
Member Author

Addressed review comments from copilot. This one is good to go in. The next PRs will enhance error wrapping and logging fixes.

Library methods now return structured data instead of printing directly
to stdout, letting callers control display formatting.

gpu-tracker:
- ShowStatus returns []GPUStatusEntry instead of printing a table
- MakeGPUsExclusive/MakeGPUsShared return *AccessibilityResult with
  Changed, NotChanged, InvalidGPUs, InvalidRanges fields
- Export Accessibility type as string with SHARED_ACCESS/EXCLUSIVE_ACCESS
  constants; keep internal int representation for backward-compatible
  JSON serialization of the tracker file

cdi:
- WriteSpec no longer prints success message; caller prints it
- PrintSpec returns (string, error) instead of printing to stdout
- ValidateSpec no longer prints status/result messages; caller handles
  display based on the (bool, error) return
// PrintSpec prints the generated CDI spec on the console
PrintSpec() error
// PrintSpec returns the generated CDI spec as a formatted JSON string
PrintSpec() (string, error)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Shouild we rename this function, since it is not printing anything now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have renamed to FormatSpec reflecting that it formats the CDI spec instead of printing.

@shiv-tyagi shiv-tyagi force-pushed the remove-printf-from-library branch 2 times, most recently from 9bf84b1 to e8ac0f7 Compare April 8, 2026 08:21
@sgopinath1 sgopinath1 merged commit 288996e into ROCm:main Apr 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants