cpu.getUsed() negative result?



  • I use cpu.getUsed() to keep track of the cpu used by my functions and notify me when an abnormal amount of cpu is used. I often get a report for a lot of cpu usage (>500ms) because cpu.getUsed() returns negative numbers. This shouldn't be possible.

    I get this warning:

    {"CPUWARNING":"","OPERATIONTYPE":"HarvestingOp","cpuStart":-1289.7219116999995,"cpuUsed":1300.3438215999986}
    

    This is the code generating the warning:

            let cpuStart = Game.cpu.getUsed();
            // rest of code
            if (Game.cpu.getUsed() - cpuStart > MAX_OPERATION_CPU) {
                Game.notify(JSON.stringify({CPUWARNING: this.name, OPERATIONTYPE: this.constructor.name, cpuStart: cpuStart, cpuUsed: Game.cpu.getUsed() - cpuStart}));
            }
    

    I run this for all of my code. it triggers a couple of times per day because of this bug. cpuUsed is always between 1300 and 1301 when the bug triggers. cpuStart is between -1274 and -1297.

    Which shard is affected? Got this on Shard3, shard2, shard1, shard0

    What happened? Negative tick time from cpu.getUsed()

    What should have happened? Positive tick time result from cpu.getUsed()

    How can we reproduce this? Run my bot code (https://github.com/jerdaz/FormicAI)



  • Perhaps I'm missing it, but I don't see where you have this in your code, I don't see it in your main, main ops, utils or other locations on the github you provided.

    Its also unlikely you should be getting over ~500 as a result from any getUsed() I'd presume do to you should be cut off at that point. Is it possible you are storing your cpuUsed/cpuStart over multiple ticks in the heap? For example, if you declare before your main runs cpuStart and use it over multiple ticks and/or also adding/subtracting to it somewhere if the ref is still live?



  • It's in this code: https://github.com/jerdaz/FormicAI/blob/main/src/meta_operation.js

    Every class ending with 'Op', including MainOp extends this class, so it is called 100's of times each tick. The variable is stored locally in the function.

    It happens only once every few hours so it happens only once every 1.000.000 calls or so and only one random operation is affected. It happens in different child classes (CreepOp, AttackOp, BuildingOp, HarvestingOp etc) and if it happens I only see one notification in the tick.