colttaylor
Programmer
I had been running some of my older dos (turbo pascal) applications in Terminal Services and finding the performance to be less than I would want. After many attempts, I found the following changes which not only made my apps run at a usable speed, but also left the terminal server's resources practically untouched. Following these changes one of my customers is running twelve dos applications in six terminal server sessions on a 600mhz PII win2k box with 512m ram and the cpu usage rarely exceeds 2%. Here is what I did...
I added the following procedure to my general library...
procedure make_app_idle;
var regs : registers;
begin
regs.ax := $1680;
intr($2f,regs);
intr($2f,regs);
end;
then everywhere that my applications waited for a keystroke in a code such as...
ch := chr(0);
repeat
if keypressed
then begin
ch := readkey;
if ch = chr(0) then ch := chr(128 + ord(readkey));
end;
until (ch <> chr(0));
...I called the new routine as the else branch of the if keypressed statement. For example...
ch := chr(0);
repeat
if keypressed
then begin
ch := readkey;
if ch = chr(0) then ch := chr(128 + ord(readkey));
end
else make_app_idle;
until (ch <> chr(0));
...When I did this to my old applications, their consumption of resourses (cpu time as measured in task-manager's performance tab) went from 100% to 2% and their functioning speed (when multiple old applications were running simulataneously in multiple terminal server sessions) went back to what I am used to on dedicated desktop machines.
Other things that helped...
Because my dos applications pre-date windows with its on-screen clock and screen saver, my programs all implemented these features themselves. In the keystroke detection loop which I showed above, I would also write the current time to the screen and track how many seconds had passed since the last keystroke. Once that seconds counter reached 60, I would clear the screen and then wait in another keystroke loop, waiting for a key press, indicating I should redraw the screen. I removed these features as an early attempt to lower my resource consumption and although the server's cpu was still pegged to 100% after these changes, the applications did run noticably faster.
I also had some extra screen i/o hidden in my crt-based windowing library. (Dos applications often reuse portions of the 80x25 text screen by clearing rectangular regions, enclosing them with lines or colors, and rending new text within the "window"
. My library for doing this was actually writing out strings of spaces to clear the rectangular area, then writing the space-filled strings again, this time with line-draw characters embedded, and finally writing out the text contents of the "window". By eliminating the first set of strings (those which cleared the area), I got another perceivable performance boost.
A funny note to add here. That same crt-based windowing library used the turbo-pascal sound() command to generate a cute "twerp" sound as the window opened. Under terminal services, this sound got broken up and slowed down, making it sound like machine-gun farting. For reasons not involving performance or resource consumption, that code was also removed.
I know it seems a little unusual to be investing this kind of time on dos applications, but these are complex and fully mature systems which represent conservatively 100k man-hours of development and 100m cpu-hours usage in the field. Upgrading them to windows might be the right thing to do, but throwing away that kind of stability for the sake of a prettier user interface would be INSANE!
Hope there are other walking antiques out there who can benefit from this information.
Peace,
Colt.
NOTE : I am posting this same tip in the NT and WIN2K forums as it may be of interest to non-pascal application authors.
If it's stupid but it works, it isn't stupid
I added the following procedure to my general library...
procedure make_app_idle;
var regs : registers;
begin
regs.ax := $1680;
intr($2f,regs);
intr($2f,regs);
end;
then everywhere that my applications waited for a keystroke in a code such as...
ch := chr(0);
repeat
if keypressed
then begin
ch := readkey;
if ch = chr(0) then ch := chr(128 + ord(readkey));
end;
until (ch <> chr(0));
...I called the new routine as the else branch of the if keypressed statement. For example...
ch := chr(0);
repeat
if keypressed
then begin
ch := readkey;
if ch = chr(0) then ch := chr(128 + ord(readkey));
end
else make_app_idle;
until (ch <> chr(0));
...When I did this to my old applications, their consumption of resourses (cpu time as measured in task-manager's performance tab) went from 100% to 2% and their functioning speed (when multiple old applications were running simulataneously in multiple terminal server sessions) went back to what I am used to on dedicated desktop machines.
Other things that helped...
Because my dos applications pre-date windows with its on-screen clock and screen saver, my programs all implemented these features themselves. In the keystroke detection loop which I showed above, I would also write the current time to the screen and track how many seconds had passed since the last keystroke. Once that seconds counter reached 60, I would clear the screen and then wait in another keystroke loop, waiting for a key press, indicating I should redraw the screen. I removed these features as an early attempt to lower my resource consumption and although the server's cpu was still pegged to 100% after these changes, the applications did run noticably faster.
I also had some extra screen i/o hidden in my crt-based windowing library. (Dos applications often reuse portions of the 80x25 text screen by clearing rectangular regions, enclosing them with lines or colors, and rending new text within the "window"
A funny note to add here. That same crt-based windowing library used the turbo-pascal sound() command to generate a cute "twerp" sound as the window opened. Under terminal services, this sound got broken up and slowed down, making it sound like machine-gun farting. For reasons not involving performance or resource consumption, that code was also removed.
I know it seems a little unusual to be investing this kind of time on dos applications, but these are complex and fully mature systems which represent conservatively 100k man-hours of development and 100m cpu-hours usage in the field. Upgrading them to windows might be the right thing to do, but throwing away that kind of stability for the sake of a prettier user interface would be INSANE!
Hope there are other walking antiques out there who can benefit from this information.
Peace,
Colt.
NOTE : I am posting this same tip in the NT and WIN2K forums as it may be of interest to non-pascal application authors.
If it's stupid but it works, it isn't stupid