How to set 1 isolated core in TwinCAT

Viewed 673

I have to set one isolated core in TwinCAT from a c# code. In the documentation online it is showed how to set the number of cores, the base time, the core limit and latency warning, but nothing about the number of isolated cores.

public void AssignCPUCores()
{
    ITcSysManager3 systemManager = project.Object;
    ITcSmTreeItem realtimeSettings = systemManager.LookupTreeItem("TIRS");
    string xml = null;
    MemoryStream stream = new MemoryStream();
    StringWriter stringWriter = new StringWriter();
    using (XmlWriter writer = XmlTextWriter.Create(stringWriter))
    {
        writer.WriteStartElement("TreeItem");
        writer.WriteStartElement("RTimeSetDef");
        writer.WriteElementString("MaxCPUs", "4"); //  looking at what changes in TwinCAT by changing this number, 
                                                 // I found out that this number is the number of shared cores 
        
        string affinityString = string.Format("#x{0}", ((ulong)CpuAffinity.MaskDual).ToString("x16"));
                        //the MaskDual/MaskQuad/MaskSingle defines how many boxes will be checked (i.e. how many cores will be used)

        writer.WriteElementString("Affinity", affinityString);
        writer.WriteStartElement("CPUs");

        //    WriteCpuProperties(writer, int coreId , int loadLimit, int baseTime, latencyWarning)
        WriteCpuProperties(writer, 0, 90, 1000, 0);

        writer.WriteEndElement();     // CPUs     
        writer.WriteEndElement();     // RTimeSetDef     
        writer.WriteEndElement();     // TreeItem
    }
    xml = stringWriter.ToString();
    realtimeSettings.ConsumeXml(xml);  //here modifies are applied
    ITcSmTreeItem tasks = systemManager.LookupTreeItem("TIRT");
    
    SetTaskProperties(tasks, CpuAffinity.CPU1);
}

Seems like there is no way to set the isolated cores. Is it possible or not? The goal would be to obtain something like this:

enter image description here

But throw the c# code the number of isolated cores is 0 and all cores has the (shared) between brackets.

2 Answers

I don't think this is possible. Normally when the number of isolated cores are changed, a reboot is needed in order for it to take effect. I would be surprised if you can do it with your code without a reboot.

Isolated cores are set on the machine independent of your code. Some config is written in to the PLC that is running besides windows. The PLC than claims a core upon boot-up and windows sees 1 core less. You must reboot for the (isolated) core configuration to be activated.

When you run a plc program you can specify on which cores your program should run. Problem is that TwinCAT behaves differently when isolated cores are present and not.

When isolated cores are present TwinCAT makes it mandatory to match the core configuration exactly. So your program must read the core configuration, alter it if needed and write it back. This way you can load your plc programs in any plc regardless of the CPU configuration.

This is not trivial but can be done.

Related