sysinfo.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from os import *
  2. import psutil
  3. from utils.log.trans_log import logger
  4. def print_memory_usage(detail=""):
  5. # 获取当前进程ID
  6. pid = getpid()
  7. # 获取进程信息
  8. py = psutil.Process(pid)
  9. # 获取内存信息
  10. memory_info = py.memory_info()
  11. # RSS (Resident Set Size) 是进程实际占用的物理内存大小
  12. memory_usage_rss = memory_info.rss
  13. # VMS (Virtual Memory Size) 是进程使用的虚拟内存大小
  14. memory_usage_vms = memory_info.vms
  15. # 将字节转换为更易读的单位
  16. memory_usage_rss_mb = memory_usage_rss / (1024 ** 2)
  17. memory_usage_vms_mb = memory_usage_vms / (1024 ** 2)
  18. logger.info(f"{detail},Memory usage (RSS): {memory_usage_rss_mb:.2f} MB")
  19. logger.info(f"{detail},Memory usage (VMS): {memory_usage_vms_mb:.2f} MB")
  20. def get_cpu_count():
  21. return psutil.cpu_count()
  22. def get_available_cpu_count_with_percent(percent: float = 1):
  23. cpu_count = get_cpu_count()
  24. return int(cpu_count * percent)
  25. def get_file_size(file_path):
  26. return path.getsize(file_path)
  27. def get_dir_size(dir_path):
  28. return sum(get_file_size(path.join(dir_path, file)) for file in listdir(dir_path) if
  29. path.isfile(path.join(dir_path, file)))
  30. def get_available_memory_with_percent(percent: float = 1):
  31. memory_info = psutil.virtual_memory()
  32. return int(memory_info.available * percent)
  33. def get_max_file_size(file_paths: list[str]):
  34. max_size = 0
  35. for file_path in file_paths:
  36. file_size = get_file_size(file_path)
  37. if file_size > max_size:
  38. max_size = file_size
  39. return max_size
  40. def use_files_get_max_cpu_count(file_paths: list[str], memory_percent: float = 1 / 12, cpu_percent: float = 2 / 5):
  41. max_file_size = get_max_file_size(file_paths)
  42. free_memory = get_available_memory_with_percent(memory_percent)
  43. count = int(free_memory / max_file_size)
  44. max_cpu_count = get_available_cpu_count_with_percent(cpu_percent)
  45. result = count if count <= max_cpu_count else max_cpu_count
  46. if result == 0:
  47. result = 1
  48. if result > len(file_paths):
  49. result = len(file_paths)
  50. logger.info(f"总文件数:{len(file_paths)},获取最大文件大小:{str(round(max_file_size / 2 ** 20, 2))}M"
  51. f"可用内存:{str(get_available_memory_with_percent(1) / 2 ** 20)}M"
  52. f"总CPU数:{get_cpu_count()}CPU使用比例:{round(cpu_percent, 2)}"
  53. f"CPU可用数量:{max_cpu_count},最终确定使用进程数:{result}")
  54. return result
  55. def max_file_size_get_max_cpu_count(max_file_size, memory_percent: float = 1 / 6, cpu_percent: float = 2 / 5):
  56. free_memory = get_available_memory_with_percent(memory_percent)
  57. count = int(free_memory / max_file_size)
  58. max_cpu_count = get_available_cpu_count_with_percent(cpu_percent)
  59. result = count if count <= max_cpu_count else max_cpu_count
  60. if result == 0:
  61. result = 1
  62. logger.info(f"获取最大文件大小:{str(round(max_file_size / 2 ** 20, 2))}M"
  63. f"可用内存:{str(get_available_memory_with_percent(1) / 2 ** 20)}M"
  64. f"总CPU数:{get_cpu_count()}CPU使用比例:{round(cpu_percent, 2)}"
  65. f"CPU可用数量:{max_cpu_count},最终确定使用进程数:{result}")
  66. return result