View Javadoc
1   /**
2    * Oshi (https://github.com/dblock/oshi)
3    * 
4    * Copyright (c) 2010 - 2015 The Oshi Project Team
5    * 
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   * 
11   * Contributors:
12   * dblock[at]dblock[dot]org
13   * alessandro[at]perucchi[dot]org
14   * widdis[at]gmail[dot]com
15   * https://github.com/dblock/oshi/graphs/contributors
16   */
17  package oshi.software.os.mac.local;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import com.sun.jna.Library;
23  import com.sun.jna.Native;
24  import com.sun.jna.Pointer;
25  import com.sun.jna.Structure;
26  import com.sun.jna.ptr.IntByReference;
27  import com.sun.jna.ptr.LongByReference;
28  import com.sun.jna.ptr.PointerByReference;
29  
30  /**
31   * Memory and CPU stats from vm_stat and sysctl
32   * 
33   * @author widdis[at]gmail[dot]com
34   */
35  public interface SystemB extends Library {
36  	// TODO: Submit this class to JNA
37  	SystemB INSTANCE = (SystemB) Native.loadLibrary("System", SystemB.class);
38  
39  	// host_statistics()
40  	static int HOST_LOAD_INFO = 1;// System loading stats
41  	static int HOST_VM_INFO = 2; // Virtual memory stats
42  	static int HOST_CPU_LOAD_INFO = 3;// CPU load stats
43  
44  	// host_statistics64()
45  	static int HOST_VM_INFO64 = 4; // 64-bit virtual memory stats
46  	static int HOST_EXTMOD_INFO64 = 5;// External modification stats
47  	static int HOST_EXPIRED_TASK_INFO = 6; // Statistics for expired tasks
48  
49  	static int PROCESSOR_CPU_LOAD_INFO = 2;
50  
51  	// sysctl()
52  	static int CTL_KERN = 1; // "high kernel": proc, limits
53  	static int KERN_OSVERSION = 65;
54  
55  	static int CTL_HW = 6; // generic cpu/io
56  	static int HW_MEMSIZE = 24; // uint64_t: physical ram size
57  	static int HW_LOGICALCPU = 103;
58  	static int HW_LOGICALCPU_MAX = 104;
59  	static int HW_CPU64BIT_CAPABLE = 107;
60  
61  	static int CTL_MACHDEP = 7; // machine dependent
62  	static int MACHDEP_CPU = 102; // cpu
63  	static int MACHDEP_CPU_VENDOR = 103;
64  	static int MACHDEP_CPU_BRAND_STRING = 104;
65  	static int MACHDEP_CPU_FAMILY = 105;
66  	static int MACHDEP_CPU_MODEL = 106;
67  	static int MACHDEP_CPU_STEPPING = 109;
68  
69  	// host_cpu_load_info()
70  	static int CPU_STATE_MAX = 4;
71  	static int CPU_STATE_USER = 0;
72  	static int CPU_STATE_SYSTEM = 1;
73  	static int CPU_STATE_IDLE = 2;
74  	static int CPU_STATE_NICE = 3;
75  
76  	// Data size
77  	static int UINT64_SIZE = Native.getNativeSize(long.class);
78  	static int INT_SIZE = Native.getNativeSize(int.class);
79  
80  	public static class HostCpuLoadInfo extends Structure {
81  		public int[] cpu_ticks = new int[CPU_STATE_MAX];
82  
83  		@Override
84  		protected List<String> getFieldOrder() {
85  			return Arrays.asList(new String[] { "cpu_ticks" });
86  		}
87  	}
88  
89  	public static class HostLoadInfo extends Structure {
90  		public int[] avenrun = new int[3]; // scaled by LOAD_SCALE
91  		public int[] mach_factor = new int[3]; // scaled by LOAD_SCALE
92  
93  		@Override
94  		protected List<String> getFieldOrder() {
95  			return Arrays.asList(new String[] { "avenrun", "mach_factor" });
96  		}
97  	}
98  
99  	public static class VMStatistics extends Structure {
100 		public int free_count; // # of pages free
101 		public int active_count; // # of pages active
102 		public int inactive_count; // # of pages inactive
103 		public int wire_count; // # of pages wired down
104 		public int zero_fill_count; // # of zero fill pages
105 		public int reactivations; // # of pages reactivated
106 		public int pageins; // # of pageins
107 		public int pageouts; // # of pageouts
108 		public int faults; // # of faults
109 		public int cow_faults; // # of copy-on-writes
110 		public int lookups; // object cache lookups
111 		public int hits; // object cache hits
112 		public int purgeable_count; // # of pages purgeable
113 		public int purges; // # of pages purged
114 		// # of pages speculative (included in free_count)
115 		public int speculative_count;
116 
117 		@Override
118 		protected List<String> getFieldOrder() {
119 			return Arrays.asList(new String[] { "free_count", "active_count",
120 					"inactive_count", "wire_count", "zero_fill_count",
121 					"reactivations", "pageins", "pageouts", "faults",
122 					"cow_faults", "lookups", "hits", "purgeable_count",
123 					"purges", "speculative_count" });
124 		}
125 	}
126 
127 	public static class VMStatistics64 extends Structure {
128 		public int free_count; // # of pages free
129 		public int active_count; // # of pages active
130 		public int inactive_count; // # of pages inactive
131 		public int wire_count; // # of pages wired down
132 		public long zero_fill_count; // # of zero fill pages
133 		public long reactivations; // # of pages reactivated
134 		public long pageins; // # of pageins
135 		public long pageouts; // # of pageouts
136 		public long faults; // # of faults
137 		public long cow_faults; // # of copy-on-writes
138 		public long lookups; // object cache lookups
139 		public long hits; // object cache hits
140 		public long purges; // # of pages purged
141 		public int purgeable_count; // # of pages purgeable
142 		// # of pages speculative (included in free_count)
143 		public int speculative_count;
144 		public long decompressions; // # of pages decompressed
145 		public long compressions; // # of pages compressed
146 		// # of pages swapped in (via compression segments)
147 		public long swapins;
148 		// # of pages swapped out (via compression segments)
149 		public long swapouts;
150 		// # of pages used by the compressed pager to hold all the
151 		// compressed data
152 		public int compressor_page_count;
153 		public int throttled_count; // # of pages throttled
154 		// # of pages that are file-backed (non-swap)
155 		public int external_page_count;
156 		public int internal_page_count; // # of pages that are anonymous
157 		// # of pages (uncompressed) held within the compressor.
158 		public long total_uncompressed_pages_in_compressor;
159 
160 		@Override
161 		protected List<String> getFieldOrder() {
162 			return Arrays.asList(new String[] { "free_count", "active_count",
163 					"inactive_count", "wire_count", "zero_fill_count",
164 					"reactivations", "pageins", "pageouts", "faults",
165 					"cow_faults", "lookups", "hits", "purges",
166 					"purgeable_count", "speculative_count", "decompressions",
167 					"compressions", "swapins", "swapouts",
168 					"compressor_page_count", "throttled_count",
169 					"external_page_count", "internal_page_count",
170 					"total_uncompressed_pages_in_compressor" });
171 		}
172 	}
173 
174 	int mach_host_self();
175 
176 	int host_page_size(int machPort, LongByReference pPageSize);
177 
178 	int host_statistics(int machPort, int hostStat, Object stats,
179 			IntByReference count);
180 
181 	int host_statistics64(int machPort, int hostStat, Object stats,
182 			IntByReference count);
183 
184 	int host_processor_info(int machPort, int flavor, IntByReference procCount,
185 			PointerByReference procInfo, IntByReference procInfoCount);
186 
187 	int sysctl(int[] name, int namelen, Pointer oldp, IntByReference oldlenp,
188 			Pointer newp, int newlen);
189 
190 	int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp,
191 			Pointer newp, int newlen);
192 }