TweetFollow Us on Twitter

Asynchronous IO
Volume Number:12
Issue Number:12
Column Tag:Toolbox Techniques

Building Better Applications
Via Asynchronous I/O

By Richard Clark, General Magic, Inc.

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Have you ever looked at an application and wondered how to make it faster? Sure, you can select better algorithms or rewrite sections in assembly language, but sometimes a fast processor or great algorithm is not enough. Many applications reach a limit where they can process the information faster than they can get it. These applications are said to be I/O bound. Improving such programs is straightforward, once you know something more about how the Macintosh reads and writes information.

Most developers go through several basic stages in getting information in and out of their programs. In the first stage, they use their programming language’s built-in I/O commands - printf and scanf for C, WRITELN and READLN for Pascal. Soon, driven by the derision of their peers, a desire to manipulate something other than text streams, or a feeling they should be using the underlying operating system directly, they will shift over to the Macintosh FSWrite and FSRead routines.

Quite a few Macintosh programmers spend the remainder of their careers using FSRead and FSWrite. Some use FSRead’s “newline mode” to emulate scanf or READLN. Others read their data in as needed, whether they need a single character or an entire structure. The wisest users of FSRead use buffering - they read the data in large blocks and process the information in memory.

All of these techniques have one property in common - they all use “synchronous I/O.” A synchronous I/O operation makes the calling program wait until the operation has been completed. Programmers who want to get the best possible performance out of their applications can eliminate this wait by switching to “asynchronous I/O” which asks the OS to transfer information at the same time the other code is running. There is another reason why advanced Macintosh programmers use asynchronous I/O - it’s the only way to get at some of the more advanced communications features such as TCP/IP and to get real-time information from users.

A Programmer’s Look at I/O

We will take a look at the uses of synchronous and asynchronous I/O through a function that counts occurrences of the letter “A” in a text file. The simplest version of this program uses the C Standard I/O Library functions.

int countChars(FSSpecPtr fsp)
{
  // Count the number of times the letter A appears in the file
 FILE *f = NULL;
 int  counter = 0;
 char currChar;
 char filename[64];
 
  // Homemade PtoCstr operation which makes a copy of the string
 BlockMove((Ptr)&fsp->name[1], filename, fsp->name[0]);
 filename[fsp->name[0]] = ‘\0’;
 
  // Count the characters
 f = fopen(filename, “r”);
 while ((currChar = fgetc(f)) != EOF) {
 if (currChar == ‘A’) counter += 1;
 }
 fclose(f);
 return counter;
}

While this looks like a simple program, quite a bit is going on behind the scenes. fgetc() does not simply read each character from the disk as the program requests it, but uses a buffering scheme instead. When buffering, the application (or library) reads a block of information into memory all at once, then returns each item from that block of memory. Without buffering, each read would have to position the disk’s read/write head to the proper location on the disk, then wait for the correct area of the disk to rotate into place. Thus the program would spend most of its time waiting for the drive hardware itself.

Even with buffering, most Standard I/O library implementations are not as fast as going directly to the machine’s own file system. The extra bookkeeping associated with tracking an arbitrary number of files slows things down. We can write a faster program using the “high level” File Manager calls. When we build our new program, we will buffer the data by reading it into memory in large blocks, then process the information directly in memory. The algorithm for our buffered program is as follows.

• Allocate a fixed-size buffer (for best results, the size should be an even multiple of 1K so the Macintosh OS can read entire blocks off the disk)

• Repeat:

• Read one buffer’s worth of data

• Process data

• Until the entire file has been read (charCount == 0 after FSRead)

• Release the memory used by the buffer

And here is the source code:

int countCharsFS(FSSpecPtr fsp)
{
  // Count the number of times the letter A appears in the file reading the file in blocks
 int counter = 0;
 char *buffer, *currChar;
 short refNum;
 long charCount;
 OSErr err;
 
 err = FSpOpenDF(fsp, fsRdPerm, &refNum); 
 if (err == noErr) {
 buffer = (char*)NewPtr(kBufferSize);
 if (buffer != nil) {
  for (;;) {
  charCount = kBufferSize;
  err = FSRead(refNum, &charCount, (Ptr)buffer);
  if ((err != noErr) && (err != eofErr)) break;
  if (charCount == 0) break;
  currChar= buffer;
   while (charCount- > 0) {
   if (*currChar++ == ‘A’) counter++;
   }
  }
   DisposePtr(buffer);
 }
 FSClose(refNum);
 }
 return counter;
}

In the most extreme case, our program could read the whole file in at once before processing it. This would reduce the number of seek operations to an absolute minimum, at the cost of allocating a huge block of memory. This is not always faster than reading a few blocks at a time.

Let’s compare some real-world timing figures for these three routines. We ran these tests on a variety of Macintosh systems, including 680x0 and PowerPC models. The system disk cache was set to 32K for all tests. This article includes only the results from a PowerMac 7100/66, but the other systems were similar. If you want to see the values for your own machine, the test application’s sources are available from MacTech NOW.

file size countchars countCharsFS

1000K 1003 ms 218 ms

2000K 2754 ms 661 ms

3000K 4031 ms 1076 ms

4000K 5328 ms 1467 ms

5000K 6608 ms 1885 ms

Shown graphically, the advantage of going directly to the file system becomes even more apparent:

Improving the Program with Asynchronous I/O

In all of these routines the “count characters” code has to wait for the data to arrive from the disk before starting processing. We can make the code even faster by reading in the next buffer at the same time we are processing the current buffer’s contents. Reading in some data while performing other work is known as asynchronous I/O.

Asynchronous I/O works on the basis of scheduling I/O operations. Instead of calling FSRead and waiting until the buffer has been filled, we will pass the system a request to fill a buffer and instructions on how to notify us when the request has been completed. The Macintosh OS puts our request into a list (known as a Queue) and fills the requests in the order they were made.

Here’s how to structure our program using asynchronous I/O:

• Allocate two buffers in memory.

• Tell the Macintosh OS we want the first block of data to go into the first buffer. (This schedules the buffer for filling as soon as possible and returns control immediately.)

• Tell the OS we want another block of data to go into the second buffer.

• Repeat:

• Wait until a full buffer is available,

• Process it, and

• Make another request for data using this buffer as the destination until the entire file has been processed.

• Release the memory used by the buffers

Notice that our program may have to wait for a buffer to finish filling, but it also gets to work for part of that time. Since we used to do nothing while waiting for the read to complete, any work we do while waiting now happens “for free.”

Changing Your I/O Model

Taking advantage of asynchronous I/O requires that you break away from the “high level” calls we used in the previous code samples. Fortunately, the operating system provides PBRead and PBWrite as the more flexible “low level” counterparts to FSRead and FSWrite.

The PB calls don’t take their parameters in the stack like the FS calls. Instead, each PB call takes a pointer to a “parameter block” structure containing all of the required information. You can easily translate an FS call into a PB call by allocating a parameter block and filling in the appropriate fields. In fact, the Macintosh OS basically does this every time you use an FS call.

Converting from FSRead to PBRead

err = FSRead(refNum, &charCount, (Ptr)buffer);

// Create a parameter block. We’ll use “clear” to zero fields we don’t need for this example
pb = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
pb->ioParam.ioRefNum = refNum;
pb->ioParam.ioVRefNum = vRefNum;
// Note: Somebody has to supply the volume RefNum
pb->ioParam.ioBuffer = (Ptr)buffer;
pb->ioParam.ioReqCount = charCount;
err = PBReadSync(pb);
charCount = pb->ioParam.ioActCount;
DisposePtr((Ptr)pb);

So far it looks like a lot of extra work to use a PBRead call instead of an FSRead call. That is true for basic synchronous I/O, but the PB calls can do more. One of the better aspects of PBRead and PBWrite is the ability to set the positioning mode and offset each time. If you make a simple FSWrite call, you’ll get the information located at the “mark” - a value which indicates the current position in the file. The PB calls allow you begin reading or writing from the file mark, at an offset relative to the file mark, or at an offset relative to the start of the file. In addition, PBWrite can perform a “read-verify” operation after writing data to confirm that it went out correctly.

Our real reason for introducing the PB calls in this article is to use them for asynchronous I/O. We want to place a request with the system to get some data and learn when that request has been fulfilled. The parameter blocks have just the information we need to make this happen: the ioResult and ioCompletion fields.

The ioResult field gives the result code of the operation - either 0 for “no error” or a negative value designating an operating system error. This field is filled in after the data has been transferred, which gives us one way to learn when the OS is finished. The File Manager places a positive value into this field when the request is posted. When the value changes, we know the transfer is finished and we can use the data. Hopefully to a 0, meaning “no error”, but it might also contain a negative error code.

Using what we’ve learned so far, we can improve on all of our FSRead-based routines. The code can run as fast as the “read the whole file into memory” version, but only use two small blocks of memory as buffers. Notice how we use a two entry table to hold a pair of parameter blocks. This allows us to fill one block while processing the other.

int countCharsAsync(FSSpecPtr fsp)
{
  // Count the number of times the letter A appears in the file, reading the file one 
  // character at a time
 int   counter = 0;
 ParmBlkPtr pb[2], currPBPtr;
 int   currPB = 0;
 char  *buffer, *currChar;
 short  refNum;
 long  charCount;
 OSErr  err;
 
  // Allocate parameter blocks
  // Open the file
 err = FSpOpenDF(fsp, fsRdPerm, &refNum); 
 if (err == noErr) {
  // Set up parameter blocks
 pb[0] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
 pb[1] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
 setup(pb[0], refNum, fsp->vRefNum, kBufferSize);
 setup(pb[1], refNum, fsp->vRefNum, kBufferSize);
 
  // Start 2 read operations going
 (void) PBReadAsync(pb[0]); 
 (void) PBReadAsync(pb[1]); 
 currPBPtr = pb[0];
 
 for (;;) {
    // Wait for the I/O operation to complete
  while (currPBPtr->ioParam.ioResult > 0) {};
  
    // The data is ready, so count the characters
  buffer = currPBPtr->ioParam.ioBuffer;
  charCount = currPBPtr->ioParam.ioActCount;
  if (charCount == 0) break;
  currChar= buffer;
  while (charCount- > 0) {
   if (*currChar++ == ‘A’) counter++;
  }
  
    // Put this buffer back into the reading queue
  (void) PBReadAsync(currPBPtr);
  
    // Switch to the other buffer
  currPB = 1 - currPB;
  currPBPtr = pb[currPB];
  currPBPtr->ioParam.ioPosMode = fsAtMark;
 }
    // Release the memory
  destroy(pb[0]);
  destroy(pb[1]);
  FSClose(refNum);
 }
 return counter;
}

void setup (ParmBlkPtr pb, short refNum,
   short vRefNum, long bufSize)
{
 pb->ioParam.ioCompletion = NULL;
 pb->ioParam.ioResult = 1;
 pb->ioParam.ioRefNum = refNum;
 pb->ioParam.ioVRefNum = vRefNum;
 pb->ioParam.ioReqCount = bufSize;
 pb->ioParam.ioBuffer = NewPtr(bufSize);
 pb->ioParam.ioPosMode = fsAtMark;
 pb->ioParam.ioPosOffset = 0;
}
void destroy (ParmBlkPtr pb)
{
  DisposePtr(pb->ioParam.ioBuffer);
  DisposePtr((Ptr)pb);
}

Let’s look at the timing results for the above code. Again, since the PowerPC and 68K numbers follow the same pattern, we will show only the PowerPC numbers here.

file size countCharsFS PBRead async

1000K 218 ms 167 ms

2000K 661 ms 454 ms

3000K 1076 ms 700 ms

4000K 1467 ms 934 ms

5000K 1885 ms 1183 ms

Graphically, the timing looks like this: (Notice that we’ve changed scale from our previous graph so you can get a better look at the difference between synchronous and asynchronous I/O.)

While not a dramatic change, the last result is still an improvement over synchronous I/O. It’s hard to make this code much faster, but you can take advantage of the available processing time to add features and improve the user’s experience.

Improving the User’s Experience

All of these routines share a common drawback - they don’t allow any time for other programs to run. They sit in tight loops reading and processing information or waiting for the next read to complete. This is OK when writing demonstration code for a magazine article, but it isn’t a reasonable practice in “real” programs. Real applications should arrange for their I/O intensive routines to give time to other applications and to allow the user to cancel at any time.

A well-designed application will give away time while it’s simply waiting for an I/O request to finish. Most applications could do this by calling WaitNextEvent, processing the event, then checking the result code of the pending operation before giving away any more time. The only problem is that when an application gives away time with WaitNextEvent, there’s no telling how soon control will be returned. Applications that need immediate notification at the end of an I/O operation must use completion routines.

A completion routine is a function in the program that the Macintosh OS calls when a specific I/O operation ends. (The requesting program supplies the function pointer in the ioCompletion field of the parameter block.) Completion routines run under the same tight restrictions as any other “interrupt time” code including not being able to allocate or move memory nor being able to use the content of any relocatable block. Most completion routines are not guaranteed access to their application’s globals, and the information passed into each completion routine varies wildly. For these reasons, we will defer a thorough discussion of completion routines to another article.

The completion routine for PBRead is especially poor, as it receives no parameters and the Parameter Block has been pulled off of the I/O queue by the time the routine is called. This routine appears to have A5 set up for it so it can reach the application’s globals, but it can’t do much even then - only set a flag indicating the completion of I/O or take an existing block and issuing another I/O call for it.

Besides giving away time, there is one other thing a well-behaved application should do, and that is allow the user to cancel an operation. If the user asks to cancel during a synchronous I/O operation, the application simply completes that operation and doesn’t begin another. However, if the user cancels during asynchronous I/O, the application has to remove all of the pending requests. The KillIO() call takes a file or driver reference number and removes all of its pending I/O requests, so applications can kill the pending requests then wait for the current operation to complete before closing the file or driver.

Conclusion

Developers need to look beyond basic I/O calls if they want to get maximum performance from their programs. Asynchronous I/O, while the most complicated way to read and write information, is one of the best ways to improve your application’s performance. These same techniques that improve the performance of File I/O become critical when dealing in near real-time applications such as TCP/IP networking or serial communications that cannot afford pauses in their data collection.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Dropbox 193.4.5594 - Cloud backup and sy...
Dropbox is a file hosting service that provides cloud storage, file synchronization, personal cloud, and client software. It is a modern workspace that allows you to get to all of your files, manage... Read more
Google Chrome 122.0.6261.57 - Modern and...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
Skype 8.113.0.210 - Voice-over-internet...
Skype is a telecommunications app that provides HD video calls, instant messaging, calling to any phone number or landline, and Skype for Business for productive cooperation on the projects. This... Read more
Tor Browser 13.0.10 - Anonymize Web brow...
Using Tor Browser you can protect yourself against tracking, surveillance, and censorship. Tor was originally designed, implemented, and deployed as a third-generation onion-routing project of the U.... Read more
Deeper 3.0.4 - Enable hidden features in...
Deeper is a personalization utility for macOS which allows you to enable and disable the hidden functions of the Finder, Dock, QuickTime, Safari, iTunes, login window, Spotlight, and many of Apple's... Read more
OnyX 4.5.5 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more

Latest Forum Discussions

See All

Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »
TouchArcade Game of the Week: ‘Vroomies’
So here’s a thing: Vroomies from developer Alex Taber aka Unordered Games is the Game of the Week! Except… Vroomies came out an entire month ago. It wasn’t on my radar until this week, which is why I included it in our weekly new games round-up, but... | Read more »
SwitchArcade Round-Up: ‘MLB The Show 24’...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for March 15th, 2024. We’re closing out the week with a bunch of new games, with Sony’s baseball franchise MLB The Show up to bat yet again. There are several other interesting games to... | Read more »
Steam Deck Weekly: WWE 2K24 and Summerho...
Welcome to this week’s edition of the Steam Deck Weekly. The busy season has begun with games we’ve been looking forward to playing including Dragon’s Dogma 2, Horizon Forbidden West Complete Edition, and also console exclusives like Rise of the... | Read more »
Steam Spring Sale 2024 – The 10 Best Ste...
The Steam Spring Sale 2024 began last night, and while it isn’t as big of a deal as say the Steam Winter Sale, you may as well take advantage of it to save money on some games you were planning to buy. I obviously recommend checking out your own... | Read more »
New ‘SaGa Emerald Beyond’ Gameplay Showc...
Last month, Square Enix posted a Let’s Play video featuring SaGa Localization Director Neil Broadley who showcased the worlds, companions, and more from the upcoming and highly-anticipated RPG SaGa Emerald Beyond. | Read more »
Choose Your Side in the Latest ‘Marvel S...
Last month, Marvel Snap (Free) held its very first “imbalance" event in honor of Valentine’s Day. For a limited time, certain well-known couples were given special boosts when conditions were right. It must have gone over well, because we’ve got a... | Read more »
Warframe welcomes the arrival of a new s...
As a Warframe player one of the best things about it launching on iOS, despite it being arguably the best way to play the game if you have a controller, is that I can now be paid to talk about it. To whit, we are gearing up to receive the first... | Read more »
Apple Arcade Weekly Round-Up: Updates an...
Following the new releases earlier in the month and April 2024’s games being revealed by Apple, this week has seen some notable game updates and events go live for Apple Arcade. What The Golf? has an April Fool’s Day celebration event going live “... | Read more »

Price Scanner via MacPrices.net

Apple Education is offering $100 discounts on...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take $100 off the price of a new M3 MacBook Air.... Read more
Apple Watch Ultra 2 with Blood Oxygen feature...
Best Buy is offering Apple Watch Ultra 2 models for $50 off MSRP on their online store this week. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
New promo at Sams Club: Apple HomePods for $2...
Sams Club has Apple HomePods on sale for $259 through March 31, 2024. Their price is $40 off Apple’s MSRP, and both Space Gray and White colors are available. Sale price is for online orders only, in... Read more
Get Apple’s 2nd generation Apple Pencil for $...
Apple’s Pencil (2nd generation) works with the 12″ iPad Pro (3rd, 4th, 5th, and 6th generation), 11″ iPad Pro (1st, 2nd, 3rd, and 4th generation), iPad Air (4th and 5th generation), and iPad mini (... Read more
10th generation Apple iPads on sale for $100...
Best Buy has Apple’s 10th-generation WiFi iPads back on sale for $100 off MSRP on their online store, starting at only $349. With the discount, Best Buy’s prices are the lowest currently available... Read more
iPad Airs on sale again starting at $449 on B...
Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices again for $150 off Apple’s MSRP, starting at $449. Sale prices for online orders only, in-store price may vary. Order online, and choose... Read more
Best Buy is blowing out clearance 13-inch M1...
Best Buy is blowing out clearance Apple 13″ M1 MacBook Airs this weekend for only $649.99, or $350 off Apple’s original MSRP. Sale prices for online orders only, in-store prices may vary. Order... Read more
Low price alert! You can now get a 13-inch M1...
Walmart has, for the first time, begun offering new Apple MacBooks for sale on their online store, albeit clearance previous-generation models. They now have the 13″ M1 MacBook Air (8GB RAM, 256GB... Read more
Best Apple MacBook deal this weekend: Get the...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New 15-inch M3 MacBook Air (Midnight) on sale...
Amazon has the new 15″ M3 MacBook Air (8GB RAM/256GB SSD/Midnight) in stock and on sale today for $1249.99 including free shipping. Their price is $50 off MSRP, and it’s the lowest price currently... Read more

Jobs Board

Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Relationship Banker *Apple* Valley Main - W...
…Alcohol Policy to learn more. **Company:** WELLS FARGO BANK **Req Number:** R-350696 **Updated:** Mon Mar 11 00:00:00 UTC 2024 **Location:** APPLE VALLEY,California Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill WellSpan Medical Group, York, PA | Nursing | Nursing Support | FTE: 1 | Regular | Tracking Code: 200555 Apply Now Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.