How to fix high CPU Usage issue?
#1

Hello!

So then, lately my server time by time, automaticly shuts down and I had no idea, why this was happening and I was recieving notification messages from the host:

Quote:

Your process (samp03svr) was automatically stopped at Wed Dec 2 14:34:10 GMT 2015 due to excessive CPU usage.
The average CPU usage of your server over the past 3 minutes was 90.8%.
This indicates that your server was using an abnormally high amount of the processor's available CPU time.

High usage such as this can be caused by infinite loops and poorly designed or malfunctioning scripts, plugins, mods or addons.

To prevent further interruptions, we recommend debugging your server to eliminate the source of the high CPU usage.
Running your server on your computer may give an accurate representation of the resource usage problem.

Our resource monitor only kills processes which have an average CPU usage of over 90% for the past three minutes. On a physical sense, this is 90% of the available CPU time of a single core of an Intel Xeon E3-1231 v3 @ 3.40GHz. This is the CPU that your server's host machine has. Typically, servers will not use this much CPU time unless an infinite loop is present. Under normal operation, this is exceptionally rare

I am sure, that this could not be caused by plugins or scripts and I am not using any kind of mods or addons.

I have heard, that this could be fixed by chaning something OnPlayerUpdate or OnServerUpdate, not sure.

Also, the server of course shuts down, when there are few players online.

Please help me to solve this issue, I need some advices to fix it as soon as possible.
Reply
#2

Check your loops, check your OnPlayerUpdate, try to avoid using OnPlayerUpdate if you arent too experienced.
OnPlayerUpdate gets called 2-3 times per secnod, it could cause high CPU usage, but mainly check your loops, infinite loops usualy lead to this problem.
Also besides everything, download CrashDetect plugin, it will help.


Also most important thing, print your loops, atleast what I do with any code I add, I print the results so I can know what could possibly cause odd behavious
Example function.
PHP код:
new vehiclesinloop;
MyTestFunction() {
    for(new 
1<= MAX_VEHICLESx++) {
        if(
IsValidVehicle(x)) {
            
vehiclesinloop++;
        }
    }
    
printf("Loop called %i times",vehiclesinloop);
    return 
true;

So there you know how many times your loop was called and if stopped or continued working etc. etc.
The coding isnt about only if there are bugs for players (Ie you forgot to add a check if player has enough money for something or such) its mainly about if the code itself works properly.

Also add some more informations to your thread, and possibly some code (if you use OnPlayerUpdate post it)
What sort of script are you using?
How many lines are in?
Have this occured before or it just started happening?
If it started just now, give us latest code that you added before it started crashing etc. etc. etc.
Reply
#3

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
OnPlayerUpdate gets called 2-3 times per secnod
More like 20-30 times per second. A loop also may not always manifest itself as an actual for loop or while loop. Using a function that calls a callback inside that callback will also cause this. For example using SpawnPlayer or TogglePlayerSpectating (false) within OnPlayerSpawn. Or using CanselSelectTextDraw within OnPlayerClickTextDraw.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
More like 20-30 times per second. A loop also may not always manifest itself as an actual for loop or while loop. Using a function that calls a callback inside that callback will also cause this. For example using SpawnPlayer or TogglePlayerSpectating (false) within OnPlayerSpawn. Or using CanselSelectTextDraw within OnPlayerClickTextDraw.
Yeah, just gave an example, depends.

He sent me his code, he had around 10-15 functions including few anti cheats etc. that were being called under OnPlayerUpdate, I gave him some timers etc, might help him out, but yeah I preety much think that OnPlayerUpdate was causing that CPU usage. Simply put, something that was supposted to be in 10ish timers or detected using other methods was pilled up under OnPlayerUpdate.
There were also 5-6 functions calling some other functions but as I said, Im gonna wait to see if he has resolved problem with few timers or whats up but Im preety sure OnPlayerUpdate was causing that high CPU usage
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)