Raised This Month: $ Target: $400
 0% 

Question regarding set_task and code optimizations


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mugwump
Senior Member
Join Date: May 2004
Old 09-03-2004 , 22:59   Question regarding set_task and code optimizations
Reply With Quote #1

I am looking to optimize some aspects of a plugin I am working on which is now up to 14.5k lines of code, uses tons of variables and many tasks which run throughout a given round in CS. Some reports coming in from some beta server admins are that their servers are using more system resources and causing a higher load average overall, this I expected. I am considering ways to optimize the code and the first thing that comes to mind involves the tasks I have running.

How are tasks handled within amxx, are they spawned off as seperate threads or are they executed within a single thread? Do tasks use a standard pool of memory or do they have their own copies for variables and such? Many tasks I have running are for a single player and I am wondering if it is worth the time coming up with new approaches that have 1 task running to handle all of the players for specific things.

Any insight the amxx devs or other knowledgable people can shed on how tasks are handled will be appreciated!

-Mug
Mugwump is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 09-03-2004 , 23:48  
Reply With Quote #2

well, server_frame that would handle all the things would work better.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
BAILOPAN
Join Date: Jan 2004
Old 09-04-2004 , 00:23  
Reply With Quote #3

Mugwump, to appropriately answer your question, AMX Mod X is single threaded just as the HL server is.

Tasks work like this:
Every time the server executes a frame, a timer is increased. If the timer has increased by 0.1 seconds, a list of all the current tasks is checked. If a task's interval has been reached, the task is executed.

Therefore, tasks are decently efficient.

The question about allocating variables is an even better one. Each plugin has a heap and a stack of memory which is statically allocated before runtime. This means that although your plugin may do this:
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.

Plugin memory structure:
[CODE][HEAP-> <- STACK]

As you allocate new variables, you're not using more memory... you're just pointing toward memory that's already been allocated. You can verify this with the amx_Allot() and amx_Release() functions which just shift an index.

If anything, the best way to optimize your plugin is to shift the intensive work to an AMXx 0.20 module. The 0.20 module API gives an excellent and easy way to integrate your module with your plugin[s].

I hope this answers your question ;]
__________________
egg
BAILOPAN is offline
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 09-04-2004 , 00:36  
Reply With Quote #4

I found, its best to do very very little in server_frame. Do too much, you can lag the server easily, a set task to a very low amount might be better, as it wont lag the server as much
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
Mugwump
Senior Member
Join Date: May 2004
Old 09-04-2004 , 00:43  
Reply With Quote #5

Thanks for the responses guys and thanks Bail for the detailed explanation, I dont feel quite as guilty utilizing a ton of set_tasks now but I am curious about your suggestion to put the intensive code in a 0.20 module. Is there a plugin I can see to use as an example of this, I am not quite familiar with the new module api yet ...

Thanks!

-Mug
Mugwump is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 09-04-2004 , 00:46  
Reply With Quote #6

Quote:
Originally Posted by devicenull
I found, its best to do very very little in server_frame. Do too much, you can lag the server easily, a set task to a very low amount might be better, as it wont lag the server as much
Hmm. so use server_frame only when nessasary? Sounds good.

on a totally unrelated note, will multi-threading ever be ported or created again?
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
BAILOPAN
Join Date: Jan 2004
Old 09-04-2004 , 00:56  
Reply With Quote #7

Quote:
Originally Posted by Mugwump
Is there a plugin I can see to use as an example of this, I am not quite familiar with the new module api yet ...
I may convert CSDM to such a style

Twilight: No, making AMxx thread safe is very difficult
__________________
egg
BAILOPAN is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 09-04-2004 , 04:18  
Reply With Quote #8

Quote:
Originally Posted by BAILOPAN
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.
Hmm not really. The compiler still generates zerofill instructions so you will get 4096 iterations which set something to 0.
I was talking about this with EKS on IRC recently.. If you want to have the maximal performance from variables (and dont need them to be initialized everytime the execution thread sees them), make them global or static (if small supports static; i forgot )
__________________
hello, i am pm
PM is offline
Anpheus
Senior Member
Join Date: Aug 2004
Old 09-04-2004 , 13:07  
Reply With Quote #9

It may also be more efficient to put multiple set_tasks together, lets say you have two tasks running at the same interval for a specific player. Just merge them. There's no reason to have them called seperately.


The performance gain may be minute, however.
Anpheus is offline
BAILOPAN
Join Date: Jan 2004
Old 09-04-2004 , 13:48  
Reply With Quote #10

Quote:
Originally Posted by PM
Quote:
Originally Posted by BAILOPAN
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.
Hmm not really. The compiler still generates zerofill instructions so you will get 4096 iterations which set something to 0.
I thought the compiler wasn't generating these fill instructions anymore? At least the web compiler version was orginally known for this. I know in my asm programs I never zerofill :p

Anpheus: That's a very good idea
__________________
egg
BAILOPAN is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:23.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode