mac os x using Mach calls for basic host information

// host_basic_info.c

#include <stdio.h>
#include <stdlib.h>
#include <mach/mach.h>

#define EXIT_ON_MACH_ERROR(msg, retval) \
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); exit((retval)); }

int
main()
{
    kern_return_t           kr; // the standard return type for Mach calls
    host_name_port_t        myhost;
    kernel_version_t        kversion;
    host_basic_info_data_t  hinfo;
    mach_msg_type_number_t  count;
    char                   *cpu_type_name, *cpu_subtype_name;
    vm_size_t               page_size;

    // get send rights to the name port for the current host
    myhost = mach_host_self();

    kr = host_kernel_version(myhost, kversion);
    EXIT_ON_MACH_ERROR("host_kernel_version", kr);

    count = HOST_BASIC_INFO_COUNT;      // size of the buffer
    kr = host_info(myhost,              // the host name port
                   HOST_BASIC_INFO,     // flavor
                   (host_info_t)&hinfo, // out structure
                   &count);             // in/out size
    EXIT_ON_MACH_ERROR("host_info", kr);

    kr = host_page_size(myhost, &page_size);
    EXIT_ON_MACH_ERROR("host_page_size", kr);

    printf("%s\n", kversion);

    // the slot_name() library function converts the specified
    // cpu_type/cpu_subtype pair to a human-readable form
    slot_name(hinfo.cpu_type, hinfo.cpu_subtype, &cpu_type_name,
              &cpu_subtype_name);

    printf("cpu              %s (%s, type=0x%x subtype=0x%x "
           "threadtype=0x%x)\n", cpu_type_name, cpu_subtype_name,
           hinfo.cpu_type, hinfo.cpu_subtype, hinfo.cpu_threadtype);
    printf("max_cpus         %d\n", hinfo.max_cpus);
    printf("avail_cpus       %d\n", hinfo.avail_cpus);
    printf("physical_cpu     %d\n", hinfo.physical_cpu);
    printf("physical_cpu_max %d\n", hinfo.physical_cpu_max);
    printf("logical_cpu      %d\n", hinfo.logical_cpu);
    printf("logical_cpu_max  %d\n", hinfo.logical_cpu_max);
    printf("memory_size      %u MB\n", (hinfo.memory_size >> 20));
    printf("max_mem          %llu MB\n", (hinfo.max_mem >> 20));
    printf("page_size        %u bytes\n", page_size);

    exit(0);
}

mac os x using Mach calls for basic host information

haidragondeMacBook-Air:6-1 haidragon$ gcc -Wall -o host_basic_info host_basic_info.c 
host_basic_info.c:55:43: warning: format specifies type 'unsigned int' but the argument has type
      'vm_size_t' (aka 'unsigned long') [-Wformat]
    printf("page_size        %u bytes\n", page_size);
                             ~~           ^~~~~~~~~
                             %lu
1 warning generated.
haidragondeMacBook-Air:6-1 haidragon$ ls
host_basic_info     host_basic_info.c
haidragondeMacBook-Air:6-1 haidragon$ ./host_basic_info
Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64
cpu              x86_64h (Intel x86-64h Haswell, type=0x7 subtype=0x8 threadtype=0x1)
max_cpus         4
avail_cpus       4
physical_cpu     2
physical_cpu_max 2
logical_cpu      4
logical_cpu_max  4
memory_size      2048 MB
max_mem          8192 MB
page_size        4096 bytes
haidragondeMacBook-Air:6-1 haidragon$ 

Guess you like

Origin blog.51cto.com/haidragon/2416417