+ All Categories
Home > Documents > Method for increasing performance of select and poll applications ...

Method for increasing performance of select and poll applications ...

Date post: 19-Dec-2016
Category:
Upload: vuquynh
View: 215 times
Download: 0 times
Share this document with a friend
58
US 20040031044A1 (19) United States (12) Patent Application Publication (10) Pub. No.: US 2004/0031044 A1 Jones (43) Pub. Date: Feb. 12, 2004 (54) (76) (21) (22) (51) (52) METHOD FOR INCREASING PERFORMANCE OF SELECT AND POLL APPLICATIONS WITHOUT RECOMPILATION Inventor: Richard A. Jones, San Jose, CA (US) Correspondence Address: HEWLETT-PACKARD COMPANY Intellectual Property Administration P.O. Box 272400 Fort Collins, CO 80527-2400 (US) Appl. No.: 10/215,995 Filed: Aug. 8, 2002 Publication Classi?cation Int. Cl.7 .............................. .. G06F 9/46; G06F 9/00 US. Cl. .......................................... .. 719/330; 719/331 300 (57) ABSTRACT A method for increasing performance of selected poll appli cations Without recompilation. In one embodiment, the method is comprised of assigning the name of a library as a value for an environment variable in a softWare application. The library is comprised of softWare code for emulating a system call. The method is further comprised of launching the softWare application. The launching causes invocation of a loader associated With the softWare application. The method is further comprised of preloading the library into the softWare application. The preloading performed by the loader. The method is further comprised of redirecting the softWare application to invoke an alternative mechanism When the softWare application requests a system call. The invocation of the alternative mechanism implements the library as the system call. The method is further comprised of registering a plurality of ?le descriptors and events of interest With the alternative mechanism When the application makes said system call With a list of ?le descriptors identi fying sockets for Which the application has interest in events. Each of said plurality said ?le descriptors is related to an event of interest pertaining to an associated socket of interest. Those sockets With registered ?le descriptors are examined by the softWare application. 1% Assigning the name ofa library as a value for an environment variable in a software application. The library contains code for replacing system calls in a soltware application. 1 Launching the software application, which causes invocation ofa loader associated with said software application. I l’reloading the environment variable into the software application. the preloading performed by the loader. Registering a plurality of tile descriptors and events ofinterest with the alternative mechanism when the application makes said system call with a list of?le descriptors identifying sockets for which the application has interest in events. Each of said plurality said ?le descriptors is related to an event ofintercst pertaining lo an associated 7 socket ol‘interest. Redirecting the sotlware application to invoke the alternative mechanism when the so?ware application requests a system call, and wherein the invocation ofthe alternative mechanism implements the library as the system call. l Accessing those sockets having ?le descriptors registered with the allemative mechanism for which events of interest are indicated. m
Transcript
Page 1: Method for increasing performance of select and poll applications ...

US 20040031044A1

(19) United States (12) Patent Application Publication (10) Pub. No.: US 2004/0031044 A1

Jones (43) Pub. Date: Feb. 12, 2004

(54)

(76)

(21)

(22)

(51)

(52)

METHOD FOR INCREASING PERFORMANCE OF SELECT AND POLL APPLICATIONS WITHOUT RECOMPILATION

Inventor: Richard A. Jones, San Jose, CA (US)

Correspondence Address: HEWLETT-PACKARD COMPANY Intellectual Property Administration P.O. Box 272400 Fort Collins, CO 80527-2400 (US)

Appl. No.: 10/215,995

Filed: Aug. 8, 2002

Publication Classi?cation

Int. Cl.7 .............................. .. G06F 9/46; G06F 9/00

US. Cl. .......................................... .. 719/330; 719/331

300

(57) ABSTRACT A method for increasing performance of selected poll appli cations Without recompilation. In one embodiment, the method is comprised of assigning the name of a library as a value for an environment variable in a softWare application. The library is comprised of softWare code for emulating a system call. The method is further comprised of launching the softWare application. The launching causes invocation of a loader associated With the softWare application. The method is further comprised of preloading the library into the softWare application. The preloading performed by the loader. The method is further comprised of redirecting the softWare application to invoke an alternative mechanism When the softWare application requests a system call. The invocation of the alternative mechanism implements the library as the system call. The method is further comprised of registering a plurality of ?le descriptors and events of interest With the alternative mechanism When the application makes said system call With a list of ?le descriptors identi fying sockets for Which the application has interest in events. Each of said plurality said ?le descriptors is related to an event of interest pertaining to an associated socket of interest. Those sockets With registered ?le descriptors are examined by the softWare application.

1%

Assigning the name ofa library as a value for an environment variable in a software application.

The library contains code for replacing system calls in a soltware application.

1 Launching the software application, which causes invocation ofa

loader associated with said software application.

I l’reloading the environment variable into the software application.

the preloading performed by the loader.

Registering a plurality of tile descriptors and events ofinterest with the alternative

mechanism when the application makes said system call with a list of?le descriptors

identifying sockets for which the application has interest in events. Each of said

plurality said ?le descriptors is related to an event ofintercst pertaining lo an associated

7 socket ol‘interest.

Redirecting the sotlware application to invoke the alternative mechanism when the

so?ware application requests a system call, and wherein the invocation ofthe

alternative mechanism implements the library as the system call.

l Accessing those sockets having ?le descriptors registered with the

allemative mechanism for which events of interest are indicated.

m

Page 2: Method for increasing performance of select and poll applications ...

Patent Application Publication Feb. 12, 2004 Sheet 1 0f 4 US 2004/0031044 A1

Client Computer System 1E2

150

Client Computer System 190a

S_L l 50

Communication Lines 173a 173b

Server Computer System 21.

Shim Library 150

FIGURE 1

Page 3: Method for increasing performance of select and poll applications ...
Page 4: Method for increasing performance of select and poll applications ...

Patent Application Publication Feb. 12, 2004 Sheet 3 of 4 US 2004/0031044 A1

300

Assigning the name of a library as a value for an environment variable in a software application.

The library contains code for replacing system calls in a sol‘tware application.

i Launching the so?ware application, which causes invocation of a

loader associated with said software application.

195

l Preloading the environment variable into the software application,

the preloading performed by the loader.

3%

Registering a plurality of ?le descriptors and events of interest with the alternative

mechanism when the application makes said system call with a list of tile descriptors

identifying sockets for which the application has interest in events. Each of said

plurality said ?le descriptors is related to an event of interest pertaining to an associated

7 socket of interest.

E

Redirecting the so?ware application to invoke the alternative mechanism when the

so?ware application requests a system call, and wherein the invocation of the

alternative mechanism implements the library as the system call.

Accessing those sockets having ?le descriptors registered with the

alternative mechanism for which events of' interest are indicated.

FIGURE 3

Page 5: Method for increasing performance of select and poll applications ...

Patent Application Publication Feb. 12, 2004 Sheet 4 of 4

400

US 2004/0031044 A1

Assigning the name of a library as a value for an environment variable in a software application. 402

i Launching ‘said software application causing invocation of a loader associated with said software application.

M

l Preloading said library into said software application, said preloading performed by said loader.

M

Registering a plurality of ?le descriptors and events of interest with the alternative mechanism when the application makes said system call with a list of ?le descriptors identifying sockets for which the application has interest in events. Each of said plurality said ?le descriptors is related to an event of interest pertaining to an associated socket of interest.

408

De?ned number of ?le descriptors necessary to invoke the

alternative mechanism ?

the minimum been exceeded ‘.7

4_1Z

Yes

Redirecting the software application to invoke the alternative mechanism when the software application requests a system call, subsequent to the minimum number of ?le descriptors needed to invoke said alternative mechanism being exceeded, wherein invocation of the alternative mechanism implements the library to replace the system call.

4 l 4

l Examing those sockets having ?le descriptors registered with the alternative mechanism for which events of interest are indicated‘

3%

FIGURE 4

Page 6: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

METHOD FOR INCREASING PERFORMANCE OF SELECT AND POLL APPLICATIONS WITHOUT

RECOMPILATION

TECHNICAL FIELD

[0001] Embodiments of the present invention are related to increasing performance of software applications. More particularly, the present invention provides a method to increase performance of softWare applications Without requiring application recompilation.

BACKGROUND ART

[0002] In a computer system or a computer network, a port number identi?es a sending or destination application. Every application running on a host computer system uses certain ports or ranges of ports to communicate With other appli cations running on other host computer systems. It is by these port numbers that a communication protocol, e.g., TCP (transmission control protocol) or UDP (user datagram pro tocol) determines Which application to pass the data to in the application layer. [0003] When an application on one computer system Wants to communicate With an application on another com puter system, it must specify the address of the device (IP address) and identify the application (port number). The combination of sending and destination port numbers and the sending and destination IP addresses de?nes a socket. A socket can be used to uniquely de?ne a UDP or TCP connection.

[0004] Applications, e.g., Internet applications, Were ini tially Written to handle one or tWo sockets. Internet appli cations, e.g., Telnet, UDP, or TCP, etc., typically handle one or tWo sockets, and accordingly, handle one or tWo connec tions at a time. With each socket are associated socket calls. A socket call can be, but is not limited to, a call to read from the socket, a call to Write to the socket, a call for error check on the socket, etc.

[0005] The proliferation of the Internet, and more particu larly the World Wide Web, has caused, in part, the devel opment of a Web server, a type of computer system that is adapted to serve/handle numerous sockets. Many of today’s current Web servers can handle thousands of concurrent

sockets/connections.

[0006] The socket calls are the same as those listed above, e.g., read, Write, and error check, but instead of one or tWo sockets to handle, there are commonly hundreds if not thousands of sockets that can be simultaneously opened. This means that the application Will check each of the sockets for read, Write, and/or error check. Accordingly, applications use vast amounts of processing time performing socket calls on the thousands of sockets. Disadvantageously, other useful Work or processes may not be performed While the Internet application is performing these thousands upon thousands of socket calls.

[0007] To overcome this draWback, system calls, e.g., select system call and poll system call, Were developed to be able to inform the application, e.g., an Internet application, When it is permitted to read from a socket or permitted to Write to a socket.

[0008] The system calls are not Without draWbacks asso ciated thereWith. When a poll call or a select call Was made,

Feb. 12, 2004

the kernel Would go through every socket opened by the application in the poll or select call, locking and unlocking the socket and checking for events of interest. Because there can be hundreds, thousands, or tens of thousands of sockets and each socket is looked at, With some sockets having hold mechanism, this can also use large amounts of processing time.

[0009] Hence, many applications are not provided With ef?cient socket communication.

DISCLOSURE OF THE INVENTION

[0010] Thus, embodiments of the present invention are draWn to providing a method for increasing performance in system calls in a softWare application Without recompilation by redirecting certain system calls. Embodiments of the present invention further provide for registration of ?le descriptors With an alternative event mechanism, Wherein each ?le descriptor can have an event of interest to the application. Embodiments of the present invention further provide for scanning those sockets With ?le descriptors registered With the alternative event mechanism.

[0011] In one embodiment, a method of replacing a system call With an alternative mechanism in a softWare application is comprised of assigning the name of a library as a value for an environment variable in a softWare application. The library is comprised of softWare code for emulating a system call. The method is further comprised of launching the softWare application Which causes invocation of a loader associated With the softWare application. The method is further comprised of preloading the library into the softWare application. The loader performs the preloading. The method is further comprised of redirecting the softWare application to invoke an alternative event mechanism When the softWare application requests a system call. The invo cation of the alternative mechanism implements the library as said system call. The method is further comprised of registering a plurality of ?le descriptors and events of interest With the alternative mechanism When the application makes said system call With a list of ?le descriptors identi fying sockets for Which the application has interest in events. Each of said plurality said ?le descriptors is related to an event of interest pertaining to an associated socket of interest. Those sockets With registered ?le descriptors are eXamined by the softWare application.

BRIEF DESCRIPTION OF THE DRAWINGS

[0012] The accompanying draWings, Which are incorpo rated in and form a part of this speci?cation, illustrate embodiments of the invention and, together With the description, serve to explain the principles of the invention.

[0013] FIG. 1 is an illustration of an exemplary netWork environment in Which embodiments of the present invention can be practiced, in accordance With one embodiment of the present invention.

[0014] FIG. 2 is a block diagram of a computer system upon Which embodiments of the present invention may be practiced, in accordance With one embodiment of the present invention.

[0015] FIG. 3 is a ?oWchart of shoWing steps in a process of increasing performance of system calls in a softWare

Page 7: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

application Without recompilation, in accordance With one embodiment of the present invention.

[0016] FIG. 4 is a ?owchart of showing steps in another process of increasing performance of system calls in a softWare application Without recompilation, in accordance With another embodiment of the present invention.

BEST MODES FOR CARRYING OUT THE INVENTION

[0017] Embodiments for a method for increasing perfor mance in selected system calls in a softWare application Without application recompilation are described. Reference Will noW be made in detail to the preferred embodiments of the invention, examples of Which are illustrated in the accompanying draWings. While the invention Will be described in conjunction With the preferred embodiments, it Will be understood that they are not intended to limit the invention to these embodiments. On the contrary, the inven tion is intended to cover alternatives, modi?cations and equivalents, Which may be included Within the spirit and scope of the invention as de?ned by the appended claims. Furthermore, in the folloWing detailed description of the present invention, numerous speci?c details are set forth in order to provide a thorough understanding of the present invention.

[0018] The method for increasing performance in an appli cation is, in one embodiment, comprised of a library of computer instructions, e. g., shim library 150 of FIGS. 1 and 2, that provides an alternative to certain system calls asso ciated With an application including, but not limited to, select system calls, poll system calls, and close calls. The shim library contains code that provides information related to those events of interest on sockets of interest. Those events of interest on sockets of interest are automatically registered With the alternative event mechanism on behalf of the application. The shim library, When invoked, obviates the application’s select and poll system calls, and in their stead utiliZes a functionality contained Within the operating system under Which the application runs, e.g., Eventports, to initiate functions analogous to the obviated system calls.

[0019] Advantages of embodiments of the present inven tion, as Will be shoWn, beloW, are that the shim library is invoked autonomous With loading of the application. Another advantage is that by virtue of the shim library being modi?ed, the application does not require recoding or recompiling. Additionally advantageous is that the shim library commonly reduces processor time used during sys tem calls by only examining those sockets that have been registered and have pending events of interest, When com pared With conventional system calls Where each socket is examined.

[0020] Embodiments of the present invention are dis cussed primarily in the context of softWare applications Which can simultaneously service or handle multiple sock ets, e.g., Internet applications, in a netWork and/or client server environment. HoWever, it is noted that embodiments of the present invention can be utiliZed by other softWare applications that are adapted to simultaneously service or handle multiple sockets including, but not limited to, Inter net applications.

[0021] FIG. 1 is a block diagram illustrating an exemplary client-server computer system netWork, e.g., netWork 100,

Feb. 12, 2004

upon Which embodiments of the present invention may be practiced. NetWork 100 may be a communication netWork located Within a ?reWall of an organiZation or corporation

(an “Intranet”), or netWork 100 may represent a portion of the World Wide Web or Internet. Client (or user) computer systems 190a and 190b and server computer system 191 are communicatively coupled via communication lines 173a and 173b,‘ the mechanisms for coupling computer systems over the Internet or over Intranets are Well-knoWn in the art.

This coupling can be accomplished over any netWork pro tocol that supports a netWork connection, such as IP (Internet Protocol), TCP (Transmission Control Protocol), UDP (User Datagram Protocol), TELNET, NetBIOS, IPX (Internet Packet Exchange), and LU6.2, and link layers protocols such as Ethernet, token ring, and ATM (Asynchronous Transfer Mode). Alternatively, client computer systems 190a and 190b can be coupled to server computer system 191 via an input/output port (e.g., a serial port) of server computer system 191; that is, client computer systems 190a and 190b and server computer system 191 may be non netWorked devices. It is appreciated that, for simplicity, only tWo client computer systems and a single server computer system are shoWn; hoWever, it is understood that netWork 100 may comprise any number of client computer systems and server computer systems.

[0022] Still referring to FIG. 1, also shoWn, in one embodiment, is a library of instructions, e.g., shim library 150 (S_L 150), representing embodiments of the present invention that are disposed in server 191. In one embodi ment, shim library 150 is also disposed in client computers 190a and 190b as Well. In one embodiment, shim library 150 (S_L 150) contains a collection of softWare instructions or code that is used in place of some of the system calls that applications make during operation, e.g., select system calls and poll system calls.

[0023] FIG. 2 is a block diagram illustrating components and circuitry of an exemplary computer system 200, Which can be implemented Within a client computer system, e.g., client computer system 190a and/or 190b, and in a server computer system, e.g., server computer system 190, of FIG. 1, upon Which embodiments of the present invention may be practiced. Computer system 200 includes an address/data bus 210 for communicating information, a central processor 201 coupled With the bus for processing information and instructions, a volatile memory 202 (e.g., random access memory, RAM) coupled With the bus 210 for storing infor mation and instructions for the central processor 201 and a non-volatile memory 203 (e.g., read only memory, ROM) coupled With the bus 210 for storing static information and instructions for the processor 201. It is noted that in one embodiment, computer system 200 can be con?gured With a plurality of processors 201.

[0024] Computer system 200 of FIG. 2 also includes a data storage device 204 coupled With bus 210 for storing information and instructions, e.g., shim library 150. Data storage device 204 can be, for example, an HDD (hard disk drive), an FDD (?oppy disk drive), a memory stick, a CD-RW (compact disk With Write functionality), a tape drive, etc., and furthermore device 204 can be in multiples or in a combination thereof. Data storage device 204 may also be multiply instanced, removable, and/or hot sWappable (connected or unconnected While computer is poWered).

Page 8: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

[0025] With reference still to FIG. 2, computer system 200 also includes a network communication device 235, Which is coupled to bus 210 for providing a communication link betWeen computer system 200, and a netWork environ ment, e.g., netWork environment 100 of FIG. 1. As such, netWork communication device 235 enables central proces sor unit 201 to communicate With other electronic systems coupled to the network, e.g., netWork 100 of FIG. 1. It should be appreciated that the present embodiment of net Work communication device 235 is Well suited to be imple mented in a Wide variety of Ways. In one example, netWork communication device 235 is coupled to an antenna and provides the functionality to transmit and receive informa tion over a Wireless communication interface. In another example, netWork communication device 235 could be implemented as a modem. In yet another example, netWork communication device 235 could be con?gured as a NIC (netWork interface card). [0026] Still referring to FIG. 2, netWork communication device 235, in one embodiment, includes an optional digital signal processor (DSP) 220 for processing data to be trans mitted or data that are received via netWork communication device 235. Alternatively, processor 201 can perform some or all of the functions performed by DSP 220.

[0027] Also included in computer system 200 of FIG. 2 is an optional alphanumeric input device 206. In one imple mentation, device 206 is a keyboard. Alphanumeric input device 206 can communicate information and command selections to processor 201.

[0028] Computer system 200 of FIG. 2 also includes an optional cursor control or directing device (on-screen cursor control 207) coupled to bus 210 for communicating user input information and command selections to processor 201. Many implementations of the cursor control device are knoWn in the art including a trackball, mouse, joystick or special keys on alphanumeric input device 206 capable of signaling movement of a given direction or manner of displacement. It is to be appreciated that the cursor control 207 also may be directed and/or activated via input from the keyboard using special keys and key sequence commands. Alternatively, the cursor may be directed and/or activated via input from a number of specially adapted cursor direct ing devices. [0029] In one implementation, on-screen cursor control device 207 is a touch screen device incorporated With display device 205. On-screen cursor control device 207 is capable of registering a position on display device 205 Where a pointing object, e.g., a stylus or a user’s ?nger, makes contact. In another implementation, on-screen cursor control device 207 is a touch pad, or, in another implemen tation, a ?nger activated push stick. In another common implementation, on-screen cursor control device 207 is a mouse or similar pointing device.

[0030] Computer system 200 also contains a display device 205 coupled to the bus 210 for displaying informa tion to the computer user. Display device 205 is suitable for generating graphic images and alphanumeric characters rec ogniZable to the user. Any of a number of display technolo gies can be used, e.g., cathode ray tube (CRT), liquid crystal display (LCD), ?eld emission display (FED), thin ?lm transistor (TFT), plasma, etc., for display device 205. [0031] Referring, collectively, to functionalities provided by embodiments of the present invention, a shim library,

Feb. 12, 2004

e.g., shim library 150 of FIGS. 1 and 2, is adapted to take an application’s, e.g., an Internet application’s, select, poll, and close system calls (select( ), poll( ), and close( ), respectively) and turn those select and poll system calls into eventport calls in a manner invisible to the application. In one embodiment, shim library 150 is comprised of tWo portions, as seen in Appendices A and B. In the present embodiment, Appendix A is related to poll system calls and Appendix B is related to select system calls. It is noted that each portion of shim library 150, Appendices A and B, also contain therein close( ) call related instructions for poll system calls and select system calls. Eventports is an alter native event noti?cation mechanism contained Within the operating system under Which the application is operable. It is noted that other alternative event noti?cation mechanisms having analogous functionality to eventports can be utiliZed under embodiments of the present invention.

[0032] With reference to poll system calls, as shoWn in Appendix A, and more particularly shoWn in SECTION 5 thereof, the present invention, shim library 150, provides, in one embodiment, a table/tree structure Which contains infor mation regarding event ports. In one embodiment, there is one event port ?le descriptor for each unique array of pollfds (poll ?le descriptors) from the application. When the appli cation calls poll, e.g., poll( ), there is found an associated eventport. The application then Walks, or scans, the appli cations poll() array. Each of the ?le descriptors in the pollfd array is checked against the list of fds (?le descriptors) registered With the alternative event noti?cation mechanism, e.g., eventports, as shoWn in SECTION 3 of Appendix A. In one embodiment, When the fd has been previously regis tered, With the events the application has placed in the event ?eld of the pollfd structure for that fd, the library proceeds to the next fd. In one embodiment, When the fd has not been previously registered With the events the application has requested, the library Will register that fd and subsequent thereto, move to the next fd.

[0033] In one embodiment, When the registration fails, functionalities contained Within shim library 150 provide for a seamless reverting back to a real poll() system call, using those arguments provided by the application. In one embodi ment, When the events ?eld of the pollfd struct is for an undesired event type, shim library 150 also provides for a seamless reverting back to calling a real poll( ) system call, using those arguments provided by the application.

[0034] In one embodiment, an optional threshold and limit can be set, such that if the number of ?le descriptors reference in the application’s call to poll or select is beloW a minimum (threshold) or above a maximum (limit), the library Will revert back to calling a real poll( ) using those arguments provided by the application, as shoWn in SEC TION 1 of Appendix A.

[0035] In one embodiment, When scanning the applica tion’s pollfd array, and there is a ?le descriptor greater than the siZe of the application’s internal arrays, the application Will revert back to calling a real poll( ) using those argu ments provided by the application.

[0036] In one embodiment, once an fd (?le descriptor) is registered to an eventport, it Will not be immediately de registered When not part of a subsequent poll( ) call. In one embodiment, an optional threshold for de-registering a fd can be set, such that for a given fd that is not part of a poll(

Page 9: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

) call for a given set of pollfds N times in succession, that fd Will be de-registered. It is noted that by virtue of not immediately de-registering fds from eventports, When the library makes the ioctl (input/output control) to retrieve events from the eventports, there can be some fds that are not of interest to the application. If none of the application’s fds are returned by the ioctl from the eventports, there can be a pause prior to a subsequent ioctl retrieval of events from the eventports, in one embodiment. In another embodiment, the application Would de-register interest in those fds in Which the application is not interested and then attempt the ioctl retrieval of events or in yet another embodiment, revert back to a real poll( ) call, as is shoWn collectively in SECTION 4 of Appendix A.

[0037] With reference to select system calls, as shoWn in Appendix B, and more particularly With reference to SEC TION 5 thereof, the present invention, shim library 150, provides, in one embodiment, a table/tree structure Which contains information regarding event ports. In one embodi ment, there is one event port ?le descriptor for each unique array of fd_sets from the application. When the application calls select, e.g., select( ), With the fd_set array address, there is found an associated eventport. The application then Walks, or scans, the applications fd_set array. Each of the ?le descriptors in the fd_set array is checked against the list of fds (?le descriptors) registered, as described in SECTION 3 of Appendix B, With the alternative event noti?cation mechanism, e.g., eventports. In one embodiment, When the fd has been previously registered, With the events the application has placed in the event ?eld of the fd_set structure for that fd, the application proceeds to the next fd. In one embodiment, When the fd has not been previously registered With the events the application has requested, the application Will register that fd and subsequent thereto, move to the next fd.

[0038] In one embodiment, When the registration fails, functionalities contained Within shim library 150 provide for a seamless reverting back to a real select() system call, using those arguments provided by the application. In one embodi ment, When the events ?eld of the fd_set struct is for an undesired event type, shim library 150 also provides, for the application, a seamless reverting back to calling a real select( ) system call, using those arguments provided by the application. [0039] In one embodiment, an optional threshold and limit can be set, such that if the number of ?le descriptors (nfds) registered is beloW a minimum (threshold) or above a maximum (limit), the application Will revert back to calling a real select( ) using those arguments provided by the application, as shoWn in SECTION 1 of Appendix B.

[0040] In one embodiment, When scanning the applica tion’s fd_set array, and there is a ?le descriptor greater than the siZe of the application’s internal arrays, the application Will revert back to calling a real select( ) using those arguments provided by the application.

[0041] In one embodiment, once an fd (?le descriptor) is registered to an eventport, it Will not be immediately de registered When not part of a poll() call. In one embodiment, an optional threshold for de-registering a fd can be set, such that for a given fd that is not part of a select() call for a given set of fd_sets N times in succession, that fd Will be de registered. It is noted that by virtue of not immediately

Feb. 12, 2004

de-registering fds from eventports, When the application makes the ioctl (input/output control) retrieve events from the eventports, there can be some fds that are not of interest to the application. If none of the application’s fds are returned by the ioctl from the eventports, there can be a pause prior to a subsequent ioctl retrieval of events from the eventports, in one embodiment. In another embodiment, the application Would de-register interest in those fds in Which the application is not interested and then attempt the ioctl retrieval of events or in yet another embodiment, revert back to a real select( ) call, as shoWn collectively in SECTION 4 of Appendix B.

[0042] In one embodiment, there is a mutex (mutually exclusive) for each unique pollfd array base and fd_set array base called by the application. If there are separate threads calling poll() and/or select( ), those threads do not call polI( ) and/or select( ) With the same pollfd array and/orfd_set array, so those threads do not content on the mutex.

[0043] In one embodiment, there is a mutex protecting the list of eventports themselves. In one embodiment, the “glo bal” mutex is locked, then the relevant entry is located. It’s mutex is unlocked, then the global mutex is unlocked, thus alloWing other threads to perform their associated functions and/or tasks. It is noted that in another embodiment, the mutex and global mutex are combined in one large global mutex.

