F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Memory.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Darwin/Memory.cpp
3 // \brief Darwin implementation for Os::Memory
4 // ======================================================================
5 #include <Os/Darwin/Memory.hpp>
6 #include <mach/mach_error.h>
7 #include <mach/mach_host.h>
8 #include <mach/mach_init.h>
9 #include <mach/mach_types.h>
10 #include <mach/message.h>
11 
12 namespace Os {
13 namespace Darwin {
14 namespace Memory {
15 
28 kern_return_t vm_stat_helper(FwSizeType& used, FwSizeType& total) {
29  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
30  vm_statistics_data_t vmstat;
31  vm_size_t vmsize;
32 
33  kern_return_t status1 = host_statistics(mach_host_self(), HOST_VM_INFO, reinterpret_cast<host_info_t>(&vmstat), &count);
34  kern_return_t status2 = host_page_size(mach_host_self(), &vmsize);
35 
36  if (KERN_SUCCESS == status1 and KERN_SUCCESS == status2) {
37  // Wired (permanently in RAM), active (recently used), and inactive (not recently used) pages
38  used = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count;
39  total = used + vmstat.free_count;
40 
41  // Pages to totals
42  used *= vmsize;
43  total *= vmsize;
44  }
45  return (status1 == KERN_SUCCESS) ? status2 : status1;
46 }
47 
48 
50  // Call out VM helper
51  if (KERN_SUCCESS == vm_stat_helper(memory_usage.used, memory_usage.total)) {
52  return Status::OP_OK;
53  }
54  // Force something sensible, while preventing divide by zero
55  memory_usage.total = 1;
56  memory_usage.used = 1;
57  return Status::ERROR;
58 }
59 
61  return &this->m_handle;
62 }
63 
64 } // namespace Memory
65 } // namespace Darwin
66 } // namespace Os
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
Status _getUsage(Os::Memory::Usage &memory_usage) override
get system memory usage
Definition: Memory.cpp:49
MemoryHandle * getHandle() override
returns the raw console handle
Definition: Memory.cpp:60
Memory variable handle parent.
Definition: Memory.hpp:13
memory implementation
Definition: Memory.hpp:47
kern_return_t vm_stat_helper(FwSizeType &used, FwSizeType &total)
reads macOS virtual memory statistics for memory calculation
Definition: Memory.cpp:28
Status
Generic OK/ERROR status.
Definition: Os.hpp:25
@ OP_OK
Operation succeeded.
Definition: Os.hpp:26
@ ERROR
Operation failed.
Definition: Os.hpp:27
Generic used/total struct.
Definition: Os.hpp:31
FwSizeType total
Total amount.
Definition: Os.hpp:33
FwSizeType used
Used amount.
Definition: Os.hpp:32