ruarai / CompilePal

A tool to assist in the compiling of source engine maps
GNU General Public License v3.0
224 stars 26 forks source link

Increased performance on 16+ core CPUs by limiting VRAD. #106

Open Lpfreaky90 opened 4 years ago

Lpfreaky90 commented 4 years ago

VRAD can run a maximum of 16 CPU threads. (see wiki: https://developer.valvesoftware.com/wiki/VRAD ) On CPUs with more than 16 cores, the process keeps bouncing between cores, creating a severe slowdown. By setting the Processor affinity to cores 0-15, we improve the performance significantly

Exactol commented 4 years ago

How will this affect modded compile tools that support up to 32 threads? https://developer.valvesoftware.com/wiki/Increased_Thread_Limit_for_Compile_Tools

Lpfreaky90 commented 4 years ago

Good question, I'm not sure, will investigate.

Lpfreaky90 commented 4 years ago

Unfortunately I cannot properly monitor the number of threads that VRAD actually spawns.

A somewhat hacky solution would be something akin this:


            //By assigning affinity to the process significantly speed up compiles on CPUs with more than 16 cores.
            if ((Name == "VRAD") &&
                Environment.ProcessorCount > 16)
            {
                //Give the system 100ms to spawn all its threads.
                System.Threading.Thread.Sleep(100);

                //monitor performance for a second.
                PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                var cpuUsage = cpuCounter.NextValue();
                System.Threading.Thread.Sleep(1000);
                cpuUsage = cpuCounter.NextValue();

                //if we haven't had full processor uselisation, we don't have a patched dll.
                //94.2 > 16/17, means that we have at least 16/17 cores running at 100%
                if (cpuUsage < 94.2f)
                {
                    //Hex 0xffff means core use core 0 through 15.
                    Process.ProcessorAffinity = (IntPtr)0xffff;
                }
            }```

But putting this behind an option will probably be a cleaner solution.
Exactol commented 4 years ago

Trying to detect how many threads its using does seem kindof hacky. I think adding a flag would be the best option