| 1 | /** @file | 
|---|
| 2 | * | 
|---|
| 3 | * Testcase for bug #1023 autoincrement of file handles. | 
|---|
| 4 | * | 
|---|
| 5 | * InnoTek Systemberatung GmbH confidential | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (c) 2004 InnoTek Systemberatung GmbH | 
|---|
| 8 | * Author: knut st. osmundsen <bird-srcspam@anduin.net> | 
|---|
| 9 | * | 
|---|
| 10 | * All Rights Reserved | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | /******************************************************************************* | 
|---|
| 15 | *   Header Files                                                               * | 
|---|
| 16 | *******************************************************************************/ | 
|---|
| 17 | #define INCL_DOS | 
|---|
| 18 | #define INCL_DOSINFOSEG | 
|---|
| 19 | #ifdef __IBMC__ | 
|---|
| 20 | #include "../src/emx/include/os2emx.h" | 
|---|
| 21 | #else | 
|---|
| 22 | #include <os2.h> | 
|---|
| 23 | #endif | 
|---|
| 24 | #include <stdio.h> | 
|---|
| 25 | #include <io.h> | 
|---|
| 26 | #include <fcntl.h> | 
|---|
| 27 | #include <string.h> | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | int main(int argc, const char **argv) | 
|---|
| 32 | { | 
|---|
| 33 | int             i,j; | 
|---|
| 34 | LONG            lFHDelta = 0; | 
|---|
| 35 | ULONG           cFHs = 0; | 
|---|
| 36 | ULONG           cFHLast; | 
|---|
| 37 | unsigned        msLastInc; | 
|---|
| 38 | unsigned        msEnd; | 
|---|
| 39 | unsigned        msStart; | 
|---|
| 40 | unsigned        msTotal = 0; | 
|---|
| 41 | unsigned        cFiles = 0; | 
|---|
| 42 | unsigned        cIncrements = 0; | 
|---|
| 43 | int             fStreams; | 
|---|
| 44 | PGINFOSEG       pGIS = GETGINFOSEG(); | 
|---|
| 45 | #define MAX_FHS 10000 | 
|---|
| 46 | static FILE    *apFiles[MAX_FHS]; | 
|---|
| 47 | static int      aiFiles[MAX_FHS]; | 
|---|
| 48 |  | 
|---|
| 49 | /* | 
|---|
| 50 | * Init | 
|---|
| 51 | */ | 
|---|
| 52 | fStreams = argc >= 2; | 
|---|
| 53 |  | 
|---|
| 54 | DosSetRelMaxFH(&lFHDelta, &cFHs); | 
|---|
| 55 | printf("1023-maxfilehandles: starting at cFHs=%d; testing %s.\n", cFHs, fStreams ? "streams" : "io"); | 
|---|
| 56 |  | 
|---|
| 57 | /* | 
|---|
| 58 | * Open files. | 
|---|
| 59 | */ | 
|---|
| 60 | cFHLast = cFHs; | 
|---|
| 61 | msLastInc = pGIS->msecs; | 
|---|
| 62 | for (i = 0; i < MAX_FHS; i++) | 
|---|
| 63 | { | 
|---|
| 64 | msStart = pGIS->msecs; | 
|---|
| 65 | if (fStreams) | 
|---|
| 66 | apFiles[i] = fopen(argv[argc - 1], "rb"); | 
|---|
| 67 | else | 
|---|
| 68 | aiFiles[i] = open(argv[argc - 1], O_BINARY | O_RDONLY); | 
|---|
| 69 | msEnd = pGIS->msecs; | 
|---|
| 70 | if (fStreams ? apFiles[i] == NULL : aiFiles[i] < 0) | 
|---|
| 71 | { | 
|---|
| 72 | printf("error %d!\n", i + 1); | 
|---|
| 73 | break; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | lFHDelta = cFHs = 0; | 
|---|
| 77 | DosSetRelMaxFH(&lFHDelta, &cFHs); | 
|---|
| 78 | lFHDelta = cFHs = 0; | 
|---|
| 79 | DosSetRelMaxFH(&lFHDelta, &cFHs); | 
|---|
| 80 | if (cFHs != cFHLast) | 
|---|
| 81 | { | 
|---|
| 82 | printf("Max FH change %i (fh=%d): %d -> %d (inc: %d ms  since last: %d ms)\n", | 
|---|
| 83 | i + 1, fStreams ? fileno(apFiles[i]) : aiFiles[i], | 
|---|
| 84 | cFHLast, cFHs, msEnd - msStart, msStart - msLastInc); | 
|---|
| 85 | /* stats */ | 
|---|
| 86 | cIncrements++; | 
|---|
| 87 | msTotal += msEnd - msLastInc; | 
|---|
| 88 | msEnd = msLastInc = pGIS->msecs; | 
|---|
| 89 | } | 
|---|
| 90 | cFHLast = cFHs; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | msTotal += msEnd - msLastInc; | 
|---|
| 94 | if (!msTotal) | 
|---|
| 95 | msTotal++; | 
|---|
| 96 | cFiles = i; | 
|---|
| 97 |  | 
|---|
| 98 | lFHDelta = cFHs = 0; | 
|---|
| 99 | DosSetRelMaxFH(&lFHDelta, &cFHs); | 
|---|
| 100 | printf("cFHs=%d\n", cFHs); | 
|---|
| 101 | printf("msTotal=%d  cIncrements=%d  cFiles=%d  i.e. %d handles per second (%s)\n", | 
|---|
| 102 | msTotal, cIncrements, cFiles, cFiles * 1000 / msTotal, fStreams ? "streams" : "io"); | 
|---|
| 103 |  | 
|---|
| 104 | /* | 
|---|
| 105 | * Free the files. | 
|---|
| 106 | */ | 
|---|
| 107 | msStart = pGIS->msecs; | 
|---|
| 108 | if (fStreams) | 
|---|
| 109 | { | 
|---|
| 110 | for (j = 0; j < i; j++) | 
|---|
| 111 | fclose(apFiles[j]); | 
|---|
| 112 | } | 
|---|
| 113 | else | 
|---|
| 114 | { | 
|---|
| 115 | for (j = 0; j < i; j++) | 
|---|
| 116 | close(aiFiles[j]); | 
|---|
| 117 | } | 
|---|
| 118 | msEnd = pGIS->msecs; | 
|---|
| 119 | msTotal = msEnd - msStart; | 
|---|
| 120 | if (!msTotal) | 
|---|
| 121 | msTotal++; | 
|---|
| 122 | printf("close of %d files took %d ms  i.e. %d handles per seconds\n", | 
|---|
| 123 | j, msTotal, cFiles * 1000 / msTotal); | 
|---|
| 124 |  | 
|---|
| 125 | /* | 
|---|
| 126 | * Report the result. | 
|---|
| 127 | */ | 
|---|
| 128 | if (i < 9900) | 
|---|
| 129 | { | 
|---|
| 130 | printf("1023-maxfilehandles: failed, could only open %d handles, expected > 9900\n", i); | 
|---|
| 131 | return 1; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | printf("1023-maxfilehandles: succeeded opening %d handles.\n", i); | 
|---|
| 135 | return 0; | 
|---|
| 136 | } | 
|---|