LINUXNAUTA

    Experimenting to assign the exclusive use of CPU Threads to FreeCAD

    September 2025

    The main idea is to let FreeCAD use processor threads exclusively for it, this will improve performance in slightly large assemblies etc etc. I'm not an expert - I like to experiment - so use this at your risk.

    I use Void Linux and the package we need is cpuset , I don't know about it in other distributions but I assume it must be very similar.

    juanma@VOID:~$ xbps-query -Rs cpuset
    [*] cpuset-1.6.2_2 Wrapper to make kernel cpusets facilities easier to use
    
    
    juanma@VOID:~$ xbps-query -Rs freecad
    [*] freecad-1.0.2_3       General purpose 3D CAD modeler
    

    Process - How to !

    We start FreeCAD and look for what the process is runnig.

    juanma@VOID:~$ pgrep -f FreeCAD
    16636
    
    

    or using 'btop'

    freecad-cset

    I have an AMD Ryzen 5 5600X with 6 cores and 12 threads and for this example I am going to assign 2 exclusive threads , 6 and 11 - why ? I want two threads and I have chosen them at random. It could be more than 2 but this is a test.

    To view cset status and processes and threads:

    juanma@VOID:~$ cset shield -s -v 
    cset: **> shielding not active on system 
    

    We reserve the threads:

    juanma@VOID:~$ sudo cset shield --cpu 6,11 
    Password: 
    cset: --> activating shielding: cset: moving 1455 tasks from root into system cpuset... 
    [======== cset: "system" cpuset of CPUSPEC(0-5,7-10) with 1455 tasks running 
    cset: "user" cpuset of CPUSPEC(6,11) with 0 tasks running 
    

    We assign the FreeCAD pid

    juanma@VOID:~$ sudo cset shield --shield --pid 16636 
    cset: --> shielding following pidspec: 16636 cset: done 
    

    We review the status to verify if they have been assigned correctly:

    juanma@VOID:~$ cset shield -s -v 
    
    cset: "user" cpuset of CPUSPEC(6,11) with 1 task running 
    
    USER PID PPID SPPR TASK NAME 
    -------- ----- ----- ------- 
    juanma 16636 1 Soth /usr/lib/freecad/bin/FreeCAD - --single-instance 
    cset: done 
    

    Seeing it in btop - for example doing a render or something that consumes a lot - we have:

    freecad-cset

    Works OK!!!

    To reverse all this we could unassign the pid to the threads or there is also an option to reset everything - this can be investigated and experimented with - but in this example I do a reset.

    juanma@VOID:~$ sudo cset shield --reset 
    Password: 
    cset: --> deactivating/resetting shielding cset: moving 1 tasks from "/user" user set to root set... 
    cset: moving 1435 tasks from "/system" system set to root set... 
    [======== cset: deleting "/user" and "/system" sets cset: done 
    

    we review the status :

    juanma@VOID:~$ cset shield -s -v 
    cset: **> shielding not active on system 
    

    Everything ok.

    Note: I have not made any benchmarks or anything like that , this is for fun and experimenting !!!

    template download


    This is the script , modify it if you need to.

    
      
        
    #!/usr/bin/env bash
    
    # This program tries to asign exclusive use of CPU Threads to FREECAD
    # to allow the program to runs smootly
    
    # This program needs to use 'sudo or doas' for certan tasks
    
    # JuanMa <numblinux@gmail.com>
    # https://linuxnauta.com
    # 2025
    set -m
    
    VERSION='0.1'
    PROGRAM='FreeCAD' # this is the name in Void Linux
    CORES=(6 11)
    
    
        #If we run the script with unset as parameter we reset all and exit
        if [ "$1" == "unset" ]; then
            sudo cset shield --reset
            cset shield -s -v
            exit
        fi
    
        #Run FreeCAD
        nohup FreeCAD &
    
        sleep 2000 &
    
        pid=$(pgrep -f "$PROGRAM")
    
        #Is running ?
        if [ -z "${pid}" ]; then
            echo "${PROGRAM} is not running ... so ... exit"
            exit
        else
            echo "The PID of $1 is: $pid"
        fi
    
    
        echo "trying to set exclusive use of cores  ${CORES[@]}"
    
        echo -e "\n"
    
        cset shield -s -v
    
    
        for value in ${CORES[@]}
            do
                CORESTXT=${CORESTXT}"${value},"
            done
    
        # remove the last ","
        CORESTXT=${CORESTXT:0:-1}
    
        echo CORESTXT
    
        sudo cset shield --cpu ${CORESTXT} --kthread
        sudo cset shield --shield --pid ${pid}
    
        cset shield -s -v
    
    
        echo "-----------------------------------------------------------"
        echo " DONT FORGET to RUN  'sudo cset shield --reset' to clear all"
        echo "-----------------------------------------------------------"
        echo " or run the script again with unset as parameter 'sudo freecad-cset unset"
        echo "-----------------------------------------------------------"
    
        exit
    
    
    

    September 2025