Macã®ã·ã¹ãã ã³ã¼ã«
Mac OS Xã§POSIX以å¤ã®ã·ã¹ãã ã³ã¼ã«ã«ã¤ãã¦ã®æ å ±ãä¸ã ç¡ãã£ãã®ã§ãããã«ææ¸åãã¦ãããhttp://web.mit.edu/darwin/src/modules/xnu/osfmk/man/ ãã®ãã¼ã¸ã¨ããã¤ãã®ãªã¼ãã½ã¼ã¹ã®ã½ã¼ã¹ã³ã¼ããåèã«ããã
Load Average ãåå¾
Load averageã¯host_load_infoæ§é ä½ã®averun[]ã¨ããã¡ã³ãã¼ã«æ ¼ç´ããããavenrunã«ã¯5,15,60ç§éã§ã®load averageãæ ¼ç´ããã¦ããã
#include <mach/host_info.h> #define CPU_STATE_USER 0 #define CPU_STATE_SYSTEM 1 #define CPU_STATE_IDLE 2 struct host_load_info { integer_t avenrun[3]; integer_t mach_factor[3]; }; typedef struct host_load_info* host_load_info_t;
ãã®æ§é ä½ãç¨ãã¦æå¹ãªå¤ãåå¾ããæã«ã¯æ¬¡ã®host_infoé¢æ°ã使ã
#include <mach/host_info.h> kern_return_t host_info (host_t host, host_flavor_t flavor, host_info_t host_info, mach_msg_type_number_t host_info_count);
hostã¯æ
å ±ãå¾ããhostãããã¦ãhost_tæ§é ä½ãflavorã¯åå¾ããæ
å ±ã®ç¨®é¡ã表ããLoad averageãåå¾ããã¨ãã¯HOST_LOAD_INFOã¨æå®ã
æ£å¸¸ã«åä½ããã¨ãã¯host_infoã«å¤ãæ ¼ç´ããããå°ããã®å¤ã¯LOAD_SCALEã¨ãã大ããã§ã¹ã±ã¼ã«ããã¦ãããhost_info_countã¯å
¥åæã«ã¯host_infoã®ç¢ºä¿ãã¦ããã¡ã¢ãªã®å¤§ãããæ£å¸¸ã«åä½ããã¨ãã¯è¿ãå¤ã¨ãã¦ã®host_infoã®å¤§ãããæ ¼ç´ããã¦ããã
ã¾ããããã°ã©ã ãå®è¡ãããã·ã³ã«å¯¾å¿ããhost_tæ§é ä½ãå¾ãã¨ãã¯æ¬¡ã®é¢æ°ã使ãã
#include <mach/host_info.h> host_name_port_t mach_host_self( void );
以ä¸ã¯ãµã³ãã«ã³ã¼ãã
/* load.c 60ç§éã§å¹³åãåã£ãload averageãæ¨æºåºåã«è¡¨ç¤ºããã */ #include <mach/host_info.h> /* LOAD_SCALE ãå®ç¾©ããã¦ãã */ #include <mach/processor_info.h> #include <stdio.h> int main(int argc, char *argv[]) { struct host_load_info hl; mach_msg_type_number_t count = HOST_LOAD_INFO_COUNT; kern_return_t error = host_statistics(mach_host_self(),HOST_LOAD_INFO,(host_info_t)&hl,&count); double res = (double)(hl.avenrun[2])/(double)(LOAD_SCALE); printf("%lf\n",res); return 0; }
ç·ã¡ã¢ãªéãåå¾
次ã®host_basic_infoæ§é ä½ãç¨ããã
#include <mach/host_info.h> struct host_basic_info { integer_t max_cpus; integer_t avail_cpus; vm_size_t memory_size; cpu_type_t cpu_type; cpu_subtype_t cpu_subtype; };
åã¨ã³ããªã®host_infoé¢æ°ãflavorãHOST_BASIC_INFOã«ãã¦å®è¡ããã¨ãmemory_sizeã«ç·ã¡ã¢ãªéãæ ¼ç´ãããã
/* ç·ã¡ã¢ãªéãæ¨æºåºåã«è¡¨ç¤º */ #include <mach/host_info.h> #include <stdio.h> int main(int argc, char *argv[]) { struct host_basic_info host; mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&host, &count); printf("%d\n",host.memory_size); return 0; }
空ãã¡ã¢ãªéã®åå¾
空ãã¡ã¢ãªéãåå¾ããéã«ã¯vm_statisticsæ§é ä½ãç¨ããã
#include <mach/vm_statistics.h> struct vm_statistics { integer_t free_count; integer_t active_count; integer_t inactive_count; integer_t wire_count; integer_t zero_fill_count; integer_t reactivations; integer_t pageins; integer_t pageouts; integer_t faults; integer_t cow_faults; integer_t lookups; integer_t hits; }; typedef struct vm_statistics* vm_statistics_t;
ãã®æ§é ä½ã®free_countã«ç©ºãã¡ã¢ãªã®ãã¼ã¸æ°ãæ ¼ç´ããããå°ãmach/vm_statistics.hã¯mach/host_info.hãèªã¿è¾¼ãã¨èªåçã«èªã¿è¾¼ã¾ããã
æå¹ãªå¤ãåå¾ããçºã«ã¯æ¬¡ã®host_statisticsé¢æ°ãç¨ããã
#include <mach/host_info.h> kern_return_t host_statistics (host_priv_t host_priv, host_flavor_t flavor, host_info_t host_info, mach_msg_type_number_t host_info_count);
host_privã¯çµ±è¨æ
å ±ãåãããhostã示ããããã°ã©ã ãå®è¡ãããã·ã³ã®æ
å ±ãå¾ããã¨ãã¯mach_host_self()é¢æ°ãç¨ãããflavorã¯åå¾ãããæ
å ±ã示ãããã®å ´åã¯HOST_VM_INFOãæå®ãããæ£å¸¸ã«åä½ããã¨ãã¯host_info
ã«å¤ãæ ¼ç´ããããhost_info_countã¯å
¥åæã«ã¯host_infoã®ç¢ºä¿ãã¦ããã¡ã¢ãªã®å¤§ãããæ£å¸¸ã«åä½ããã¨ãã¯è¿ãå¤ã¨ãã¦ã®host_infoã®å¤§ãããæ ¼ç´ããã¦ããã
以ä¸ã¯ãµã³ãã«ã³ã¼ãã
/* 空ãã¡ã¢ãªéãæ¨æºåºåã«è¡¨ç¤ºãã */ #include <mach/host_info.h> #include <mach/mach_init.h> #include <stdio.h> int main(int argc, char *argv[]) { struct vm_statistics vm_info; mach_msg_type_number_t count = HOST_VM_INFO_COUNT; host_statistics(mach_host_self(),HOST_VM_INFO,(host_info_t)&vm_info,&count); printf("%d\n",vm_info.free_count*vm_page_size); return 0; }