Skip to content

Allow for fluctuation of MemTotal for automatic swapfile size#4016

Merged
sairon merged 1 commit into
devfrom
allow-auto-swapsize-fluctuation
Apr 10, 2025
Merged

Allow for fluctuation of MemTotal for automatic swapfile size#4016
sairon merged 1 commit into
devfrom
allow-auto-swapsize-fluctuation

Conversation

@sairon
Copy link
Copy Markdown
Member

@sairon sairon commented Apr 10, 2025

As the reported MemTotal can fluctuate a bit on some systems, e.g. because the reserved memory changes between kernel version or other factors affect it like VRAM, the swap file can be recreated unnecessarily between boots. Allow for some fluctuation (up to +-32MB) before the swapfile is recreated.

This was a problem already before the recent haos-swapfile changes, however, before it checked if the existing swapfile isn't smaller than the desired value. If the MemTotal fluctuated there, the swapfile size eventually settled on the highest value seen and it wasn't recreated anymore. With this change, things should be stable even more.

Summary by CodeRabbit

  • New Features
    • Enhanced swap file management by introducing a tolerance for small size discrepancies.
    • Improved error handling during swap file verification to reduce unnecessary file recreations.

As the reported MemTotal can fluctuate a bit on some systems, e.g. because the
reserved memory changes between kernel version or other factors affect it like
VRAM, the swap file can be recreated unnecessarily between boots. Allow for
some fluctuation (up to +-32MB) before the swapfile is recreated.

This was a problem already before the recent haos-swapfile changes, however,
before it checked if the existing swapfile isn't smaller than the desired
value. If the MemTotal fluctuated there, the swapfile size eventually settled
on the highest value seen and it wasn't recreated anymore. With this change,
things should be stable even more.
@sairon sairon added the os label Apr 10, 2025
@sairon sairon requested a review from agners April 10, 2025 11:21
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2025

📝 Walkthrough

Walkthrough

The changes modify the swap file management script by introducing a new variable, SWAPSIZE_TOLERANCE, which allows a tolerance of 32MB when comparing the swap file size. The logic now verifies whether the file size falls within the expected range (defined by SWAPSIZE ± SWAPSIZE_TOLERANCE). If the swap file exists and meets this criteria, the script logs an informational message; otherwise, it checks for available space and creates the swap file if needed.

Changes

File Summary
buildroot-external/.../haos-swapfile Added the SWAPSIZE_TOLERANCE variable and updated the swap file checking logic to include a tolerance range.

Sequence Diagram(s)

sequenceDiagram
    participant Script as SwapFile Script
    participant FS as File System
    participant Logger as Logger

    Script->>FS: Check if swap file exists
    alt Swap file exists
        Script->>FS: Retrieve file size
        Script->>Script: Compare file size with expected range (SWAPSIZE ± SWAPSIZE_TOLERANCE)
        alt File size within range
            Script->>Logger: Log informational message
        else File size outside range
            Script->>FS: Check available disk space
            Script->>FS: Create/resize swap file
        end
    else Swap file does not exist
        Script->>FS: Check available disk space
        Script->>FS: Create swap file
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b3ce53 and c39f9ac.

📒 Files selected for processing (1)
  • buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile (2 hunks)
🔇 Additional comments (3)
buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile (3)

24-24: Good addition of tolerance variable.

The new variable SWAPSIZE_TOLERANCE provides a clean way to implement the tolerance mechanism for swap file size fluctuations.


30-30: Appropriate tolerance value for memory fluctuations.

Setting a 32MB tolerance for the default calculated swap size directly addresses the PR objective of allowing for MemTotal fluctuations between boots without triggering unnecessary swap file recreation.


47-52:

Details

❓ Verification inconclusive

Improved swap file size checking logic.

The new implementation correctly implements a tolerance range check for the swap file size, preventing recreation when the size is within the acceptable range. The code properly:

  1. Gets the current size of the swap file (or 0 if it doesn't exist)
  2. Checks if the size falls within the tolerance range
  3. Only recreates the swap file when necessary

This change directly addresses the issue mentioned in the PR objectives regarding unnecessary swap file recreation due to minor memory fluctuations.

However, I noticed one potential issue:

It seems tolerance is only applied for the default swap size calculation (33% of RAM), but not when SWAPSIZE is explicitly configured in /etc/default/haos-swapfile. You might want to consider applying tolerance in both cases to fully address fluctuation issues.


🏁 Script executed:

#!/bin/bash
# Verify if tolerance is applied when SWAPSIZE is manually configured
rg -l "SWAPSIZE=" --glob "buildroot-external/*/haos-swapfile"

Length of output: 210


Action: Manually verify tolerance application for manual SWAPSIZE configurations.

The updated logic for the swap file size check correctly applies the tolerance range when the size is derived from the default (33% of RAM) calculation. However, it’s not clear that the same tolerance is applied when SWAPSIZE is manually configured (for example, via /etc/default/haos-swapfile).

  • The command used to search for "SWAPSIZE=" in files under buildroot-external/*/haos-swapfile returned no results, indicating that either no such explicit configuration file exists in that path or that manual swap size settings aren’t being detected.
  • Please manually verify whether the tolerance logic is meant to cover cases when SWAPSIZE is explicitly set. If it isn’t, consider extending the tolerance check to also handle manually configured swap sizes to prevent unnecessary recreation due to minor fluctuations.

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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 or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@sairon sairon merged commit 5744509 into dev Apr 10, 2025
3 checks passed
@sairon sairon deleted the allow-auto-swapsize-fluctuation branch April 10, 2025 15:18
This was referenced Apr 11, 2025
@felipecrs
Copy link
Copy Markdown

Sorry, I know this is off topic, but there's no better place to ask:

Is REST API the only way to control the swap in HAOS for now?

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants