diff --git a/common/common.cpp b/common/common.cpp index dd09cc05..a1387531 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -838,10 +838,15 @@ struct llama_init_result llama_init_from_gpt_params(gpt_params & params) { uint32_t n_cpu_cores = profiler::device_cpu_cores(); uint64_t total_memory = profiler::device_physical_memory(false); uint64_t available_memory = profiler::device_physical_memory(true); + uint64_t total_swap = profiler::device_swap_memory(false); + uint64_t available_swap = profiler::device_swap_memory(true); uint64_t disk_read_bw = profiler::get_disk_read_speed(params.model.c_str(), 500); + LOG_INF("Number of CPU cores: %u\n", n_cpu_cores); LOG_INF("Total Physical Memory: %.2f GB\n", total_memory / (double)(1 << 30)); LOG_INF("Available Physical Memory: %.2f GB\n", available_memory / (double)(1 << 30)); + LOG_INF("Total Swap Memory: %.2f GB\n", total_swap / (double)(1 << 30)); + LOG_INF("Available Swap Memory: %.2f GB\n", available_swap / (double)(1 << 30)); LOG_INF("Disk Read Bandwidth: %.2f GB/s\n", disk_read_bw / (double)(1 << 30)); if (model == NULL) { diff --git a/common/profiler.cpp b/common/profiler.cpp index 2c23b2c4..58385ca8 100644 --- a/common/profiler.cpp +++ b/common/profiler.cpp @@ -25,23 +25,18 @@ uint32_t device_cpu_cores() { unsigned int core_count = 1; // default to 1 in case of failure #if defined(_WIN32) || defined(_WIN64) - // Windows implementation SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); core_count = sysinfo.dwNumberOfProcessors; #elif defined(__linux__) - // Linux implementation core_count = sysconf(_SC_NPROCESSORS_ONLN); #elif defined(__APPLE__) && defined(__MACH__) - // macOS implementation int mib[4]; size_t len = sizeof(core_count); - // set the mib for hw.ncpu mib[0] = CTL_HW; - mib[1] = HW_AVAILCPU; // number of available cpus + mib[1] = HW_AVAILCPU; - // get the number of available cpus if (sysctl(mib, 2, &core_count, &len, NULL, 0) != 0 || core_count < 1) { mib[1] = HW_NCPU; // total number of cpus if (sysctl(mib, 2, &core_count, &len, NULL, 0) != 0 || core_count < 1) { @@ -113,6 +108,68 @@ uint64_t device_physical_memory(bool available) { return memory; } +uint64_t device_swap_memory(bool available) { + uint64_t swap_memory = 0; + +#if defined(_WIN32) || defined(_WIN64) + PERFORMANCE_INFORMATION performance_info; + performance_info.cb = sizeof(performance_info); + if (GetPerformanceInfo(&performance_info, sizeof(performance_info))) { + if (available) { + swap_memory = (performance_info.PageFileTotal - performance_info.PageFileUsage) * performance_info.PageSize; + } else { + swap_memory = performance_info.PageFileTotal * performance_info.PageSize; + } + } +#elif defined(__linux__) + std::ifstream meminfo("/proc/meminfo"); + std::string line; + uint64_t total_swap = 0; + uint64_t free_swap = 0; + + if (meminfo.is_open()) { + while (std::getline(meminfo, line)) { + if (line.find("SwapTotal:") == 0) { + std::istringstream iss(line); + std::string key; + uint64_t kb; + iss >> key >> kb; + total_swap = kb * 1024; + } + if (line.find("SwapFree:") == 0) { + std::istringstream iss(line); + std::string key; + uint64_t kb; + iss >> key >> kb; + free_swap = kb * 1024; + } + } + meminfo.close(); + } + + if (available) { + swap_memory = free_swap; + } else { + swap_memory = total_swap; + } + +#elif defined(__APPLE__) && defined(__MACH__) + int mib[2] = {CTL_VM, VM_SWAPUSAGE}; + struct xsw_usage swap; + size_t len = sizeof(swap); + + if (sysctl(mib, 2, &swap, &len, NULL, 0) == 0) { + if (available) { + swap_memory = swap.xsu_avail; + } else { + swap_memory = swap.xsu_total; + } + } +#endif + + return swap_memory; +} + uint64_t get_disk_read_speed(const char * test_file, size_t buffer_size_mb) { uint64_t speed = 0; size_t buffer_size = buffer_size_mb * 1024 * 1024; // buffer size in bytes diff --git a/common/profiler.h b/common/profiler.h index 1f3374f1..347ebb96 100644 --- a/common/profiler.h +++ b/common/profiler.h @@ -6,6 +6,7 @@ namespace profiler { uint32_t device_cpu_cores(); uint64_t device_physical_memory(bool available = true); + uint64_t device_swap_memory(bool available = true); uint64_t get_disk_read_speed(const char * test_file, size_t buffer_size_mb = 500); } // namespace profiler