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

Memory Statistics Config and Show Commands #3575

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ac16864
added show and config commands
kanza-latif Oct 14, 2024
f4811d1
resolved the errors
kanza-latif Oct 14, 2024
b8d7274
resolved the errors
kanza-latif Oct 14, 2024
636f30a
added test filw
kanza-latif Oct 21, 2024
81df5f0
updated test cases
kanza-latif Oct 23, 2024
1ad1222
updated test cases
kanza-latif Oct 23, 2024
29afad4
updated the mock db
kanza-latif Oct 28, 2024
aecbc72
removed pre-commit errors
kanza-latif Oct 28, 2024
2d066ee
resolivng pre-commit errors
kanza-latif Oct 28, 2024
0cf9043
resolivng pre-commit errors
kanza-latif Oct 28, 2024
52d7c0a
added mock db in config file
kanza-latif Oct 28, 2024
43f1f9c
removed incomaptibilities from the config and test files
kanza-latif Oct 28, 2024
b4716c3
removed incomaptibilities from the config and test files
kanza-latif Oct 28, 2024
b17f226
updated the test file
kanza-latif Oct 28, 2024
a6680d9
updated the test file
kanza-latif Oct 28, 2024
732612f
corrected the failing test case
kanza-latif Oct 28, 2024
7b17295
added testfile for show commands
kanza-latif Nov 5, 2024
5e72f6b
added testfile for show commands
kanza-latif Nov 5, 2024
a51c84d
removed the commented import
kanza-latif Nov 6, 2024
07fbf15
Add show memory-stats CLI command with Dict2Obj handling, clean outpu…
Arham-Nasir Nov 13, 2024
9ddaf96
updated config and test files
kanza-latif Nov 25, 2024
5c6b013
updated config and test files
kanza-latif Nov 25, 2024
81be860
update CLI commands for memory statistics configuration and management.
Arham-Nasir Dec 26, 2024
02848eb
Add show commands for memory statistics and configuration
Arham-Nasir Dec 26, 2024
4aab773
Add memory statistics monitoring commands to documentation.
Arham-Nasir Dec 26, 2024
bc8c240
Merge branch 'master' into show-config
Arham-Nasir Dec 26, 2024
c47b61b
Implement single DB connection for config commands
Arham-Nasir Dec 30, 2024
cf7f6d0
add closelog to ensure that connection is closed
Arham-Nasir Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions config/memory_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import syslog

import click
from swsscommon.swsscommon import ConfigDBConnector

# Constants
MEMORY_STATISTICS_TABLE = "MEMORY_STATISTICS"
MEMORY_STATISTICS_KEY = "memory_statistics"
SAMPLING_INTERVAL_MIN = 3
SAMPLING_INTERVAL_MAX = 15
RETENTION_PERIOD_MIN = 1
RETENTION_PERIOD_MAX = 30
DEFAULT_SAMPLING_INTERVAL = 5 # minutes
DEFAULT_RETENTION_PERIOD = 15 # days


def log_to_syslog(message, level=syslog.LOG_INFO):
"""Log a message to syslog."""
syslog.openlog("memory_statistics", syslog.LOG_PID | syslog.LOG_CONS, syslog.LOG_USER)
syslog.syslog(level, message)
Copy link
Contributor

Choose a reason for hiding this comment

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

syslog

Suggest to close the log after msg being logged.

def log_message(...):
try:
syslog.openlog(...)
syslog.syslog(...)
finally:
syslog.closelog()

Choose a reason for hiding this comment

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

Thank you for the suggestion. I have added a try-finally block to ensure syslog.closelog() is always called after logging the message. Please let me know if there is anything else you’d like me to adjust.



def get_db_connection():
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like every command we are creating a new connection to config db. Perhaps we can create a class variable and create only one connection and reuse?

Copy link

@Arham-Nasir Arham-Nasir Dec 30, 2024

