Download src/io/os_info.cpp from Ozomozyan/hkimi: direct link, hf CLI and curl.
- Browser
- Download file 6.74 kB
-
https://huggingface.co/Ozomozyan/hkimi/resolve/main/src/io/os_info.cpp
- Command line
-
hf download hf://Ozomozyan/hkimi/src/io/os_info.cpp
-
curl -L -o os_info.cpp https://huggingface.co/Ozomozyan/hkimi/resolve/main/src/io/os_info.cpp
6.74 kB
| // GetProcessMemoryInfo from kernel32 itself, so that no psapi library is linked | |
| namespace hk { | |
| // cgroup v2 (memory.max) or v1 (memory.limit_in_bytes); "max" or a huge number means no limit | |
| static int64_t cgroup_limit() { | |
| const char* files[] = {"/sys/fs/cgroup/memory.max", "/sys/fs/cgroup/memory/memory.limit_in_bytes"}; | |
| for (const char* f : files) { | |
| FILE* fp = fopen(f, "r"); | |
| if (!fp) continue; | |
| char buf[64] = {0}; | |
| size_t n = fread(buf, 1, sizeof(buf) - 1, fp); | |
| fclose(fp); | |
| if (n == 0 || buf[0] == 'm') continue; | |
| long long v = atoll(buf); | |
| if (v > 0 && v < ((long long)1 << 60)) return (int64_t)v; | |
| } | |
| return -1; | |
| } | |
| int64_t system_ram_bytes() { | |
| MEMORYSTATUSEX st; | |
| st.dwLength = sizeof(st); | |
| if (!GlobalMemoryStatusEx(&st)) return -1; | |
| return (int64_t)st.ullTotalPhys; | |
| int64_t mem = 0; | |
| size_t len = sizeof(mem); | |
| if (sysctlbyname("hw.memsize", &mem, &len, nullptr, 0) != 0) return -1; | |
| return mem; | |
| int64_t phys = (int64_t)sysconf(_SC_PHYS_PAGES) * (int64_t)sysconf(_SC_PAGE_SIZE); | |
| int64_t lim = cgroup_limit(); | |
| if (lim > 0 && lim < phys) return lim; | |
| return phys; | |
| } | |
| // a number from a one-line file, or -1 | |
| static int64_t file_number(const char* path) { | |
| FILE* fp = fopen(path, "r"); | |
| if (!fp) return -1; | |
| char buf[64] = {0}; | |
| size_t n = fread(buf, 1, sizeof(buf) - 1, fp); | |
| fclose(fp); | |
| if (n == 0 || buf[0] < '0' || buf[0] > '9') return -1; | |
| long long v = atoll(buf); | |
| return v >= 0 && v < ((long long)1 << 60) ? (int64_t)v : -1; | |
| } | |
| int64_t system_ram_free_bytes() { | |
| MEMORYSTATUSEX st; | |
| st.dwLength = sizeof(st); | |
| if (!GlobalMemoryStatusEx(&st)) return -1; | |
| return (int64_t)st.ullAvailPhys; | |
| int64_t avail = -1; | |
| if (FILE* fp = fopen("/proc/meminfo", "r")) { | |
| char line[256]; | |
| while (fgets(line, sizeof(line), fp)) { | |
| long long kb = 0; | |
| if (sscanf(line, "MemAvailable: %lld kB", &kb) == 1) { | |
| avail = (int64_t)kb * 1024; | |
| break; | |
| } | |
| } | |
| fclose(fp); | |
| } | |
| // In a container the limit counts, not the host's memory. File cache counts as used | |
| // there, but the kernel gives it up when memory is asked for. | |
| const int64_t lim = cgroup_limit(); | |
| if (lim > 0) { | |
| int64_t used = file_number("/sys/fs/cgroup/memory.current"); | |
| const char* stat = "/sys/fs/cgroup/memory.stat"; | |
| if (used < 0) { | |
| used = file_number("/sys/fs/cgroup/memory/memory.usage_in_bytes"); | |
| stat = "/sys/fs/cgroup/memory/memory.stat"; | |
| } | |
| if (used >= 0) { | |
| long long cached = 0, total_cached = -1; | |
| if (FILE* fp = fopen(stat, "r")) { | |
| char name[64]; | |
| long long v = 0; | |
| while (fscanf(fp, "%63s %lld", name, &v) == 2) { | |
| const std::string s = name; | |
| if (s == "inactive_file" || s == "active_file") cached += v; | |
| if (s == "total_inactive_file" || s == "total_active_file") total_cached = (total_cached < 0 ? 0 : total_cached) + v; | |
| } | |
| fclose(fp); | |
| } | |
| if (total_cached >= 0) cached = total_cached; // cgroup v1 with child groups | |
| used = used > cached ? used - cached : 0; | |
| const int64_t left = lim > used ? lim - used : 0; | |
| if (avail < 0 || left < avail) avail = left; | |
| } | |
| } | |
| return avail; | |
| return -1; | |
| } | |
| int64_t process_ram_peak_bytes() { | |
| PROCESS_MEMORY_COUNTERS pmc; | |
| memset(&pmc, 0, sizeof(pmc)); | |
| pmc.cb = sizeof(pmc); | |
| if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return -1; | |
| return (int64_t)pmc.PeakWorkingSetSize; | |
| struct rusage ru; | |
| if (getrusage(RUSAGE_SELF, &ru) != 0) return -1; | |
| return (int64_t)ru.ru_maxrss; // bytes there | |
| return (int64_t)ru.ru_maxrss * 1024; // kilobytes here | |
| } | |
| int64_t disk_free_bytes(const std::string& path) { | |
| std::error_code ec; | |
| auto info = std::filesystem::space(std::filesystem::u8path(path), ec); | |
| if (ec) return -1; | |
| return (int64_t)info.available; | |
| } | |
| bool same_volume(const std::string& a, const std::string& b) { | |
| // the volume of a path: the longest mount point that starts it | |
| auto volume = [](const std::string& path) { | |
| std::error_code ec; | |
| std::wstring full = std::filesystem::absolute(std::filesystem::u8path(path), ec).wstring(); | |
| wchar_t root[MAX_PATH + 1] = {0}; | |
| if (ec || !GetVolumePathNameW(full.c_str(), root, MAX_PATH)) return std::wstring(); | |
| std::wstring v(root); | |
| for (wchar_t& ch : v) ch = (wchar_t)towlower(ch); | |
| return v; | |
| }; | |
| std::wstring va = volume(a), vb = volume(b); | |
| return !va.empty() && va == vb; | |
| struct stat sa, sb; | |
| if (stat(a.c_str(), &sa) != 0 || stat(b.c_str(), &sb) != 0) return false; | |
| return sa.st_dev == sb.st_dev; | |
| } | |
| bool file_is_open_here(const std::string& path) { | |
| // One entry per open descriptor. Compared by device and inode, so a file reached | |
| // under another name still counts. | |
| struct stat want; | |
| if (stat(path.c_str(), &want) != 0) return false; | |
| std::error_code ec; | |
| for (std::filesystem::directory_iterator it("/proc/self/fd", ec), end; !ec && it != end; it.increment(ec)) { | |
| struct stat st; | |
| int fd = atoi(it->path().filename().string().c_str()); | |
| if (fstat(fd, &st) == 0 && st.st_dev == want.st_dev && st.st_ino == want.st_ino) return true; | |
| } | |
| return false; | |
| (void)path; | |
| return false; | |
| } | |
| std::string os_name() { | |
| return "windows"; | |
| return "macos"; | |
| return "linux"; | |
| return "posix"; | |
| } | |
| } // namespace hk | |