7zip/CPP/Windows/System.cpp

315 lines
7.3 KiB
C++
Raw Normal View History

2021-12-27 01:00:00 +01:00
// Windows/System.cpp
#include "StdAfx.h"
#ifndef _WIN32
#include <unistd.h>
2023-06-21 02:00:00 +02:00
#include <limits.h>
2024-05-14 02:00:00 +02:00
#if defined(__APPLE__) || defined(__DragonFly__) || \
defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
2021-12-27 01:00:00 +01:00
#include <sys/sysctl.h>
#else
#include <sys/sysinfo.h>
#endif
#endif
#include "../Common/Defs.h"
// #include "../Common/MyWindows.h"
// #include "../../C/CpuArch.h"
#include "System.h"
namespace NWindows {
namespace NSystem {
#ifdef _WIN32
UInt32 CountAffinity(DWORD_PTR mask)
{
UInt32 num = 0;
for (unsigned i = 0; i < sizeof(mask) * 8; i++)
2024-05-14 02:00:00 +02:00
{
num += (UInt32)(mask & 1);
mask >>= 1;
}
2021-12-27 01:00:00 +01:00
return num;
}
BOOL CProcessAffinity::Get()
{
#ifndef UNDER_CE
return GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
#else
return FALSE;
#endif
}
UInt32 GetNumberOfProcessors()
{
// We need to know how many threads we can use.
// By default the process is assigned to one group.
// So we get the number of logical processors (threads)
// assigned to current process in the current group.
// Group size can be smaller than total number logical processors, for exammple, 2x36
CProcessAffinity pa;
if (pa.Get() && pa.processAffinityMask != 0)
return pa.GetNumProcessThreads();
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
// the number of logical processors in the current group
return (UInt32)systemInfo.dwNumberOfProcessors;
}
#else
BOOL CProcessAffinity::Get()
{
numSysThreads = GetNumberOfProcessors();
/*
numSysThreads = 8;
for (unsigned i = 0; i < numSysThreads; i++)
CpuSet_Set(&cpu_set, i);
return TRUE;
*/
2023-06-21 02:00:00 +02:00
#ifdef Z7_AFFINITY_SUPPORTED
2021-12-27 01:00:00 +01:00
// numSysThreads = sysconf(_SC_NPROCESSORS_ONLN); // The number of processors currently online
if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) != 0)
return FALSE;
return TRUE;
#else
// cpu_set = ((CCpuSet)1 << (numSysThreads)) - 1;
return TRUE;
// errno = ENOSYS;
// return FALSE;
#endif
}
UInt32 GetNumberOfProcessors()
{
2023-06-21 02:00:00 +02:00
#ifndef Z7_ST
2021-12-27 01:00:00 +01:00
long n = sysconf(_SC_NPROCESSORS_CONF); // The number of processors configured
if (n < 1)
n = 1;
return (UInt32)n;
#else
return 1;
#endif
}
#endif
#ifdef _WIN32
#ifndef UNDER_CE
2023-06-21 02:00:00 +02:00
#if !defined(_WIN64) && \
(defined(__MINGW32_VERSION) || defined(Z7_OLD_WIN_SDK))
2021-12-27 01:00:00 +01:00
2024-05-14 02:00:00 +02:00
typedef struct {
2021-12-27 01:00:00 +01:00
DWORD dwLength;
DWORD dwMemoryLoad;
DWORDLONG ullTotalPhys;
DWORDLONG ullAvailPhys;
DWORDLONG ullTotalPageFile;
DWORDLONG ullAvailPageFile;
DWORDLONG ullTotalVirtual;
DWORDLONG ullAvailVirtual;
DWORDLONG ullAvailExtendedVirtual;
} MY_MEMORYSTATUSEX, *MY_LPMEMORYSTATUSEX;
#else
#define MY_MEMORYSTATUSEX MEMORYSTATUSEX
#define MY_LPMEMORYSTATUSEX LPMEMORYSTATUSEX
#endif
2023-06-21 02:00:00 +02:00
typedef BOOL (WINAPI *Func_GlobalMemoryStatusEx)(MY_LPMEMORYSTATUSEX lpBuffer);
2021-12-27 01:00:00 +01:00
#endif // !UNDER_CE
bool GetRamSize(UInt64 &size)
{
size = (UInt64)(sizeof(size_t)) << 29;
#ifndef UNDER_CE
MY_MEMORYSTATUSEX stat;
stat.dwLength = sizeof(stat);
#endif
#ifdef _WIN64
if (!::GlobalMemoryStatusEx(&stat))
return false;
size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
return true;
#else
#ifndef UNDER_CE
2023-06-21 02:00:00 +02:00
const
Func_GlobalMemoryStatusEx fn = Z7_GET_PROC_ADDRESS(
Func_GlobalMemoryStatusEx, ::GetModuleHandleA("kernel32.dll"),
"GlobalMemoryStatusEx");
if (fn && fn(&stat))
2021-12-27 01:00:00 +01:00
{
size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
return true;
}
#endif
{
MEMORYSTATUS stat2;
stat2.dwLength = sizeof(stat2);
::GlobalMemoryStatus(&stat2);
size = MyMin(stat2.dwTotalVirtual, stat2.dwTotalPhys);
return true;
}
#endif
}
#else
// POSIX
// #include <stdio.h>
bool GetRamSize(UInt64 &size)
{
size = (UInt64)(sizeof(size_t)) << 29;
2024-05-14 02:00:00 +02:00
#if defined(__APPLE__) || defined(__DragonFly__) || \
defined(BSD) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
uint64_t val = 0;
int mib[2];
mib[0] = CTL_HW;
2021-12-27 01:00:00 +01:00
#ifdef HW_MEMSIZE
2024-05-14 02:00:00 +02:00
mib[1] = HW_MEMSIZE;
// printf("\n sysctl HW_MEMSIZE");
2021-12-27 01:00:00 +01:00
#elif defined(HW_PHYSMEM64)
2024-05-14 02:00:00 +02:00
mib[1] = HW_PHYSMEM64;
// printf("\n sysctl HW_PHYSMEM64");
2021-12-27 01:00:00 +01:00
#else
2024-05-14 02:00:00 +02:00
mib[1] = HW_PHYSMEM;
// printf("\n sysctl HW_PHYSMEM");
#endif
2021-12-27 01:00:00 +01:00
2024-05-14 02:00:00 +02:00
size_t size_sys = sizeof(val);
int res = sysctl(mib, 2, &val, &size_sys, NULL, 0);
// printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val, (int)size_sys, errno);
// we use strict check (size_sys == sizeof(val)) for returned value
// because big-endian encoding is possible:
if (res == 0 && size_sys == sizeof(val) && val)
size = val;
else
{
uint32_t val32 = 0;
size_sys = sizeof(val32);
res = sysctl(mib, 2, &val32, &size_sys, NULL, 0);
// printf("\n sysctl res=%d val=%llx size_sys = %d, %d\n", res, (long long int)val32, (int)size_sys, errno);
if (res == 0 && size_sys == sizeof(val32) && val32)
size = val32;
}
2021-12-27 01:00:00 +01:00
#elif defined(_AIX)
2024-05-14 02:00:00 +02:00
#if defined(_SC_AIX_REALMEM) // AIX
size = (UInt64)sysconf(_SC_AIX_REALMEM) * 1024;
#endif
#elif 0 || defined(__sun)
#if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
// FreeBSD, Linux, OpenBSD, and Solaris.
{
const long phys_pages = sysconf(_SC_PHYS_PAGES);
const long page_size = sysconf(_SC_PAGESIZE);
// #pragma message("GetRamSize : sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE)")
// printf("\n_SC_PHYS_PAGES (hex) = %lx", (unsigned long)phys_pages);
// printf("\n_SC_PAGESIZE = %lu\n", (unsigned long)page_size);
if (phys_pages != -1 && page_size != -1)
size = (UInt64)(Int64)phys_pages * (UInt64)(Int64)page_size;
}
#endif
2021-12-27 01:00:00 +01:00
#elif defined(__gnu_hurd__)
// fixme
#elif defined(__FreeBSD_kernel__) && defined(__GLIBC__)
// GNU/kFreeBSD Debian
// fixme
#else
struct sysinfo info;
if (::sysinfo(&info) != 0)
return false;
size = (UInt64)info.mem_unit * info.totalram;
/*
printf("\n mem_unit = %lld", (UInt64)info.mem_unit);
printf("\n totalram = %lld", (UInt64)info.totalram);
printf("\n freeram = %lld", (UInt64)info.freeram);
*/
#endif
2024-05-14 02:00:00 +02:00
const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1);
if (size > kLimit)
size = kLimit;
2021-12-27 01:00:00 +01:00
return true;
}
#endif
2023-06-21 02:00:00 +02:00
unsigned long Get_File_OPEN_MAX()
{
#ifdef _WIN32
return (1 << 24) - (1 << 16); // ~16M handles
#else
// some linux versions have default open file limit for user process of 1024 files.
long n = sysconf(_SC_OPEN_MAX);
// n = -1; // for debug
// n = 9; // for debug
if (n < 1)
{
// n = OPEN_MAX; // ???
// n = FOPEN_MAX; // = 16 : <stdio.h>
#ifdef _POSIX_OPEN_MAX
n = _POSIX_OPEN_MAX; // = 20 : <limits.h>
#else
n = 30; // our limit
#endif
}
return (unsigned long)n;
#endif
}
unsigned Get_File_OPEN_MAX_Reduced_for_3_tasks()
{
unsigned long numFiles_OPEN_MAX = NSystem::Get_File_OPEN_MAX();
const unsigned delta = 10; // the reserve for another internal needs of process
if (numFiles_OPEN_MAX > delta)
numFiles_OPEN_MAX -= delta;
else
numFiles_OPEN_MAX = 1;
numFiles_OPEN_MAX /= 3; // we suppose that we have up to 3 tasks in total for multiple file processing
numFiles_OPEN_MAX = MyMax(numFiles_OPEN_MAX, (unsigned long)3);
2024-05-14 02:00:00 +02:00
unsigned n = (unsigned)(int)-1;
2023-06-21 02:00:00 +02:00
if (n > numFiles_OPEN_MAX)
n = (unsigned)numFiles_OPEN_MAX;
return n;
}
2021-12-27 01:00:00 +01:00
}}