CPU Usage Spike [URGENT]
#1

What could cause a sudden spike in CPU usage, from 8% average CPU usage to suddenly going to 103% and crashing the server.

Commands don't seem to have a role as there isn't any significance in the commands done prior to the spike.
Scripting wise, what could cause it?
Reply
#2

It can be due to these.

1. Custom callbacks and codes.
2. Faulty codes.
3. Usage of Dini.
4. Your script calls a remote function or interacts with any kind of database.
Reply
#3

I'm still getting used to new 03 code (i left just after 03 released) but I do know in the past, my creating really shitty timers killed me in the CPU dept. Also, any time you loop through all the players, really make sure that 1) you need to and 2) all the things you do as it gets to each step (player) in the loop.

I get too much in the habit of nesting if/else and switch loops so bad it would really bog things down.
Reply
#4

Quote:
Originally Posted by kaisersouse
Посмотреть сообщение
I'm still getting used to new 03 code (i left just after 03 released) but I do know in the past, my creating really shitty timers killed me in the CPU dept. Also, any time you loop through all the players, really make sure that 1) you need to and 2) all the things you do as it gets to each step (player) in the loop.

I get too much in the habit of nesting if/else and switch loops so bad it would really bog things down.
+1 That can cause this too.
Reply
#5

Also, log EVERYTHING and use some way of parsing those logs into something that you can use to check for patterns. Does the espike happen during a certain loop or timer? Does a frequently-called callback have a ton of useless crap in it? etc.
Reply
#6

Quote:
Originally Posted by kaisersouse
Посмотреть сообщение
Also, log EVERYTHING and use some way of parsing those logs into something that you can use to check for patterns. Does the espike happen during a certain loop or timer? Does a frequently-called callback have a ton of useless crap in it? etc.
Thanks, i'll have a look at that. Is it possible for a sudden spike though, as you said maybe it would be the looping through all the players?
Reply
#7

Quote:
Originally Posted by Darklom
Посмотреть сообщение
Thanks, i'll have a look at that. Is it possible for a sudden spike though, as you said maybe it would be the looping through all the players?
A CPU spike would definitely happen if you are looping through all the players and doing a lot of slow stuff inside those loops. I used to get carried away w/ foreach statements and getting vehicle id's etc. Now each player gets a variable that maintains their state (onfoot, dead, driving, etc) and that variable changes in onplayerstate only. That way, instead of using GetPlayerVehicleID inside loops, I check pstate[playerid] instead. Thing like that could streamline your player-checks a bit. I don't know if thats your problem per se, but if it is, this might help.
Reply
#8

The server and virtual machinery is still very very fast and the exhaustive code that kaisersouse is talking about would still take only up to XXms to execute and such cases are quite rare. Although there's always room for improvement, don't go for premature optimization too much.

****** has made a great topic on the similar subject: Code optimisations. I think everyone should have a look there.

The most easily understandable example I can think of right now is having code like this:
pawn Код:
for(i = 0; i < GetMaxPlayers(); i++)
{
    if(IsPlayerConnected(i))
    {
        if(IsPlayerInAnyVehicle(i))
        {
            RepairVehicle(GetPlayerVehicleID(i));
        }
    }
}
While the loop might not be as unsophisticated when most coders write it, instead something like for(new i = 0; i != MAX_PLAYERS; i++), it still can be improved by starting to use foreach.
pawn Код:
foreach(i : Player)
{
    vID = GetPlayerVehicleID(i);
    if(vID)
    {
        RepairVehicle(vID);
    }
}
Sure, both codes are quite fast and you won't notice a thing when they execute even for hundreds of times, but it all comes down to good coding practices, which as kaisersouse mentioned, is a very important aspect of programming.

And I get carried away with my vehicle ID "optimization" talk once again while your server problem is most likely caused by another factor. Try to see if you're doing any huge file loads or MySQL operations which might have an effect on your server wholly.

kaisersouse - nice to see you back here.
Reply
#9

I develop the script with this problem.
I have one huge core timer using two foreach loops to process everything and there is absolutely no data saving, but it has a 1000ms interval. The code processed in this timer is measured to roughly 1100 lines, but I've tried to optimize it as much as I could.

I did some debugging to check how long it did take the timer to be processed (1 player) and the results were:
Код:
[18:27:21] *tick: 1
[18:27:23] *tick: 0
[18:27:24] *tick: 0
[18:27:25] *tick: 0
[18:27:26] *tick: 0
[18:27:27] *tick: 0
[18:27:28] *tick: 0
[18:27:30] *tick: 0
[18:27:31] *tick: 0
[18:27:32] *tick: 0
[18:27:33] *tick: 0
[18:27:34] *tick: 0
[18:27:35] *tick: 0
[18:27:36] *tick: 0
[18:27:38] *tick: 0
[18:27:39] *tick: 0
[18:27:40] *tick: 0
[18:27:41] *tick: 0
[18:27:42] *tick: 2
[18:27:43] *tick: 0
[18:27:44] *tick: 1
[18:27:46] *tick: 0
[18:27:47] *tick: 0
[18:27:48] *tick: 1
[18:27:49] *tick: 0
[18:27:50] *tick: 0
[18:27:51] *tick: 0
I used the crashdetect plugin to find the cause too, and I had a hint on my login script. Now, I can't see how this is affecting my script, since it happens randomly. Sometimes it's normal (no CPU usage increase) but sometimes it rises to over %100.
Reply
#10

Only thing I can think of are a loop made in your script which generates allot of cpu
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)