Significant cpu is used before my script is run



  • Here is an excerpt from my log with the entire contents of my script being just
    console.log(Game.getUsedCpu());

    [6:29:12 AM]48.676411
    [6:29:13 AM]33.314896
    [6:29:14 AM]53.990168
    [6:29:16 AM]46.478472
    [6:29:18 AM]37.185643
    [6:29:19 AM]52.158025
    [6:29:20 AM]25.419606
    [6:29:22 AM]38.600426
    [6:29:23 AM]64.554256
    [6:29:24 AM]46.031705
    [6:29:26 AM]36.342681
    [6:29:27 AM]82.565116
    ...
    [6:33:53 AM]106.455747

    106? With a 100 cpu budget, that gives me -6 to execute my code.
    Right now it seems to average around 50 cpu used before my script is started.
    I deleted everything in Memory and there is no other modules other than "main" which contains the above code.

    It's hard to do anything with approximately 50 cpu, especially when it appears the JIT needs to recompile everything every frame.

    At the moment I have maybe 60 creep spread out over 16 rooms or so, is that related? Is it running some script to set up things (eg. Game and all the objects) and counting that as part of my cpu?

    Despite any factors regarding server load or such, I feel like it should count the used CPU honestly.



  • Just as a test, I suicided all of my creeps and it now outputs

    [6:53:39 AM]5.816676
    [6:53:40 AM]3.636976
    [6:53:42 AM]14.490425
    [6:53:43 AM]25.138307
    [6:53:45 AM]5.462814
    [6:53:46 AM]6.005194
    [6:53:48 AM]6.023788
    [6:53:49 AM]4.453297
    [6:53:51 AM]5.006698
    [6:53:52 AM]4.547158
    [6:53:53 AM]3.560707
    [6:53:56 AM]7.207466

    So that definitely helped, and reinforces my suspicion that the system runs some code to set up the Game interface and all related objects, and it counts the time used towards my used cpu. I don't know why it would take so long though, 60 creeps isn't much...
    Either way this is out of my control and it would be nice if it either didn't count it or it was optimized to be less significant.



  • fixed?



  • I'm having this problem too and I think I managed to track it down. I started by putting a Game.getUsedCpu() at the top of my main.js and it almost always had a value less than 2, so the reported cpu is actually going into the execution of your scripts.

    I have a cpu object in the global scope to keep track of cpu usage and a lot of time goes into the require() function it seems. I do most of my require'ing in a module called init, looking like this:

        cpu.track("init", function() {
            var modules = [
                "string", // *must* be first or catch-block will fail
                "global",
                "math",
                "protoGame",
                "protoGame",
                "protoSpawn",
                "protoCreep",
                "protoRoom",
                "protoSource",
                "protoStructure",
                "protoRoomPosition"
            ];
    
            _.each(modules, function(module) {
                try {
                    cpu.begin(module);
                    require(module)();
                    cpu.end(module);
                }
                catch (ex) {
                    console.log("Exception caught: {0}.Stack: {1}".format(ex, ex.stack));
                }
            });
    
            _.mixin(Room.prototype, brainRoom, { "chain": false });
    
            if (_.isUndefined(Memory.index)) {
                Memory.index = {};
            }
        });
    

    The output of that looks like this (I stripped away the items that were <2):
    ```
    protoCreep: 10.955753000000001
    protoRoom: 6.102737000000001
    protoRoom.require: 4.112070000000003
    init: 27.029208

    protoCreep: 7.1586099999999995
    protoRoom: 12.721302000000001
    protoRoom.require: 9.670390999999999
    init: 28.924640999999998
    ```

    Most of these files modify the prototypes of various game objects(hence the proto prefix) but I also timed the require calls and as you can see they take up most of the time.

    To add some more detailed output I decided to time the execution of the modules that are actually being required by adding a begin()/end() call at the top/bottom of the those to time how long it took the VM to evaluate the actual files. The results were as follows:

    protoRoom: 5.566376000000002
    EnergyMap: 0.04861100000000107
    B-Q: 0.0905040000000028
    C-Q: 0.08399100000000104
    Statistics: 0.04209700000000183
    protoRoom.require: 3.4115080000000013
    init: 22.651317

    It would be really nice if all the source files were actually pre-included before our main functions were executed so that the amount of time spent in require() calls would be reduced to a minimum. Any script file that throws a compile error is ignored. And of course, the scripts shouldn't be executed, just precompiled.