Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MacTelemetryDetails.cs « MacPlatform « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b2630c153de889f815bec3a69f4bdb5ff46053b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// MacTelemetryDetails.cs
//
// Author:
//       iain <iaholmes@microsoft.com>
//
// Copyright (c) 2018 
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using CoreFoundation;
using Foundation;
using MonoDevelop.Components;
using MonoDevelop.Core;
using MonoDevelop.Ide.Desktop;

namespace MacPlatform
{
	internal class MacTelemetryDetails : IPlatformTelemetryDetails
	{
		int family;
		int coreCount;
		long freq;
		string arch;
		ulong size;
		ulong freeSize;

		PlatformHardDriveMediaType osType;
		TimeSpan sinceLogin;

		internal MacTelemetryDetails ()
		{
		}

		internal static MacTelemetryDetails CreateTelemetryDetails ()
		{
			var result = new MacTelemetryDetails ();

			Interop.SysCtl ("hw.machine", out result.arch);
			Interop.SysCtl ("hw.cpufamily", out result.family);
			Interop.SysCtl ("hw.cpufrequency", out result.freq);
			Interop.SysCtl ("hw.physicalcpu", out result.coreCount);

			var attrs = NSFileManager.DefaultManager.GetFileSystemAttributes ("/");
			result.size = attrs.Size;
			result.freeSize = attrs.FreeSize;

			result.osType = GetMediaType ("/");

			try {
				var login = GetLoginTime ();
				if (login != DateTimeOffset.MinValue) {
					var epoch = new DateTimeOffset (1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);

					var timeSinceEpoch = DateTimeOffset.UtcNow - epoch;
					var loginSinceEpoch = login - epoch;

					result.sinceLogin = timeSinceEpoch - loginSinceEpoch;
				}
			} catch (Exception e) {
				LoggingService.LogError ("Error getting logintime", e);
				result.sinceLogin = TimeSpan.Zero;
			}
			return result;
		}

		public TimeSpan TimeSinceMachineStart => FromNSTimeInterval (NSProcessInfo.ProcessInfo.SystemUptime);

		public TimeSpan TimeSinceLogin => sinceLogin;

		public TimeSpan KernelAndUserTime => TimeSpan.Zero;

		public TimeSpan KernelTime => TimeSpan.Zero;

		public TimeSpan UserTime => TimeSpan.Zero;

		public string CpuArchitecture => arch;

		public int CpuCount => (int)NSProcessInfo.ProcessInfo.ActiveProcessorCount;

		public int PhysicalCpuCount => coreCount;

		public int CpuFamily => family;

		public long CpuFrequency => freq;

		public ulong HardDriveTotalVolumeSize => size;

		public ulong HardDriveFreeVolumeSize => freeSize;

		public ulong RamTotal => NSProcessInfo.ProcessInfo.PhysicalMemory;

		public PlatformHardDriveMediaType HardDriveOsMediaType => osType;

		static PlatformHardDriveMediaType GetMediaType (string path)
		{
			IntPtr diskHandle = IntPtr.Zero;
			IntPtr sessionHandle = IntPtr.Zero;
			IntPtr charDictRef = IntPtr.Zero;
			uint service = 0;

			try {
				sessionHandle = DASessionCreate (IntPtr.Zero);

				// This seems to only work for '/'
				var url = CFUrl.FromFile (path);
				diskHandle = DADiskCreateFromVolumePath (IntPtr.Zero, sessionHandle, url.Handle);
				if (diskHandle == IntPtr.Zero) {
					return PlatformHardDriveMediaType.Unknown;
				}

				service = DADiskCopyIOMedia (diskHandle);

				var cfStr = new CFString ("Device Characteristics");
				charDictRef = IORegistryEntrySearchCFProperty (service, "IOService", cfStr.Handle, IntPtr.Zero, 3);

				// CFDictionary owns the object pointed to by resultHandle, so no need to release it
				var resultHandle = CFDictionaryGetValue (charDictRef, new CFString ("Medium Type").Handle);
				if (resultHandle == IntPtr.Zero) {
					return PlatformHardDriveMediaType.Unknown;
				}
				var resultString = (string)NSString.FromHandle (resultHandle);

				if (resultString == "Solid State") {
					return PlatformHardDriveMediaType.SolidState;
				} else if (resultString == "Rotational") {
					return PlatformHardDriveMediaType.Rotational;
				} else {
					return PlatformHardDriveMediaType.Unknown;
				}
			} finally {
				if (service != 0) {
					IOObjectRelease (service);
				}
				CFRelease (sessionHandle);
				CFRelease (charDictRef);
			}
		}

		[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", EntryPoint="CFRelease")]
		static extern void CFReleaseInternal (IntPtr cfRef);

		static void CFRelease (IntPtr cfRef)
		{
			if (cfRef != IntPtr.Zero)
				CFReleaseInternal (cfRef);
		}

		/*
		 * It appears that getlastlogxbyname only works if you have elevated permissions
		 * but it also doesn't distinguish between user login into the system and user opening a new login terminal
		 */
		static DateTimeOffset GetLoginTime ()
		{
			LastLogX ll = new LastLogX ();
			if (IntPtr.Zero == getlastlogxbyname (Environment.UserName, ref ll)) {
				// getlastlogxbyname doesn't work if SIP is disabled
				return DateTimeOffset.MinValue;
			}

			var dt = DateTimeOffset.FromUnixTimeSeconds (ll.ll_tv_tv_sec);

			return dt;
		}

		[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
		extern static IntPtr IORegistryEntrySearchCFProperty(uint service, string plane, IntPtr key, IntPtr allocator, int options);

		[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
		extern static int IOObjectRelease (uint handle);

		[DllImport ("/System/Library/Frameworks/DiskArbitration.framework/DiskArbitration")]
		extern static IntPtr DADiskCreateFromVolumePath (IntPtr allocator, IntPtr sessionRef, IntPtr pathRef);

		[DllImport ("/System/Library/Frameworks/DiskArbitration.framework/DiskArbitration")]
		extern static IntPtr DASessionCreate (IntPtr allocator);

		[DllImport ("/System/Library/Frameworks/DiskArbitration.framework/DiskArbitration")]
		extern static uint DADiskCopyIOMedia (IntPtr diskRef);

		[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
		extern static IntPtr CFDictionaryGetValue (IntPtr handle, IntPtr keyHandle);

		[StructLayout(LayoutKind.Explicit, Size = 304)]
		struct LastLogX {

			// In C this is a struct timeval but just expand it here
			[FieldOffset (0)]
			public long ll_tv_tv_sec;
			[FieldOffset (8)]
			public int ll_tv_tv_usec;

			#pragma warning disable 0169
			[FieldOffset(16)]
			public byte ll_line;
			[FieldOffset (48)]
			public byte ll_host;
			#pragma warning restore 0169
		}

		[DllImport ("libc")]
		extern static IntPtr getlastlogxbyname (string name, ref LastLogX ll);

		public bool TrySampleHostCpuLoad (out double value)
		{
			return KernelInterop.TrySampleHostCpu (out value);
		}

		[DllImport("libgtk-win32-2.0-0.dll", CallingConvention=CallingConvention.Cdecl)]
		static extern IntPtr gtk_get_current_event ();

		[DllImport ("libgdk-win32-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
		static extern void gdk_event_free (IntPtr raw);

		public TimeSpan GetEventTime (Gdk.EventKey eventKey)
		{
			// Gtk.Application.CurrentEvent and other copied gdk_events seem to have a problem
			// when used as they use gdk_event_copy which seems to crash on de-allocating the private slice.
			IntPtr currentEvent = GtkWorkarounds.GetCurrentEventHandle ();
			bool equals = currentEvent == eventKey.Handle;
			GtkWorkarounds.FreeEvent (currentEvent);

			// If this GDK event is the current Gtk.Application event, assume that NSApplication's
			// current event is the event from which the GDK event was created and use its timestamp
			// instead, which has a much higher precision than the GDK time.
			if (equals)
				return FromNSTimeInterval (AppKit.NSApplication.SharedApplication.CurrentEvent.Timestamp);

			return TimeSpan.FromMilliseconds (eventKey.Time);
		}

		/// <summary>
		/// <para>
		/// Converts a high precision <c>NSTimeInterval</c> to a <see cref="TimeSpan"/>,
		/// preserving as much precision as <see cref="TimeSpan"/> allows.
		/// </para>
		/// <para>
		/// n.b. An <c>NSTimeInterval</c> value is always specified in seconds; it yields
		/// sub-millisecond precision over a range of 10,000 years.
		/// </para>
		/// </summary>
		/// <remarks>
		/// Uses <see cref="TimeSpan.FromTicks"/> and not <see cref="TimeSpan.FromTicks"/>
		/// (which truncates the time value).
		/// </remarks>
		[MethodImpl (MethodImplOptions.AggressiveInlining)]
		static TimeSpan FromNSTimeInterval (double nsTimeInterval)
		{
			const double ticksPerSecond = 1e7;
			return TimeSpan.FromTicks ((long)(nsTimeInterval * ticksPerSecond));
		}
	}
}