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

Recreating the swap file if the current size is sufficiently larger #31

Merged
merged 1 commit into from
Aug 4, 2023

Conversation

deborshi1988
Copy link
Contributor

Description of changes:
This Pull request adds a logic of deleting and recreating the swap file, if the current swap is larger than the needed size.
This will help wasting extra memory in storage and make it available to the user when modifying instances from larger to smaller type.

Testing: Tested the change internally inside an instance by modifying the file.

@@ -41,6 +41,7 @@ HIB_ENABLED_FILE = "hibernation-enabled"
IMDS_BASEURL = 'http://169.254.169.254'
IMDS_API_TOKEN_PATH = 'latest/api/token'
IMDS_SPOT_ACTION_PATH = 'latest/meta-data/hibernation/configured'
MAX_SWAP_SIZE_OFFSET_ALLOWED = 100 * 1024

Choose a reason for hiding this comment

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

Why 100K is max offset?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The offset is kept just to make sure that on every fresh start we are not recreating the swap. It is a limit that I am proposing to avoid unnecessary deletion of swap. But let me know if you feel otherwise

@@ -467,7 +468,11 @@ def main():
cur_swap = os.path.getsize(SWAP_FILE)

bi = None
if cur_swap >= target_swap_size - SWAP_RESERVED_SIZE:
if cur_swap > target_swap_size - SWAP_RESERVED_SIZE + MAX_SWAP_SIZE_OFFSET_ALLOWED:

Choose a reason for hiding this comment

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

How was this scenario tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How was this scenario tested?

  1. Created a c5.xlarge instance.
  2. Made the changes in the hibinit file ( inside /usr/bin)
  3. Stopped the instance
  4. Modified to small instance( c5.large)
  5. journalctl -b | grep hibinit to verify swap is recreated
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Will check if swap is at least: 4000 megabytes
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Swap already exists! (have 8011616256, need 4194304000), deleting existing swap file /swap since current swap is sufficiently large and wasting memory
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Create swap and initialize it
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: kicking child process to initiate the setup
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Allocating 4194304000 bytes in /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Swap pre-heating is skipped, the swap blocks won't be touched during to ensure they are ready
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: mkswap /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: no label, UUID=f5142536-a989-42bc-95d0-cd971f491f57
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: swapon /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 265 for resume
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: GRUB configuration is updated
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Setting swap device to 66305 with offset 265
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Done updating the swap offset. Turning swapoff
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: swapoff /swap
  1. Verified it is successfully hibernated and resumed from hibernate
  2. Stop and start the instance again
  3. journalctl -b | grep hibinit to verified no recreation. of swap occured
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Will check if swap is at least: 4000 megabytes
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: There's sufficient swap available (have 4194304000, need 4194304000)
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Running: swapon /swap
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 265 for resume
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: GRUB configuration is updated
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Setting swap device to 66305 with offset 265
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Done updating the swap offset. Turning swapoff
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Running: swapoff /swap
  1. stop the instance
  2. Modified back to c5.xlarge
  3. Start the instance
  4. journalctl -b | grep hibinit to verify swap is created due to larger swap required.
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Will check if swap is at least: 7640 megabytes
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Swap already exists! (have 4194304000, need 8011620352), deleting existing swap file /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Create swap and initialize it
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: kicking child process to initiate the setup
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Allocating 8011620352 bytes in /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Swap pre-heating is skipped, the swap blocks won't be touched during to ensure they are ready
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: mkswap /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Setting up swapspace version 1, size = 7.5 GiB (8011616256 bytes)
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: no label, UUID=757d742b-b666-4917-87fa-0b7a5418a983
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: swapon /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 57612 for resume
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: GRUB configuration is updated
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Setting swap device to 66305 with offset 57612
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Done updating the swap offset. Turning swapoff
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: swapoff /swap
  1. Verify hibernation and resume from hibernation is successful post that.

@secarola secarola self-requested a review August 4, 2023 19:22
@deborshi1988
Copy link
Contributor Author

Got 2 approvals. Tested the same in instances

@deborshi1988 deborshi1988 reopened this Aug 4, 2023
@deborshi1988 deborshi1988 merged commit d2ce783 into aws:master Aug 4, 2023
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.

None yet

3 participants