SORT STRING GENERATION IN A STAGED STORAGE SYSTEM

Abstract: A method for utilising a cache in a staged storage system for sort string generation while minimising the number of reference misses per record. During a first pass, a partially nested ordering or sort is effectuated on subsets of the records, and then during a second pass the subsets of records are merged such that the subsets are dynamically constrained to fit within the cache.

Description: This invention relates to sort string generation in a staged storage processing system. Lorin, "Sorting and Sort Systems", Addison-Wesley Publishing Co., copyright 1975, at page 1, defines sorting as a process of arranging items in order. He further points out that while ordering can involve physical ordering, such as the arrangement of a deck of punched cards or records on a magnetic tape, the output of a sorting operation does not necessarily involve an actual or physical rearrangement. In this regard, order in a file may be represented in other ways, particularly by use of an index. The rearrangement of an index or its equivalent is termed "logical ordering" or "rearrangement". Thus, where the elements to be sorted are contained in a linked list, then altering the sorting order consists of altering the pointers, which pointers define the succession of elements in the list. Modern data processing machines generally comprise an instruction processor coupled to a hierarchically organised and least recently used (LRU) managed staged storage system containing software and data. The fastest, most rapidly accessed memory closest to the instruction processor is placed at the top of the hierarchy, while progressively slower forms of memory having the bulk of the information written thereon occupies the lower positions within the hierarchy. Because memory costs increase dramatically with speed, many computer systems divide the physical memory subsystem into a number of performance levels. Some of these levels, such as direct access storage devices (DASD) (eg disks) and tape, have traditionally been treated as I/O devices while other levels, such as RAM and cache, have been treated directly by system hardware as main memory. The term "primary storage" or "internal storage" specifies system memory that can be randomly addressed for single read or write transfers. "Secondary" or "external storage" refers to storage that is not randomly addressable and is too slow for direct access or must be accessed in fixed-size blocks. A cache is a memory with an access time considerably faster than the access times of other forms of randomly accessed memory constituting primary or internal store. Because referencing these forms of memory is managed by the system, the existence of a cache is transparent to the application software. Data is brought into the cache usually in lines that contain the reference data, and a line resides in the cache until it is overlaid with another line. The movement of data in and out of the cache is commonly supported by a hardware implementation of a least recently used (LRU) algorithm. The cache operates on the principle that certain memory locations tend to be accessed often. When a main memory location is read, its contents are stored in the cache at the same time. Further, read references to this location are automatically routed to the cache. A write access usually writes to both main memory and the cache. Since a cache may represent many noncontiguous main memory locations, content-addressable registers are used to determine when a main memory location is currently duplicated in the cache. Traditionally, sort methods are classified into internal methods and external methods. An internal method is one that can be applied with acceptable performance only to those lists of data that can be completely contained within the primary storage of the processor. An external method is one that reasonably applies to files of data that are too large to fit into the primary store and must therefore rely on external bulk storage devices, such as tape or DASD, during the sorting process. In the process of external sorting, parts of a file are read into primary storage, ordered internally, and then rewritten on the external devices. This process may occur a number of times. In contrast, internal methods are used to rearrange the data developed from pass to pass. Restated, most external sorting methods make a first pass through the file to be sorted, breaking it up into blocks about the size of internal memory, and then sort these blocks. A merge is then performed upon the sorted blocks together by making several passes through the file and creating successively larger sorted blocks until the whole file is sorted. For example, if there is an unordered list of n keys and an internal memory capacity m<<n words, then each sort pass produces n/m sorted blocks. If a p-way merge is performed on each subsequent pass, then log(base key)(n/m) passes may be required. In Knuth's book, "Art of Computer Programming", Vol. 3, Sorting and Searching, copyright 1973, pp. 251-266, replacement selection sorting employing p-way merging is described. It is also recognised that other references such as Whitlow et al, USP 4,210,961, "Sorting System", issued July 1, 1980, address the minimisation of computing resources attendant to an n-way (or p-way using Knuth' terminology) merging of ordered strings in a sort/merge operation. Whitlow selectively reverses, prior to the merge, the direction of ordering of some of the strings as a function of a comparison match between actual and predicted string size. The state of the art is also illustrated by Chang et al, USP 4,417,321, issued November 22, 1983, and USP 4,510,567, issued April 9, 1985, both entitled "Qualifying and Sorting File Record Data". Also of interest is Sedgewick, "Algorithms", Addison-Wesley Publishing Co., Chapter 11, Priority Queues, copyright 1983. Sedgewick proposes an abstract data type in which, for each new element inserted into a queue, the largest magnitude element is deleted therefrom. Relatedly, the Chang patents implement a variation of Sedgewick's priority queue as applied to sorting and staging from a mass store to an office system. Chang's sorting method is biased in favour of excluding the lowest ranked element from the list or buffer. It is accordingly an object of this invention to devise an effective sort string generating method which minimises the resources involved in sorting and the number of misses upon referencing in the cache. The aforementioned objects are achieved by a method and an apparatus as claimed. The above-cited references, singly or in combination, neither teach nor suggest the invention as claimed; namely, a multiple pass sort and merge in which the sort and merge passes are dynamically constrained over a cache. This means that during the first pass the number of elements within each unordered subset filling the cache, as well as the number of elements involved in each merge cycle, fit within the cache during their respective passes. Replacement selection is the merge method of choice. Other merge techniques may be used although at some sacrifice in the time necessary to complete operation. In all cases, however, the number of misses per record approximates no more than one miss during sorting. The present invention will be further described hereinafter. Reference will be made to the accompanying drawings which illustrate general aspects of staged storage sorting. Figure 1 is a schematic diagram of a staged storage system including a cache. Figure 2 is a diagrammatic representation of the hit and miss effects upon an LRU-managed cache on being subjected to a reference string. Referring now to Figure 1, there is shown a typical arrangement of memories in a hierarchy available in most systems. When a memory access is made by way of execution of an instruction in the processor, the contents of the accessed location, as previously mentioned, plus its neighbours are copied into a cache. If another reference is made to these locations, it can be fetched directly from the cache without having to go to the slower speed main memory. The environment illustrated by Figure 1 is that of demand paging information from the lower depths or echelons of memory so that it is resident in more accessible portions thereof as some function of demand and dereferencing or migration downward of those portions of information that are not of present interest to executing tasks. Superimposed upon this virtual memory demand paging model is a multiprogramming or contention model wherein tasks are suddenly switched by way of interrupt so that flushing and reconstitution of an appropriate working set must be built up until the current task is switched out and the associated working set purged. Referring now to Figure 2, there is shown an example of least recently used (LRU) managed cache. The LRU-managed cache associates with each reference the time of its last use. When an element must be replaced, the LRU scheme chooses that element in the cache which has not been used for the longest period of time. This form of replacement looks backward in time rather than forward. The result of applying LRU to a cache reference string is shown in Figure 2. This produces 12 misses. When the reference to element 4 occurs, however, the LRU sees that, of the three elements in the cache, element 2 was used least recently. The most recently used was element 0 and just before that, element 3 was used. Thus, LRU replaces element 2, not knowing that it is about to be used. When it then misses by way of a reference to element 3, the LRU replaces element 0 since, of the three elements in the cache, element 0 was the least recently used. The present invention is directed to a method for internal multiple pass sorting of records in a staged storage system comprising a cache. The cache in fact forms a repository for the keys of the records currently being sorted. This method has broadly two steps. These are (a) sorting subsets of the records until the set (string) is exhausted; and (b) merging of the subsets such that the sub-sets are dynamically constrained to fit within the cache. Step (b) can be performed by replacement selection sorting of r levels of r-layered subsets of records such that the subsets are dynamically constrained to fit within the cache. There occurs no more than one cache referencing miss per record during the sorting within a level. In practice, a cache is subject to contention such that control of it may be switched from a sorting task to another task or vice versa by some external scheduler resident within the processor. When the sort task has control, the cache becomes loaded with a portion of the string to be ordered as the portion is sorted. In the event the sorting task becomes interrupted, another task may take control and flush the task of the partially sorted results. While the cache is being flushed, only those entries which are modified are copied back to memory. When the sort task regains control, it resumes where it left off, note being taken that the partially sorted results were previously stored. In practice, the cache is always LRU managed, whether involved in a sort task or not. Because the cache is LRU managed, the pattern of reference determines the cache contents. If the set of referenced objects is much larger than the cache capacity, then the cache contents are partially replaced when referencing objects. In order, therefore to achieve low miss rates in LRU-managed storage the locality of referencing is important. Also, even though such locality might not be observed in sorting operations, the miss rate can still be minimised if the subset of objects to be sorted was kept well within the capacity of cache storage. Thus it is desirable to keep the substring being sorted within the cache small enough so that the cache LRU discipline is NOT invoked. In determining how small to make the substring size, the number the lines in the cache and the depth of the substring must be taken into account. Initially therefore, when performing the method, parameters such as the size s of each of the subsets to be sorted during a first pass need to be computed. If a merge phase involves several merge subpasses, such as an n-way and t-way merge, then the parameters n and t must also be computed. Note that n and t also correspond to the number of nodes on a selection tree. Also, as will be apparent to the skilled person it may be necessary to reformat the records in a conventional manner to identify those key elements of the records which will be used to form the keys for the sort. These parameters can be computed in any suitable manner as will be apparent to the skilled person. In order to assist the conceptual appreciation of this invention, however, the computation of the parameters is described in general terms below. Initially a size t for an output tree to be used to merge ordered lists of records can be computed. T is chosen to be as large as possible while being limited such that the tree merge operation will still have a low cache-miss ratio. T is therefore a function of the size of the cache, the characteristics of the tree merge operation (i.e. how much data is referenced in this tree merge operation), and the size of the records and the keys being sorted. A subset size s for subsets of unordered records to be sorted to create initial ordered lists (or subsets) for tree merge operations can also be computed. S is chosen to be as large as possible while being limited such that the sort operation will still have a low cache-miss ratio. S is therefore a function of the size of the cache, the characteristics of the sort operation that create the initial ordered lists (i.e. how much data is referenced in this sort operation), and the size of the records and the keys being sorted. The number n of ordered lists of size s that need to be merged to create an ordered list for the output tree can then be computed. N is chosen so that the sort operation will have a low cache-miss ratio. For a given t and s, n is a function of the main storage available to the program and the size of the records. More precisely, for a given t and s, n is a function of how many records may be in storage at one time. n is chosen to be the minimum of the number of records that may be in storage at one time divided by the product of t times s, and t (the maximum number of ordered lists that may be merged together using a tree merge operation while having a low cache-miss ratio). Once the parameters t, s and n are known, a first subset of s input record keys are read in cache and are sorted to form a sorted list. This is repeated for successive subsets of s input record keys until the set of records is exhausted. These operations therefore form the step (a) mentioned above. In general terms, the second or merging step (b) then comprises the substeps 1 to 4 below. Sub-Step 1 : Select an intermediate set of n of the ordered lists produced in step (a). If n is one, then add up to t ordered lists produced in step (a) to the output tree, and go to substep 4. Sub-Step 2 : Remove t or fewer lists from the intermediate set, merge them, and add the resulting ordered list to the intermediate set. Repeat this substep until the intermediate set is reduced to a single ordered list. Step 3 : Add the single intermediate set list to the output tree. If n or more lists remain from step (a), go to substep 1. Step 4 : Merge records from the lists on the output tree until the work file buffers are filled. Write the work file buffers. If there are more records yet to be processed, go to step (a). Thus t is the size of an output tree to be used to merge ordered lists of records. The subset size s represents an upward bound on the depth or cardinality of the subset that can fit within the cache for subsets of unordered records to be sorted to create the initial ordered lists or tree merge operations. N is the number of sorted subsets of size s that need to be merged to create an ordered list for the output tree is determined. Thus the number n is a function of the available main storage and would vary for any given t and s. Step (a) involves the obtaining of s input records and sorting them. In practice this step would be repeated until main storage is full, or the set of records is exhausted. As indicated, substeps 1-4 form a multiple subpass merging step. In this regard, substep 1 involves selecting an intermediate set of n of the ordered lists produced in step 4. If n is 1, then merely take t ordered lists, produced in step (a), and transfer them to the output tree and jump to substep 4. Sub-step 4 includes the merging of records from the lists on the output tree until the work file buffers are filled. When these buffers are filled, they are then written. If there are more records yet to be processed, a return must be made to step (a). If indeed n is 2 or more, then a branch is made to substep 2. At this point, t or fewer lists from the intermediate set are removed and merged with the resulting ordered list being added to the intermediate set. This step is repeated until the intermediate set is reduced to a single ordered list. Thus in both the first and the second passes (including the subpasses of the second pass) the subsets are constrained to fit within the cache. Two sorting examples using a cache in a staged storage data processing system will be illustrated. Both examples will sort 12 records in memory. The first example illustrates sorting according to the prior art, while the second example exercises the method of the invention. It will be assumed that only four records at a time can be kept within the cache for purposes of these examples. Instead of replacement selection, the methods shown for sorting will be simply that of traversing a list and selecting the smallest element in the list. The same principles to illustrate the effects of cache misses would apply likewise to replacement selection. With reference to the examples, the records are assumed to be linked together in logical order using pointers; the sorting method of the invention can therefore be implemented by rewriting the pointers.

Claims: A method for internal multiple passing sorting of records in a staged storage data processing system in which a cache is a repository for the keys of the records currently being sorted, said method comprising the steps of: during a first pass: (a) calling into cache and sorting an unordered subset of the keys of the records to form an ordered subset of keys; (b) step (a) being repeated for successive subsets until the set of records has become exhausted; and during a second pass: (c) merging a multiple number t of ordered subsets, wherein the number s in each subset is such that the product n of t times s is a function of the number of records that may be in storage at any one time and the number s can fit within the cache, until an ordered output is obtained. A method according to claim 1 wherein the step of merging the multiple ordered subsets is performed using replacement selection sorting of the keys. A method according to any one of the preceding claims, wherein the cache is LRU managed. A method according to any one of the preceding claims wherein said method encompasses a preliminary step of reformatting key elements of the records to identify the keys used during the sort so as to facilitate comparison between keys. A staged storage data processing system for internal multiple passing sorting of records, in which a cache is a repository for the keys of the records currently being sorted, said system comprising means for: during a first pass: (a) calling into cache and sorting an unordered subset of the keys of the records to form an ordered subset of keys; (b) repeating (a) for successive subsets until the set of records has become exhausted; and during a second pass: (c) merging a multiple number t of ordered subsets, wherein the number s in each subset is such that the product n of t times s is a function of the number of records that may be in storage at any one time and the number s can fit within the cache, until an ordered output is obtained. A system according to claim 5, further comprising means for merging the multiple ordered subsets using replacement selection sorting of the keys. A system according to claim 5 or claim 6 wherein the cache is LRU managed. A system according to any one of claims 5 to 7, and further comprising means for the preliminary reformatting of key elements of the records to identify the keys used during the sort so as to facilitate comparison between keys.

Inventors: CASE, DOUGLAS ROBERT, CONNER, WATSON MANWARING
Applicants: INTERNATIONAL BUSINESS MACHINES CORPORATION
Application year: 1986
Main classification: G06F7
IPC codes: G06F7