MCPcopy Create free account
hub / github.com/apple/foundationdb / getMemoryUsage

Function getMemoryUsage

flow/Platform.actor.cpp:343–401  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341}
342
343uint64_t getMemoryUsage() {
344#if defined(__linux__)
345 uint64_t vmsize = 0;
346
347 std::ifstream stat_stream("/proc/self/statm", std::ifstream::in);
348
349 if (!stat_stream.good()) {
350 TraceEvent(SevError, "GetMemoryUsage").GetLastError();
351 throw platform_error();
352 }
353
354 stat_stream >> vmsize;
355
356 vmsize *= sysconf(_SC_PAGESIZE);
357
358 return vmsize;
359#elif defined(__FreeBSD__)
360 uint64_t vmsize = 0;
361
362 int status;
363 pid_t ppid = getpid();
364 int pidinfo[4];
365 pidinfo[0] = CTL_KERN;
366 pidinfo[1] = KERN_PROC;
367 pidinfo[2] = KERN_PROC_PID;
368 pidinfo[3] = (int)ppid;
369
370 struct kinfo_proc procstk;
371 size_t len = sizeof(procstk);
372
373 status = sysctl(pidinfo, nitems(pidinfo), &procstk, &len, nullptr, 0);
374 if (status < 0) {
375 TraceEvent(SevError, "GetMemoryUsage").GetLastError();
376 throw platform_error();
377 }
378
379 vmsize = (uint64_t)procstk.ki_size >> PAGE_SHIFT;
380
381 return vmsize;
382#elif defined(_WIN32)
383 PROCESS_MEMORY_COUNTERS_EX pmc;
384 if (!GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc))) {
385 TraceEvent(SevError, "GetMemoryUsage").GetLastError();
386 throw platform_error();
387 }
388 return pmc.PagefileUsage;
389#elif defined(__APPLE__)
390 struct task_basic_info info;
391 mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT;
392 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &info_count)) {
393 TraceEvent(SevError, "GetMemoryUsage").GetLastError();
394 throw platform_error();
395 }
396 return info.virtual_size;
397#else
398#warning getMemoryUsage unimplemented on this platform
399 return 0;
400#endif

Callers 2

getSystemStatisticsFunction · 0.85

Calls 1

TraceEventClass · 0.85

Tested by

no test coverage detected