Choose a reason for hiding this comment

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

Hi @zbud-msft ,Thanks for the feedback. I have implemented a Singleton pattern for the database connection, ensuring a single connection is reused across all commands.

"""Create and return a database connection."""
db = ConfigDBConnector()
Copy link
Contributor

Choose a reason for hiding this comment

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

ConfigDBConnector

Is this only considering single asic?

Copy link
Author

Choose a reason for hiding this comment

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

Can you please elaborate on asic ?

db.connect()
return db


def update_memory_statistics_status(status, db):
"""
Updates the status of the memory statistics feature in the config DB.
Returns a tuple of (success, error_message).
"""
try:
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"enabled": status})
msg = f"Memory statistics feature {'enabled' if status == 'true' else 'disabled'} successfully."
click.echo(msg)
log_to_syslog(msg)
return True, None
except Exception as e:
error_msg = f"Error updating memory statistics status: {e}"
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR)
return False, error_msg


@click.group()
def cli():
"""Memory statistics configuration tool."""
pass


@cli.group()
def config():
"""Configuration commands."""
pass


@config.group(name='memory-stats')
def memory_stats():
"""Configure memory statistics."""
pass


@memory_stats.command(name='enable')
def memory_stats_enable():
"""Enable memory statistics collection."""
db = get_db_connection()
success, error = update_memory_statistics_status("true", db)
if success:
click.echo("Reminder: Please run 'config save' to persist changes.")
log_to_syslog("Memory statistics enabled. Reminder to run 'config save'")


@memory_stats.command(name='disable')
def memory_stats_disable():
"""Disable memory statistics collection."""
db = get_db_connection()
success, error = update_memory_statistics_status("false", db)
if success:
click.echo("Reminder: Please run 'config save' to persist changes.")
log_to_syslog("Memory statistics disabled. Reminder to run 'config save'")


@memory_stats.command(name='sampling-interval')
@click.argument("interval", type=int)
def memory_stats_sampling_interval(interval):
"""
Set sampling interval for memory statistics.

The sampling interval must be between 3 and 15 minutes.
This determines how frequently memory usage data is collected.
"""
if not (SAMPLING_INTERVAL_MIN <= interval <= SAMPLING_INTERVAL_MAX):
error_msg = (
f"Error: Sampling interval must be between {SAMPLING_INTERVAL_MIN} "
f"and {SAMPLING_INTERVAL_MAX} minutes."
)
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR)
return

db = get_db_connection()
try:
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"sampling_interval": str(interval)})
success_msg = f"Sampling interval set to {interval} minutes successfully."
click.echo(success_msg)
log_to_syslog(success_msg)
click.echo("Reminder: Please run 'config save' to persist changes.")
except Exception as e:
error_msg = f"Error setting sampling interval: {e}"
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR)


@memory_stats.command(name='retention-period')
@click.argument("period", type=int)
def memory_stats_retention_period(period):
"""
Set retention period for memory statistics.

The retention period must be between 1 and 30 days.
This determines how long memory usage data is stored before being purged.
"""
if not (RETENTION_PERIOD_MIN <= period <= RETENTION_PERIOD_MAX):
error_msg = f"Error: Retention period must be between {RETENTION_PERIOD_MIN} and {RETENTION_PERIOD_MAX} days."
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR)
return

db = get_db_connection()
try:
db.mod_entry(MEMORY_STATISTICS_TABLE, MEMORY_STATISTICS_KEY, {"retention_period": str(period)})
success_msg = f"Retention period set to {period} days successfully."
click.echo(success_msg)
log_to_syslog(success_msg)
click.echo("Reminder: Please run 'config save' to persist changes.")
except Exception as e:
error_msg = f"Error setting retention period: {e}"
click.echo(error_msg, err=True)
log_to_syslog(error_msg, syslog.LOG_ERR)


if __name__ == "__main__":
cli()
Loading
Loading