If you have time to get a cup of coffee while your interactive programs load, take a look at the techniques presented in this article. We'll offer a few ways to speed up the interactive program initiation and reduce some of the load on your system.
What's the Holdup?
Users often wonder what makes programs take so long to load. Most programmers being asked this question would probably say something about the system not having enough memory or slow processors. If the program uses a lot of files, here are some great ways to reduce program initiation time without expanding your system.
From a performance standpoint, opening and closing files are two of the most overhead-intensive events that take place on the AS/400. If the interactive programs you work with have a large number of files (sometimes this is unavoidable), program initiation can seem to take forever while the program loads and all of the files are opened.
If you can find a way to spread out the work load by opening files at times other than program initiation, it stands to reason that the program would load in a shorter period of time. Two techniques that can allow you to accomplish this are user-controlled file opens and preopening of files. The following material explains how to implement both techniques.
User-controlled File Opens
IBM midrange systems have allowed user-controlled file opens for many years. This file-open capability has grown much more powerful on the AS/400 because of the changes IBM has made to RPG in the past several years.
First of all, you should open a file only if you intend to use it. As you see in 1, by placing a UC (user controlled) in the File Condition section of the F-spec, you can code your program to open files as you need them instead of when the program loads. Be sure to set a flag in the program indicating that the file has already been opened, so you will not attempt to open it again. Failure to include this step could result in a nasty RPG error if you have not included an error indicator on the open.
First of all, you should open a file only if you intend to use it. As you see in Figure 1, by placing a UC (user controlled) in the File Condition section of the F-spec, you can code your program to open files as you need them instead of when the program loads. Be sure to set a flag in the program indicating that the file has already been opened, so you will not attempt to open it again. Failure to include this step could result in a nasty RPG error if you have not included an error indicator on the open.
It is also possible that certain files may never be opened at all. Suppose the program contains multiple screens that are processed conditionally depending upon user responses. The program could be coded to open only those files associated with the screens that are selected. This situation has another advantage: files that are never opened need not be closed either.
Secondly, and perhaps more importantly, user-controlled file opens offer an excellent opportunity to distribute program overhead so that it is less noticeable to the end user. Even though the system performs the same amount of work, it is not as obvious to the end user because the file opens do not all occur at the same time.
The example in 2 illustrates how you can code the program so that the file opens occur while the program waits for a response from the end user.
The example in Figure 2 illustrates how you can code the program so that the file opens occur while the program waits for a response from the end user.
In this example, the WRITE and READ op codes replace EXFMT. Several user- controlled file opens have been placed between the WRITE and the READ statements. When this program executes, the screen panel is displayed and then the files are opened while the operator responds. Coding the program this way creates a condition where the files are opened while the program waits on the end user instead of the other way around.
For the example in 2 to work, you need to ensure that the display file sends the record format to the terminal without waiting until the program does a read. A couple of ways to do this include compiling the display file with DFRWRT(*NO) or using the FRCDTA keyword in the DDS.
For the example in Figure 2 to work, you need to ensure that the display file sends the record format to the terminal without waiting until the program does a read. A couple of ways to do this include compiling the display file with DFRWRT(*NO) or using the FRCDTA keyword in the DDS.
When implementing user-controlled file opens, you may choose to ignore closing the files explicitly and let the program close them automatically at LR. If the delay caused when the program ends creates problems for you, try using the RETRN op code instead of setting on LR. This change has other implications, however, so you may want to research further before making this change.
Preopened Files
The second technique to reduce program initiation overhead enlists a method called preopened files. This method can be very useful when multiple programs use the same data files. If you open the data or display files so that the open data paths can be shared, subsequent high-level language programs will not need to open the files.
In the example in 3, the Open Database File (OPNDBF) command opens the data files and the Override Database File (OVRDBF) command specifies that the open data path is to be shared. Subsequent calls to application programs within this same session will use the existing open data path instead of opening the file again. The term "shared" could be somewhat confusing, because other sessions do not have access to the open data paths.
In the example in Figure 3, the Open Database File (OPNDBF) command opens the data files and the Override Database File (OVRDBF) command specifies that the open data path is to be shared. Subsequent calls to application programs within this same session will use the existing open data path instead of opening the file again. The term "shared" could be somewhat confusing, because other sessions do not have access to the open data paths.
When you use OPNDBF to share the open data path, make sure the file is opened with the correct OPTION level. This controls whether the program opens the data path for input, output, or both. If any program is going to update the file, make sure you use OPTION (*ALL).
If your application uses the same files over and over again, those files may be opened and closed many times within a given day. The preopened-file technique can eliminate big delays in application program initiation and help to improve overall system performance significantly.
Words of Caution
I'd like to offer a word of warning for those who use the library list to manipulate which file on the system is to be used. When a file has been preopened with a shared open data path, an application program uses that file whether it is currently in your library list or not. The program simply uses the open data path of the shared file. If you do not have files with the same name in multiple libraries, this does not present a problem for you.
When using preopened files, keep in mind that the file is always in use as long as the session exists. It does not matter whether a program is actually running or not. This situation can be problematic at times when files are being saved or a job needs dedicated access to a file. Preopened files remain in use until the files are closed or the session is terminated.
You also cannot assume that the file pointer is positioned where you want it when a program is initiated. If you are going to be doing any type of sequential read operation, you should always remember to set your file pointer appropriately.
Other security and operational issues should be considered before trying this particular method. The Database Guide and the CL Reference manual offer more information on using preopened files.
In Conclusion
If end users complain about response time and your application uses a large number of files, you really should give these methods a try. You may be surprised how much difference the way your programs open and close files can make.
Doug Pence is the founder and Ron Hawkins is the research and development manager of Computer Processing Unlimited, Inc. in San Diego, California.
REFERENCES CL References (SC41-0030, CD-ROM QBKA8202). Database Guide (SC41-9569, CD-ROM QBKA7202).
Enhancing the Performance of Interactive Programs
Figure 1 User-controlled Opens
*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 FCUSTOMERIF E K DISK UC C OPNCUS IFNE 'Y' FILE NOT ALREADY C OPEN CUSTOMER OPEN, OPEN IT C MOVE 'Y' OPNCUS 1 C ENDIF C CUSKEY CHAINCUSTOMER 99 *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
Enhancing the Performance of Interactive Programs
Figure 2 User-controlled Opens Between Writing and Reading
*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 FDISPLAY CF E WORKSTN FCUSTOMERIF E K DISK UC FSALESMENIF E K DISK UC C WRITEFORMAT DISPLAY FILE FORMAT C OPNCUS IFNE 'Y' FILE NOT ALREADY C OPEN CUSTOMER OPEN, OPEN IT C MOVE 'Y' OPNCUS 1 C ENDIF C OPNSLS IFNE 'Y' C OPEN SALESMEN C MOVE 'Y' OPNSLS 1 C ENDIF C READ FORMAT 99 WAITS FOR INPUT *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
Enhancing the Performance of Interactive Programs
Figure 3 Sign-on CL Program to Preopen Files and Display In
PGM MONMSG MSGID(CPF0000) OVRDBF FILE(CUSTOMER) SHARE(*YES) OVRDBF FILE(SALESMEN) SHARE(*YES) OPNDBF FILE(CUSTOMER) OPTION(*ALL) OPNDBF FILE(SALESMEN) OPTION(*ALL) GO MENU(CUSTMENU) ENDPGM
LATEST COMMENTS
MC Press Online