Windows BOINC客户端 - 如何完全自定义处理器使用情况?

时间:2015-12-04 15:57:08

标签: windows boinc

随着时间的推移,我陷入了Boinc Manager界面关于CPU使用率的限制。当客户端在也用于某些其他活动的计算机上运行并且您希望最小化Boinc运行其进程的影响时,这一点尤为重要。

1 个答案:

答案 0 :(得分:0)

除了将Manager配置为仅在未使用计算机时运行,当CPU使用率低于某个阈值时,您还可以将其自定义为仅使用某些核心且仅在某些时间段内使用。

为了实现这一目标,步骤如下:

1)找到经理目录 - 这包含各种可执行文件(如boinccmd.exe),配置文件等。

2)在manager目录中编辑(如果不存在则创建)global_prefs_override.xml配置文件。这至少应包含您要动态更改的标记。在我的例子中,100

3)使用自定义应用程序更改global_prefs_override.xml并通知经理它已更改

就我个人而言,我创建了一个简单的C#Console应用程序,它根据提供的参数自动更改核心使用情况。

代码如下:

namespace BoincCustomizer
{
    class Program
    {
        public const String CustomFileName = "global_prefs_override.xml";
        public const String CoresUsedTag = "max_ncpus_pct";
        public const int DummyYear = 1;

        // this are some fixed public holidays 
        public static IList<DateTime> PublicHolidays = new List<DateTime>()
        {
            new DateTime(DummyYear, 1, 1),
            new DateTime(DummyYear, 1, 2),
            new DateTime(DummyYear, 1, 24),
            new DateTime(DummyYear, 5, 1),
            new DateTime(DummyYear, 8, 15),
            new DateTime(DummyYear, 11, 30),
            new DateTime(DummyYear, 12, 1),
            new DateTime(DummyYear, 12, 25),
            new DateTime(DummyYear, 12, 26)
        };

        public static DateTime GetTodayDate()
        {
            return new DateTime(2015, 12, 1); // DateTime.Now.Date;
        }

        public static bool IsFreeDay(DateTime date)
        {
            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                return true;

            DateTime checkDate = new DateTime(DummyYear, date.Month, date.Day);
            if (PublicHolidays.Contains(checkDate))
                return true;

            return false;
        }

        /// <summary>
        /// rewrites custom BOINC xml file based on provided parameters
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            // Parameters:
            // 1 - dayStartHour
            // 2 - nightStartHour
            // 3 - dayCpusPerc
            // 4 - nightCpusPerc
            // 5 - freeDayCpusPerc
            if (args.Length != 5)
            {
                Console.WriteLine("Please specify the following parameters: dayStartHour, nightStartHour, dayCpusPerc, nightCpusPerc, freeDayCpuPerc");
                Console.ReadLine();
                return;
            }

            int dayStartHour = Int32.Parse(args[0]);
            int nightStartHour = Int32.Parse(args[1]);
            int dayCpusPerc = Int32.Parse(args[2]);
            int nightCpusPerc = Int32.Parse(args[3]);
            int freeDayCpusPerc = Int32.Parse(args[4]);

            int cpuPerc = 0;
            if (IsFreeDay(GetTodayDate()))
                cpuPerc = freeDayCpusPerc;
            else
            {
                int currHour = DateTime.Now.Hour;
                bool isDay = currHour >= dayStartHour && currHour < nightStartHour;
                cpuPerc = isDay ? dayCpusPerc : nightCpusPerc;
            }

            String workingDirectory = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
            String overrideFilePath = Path.Combine(workingDirectory, CustomFileName);

            String allText = File.ReadAllText(overrideFilePath);
            String startTag = String.Format("<{0}>", CoresUsedTag);
            String stopTag = String.Format("</{0}>", CoresUsedTag);
            int startIndex = allText.IndexOf(startTag);
            int stopIndex = allText.IndexOf(stopTag);
            if (startIndex < 0 || stopIndex < 0)
            {
                Console.WriteLine("Could not find cpu usage token ");
                return;
            }

            String existingText = allText.Substring(startIndex, stopIndex - startIndex);
            String replacementText = String.Format("{0}{1}", startTag, cpuPerc);
            String replacedText = allText.Replace(existingText, replacementText);
            File.WriteAllText(overrideFilePath, replacedText);

            var startInfo = new ProcessStartInfo();
            startInfo.FileName = @"boinccmd.exe";
            startInfo.Arguments = @"--read_global_prefs_override";
            Process.Start(startInfo);
        }
    }
}

这是初步的,但它应该是定制经理如何运行其流程的良好开端。