override public String[] getValues() { NativeMethods.MEMORYSTATUSEX status = new NativeMethods.MEMORYSTATUSEX(); status.dwLength = (uint)Marshal.SizeOf(status); Boolean ret = NativeMethods.GlobalMemoryStatusEx(ref status); if (ret) { String[] value = new String[4]; value[0] = String.Format("apps.value {0}\n", status.ulTotalPhys - status.ulAvailPhys); ulong swap = ((status.ulTotalPageFile - status.ulAvailPageFile) < (status.ulTotalPhys - status.ulAvailPhys)) ? 0 : status.ulTotalPageFile - status.ulAvailPageFile - status.ulTotalPhys + status.ulAvailPhys; value[1] = String.Format("swap.value {0}\n", swap); value[2] = String.Format("free.value {0}\n", status.ulAvailPhys); value[3] = ".\n"; return(value); } else { return(new String[] { ".\n" }); } }
override public String[] getValues() { NativeMethods.MEMORYSTATUSEX status = new NativeMethods.MEMORYSTATUSEX(); status.dwLength = (uint)Marshal.SizeOf(status); Boolean ret = NativeMethods.GlobalMemoryStatusEx(ref status); if (ret) { String[] value = new String[4]; value[0] = String.Format("apps.value {0}\n", status.ulTotalPhys - status.ulAvailPhys); ulong swap = ((status.ulTotalPageFile - status.ulAvailPageFile) < (status.ulTotalPhys - status.ulAvailPhys)) ? 0 : status.ulTotalPageFile - status.ulAvailPageFile - status.ulTotalPhys + status.ulAvailPhys; value[1] = String.Format("swap.value {0}\n", swap); value[2] = String.Format("free.value {0}\n", status.ulAvailPhys); value[3] = ".\n"; return value; } else { return new String[] { ".\n" }; } }
/// <summary> /// Retrieve the available memory (in bytes) as appropriate /// for the current platform /// </summary> /// <returns>Bytes of available memory</returns> public static ulong GetAvailableMemory() { if (IsWindows) { try { var memStatus = new NativeMethods.MEMORYSTATUSEX(); if (NativeMethods.GlobalMemoryStatusEx(memStatus)) { return(memStatus.ullAvailPhys); } } catch (TypeLoadException) { // GlobalMemoryStatusEx exists in all Windows editions DMLib is supported on, // but watch for the case where it doesn't out of an abundance of caution return(0); } } if (IsLinux) { var memInfo = NativeMethods.GetLinuxMemoryInfo(); if (memInfo.ContainsKey("MemAvailable") && memInfo["MemAvailable"] != 0) { // Newer Linux builds will include a 'MemAvailable' statistic which contains // the necessary information return(memInfo["MemAvailable"] * 1024); // MemInfo values are stored as kB } else if (memInfo.ContainsKey("MemFree") && memInfo.ContainsKey("Cached")) { // If MemAvailable is not available, it can be approximated by summing // MemFree and Cached return((memInfo["MemFree"] + memInfo["Cached"]) * 1024); // MemInfo values are stored as kB } else { return(0); } } if (IsOSX) { try { // Get page size var pageSize = 4096; // Default page size to 4096 if sysctl fails #if GENERIC_MARSHAL_SIZEOF var pageSizeSize = Marshal.SizeOf <int>(); #else // GENERIC_MARSHAL_SIZEOF var pageSizeSize = Marshal.SizeOf(typeof(int)); #endif // GENERIC_MARSHAL_SIZEOF if (0 != NativeMethods.sysctlbyname("hw.pagesize", ref pageSize, ref pageSizeSize, IntPtr.Zero, 0)) { // May need to get error message here. return(0); } // Get number of free pages var freePages = 0; #if GENERIC_MARSHAL_SIZEOF var freePagesSize = Marshal.SizeOf <int>(); #else // GENERIC_MARSHAL_SIZEOF var freePagesSize = Marshal.SizeOf(typeof(int)); #endif // GENERIC_MARSHAL_SIZEOF if (0 != NativeMethods.sysctlbyname("vm.page_free_count", ref freePages, ref freePagesSize, IntPtr.Zero, 0)) { return(0); } if (freePages == 0) { // If sysctl failed to retrieve the number of free pages, try with host_statistics var vmStats = NativeMethods.GetOSXHostStatistics(); freePages = vmStats.free_count; } return(((ulong)freePages) * ((ulong)pageSize)); } catch (TypeLoadException) { // In case this is somehow run on an OSX platform without sysctl or host statistics APIs exposed return(0); } } // If the platform is unrecognized, default to returning 0 return(0); }
private void ReportUserInfo() { if (Properties.Settings.Default.UserInfoReported) { Debug.WriteLine("UserInfoReported"); return; } ThreadStart ts = new ThreadStart(delegate() { try { //string url = "http://10.211.55.2/report.php"; string url = "http://ttkz.me/keymagic/report/report.php"; WebRequest request = (WebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Dictionary<string, string> postData = new Dictionary<string, string>(); postData["OSVersion"] = Environment.OSVersion.ToString(); postData["64bit"] = is64bit.ToString(); NativeMethods.MEMORYSTATUSEX mem = new NativeMethods.MEMORYSTATUSEX(); mem.dwLength = (uint)Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX)); NativeMethods.GlobalMemoryStatusEx(ref mem); postData["RAM"] = (mem.ullTotalPhys / 1024 / 1024).ToString() + " MB"; List<byte> postByte = new List<byte>(); foreach (var e in postData) { postByte.AddRange(Encoding.ASCII.GetBytes(e.Key + "=" + e.Value + "&")); } request.ContentLength = postByte.Count; using (Stream req = request.GetRequestStream()) { req.Write(postByte.ToArray(), 0, postByte.Count); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { using (Stream res = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(res)) { string ret = sr.ReadToEnd(); if (ret.Equals("SUCCESS")) { Properties.Settings.Default.UserInfoReported = true; } else { Properties.Settings.Default.UserInfoReported = false; } } } } } } catch (Exception e) { Debug.WriteLine(e.Message); } }); Thread t = new Thread(ts); t.Start(); threads["ReportUserInfo"] = t; }
public static string GetEcpWatsonExtraData(HttpContext context, Exception ex) { StringBuilder stringBuilder = new StringBuilder(); if (context != null && context.Request != null) { stringBuilder.Append("context.Request.Url = "); stringBuilder.AppendLine(context.GetRequestUrlForLog()); if (context.HasTargetTenant() || context.IsExplicitSignOn()) { stringBuilder.Append("RawUrl = "); stringBuilder.AppendLine(context.Request.RawUrl); } } if (context != null && context.Request.Cookies != null) { HttpCookie httpCookie = context.Request.Cookies["TCMID"]; if (httpCookie != null) { stringBuilder.Append("TestCaseID: "); stringBuilder.AppendLine(httpCookie.Value); } } foreach (object obj in ex.Data.Keys) { object obj2 = ex.Data[obj]; stringBuilder.Append(obj); stringBuilder.Append(": "); stringBuilder.AppendLine((obj2 == null) ? "null" : obj2.ToString()); } if (ex is OutOfMemoryException) { stringBuilder.Append("Managed Memory: ").AppendLine(GC.GetTotalMemory(false).ToString()); using (Process currentProcess = Process.GetCurrentProcess()) { stringBuilder.Append("Private Bytes: ").AppendLine(currentProcess.PrivateMemorySize64.ToString()); stringBuilder.Append("Working Set: ").AppendLine(currentProcess.WorkingSet64.ToString()); } NativeMethods.MEMORYSTATUSEX memorystatusex = default(NativeMethods.MEMORYSTATUSEX); memorystatusex.dwLength = (uint)Marshal.SizeOf(memorystatusex); if (NativeMethods.GlobalMemoryStatusEx(ref memorystatusex)) { stringBuilder.Append("Available Physical Memory: ").Append(memorystatusex.ullAvailPhys.ToString()).Append("/").Append(memorystatusex.ullTotalPhys.ToString()).Append("(").Append((memorystatusex.ullAvailPhys * 100UL / memorystatusex.ullTotalPhys).ToString()).AppendLine("%)"); stringBuilder.Append("Available Virtual Memory: ").Append(memorystatusex.ullAvailVirtual.ToString()).Append("/").Append(memorystatusex.ullTotalPhys.ToString()).Append("(").Append((memorystatusex.ullAvailVirtual * 100UL / memorystatusex.ullTotalVirtual).ToString()).AppendLine("%)"); } stringBuilder.AppendLine("**Top 5 Apps:**"); foreach (Process process in (from x in Process.GetProcesses() orderby x.PrivateMemorySize64 descending select x).Take(5)) { using (process) { stringBuilder.Append(process.ProcessName).Append("#").Append(process.Id.ToString()).Append(": ").AppendLine(process.PrivateMemorySize64.ToString()); } } } if (stringBuilder.Length == 0) { stringBuilder.Append("Couldn't extract extra watson data from the context or exception."); } return(stringBuilder.ToString()); }