File: mem_usage.c

package info (click to toggle)
ocaml-mem-usage 0.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: ansic: 253; ml: 96; makefile: 3
file content (289 lines) | stat: -rw-r--r-- 9,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-FileCopyrightText: 2022 - 2024 Savonet team
//
// SPDX-License-Identifier: MIT

#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/threads.h>

#if defined(WIN32)
#include <pdh.h>
#include <psapi.h>
#include <tchar.h>
#include <windows.h>

typedef struct _PROCESS_MEMORY_COUNTERS_EX2 {
  DWORD   cb;
  DWORD   PageFaultCount;
  SIZE_T  PeakWorkingSetSize;
  SIZE_T  WorkingSetSize;
  SIZE_T  QuotaPeakPagedPoolUsage;
  SIZE_T  QuotaPagedPoolUsage;
  SIZE_T  QuotaPeakNonPagedPoolUsage;
  SIZE_T  QuotaNonPagedPoolUsage;
  SIZE_T  PagefileUsage;
  SIZE_T  PeakPagefileUsage;
  SIZE_T  PrivateUsage;
  SIZE_T  PrivateWorkingSetSize;
  ULONG64 SharedCommitUsage;
} PROCESS_MEMORY_COUNTERS_EX2;

CAMLprim value ocaml_mem_usage_mem_usage(value unit) {
  CAMLparam0();
  CAMLlocal1(ret);
  MEMORYSTATUSEX mem_info;
  PROCESS_MEMORY_COUNTERS_EX2 pmc;
  DWORDLONG total_virtual_memory;
  DWORDLONG total_used_virtual_memory;
  DWORDLONG total_physical_memory;
  DWORDLONG total_used_physical_memory;
  SIZE_T process_virtual_memory;
  SIZE_T process_physical_memory;
  SIZE_T process_private_memory;
  SIZE_T process_swapped_memory;

  caml_release_runtime_system();
  mem_info.dwLength = sizeof(MEMORYSTATUSEX);
  GlobalMemoryStatusEx(&mem_info);
  total_virtual_memory = mem_info.ullTotalPageFile;
  total_used_virtual_memory =
      mem_info.ullTotalPageFile - mem_info.ullAvailPageFile;
  total_physical_memory = mem_info.ullTotalPhys;
  total_used_physical_memory = mem_info.ullTotalPhys - mem_info.ullAvailPhys;

  GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc,
                       sizeof(pmc));
  process_virtual_memory = pmc.PrivateUsage;
  process_physical_memory = pmc.WorkingSetSize;
  process_private_memory = pmc.PrivateWorkingSetSize;
  process_swapped_memory = pmc.PagefileUsage;
  caml_acquire_runtime_system();

  ret = caml_alloc_tuple(8);
  Store_field(ret, 0, Val_long(total_virtual_memory));
  Store_field(ret, 1, Val_long(total_physical_memory));
  Store_field(ret, 2, Val_long(total_used_virtual_memory));
  Store_field(ret, 3, Val_long(total_used_physical_memory));
  Store_field(ret, 4, Val_long(process_virtual_memory));
  Store_field(ret, 5, Val_long(process_physical_memory));
  Store_field(ret, 6, Val_long(process_private_memory));
  Store_field(ret, 7, Val_long(process_swapped_memory));
  CAMLreturn(ret);
}
#elif defined(__APPLE__)
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/mach_types.h>
#include <mach/mach_vm.h>
#include <mach/vm_statistics.h>
#include <stdio.h>
#include <sys/mount.h>
#include <sys/sysctl.h>
#include <unistd.h>

void private_pages(unsigned int *pages_resident,
                   unsigned int *pages_swapped_out) {
  mach_vm_address_t address = 0;
  mach_vm_size_t size = 0;
  uint32_t depth = 2048;
  vm_region_submap_info_data_64_t info;
  mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
  kern_return_t kr;
  *pages_resident = 0;
  *pages_swapped_out = 0;

  while (1) {
    kr = mach_vm_region_recurse(mach_task_self(), &address, &size, &depth,
                                (vm_region_recurse_info_t)&info, &count);

    if (kr != KERN_SUCCESS || size == 0)
      break;

    if (info.share_mode == SM_PRIVATE) {
      *pages_resident += info.pages_resident;
      *pages_swapped_out += info.pages_swapped_out;
    }

    address += size;
  }
}

