]> git.saurik.com Git - apple/system_cmds.git/blame - hostinfo.tproj/hostinfo.c
system_cmds-805.220.1.tar.gz
[apple/system_cmds.git] / hostinfo.tproj / hostinfo.c
CommitLineData
1815bff5 1/*
cf37c299 2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
1815bff5
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
cf37c299 5 *
2fc1e207
A
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
cf37c299 13 *
1815bff5
A
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2fc1e207
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
cf37c299 21 *
1815bff5
A
22 * @APPLE_LICENSE_HEADER_END@
23 */
cf37c299 24/*
1815bff5
A
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * All rights reserved. The CMU software License Agreement specifies
28 * the terms and conditions for use and redistribution.
29 */
30/*
31 * File: hostinfo.c
32 * Author: Avadis Tevanian, Jr.
33 *
34 * Copyright (C) 1987, Avadis Tevanian, Jr.
35 *
36 * Display information about the host this program is
37 * execting on.
38 */
39
40#include <mach/mach.h>
41#include <mach/mach_error.h>
83f6dbe8
A
42#include <sys/sysctl.h>
43#include <sys/errno.h>
1815bff5
A
44#include <stdio.h>
45#include <stdlib.h>
46
47struct host_basic_info hi;
48kernel_version_t version;
49int slots[1024];
50
cf37c299
A
51int
52main(int argc, char *argv[])
1815bff5
A
53{
54 kern_return_t ret;
34d340d7 55 unsigned int size, count;
1815bff5 56 char *cpu_name, *cpu_subname;
cf37c299 57 int mib[2];
83f6dbe8
A
58 size_t len;
59 uint64_t memsize;
1815bff5
A
60 processor_set_name_port_t default_pset;
61 host_name_port_t host;
62 struct processor_set_basic_info basic_info;
63 struct processor_set_load_info load_info;
64
65 host = mach_host_self();
66 ret = host_kernel_version(host, version);
67 if (ret != KERN_SUCCESS) {
68 mach_error(argv[0], ret);
69 exit(EXIT_FAILURE);
70 }
71 printf("Mach kernel version:\n\t %s\n", version);
72 size = sizeof(hi)/sizeof(int);
73 ret = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &size);
74 if (ret != KERN_SUCCESS) {
75 mach_error(argv[0], ret);
76 exit(EXIT_FAILURE);
77 }
78
79 ret = processor_set_default(host, &default_pset);
80 if (ret != KERN_SUCCESS) {
81 mach_error(argv[0], ret);
82 exit(EXIT_FAILURE);
83 }
84
85 count = PROCESSOR_SET_BASIC_INFO_COUNT;
86 ret = processor_set_info(default_pset, PROCESSOR_SET_BASIC_INFO,
87 &host, (processor_set_info_t)&basic_info, &count);
88 if (ret != KERN_SUCCESS) {
89 mach_error(argv[0], ret);
90 exit(EXIT_FAILURE);
91 }
92
93 count = PROCESSOR_SET_LOAD_INFO_COUNT;
94 ret = processor_set_statistics(default_pset, PROCESSOR_SET_LOAD_INFO,
95 (processor_set_info_t)&load_info, &count);
96 if (ret != KERN_SUCCESS) {
97 mach_error(argv[0], ret);
98 exit(EXIT_FAILURE);
99 }
83f6dbe8 100
887d5eed
A
101 unsigned int cpu_count = 0;
102 unsigned int data_count = 0;
103 struct processor_basic_info *processor_basic_infop = NULL;
104 ret = host_processor_info(host,
105 PROCESSOR_BASIC_INFO,
106 &cpu_count,
107 (processor_info_array_t *)&processor_basic_infop,
108 &data_count);
109 if (ret != KERN_SUCCESS) {
110 mach_error(argv[0], ret);
111 exit(EXIT_FAILURE);
112 }
113
83f6dbe8
A
114 mib[0] = CTL_HW;
115 mib[1] = HW_MEMSIZE;
116 len = sizeof(memsize);
117 memsize = 0L;
118 if(sysctl(mib, 2, &memsize, &len, NULL, 0 ) == -1)
119 {
120 perror("sysctl");
121 exit(EXIT_FAILURE);
122 }
123
1815bff5
A
124 if (hi.max_cpus > 1)
125 printf("Kernel configured for up to %d processors.\n",
126 hi.max_cpus);
127 else
128 printf("Kernel configured for a single processor only.\n");
09fd88e4
A
129 printf("%d processor%s physically available.\n", hi.physical_cpu,
130 (hi.physical_cpu > 1) ? "s are" : " is");
131
132 printf("%d processor%s logically available.\n", hi.logical_cpu,
133 (hi.logical_cpu > 1) ? "s are" : " is");
1815bff5
A
134
135 printf("Processor type:");
136 slot_name(hi.cpu_type, hi.cpu_subtype, &cpu_name, &cpu_subname);
137 printf(" %s (%s)\n", cpu_name, cpu_subname);
138
139 printf("Processor%s active:", (hi.avail_cpus > 1) ? "s" : "");
887d5eed
A
140 for (int i = 0; i < cpu_count; i++) {
141 if (processor_basic_infop[i].running) {
142 printf(" %d", i);
143 }
144 }
1815bff5
A
145 printf("\n");
146
83f6dbe8
A
147 if (((float)memsize / (1024.0 * 1024.0)) >= 1024.0)
148 printf("Primary memory available: %.2f gigabytes\n",
149 (float)memsize/(1024.0*1024.0*1024.0));
150 else
151 printf("Primary memory available: %.2f megabytes\n",
152 (float)memsize/(1024.0*1024.0));
cf37c299 153
1815bff5
A
154 printf("Default processor set: %d tasks, %d threads, %d processors\n",
155 load_info.task_count, load_info.thread_count, basic_info.processor_count);
156 printf("Load average: %d.%02d, Mach factor: %d.%02d\n",
157 load_info.load_average/LOAD_SCALE,
158 (load_info.load_average%LOAD_SCALE)/10,
159 load_info.mach_factor/LOAD_SCALE,
160 (load_info.mach_factor%LOAD_SCALE)/10);
83f6dbe8
A
161
162 exit(0);
1815bff5 163}