]> git.saurik.com Git - apple/system_cmds.git/blob - hostinfo.tproj/hostinfo.c
1993b30c2daca7b9e5897d6ae9a87868619c8b47
[apple/system_cmds.git] / hostinfo.tproj / hostinfo.c
1 /*
2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
13 *
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,
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."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
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>
42 #include <sys/sysctl.h>
43 #include <sys/errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 struct host_basic_info hi;
48 kernel_version_t version;
49 int slots[1024];
50
51 int
52 main(int argc, char *argv[])
53 {
54 kern_return_t ret;
55 unsigned int size, count;
56 char *cpu_name, *cpu_subname;
57 int i;
58 int mib[2];
59 size_t len;
60 uint64_t memsize;
61 processor_set_name_port_t default_pset;
62 host_name_port_t host;
63 struct processor_set_basic_info basic_info;
64 struct processor_set_load_info load_info;
65
66 host = mach_host_self();
67 ret = host_kernel_version(host, version);
68 if (ret != KERN_SUCCESS) {
69 mach_error(argv[0], ret);
70 exit(EXIT_FAILURE);
71 }
72 printf("Mach kernel version:\n\t %s\n", version);
73 size = sizeof(hi)/sizeof(int);
74 ret = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &size);
75 if (ret != KERN_SUCCESS) {
76 mach_error(argv[0], ret);
77 exit(EXIT_FAILURE);
78 }
79
80 ret = processor_set_default(host, &default_pset);
81 if (ret != KERN_SUCCESS) {
82 mach_error(argv[0], ret);
83 exit(EXIT_FAILURE);
84 }
85
86 count = PROCESSOR_SET_BASIC_INFO_COUNT;
87 ret = processor_set_info(default_pset, PROCESSOR_SET_BASIC_INFO,
88 &host, (processor_set_info_t)&basic_info, &count);
89 if (ret != KERN_SUCCESS) {
90 mach_error(argv[0], ret);
91 exit(EXIT_FAILURE);
92 }
93
94 count = PROCESSOR_SET_LOAD_INFO_COUNT;
95 ret = processor_set_statistics(default_pset, PROCESSOR_SET_LOAD_INFO,
96 (processor_set_info_t)&load_info, &count);
97 if (ret != KERN_SUCCESS) {
98 mach_error(argv[0], ret);
99 exit(EXIT_FAILURE);
100 }
101
102 mib[0] = CTL_HW;
103 mib[1] = HW_MEMSIZE;
104 len = sizeof(memsize);
105 memsize = 0L;
106 if(sysctl(mib, 2, &memsize, &len, NULL, 0 ) == -1)
107 {
108 perror("sysctl");
109 exit(EXIT_FAILURE);
110 }
111
112 if (hi.max_cpus > 1)
113 printf("Kernel configured for up to %d processors.\n",
114 hi.max_cpus);
115 else
116 printf("Kernel configured for a single processor only.\n");
117 printf("%d processor%s physically available.\n", hi.physical_cpu,
118 (hi.physical_cpu > 1) ? "s are" : " is");
119
120 printf("%d processor%s logically available.\n", hi.logical_cpu,
121 (hi.logical_cpu > 1) ? "s are" : " is");
122
123 printf("Processor type:");
124 slot_name(hi.cpu_type, hi.cpu_subtype, &cpu_name, &cpu_subname);
125 printf(" %s (%s)\n", cpu_name, cpu_subname);
126
127 printf("Processor%s active:", (hi.avail_cpus > 1) ? "s" : "");
128 for (i = 0; i < hi.avail_cpus; i++)
129 printf(" %d", i);
130 printf("\n");
131
132 if (((float)memsize / (1024.0 * 1024.0)) >= 1024.0)
133 printf("Primary memory available: %.2f gigabytes\n",
134 (float)memsize/(1024.0*1024.0*1024.0));
135 else
136 printf("Primary memory available: %.2f megabytes\n",
137 (float)memsize/(1024.0*1024.0));
138
139 printf("Default processor set: %d tasks, %d threads, %d processors\n",
140 load_info.task_count, load_info.thread_count, basic_info.processor_count);
141 printf("Load average: %d.%02d, Mach factor: %d.%02d\n",
142 load_info.load_average/LOAD_SCALE,
143 (load_info.load_average%LOAD_SCALE)/10,
144 load_info.mach_factor/LOAD_SCALE,
145 (load_info.mach_factor%LOAD_SCALE)/10);
146
147 exit(0);
148 }