CAMLprim value ocaml_mem_usage_mem_usage(value unit) {
  CAMLparam0();
  CAMLlocal1(ret);
  struct statfs stats;
  uint64_t total_virtual_memory, total_physical_memory,
      total_used_physical_memory, total_used_virtual_memory,
      process_physical_memory, process_virtual_memory, process_private_memory,
      process_swapped_memory;
  struct xsw_usage vmem_usage = {0};
  size_t size = sizeof(vmem_usage);
  struct task_basic_info t_info;
  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
  vm_size_t page_size;
  mach_port_t mach_port;
  mach_msg_type_number_t count;
  vm_statistics64_data_t vm_stats;
  int pagesize;
  unsigned int pages_resident, pages_swapped_out;

  caml_release_runtime_system();
  if (statfs("/", &stats) != 0) {
    fprintf(stderr, "Error while getting free swap space.\n");
    total_virtual_memory = 0;
  } else {
    total_virtual_memory = (uint64_t)stats.f_bsize * stats.f_bfree;
  }

  if (sysctlbyname("vm.swapusage", &vmem_usage, &size, NULL, 0) != 0) {
    fprintf(stderr, "Error while getting swap usage.\n");
    total_used_virtual_memory = 0;
  } else {
    total_used_virtual_memory = vmem_usage.xsu_used;
  }

  if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info,
                &t_info_count)) {
    fprintf(stderr,
            "Unable to get virtual memory currently used by the process.\n");
    process_physical_memory = 0;
    process_virtual_memory = 0;
  } else {
    process_physical_memory = t_info.resident_size;
    process_virtual_memory = t_info.virtual_size;
  }

  mach_port = mach_host_self();
  count = sizeof(vm_stats) / sizeof(natural_t);

  if (host_page_size(mach_port, &page_size) != KERN_SUCCESS) {
    fprintf(stderr, "Unable to get host page size.\n");
    total_physical_memory = 0;
    total_used_physical_memory = 0;
  } else {
    if (host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t)&vm_stats,
                          &count) != KERN_SUCCESS) {
      fprintf(stderr, "Unable to get host stats.\n");
      total_physical_memory = 0;
      total_used_physical_memory = 0;
    } else {
      total_physical_memory = vm_stats.free_count * (int64_t)page_size;

      total_used_physical_memory =
          ((int64_t)vm_stats.active_count + (int64_t)vm_stats.inactive_count +
           (int64_t)vm_stats.wire_count) *
          (int64_t)page_size;
    }
  }

  pagesize = getpagesize();
  private_pages(&pages_resident, &pages_swapped_out);
  process_private_memory = pages_resident * pagesize;
  process_swapped_memory = pages_swapped_out * pagesize;

  caml_acquire_runtime_system();

  ret = caml_alloc_tuple(8);
  ;
  Store_field(ret, 0, Val_long(total_virtual_memory));
  Store_field(ret, 1, Val_long(total_physical_memory));
  Store_field(ret, 2, Val_long(total_used_virtual_memory));
  Store_field(ret, 3, Val_long(total_used_physical_memory));
  Store_field(ret, 4, Val_long(process_virtual_memory));
  Store_field(ret, 5, Val_long(process_physical_memory));
  Store_field(ret, 6, Val_long(process_private_memory));
  Store_field(ret, 7, Val_long(process_swapped_memory));
  CAMLreturn(ret);
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <sys/types.h>

CAMLprim value ocaml_mem_usage_mem_usage(value unit) {
  CAMLparam0();
  CAMLlocal1(ret);
  struct sysinfo memInfo;
  uint64_t total_virtual_memory, total_physical_memory,
      total_used_virtual_memory, total_used_physical_memory;
  unsigned long long process_virtual_memory, process_physical_memory,
      process_private_memory, process_swapped_memory, tmp;
  FILE *file;
  char buffer[1024] = "";

  caml_release_runtime_system();
  sysinfo(&memInfo);
  total_physical_memory = memInfo.totalram;
  total_physical_memory *= memInfo.mem_unit;

  total_virtual_memory = memInfo.totalram;
  total_virtual_memory += memInfo.totalswap;
  total_virtual_memory *= memInfo.mem_unit;

  total_used_virtual_memory = memInfo.totalram - memInfo.freeram;
  total_used_virtual_memory += memInfo.totalswap - memInfo.freeswap;
  total_used_virtual_memory *= memInfo.mem_unit;

  total_used_physical_memory = memInfo.totalram - memInfo.freeram;
  total_used_physical_memory *= memInfo.mem_unit;

  file = fopen("/proc/self/status", "r");
  if (file) {
    while (fscanf(file, " %1023s", buffer) == 1) {
      if (strcmp(buffer, "VmSize:") == 0) {
        if (fscanf(file, " %lld", &process_virtual_memory) != 1)
          process_virtual_memory = 0;
        process_virtual_memory *= 1024;
        continue;
      }

      if (strcmp(buffer, "VmRSS:") == 0) {
        if (fscanf(file, " %lld", &process_physical_memory) != 1)
          process_physical_memory = 0;
        process_physical_memory *= 1024;
        continue;
      }
    }
    fclose(file);
  }

  process_private_memory = 0;
  process_swapped_memory = 0;
  file = fopen("/proc/self/smaps", "r");
  if (file) {
    while (fscanf(file, " %1023s", buffer) == 1) {
      if (strcmp(buffer, "Private_Dirty:") == 0) {
        if (fscanf(file, " %lld", &tmp) != 1)
          tmp = 0;
        process_private_memory += tmp * 1024;
        continue;
      }

      if (strcmp(buffer, "Swap:") == 0) {
        if (fscanf(file, " %lld", &tmp) != 1)
          tmp = 0;
        process_swapped_memory += tmp * 1024;
        continue;
      }
    }
    fclose(file);
  }

  caml_acquire_runtime_system();

  ret = caml_alloc_tuple(8);
  Store_field(ret, 0, Val_long(total_virtual_memory));
  Store_field(ret, 1, Val_long(total_physical_memory));
  Store_field(ret, 2, Val_long(total_used_virtual_memory));
  Store_field(ret, 3, Val_long(total_used_physical_memory));
  Store_field(ret, 4, Val_long(process_virtual_memory));
  Store_field(ret, 5, Val_long(process_physical_memory));
  Store_field(ret, 6, Val_long(process_private_memory));
  Store_field(ret, 7, Val_long(process_swapped_memory));
  CAMLreturn(ret);
}
#endif