int main () { int res, i; mach_port_t host_privileged_port; device_t device_privileged_port; res = get_privileged_ports(&host_privileged_port, &device_privileged_port); if (res != KERN_SUCCESS) { printf ("Error getting privileged ports (0x%x), %s\n", res, mach_error_string(res)); exit(1); } printf ("privileged ports: host 0x%x devices 0x%x\n", host_privileged_port, device_privileged_port); printf ("Getting processors at array 0x%x\n", processor_list); res = host_processors(host_privileged_port, &processor_list, &processor_listCnt); if (res != KERN_SUCCESS) { printf ("Error getting host_processors (0x%x), %s\n", res, mach_error_string(res)); exit(1); } printf (" processors at array 0x%x\n", processor_list); printf ("processor_listCnt %d\n", processor_listCnt); for (i=0; i < processor_listCnt; i++) printf ("processor_list[%d] 0x%x\n", i, processor_list[i]); }
int main () { int res, i; mach_port_t host_privileged_port; device_t device_privileged_port; mach_port_t* host; mach_msg_type_number_t procInfoCnt; processor_info_t procInfo; res = get_privileged_ports(&host_privileged_port, &device_privileged_port); if (res != KERN_SUCCESS) { printf ("Error getting privileged ports (0x%x), %s\n", res, mach_error_string(res)); exit(1); } printf ("privileged ports: host 0x%x devices 0x%x\n", host_privileged_port, device_privileged_port); printf ("Getting processors at array 0x%x\n", processor_list); res = host_processors(host_privileged_port, &processor_list, &processor_listCnt); if (res != KERN_SUCCESS) { printf ("Error getting host_processors (0x%x), %s\n", res, mach_error_string(res)); exit(1); } printf (" processors at array 0x%x\n", processor_list); printf ("processor_listCnt %d\n", processor_listCnt); procInfo = (processor_info_t) malloc(PROCESSOR_INFO_MAX*sizeof(int)); host = (mach_port_t *) malloc(10*sizeof(mach_port_t)); for (i=0; i < processor_listCnt; i++) { printf ("processor_list[%d] 0x%x\n", i, processor_list[i]); procInfoCnt = sizeof(int)*PROCESSOR_INFO_MAX; res = processor_info (processor_list[i], PROCESSOR_BASIC_INFO, host, procInfo, &procInfoCnt); if( res != KERN_SUCCESS) { fprintf(stderr, "Error getting the processor %d information (0x%x), %s\n", i, res, mach_error_string(res)); exit(1); } processor_basic_info_t procBasicInfo = (processor_basic_info_t) &procInfo[i]; fprintf(stdout, "------------CPU %d------------\n", i); fprintf(stdout, "CPU Type:\t\t%d\n", procBasicInfo->cpu_type); fprintf(stdout, "CPU Subtype:\t\t%d\n", procBasicInfo->cpu_subtype); if(procBasicInfo->running) fprintf(stdout, "Running?:\t\tYES\n"); else fprintf(stdout, "Running?:\t\tNO\n"); fprintf(stdout, "Slot number:\t\t%d\n", procBasicInfo->slot_num); if(procBasicInfo->is_master) fprintf(stdout, "Master?:\t\tYES\n"); else fprintf(stdout, "Master?:\t\tNO\n"); fprintf(stdout, "-----------------------------\n"); } }
int main(int argc, char **argv) { kern_return_t kr; host_priv_t host_priv; processor_port_array_t processor_list; natural_t processor_count; char *errmsg = PROGNAME; if (argc != 2) { fprintf(stderr, "usage: %s <cmd>, where <cmd> is \"exit\" or \"start\"\n", PROGNAME); exit(1); } kr = host_get_host_priv_port(mach_host_self(), &host_priv); EXIT_ON_MACH_ERROR("host_get_host_priv_port:", kr); kr = host_processors(host_priv, &processor_list, &processor_count); EXIT_ON_MACH_ERROR("host_processors:", kr); // disable last processor on a multiprocessor system if (processor_count > 1) { if (*argv[1] == 'e') { kr = processor_exit(processor_list[processor_count - 1]); errmsg = "processor_exit:"; } else if (*argv[1] == 's') { kr = processor_start(processor_list[processor_count - 1]); errmsg = "processor_start:"; } else { kr = KERN_INVALID_ARGUMENT; } } else printf("Only one processor!\n"); // this will deallocate while rounding up to page size (void)vm_deallocate(mach_task_self(), (vm_address_t)processor_list, processor_count * sizeof(processor_t *)); EXIT_ON_MACH_ERROR(errmsg, kr); fprintf(stderr, "%s successful\n", errmsg); exit(0); }
/// Initialize Task Info Manager internal data. /// @return Upon successful completion 0 is returned. int task_info_manager_init(void) { static int completed = FALSE; if (completed == TRUE) { return 0; } kern_return_t kr; host_priv_t host_priv; processor_port_array_t processor_list; task_manager_port = mach_host_self(); kr = host_get_host_priv_port(task_manager_port, &host_priv); if (KERN_SUCCESS == kr) { host_processors(host_priv, &processor_list, &processor_count); } CFDictionaryKeyCallBacks tasksKeysCallBask = {0, NULL, NULL, NULL, KeysCompareFunc, KeysHashFunc}; CFDictionaryValueCallBacks tasksValueCallBask = {0, NULL, KeyFreeFunc, NULL, KeysCompareFunc}; tasks_dict = CFDictionaryCreateMutable(NULL, 0, &tasksKeysCallBask, &tasksValueCallBask); host_info_record.seq = 1; int mib[2] = {CTL_HW, HW_MEMSIZE}; size_t size; size = sizeof(host_info_record.memsize); if (sysctl(mib, 2, &host_info_record.memsize, &size, NULL, 0) == -1) { syslog(LOG_WARNING, "%s, Error in sysctl(): %m", __FUNCTION__); return -1; } gettimeofday(&host_info_record.time, NULL); man_info_init(); stack_info_init(); completed = TRUE; return 0; }
static int init (void) { #if PROCESSOR_CPU_LOAD_INFO kern_return_t status; port_host = mach_host_self (); /* FIXME: Free `cpu_list' if it's not NULL */ if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS) { ERROR ("cpu plugin: host_processors returned %i", (int) status); cpu_list_len = 0; return (-1); } DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors"); INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s"); /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(HAVE_LIBKSTAT) kstat_t *ksp_chain; numcpu = 0; if (kc == NULL) return (-1); /* Solaris doesn't count linear.. *sigh* */ for (numcpu = 0, ksp_chain = kc->kc_chain; (numcpu < MAX_NUMCPU) && (ksp_chain != NULL); ksp_chain = ksp_chain->ks_next) if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0) ksp[numcpu++] = ksp_chain; /* #endif HAVE_LIBKSTAT */ #elif CAN_USE_SYSCTL size_t numcpu_size; int mib[2] = {CTL_HW, HW_NCPU}; int status; numcpu = 0; numcpu_size = sizeof (numcpu); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &numcpu, &numcpu_size, NULL, 0); if (status == -1) { char errbuf[1024]; WARNING ("cpu plugin: sysctl: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } /* #endif CAN_USE_SYSCTL */ #elif defined (HAVE_SYSCTLBYNAME) size_t numcpu_size; numcpu_size = sizeof (numcpu); if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } #ifdef HAVE_SYSCTL_KERN_CP_TIMES numcpu_size = sizeof (maxcpu); if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } #else if (numcpu != 1) NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu); #endif /* #endif HAVE_SYSCTLBYNAME */ #elif defined(HAVE_LIBSTATGRAB) /* nothing to initialize */ /* #endif HAVE_LIBSTATGRAB */ #elif defined(HAVE_PERFSTAT) /* nothing to initialize */ #endif /* HAVE_PERFSTAT */ return (0); } /* int init */
static int init (void) { #if PROCESSOR_CPU_LOAD_INFO kern_return_t status; port_host = mach_host_self (); status = host_processors (port_host, &cpu_list, &cpu_list_len); if (status == KERN_INVALID_ARGUMENT) { ERROR ("cpu plugin: Don't have a privileged host control port. " "The most common cause for this problem is " "that collectd is running without root " "privileges, which are required to read CPU " "load information. " "<https://collectd.org/bugs/22>"); cpu_list_len = 0; return (-1); } if (status != KERN_SUCCESS) { ERROR ("cpu plugin: host_processors() failed with status %d.", (int) status); cpu_list_len = 0; return (-1); } INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s"); /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(HAVE_LIBKSTAT) kstat_t *ksp_chain; numcpu = 0; if (kc == NULL) return (-1); /* Solaris doesn't count linear.. *sigh* */ for (numcpu = 0, ksp_chain = kc->kc_chain; (numcpu < MAX_NUMCPU) && (ksp_chain != NULL); ksp_chain = ksp_chain->ks_next) if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0) ksp[numcpu++] = ksp_chain; /* #endif HAVE_LIBKSTAT */ #elif CAN_USE_SYSCTL size_t numcpu_size; int mib[2] = {CTL_HW, HW_NCPU}; int status; numcpu = 0; numcpu_size = sizeof (numcpu); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &numcpu, &numcpu_size, NULL, 0); if (status == -1) { char errbuf[1024]; WARNING ("cpu plugin: sysctl: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } /* #endif CAN_USE_SYSCTL */ #elif defined (HAVE_SYSCTLBYNAME) size_t numcpu_size; numcpu_size = sizeof (numcpu); if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } #ifdef HAVE_SYSCTL_KERN_CP_TIMES numcpu_size = sizeof (maxcpu); if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } #else if (numcpu != 1) NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu); #endif /* #endif HAVE_SYSCTLBYNAME */ #elif defined(HAVE_LIBSTATGRAB) /* nothing to initialize */ /* #endif HAVE_LIBSTATGRAB */ #elif defined(HAVE_PERFSTAT) /* nothing to initialize */ #endif /* HAVE_PERFSTAT */ return (0); } /* int init */
int main(void) { int i; kern_return_t kr; host_name_port_t myhost; host_priv_t host_priv; processor_port_array_t processor_list; natural_t processor_count; processor_basic_info_data_t basic_info; processor_cpu_load_info_data_t cpu_load_info; natural_t info_count; myhost = mach_host_self(); kr = host_get_host_priv_port(myhost, &host_priv); if (kr != KERN_SUCCESS) { mach_error("host_get_host_priv_port:", kr); exit(1); } processor_list = (processor_port_array_t)0; kr = host_processors(host_priv, &processor_list, &processor_count); if (kr != KERN_SUCCESS) { mach_error("host_processors:", kr); exit(1); } // #include <unistd.h> for getpid(2) and sleep(3) printf("processor_list = %p\n", processor_list); printf("my process ID is %d\n", getpid()); sleep(60); printf("%d processors total.\n", processor_count); for (i = 0; i < processor_count; i++) { info_count = PROCESSOR_BASIC_INFO_COUNT; kr = processor_info(processor_list[i], PROCESSOR_BASIC_INFO, &myhost, (processor_info_t)&basic_info, &info_count); if (kr == KERN_SUCCESS) print_basic_info((processor_basic_info_t)&basic_info); info_count = PROCESSOR_CPU_LOAD_INFO_COUNT; kr = processor_info(processor_list[i], PROCESSOR_CPU_LOAD_INFO, &myhost, (processor_info_t)&cpu_load_info, &info_count); if (kr == KERN_SUCCESS) print_cpu_load_info((processor_cpu_load_info_t)&cpu_load_info); } // Other processor information flavors (may be unsupported) // // PROCESSOR_PM_REGS_INFO, // performance monitor register information // PROCESSOR_TEMPERATURE, // core temperature // This will deallocate while rounding up to page size (void)vm_deallocate(mach_task_self(), (vm_address_t)processor_list, processor_count * sizeof(processor_t *)); exit(0); }