Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Measurement on macOS #168

Open
jdumas opened this issue Jul 20, 2023 · 3 comments
Open

Memory Measurement on macOS #168

jdumas opened this issue Jul 20, 2023 · 3 comments

Comments

@jdumas
Copy link

jdumas commented Jul 20, 2023

Feature Request

Hi,

It seems that the functions GetRAMPhysicalUsedByCurrentProcess(), GetRAMVirtualUsedByCurrentProcess() and GetRAMVirtualTotal() are not implemented for macOS platforms. Is this a limitation of macOS, or just a missing feature? If the latter I'd be interested in implementing it, but do you have a unit test/way to measure that the implementation is correct?

@DigitalInBlue
Copy link
Owner

This is a perfectly doable thing.

Something like:

uint64_t GetRAMPhysicalUsedByCurrentProcess() {
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;

    if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &count) == KERN_SUCCESS) {
        return vmInfo.phys_footprint;
    }

    return 0;
}

@jdumas
Copy link
Author

jdumas commented Jul 21, 2023

Thanks! Do you have a similar code for GetRAMVirtualUsedByCurrentProcess()? It seems that's what Celero is currently measuring during testing.

@DigitalInBlue
Copy link
Owner

Perhaps something like this. You should look at the API to be sure.

#include <iostream>
#include <mach/mach.h>
#include <mach/task_info.h>

uint64_t GetVirtualMemoryUsed() {
    mach_task_basic_info_data_t taskInfo;
    mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
    kern_return_t kr = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&taskInfo), &infoCount);

    if (kr != KERN_SUCCESS) {
        std::cerr << "Error: Unable to get task_info. Error code: " << kr << std::endl;
        return 0;
    }

    return taskInfo.virtual_size;
}

int main() {
    uint64_t virtualRAMUsed = GetVirtualMemoryUsed();
    std::cout << "Virtual RAM used by current process: " << virtualRAMUsed << " bytes." << std::endl;

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants