Skip to content

Commit

Permalink
NAS-133069 / 25.04 / Call get_memory_info() everywhere (#15200)
Browse files Browse the repository at this point in the history
* remove utils/metrics/meminfo

* add utils/memory.py

* optimize truenas_meminfo_chart.py

* call get_memory_info() in truenas-grub.py

* add available to utils/memory.py

* call get_memory_info in vm/vm_memory_info.py

* fix truenas_meminfo.chart.py

* add avail variable to MemoryInfo

* be consistent with parenthesis placement
  • Loading branch information
yocalebo authored Dec 13, 2024
1 parent 3201abf commit 2c636bc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
9 changes: 2 additions & 7 deletions src/freenas/usr/lib/netdata/python.d/truenas_meminfo.chart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from dataclasses import asdict

from middlewared.utils.metrics.meminfo import get_memory_info
from middlewared.utils.memory import get_memory_info

from bases.FrameworkServices.SimpleService import SimpleService

Expand All @@ -22,10 +20,7 @@ def __init__(self, configuration=None, name=None):
self.definitions = CHARTS

def get_data(self):
data = {}
for key, value in asdict(get_memory_info()).items():
data[key] = value
return data
return {'total': get_memory_info()['total']}

def check(self):
return True
4 changes: 2 additions & 2 deletions src/freenas/usr/local/bin/truenas-grub.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python3
import math
import psutil
import os
import json
import logging

from middlewared.utils.serial import serial_port_choices
from middlewared.utils.db import query_config_table
from middlewared.utils.vendor import Vendors
from middlewared.utils.memory import get_memory_info

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,7 +70,7 @@ def get_serial_ports():
# With our custom kernel, having 256MB RAM as base is not enough.
# In my tests it worked with having 400MB as base RAM.
# TODO: Let's please see what we can do to bring this down on the kernel side perhaps
current_mem = psutil.virtual_memory().total / 1024
current_mem = get_memory_info()['total'] / 1024
cmdline.append(f"crashkernel={400 + math.ceil(current_mem / 16 / 1024 / 1024)}M")

config.append(f'GRUB_TERMINAL_INPUT="{" ".join(terminal_input)}"')
Expand Down
5 changes: 2 additions & 3 deletions src/middlewared/middlewared/plugins/vm/vm_memory_info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import psutil

from middlewared.api import api_method
from middlewared.api.current import (
VMGetVMMemoryInfoArgs, VMGetVMMemoryInfoResult, VMGetAvailableMemoryArgs, VMGetAvailableMemoryResult,
VMGetVMemoryInUseArgs, VMGetVMemoryInUseResult, VMRandomMacArgs, VMRandomMacResult,
)
from middlewared.service import CallError, Service
from middlewared.utils.memory import get_memory_info

from .devices import NIC
from .utils import ACTIVE_STATES
Expand Down Expand Up @@ -54,7 +53,7 @@ async def get_available_memory(self, overcommit):
available at the current moment and if a VM should be allowed to be launched.
"""
# Use 90% of available memory to play safe
free = int(psutil.virtual_memory().available * 0.9)
free = get_memory_info()['available'] * 0.9

# Difference between current ARC total size and the minimum allowed
arc_total = await self.middleware.call('sysctl.get_arcstats_size')
Expand Down
28 changes: 28 additions & 0 deletions src/middlewared/middlewared/utils/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import TypedDict

__all__ = ('get_memory_info')


class MemoryInfo(TypedDict):
total: int
"""Total amount of memory in bytes"""
available: int
"""Total available memory in bytes"""


def get_memory_info() -> MemoryInfo:
total = avail = 0
with open('/proc/meminfo') as f:
for line in f:
if total and avail:
break

if not total and 'MemTotal' in line:
total = int(line.split()[1]) * 1024
continue

if not avail and 'MemAvailable' in line:
avail = int(line.split()[1]) * 1024
continue

return MemoryInfo(total=total, available=avail)
18 changes: 0 additions & 18 deletions src/middlewared/middlewared/utils/metrics/meminfo.py

This file was deleted.

0 comments on commit 2c636bc

Please sign in to comment.