Previous Thread
Next Thread
Print Thread
Background Job best practices #36816 15 Nov 23 09:18 PM
Joined: Nov 2006
Posts: 2,192
S
Stephen Funkhouser Online Content OP
Member
OP Online Content
Member
S
Joined: Nov 2006
Posts: 2,192
When writing a background application that is constantly iterating a directory to process any new files (in as close to real time as possible) what is the best practice for not having the process not hog too much CPU time?

The only A-Shell command I know of is to sleep for some period. Is this the only way, or am missing something? What would be an appropriate SLEEP time if that's the best we can do?

We are launching the process from A-Shell with XCALL AMOS,"submit cmd:fhkbgstart.cmd /next:0,0,0","Q"

Would it make any difference to try to launch the BG process with nice and ionice.


Stephen Funkhouser
Diversified Data Solutions
Re: Background Job best practices [Re: Stephen Funkhouser] #36817 16 Nov 23 01:46 AM
Joined: Jun 2001
Posts: 11,645
J
Jack McGregor Online Content
Member
Online Content
Member
J
Joined: Jun 2001
Posts: 11,645
The overhead here is in the directory scan, not the sleep (which has almost no overhead). So the first priority would be to make sure that the directory is kept as empty as possible, so that each iteration didn't have to scan over files that had previously been seen. Scanning a directory is pretty efficient too, as long as we're only looking at information that is in the directory inode. So if your scheme involves moving the file out of the directory once it has been processed, then I wouldn't worry too much about it, and you could probably set the sleep time to somewhere between 0.1 and 0.5 seconds without any noticeable overhead. But if the directory is going to accumulate thousands of files, that's another story.

The only way to make it more efficient would be to have the background process wait on some kind of signal, or maybe a TCP message, rather than repeating the directory scan every unit of sleep time. For example, you could have it sleep in a loop until it gets a signal, the simplest of which would be SIGINT (via MX_KILL or just KILL.LIT) causing it to jump the error trap routine ...
Code
on error goto TRAP

WAIT:
    ? "Waiting...";tab(-1,254);
    do
        sleep 10
        ? ".";tab(-1,254);
    loop
    ? "Fell out of loop"
    end

SCAN'DIRECTORY:
    ? "Scanning directory for new files..."
    ! <processing here>
    goto WAIT

TRAP:
    ? "Error ";err(0)
    if err(0)=1 then
        resume SCAN'DIRECTORY
    else
        ? "Aborting"
       end
    endif

In that case you'd need another signal, perhaps -TERM, to actually terminate it.

Setting the nice and/or ionice values presumably can't hurt. I think you can use renice on your own process (via XCALL HOSTEX), so that would be the simplest add-on. The other two would require changing the way you launch the process from AMOS to HOSTEX, but that's easy enough too. Again, I don't think there is much CPU overhead in your scheme (unless the directory contains a lot of files), so probably the ionice would be the more significant one. But I bet you'll have a hard time measuring it, since we're probably only talking about a one block read, which most of the time will already have been cached.

It might be interesting to try using RUNPROF on the program to see if you can measure any appreciable CPU time. (It doesn't have a provision to help you measure how much I/O overhead it's causing though.)



Re: Background Job best practices [Re: Stephen Funkhouser] #36836 20 Nov 23 06:47 PM
Joined: Sep 2002
Posts: 5,450
F
Frank Online Content
Member
Online Content
Member
F
Joined: Sep 2002
Posts: 5,450
Ahh I see the RUNPROF usage here... guess i read these threads out of order!

Steve - just curious how many files are in the ppn you are scanning. Performing a dir dir.lst=*.* on a large dir doesn't take much resources on a large server here.. of course you don't want to do this repetetively w/o some sort of sleep cycle - but 60 secs of sleep still saves a lot of overhead.


Moderated by  Jack McGregor, Ty Griffin 

Powered by UBB.threads™ PHP Forum Software 7.7.3