/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate.</param> /// <param name="dr">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return.</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> private static AsyncRes Do(DlgR dr, Dlg d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode, Control control, bool async) { //get a generic MethodInfo for checks.. MethodInfo mi = ((dr != null) ? dr.Method : d.Method); //make a unique key for output usage string key = string.Format("{0}{1}{2}{3}", ((getRetVal) ? "<-" : ""), mi.DeclaringType, ((mi.IsStatic) ? ":" : "."), mi.Name); //our custom return value, holds our delegate, state, key, etc. AsyncRes res = new AsyncRes(state, ((dr != null) ? (Delegate)dr : (Delegate)d), key, rMode); //Create a delegate wrapper for what we will actually invoke.. Dlg dlg = (Dlg) delegate { if (!BeforeInvoke(res)) { return; //checks for reentrance issues and sets us up } try { if (res.IsCompleted) { return; } if (dr != null) { res.retVal = dr(); //use this one if theres a return } else { d(); //otherwise the simpler dlg } } catch (Exception ex) { //we never want a rogue exception on a random thread, it can't bubble up anywhere Console.WriteLine("Async Exception:" + ex); } finally { FinishInvoke(res); //this will fire our callback if they used it, and clean up } }; if (control != null) { res.control = control; res.result = AsyncAction.ControlInvoked; if (!async) { if (!control.InvokeRequired) { res.completedSynchronously = true; dlg(); } else { control.Invoke(dlg); } } else { control.BeginInvoke(dlg); } return(res); } //don't catch these errors - if this fails, we shouldn't try a real thread or threadpool! if (tryThreadPool) //we are going to use the .NET threadpool { try { //get some stats - much better than trying and silently failing or throwing an expensive exception int minThreads, minIO, threads, ioThreads, totalThreads, totalIO; ThreadPool.GetMinThreads(out minThreads, out minIO); ThreadPool.GetAvailableThreads(out threads, out ioThreads); ThreadPool.GetMaxThreads(out totalThreads, out totalIO); //check for at least our thread plus one more in ThreadPool if (threads > minThreads) { //this is what actually fires this task off.. bool result = ThreadPool.QueueUserWorkItem((WaitCallback) delegate { dlg(); }); if (result) { res.result = AsyncAction.ThreadPool; //this means success in queueing and running the item return(res); } else { //according to docs, this "won't ever happen" - exception instead, but just for kicks. Console.WriteLine("Failed to queue in threadpool.", "Method: " + key); } } else { Console.WriteLine(String.Format("Insufficient idle threadpool threads: {0} of {1} - min {2}, Method: {3}", threads, totalThreads, minThreads, key)); } } catch (Exception ex) { Console.WriteLine("Failed to queue in threadpool: " + ex.Message, "Method: " + key); } } //if we got this far, then something up there failed, or they wanted a dedicated thread Thread t = new Thread((ThreadStart) delegate { dlg(); }); t.IsBackground = true; //this or threadpriority are candidates for additional settings t.Name = "Async_" + key; res.result = AsyncAction.Thread; t.Start(); return(res); }
// Token: 0x06000018 RID: 24 RVA: 0x0000283C File Offset: 0x00000A3C private void OnStartInternalHelper() { MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.StartService); MailboxTransportSubmissionService.StoreDriverTracer.ServiceTracer.TracePfdPass <int, DateTime>(MailboxTransportSubmissionService.StoreDriverTracer.MessageProbeActivityId, 0L, "PFD EMS {0} Starting MailboxTransportSubmissionService ({1})", 24475, DateTime.UtcNow); bool flag = false; bool flag2 = false; string text = null; try { MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.RegisterPamComponent); ProcessAccessManager.RegisterComponent(this); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.InitializePerformanceMonitoring); MailboxTransportSubmissionService.InitializePerfMon(); MailboxTransportSubmissionService.StoreDriverTracer.ServiceTracer.TracePfdPass <int, DateTime>(MailboxTransportSubmissionService.StoreDriverTracer.MessageProbeActivityId, 0L, "PFD EMS {0} Finished Loading Perfmon ({1})", 32667, DateTime.UtcNow); TransportADNotificationAdapter.Instance.RegisterForSubmissionServiceEvents(); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.LoadConfiguration); StorageExceptionHandler.Init(); this.ConstructComponentLoadTree(); SubmissionConfiguration.Instance.Load(); int num; ThreadPool.GetMaxThreads(out this.maxThreads, out num); ITimeBasedAssistantType[] timeBasedAssistantTypeArray = null; IEventBasedAssistantType[] eventBasedAssistantTypeArray = new IEventBasedAssistantType[] { new MailboxTransportSubmissionAssistantType() }; this.databaseManager = new DatabaseManager("Microsoft Exchange Mailbox Transport Submission", MailboxTransportSubmissionAssistant.MaxConcurrentSubmissions, eventBasedAssistantTypeArray, timeBasedAssistantTypeArray, false); MailboxTransportSubmissionEventLogger.LogEvent(MSExchangeSubmissionEventLogConstants.Tuple_SubmissionServiceStartSuccess, null, new object[0]); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.CreateBackgroundThread); MailboxTransportSubmissionService.backgroundThread = new BackgroundProcessingThread(); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.StartBackgroundThread); MailboxTransportSubmissionService.backgroundThread.Start(false, ServiceState.Active); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.StartDatabaseManager); this.databaseManager.Start(); MailboxTransportSubmissionService.LogStage(MailboxTransportSubmissionService.Stage.ServiceStarted); MailboxTransportSubmissionService.StoreDriverTracer.ServiceTracer.TracePfdPass <int>(MailboxTransportSubmissionService.StoreDriverTracer.MessageProbeActivityId, 0L, "PFD EMS {0} MailTransportSubmissionService Started", 26523); flag = true; } catch (ConfigurationErrorsException ex) { text = ex.Message; flag2 = true; } catch (HandlerParseException ex2) { text = ex2.Message; flag2 = true; } finally { if (!flag) { MailboxTransportSubmissionEventLogger.LogEvent(MSExchangeSubmissionEventLogConstants.Tuple_SubmissionServiceStartFailure, null, new object[] { text }); MailboxTransportSubmissionService.StoreDriverTracer.ServiceTracer.TracePfdPass(MailboxTransportSubmissionService.StoreDriverTracer.MessageProbeActivityId, 0L, "Failed to start MailboxTransportSubmissionService"); base.ExRequestAdditionalTime(60000); if (flag2) { base.Stop(); } } } }
public void Initalize() { logger.Info("Starting Jackett " + configService.GetVersion()); try { var x = Environment.OSVersion; var runtimedir = RuntimeEnvironment.GetRuntimeDirectory(); logger.Info("Environment version: " + Environment.Version.ToString() + " (" + runtimedir + ")"); logger.Info("OS version: " + Environment.OSVersion.ToString() + (Environment.Is64BitOperatingSystem ? " (64bit OS)" : "") + (Environment.Is64BitProcess ? " (64bit process)" : "")); try { int workerThreads; int completionPortThreads; ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); logger.Info("ThreadPool MaxThreads: " + workerThreads + " workerThreads, " + completionPortThreads + " completionPortThreads"); } catch (Exception e) { logger.Error("Error while getting MaxThreads details: " + e); } try { var issuefile = "/etc/issue"; if (File.Exists(issuefile)) { using (StreamReader reader = new StreamReader(issuefile)) { string firstLine; firstLine = reader.ReadLine(); if (firstLine != null) { logger.Info("issue: " + firstLine); } } } } catch (Exception e) { logger.Error(e, "Error while reading the issue file"); } Type monotype = Type.GetType("Mono.Runtime"); if (monotype != null) { MethodInfo displayName = monotype.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); var monoVersion = "unknown"; if (displayName != null) { monoVersion = displayName.Invoke(null, null).ToString(); } logger.Info("mono version: " + monoVersion); var monoVersionO = new Version(monoVersion.Split(' ')[0]); if (monoVersionO.Major < 4) { logger.Error("Your mono version is to old (mono 3 is no longer supported). Please update to the latest version from http://www.mono-project.com/download/"); Environment.Exit(2); } else if (monoVersionO.Major == 4 && monoVersionO.Minor == 2) { var notice = "mono version 4.2.* is known to cause problems with Jackett. If you experience any problems please try updating to the latest mono version from http://www.mono-project.com/download/ first."; _notices.Add(notice); logger.Error(notice); } try { // Check for mono-devel // Is there any better way which doesn't involve a hard cashes? var mono_devel_file = Path.Combine(runtimedir, "mono-api-info.exe"); if (!File.Exists(mono_devel_file)) { var notice = "It looks like the mono-devel package is not installed, please make sure it's installed to avoid crashes."; _notices.Add(notice); logger.Error(notice); } } catch (Exception e) { logger.Error(e, "Error while checking for mono-devel"); } try { // Check for ca-certificates-mono var mono_cert_file = Path.Combine(runtimedir, "cert-sync.exe"); if (!File.Exists(mono_cert_file)) { if (monoVersionO.Major >= 4 && monoVersionO.Minor >= 8) { var notice = "The ca-certificates-mono package is not installed, HTTPS trackers won't work. Please install it."; _notices.Add(notice); logger.Error(notice); } else { logger.Info("The ca-certificates-mono package is not installed, it will become mandatory once mono >= 4.8 is used."); } } } catch (Exception e) { logger.Error(e, "Error while checking for ca-certificates-mono"); } try { Encoding.GetEncoding("windows-1255"); } catch (NotSupportedException e) { logger.Debug(e); logger.Error(e.Message + " Most likely the mono-locale-extras package is not installed."); Environment.Exit(2); } } } catch (Exception e) { logger.Error("Error while getting environment details: " + e); } CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); // Load indexers indexerService.InitIndexers(); foreach (string dir in configService.GetCardigannDefinitionsFolders()) { indexerService.InitCardigannIndexers(dir); } indexerService.InitAggregateIndexer(); indexerService.SortIndexers(); client.Init(); updater.CleanupTempDir(); }
public static void SetMinMaxThreadsTest() { RemoteExecutor.Invoke(() => { int minw, minc, maxw, maxc; ThreadPool.GetMinThreads(out minw, out minc); ThreadPool.GetMaxThreads(out maxw, out maxc); try { int mint = Environment.ProcessorCount * 2; int maxt = mint + 1; ThreadPool.SetMinThreads(mint, mint); ThreadPool.SetMaxThreads(maxt, maxt); Assert.False(ThreadPool.SetMinThreads(maxt + 1, mint)); Assert.False(ThreadPool.SetMinThreads(mint, maxt + 1)); Assert.False(ThreadPool.SetMinThreads(MaxPossibleThreadCount, mint)); Assert.False(ThreadPool.SetMinThreads(mint, MaxPossibleThreadCount)); Assert.False(ThreadPool.SetMinThreads(MaxPossibleThreadCount + 1, mint)); Assert.False(ThreadPool.SetMinThreads(mint, MaxPossibleThreadCount + 1)); Assert.False(ThreadPool.SetMinThreads(-1, mint)); Assert.False(ThreadPool.SetMinThreads(mint, -1)); Assert.False(ThreadPool.SetMaxThreads(mint - 1, maxt)); Assert.False(ThreadPool.SetMaxThreads(maxt, mint - 1)); VerifyMinThreads(mint, mint); VerifyMaxThreads(maxt, maxt); Assert.True(ThreadPool.SetMaxThreads(MaxPossibleThreadCount, MaxPossibleThreadCount)); VerifyMaxThreads(MaxPossibleThreadCount, MaxPossibleThreadCount); Assert.True(ThreadPool.SetMaxThreads(MaxPossibleThreadCount + 1, MaxPossibleThreadCount + 1)); VerifyMaxThreads(MaxPossibleThreadCount, MaxPossibleThreadCount); Assert.Equal(PlatformDetection.IsNetFramework, ThreadPool.SetMaxThreads(-1, -1)); VerifyMaxThreads(MaxPossibleThreadCount, MaxPossibleThreadCount); Assert.True(ThreadPool.SetMinThreads(MaxPossibleThreadCount, MaxPossibleThreadCount)); VerifyMinThreads(MaxPossibleThreadCount, MaxPossibleThreadCount); Assert.False(ThreadPool.SetMinThreads(MaxPossibleThreadCount + 1, MaxPossibleThreadCount)); Assert.False(ThreadPool.SetMinThreads(MaxPossibleThreadCount, MaxPossibleThreadCount + 1)); Assert.False(ThreadPool.SetMinThreads(-1, MaxPossibleThreadCount)); Assert.False(ThreadPool.SetMinThreads(MaxPossibleThreadCount, -1)); VerifyMinThreads(MaxPossibleThreadCount, MaxPossibleThreadCount); Assert.True(ThreadPool.SetMinThreads(0, 0)); Assert.True(ThreadPool.SetMaxThreads(1, 1)); VerifyMaxThreads(1, 1); Assert.True(ThreadPool.SetMinThreads(1, 1)); VerifyMinThreads(1, 1); } finally { Assert.True(ThreadPool.SetMaxThreads(maxw, maxc)); VerifyMaxThreads(maxw, maxc); Assert.True(ThreadPool.SetMinThreads(minw, minc)); VerifyMinThreads(minw, minc); } }).Dispose(); }
private void HandleDebugThreadpoolSet(string module, string[] args) { if (args.Length != 6) { Notice("Usage: debug threadpool set worker|iocp min|max <n>"); return; } int newThreads; if (!ConsoleUtil.TryParseConsoleInt(m_console, args[5], out newThreads)) { return; } string poolType = args[3]; string bound = args[4]; bool fail = false; int workerThreads, iocpThreads; if (poolType == "worker") { if (bound == "min") { ThreadPool.GetMinThreads(out workerThreads, out iocpThreads); if (!ThreadPool.SetMinThreads(newThreads, iocpThreads)) { fail = true; } } else { ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads); if (!ThreadPool.SetMaxThreads(newThreads, iocpThreads)) { fail = true; } } } else { if (bound == "min") { ThreadPool.GetMinThreads(out workerThreads, out iocpThreads); if (!ThreadPool.SetMinThreads(workerThreads, newThreads)) { fail = true; } } else { ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads); if (!ThreadPool.SetMaxThreads(workerThreads, newThreads)) { fail = true; } } } if (fail) { Notice("ERROR: Could not set {0} {1} threads to {2}", poolType, bound, newThreads); } else { int minWorkerThreads, maxWorkerThreads, minIocpThreads, maxIocpThreads; ThreadPool.GetMinThreads(out minWorkerThreads, out minIocpThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxIocpThreads); Notice("Min worker threads now {0}", minWorkerThreads); Notice("Min IOCP threads now {0}", minIocpThreads); Notice("Max worker threads now {0}", maxWorkerThreads); Notice("Max IOCP threads now {0}", maxIocpThreads); } }
public void Collect(StatusInfoCollection statusCollection) { int availableWorkingThreads, availableCompletionPortThreads; ThreadPool.GetAvailableThreads(out availableWorkingThreads, out availableCompletionPortThreads); int maxWorkingThreads; int maxCompletionPortThreads; ThreadPool.GetMaxThreads(out maxWorkingThreads, out maxCompletionPortThreads); var retry = false; while (true) { try { statusCollection[StatusInfoKeys.AvailableWorkingThreads] = availableWorkingThreads; statusCollection[StatusInfoKeys.AvailableCompletionPortThreads] = availableCompletionPortThreads; statusCollection[StatusInfoKeys.MaxCompletionPortThreads] = maxCompletionPortThreads; statusCollection[StatusInfoKeys.MaxWorkingThreads] = maxWorkingThreads; #if !NETSTANDARD2_0 statusCollection[StatusInfoKeys.TotalThreadCount] = (int)m_ThreadCountPC.NextValue(); statusCollection[StatusInfoKeys.CpuUsage] = m_CpuUsagePC.NextValue() / m_CpuCores; statusCollection[StatusInfoKeys.MemoryUsage] = (long)m_WorkingSetPC.NextValue(); #else var proc = Process.GetCurrentProcess(); statusCollection[StatusInfoKeys.TotalThreadCount] = proc.Threads.Count; statusCollection[StatusInfoKeys.CpuUsage] = proc.TotalProcessorTime.TotalMilliseconds / 1000; statusCollection[StatusInfoKeys.MemoryUsage] = proc.WorkingSet64 / 1024; #endif break; } catch (InvalidOperationException e) { //Only re-get performance counter one time if (retry) { throw e; } //Only re-get performance counter for .NET/Windows if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX || Platform.IsMono) { throw e; } //If a same name process exited, this process's performance counters instance name could be changed, //so if the old performance counter cannot be access, get the performance counter's name again var newInstanceName = GetPerformanceCounterInstanceName(m_Process); if (string.IsNullOrEmpty(newInstanceName)) { break; } SetupPerformanceCounters(newInstanceName); retry = true; } } }
public static void Main(string[] args) { ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.Layout = new PatternLayout("[%thread] - %message%newline"); log4net.Config.BasicConfigurator.Configure(consoleAppender); string serverURI = "http://127.0.0.1:8003"; if (args.Length > 1) { serverURI = args[1]; } int max1, max2; ThreadPool.GetMaxThreads(out max1, out max2); m_log.InfoFormat("[ASSET CLIENT]: Connecting to {0} max threads = {1} - {2}", serverURI, max1, max2); ThreadPool.GetMinThreads(out max1, out max2); m_log.InfoFormat("[ASSET CLIENT]: Connecting to {0} min threads = {1} - {2}", serverURI, max1, max2); if (!ThreadPool.SetMinThreads(1, 1)) { m_log.WarnFormat("[ASSET CLIENT]: Failed to set min threads"); } if (!ThreadPool.SetMaxThreads(10, 3)) { m_log.WarnFormat("[ASSET CLIENT]: Failed to set max threads"); } ThreadPool.GetMaxThreads(out max1, out max2); m_log.InfoFormat("[ASSET CLIENT]: Post set max threads = {1} - {2}", serverURI, max1, max2); ThreadPool.GetMinThreads(out max1, out max2); m_log.InfoFormat("[ASSET CLIENT]: Post set min threads = {1} - {2}", serverURI, max1, max2); ServicePointManager.DefaultConnectionLimit = 12; AssetServicesConnector m_Connector = new AssetServicesConnector(serverURI); m_Connector.MaxAssetRequestConcurrency = 30; for (int i = 0; i < NREQS; i++) { UUID uuid = UUID.Random(); m_Connector.Get(uuid.ToString(), null, ResponseReceived); m_log.InfoFormat("[ASSET CLIENT]: [{0}] requested asset {1}", i, uuid); } for (int i = 0; i < 500; i++) { var x = i; ThreadPool.QueueUserWorkItem(delegate { Dummy(x); }); } Thread.Sleep(30 * 1000); m_log.InfoFormat("[ASSET CLIENT]: Received responses {0}", m_NReceived); }
static void Main(string[] args) { if (args.Length != 4 && args.Length != 0) { Console.WriteLine("usage: SharpLink [local_port] [target_tox_id] [target_ip] [target_port]"); return; } Skynet.Base.Skynet mSkynet = null; string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Directory.SetCurrentDirectory(exeDir); if (args.Length == 0) { // log to file Utils.setLogFile("server.log"); } else { // log to file Utils.setLogFile("client.log"); } // Save tox data for server if (args.Length == 0 && File.Exists("tox.dat")) { mSkynet = new Skynet.Base.Skynet("tox.dat"); } else if (args.Length == 0 && !File.Exists("tox.dat")) { mSkynet = new Skynet.Base.Skynet(); mSkynet.Save("tox.dat"); } else { mSkynet = new Skynet.Base.Skynet(); } // 线程监控程序 Task.Run(() => { while (runningFlag) { int workerThreads, completionPortThreads; ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); int workerThreadsMax, completionPortThreadsMax; ThreadPool.GetMaxThreads(out workerThreadsMax, out completionPortThreadsMax); int workerThreadsMin, completionPortThreadsMin; ThreadPool.GetMinThreads(out workerThreadsMin, out completionPortThreadsMin); ThreadPool.SetMinThreads(workerThreadsMax - workerThreads + 40, workerThreadsMax - workerThreads + 40); Thread.Sleep(2000); } }); if (args.Length == 4) { string localPort = args[0]; string targetToxId = args[1]; string targetIP = args[2]; int targetPort = Convert.ToInt32(args[3]); if (!ToxId.IsValid(targetToxId)) { Console.WriteLine("not a valid id"); Console.WriteLine("usage: SharpLink [local_port] [target_tox_id] [target_ip] [target_port]"); return; } // 连接维护程序 Task.Run(() => { while (runningFlag) { IsConnected = mSkynet.HandShake(new ToxId(targetToxId)).GetAwaiter().GetResult(); if (!IsConnected) { var toxid = new ToxId(targetToxId); ToxKey toxkey = toxid.PublicKey; int friendNum = mSkynet.tox.GetFriendByPublicKey(toxkey); if (friendNum == -1) { mSkynet.tox.DeleteFriend(friendNum); } } Thread.Sleep(60 * 1000); } }); // create local socket server IPAddress ip = IPAddress.Parse("0.0.0.0"); var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(ip, Convert.ToInt32(localPort))); serverSocket.Listen(1000); Task.Factory.StartNew(() => { while (true) { Utils.Log("Event: Waiting socket"); List <byte> tempData = new List <byte>(); Socket clientSocket = serverSocket.Accept(); Task.Factory.StartNew(() => { bool closeFlag = false; LinkClient mlink = null; string tempConnectId = Guid.NewGuid().ToString(); Task.Factory.StartNew(() => { while (true) { byte[] buf = new byte[1024 * 512]; try { int size = 0; if (clientSocket != null && clientSocket.Connected) { size = clientSocket.Receive(buf); } else { break; } if (mlink == null) { tempData.AddRange(buf.Take(size)); } if (size == 0) { // socket closed if (mlink != null) { mlink.CloseRemote(); mlink.Close(); } if (!closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); if (mlink != null) { Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); } else { Utils.Log("Event: Close Connection, ClinetId: null" + ", ConnectId: " + tempConnectId); } } break; } if (mlink != null) { var res = mlink.Send(buf, size); if (!res && !closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); mlink.CloseRemote(); mlink.Close(); Utils.Log("Event: Tox send message failed, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); break; } } } catch (Exception e) { Utils.Log("Event: ERROR " + e.Message); Utils.Log(e.StackTrace); if (mlink != null) { mlink.CloseRemote(); mlink.Close(); } if (!closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); if (mlink != null) { Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); } else { Utils.Log("Event: Close Connection, ClinetId: null" + ", ConnectId: " + tempConnectId); } } break; } } }, TaskCreationOptions.LongRunning).ForgetOrThrow(); mlink = LinkClient.Connect(mSkynet, targetToxId, IPAddress.Parse(targetIP), Convert.ToInt32(targetPort), // message handler (msg) => { try { if (clientSocket != null && clientSocket.Connected) { clientSocket.Send(msg, SocketFlags.None); } } catch (Exception e) { Utils.Log("ERROR " + e.Message); Utils.Log(e.StackTrace); mlink.CloseRemote(); mlink.Close(); if (!closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); } } }, // close handler () => { if (!closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); } } ); if (mlink == null) { // connected failed Utils.Log("Event: Connected failed, ClientId: null" + ", ConnectId: " + tempConnectId); if (!closeFlag && clientSocket.Connected) { closeFlag = true; try { clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event ERROR: " + ex.Message); } clientSocket.Close(); Utils.Log("Event: Close Connection, ClientId: null" + ", ConnectId: " + tempConnectId); } return; } if (tempData.Count != 0) { mlink.Send(tempData.ToArray(), tempData.Count); } // check if socket has closed if (closeFlag) { // socket has closed Utils.Log("Event: Close Remote, ClientId: " + mlink.clientId + ", ConnectId: " + tempConnectId); mlink.CloseRemote(); mlink.Close(); } }, TaskCreationOptions.LongRunning).ForgetOrThrow(); } }, TaskCreationOptions.LongRunning).ForgetOrThrow(); } mSkynet.addNewReqListener("", (req) => { // handle if (req.toNodeId == "" && req.url == "/connect") { Utils.Log("Event: Task Connect to " + req.fromNodeId + ", MessageId: " + req.uuid); Task.Factory.StartNew(() => { // connect to server received, create sockets Utils.Log("Event: Task Started Connect to " + req.fromNodeId); try { string reqStr = Encoding.UTF8.GetString(req.content); string ipstr = reqStr.Split('\n')[0]; string port = reqStr.Split('\n')[1]; Utils.Log("Event: Connect to " + ipstr + " " + port + " " + req.fromNodeId); IPAddress targetIp = IPAddress.Parse(ipstr); Socket mClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); bool closeFlag = false; mClientSocket.Connect(new IPEndPoint(targetIp, Convert.ToInt32(port))); Utils.Log("Event: Connect to " + ipstr + " " + port + " Success " + req.fromNodeId); var mlink = LinkClient.Connect(mSkynet, req.fromToxId, req.fromNodeId); req.toNodeId = mlink.clientId; Utils.Log("Event: Connect to " + ipstr + " " + port + " Success " + req.fromNodeId + " , mLinkID: " + mlink.clientId); mlink.OnMessage((msg) => { try { Utils.Log("Event: Start Write Message, mLinkID: " + mlink.clientId); if (mClientSocket != null && mClientSocket.Connected) { mClientSocket.Send(msg, SocketFlags.None); } Utils.Log("Event: Write Message Success, mLinkID: " + mlink.clientId); } catch (Exception e) { Utils.Log("Event: ERROR " + e.Message); Utils.Log(e.StackTrace); mlink.CloseRemote(); mlink.Close(); if (!closeFlag && mClientSocket.Connected) { closeFlag = true; try { mClientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event: " + ex.Message); } mClientSocket.Close(); Utils.Log("Event: Close Socket" + ipstr + " " + port + " mLinkID " + mlink.clientId); } } }); mlink.OnClose(() => { if (!closeFlag && mClientSocket.Connected) { closeFlag = true; try { mClientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event: " + ex.Message); } mClientSocket.Close(); Utils.Log("Event: Close Socket" + ipstr + " " + port + " mLinkID " + mlink.clientId); } }); // send response after all handler has been set mSkynet.sendResponse(req.createResponse(Encoding.UTF8.GetBytes("OK")), new ToxId(req.fromToxId)); Task.Factory.StartNew(() => { while (true) { byte[] buf = new byte[1024 * 512]; try { Utils.Log("Event: Start Read Data, Clientid: " + mlink.clientId); int size = 0; if (mClientSocket != null && mClientSocket.Connected) { size = mClientSocket.Receive(buf); } else { Utils.Log("Event: Socket already closed" + ipstr + " " + port + " mLinkID " + mlink.clientId); break; } if (size == 0) { if (!closeFlag && mClientSocket.Connected) { Utils.Log("Event: Close Connection, Clientid: " + mlink.clientId); closeFlag = true; try { mClientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event: " + ex.Message); } mClientSocket.Close(); } mlink.CloseRemote(); mlink.Close(); break; } else { Utils.Log("Event: Read Data " + size + ", Clientid: " + mlink.clientId); } var res = mlink.Send(buf, size); if (!res) { // send failed if (!closeFlag && mClientSocket.Connected) { closeFlag = true; try { mClientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event: " + ex.Message); } mClientSocket.Close(); mlink.Close(); Utils.Log("Event: Tox send message failed, Clientid: " + mlink.clientId); break; } } } catch (Exception e) { /*if (e.ErrorCode != 10004) // this is not an error * { * Console.WriteLine("Time: " + Utils.UnixTimeNow() + " Event: ERROR " + e.Message); * Console.WriteLine(e.StackTrace); * }*/ Utils.Log("Event: ERROR " + e.Message); Utils.Log(e.StackTrace); mlink.CloseRemote(); mlink.Close(); if (!closeFlag && mClientSocket.Connected) { closeFlag = true; try { mClientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Utils.Log("Event: " + ex.Message); } mClientSocket.Close(); Utils.Log("Event: Close Connection, ClientId: " + mlink.clientId); } break; } } }, TaskCreationOptions.LongRunning).ForgetOrThrow(); Utils.Log("Event: Connect to " + ipstr + " " + port + " All Success " + req.fromNodeId + ", mLinkID: " + mlink.clientId); } catch (Exception e) { Utils.Log("Event: ERROR " + e.Message); Utils.Log(e.StackTrace); // connected failed string reqStr = Encoding.UTF8.GetString(req.content); string ipstr = reqStr.Split('\n')[0]; string port = reqStr.Split('\n')[1]; Utils.Log("Event: Connect to " + ipstr + " " + port + " failed"); var response = req.createResponse(Encoding.UTF8.GetBytes("failed")); mSkynet.sendResponse(response, new ToxId(response.toToxId)); } }, TaskCreationOptions.LongRunning).ForgetOrThrow(); } else if (req.toNodeId == "" && req.url == "/handshake") { var response = req.createResponse(Encoding.UTF8.GetBytes("OK")); Utils.Log("Event: HandShake from " + response.toToxId + ", MessageID: " + req.uuid); Utils.Log("Event: Send HandShake response " + response.uuid + ", ToxId: " + response.toToxId); mSkynet.sendResponse(response, new ToxId(response.toToxId)); } }); while (true) { Thread.Sleep(10); } }
internal static void RegisterThreadPoolGauges(MetricsContext context) { context.Gauge("Thread Pool Available Threads", () => { int threads, ports; ThreadPool.GetAvailableThreads(out threads, out ports); return(threads); }, Unit.Threads, tags: "threads"); context.Gauge("Thread Pool Available Completion Ports", () => { int threads, ports; ThreadPool.GetAvailableThreads(out threads, out ports); return(ports); }, Unit.Custom("Ports"), tags: "threads"); context.Gauge("Thread Pool Min Threads", () => { int threads, ports; ThreadPool.GetMinThreads(out threads, out ports); return(threads); }, Unit.Threads, tags: "threads"); context.Gauge("Thread Pool Min Completion Ports", () => { int threads, ports; ThreadPool.GetMinThreads(out threads, out ports); return(ports); }, Unit.Custom("Ports"), tags: "threads"); context.Gauge("Thread Pool Max Threads", () => { int threads, ports; ThreadPool.GetMaxThreads(out threads, out ports); return(threads); }, Unit.Threads, tags: "threads"); context.Gauge("Thread Pool Max Completion Ports", () => { int threads, ports; ThreadPool.GetMaxThreads(out threads, out ports); return(ports); }, Unit.Custom("Ports"), tags: "threads"); var currentProcess = Process.GetCurrentProcess(); Func <TimeSpan> uptime = () => (DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime()); context.Gauge(currentProcess.ProcessName + " Uptime Seconds", () => uptime().TotalSeconds, Unit.Custom("Seconds")); context.Gauge(currentProcess.ProcessName + " Uptime Hours", () => uptime().TotalHours, Unit.Custom("Hours")); context.Gauge(currentProcess.ProcessName + " Threads", () => Process.GetCurrentProcess().Threads.Count, Unit.Threads, tags: "threads"); }
static bool _IsHandlingException; // Make sure we don't go recursive on ourself //could move our main function into OpenSimMain and kill this class public static void BaseMain(string[] args, string defaultIniFile, ISimulationBase simBase) { // First line, hook the appdomain to the crash reporter AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // Add the arguments supplied when running the application to the configuration ArgvConfigSource configSource = new ArgvConfigSource(args); if (!args.Contains("-skipconfig")) { Configure(false); } // Increase the number of IOCP threads available. Mono defaults to a tragically low number int workerThreads, iocpThreads; ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads); //MainConsole.Instance.InfoFormat("[WHiteCore MAIN]: Runtime gave us {0} worker threads and {1} IOCP threads", workerThreads, iocpThreads); if (workerThreads < 500 || iocpThreads < 1000) { workerThreads = 500; iocpThreads = 1000; //MainConsole.Instance.Info("[WHiteCore MAIN]: Bumping up to 500 worker threads and 1000 IOCP threads"); ThreadPool.SetMaxThreads(workerThreads, iocpThreads); } BinMigratorService service = new BinMigratorService(); service.MigrateBin(); // Configure nIni aliases and localles Culture.SystemCultureInfo = CultureInfo.CurrentCulture; Culture.SetCurrentCulture(); configSource.Alias.AddAlias("On", true); configSource.Alias.AddAlias("Off", false); configSource.Alias.AddAlias("True", true); configSource.Alias.AddAlias("False", false); //Command line switches configSource.AddSwitch("Startup", "inifile"); configSource.AddSwitch("Startup", "inimaster"); configSource.AddSwitch("Startup", "inigrid"); configSource.AddSwitch("Startup", "inisim"); configSource.AddSwitch("Startup", "inidirectory"); configSource.AddSwitch("Startup", "oldoptions"); configSource.AddSwitch("Startup", "inishowfileloading"); configSource.AddSwitch("Startup", "mainIniDirectory"); configSource.AddSwitch("Startup", "mainIniFileName"); configSource.AddSwitch("Startup", "secondaryIniFileName"); configSource.AddSwitch("Startup", "RegionDataFileName"); configSource.AddSwitch("Console", "Console"); configSource.AddSwitch("Console", "LogAppendName"); configSource.AddSwitch("Console", "LogPath"); configSource.AddSwitch("Network", "http_listener_port"); IConfigSource m_configSource = Configuration(configSource, defaultIniFile); // Check if we're saving crashes m_saveCrashDumps = m_configSource.Configs["Startup"].GetBoolean("save_crashes", m_saveCrashDumps); // load Crash directory config m_crashDir = m_configSource.Configs["Startup"].GetString("crash_dir", m_crashDir); //Initialize the sim base now Startup(configSource, m_configSource, simBase.Copy(), args); }
private static bool _IsHandlingException; // Make sure we don't go recursive on ourself //could move our main function into OpenSimMain and kill this class public static void BaseMain(string[] args, string defaultIniFile, ISimulationBase simBase) { // First line, hook the appdomain to the crash reporter AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // Add the arguments supplied when running the application to the configuration ArgvConfigSource configSource = new ArgvConfigSource(args); if (!args.Contains("-skipconfig")) { Configure(false); } // Configure Log4Net configSource.AddSwitch("Startup", "logconfig"); string logConfigFile = configSource.Configs["Startup"].GetString("logconfig", String.Empty); if (logConfigFile != String.Empty) { XmlConfigurator.Configure(new FileInfo(logConfigFile)); //MainConsole.Instance.InfoFormat("[OPENSIM MAIN]: configured log4net using \"{0}\" as configuration file", // logConfigFile); } else { XmlConfigurator.Configure(); //MainConsole.Instance.Info("[OPENSIM MAIN]: configured log4net using default OpenSim.exe.config"); } // Increase the number of IOCP threads available. Mono defaults to a tragically low number int workerThreads, iocpThreads; ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads); //MainConsole.Instance.InfoFormat("[OPENSIM MAIN]: Runtime gave us {0} worker threads and {1} IOCP threads", workerThreads, iocpThreads); if (workerThreads < 500 || iocpThreads < 1000) { workerThreads = 500; iocpThreads = 1000; //MainConsole.Instance.Info("[OPENSIM MAIN]: Bumping up to 500 worker threads and 1000 IOCP threads"); ThreadPool.SetMaxThreads(workerThreads, iocpThreads); } // Check if the system is compatible with OpenSimulator. // Ensures that the minimum system requirements are met //MainConsole.Instance.Info("[Setup]: Performing compatibility checks... \n"); string supported = String.Empty; if (Util.IsEnvironmentSupported(ref supported)) { int minWorker, minIOC; // Get the current settings. ThreadPool.GetMinThreads(out minWorker, out minIOC); //MainConsole.Instance.InfoFormat("[Setup]: Environment is compatible. Thread Workers: {0}, IO Workers {1}\n", minWorker, minIOC); } else { MainConsole.Instance.Warn("[Setup]: Environment is unsupported (" + supported + ")\n"); #if BlockUnsupportedVersions Thread.Sleep(10000); //Sleep 10 seconds return; #endif } BinMigratorService service = new BinMigratorService(); service.MigrateBin(); // Configure nIni aliases and localles Culture.SetCurrentCulture(); configSource.Alias.AddAlias("On", true); configSource.Alias.AddAlias("Off", false); configSource.Alias.AddAlias("True", true); configSource.Alias.AddAlias("False", false); //Command line switches configSource.AddSwitch("Startup", "inifile"); configSource.AddSwitch("Startup", "inimaster"); configSource.AddSwitch("Startup", "inigrid"); configSource.AddSwitch("Startup", "inisim"); configSource.AddSwitch("Startup", "inidirectory"); configSource.AddSwitch("Startup", "oldoptions"); configSource.AddSwitch("Startup", "inishowfileloading"); configSource.AddSwitch("Startup", "mainIniDirectory"); configSource.AddSwitch("Startup", "mainIniFileName"); configSource.AddSwitch("Startup", "secondaryIniFileName"); configSource.AddConfig("Network"); IConfigSource m_configSource = Configuration(configSource, defaultIniFile); // Check if we're saving crashes m_saveCrashDumps = m_configSource.Configs["Startup"].GetBoolean("save_crashes", m_saveCrashDumps); // load Crash directory config m_crashDir = m_configSource.Configs["Startup"].GetString("crash_dir", m_crashDir); // check auto restart bool AutoRestart = m_configSource.Configs["Startup"].GetBoolean("AutoRestartOnCrash", true); //Set up the error reporting if (m_configSource.Configs["ErrorReporting"] != null) { m_sendErrorReport = m_configSource.Configs["ErrorReporting"].GetBoolean("SendErrorReports", true); m_urlToPostErrors = m_configSource.Configs["ErrorReporting"].GetString("ErrorReportingURL", m_urlToPostErrors); } bool Running = true; //If auto restart is set, then we always run. // otherwise, just run the first time that Running == true while (AutoRestart || Running) { //Always run once, then disable this Running = false; //Initialize the sim base now Startup(configSource, m_configSource, simBase.Copy(), args); } }
private void OnPerformanceTimerCallback(object state) { int availableWorkingThreads, availableCompletionPortThreads; ThreadPool.GetAvailableThreads(out availableWorkingThreads, out availableCompletionPortThreads); int maxWorkingThreads; int maxCompletionPortThreads; ThreadPool.GetMaxThreads(out maxWorkingThreads, out maxCompletionPortThreads); var globalPerfData = new GlobalPerformanceData { AvailableWorkingThreads = availableWorkingThreads, AvailableCompletionPortThreads = availableCompletionPortThreads, MaxCompletionPortThreads = maxCompletionPortThreads, MaxWorkingThreads = maxWorkingThreads, CpuUsage = m_CpuUsagePC.NextValue() / m_CpuCores, TotalThreadCount = (int)m_ThreadCountPC.NextValue(), WorkingSet = (long)m_WorkingSetPC.NextValue() }; var perfBuilder = new StringBuilder(); perfBuilder.AppendLine("---------------------------------------------------"); perfBuilder.AppendLine(string.Format("CPU Usage: {0}%, Physical Memory Usage: {1:N}, Total Thread Count: {2}", globalPerfData.CpuUsage.ToString("0.00"), globalPerfData.WorkingSet, globalPerfData.TotalThreadCount)); perfBuilder.AppendLine(string.Format("AvailableWorkingThreads: {0}, AvailableCompletionPortThreads: {1}", globalPerfData.AvailableWorkingThreads, globalPerfData.AvailableCompletionPortThreads)); perfBuilder.AppendLine(string.Format("MaxWorkingThreads: {0}, MaxCompletionPortThreads: {1}", globalPerfData.MaxWorkingThreads, globalPerfData.MaxCompletionPortThreads)); var instancesData = new List <PerformanceDataInfo>(m_AppServers.Count); m_AppServers.ForEach(s => { var perfSource = s as IPerformanceDataSource; if (perfSource != null) { var perfData = perfSource.CollectPerformanceData(globalPerfData); instancesData.Add(new PerformanceDataInfo { ServerName = s.Name, Data = perfData }); perfBuilder.AppendLine(string.Format("{0} - Total Connections: {1}, Total Handled Requests: {2}, Request Handling Speed: {3:f0}/s", s.Name, perfData.CurrentRecord.TotalConnections, perfData.CurrentRecord.TotalHandledRequests, (perfData.CurrentRecord.TotalHandledRequests - perfData.PreviousRecord.TotalHandledRequests) / perfData.CurrentRecord.RecordSpan)); } }); m_PerfLog.Info(perfBuilder.ToString()); var handler = m_PerformanceDataCollected; if (handler == null) { return; } handler.BeginInvoke(this, new PermformanceDataEventArgs(globalPerfData, instancesData.ToArray()), null, null); }
private void ConfigureWebServer(IWebHostBuilder webHostBuilder) { bool useHttps = _configuration.GetValue <bool>("WebServer:UseHttps"); bool useWeb = _configuration.GetValue <bool>("WebServer:UseWeb"); bool useApi = _configuration.GetValue <bool>("WebServer:UseApi"); if (!useWeb && !useApi) { throw new Exception("Please specify which kind of service (web or API) you want to use"); } int numberOfCores = _configuration.GetValue <int>("WebServer:NumberOfCores"); if (numberOfCores <= 0) { throw new Exception("Please specify the number of cores that the server provides"); } long maxRequestBodySizeKB = _configuration.GetValue <long>("WebServer:MaxRequestBodySizeKB"); // see https://www.monitis.com/blog/improving-asp-net-performance-part3-threading/ ThreadPool.SetMinThreads(88 * numberOfCores, 88 * numberOfCores); ThreadPool.SetMaxThreads(250 * numberOfCores, 250 * numberOfCores); int minWorkerThreads, minWorkerIoThreads; int maxWorkerThreads, maxWorkerIoThreads; ThreadPool.GetMinThreads(out minWorkerThreads, out minWorkerIoThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxWorkerIoThreads); Console.WriteLine($"Thread limits: {minWorkerThreads}/{minWorkerIoThreads} - {maxWorkerThreads}/{maxWorkerIoThreads}"); ServicePointManager.UseNagleAlgorithm = false; // see https://support.microsoft.com/de-de/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s ServicePointManager.DefaultConnectionLimit = 12 * numberOfCores; Console.WriteLine($"Default connection limit: {ServicePointManager.DefaultConnectionLimit}"); int availableWorkerThreads, availableWorkerIoThreads; ThreadPool.GetAvailableThreads(out availableWorkerThreads, out availableWorkerIoThreads); Console.WriteLine($"Available threads: {availableWorkerThreads}/{availableWorkerThreads}"); webHostBuilder.UseKestrel((builderContext, options) => { X509Certificate2 defaultWebCertificate = null; X509Certificate2 defaultApiCertificate = null; if (useHttps) { if (useWeb) { string httpsCertificateAssembly = _configuration["WebServer:Https:Certificates:Web:Assembly"]; if (string.IsNullOrEmpty(httpsCertificateAssembly)) { throw new Exception("HTTPS web certificate assembly not found"); } string httpsCertificateName = _configuration["WebServer:Https:Certificates:Web:Name"]; if (string.IsNullOrEmpty(httpsCertificateName)) { throw new Exception("HTTPS web certificate name not found"); } string httpsCertificatePassword = _configuration["WebServer:Https:Certificates:Web:Password"]; if (string.IsNullOrEmpty(httpsCertificatePassword)) { throw new Exception("HTTPS web certificate password not found"); } // from https://stackoverflow.com/questions/50708394/read-embedded-file-from-resource-in-asp-net-core Assembly httpsAssembly = AppDomain.CurrentDomain.GetAssemblies(). SingleOrDefault(assembly => assembly.GetName().Name == httpsCertificateAssembly); if (httpsAssembly == null) { throw new Exception("HTTPS web certificate assembly is not present in the list of assemblies"); } var resourceStream = httpsAssembly.GetManifestResourceStream(httpsCertificateName); if (resourceStream == null) { throw new Exception("HTTPS web certificate resource not found"); } using (var memory = new MemoryStream((int)resourceStream.Length)) { resourceStream.CopyTo(memory); defaultWebCertificate = new X509Certificate2(memory.ToArray(), httpsCertificatePassword); } } if (useApi) { string httpsCertificateAssembly = _configuration["WebServer:Https:Certificates:Api:Assembly"]; if (string.IsNullOrEmpty(httpsCertificateAssembly)) { throw new Exception("HTTPS API certificate assembly not found"); } string httpsCertificateName = _configuration["WebServer:Https:Certificates:Api:Name"]; if (string.IsNullOrEmpty(httpsCertificateName)) { throw new Exception("HTTPS API certificate name not found"); } string httpsCertificatePassword = _configuration["WebServer:Https:Certificates:Api:Password"]; if (string.IsNullOrEmpty(httpsCertificatePassword)) { throw new Exception("HTTPS API certificate password not found"); } // from https://stackoverflow.com/questions/50708394/read-embedded-file-from-resource-in-asp-net-core Assembly httpsAssembly = AppDomain.CurrentDomain.GetAssemblies(). SingleOrDefault(assembly => assembly.GetName().Name == httpsCertificateAssembly); if (httpsAssembly == null) { throw new Exception("HTTPS API certificate assembly is not present in the list of assemblies"); } var resourceStream = httpsAssembly.GetManifestResourceStream(httpsCertificateName); if (resourceStream == null) { throw new Exception("HTTPS API certificate resource not found"); } using (var memory = new MemoryStream((int)resourceStream.Length)) { resourceStream.CopyTo(memory); defaultApiCertificate = new X509Certificate2(memory.ToArray(), httpsCertificatePassword); } } } options.Configure(builderContext.Configuration.GetSection("Kestrel")); // try to avoid thread starvation problems // see https://www.strathweb.com/2019/02/be-careful-when-manually-handling-json-requests-in-asp-net-core/ // see https://www.monitis.com/blog/improving-asp-net-performance-part3-threading/ options.Limits.MaxConcurrentConnections = 24 * numberOfCores; options.Limits.MaxConcurrentUpgradedConnections = 24 * numberOfCores; if (maxRequestBodySizeKB > 0) { options.Limits.MaxRequestBodySize = maxRequestBodySizeKB * 1024; } if (useHttps) { // Important: Only enable TLS 1.2 and TLS 1.3 to comply with SSL Server tests. // TLS 1.1, TLS 1.0 and SSLv3 are considered insecure by todays standards. options.ConfigureHttpsDefaults(httpsOptions => { httpsOptions.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13; if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) { var allowedCipherSuites = CipherSuitesPolicy.AllowedCipherSuites; foreach (var allowedCipherSuite in allowedCipherSuites) { Console.WriteLine($"Cipher suite supported: {allowedCipherSuite}"); } httpsOptions.OnAuthenticate = (conContext, sslAuthOptions) => { // not supported if OpenSSL 1.1.1 is not present! sslAuthOptions.CipherSuitesPolicy = CipherSuitesPolicy; }; } int?webPort = null; if (useWeb) { webPort = _configuration.GetValue <int>("WebServer:WebPort"); } int?apiPort = null; if (useApi) { apiPort = _configuration.GetValue <int>("WebServer:ApiPort"); } httpsOptions.ServerCertificateSelector = (connectionContext, hostName) => { var port = ((IPEndPoint)connectionContext.LocalEndPoint).Port; if (string.IsNullOrEmpty(hostName) || hostName.EndsWith(".smint.io")) { // this is our default certificates if (useWeb && port == webPort && defaultWebCertificate != null) { return(defaultWebCertificate); } else if (useApi && port == apiPort && defaultApiCertificate != null) { return(defaultApiCertificate); } else { throw new Exception($"Default certificate not found ({hostName} / {useWeb} / {useApi} / {port} / {webPort} / {apiPort})"); } } // this is some external certificates coming e.g. from the tenant database var serviceProvider = options.ApplicationServices; var serverCertificateSelector = serviceProvider.GetService <IServerCertificateSelector>(); if (serverCertificateSelector != null) { var customCertificate = serverCertificateSelector.GetServerCertificate(hostName); if (customCertificate == null) { throw new Exception($"Custom certificate not found ({hostName} / {useWeb} / {useApi} / {port} / {webPort} / {apiPort})"); } return(customCertificate); } // default fallback if (useWeb && port == webPort && defaultWebCertificate != null) { return(defaultWebCertificate); } else if (useApi && port == apiPort && defaultApiCertificate != null) { return(defaultApiCertificate); } else { throw new Exception($"Fallback certificate not found ({hostName} / {useWeb} / {useApi} / {port} / {webPort} / {apiPort})"); } }; }); if (useWeb) { int webPort = _configuration.GetValue <int>("WebServer:WebPort"); options.Listen(IPAddress.Any, webPort, listenOptions => listenOptions.UseHttps()); int httpHealthCheckPort = _configuration.GetValue <int>("WebServer:HttpHealthCheckPort"); if (httpHealthCheckPort > 0) { options.Listen(IPAddress.Any, httpHealthCheckPort); } else { // we can not do redirects to HTTPS if we have a health check running int redirectHttpToHttpsWebPort = _configuration.GetValue <int>("WebServer:RedirectHttpToHttpsWebPort"); if (redirectHttpToHttpsWebPort > 0) { options.Listen(IPAddress.Any, redirectHttpToHttpsWebPort); } } } if (useApi) { int apiPort = _configuration.GetValue <int>("WebServer:ApiPort"); options.Listen(IPAddress.Any, apiPort, listenOptions => listenOptions.UseHttps()); } } }); webHostBuilder.ConfigureServices((hostingContext, services) => { var configuration = hostingContext.Configuration; // Fallback services.PostConfigure <HostFilteringOptions>(options => { if (options.AllowedHosts == null || options.AllowedHosts.Count == 0) { // "AllowedHosts": "localhost;127.0.0.1;[::1]" var hosts = configuration["WebServer:AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // Fall back to "*" to disable. options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" }); } }); // Change notification services.AddSingleton <IOptionsChangeTokenSource <HostFilteringOptions> >( new ConfigurationChangeTokenSource <HostFilteringOptions>(configuration)); services.AddTransient <IStartupFilter, HostFilteringStartupFilter>(); }); webHostBuilder.UseIISIntegration(); List <string> urls = new List <string>(); string urlPattern = _configuration["WebServer:UrlPattern"]; if (string.IsNullOrEmpty(urlPattern)) { throw new Exception("URL pattern not found in application settings"); } if (useWeb) { string webServerUrl = useHttps ? "https://" : "http://"; int webPort = _configuration.GetValue <int>("WebServer:WebPort"); webServerUrl += urlPattern; webServerUrl += ":" + webPort; urls.Add(webServerUrl); if (useHttps) { int httpHealthCheckPort = _configuration.GetValue <int>("WebServer:HttpHealthCheckPort"); if (httpHealthCheckPort > 0) { webServerUrl = "http://"; webServerUrl += urlPattern; webServerUrl += ":" + httpHealthCheckPort; urls.Add(webServerUrl); } else { // we can not do redirects to HTTPS if we have a health check running int redirectHttpToHttpsWebPort = _configuration.GetValue <int>("WebServer:RedirectHttpToHttpsWebPort"); if (redirectHttpToHttpsWebPort > 0) { webServerUrl = "http://"; webServerUrl += urlPattern; webServerUrl += ":" + redirectHttpToHttpsWebPort; urls.Add(webServerUrl); } } } } if (useApi) { string apiServerUrl = useHttps ? "https://" : "http://"; int apiPort = _configuration.GetValue <int>("WebServer:ApiPort"); apiServerUrl += urlPattern; apiServerUrl += ":" + apiPort; urls.Add(apiServerUrl); } string[] urlsArray = urls.ToArray <string>(); webHostBuilder.UseUrls(urlsArray); webHostBuilder.UseStartup(typeof(TStartup)); if (_configuration.GetValue <bool>("Sentry:UseSentry")) { webHostBuilder.UseSentry(); } _serverUrl = string.Join(" / ", urlsArray); }
static void ExecuteNoGUI(string[] args) { bool all_assets = false; string savepath = null; List <string> folder = new List <string>(); List <string> files = new List <string>(); for (uint i = 0; i < args.Length; i++) { string arg = args[i]; if (arg.StartsWith("-a")) { all_assets = true; } else if (arg.StartsWith("-o")) { if (++i < args.Length) { savepath = args[i]; } } else if (Directory.Exists(arg)) { folder.Add(arg); } else if (File.Exists(arg)) { files.Add(arg); } } if (savepath == null) { Console.Error.WriteLine("No output directory specified!"); Console.Error.WriteLine("Use '-o <directory>' to specify output directory."); Application.Exit(); } foreach (string f in folder) { DirectoryInfo d = new DirectoryInfo(f); foreach (FileInfo file in d.GetFiles()) { if (!file.Attributes.HasFlag(FileAttributes.Directory)) { files.Add(file.FullName); } } } assetsManager.LoadFiles(files.ToArray()); BuildAssetData(); while (exportableAssets.Count < 1) { Thread.Sleep(20); } List <AssetItem> assets = new List <AssetItem>(); foreach (var asset in exportableAssets) { if (!all_assets) { if (asset.Type != AssetStudio.ClassIDType.Texture2D) { continue; } } assets.Add(asset); } ExportAssets(savepath, assets, ExportType.Convert); int worker, worker2; do { ThreadPool.GetAvailableThreads(out worker, out _); ThreadPool.GetMaxThreads(out worker2, out _); Thread.Sleep(50); } while (worker < worker2); }
public override string ToString() { var sb = new StringBuilder(); sb.AppendLine("Platform version info:").Append(ConfigUtilities.RuntimeVersionInfo()); sb.Append(" Host: ").AppendLine(Dns.GetHostName()); sb.Append(" Processor Count: ").Append(System.Environment.ProcessorCount).AppendLine(); sb.AppendLine("Client Configuration:"); sb.Append(" Config File Name: ").AppendLine(string.IsNullOrEmpty(SourceFile) ? "" : Path.GetFullPath(SourceFile)); sb.Append(" Start time: ").AppendLine(LogFormatter.PrintDate(DateTime.UtcNow)); sb.Append(" Gateway Provider: ").Append(GatewayProvider); if (GatewayProvider == GatewayProviderType.None) { sb.Append(". Gateway Provider that will be used instead: ").Append(GatewayProviderToUse); } sb.AppendLine(); if (Gateways != null && Gateways.Count > 0) { sb.AppendFormat(" Gateways[{0}]:", Gateways.Count).AppendLine(); foreach (var endpoint in Gateways) { sb.Append(" ").AppendLine(endpoint.ToString()); } } else { sb.Append(" Gateways: ").AppendLine("Unspecified"); } sb.Append(" Preferred Gateway Index: ").AppendLine(PreferedGatewayIndex.ToString()); if (Gateways != null && PreferedGatewayIndex >= 0 && PreferedGatewayIndex < Gateways.Count) { sb.Append(" Preferred Gateway Address: ").AppendLine(Gateways[PreferedGatewayIndex].ToString()); } sb.Append(" GatewayListRefreshPeriod: ").Append(GatewayListRefreshPeriod).AppendLine(); if (!String.IsNullOrEmpty(DeploymentId) || !String.IsNullOrEmpty(DataConnectionString)) { sb.Append(" Azure:").AppendLine(); sb.Append(" DeploymentId: ").Append(DeploymentId).AppendLine(); string dataConnectionInfo = ConfigUtilities.RedactConnectionStringInfo(DataConnectionString); // Don't print Azure account keys in log files sb.Append(" DataConnectionString: ").Append(dataConnectionInfo).AppendLine(); } if (!string.IsNullOrWhiteSpace(NetInterface)) { sb.Append(" Network Interface: ").AppendLine(NetInterface); } if (Port != 0) { sb.Append(" Network Port: ").Append(Port).AppendLine(); } sb.Append(" Preferred Address Family: ").AppendLine(PreferredFamily.ToString()); sb.Append(" DNS Host Name: ").AppendLine(DNSHostName); sb.Append(" Client Name: ").AppendLine(ClientName); sb.Append(ConfigUtilities.TraceConfigurationToString(this)); sb.Append(ConfigUtilities.IStatisticsConfigurationToString(this)); sb.Append(LimitManager); sb.AppendFormat(base.ToString()); sb.Append(" .NET: ").AppendLine(); int workerThreads; int completionPortThreads; ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); sb.AppendFormat(" .NET thread pool sizes - Min: Worker Threads={0} Completion Port Threads={1}", workerThreads, completionPortThreads).AppendLine(); ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); sb.AppendFormat(" .NET thread pool sizes - Max: Worker Threads={0} Completion Port Threads={1}", workerThreads, completionPortThreads).AppendLine(); sb.AppendFormat(" Providers:").AppendLine(); sb.Append(ProviderConfigurationUtility.PrintProviderConfigurations(ProviderConfigurations)); return(sb.ToString()); }
public async Task RunAsync() { int minWorkerThreads, maxWorkerThreads, minCompletionPortThreads, maxCompletionPortThreads; ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); Console.WriteLine($"Test Configuration: workers {minWorkerThreads}/{maxWorkerThreads}, iocp {minCompletionPortThreads}/{maxCompletionPortThreads}"); await TaskEx.YieldToThreadPool(); ThreadPool.SetMaxThreads(128, 128); await SetupAsync().ConfigureAwait(false); var sw = new Stopwatch(); sw.Start(); Console.WriteLine(sw.ElapsedMilliseconds + " Starting Cluster"); var cluster = await TestUtils.CreateCluster <int, TestDto>( clusterSize, () => new CacheConfiguration <int, TestDto>("test-cache") { CachePersistenceStrategy = CachePersistenceStrategy <int, TestDto> .Create( BatchedCacheReadStrategy <int, TestDto> .Create(hitler), WriteBehindCacheUpdateStrategy <int, TestDto> .Create(hitler, 5000)), PartitioningConfiguration = new PartitioningConfiguration { Redundancy = replicationFactor } }).ConfigureAwait(false); Console.WriteLine(sw.ElapsedMilliseconds + " Started Cluster"); var sync = new AsyncCountdownLatch(workerCount); var tasks = Util.Generate( workerCount, async workerId => { await TaskEx.YieldToThreadPool(); sync.Signal(); await sync.WaitAsync().ConfigureAwait(false); var jobs = Util.Generate( rowCount * iterationsPerWorker, i => cluster[(workerId + i) % clusterSize].UserCache.ProcessAsync( entryIdsByOriginalName["Name" + (i % rowCount)], AppendToNameOperation.Create("_") )); Console.WriteLine(DateTime.Now + " Worker " + workerId + " started iterations"); await Task.WhenAll(jobs).ConfigureAwait(false); Console.WriteLine(DateTime.Now + " Worker " + workerId + " completed iterations"); }); try { Console.WriteLine(sw.ElapsedMilliseconds + " Awaiting workers"); await Task.WhenAll(tasks).ConfigureAwait(false); Console.WriteLine(sw.ElapsedMilliseconds + " Validating cache state"); await Task.WhenAll( Util.Generate( rowCount, i => Go(async() => { var originalName = "Name" + i; var entryId = entryIdsByOriginalName[originalName]; var entry = await cluster[i % clusterSize].UserCache.GetAsync(entryId).ConfigureAwait(false); AssertEquals(originalName + "_".Repeat(workerCount *iterationsPerWorker), entry.Value.Name); }))).ConfigureAwait(false); Console.WriteLine(sw.ElapsedMilliseconds + " Validation completed"); await CleanupAsync().ConfigureAwait(false); while (true) { ; } } catch (Exception e) { Console.WriteLine("Write behind test threw " + e); throw; } }
public static bool CrossCompileAOTDirectoryParallel(string crossCompilerPath, BuildTarget buildTarget, CrossCompileOptions crossCompileOptions, string sourceAssembliesFolder, string targetCrossCompiledASMFolder, string additionalOptions) { sourceAssembliesFolder = Path.Combine(Directory.GetCurrentDirectory(), sourceAssembliesFolder); targetCrossCompiledASMFolder = Path.Combine(Directory.GetCurrentDirectory(), targetCrossCompiledASMFolder); int num = 1; int num2 = 1; ThreadPool.GetMaxThreads(out num, out num2); List <MonoCrossCompile.JobCompileAOT> list = new List <MonoCrossCompile.JobCompileAOT>(); List <ManualResetEvent> list2 = new List <ManualResetEvent>(); bool flag = true; List <string> list3 = new List <string>(from path in Directory.GetFiles(sourceAssembliesFolder) where Path.GetExtension(path) == ".dll" select path); int count = list3.Count; int num3 = 0; MonoCrossCompile.DisplayAOTProgressBar(count, num3); long num4 = (long)Math.Min(1800000, (count + 3) * 1000 * 30); foreach (string current in list3) { string fileName = Path.GetFileName(current); string output = Path.Combine(targetCrossCompiledASMFolder, fileName + ".s"); MonoCrossCompile.JobCompileAOT jobCompileAOT = new MonoCrossCompile.JobCompileAOT(buildTarget, crossCompilerPath, sourceAssembliesFolder, crossCompileOptions, fileName, output, additionalOptions); list.Add(jobCompileAOT); list2.Add(jobCompileAOT.m_doneEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(jobCompileAOT.ThreadPoolCallback)); if (list2.Count >= Environment.ProcessorCount) { flag = MonoCrossCompile.WaitForBuildOfFile(list2, ref num4); MonoCrossCompile.DisplayAOTProgressBar(count, num3); num3++; if (!flag) { break; } } } while (list2.Count > 0) { flag = MonoCrossCompile.WaitForBuildOfFile(list2, ref num4); MonoCrossCompile.DisplayAOTProgressBar(count, num3); num3++; if (!flag) { break; } } foreach (MonoCrossCompile.JobCompileAOT current2 in list) { if (current2.m_Exception != null) { UnityEngine.Debug.LogErrorFormat("Cross compilation job {0} failed.\n{1}", new object[] { current2.m_input, current2.m_Exception }); flag = false; } } return(flag); }
public void Start() { try { int threadsCount, k; ThreadPool.GetMaxThreads(out threadsCount, out k); this.networkServer.Start(); this.networkServer.ServerInformationUpdated += (sender, eventArgs) => { var dateTime = DateTime.UtcNow; Console.WriteLine( $"{dateTime} {eventArgs.ProcessedTcpPackets} TCP Packets Processed"); Console.WriteLine( $"{dateTime} {eventArgs.InvalidTcpPackets} Invalid or Lost TCP Packets"); Console.WriteLine( $"{dateTime} {eventArgs.ProcessedUdpPackets} UDP Packets Processed"); Console.WriteLine( $"{dateTime} {eventArgs.InvalidUdpPackets} Invalid or Lost UDP Packets"); Console.WriteLine( $"{dateTime} {eventArgs.TcpConnections} TCP connections active"); }; this.networkServer.ClientConnected += (sender, eventArgs) => { Console.WriteLine( $"Client Connected - {eventArgs.Connection.Socket.RemoteEndPoint}"); var viewModel = new MultiPlatform.ViewModels.ConnectedClientViewModelServerSide(); viewModel.Connection = eventArgs.Connection; this.connectedClients.Add(viewModel); }; this.networkServer.ClientDisconnected += (sender, eventArgs) => { Console.WriteLine( $"Client Disconnected - {eventArgs.Connection.Socket.RemoteEndPoint}"); foreach (var client in this.connectedClients.GetAll()) { } }; var can = true; Task.Factory.StartNew(() => { while (true) { try { var cfg = new ClientConfigurationPacket() { AudioSniffConfig = new AudioSniffConfigurationPacket() { CanRecive = can } }; this.networkServer.SendToAllTCP <ClientConfigurationPacket>(cfg); //can = !cfg.AudioSniffConfig.CanRecive; var admins = this.connectedClients.GetAll().Where(c => c.IsAdmin); foreach (var item in admins) { var connectedd = this.connectedClients.GetAll().Cast <ConnectedClientViewModel>().ToList(); var packet = new ConnectedClientsPacket() { ConnectedClients = new System.Collections.Generic.List <ConnectedClientInfoPacket>() }; foreach (var connected in connectedd) { packet.ConnectedClients.Add(new ConnectedClientInfoPacket() { ConnectedClientViewModel = connected }); } networkServer.Send <ConnectedClientsPacket>(packet, item.Connection); } Thread.Sleep(10000); } catch { continue; } } }); run = true; while (run) { Thread.Sleep(500); } } catch { } }
public static bool ResetThreadPool(int?maxWorkingThreads, int?maxCompletionPortThreads, int?minWorkingThreads, int?minCompletionPortThreads) { if (maxWorkingThreads.HasValue || maxCompletionPortThreads.HasValue) { int oldMaxWorkingThreads, oldMaxCompletionPortThreads; ThreadPool.GetMaxThreads(out oldMaxWorkingThreads, out oldMaxCompletionPortThreads); if (!maxWorkingThreads.HasValue) { maxWorkingThreads = oldMaxWorkingThreads; } if (!maxCompletionPortThreads.HasValue) { maxCompletionPortThreads = oldMaxCompletionPortThreads; } if (maxWorkingThreads.Value != oldMaxWorkingThreads || maxCompletionPortThreads.Value != oldMaxCompletionPortThreads) { if (!ThreadPool.SetMaxThreads(maxWorkingThreads.Value, maxCompletionPortThreads.Value)) { LogUtil.LogError(string.Format("Failed to run ThreadPool.SetMaxThreads({0}, {1})", maxWorkingThreads.Value, maxCompletionPortThreads.Value)); return(false); } else { LogUtil.LogInfo(string.Format("ThreadPool.SetMaxThreads({0}, {1})", maxWorkingThreads.Value, maxCompletionPortThreads.Value)); } } } if (minWorkingThreads.HasValue || minCompletionPortThreads.HasValue) { int oldMinWorkingThreads, oldMinCompletionPortThreads; ThreadPool.GetMinThreads(out oldMinWorkingThreads, out oldMinCompletionPortThreads); if (!minWorkingThreads.HasValue) { minWorkingThreads = oldMinWorkingThreads; } if (!minCompletionPortThreads.HasValue) { minCompletionPortThreads = oldMinCompletionPortThreads; } if (minWorkingThreads.Value != oldMinWorkingThreads || minCompletionPortThreads.Value != oldMinCompletionPortThreads) { if (!ThreadPool.SetMinThreads(minWorkingThreads.Value, minCompletionPortThreads.Value)) { LogUtil.LogError(string.Format("Failed to run ThreadPool.SetMinThreads({0}, {1})", minWorkingThreads.Value, minCompletionPortThreads.Value)); return(false); } else { LogUtil.LogInfo(string.Format("ThreadPool.SetMinThreads({0}, {1})", minWorkingThreads.Value, minCompletionPortThreads.Value)); } } } return(true); }
public ThreadStats() { ThreadPool.GetMinThreads(out _minWorkerThreads, out _minIOThreads); ThreadPool.GetAvailableThreads(out _availableWorkerThreads, out _availableIOThreads); ThreadPool.GetMaxThreads(out _maxWorkerThreads, out _maxIOThreads); }
public void Initalize() { logger.Info("Starting Jackett " + configService.GetVersion()); try { var x = Environment.OSVersion; var runtimedir = RuntimeEnvironment.GetRuntimeDirectory(); logger.Info("Environment version: " + Environment.Version.ToString() + " (" + runtimedir + ")"); logger.Info("OS version: " + Environment.OSVersion.ToString() + (Environment.Is64BitOperatingSystem ? " (64bit OS)" : "") + (Environment.Is64BitProcess ? " (64bit process)" : "")); try { int workerThreads; int completionPortThreads; ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); logger.Info("ThreadPool MaxThreads: " + workerThreads + " workerThreads, " + completionPortThreads + " completionPortThreads"); } catch (Exception e) { logger.Error("Error while getting MaxThreads details: " + e); } try { var issuefile = "/etc/issue"; if (File.Exists(issuefile)) { using (StreamReader reader = new StreamReader(issuefile)) { string firstLine; firstLine = reader.ReadLine(); if (firstLine != null) { logger.Info("issue: " + firstLine); } } } } catch (Exception e) { logger.Error(e, "Error while reading the issue file"); } Type monotype = Type.GetType("Mono.Runtime"); if (monotype != null) { MethodInfo displayName = monotype.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); var monoVersion = "unknown"; if (displayName != null) { monoVersion = displayName.Invoke(null, null).ToString(); } logger.Info("mono version: " + monoVersion); var monoVersionO = new Version(monoVersion.Split(' ')[0]); if (monoVersionO.Major < 4) { logger.Error("Your mono version is to old (mono 3 is no longer supported). Please update to the latest version from http://www.mono-project.com/download/"); Engine.Exit(2); } else if (monoVersionO.Major == 4 && monoVersionO.Minor == 2) { var notice = "mono version 4.2.* is known to cause problems with Jackett. If you experience any problems please try updating to the latest mono version from http://www.mono-project.com/download/ first."; _notices.Add(notice); logger.Error(notice); } try { // Check for mono-devel // Is there any better way which doesn't involve a hard cashes? var mono_devel_file = Path.Combine(runtimedir, "mono-api-info.exe"); if (!File.Exists(mono_devel_file)) { var notice = "It looks like the mono-devel package is not installed, please make sure it's installed to avoid crashes."; _notices.Add(notice); logger.Error(notice); } } catch (Exception e) { logger.Error(e, "Error while checking for mono-devel"); } try { // Check for ca-certificates-mono var mono_cert_file = Path.Combine(runtimedir, "cert-sync.exe"); if (!File.Exists(mono_cert_file)) { if ((monoVersionO.Major >= 4 && monoVersionO.Minor >= 8) || monoVersionO.Major >= 5) { var notice = "The ca-certificates-mono package is not installed, HTTPS trackers won't work. Please install it."; _notices.Add(notice); logger.Error(notice); } else { logger.Info("The ca-certificates-mono package is not installed, it will become mandatory once mono >= 4.8 is used."); } } } catch (Exception e) { logger.Error(e, "Error while checking for ca-certificates-mono"); } try { Encoding.GetEncoding("windows-1255"); } catch (NotSupportedException e) { logger.Debug(e); logger.Error(e.Message + " Most likely the mono-locale-extras package is not installed."); Engine.Exit(2); } if (Engine.WebClientType == typeof(HttpWebClient) || Engine.WebClientType == typeof(HttpWebClient2)) { // check if the certificate store was initialized using Mono.Security.X509.X509StoreManager.TrustedRootCertificates.Count try { var monoSecurity = Assembly.Load("Mono.Security"); Type monoX509StoreManager = monoSecurity.GetType("Mono.Security.X509.X509StoreManager"); if (monoX509StoreManager != null) { var TrustedRootCertificatesProperty = monoX509StoreManager.GetProperty("TrustedRootCertificates"); var TrustedRootCertificates = (ICollection)TrustedRootCertificatesProperty.GetValue(null); logger.Info("TrustedRootCertificates count: " + TrustedRootCertificates.Count); if (TrustedRootCertificates.Count == 0) { var CACertificatesFiles = new string[] { "/etc/ssl/certs/ca-certificates.crt", // Debian based "/etc/pki/tls/certs/ca-bundle.c", // RedHat based "/etc/ssl/ca-bundle.pem", // SUSE }; var notice = "The mono certificate store is not initialized.<br/>\n"; var logSpacer = " "; var CACertificatesFile = CACertificatesFiles.Where(f => File.Exists(f)).FirstOrDefault(); var CommandRoot = "curl -sS https://curl.haxx.se/ca/cacert.pem | cert-sync /dev/stdin"; var CommandUser = "******"; if (CACertificatesFile != null) { CommandRoot = "cert-sync " + CACertificatesFile; CommandUser = "******" + CACertificatesFile; } notice += logSpacer + "Please run the following command as root:<br/>\n"; notice += logSpacer + "<pre>" + CommandRoot + "</pre><br/>\n"; notice += logSpacer + "If you don't have root access or you're running MacOS, please run the following command as the jackett user (" + Environment.UserName + "):<br/>\n"; notice += logSpacer + "<pre>" + CommandUser + "</pre>"; _notices.Add(notice); logger.Error(Regex.Replace(notice, "<.*?>", String.Empty)); } } } catch (Exception e) { logger.Error(e, "Error while chekcing the mono certificate store"); } } } } catch (Exception e) { logger.Error("Error while getting environment details: " + e); } try { if (Environment.UserName == "root") { var notice = "Jackett is running with root privileges. You should run Jackett as an unprivileged user."; _notices.Add(notice); logger.Error(notice); } } catch (Exception e) { logger.Error(e, "Error while checking the username"); } CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); // Load indexers indexerService.InitIndexers(configService.GetCardigannDefinitionsFolders()); client.Init(); updater.CleanupTempDir(); }
static void Main(string[] args) { int maxthread = 0; int io; ThreadPool.GetMaxThreads(out maxthread, out io); ThreadPool.SetMaxThreads(1, 1); SimpleIOPlugin plugn = new SimpleIOPlugin(); plugn.onFileListenerChanged += plugn_OnFileListenerChanged; plugn.getMonitorDPI(65539, new Action <object, object>((x, y) => { })); var folder = plugn.PROGRAMFILES + "/overwolf"; plugn.getLatestFileInDirectory(folder, new Action <object, object>((x, y) => { })); plugn.getLatestFileInDirectory(folder + "/*.msi", new Action <object, object>((x, y) => { })); plugn.getTextFile(@"c:\Users\elad.bahar\AppData\Local\Overwolf\Log\OverwolfCEF_13096.log", true, new Action <object, object>((x, y) => { })); plugn.getBinaryFile(plugn.PROGRAMFILES + "/overwolf/Overwolf.exe.config", -1, new Action <object, object>((x, y) => { })); plugn.listDirectory(@"c:\Users\elad.bahar\AppData\Local\Overwolf", new Action <object, object>((x, y) => { })); plugn.getCurrentCursorPosition(new Action <object, object, object, object>((status, reason, x, y) => { Trace.WriteLine(reason); })); plugn.listenOnFile("test", @"c:\Temp\test.txt", false, new Action <object, object, object>((id, status, line) => { Trace.WriteLine(line); })); Task.Run(() => { try { Thread.Sleep(1000); plugn.stopFileListen("test"); //plugn.listenOnFile("test", @"c:\Temp\test.txt", true, new Action<object, object, object>((id, status, line) => //{ // Trace.WriteLine(line); //})); } catch (Exception ex) { //callback(string.Format("error: ", ex.ToString())); } }); Console.ReadLine(); }
internal void Start() { if (!countersAvailable) { logger.Warn(ErrorCode.PerfCounterNotRegistered, "CPU & Memory perf counters did not initialize correctly - try repairing Windows perf counter config on this machine with 'lodctr /r' command"); } if (cpuCounterPF != null) { cpuUsageTimer = new SafeTimer(CheckCpuUsage, null, CPU_CHECK_PERIOD, CPU_CHECK_PERIOD); } try { if (cpuCounterPF != null) { // Read initial value of CPU Usage counter CpuUsage = cpuCounterPF.NextValue(); } } catch (InvalidOperationException) { // Can sometimes get exception accessing CPU Usage counter for first time in some runtime environments CpuUsage = 0; } FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_CPUUSAGE, () => CpuUsage); IntValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_TOTALMEMORYKB, () => (long)((MemoryUsage + KB - 1.0) / KB)); // Round up #if LOG_MEMORY_PERF_COUNTERS // print GC stats in the silo log file. StringValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_GENCOLLECTIONCOUNT, () => GCGenCollectionCount); StringValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_GENSIZESKB, () => GCGenSizes); if (timeInGCPF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_PERCENTOFTIMEINGC, () => timeInGCPF.NextValue()); } if (allocatedBytesPerSecPF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_ALLOCATEDBYTESINKBPERSEC, () => allocatedBytesPerSecPF.NextValue() / KB); } if (promotedMemoryFromGen1PF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_PROMOTEDMEMORYFROMGEN1KB, () => promotedMemoryFromGen1PF.NextValue() / KB); } if (largeObjectHeapSizePF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_LARGEOBJECTHEAPSIZEKB, () => largeObjectHeapSizePF.NextValue() / KB); } if (promotedFinalizationMemoryFromGen0PF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_PROMOTEDMEMORYFROMGEN0KB, () => promotedFinalizationMemoryFromGen0PF.NextValue() / KB); } if (numberOfInducedGCsPF != null) { FloatValueStatistic.FindOrCreate(StatisticNames.RUNTIME_GC_NUMBEROFINDUCEDGCS, () => numberOfInducedGCsPF.NextValue()); } IntValueStatistic.FindOrCreate(StatisticNames.RUNTIME_MEMORY_TOTALPHYSICALMEMORYMB, () => (long)((TotalPhysicalMemory / KB) / KB)); if (availableMemoryCounterPF != null) { IntValueStatistic.FindOrCreate(StatisticNames.RUNTIME_MEMORY_AVAILABLEMEMORYMB, () => (long)((AvailableMemory / KB) / KB)); // Round up } #endif IntValueStatistic.FindOrCreate(StatisticNames.RUNTIME_DOT_NET_THREADPOOL_INUSE_WORKERTHREADS, () => { int maXworkerThreads; int maXcompletionPortThreads; ThreadPool.GetMaxThreads(out maXworkerThreads, out maXcompletionPortThreads); int workerThreads; int completionPortThreads; // GetAvailableThreads Retrieves the difference between the maximum number of thread pool threads // and the number currently active. // So max-Available is the actual number in use. If it goes beyond min, it means we are stressing the thread pool. ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); return(maXworkerThreads - workerThreads); }); IntValueStatistic.FindOrCreate(StatisticNames.RUNTIME_DOT_NET_THREADPOOL_INUSE_COMPLETIONPORTTHREADS, () => { int maXworkerThreads; int maXcompletionPortThreads; ThreadPool.GetMaxThreads(out maXworkerThreads, out maXcompletionPortThreads); int workerThreads; int completionPortThreads; ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); return(maXcompletionPortThreads - completionPortThreads); }); }
public MainWindowViewModel() { this.disposables = new CompositeDisposable(); this.Log = new ObservableCollection <string>(); this.Count = new ReactiveProperty <int>(8).AddTo(this.disposables); //ThreadPool.SetMinThreads(100, 100); #region ThreadPoolStatus var maxThreads = ObserveEveryValueChangedExtensions.ObserveEveryValueChanged(this, _ => { int workerThreads; int completionPortThreads; ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads); return(new ThreadPoolStatus(workerThreads, completionPortThreads)); }) .Publish().RefCount(); var availableThreads = ObserveEveryValueChangedExtensions.ObserveEveryValueChanged(this, _ => { int workerThreads; int completionPortThreads; ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); return(new ThreadPoolStatus(workerThreads, completionPortThreads)); }) .Publish().RefCount(); var minThreads = ObserveEveryValueChangedExtensions.ObserveEveryValueChanged(this, _ => { int workerThreads; int completionPortThreads; ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); return(new ThreadPoolStatus(workerThreads, completionPortThreads)); }) .Publish().RefCount(); this.MaxWorkerThreads = maxThreads.Select(x => x.WorkerThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.AvailableWorkerThreads = availableThreads.Select(x => x.WorkerThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.BusyWorkerThreads = this.MaxWorkerThreads .CombineLatest(this.AvailableWorkerThreads, (max, available) => max - available) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.MinWorkerThreads = minThreads.Select(x => x.WorkerThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.MaxCompletionPortThreads = maxThreads.Select(x => x.CompletionPortThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.AvailableCompletionPortThreads = availableThreads.Select(x => x.CompletionPortThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.BusyCompletionPortThreads = this.MaxCompletionPortThreads .CombineLatest(this.AvailableCompletionPortThreads, (max, available) => max - available) .ToReadOnlyReactiveProperty().AddTo(this.disposables); this.MinCompletionPortThreads = minThreads.Select(x => x.CompletionPortThreads) .ToReadOnlyReactiveProperty().AddTo(this.disposables); #endregion // asynchronous without thread pool this.HeavyWorkCommand1 = new ReactiveCommand().AddTo(this.disposables); this.HeavyWorkCommand1.Subscribe(async _ => { using (new TestContainer("1", this.Log)) { await Task.WhenAll(Enumerable.Range(0, this.Count.Value).Select(c => Task.Delay(2000))); } }) .AddTo(this.disposables); // asynchronous with thread pool this.HeavyWorkCommand2 = new ReactiveCommand().AddTo(this.disposables); this.HeavyWorkCommand2.Subscribe(async _ => { using (new TestContainer("2", this.Log)) { await Task.WhenAll(Enumerable.Range(0, this.Count.Value).Select(c => Task.Run(() => Thread.Sleep(2000)))); } }) .AddTo(this.disposables); // synchronous on UI thread this.HeavyWorkCommand3 = new ReactiveCommand().AddTo(this.disposables); this.HeavyWorkCommand3.Subscribe(_ => { using (new TestContainer("3", this.Log)) { Task.Delay(2000).Wait(); } }) .AddTo(this.disposables); }
/// <summary> /// Get a thread pool report. /// </summary> /// <returns></returns> public static string GetThreadPoolReport() { StringBuilder sb = new StringBuilder(); // framework pool is alwasy active int maxWorkers; int minWorkers; int curWorkers; int maxComp; int minComp; int curComp; try { ThreadPool.GetMaxThreads(out maxWorkers, out maxComp); ThreadPool.GetMinThreads(out minWorkers, out minComp); ThreadPool.GetAvailableThreads(out curWorkers, out curComp); curWorkers = maxWorkers - curWorkers; curComp = maxComp - curComp; sb.Append("\nFramework main threadpool \n"); sb.AppendFormat("workers: {0} ({1} / {2})\n", curWorkers, maxWorkers, minWorkers); sb.AppendFormat("Completion: {0} ({1} / {2})\n", curComp, maxComp, minComp); } catch { } if (Util.FireAndForgetMethod == FireAndForgetMethod.QueueUserWorkItem) { sb.AppendFormat("\nThread pool used: Framework main threadpool\n"); return(sb.ToString()); } string threadPoolUsed = null; int maxThreads = 0; int minThreads = 0; int allocatedThreads = 0; int inUseThreads = 0; int waitingCallbacks = 0; if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool) { STPInfo stpi = Util.GetSmartThreadPoolInfo(); // ROBUST currently leaves this the FireAndForgetMethod but never actually initializes the threadpool. if (stpi != null) { threadPoolUsed = "SmartThreadPool"; maxThreads = stpi.MaxThreads; minThreads = stpi.MinThreads; inUseThreads = stpi.InUseThreads; allocatedThreads = stpi.ActiveThreads; waitingCallbacks = stpi.WaitingCallbacks; } } if (threadPoolUsed != null) { sb.Append("\nThreadpool (excluding script engine pools)\n"); sb.AppendFormat("Thread pool used : {0}\n", threadPoolUsed); sb.AppendFormat("Max threads : {0}\n", maxThreads); sb.AppendFormat("Min threads : {0}\n", minThreads); sb.AppendFormat("Allocated threads : {0}\n", allocatedThreads < 0 ? "not applicable" : allocatedThreads.ToString()); sb.AppendFormat("In use threads : {0}\n", inUseThreads); sb.AppendFormat("Work items waiting : {0}\n", waitingCallbacks < 0 ? "not available" : waitingCallbacks.ToString()); } else { sb.AppendFormat("Thread pool not used\n"); } return(sb.ToString()); }
private static async Task processVideo() { //int maxThreads = var progressHandler = new Progress <ProcessingProgressReport>(value => { //Console.WriteLine(value.message); Console.Write("\r" + value.message); //status_txt.Text = value.message; //if (value.drawStats) drawStats(value.currentIndex); }); Console.WriteLine("Commencing processing."); Console.WriteLine("Yada yada blah blah."); Console.WriteLine(); Console.WriteLine(); var progress = progressHandler as IProgress <ProcessingProgressReport>; await Task.Run(() => { int frameCount = loadedVideo.Length; int[] sortedIndizi = new int[loadedVideo.Length]; bool[] alreadyUsedFrames = new bool[loadedVideo.Length]; alreadyUsedFrames[0] = true; int width = loadedVideo[0].width; int height = loadedVideo[0].height; int pixelCount = width *height; int pixelCountX3 = width *height * 3; int stride = loadedVideo[0].stride; int channelMultiplier = 3; // Stats Stopwatch stopWatch = new Stopwatch(); ProcessingProgressReport progressReport = new ProcessingProgressReport(); fps = new float[loadedVideo.Length]; smallestDifferences = new float[loadedVideo.Length]; lowestFPS = float.PositiveInfinity; highestFPS = 0; smallestSmallestDifference = float.PositiveInfinity; highestSmallestDifference = 0; // Vectorization related stuff int elementsPerVector = Vector <short> .Count; int elementsPerTwoVectors = 2 * elementsPerVector; int maxDifferencesPerVector = short.MaxValue / 255; // How often can we add a difference (assuming unlikely case of 255 each time) to a vector until it overflows? Then we need to flush. int oneFrameTotalLength = loadedVideo[0].imageData.Length; int currentFrame = 0; //Vector3Image currentFrameData; double[] frameDifferences = new double[frameCount]; int maxWorkers = 0; int maxCompletionPortThreads = 0; ThreadPool.GetMaxThreads(out maxWorkers, out maxCompletionPortThreads); progress.Report("Starting processing of " + frameCount + " frames with up to " + maxWorkers + "(workers)/" + maxCompletionPortThreads + "(IO) ..."); #if DEBUG try { #endif stopWatch.Start(); for (int currentIndex = 0; currentIndex < frameCount; currentIndex++) { // not to confuse with current frame. We're just tracking our progress here. double smallestDifference = double.PositiveInfinity; int smallestDifferenceFrame = -1; //currentFrameData = new Vector3Image(loadedVideo[currentFrame]); //ParallelOptions options = new ParallelOptions(); //options.MaxDegreeOfParallelism = 4; double smallestDifferenceImpreciseOpt = double.PositiveInfinity; // Go through all frames, comparing them to the current frame Parallel.For(0, frameCount, (compareFrame) => { //File.WriteAllText(compareFrame + ".txt", ""); // debug frameDifferences[compareFrame] = double.PositiveInfinity; if (compareFrame == currentFrame) { return; // No need to compare to itself. } if (alreadyUsedFrames[compareFrame] == true) { return; // No need to go through already used frames } double thisFrameDifference = 0; /*Vector3 thisFrameRGBDifference = new Vector3() { X = 0, Y = 0, Z = 0 }; * Vector3 currentFramePixel = new Vector3() { X = 0, Y = 0, Z = 0 }; * Vector3 compareFramePixel = new Vector3() { X = 0, Y = 0, Z = 0 };*/ Vector <short> thisFrameRGBDifference = new Vector <short>(0); Vector <short> currentFramePixel = new Vector <short>(); Vector <short> currentFramePixel2 = new Vector <short>(); Vector <short> compareFramePixel = new Vector <short>(); Vector <short> compareFramePixel2 = new Vector <short>(); //Vector<short> tmpDiff = new Vector<short>(); // Calculate difference int baseIndex; //int pixelOffsetBase; int i, a; int donePixelsPerVectorElement = 0; for (i = 0; i < oneFrameTotalLength; i += elementsPerTwoVectors) { Vector.Widen(new Vector <sbyte>(loadedVideo[currentFrame].imageData, i), out currentFramePixel, out currentFramePixel2); Vector.Widen(new Vector <sbyte>(loadedVideo[compareFrame].imageData, i), out compareFramePixel, out compareFramePixel2); thisFrameRGBDifference += Vector.Abs <short>(currentFramePixel - compareFramePixel); thisFrameRGBDifference += Vector.Abs <short>(currentFramePixel2 - compareFramePixel2); // Danger: XOR fuckery //tmpDiff = currentFramePixel - compareFramePixel; //(tmpDiff ^ (tmpDiff >> 31)) - (tmpDiff >> 31); // /Danger XOR fuckery over donePixelsPerVectorElement += 2; if (donePixelsPerVectorElement >= (maxDifferencesPerVector - 2)) { for (a = 0; a < elementsPerVector; a++) { thisFrameDifference += thisFrameRGBDifference[a]; } donePixelsPerVectorElement = 0; if (thisFrameDifference / pixelCountX3 > smallestDifferenceImpreciseOpt) { break; } thisFrameRGBDifference = new Vector <short>(0); } /*for(a=0; a < elementsPerVector; a++) * { * currentFramePixel.Item[] * }*/ } if (donePixelsPerVectorElement > 0) { for (a = 0; a < elementsPerVector; a++) { thisFrameDifference += thisFrameRGBDifference[a]; } } /* * for (int y = 0; y < height; y++) * { * //pixelOffsetBase = y * width; * for (int x = 0; x < width; x++) * { * baseIndex = stride * y + x * channelMultiplier; * currentFramePixel.X = loadedVideo[currentFrame].imageData[baseIndex]; * currentFramePixel.Y = loadedVideo[currentFrame].imageData[baseIndex + 1]; * currentFramePixel.Z = loadedVideo[currentFrame].imageData[baseIndex + 2]; * compareFramePixel.X = loadedVideo[compareFrame].imageData[baseIndex]; * compareFramePixel.Y = loadedVideo[compareFrame].imageData[baseIndex + 1]; * compareFramePixel.Z = loadedVideo[compareFrame].imageData[baseIndex + 2]; * thisFrameRGBDifference += Vector3.Abs(currentFramePixel - compareFramePixel); * //thisFrameRGBDifference += Vector3.Abs(currentFrameData.imageData[pixelOffsetBase+x]-compareFramePixel); * //thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex] - loadedVideo[compareFrame].imageData[baseIndex]); * //thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex + 1] - loadedVideo[compareFrame].imageData[baseIndex + 1]); * //thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex + 2] - loadedVideo[compareFrame].imageData[baseIndex + 2]); * } * //if (thisFrameDifference / pixelCountX3 > smallestDifferenceImpreciseOpt) break; // fast skip for very different frames. Since this is multithreaded, this might not always be correct in the sense of always having the right number in smallestDifference, but might work as optimization. * thisFrameDifference = thisFrameRGBDifference.X + thisFrameRGBDifference.Y + thisFrameRGBDifference.Z; * if (thisFrameDifference / pixelCountX3 > smallestDifferenceImpreciseOpt) break; // fast skip for very different frames. Since this is multithreaded, this might not always be correct in the sense of always having the right number in smallestDifference, but might work as optimization. * } * thisFrameDifference = thisFrameRGBDifference.X + thisFrameRGBDifference.Y + thisFrameRGBDifference.Z;*/ frameDifferences[compareFrame] = thisFrameDifference / pixelCountX3; if (frameDifferences[compareFrame] < smallestDifferenceImpreciseOpt) { smallestDifferenceImpreciseOpt = frameDifferences[compareFrame]; } //if (compareFrame % 1000 == 0) //{ // progress.Report("Processing: " + currentIndex + "/" + frameCount + " ordered frames. Current frame is "+currentFrame+" comparing to "+compareFrame); //} }); for (int compareFrame = 0; compareFrame < frameCount; compareFrame++) { if (frameDifferences[compareFrame] < smallestDifference) { smallestDifference = frameDifferences[compareFrame]; smallestDifferenceFrame = compareFrame; } } /*for (int compareFrame = 0; compareFrame < frameCount; compareFrame++) * { * frameDifferences[compareFrame] = double.PositiveInfinity; * if (compareFrame == currentFrame) continue; // No need to compare to itself. * if (alreadyUsedFrames[compareFrame] == true) continue; // No need to go through already used frames * * double thisFrameDifference = 0; * // Calculate difference * int baseIndex; * for (int y = 0; y < height; y++) * { * for (int x = 0; x < width; x++) * { * baseIndex = stride * y + x*channelMultiplier; * thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex] - loadedVideo[compareFrame].imageData[baseIndex]); * thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex + 1] - loadedVideo[compareFrame].imageData[baseIndex + 1]); * thisFrameDifference += Math.Abs(loadedVideo[currentFrame].imageData[baseIndex + 2] - loadedVideo[compareFrame].imageData[baseIndex + 2]); * } * if (thisFrameDifference / pixelCountX3 > smallestDifferenceImpreciseOpt) break; // fast skip for very different frames. Since this is multithreaded, this might not always be correct in the sense of always having the right number in smallestDifference, but might work as optimization. * } * frameDifferences[compareFrame] = thisFrameDifference / pixelCountX3; * if (frameDifferences[compareFrame] < smallestDifference) * { * smallestDifference = frameDifferences[compareFrame]; * smallestDifferenceFrame = compareFrame; * } * }*/ //Stats fps[currentIndex] = 1 / ((float)stopWatch.ElapsedTicks / (float)Stopwatch.Frequency); if (fps[currentIndex] < lowestFPS) { lowestFPS = fps[currentIndex]; } if (fps[currentIndex] > highestFPS) { highestFPS = fps[currentIndex]; } smallestDifferences[currentIndex] = (float)smallestDifference; if (smallestDifference < smallestSmallestDifference) { smallestSmallestDifference = smallestDifferences[currentIndex]; } if (smallestDifference > highestSmallestDifference && smallestDifference != double.PositiveInfinity) { highestSmallestDifference = smallestDifferences[currentIndex]; } progressReport.drawStats = currentIndex % 100 == 0; // Only after 100 processed frames draw stats, to not have a notable performance impact stopWatch.Restart(); // Status update progressReport.message = "Processing: " + currentIndex + "/" + frameCount + " ordered frames.";//+" Current frame is " + currentFrame + ", last smallest difference was " + smallestDifference; progressReport.currentIndex = currentIndex; progress.Report(progressReport); if (smallestDifferenceFrame != -1) { sortedIndizi[currentIndex] = smallestDifferenceFrame; currentFrame = smallestDifferenceFrame; alreadyUsedFrames[smallestDifferenceFrame] = true; } } #if DEBUG } catch (Exception e) { Console.WriteLine("le error: " + e.Message); } #endif progressReport.message = "Processing finished"; progressReport.drawStats = true; progressReport.currentIndex = frameCount - 1; progress.Report(progressReport); orderedVideo = sortedIndizi; }); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Finished processing"); }
/// <summary> /// base "program" to start running the process <see cref="System.Console.WriteLine(System.String)"/> - <para>see <see cref="recursive_robocopy.classes.RobocopyTask.doRoboCopy()"/> for the algorithm and logic implementation</para> /// /// </summary> public void startRoboCopy() { System.Console.Out.WriteLine("START - Recursive robocopy"); // validate folders bool hasErrors = false; //// basics - neither should be blank // source - exists if (!System.IO.Directory.Exists(myContext.Folders.SourceFolder)) { hasErrors = true; System.Console.Out.WriteLine(String.Format("ERROR: Source directory does not exist ({0})", myContext.Folders.SourceFolder)); } // target - can be created (mostly access tests) try { // create the target directory System.IO.Directory.CreateDirectory(myContext.Folders.TargetFolder); } catch (IOException ioe) { //directory cannot be created - record and log details to console hasErrors = true; System.Console.Out.WriteLine(String.Format("ERROR: Target directory could not be created - Detail: ({0}) Dir: {1}", ioe.Message, myContext.Folders.TargetFolder)); consoleHelper.WriteUsage(); } if (hasErrors) // stop process on errors { return; } // TODO report start & implement all logging // create and queue the top-level task var robocopytask = new RobocopyTask(myContext.Folders); // setup the countdownevent object and pass it in as state var cde = new CountdownEvent(1); var cdeActiveTasks = new CountdownEvent(1); var semOutput = new Semaphore(1, 1); // semaphore to control output var myState = new doRoboCopyState(cde, cdeActiveTasks, semOutput, true); System.Console.Out.WriteLine(string.Format("QUEUED - {0}", myContext.Folders.SourceFolder)); // setup the thread pool int maxThreads, maxPortThreads; int localmaxThreads, localmaxPortThreads; int minThreads, minPortThreads; ThreadPool.GetMinThreads(out minThreads, out minPortThreads); ThreadPool.GetMaxThreads(out maxThreads, out maxPortThreads); // set max threads from the config or defaults if missing ThreadPool.SetMinThreads(1, minPortThreads); ThreadPool.SetMaxThreads((int)ConfigDefaults.getMaxThreads(), maxPortThreads); // set thread pool limits - don't touch the local port max ThreadPool.GetMaxThreads(out localmaxThreads, out localmaxPortThreads); ThreadPool.GetMinThreads(out minThreads, out minPortThreads); System.Console.Out.WriteLine(string.Format(" Thread Pool - Min {0} max {1} local max {2}", minThreads, maxThreads, localmaxThreads)); ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(robocopytask.doRoboCopy), myState); // Wait for everything to finish cde.Wait(); // report end - System.Console.Out.WriteLine("END - Recursive robocopy"); }
/// <summary> /// print out some periodic information on server statistics /// </summary> /// <param name="state"></param> public static void PrintStats(object state) { try { //Don't enable this line unless you have memory issues and //need more details in memory usage //GC.Collect(); long newTick = DateTime.Now.Ticks; long time = newTick - m_lastMeasureTick; m_lastMeasureTick = newTick; time /= 10000000L; if (time < 1) { log.Warn("Time has not changed since last call of PrintStats"); time = 1; // prevent division by zero? } long inRate = (Statistics.BytesIn - m_lastBytesIn) / time; long outRate = (Statistics.BytesOut - m_lastBytesOut) / time; long inPckRate = (Statistics.PacketsIn - m_lastPacketsIn) / time; long outPckRate = (Statistics.PacketsOut - m_lastPacketsOut) / time; m_lastBytesIn = Statistics.BytesIn; m_lastBytesOut = Statistics.BytesOut; m_lastPacketsIn = Statistics.PacketsIn; m_lastPacketsOut = Statistics.PacketsOut; // Get threadpool info int iocpCurrent, iocpMin, iocpMax; int poolCurrent, poolMin, poolMax; ThreadPool.GetAvailableThreads(out poolCurrent, out iocpCurrent); ThreadPool.GetMinThreads(out poolMin, out iocpMin); ThreadPool.GetMaxThreads(out poolMax, out iocpMax); int globalHandlers = GameEventMgr.NumGlobalHandlers; int objectHandlers = GameEventMgr.NumObjectHandlers; if (log.IsInfoEnabled) { StringBuilder stats = new StringBuilder(256) .Append("-stats- Mem=").Append(GC.GetTotalMemory(false) / 1024 / 1024).Append("MB") .Append(" Clients=").Append(GameServer.Instance.ClientCount) .Append(" Down=").Append(inRate / 1024).Append("kb/s (").Append(Statistics.BytesIn / 1024 / 1024).Append("MB)") .Append(" Up=").Append(outRate / 1024).Append("kb/s (").Append(Statistics.BytesOut / 1024 / 1024).Append("MB)") .Append(" In=").Append(inPckRate).Append("pck/s (").Append(Statistics.PacketsIn / 1000).Append("K)") .Append(" Out=").Append(outPckRate).Append("pck/s (").Append(Statistics.PacketsOut / 1000).Append("K)") .AppendFormat(" Pool={0}/{1}({2})", poolCurrent, poolMax, poolMin) .AppendFormat(" IOCP={0}/{1}({2})", iocpCurrent, iocpMax, iocpMin) .AppendFormat(" GH/OH={0}/{1}", globalHandlers, objectHandlers); lock (m_timerStatsByMgr.SyncRoot) { foreach (GameTimer.TimeManager mgr in WorldMgr.GetRegionTimeManagers()) { TimerStats ts = (TimerStats)m_timerStatsByMgr[mgr]; if (ts == null) { ts = new TimerStats(); m_timerStatsByMgr.Add(mgr, ts); } long curInvoked = mgr.InvokedCount; long invoked = curInvoked - ts.InvokedCount; stats.Append(" ").Append(mgr.Name).Append('=').Append(invoked / time).Append("t/s (") .Append(mgr.ActiveTimers).Append(')'); ts.InvokedCount = curInvoked; } } if (m_systemCpuUsedCounter != null) { stats.Append(" CPU=").Append(m_systemCpuUsedCounter.NextValue().ToString("0.0")).Append('%'); } if (m_processCpuUsedCounter != null) { stats.Append(" DOL=").Append(m_processCpuUsedCounter.NextValue().ToString("0.0")).Append('%'); } if (m_memoryPages != null) { stats.Append(" pg/s=").Append(m_memoryPages.NextValue().ToString("0.0")); } if (m_physycalDisk != null) { stats.Append(" dsk/s=").Append(m_physycalDisk.NextValue().ToString("0.0")); } log.Info(stats); } if (log.IsFatalEnabled) { lock (m_timerStatsByMgr.SyncRoot) { foreach (GameTimer.TimeManager mgr in WorldMgr.GetRegionTimeManagers()) { TimerStats ts = (TimerStats)m_timerStatsByMgr[mgr]; if (ts == null) { continue; } long curTick = mgr.CurrentTime; if (ts.Time == curTick) { log.FatalFormat("{0} stopped ticking; timer stacktrace:\n{1}\n", mgr.Name, Util.FormatStackTrace(mgr.GetStacktrace())); log.FatalFormat("NPC update stacktrace:\n{0}\n", Util.FormatStackTrace(WorldMgr.GetWorldUpdateStacktrace())); log.FatalFormat("Relocate() stacktrace:\n{0}\n", Util.FormatStackTrace(WorldMgr.GetRelocateRegionsStacktrace())); log.FatalFormat("Packethandlers stacktraces:\n{0}\n", PacketProcessor.GetConnectionThreadpoolStacks()); } ts.Time = curTick; } } } } catch (Exception e) { log.Error("stats Log callback", e); } finally { lock (typeof(StatPrint)) { if (m_timer != null) { m_timer.Change(ServerProperties.Properties.STATPRINT_FREQUENCY, 0); } } } }
public void SetUp() { ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads); ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads); }
Dictionary <string, string> GetConfigs() { Dictionary <string, string> configs = new Dictionary <string, string>(); #region Custom configs configs.Add("ParallelDistantRequestValue", Config.ParallelRequests.ToString()); #endregion #region ProcessModel ProcessModelSection pms = (ProcessModelSection)ConfigurationManager.OpenMachineConfiguration().GetSectionGroup("system.web").Sections["processModel"]; configs.Add("RequestQueueLimit", pms.RequestQueueLimit.ToString()); #endregion #region HttpRuntime RequestQueue HttpRuntime runtime = (HttpRuntime)((FieldInfo)typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static)).GetValue(null); object requestQueue = ((FieldInfo)typeof(HttpRuntime).GetField("_requestQueue", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)).GetValue(runtime); Assembly sweb = Assembly.Load(new AssemblyName("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")); Type RequestQueue = sweb.GetType("System.Web.RequestQueue");//.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); //System.Reflection.FieldInfo[] configs.Add("runtimeQueueCount", RequestQueue.GetField("_count", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField).GetValue(requestQueue).ToString()); #endregion #region Connection management object connMgt = ConfigurationManager.OpenMachineConfiguration().GetSectionGroup("system.net").Sections["connectionManagement"]; ConnectionManagementSection connMgtSection = connMgt as ConnectionManagementSection; int maxConns = 0; if (connMgtSection != null && connMgtSection.ConnectionManagement.Count > 0) { maxConns = connMgtSection.ConnectionManagement[0].MaxConnection; } else { maxConns = DefaultMaxConnection; } configs.Add("maxconnections", maxConns.ToString()); #endregion #region Thread pool available threads int wt, iot; ThreadPool.GetAvailableThreads(out wt, out iot); configs.Add("AvailableWorkerThreads", wt.ToString()); configs.Add("AvailableIOThreads", iot.ToString()); #endregion #region Thread pool Max threads int mwt, miot; ThreadPool.GetMaxThreads(out mwt, out miot); configs.Add("MaxWorkerThreads", mwt.ToString()); configs.Add("MaxIOThreads", miot.ToString()); #endregion #region Thread pool Max threads int minwt, miniot; ThreadPool.GetMinThreads(out minwt, out miniot); configs.Add("MinWorkerThreads", minwt.ToString()); configs.Add("MinIOThreads", miniot.ToString()); #endregion #region Timeout configs.Add("Timeout", Config.Timeout.ToString()); configs.Add("ReadWriteTimeout", Config.ReadWriteTimeout.ToString()); #endregion return(configs); }