VirtualBox

Changeset 11564 in vbox for trunk/src/VBox/Main/darwin


Ignore:
Timestamp:
Aug 22, 2008 7:09:03 AM (16 years ago)
Author:
vboxsync
Message:

PerfAPI: Darwin implementation without CPU/MHz

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/darwin/PerformanceDarwin.cpp

    r11180 r11564  
    2222 */
    2323
     24#include <iprt/stdint.h>
     25#include <mach/mach_error.h>
     26#include <mach/mach_host.h>
     27#include <mach/mach_init.h>
     28#include <mach/mach_time.h>
     29#include <mach/task.h>
     30#include <mach/task_info.h>
     31#include <mach/vm_statistics.h>
     32#include <sys/sysctl.h>
     33#include <sys/errno.h>
     34#include <iprt/err.h>
     35#include <iprt/log.h>
     36#include <iprt/param.h>
    2437#include "Performance.h"
    2538
     
    2942{
    3043public:
    31     virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
     44    virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
    3245    virtual int getHostCpuMHz(ULONG *mhz);
    3346    virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
    34     virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
     47    virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
    3548    virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
    3649};
     
    4255}
    4356
    44 int CollectorDarwin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
     57int CollectorDarwin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
    4558{
    46     return E_NOTIMPL;
     59    kern_return_t krc;
     60    mach_msg_type_number_t count;
     61    host_cpu_load_info_data_t info;
     62   
     63    count = HOST_CPU_LOAD_INFO_COUNT;
     64   
     65    krc = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&info, &count);
     66    if (krc != KERN_SUCCESS)
     67    {
     68        Log(("host_statistics() -> %s", mach_error_string(krc)));
     69        return RTErrConvertFromDarwinKern(krc);
     70    }
     71       
     72    *user = (uint64_t)info.cpu_ticks[CPU_STATE_USER]
     73                    + info.cpu_ticks[CPU_STATE_NICE];
     74    *kernel = (uint64_t)info.cpu_ticks[CPU_STATE_SYSTEM];
     75    *idle = (uint64_t)info.cpu_ticks[CPU_STATE_IDLE];
     76    return VINF_SUCCESS;
    4777}
    4878
    4979int CollectorDarwin::getHostCpuMHz(ULONG *mhz)
    5080{
    51     return E_NOTIMPL;
     81    return VERR_NOT_IMPLEMENTED;
    5282}
    5383
    5484int CollectorDarwin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
    5585{
    56     return E_NOTIMPL;
     86    kern_return_t krc;
     87    mach_msg_type_number_t count;
     88    vm_statistics_data_t info;
     89   
     90    count = HOST_VM_INFO_COUNT;
     91   
     92    krc = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&info, &count);
     93    if (krc != KERN_SUCCESS)
     94    {
     95        Log(("host_statistics() -> %s", mach_error_string(krc)));
     96        return RTErrConvertFromDarwinKern(krc);
     97    }
     98
     99    uint64_t hostMemory;
     100    int mib[2];
     101    size_t size;
     102
     103    mib[0] = CTL_HW;
     104    mib[1] = HW_MEMSIZE;
     105
     106    size = sizeof(hostMemory);
     107    if (sysctl(mib, 2, &hostMemory, &size, NULL, 0) == -1) {
     108        int error = errno;
     109        Log(("sysctl() -> %s", strerror(error)));
     110        return RTErrConvertFromErrno(error);
     111    }
     112    *total = (ULONG)(hostMemory / 1024);
     113    *available = info.free_count * (PAGE_SIZE / 1024);
     114    *used = *total - *available;
     115    return VINF_SUCCESS;
    57116}
    58117
    59 int CollectorDarwin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
     118int CollectorDarwin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
    60119{
    61     return E_NOTIMPL;
     120    kern_return_t krc;
     121    task_absolutetime_info_data_t tinfo;
     122    mach_port_name_t vmTask;
     123    mach_msg_type_number_t count;
     124
     125    krc = task_for_pid(task_self_trap(), process, &vmTask);
     126    if (krc != KERN_SUCCESS)
     127    {
     128        Log(("task_for_pid() -> %s", mach_error_string(krc)));
     129        return RTErrConvertFromDarwinKern(krc);
     130    }
     131
     132    count = TASK_ABSOLUTETIME_INFO_COUNT;
     133    krc = task_info(vmTask, TASK_ABSOLUTETIME_INFO, (task_info_t)&tinfo, &count);
     134    if (krc != KERN_SUCCESS) {
     135        Log(("task_info() -> %s", mach_error_string(krc)));
     136        return RTErrConvertFromDarwinKern(krc);
     137    }
     138
     139    *user = tinfo.total_user;
     140    *kernel = tinfo.total_system;
     141    *total = mach_absolute_time();
     142    return VINF_SUCCESS;
    62143}
    63144
    64145int CollectorDarwin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
    65146{
    66     return E_NOTIMPL;
     147    kern_return_t krc;
     148    task_basic_info_64_data_t tinfo;
     149    mach_port_name_t vmTask;
     150    mach_msg_type_number_t count;
     151
     152    krc = task_for_pid(task_self_trap(), process, &vmTask);
     153    if (krc != KERN_SUCCESS)
     154    {
     155        Log(("task_for_pid() -> %s", mach_error_string(krc)));
     156        return RTErrConvertFromDarwinKern(krc);
     157    }
     158    count = TASK_BASIC_INFO_64_COUNT;
     159    krc = task_info(task_self_trap(), TASK_BASIC_INFO_64, (task_info_t)&tinfo, &count);
     160    if (krc != KERN_SUCCESS) {
     161        Log(("host_statistics() -> %s", mach_error_string(krc)));
     162        return RTErrConvertFromDarwinKern(krc);
     163    }
     164
     165    *used = tinfo.resident_size / 1024;
     166    return VINF_SUCCESS;
    67167}
    68168
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette