Skip to content

Conversation

cmeesters
Copy link
Member

@cmeesters cmeesters commented Mar 9, 2025

The aim of this PR is:

  • measure compute efficiency similar to seff
  • report compute efficiency and memory footprint per job of the workflow (in a logfile) and
  • issue a warning, if it falls below a configurable threshold

Note: seff reports so-called "Memory Efficiency". What is meant is: "Memory Usage", because an application which reserve a compute node to compute and hardly uses RAM to do so, will have an apparently low "Memory Efficiency". It needs to reserve the memory of that node, might be highly efficient, but will not have used memory.

The resulting code of this PR will hence NOT report warnings about memory usage.

This PR is related to issue #147

Summary by CodeRabbit

  • New Features

    • Introduced an option to generate an efficiency report for SLURM jobs, summarizing resource usage for all workflow jobs.
    • Added settings to enable efficiency reporting, specify the report file location, and set a CPU efficiency threshold for warnings.
    • Efficiency reports are automatically generated upon executor shutdown when enabled.
  • Documentation

    • Added instructions on generating and interpreting job efficiency reports with the new reporting option.
  • Bug Fixes

    • Improved error messages and logging for log file cleanup and SLURM job data handling.
  • Tests

    • Added automated tests to verify the creation and content of the efficiency report.
  • Chores

    • Added pandas and numpy as new dependencies and upgraded Snakemake and pandas versions in development dependencies.

Copy link
Contributor

coderabbitai bot commented Mar 9, 2025

Walkthrough

This update introduces job efficiency reporting to the SLURM executor plugin for Snakemake. It replaces the log cleanup mechanism with a shutdown hook, adds new configuration options for efficiency reports, implements the report generation logic, updates documentation, adjusts dependencies, and expands the test suite to validate the new feature.

Changes

File(s) Change Summary
snakemake_executor_plugin_slurm/init.py Replaces atexit-based cleanup with shutdown method; adds efficiency report generation and new settings.
snakemake_executor_plugin_slurm/efficiency_report.py Adds functions and main logic for generating SLURM job efficiency reports.
docs/further.md Documents the new --slurm-efficiency-report feature and its requirements.
pyproject.toml Adds pandas and numpy dependencies; upgrades snakemake and adds pandas to dev dependencies.
tests/tests.py Updates status check timing; adds tests for efficiency report creation and validation.
.github/workflows/announce-release.yml Updates Mastodon release post action to version v1.5.0.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Executor
    participant EfficiencyReport
    participant SLURM (sacct)
    participant FileSystem

    User->>Executor: Run workflow with efficiency_report enabled
    Executor->>Executor: On shutdown, call clean_old_logs
    Executor->>EfficiencyReport: create_efficiency_report(threshold, uuid, path, logger)
    EfficiencyReport->>SLURM (sacct): Query job data (by workflow UUID)
    SLURM (sacct)-->>EfficiencyReport: Return job accounting data
    EfficiencyReport->>FileSystem: Write efficiency_report_<uuid>.csv
    EfficiencyReport->>Executor: Log report location and warnings if needed
Loading

Possibly related PRs

Suggested labels

autorelease: tagged

Suggested reviewers

  • johanneskoester
  • dlaehnemann

Poem

In the warren of SLURM, where jobs hop and leap,
A new report is born, its secrets to keep.
With pandas in tow and logs swept away,
Efficiency shines at the end of the day.
So run your workflows, let the numbers be told—
This rabbit brings insights, both daring and bold!
🐇📊


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1879205 and 89500f1.

📒 Files selected for processing (2)
  • pyproject.toml (1 hunks)
  • tests/tests.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • pyproject.toml
  • tests/tests.py
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: testing
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

cmeesters and others added 28 commits March 9, 2025 12:15
…-executor-plugin-slurm into docs/review-new-docs
…on for regular jobs, as this is covered in the where to do configuration section and the main snakemake docs
…seful for a user -- just explain special cases MPI and GPU below
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
snakemake_executor_plugin_slurm/efficiency_report.py (1)

123-127: Handle division by zero in CPU efficiency calculation.

The CPU efficiency calculation can produce infinite values when Elapsed_sec or NCPUS are zero, which are not handled by fillna(0). This can lead to inf values in the final report.

-    df["CPU Efficiency (%)"] = (
-        df["TotalCPU_sec"] / (df["Elapsed_sec"] * df["NCPUS"])
-    ) * 100
-    df["CPU Efficiency (%)"] = df["CPU Efficiency (%)"].fillna(0).round(2)
+    # Avoid division by zero by using clip to ensure minimum values of 1
+    df["CPU Efficiency (%)"] = (
+        df["TotalCPU_sec"] / (df["Elapsed_sec"].clip(lower=1) * df["NCPUS"].clip(lower=1))
+    ) * 100
+    # Handle any remaining infinite values
+    df["CPU Efficiency (%)"] = df["CPU Efficiency (%)"].replace([float('inf'), float('-inf')], 0)
+    df["CPU Efficiency (%)"] = df["CPU Efficiency (%)"].fillna(0).round(2)
🧹 Nitpick comments (4)
snakemake_executor_plugin_slurm/efficiency_report.py (4)

7-8: Remove temporary debug import.

The os import appears to be temporary for debugging purposes and should be removed before production deployment.

-import os  # only temporarily needed for printf debugging
-

16-21: Simplify conditional structure per static analysis recommendation.

The elif statements after return are unnecessary and can be simplified to if statements.

     if len(parts) == 3:  # H:M:S
         return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
-    elif len(parts) == 2:  # M:S
+    if len(parts) == 2:  # M:S
         return int(parts[0]) * 60 + float(parts[1])
-    elif len(parts) == 1:  # S
+    if len(parts) == 1:  # S
         return float(parts[0])

172-175: Simplify file path construction with ternary operator.

Static analysis suggests using a ternary operator for cleaner code.

-    if e_report_path:
-        logfile = Path(e_report_path) / logfile
-    else:
-        logfile = p.cwd() / logfile
+    logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile

183-183: Remove debug logging statement.

The debug statement using the temporary os import should be removed along with the import.

-    logger.debug(f"Current directory contents in '{p.cwd()}': {os.listdir(p.cwd())}")
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e59b1ff and fbe472a.

📒 Files selected for processing (1)
  • snakemake_executor_plugin_slurm/efficiency_report.py (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
snakemake_executor_plugin_slurm/efficiency_report.py (2)
Learnt from: cmeesters
PR: snakemake/snakemake-executor-plugin-slurm#221
File: snakemake_executor_plugin_slurm/efficiency_report.py:34-49
Timestamp: 2025-05-26T12:22:07.005Z
Learning: In the parse_reqmem function in snakemake_executor_plugin_slurm/efficiency_report.py, the conditional check for per_unit == "/node" is necessary for proper conversion of memory values in multi-node jobs, not redundant as it might initially appear.
Learnt from: cmeesters
PR: snakemake/snakemake-executor-plugin-slurm#221
File: snakemake_executor_plugin_slurm/efficiency_report.py:0-0
Timestamp: 2025-06-16T08:54:07.957Z
Learning: In the context of SLURM executor plugin development, user cmeesters (experienced with SLURM) noted they are not aware of SLURM typically reporting memory values with decimals (like 1.5G), but accepted defensive programming to handle such cases anyway.
🪛 Ruff (0.11.9)
snakemake_executor_plugin_slurm/efficiency_report.py

172-175: Use ternary operator logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile instead of if-else-block

Replace if-else-block with logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile

(SIM108)

🪛 Pylint (3.3.7)
snakemake_executor_plugin_slurm/efficiency_report.py

[refactor] 16-21: Unnecessary "elif" after "return", remove the leading "el" from "elif"

(R1705)


[refactor] 57-57: Either all return statements in a function should return an expression, or none of them should.

(R1710)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: testing
🔇 Additional comments (1)
snakemake_executor_plugin_slurm/efficiency_report.py (1)

57-182: LGTM! Well-structured efficiency reporting implementation.

The overall implementation is robust and handles the complex task of parsing SLURM accounting data effectively. The function properly handles edge cases like empty data, missing comments, and various memory format specifications. The integration of lessons learned from previous reviews (NaN handling, empty string checks, decimal parsing) makes this a solid implementation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (6)
snakemake_executor_plugin_slurm/efficiency_report.py (6)

7-8: Remove temporary debugging imports and comment.

The comment indicates os and numpy are temporarily needed for debugging, but both are actually used in the production code (os.listdir on line 185, np.inf on line 128).

-import os  # only temporarily needed for printf debugging
-import numpy as np
+import os
+import numpy as np

17-22: Simplify control flow by removing unnecessary elif statements.

The elif statements after return are unnecessary and can be simplified for better readability.

-    if len(parts) == 3:  # H:M:S
+    if len(parts) == 3:  # H:M:S
         return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
-    elif len(parts) == 2:  # M:S
+    if len(parts) == 2:  # M:S
         return int(parts[0]) * 60 + float(parts[1])
-    elif len(parts) == 1:  # S
+    if len(parts) == 1:  # S
         return float(parts[0])
     return 0

58-80: Make return behavior consistent throughout the function.

The function returns None in error cases but implicitly returns None at the end of successful execution. Consider returning a consistent value or explicit success indicator.

 def create_efficiency_report(e_threshold, run_uuid, e_report_path, logger):
     """
     Fetch sacct job data for a Snakemake workflow
     and compute efficiency metrics.
+    
+    Returns:
+        str: Path to the generated report file, or None if failed
     """

And at the end of the function:

     # write out the efficiency report at normal verbosity in any case
     logger.info(f"Efficiency report for workflow {run_uuid} saved to {logfile}.")
     # state directory contents for debugging purposes
     logger.debug(f"Current directory contents in '{p.cwd()}': {os.listdir(p.cwd())}")
+    return str(logfile)

168-177: Simplify path construction using ternary operator.

The if-else block for constructing the logfile path can be simplified using a ternary operator for better readability.

-    # we construct a path object to allow for a customi
+    # we construct a path object to allow for a custom
     # logdir, if specified
     p = Path()
 
     # Save the report to a CSV file
     logfile = f"efficiency_report_{run_uuid}.csv"
-    if e_report_path:
-        logfile = Path(e_report_path) / logfile
-    else:
-        logfile = p.cwd() / logfile
+    logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile

180-180: Consider adding index=False to CSV output for cleaner reports.

The CSV file includes pandas DataFrame index by default, which may not be useful for end users reading the efficiency reports.

-    df.to_csv(logfile)
+    df.to_csv(logfile, index=False)

185-185: Replace debug directory listing with more informative logging.

The current debug output lists all files in the current directory, which may be verbose and not directly relevant. Consider logging only efficiency report files or making it more targeted.

-    # state directory contents for debugging purposes
-    logger.debug(f"Current directory contents in '{p.cwd()}': {os.listdir(p.cwd())}")
+    # log efficiency report files for debugging purposes
+    efficiency_files = list(p.cwd().glob("efficiency_report_*.csv"))
+    logger.debug(f"Efficiency report files in '{p.cwd()}': {efficiency_files}")
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fbe472a and ef7eef2.

📒 Files selected for processing (1)
  • snakemake_executor_plugin_slurm/efficiency_report.py (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
snakemake_executor_plugin_slurm/efficiency_report.py (2)
Learnt from: cmeesters
PR: snakemake/snakemake-executor-plugin-slurm#221
File: snakemake_executor_plugin_slurm/efficiency_report.py:34-49
Timestamp: 2025-05-26T12:22:07.005Z
Learning: In the parse_reqmem function in snakemake_executor_plugin_slurm/efficiency_report.py, the conditional check for per_unit == "/node" is necessary for proper conversion of memory values in multi-node jobs, not redundant as it might initially appear.
Learnt from: cmeesters
PR: snakemake/snakemake-executor-plugin-slurm#221
File: snakemake_executor_plugin_slurm/efficiency_report.py:0-0
Timestamp: 2025-06-16T08:54:07.957Z
Learning: In the context of SLURM executor plugin development, user cmeesters (experienced with SLURM) noted they are not aware of SLURM typically reporting memory values with decimals (like 1.5G), but accepted defensive programming to handle such cases anyway.
🪛 Ruff (0.11.9)
snakemake_executor_plugin_slurm/efficiency_report.py

174-177: Use ternary operator logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile instead of if-else-block

Replace if-else-block with logfile = Path(e_report_path) / logfile if e_report_path else p.cwd() / logfile

(SIM108)

🪛 Pylint (3.3.7)
snakemake_executor_plugin_slurm/efficiency_report.py

[refactor] 17-22: Unnecessary "elif" after "return", remove the leading "el" from "elif"

(R1705)


[refactor] 58-58: Either all return statements in a function should return an expression, or none of them should.

(R1710)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: testing

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ef7eef2 and 92836de.

📒 Files selected for processing (1)
  • tests/tests.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
tests/tests.py

70-70: Undefined name expected_path

(F821)

🪛 Flake8 (7.2.0)
tests/tests.py

[error] 70-70: undefined name 'expected_path'

(F821)

🪛 Pylint (3.3.7)
tests/tests.py

[error] 70-70: Undefined variable 'expected_path'

(E0602)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: testing
🔇 Additional comments (3)
tests/tests.py (3)

1-4: Imports look good.

The import statements are well-organized and include all necessary modules for the efficiency report testing functionality.


24-28: Good adjustment to timing settings.

Increasing the initial delay before status checks from 1 to 2 seconds is a reasonable change that should help with test reliability.


30-45: Well-structured test class for efficiency reporting.

The test class is properly configured with appropriate settings for testing the efficiency report feature. The use of Path.cwd() for the report path is a good approach.

Copy link
Contributor

@johanneskoester johanneskoester left a comment

Choose a reason for hiding this comment

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

Here are two final comments.

@cmeesters cmeesters merged commit 3cef6b7 into main Jul 4, 2025
6 checks passed
@cmeesters cmeesters deleted the feat/eff-report branch July 4, 2025 16:19
cmeesters pushed a commit that referenced this pull request Jul 4, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.5.0](v1.4.0...v1.5.0)
(2025-07-04)


### Features

* measuring compute efficiency per job
([#221](#221))
([3cef6b7](3cef6b7))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Documentation**
* Updated the changelog to include version 1.5.0 with details about the
new compute efficiency measurement feature.
* **Chores**
  * Bumped the package version to 1.5.0.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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