[0044] With reference to close system call, e.g., close( ), as shoWn in SECTION 6 of Appendices A and B, When an application calls close( ), functionalities contained Within shim library 150 also intercept the close call, in one embodi ment of the present invention. When the application calls close, the application acquires the global mutex and then commences scanning the shim_contexts clearing the fd_context entries for that fd (?le descriptor) in each of the allocated contexts. The library is not required to de-register the fd from the eventport, that is accomplished by automati cally When the application calls the real close(

[0045] Subsequently, When the application gets the fd again and calls poll( ) With it in one of the pollfd structures and/or When the application gets the fd again and calls select( ) With it in one of the fd_set structures, the pollfd structures and the fd_set structure Will be re-registered into the appropriate shim context.

[0046] FIG. 3 is a ?oWchart 300 of steps performed in accordance With one embodiment of the present invention for providing a method for increasing performance of selected system calls, e. g., select system call and poll system call, in a softWare application Without recompilation. FloW chart 300 includes processes of the present invention Which, in one embodiment, are carried out by processors and electrical components under the control of computer read able and computer executable instructions. The computer readable and computer executable instructions reside, for example, in data storage features such as computer usable volatile memory 202 and/or computer usable non-volatile memory 203 and/or data storage device 204 of FIG. 1. HoWever, the computer readable and computer executable instructions may reside in any type of computer readable medium. Although speci?c steps are disclosed in ?oWchart 300, such steps are exemplary. That is, the present invention is Well suited to performing various other steps or variations of the steps recited in FIG. 3. Within the present embodi

Page 10: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

ment, it should be appreciated that the steps of ?owchart 300 may be performed by softWare, by hardWare or by any combination of softWare and hardWare.

[0047] In step 302 of FIG. 3, the name of the library, e.g., shim library 150 (shim_lib) of FIGS. 1 and 2 and Appen dices A and B, is assigned as the value for an environmental variable in a softWare application, in one embodiment of the present invention. Shim library 150 contains, in one embodi ment, softWare code for replacing system calls made by an application, e.g., an Internet application, during the opera tion of the application.

[0048] In step 304 of FIG. 3, the softWare application is launched. There are many Well knoWn methods to launch an application including, but not limited to, using a command line interface or a graphical user interface to invoke the executable ?le associated With the softWare application. Launching of the softWare application causes the invocation of a loader associated With the softWare application.

[0049] In step 306 of FIG. 3, the loader preloads the library Whose name is speci?ed in the environment variable, e.g., shim library 150 of FIG. 2 into the softWare applica tion, in one embodiment of the present invention. In one embodiment, the operating system, under Which the soft Ware application is operable, has the preloading functional ity contained therein, and accordingly provided the preload ing natively. In another embodiment, patches, in the form of softWare code, may be added to the operating system to provide the preloading functionality.

[0050] In step 308 of FIG. 3, subsequent to launching the softWare application, the softWare application, in this example an Internet application, opens a plurality of sockets and also creates a plurality of ?le descriptors related to each of the opened sockets. Each ?le descriptor is related to an event of interest pertaining to an associated socket of interest. These ?le descriptors are registered With an alter native event noti?cation mechanism, as shoWn in SECTION 3 of Appendices A and B, Whose functionality is contained Within the operating system under Which the softWare appli cation is operable.

[0051] Still referring to step 308, in one embodiment, the alternative event noti?cation mechanism is eventports. In one embodiment, eventports is native to the operating sys tem, meaning its functionality is contained Within the oper ating system. In another embodiment, patches can also applied to the operating system to provide eventport func tionality to those operating systems not natively con?gured With eventports. It is noted that nearly any alternative event noti?cation mechanism can also bene?t from functionalities provided by embodiments of the present invention.

[0052] Still referring to FIG. 3, in one embodiment, the operating system under Which the softWare application is operable is a Unix type operating system including, but not limited to Unix, Linux, Apple OS X (a Unix derivative), and the like. It is noted that other operating systems may also bene?t from functionalities provided by embodiments of the present invention, provided those other operating systems are con?gured With preloading and alternative event noti? cation mechanism functionalities.

[0053] In step 310 of FIG. 3, the softWare application, e.g., an Internet application, is redirected to invoke an alternative event noti?cation mechanism, e.g., eventports,

Feb. 12, 2004

When the application makes a system call, e.g., a select system call and/or a poll system call. The invocation of the eventports causes shim library 150 to be implemented so as to replace the system calls including, but not limited to select system calls and poll system calls.

[0054] In step 312 of FIG. 3, by virtue of registering events of interest on sockets of interest With eventports, only those sockets that have events so registered are checked for events of interest. Advantageously, this provides a substan tial reduction in the number of sockets that are scanned, thus reducing CPU usage and time consumed.

[0055] FIG. 4 is a ?oWchart 400 of steps performed in accordance With another embodiment of the present inven tion for providing a method for increasing performance of selected system calls, e. g., select system call and poll system call, in a softWare application Without recompilation. FloW chart 400 includes processes of the present invention Which, in one embodiment, are carried out by processors and electrical components under the control of computer read able and computer executable instructions. The computer readable and computer executable instructions reside, for example, in data storage features such as computer usable volatile memory 202 and/or computer usable non-volatile memory 203 and/or data storage device 204 of FIG. 2. HoWever, the computer readable and computer executable instructions may reside in any type of computer readable medium. Although speci?c steps are disclosed in ?oWchart 400, such steps are exemplary. That is, the present invention is Well suited to performing various other steps or variations of the steps recited in FIG. 4. Within the present embodi ment, it should be appreciated that the steps of ?oWchart 400 may be performed by softWare, by hardWare or by any combination of softWare and hardWare.

[0056] In step 402 of FIG. 4, the name of the library, e.g., shim library 150 (shim_lib) of Appendices A and B, is assigned as the value for an environmental variable in a softWare application, in one embodiment of the present invention. Shim library 150 contains, in one embodiment, softWare code for replacing system calls made by an appli cation, e.g., an Internet application, during the operation of the application.

[0057] In step 404 of FIG. 4, the softWare application is launched. There are many Well knoWn methods to launch an application including, but not limited to, using a command line interface or a graphical user interface to invoke the executable ?le associated With the softWare application. Launching of the softWare application causes the invocation of a loader associated With the softWare application. In one embodiment, the operating system under Which the softWare application is operable is a Unix type operating system including, but not limited to Unix, Linux, Apple OS X (a Unix derivative), and the like. It is noted that other operating systems may also bene?t from functionalities provided by embodiments of the present invention, provided those other operating systems are con?gured With preloading and alter native event noti?cation mechanism functionalities.

[0058] In step 406 of FIG. 4, the loader preloads the environment variable, e.g., shim library 150 of FIG. 2 into the softWare application, in one embodiment of the present invention. In one embodiment, the operating system, under Which the softWare application is operable, has the preload ing functionality contained therein, and accordingly pro

Page 11: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1

vided the preloading natively. In another embodiment, patches, in the form of software code, may be added to the operating system to provide the preloading functionality.

[0059] In step 408 of FIG. 4, a plurality of ?le descriptors are registered With the alternative event noti?cation mecha nism, e.g., eventports, as shoWn in SECTION 3 of Appen dices A and B, in one embodiment of the present invention. The ?le descriptors are created by the operating system When sockets are opened by the application. Each of the plurality of ?le descriptors is related to an event of interest pertaining to an associated socket.

[0060] In step 410 of FIG. 4, there can be a de?ned minimum number of ?le descriptors that Would need to be registered With eventports before eventports can be invoked, in one embodiment of the present invention, as shoWn in SECTION 1 of AppendicesA and B. In one embodiment, the minimum number of registered ?le descriptors is 128 ?le descriptors, In another embodiment, a greater number or a feWer number of registered ?le descriptors can be speci?ed to invoke the alternative event noti?cation mechanism.

[0061] In step 412 of FIG. 4, by virtue of the de?ned number of registered ?le descriptors necessary to invoke the alternative event noti?cation mechanism, as described in step 410, above, until that minimum is exceeded, eventports Will not be invoked, in one embodiment of the present invention. This means that When the application makes a poll or select system call, the real poll and select call Will be made. HoWever, once the minimum has been exceeded, the alternative event noti?cation mechanism is invoked.

[0062] In step 414 of FIG. 4, the softWare application, e.g., an Internet application, is redirected to invoke an alternative event noti?cation mechanism, e.g., eventports, When the application makes a system call, e.g., a select system call and/or a poll system call. The invocation of the eventports causes shim library 150 to be implemented so as to replace the system calls including, but not limited to select system calls and poll system calls.

[0063] In step 416 of FIG. 34, by virtue of registering events of interest of sockets of interest With eventports, only those sockets that have events so registered are checked for events of interest. Advantageously, this provides a substan tial reduction in the number of sockets that are scanned, thus reducing CPU usage and time consumption.

[0064] In a practical implementation of the present inven tion, shim library 150 is adapted for an application for a company in the travel industry. The application Was already Written to use poll( ), so the application Was already con ?gured With much of the semantics and syntax needed for Eventports. This application had its connections spread across three threads calling poll( ) and recv( ) With three other intermediate threads and three threads calling Writev( ). So, When the number of connections in the table beloW shoWs 1000, it means that each of the calls to poll( ) Was for roughly 333 connections.

[0065] This application Would not poll( ) for Write unless necessary. Again the behavior of the application Was such that it never needed to poll( ) for Write, and again, this is a best practice. The computer netWork system under test, e.g., computer system 100 implemented in a netWork environ ment, e.g., netWork environment 100 of FIG. 1, Was a multiprocessor computer system in a multinode netWork

Feb. 12, 2004

environment (netWork 100 of FIG. 1) operating in a UNIX environment With Eventports and miscellaneous other patches installed.

[0066] On the client “side” of this computer netWork system Were ?ve multiprocessor computer systems. e.g., computer system 200 of FIG. 2, thus simulating the remote clients, and on the server “side” of this system Were another ?ve multiprocessor computer systems for simulating back end-servers. In this example the set of measurements, the SUT Was alWays taken to 100% CPU utiliZation, so We shoW Transactions per Second as the unit of measure. It is noted that the connections in this situation One, beloW.

TABLE ONE

Threaded MUX Application Performance poll( ) versus Eventports Transactions per Second

Eventport Connections poll( ) shim library

100 14110 17300 500 8800 15700 1000 5700 14700 3000 1600 13200 6000 750 12200

10000 500 11200 19800 330 9130

[0067] Table One, above, shoWs that the performance gain from eventports can be quite considerable. The column labeled “poll( )” is the performance of the application When it Was Written to use poll( The column labeled “eventport shim library” is from When the application Was loaded With shim library 150, in one embodiment.

[0068] This library Would be pre-loaded into the running application image via the LD_PRELOAD functionality and Would intercept the application’s calls to poll( ) and turn them into Eventport calls. As shoWn, this Was successful, increasing performance considerably.

[0069] Thus, embodiments of the present invention are draWn to providing a method for improving performance of selected system calls in an application Without application recompilation by invoking a library, e.g., shim library 150, as an alternative to system calls. Further, by virtue of shim library 150 examining only those sockets registered With the alternative event noti?cation mechanism, a substantial reduction in CPU usage is realiZed. Additionally advanta geous is that When additional sockets are to be added, the shim library is recoded, and not the application itself.

[0070] The foregoing description of speci?c embodiments of the present invention have been presented for purposes of illustration and description. They are not intended to be exhaustive or to limit the invention to the precise forms disclosed, and many modi?cations and variations are pos sible in light of the above teaching. The embodiments Were chosen and described in order to best explain the principles of the invention and its practical application, to thereby enable others skilled in the art to best utiliZe the invention and various embodiments With various modi?cations as are suited to the particular use contemplated. It is intended that the scope of the invention be de?ned by the Claims appended hereto and their equivalents.

Page 12: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004

APPENDIX A

/* cc +2 -D_REENTRANT <archstuff> -c poll_shim.c */ /* ld -b -0 libpollshimsl p0ll_shim.o —lpthread */

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stropts.h> #include <fcntl.h> #include <devpoll.h> /* p0ll.h is included by devpolLh, but this shouldn't hurt */ #include <p0ll.h>

#include <pthread.h>

static const char pollAshim_version[] =\ "@(#)p0ll_shimsc ";

SECTION 1:

#de?ne POLL_SHIM4MAX*CONTEXTS 16 #de?ne POLL_SHIM_NFDS_LIMIT 20000 #de?ne POLL_SHIM_NFDS_THRESHOLD 12s #de?ne POLL_SHIM_DUMP_STATS 1024 #de?ne POLLWSHIM_NFDS_DUP_COUNT INT_MAX

static int poll_shim~nfds_threshold I POLL_SHIM_NFDS_THRESHOLD; static int poll_shim_nfds_dup_count *- POLL_SHIM_NFDS?DUPiCOUNT; static int poll_shim_nfds_limit = POLL_SHIM~NFDS_LIMIT;

/* For tracking things associated with a given FD */

struct shim_fd_context { struct pollfd *users_pollfd_ptr;

/* ptr to this FD in the users pollfd array. For quickly ?nding the entry in the users poll l’d array after the eventport ioctl returns */

unsigned int generation; /* The last time this FD appeared in the users pollfd array‘ For detecting

duplicate fds in the user's pollfd array. If an entry with the same generation number as this generation of call is encountered, it means that the user had the same FD in the pollfd array at least twice and we will punt to the real p0ll() call. Subsequently, it will also be used to decide when to unregister an FD from the eventport */

int registered; /* Is this FD already registered in the eventport? */

Page 13: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004

short events; /* For what events have we registered this PD in the eventport? Initially

this should either be Zero to mean nothing registered, or ?ags to indicate we've registered for interest in reading, but not writing, at normal priorities. */

l;

/* The table of contexts that will be tracked */

struct shim_context { void *users_pollfdgbaseiptr;

/* The mutex used to protect this specific shim_context */

pthread_mutex_t shim_context_mutex; unsigned int generation;

/* How many times the user called poll() with the same pollfd pointer */

int eventport; /* File descriptor for the event port. */

int nfds; /* Count of fd's registered to the eventport */

/* The list of "state" info the shim maintains on PBS that may/may not be registered with the cventport. This can be a sparse array. We are trading memory for time so we can use

FD values as indices. */

struet shim~fd_e0ntext shim_fd_c0ntexts[POLL_SHIM_NFDS_L1MIT]; };

struct shim_context poll_shim_contexts[POLL_SHIM_MAX_CONTEXTS];

/* Protecting these statistics is not a concern, since all that is ever done is to increment them and runtime decisions are not made based on these statistics. */ static int poll_calls:O;

/* Number of times application calls poll */ static int eventp0rt_ca1ls:0;

/* Number of times eventport called*/ static int evp_returned_fds=0;

/* Number of times an eventport call returned fd's of interest to user */

static int I'GHIPOHHCZIHSIO; /* Number of times poll called */

static int eventport_registers=0; /* Number of fd's registered */

static int eventport_deregisters:(); /* Number of fd's deregistered */

static int user~dup_fds=0;

Page 14: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004

/* Number of times same fd appeared twice in fds array */ static int eventport_regfail:0;

/* Number of times an eventport registration failed */ static int eventportgderegfail=0;

/* Number of times an evcntport registration failed */ static int eventwn0t_pollin=0;

/* Number of times punting because user wanted something other than POLLTN */

static int nfds_below_threshold=0; /* Number of times punting because user was asking for too few fds to be

polled. */ static int nfds_above~limit=();

/* Number of times punting to poll because user was asking for more fds than con?gured to track. */ static int closeicalls = 0;

/* Number of calls to close() intercepted by the shim */ static int close_evp_walks : 0;

/* Number of intercepted close calls which walked the shim contexts */ static int reg_walkwavoids I 0;

/* Number of times registration avoided, by virtue of the heuristic. */ static int reg_heuristicr resets = O; \

/* Number of times resetting the registration walk avoidance heuristic */ static int reg_heuristic_resetsl I 0; static int reg_heuristic_resets2 I 0; static int reg_heuristic_resets3 = 0;

static pthread_once_t once_contr0l = PTIIREADAONCEAINIT;

static pthread_mutex_t poll_shim_global_mutex I PTHREADAMUTEXJNITIALIZER;

int *(*real_poll)(struct pollfd fds[], nfds_t nfds, int timeout) = NULL; int *(*real_close)(int ?ldes) = NULL;

void poll_shim_stats() { fprintf(stderr,"poll_shim statistics:\n"); fprintf(stderr,"\t%d calls to poll() by user\n",poll_calls); fprintf(stderr,"\t%d calls to eventport by shirn\n",eventport_calls); tprintf(stderr,"\t%d evp calls returned interesting fds\n",evpgretumedjds); fprintf(stderr,"\t%d calls to poll() by shim\n",realpolLcalls); fprintf(stderr,"\t%d fds registered by shim\n",eventport_registers); fprintf(stderr,"\t%d fds deregistered by shi1n\n",eventport_deregisters); fprintf(stderr,"\t%d calls with fd listed twice in fds \n",user~dup_fds); fprintf(stderr,"\t%d eventport registration failurcs\n",evcntport_regfail); fprintf(stderr,"\t%d calls with an event other than

POLLIN]POLLRDNORM\n",event_not _po1lin);

Page 15: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 10

fprintf(stderr,”\t%d calls with nfds < threshold (%d)\n",

nfds_below_threshold, poll?shim_nfds_threshold);

fprintf(stderr,"\t%d calls with nfds > limit (%d)\n", nfds xabove ilimit, poll?shim_nfds_limit);

fprintf(stderr,"\t%d calls to close\n",close_calls); fprintf(stderr,"\t%d calls to close walked shim contexts\n",close_evp_walks); fprintf(stderr,“\t%d registration walks avoided by hcuristic\n",

reg_walk_avoids); fprintf(stderr,“\t%d registration avoidance heuristic resets\n",

reg_heuristic_resets); fprintf(stderr,"\t\t%d location l\n" ,reg__heuristic_resets l ); fprintf(stderr,"\t\t%d location 2\n",reg_heuristic_resets2); fprintf(stderr,"\t\t%d location 3\n",reg _heuristic_resets3); f?ush(stderr);

void poll_shimiinitialize() {

char *threshold_string; int ret;

if (rcal_poll == NULL) { fprint?stderr,

">>>> WARNING! POLL() to EVP SHIM ACTIVATED <<<<\n"); fprintf(stderr,“%s\n",poll_shim_version); atexit(poll_shim_stats);

} else { fprintf(stderr,"someone we had pthread_shim*initialize called twice?\n"); return;

/* dlsym() to get entry point to real poll for when it is necessary. Just use RTLD_NEXT magic "handleH and presume that LD_PRELOADed is loaded into the executable, so poll() should be found without having to dlopen() anything, as implied from the dlopen() manpage. */

real 3011 =

(int * (*) (struct pollfd fds[], nfds_t nfds, int timeout))dlsym (RTLD_NEXT, "poll") ;

if(real J01 == NULL) {

Page 16: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 11

ibrintf(stderr,"unable to dlsym poll: %s\n",dlerror()); fprintf(stderr,"consider undoing the LD?PRELOAD of the poll shim\n"); exit (0) ;

real_elose : (int * (*) (int ?ldes))dlsyzn (RTLD_NEXT, "el0se") ;

if (real_elose :: NULL)

{ fprintf(stderr,"unable to dlsym close: %s\n",dlerror()); fprintf(stderr,"c0nsider undoing the LD_PRELOAD of the poll shim\n"); exit (0) ;

}

if ((threshold_string I getenv(“EVP_SHIM_NFDS_THRESHOLD")) !:

NULL) {

/* User wants to set value for nfsd that will trigger using eventport. */

if((ret I sseanf(thresholdgstring,

Il%dll, &p0ll‘shiminfdsgthreshold» != l) {

/* Error in reading threshold value from the environment, use the default */

poll_shim_nfds_threshold I PQLL_SHIM_NFDS_THRESHOLD; }

’ }

if ((threshold_string I getenv("EVP_SHIM_NFDS_DUP_COUNT")) 1:

NULL) {

/* The user wants to set value for the number of times nfsd is the same that will trigger not

bothering to walk the pollfd array and assume that everything is already registered. */

if ((ret ; sseanf(threshold#string, ll(%)(i"y &poll_shirn_nfds_dup_count)) !:l) {

/* Error in reading dup_eount value from the environment, use the default */

poll_shim_nfds_clup_eount I POLL_SHIM_NFDS_DUP_COUNT; }

}

fprintf(stderr,"Eventport shim settings: \n"); fprintf(stderr,"\t use evp when nfds > %d\n",

Page 17: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 12

poll_shim_nfdsithreshold); fprintf(stderr,"\t use evp when nfds < %d\n",

poll_shim_nfdsilimit); fprintf(stderr,"\t use <= %d evp contexts\n",

poll_shim_nfds_limit); fprintf(stderr,"\t don't regwalk when nfds same %d times\n",

poll_shim_nfds_dup_count); }

SECTION 2;

/* This routine is only called during initialization of a poll_shimfcontext, for setting initial values of poll_fd_context struct to desired values. lt is here as a separate call only to satisfy those who insist on pure CS 101 coding styles. Because initialization should be rare, the call overhead is acceptable. */

void poll?fd_context_initialize(struct shim_fd_context fd_contexts[]) { int i;

/* By virtue of this being a contiguous array and everything is being set to zero/NULL, could just call memsett)v */

for (i=0; i < POLL_SHIM_NFDS_LIMIT; i++) { fd‘ contexts[i].users_pollfd_ptr = NULL; fdAcontexts?].generation I 0;

fd_contexts[i].cvents = 0;

} l

/* This routine will initialize a context for a given context_id (a users pollfd array address). It presumes that the poll ishim_global_mutex is locked and unlocked by the caller. Generation counter is initialized, eventport ?ledescriptor is opened, and context speci?c mutex is initialized. Set the users pollfd-baseintr to the context_id and return. If error occurs, cleanup and return a failure message and the caller can decide what to do, e.g., fallback on the "real" poll call. */

int

poll#shim_context_initialize(struct shim_context *context, void *context_id) {

int ret;

pthreadjnutexattr?t attr;

if ((ret = pthread_mutexattr_init(&attr)) == 0) {

Page 18: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 13

/* Keep going */

if ((ret : pthread_mutex_init(&(context~>shim?context~mutex), &attr)) I: 0) {

/* Keep going */

if((eontext->eventport = opcn("/dev/poll“, O;RDWR)) >= 0) {

/* Keep going, none of the rest of the stuff can "fail" */

pollffd_context_initialize(context->shim_fd_contexts); context->generation = 0;

context->users_pollfd_base_ptr = context_id;

return(0); } else {

/* The open of/dev/poll failed */

else { /* The pthread_mutex_init failed */

l } else {

/* The pthread gmutexattr_init failed */

} return(l);

/* Returns a pointer to poll_shim_context struct associated with context_id. If contcxt_id is not found and there is room in the context store, a context is

initialized. If the context is not found, and there is no room in the context store, NULL is returned, Upon return of a non-NULL pointer, the mutex for the poll_shim_context will be locked. This routine will lock the global_shim_mutex while it performs the search and also while a newly allocated context is initialized. This should be OK so long as stuff like opening an event port does not take too much time. The routine will unlock the global_shim_mutex before it returns.

If any unrecoverable errors are encountered, this routine will clean up as best it can and will return a value of NULL. At that point it will be up to the caller to decide Whether or not it wishes to abort/exit or simply fallback to the "real" poll() call. */

struct shim_context * poll_shim_?nd_eontext(void *context_id) { int i; int ret; struct shim‘context *foundgcontext = WLL;

Page 19: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 14

if ((ret = pthread_mutex?lock(&poll_shim_global_mutex)) != O) {

/*Error, time to punt. */

fprintf(stderr, npollshim: unable to lock poll_shim_global_mutex, ermo %d, exiting!\n", ret);

f?ush(stderr); exit(-l);

}

/* We only initialize contexts, we never "free" them, so the unallocated entries in the array (users _pollfd_base _ptr == NULL) should always be at the end of the array, and if we get to one, it means there is going to be no match with an existing entry. */

i = 0;

while ((i < POLLgSHIMQMAXiCONTEXTS) && (f0und_context :: NULL)) {

if (poll Vshimi eontexts[i].users_pollfd_base_ptr >2 NULL) {

/* Should usually not have a NULL user _pollfd_base _ptr before the match to the supplied contextgid is found, so ?nding a NULL means that there is no context for that context_id. */

if ((ret :

poll_shim_context_initialize(&(poll_shim_contexts[i]), context~id)) == 0) {

/* All was well */

found_context = &(p0ll?shim_contexts[i]);

else {

/* The call to p0ll_shim_context_initialiZe failed */

/* At this point, we either found and initialized a new context, or that failed. there is no point in continuing in the while loop, because if the init failure were transient we might try to initialize the next NULL entry and then actually have a NULL entry separating valid entries. That would be bad. */

break; } else {

/* The users _poll_fdgbasc _ptr was not null. Does it perhaps match the c0ntext_id for which We are looking? */

Page 20: Method for increasing performance of select and poll applications ...

US 2004/0031044 A1 Feb. 12, 2004 15

if (poll_shim_contexts[i].users_pollfd_base_ptr == context’ id) { /* We have a match */

found_context I &(p0ll_shim_contexts[i]);

else {

/* There was no match, try the next entry */

/* Once we get to this point, either we have a valid n0n—NULL shim_context, or it is NULL. Time to unlock the global mutex and return. However, if our context is non NULL, we have to remember to lock the context mutex ?rst. */

if (found_context !: NULL) { . if ((i : pthread_mutex_lock(&found_context—>shim_context_mutex)) !: O) {

/* Error, time to punt. >“/

fprintf(stderr, "pollshim: unable to lock context mutex, ermo %d, exiting!\n", i);

fllush(stderr); exit(-l);

} }

if ((i T pthread_mutex_unlock(&poll_shim_global_mutex)) l: 0)

/* Error, time to punt. */

fprintf(stderr, "pollshim: unable to unlock poll_shim_global_mutex, ermo %d, exitingl\n", i);

fflush(stderr);

return(found_context);

}

SECTION 3:

/* This routine presumes that the mutex protecting the shim_c0ntcxt struct is already locked by the caller. We walk the list of pollfds, and see what events are of interest.

Page 21: Method for increasing performance of select and poll applications ...
Page 22: Method for increasing performance of select and poll applications ...
Page 23: Method for increasing performance of select and poll applications ...
Page 24: Method for increasing performance of select and poll applications ...
Page 25: Method for increasing performance of select and poll applications ...
Page 26: Method for increasing performance of select and poll applications ...
Page 27: Method for increasing performance of select and poll applications ...
Page 28: Method for increasing performance of select and poll applications ...
Page 29: Method for increasing performance of select and poll applications ...
Page 30: Method for increasing performance of select and poll applications ...
Page 31: Method for increasing performance of select and poll applications ...
Page 32: Method for increasing performance of select and poll applications ...
Page 33: Method for increasing performance of select and poll applications ...
Page 34: Method for increasing performance of select and poll applications ...
Page 35: Method for increasing performance of select and poll applications ...
Page 36: Method for increasing performance of select and poll applications ...
Page 37: Method for increasing performance of select and poll applications ...
Page 38: Method for increasing performance of select and poll applications ...
Page 39: Method for increasing performance of select and poll applications ...
Page 40: Method for increasing performance of select and poll applications ...
Page 41: Method for increasing performance of select and poll applications ...
Page 42: Method for increasing performance of select and poll applications ...
Page 43: Method for increasing performance of select and poll applications ...
Page 44: Method for increasing performance of select and poll applications ...
Page 45: Method for increasing performance of select and poll applications ...
Page 46: Method for increasing performance of select and poll applications ...
Page 47: Method for increasing performance of select and poll applications ...
Page 48: Method for increasing performance of select and poll applications ...
Page 49: Method for increasing performance of select and poll applications ...
Page 50: Method for increasing performance of select and poll applications ...
Page 51: Method for increasing performance of select and poll applications ...
Page 52: Method for increasing performance of select and poll applications ...
Page 53: Method for increasing performance of select and poll applications ...
Page 54: Method for increasing performance of select and poll applications ...
Page 55: Method for increasing performance of select and poll applications ...
Page 56: Method for increasing performance of select and poll applications ...
Page 57: Method for increasing performance of select and poll applications ...
Page 58: Method for increasing performance of select and poll applications ...

Recommended