(git:2bb242e)
Loading...
Searching...
No Matches
machine_memory.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: GPL-2.0-or-later */
6/*----------------------------------------------------------------------------*/
7#include <stdint.h>
8
9#if defined(__APPLE__)
10#include <mach/mach.h>
11#include <sys/sysctl.h>
12#endif
13
14#if defined(__cplusplus)
15extern "C" {
16#endif
17
18/*******************************************************************************
19 * \brief Physical memory where /proc/meminfo does not exist (macOS), in bytes:
20 * the total, the free pages, and the pages likely to be allocatable
21 * (free, inactive and purgeable, which the kernel reclaims on demand).
22 * All three stay zero where the system offers no such query, which
23 * m_memory_details reads as "not available".
24 * \author Teodoro Laino
25 ******************************************************************************/
26void m_memory_details_c(int64_t *total, int64_t *free_bytes,
27 int64_t *likely_free) {
28 *total = 0;
29 *free_bytes = 0;
30 *likely_free = 0;
31#if defined(__APPLE__)
32 uint64_t memsize = 0;
33 size_t length = sizeof(memsize);
34 if (sysctlbyname("hw.memsize", &memsize, &length, NULL, 0) == 0) {
35 *total = (int64_t)memsize;
36 }
37 const mach_port_t host = mach_host_self();
38 vm_size_t page_size = 0;
39 vm_statistics64_data_t vm;
40 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
41 if (host_page_size(host, &page_size) == KERN_SUCCESS &&
42 host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm, &count) ==
43 KERN_SUCCESS) {
44 *free_bytes = (int64_t)vm.free_count * (int64_t)page_size;
45 *likely_free =
46 (int64_t)(vm.free_count + vm.inactive_count + vm.purgeable_count) *
47 (int64_t)page_size;
48 }
49 mach_port_deallocate(mach_task_self(), host);
50#endif
51}
52
53#if defined(__cplusplus)
54}
55#endif
void m_memory_details_c(int64_t *total, int64_t *free_bytes, int64_t *likely_free)
Physical memory where /proc/meminfo does not exist (macOS), in bytes: the total, the free pages,...