Created
February 6, 2016 22:11
-
-
Save jeremyrsellars/3aea02e91fd31acd767c to your computer and use it in GitHub Desktop.
Affinitizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Management; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.ComponentModel; | |
static class Program | |
{ | |
static readonly string Name = Get("Name"); | |
static readonly string Pattern = Get("Pattern"); | |
static readonly string Core = Get("Core"); | |
static string Get(string name) | |
{ | |
string fullName = "Affinitize_" + name; | |
var value = Environment.GetEnvironmentVariable(fullName); | |
Console.WriteLine("Value of Environment variable " + fullName + " is " + value); | |
if(string.IsNullOrEmpty(value)) | |
throw new ArgumentNullException("value"); | |
return value; | |
} | |
static void Main() | |
{ | |
var commandLinePattern = new Regex(Pattern, RegexOptions.IgnoreCase); | |
var processes = (Name == "" ? Process.GetProcesses() : Process.GetProcessesByName(Name)) | |
.Where(HavePermissionToGetCommandLine) | |
.Where(p => commandLinePattern.IsMatch(p.GetCommandLine())) | |
.ToList(); | |
processes.ForEach(MaybeSetAffinity); | |
} | |
static bool HavePermissionToGetCommandLine(Process process) | |
{ | |
try | |
{ | |
process.GetCommandLine(); | |
return true; | |
} | |
catch(Win32Exception ex) | |
{ | |
if ((uint)ex.ErrorCode != 0x80004005) throw; | |
return false; | |
} | |
} | |
static void MaybeSetAffinity(Process process) | |
{ | |
var affinity = GetAffinity(); | |
if(process.ProcessorAffinity != affinity) | |
{ | |
Console.WriteLine("Changing process affinity to " + affinity + " for PID " + process.Id + " - " + process.GetCommandLine()); | |
SetAffinity(process, affinity); | |
} | |
else | |
Console.WriteLine("Affinity is already " + affinity + " for PID " + process.Id + " - " + process.GetCommandLine()); | |
} | |
static void SetAffinity(Process process, IntPtr affinity) | |
{ | |
process.ProcessorAffinity = affinity; | |
} | |
static IntPtr GetAffinity() | |
{ | |
return CoreToAffinityMask(int.Parse(Core)); | |
} | |
static IntPtr CoreToAffinityMask(int core) | |
{ | |
return (IntPtr) ((int) Math.Pow(2, core)); | |
} | |
private static string GetCommandLine(this Process process) | |
{ | |
var commandLine = new StringBuilder(process.MainModule.FileName); | |
commandLine.Append(" "); | |
using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id)) | |
{ | |
foreach (var @object in searcher.Get()) | |
{ | |
commandLine.Append(@object["CommandLine"]); | |
commandLine.Append(" "); | |
} | |
} | |
return commandLine.ToString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Affinitize.cs | |
echo @ECHO OFF>regularly_affinitize.cmd | |
echo SETLOCAL>>regularly_affinitize.cmd | |
echo set affinitize_core=3>>regularly_affinitize.cmd | |
echo set affinitize_pattern=.*>>regularly_affinitize.cmd | |
echo set affinitize_name=notepad>>regularly_affinitize.cmd | |
echo :repeat>>regularly_affinitize.cmd | |
echo echo =========== %time% ===========>>regularly_affinitize.cmd | |
echo Affinitize.exe>>regularly_affinitize.cmd | |
echo timeout /t 20>>regularly_affinitize.cmd | |
echo GOTO :repeat>>regularly_affinitize.cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment