Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get_num_dpus function. #17601

Merged
merged 3 commits into from
Jan 5, 2024

Conversation

xincunli-sonic
Copy link
Contributor

@xincunli-sonic xincunli-sonic commented Dec 21, 2023

Why I did it

Refactor current get_num_dpus function to consuming platform.json which is reliable way to retrieve the number of DPUs.

Work item tracking
  • Microsoft ADO number:

How I did it

Read platform.json file to retrieve the number of DPU objects in DPUS section.

"DPUs": [
   {
      "DPU0": {
         "Ethernet244": "Ehternet0",
         "Ethernet248": "Ethernet1"
      }
   },
   {
      "DPU1": {
         "Ethernet252": "Ethernet0",
         "Ethernet256": "Ethernet1"
      },
   }
]

How to verify it

Create local platform json file which follow the format of hwsku.json then run the function call locally.

import json

def get_num_dpus():
    json_file = "/tmp/hwsku.json"
    if os.path.isfile(os.path.join(platform_path, "hwsku.json")):
        json_file = os.path.join(platform_path, "hwsku.json")

        try:
            with open(json_file, 'r') as file:
                platform_data = json.load(file)
        except (json.JSONDecodeError, IOError, TypeError, ValueError):
            # Handle any file reading and JSON parsing errors
            return 0

        # Convert to lower case avoid case sensitive.
        data = {k.lower(): v for k, v in platform_data.items()}
        DPUs = data.get('dpus', None)
        if DPUs is not None and len(DPUs) > 0:
            return len(DPUs)

    return 0

print(get_num_dpus()) # output is 4, which is expected.

Which release branch to backport (provide reason below if selected)

  • 202006
  • 202012
  • 202106
  • 202111
  • 202205
  • 202211
  • 202305

Tested branch (Please provide the tested image version)

Description for the changelog

A picture of a cute animal (not mandatory but encouraged)

Replace get platform file path.
Copy link
Contributor

@Pterosaur Pterosaur left a comment

Choose a reason for hiding this comment

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

LGTM

@yxieca yxieca merged commit cdd164b into sonic-net:master Jan 5, 2024
18 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.

7 participants