ff0888c5bfd020340fb4bc3e3d5e6e3b30f7a79c
[openafs.git] / src / WINNT / tests / torture / DumpAfsLog / DumpAfsLog.c
1 // Stress.cpp : Defines the entry point for the console application.
2 //
3
4 #include <windows.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <stdio.h>
8 #include <io.h>
9
10 void usage(void);
11 int  GetConsoleInput(HANDLE hStdin);
12
13 int main(int argc, char* argv[])
14 {
15     int     i;
16     int     Count;
17     int     SecondsDelay;
18     int     SecondsToRun;
19     int     NewSessionDeadlock;
20     int     NewSessionDeadlockCount;
21     int     SecondsToDelay;
22     int     MiniDump;
23     int     rc;
24     char    HostName[512];
25     char    LogName[512];
26     char    command[512];
27     char    WorkingDirectory[512];
28     char    LoggingDrive[512];
29     char    EnvVariable[512];
30 //    char    Buffer[512];
31 //    char    FileName[32];
32 //    char    *pPtr;
33     time_t  StartTime;
34 //    FILE    *fp;
35     HANDLE  hStdin; 
36
37
38     Count = 0;
39     SecondsDelay = 15;
40     NewSessionDeadlock = 0;
41     SecondsToRun = 30 * 60;
42     memset(HostName, '\0', sizeof(HostName));
43     memset(command, '\0', sizeof(command));
44     strcpy(LoggingDrive, "C");
45     MiniDump = 0;
46
47     for (i = 0; i < argc; i++)
48     {
49         if (!stricmp(argv[i], "-e"))
50         {
51             MiniDump = 1;
52         }
53         if (!stricmp(argv[i], "-s"))
54         {
55             SecondsDelay = atoi(argv[i+1]);
56         }
57         if (!stricmp(argv[i], "-h"))
58         {
59             strcpy(HostName, argv[i+1]);
60         }
61         if (!stricmp(argv[i], "-d"))
62         {
63             strcpy(LoggingDrive, argv[i+1]);
64             if (strlen(LoggingDrive) == 1)
65                 strcat(LoggingDrive,":");
66         }
67         if (!stricmp(argv[i], "-m"))
68         {
69             SecondsToRun = atoi(argv[i+1]) * 60;
70         }
71         if (!stricmp(argv[i], "-n"))
72         {
73             NewSessionDeadlock = 1;
74             NewSessionDeadlockCount = atoi(argv[i+1]);
75             if (NewSessionDeadlockCount == 0)
76                 NewSessionDeadlockCount = 20;
77         }
78         if (!stricmp(argv[i], "-?") || !stricmp(argv[i], "/?") || 
79             !stricmp(argv[i], "?") || !stricmp(argv[i], "help"))
80         {
81              usage();
82              exit(1);
83         }
84     }
85
86     hStdin = GetStdHandle(STD_INPUT_HANDLE);
87
88     sprintf(command, "fs trace -on");
89     rc = system(command);
90     sprintf(command, "fs trace -reset");
91     rc = system(command);
92
93     GetCurrentDirectory(sizeof(WorkingDirectory), WorkingDirectory);
94     if (WorkingDirectory[0] != LoggingDrive[0])
95         WorkingDirectory[0] = LoggingDrive[0];
96     sprintf(command, "rmdir /Q /S %s\\DumpAfsLogDir", WorkingDirectory);
97     rc = system(command);
98     sprintf(LogName, "%s\\DumpAfsLogDir", WorkingDirectory);
99     sprintf(command, "mkdir %s\\DumpAfsLogDir", WorkingDirectory);
100     rc = system(command);
101
102     time(&StartTime);
103     SecondsToRun += StartTime;
104     while (1)
105     {
106         if (MiniDump)
107         {
108             printf("\n");
109             sprintf(command, "fs minidump");
110             rc = system(command);
111             ExpandEnvironmentStrings("%windir%", EnvVariable, sizeof(EnvVariable));
112             strcat(EnvVariable, "\\TEMP\\afsd.dmp");
113             sprintf(command, "copy /Y %s %s\\DumpAfsLogDir", EnvVariable, WorkingDirectory);
114             printf("%s\n", command);
115             rc = system(command);
116             sprintf(command, "rename %s\\DumpAfsLogDir\\afsd.dmp afsd_%05d.dmp", WorkingDirectory, Count);
117             printf("%s\n", command);
118             rc = system(command);
119         }
120
121         printf("\n");
122         sprintf(command, "fs trace -dump");
123         rc = system(command);
124         ExpandEnvironmentStrings("%windir%", EnvVariable, sizeof(EnvVariable));
125         strcat(EnvVariable, "\\TEMP\\afsd.log");
126         sprintf(command, "copy /Y %s %s\\DumpAfsLogDir", EnvVariable, WorkingDirectory);
127         printf("%s\n", command);
128         rc = system(command);
129 //        sprintf(command, "crlf.pl -d %s\\DumpAfsLogDir\\afsd.log", WorkingDirectory);
130 //        printf("%s\n", command);
131 //        rc = system(command);
132         sprintf(command, "rename %s\\DumpAfsLogDir\\afsd.log afsd_%05d.log", WorkingDirectory, Count);
133         printf("%s\n\n", command);
134         rc = system(command);
135 /*
136         if (strlen(HostName) != 0)
137         {
138             sprintf(command, "cmdebug %s -long | grep refcnt > %s\\DumpAfsLogDir\\refcnt_%05d.log",
139                     HostName, WorkingDirectory, Count);
140             printf("%s\n", command);
141             rc = system(command);
142         }
143         sprintf(FileName, "%s\\DumpAfsLogDir\\afsd_%05d.log", WorkingDirectory, Count);
144
145         if (NewSessionDeadlock)
146         {
147             fp = fopen(FileName, "r");
148             if (fp != NULL)
149             {
150                 while (fgets(Buffer, 512, fp))
151                 {
152 //                  if (pPtr = strstr(Buffer, "RecordRacingRevoke"))
153 //                  {
154 //                      if (pPtr = strstr(Buffer, "activeCalls "))
155 //                      {
156 //                          pPtr += strlen("activeCalls ");
157 //                          if (atoi(pPtr) > 0)
158 //                          {
159 //                              SetEvent(ShutDownEventHandle);
160 //                              break;
161 //                          }
162 //                      }
163 //                  }
164                     if (pPtr = strstr(Buffer, "New Session lsn "))
165                     {
166                         printf("%s", Buffer);
167                         pPtr += strlen("New Session lsn "   );
168                         if (atoi(pPtr) > NewSessionDeadlockCount)
169                         {
170                             break;
171                         }
172                     }
173 //                  if (pPtr = strstr(Buffer, "Racing revoke scp"))
174 //                  {
175 //                      SetEvent(ShutDownEventHandle);
176 //                      break;
177 //                  }
178                 }
179
180             }
181             fclose(fp);
182         }
183 */
184         ++Count;
185         time(&StartTime);
186         if (StartTime > (time_t)SecondsToRun)
187             break;
188         printf("Type Q to stop DumpAfsLog\n");
189         SecondsToDelay = StartTime + SecondsDelay;
190         while (1)
191         {
192             time(&StartTime);
193             if (StartTime > (time_t)SecondsToDelay)
194                 break;
195             rc = GetConsoleInput(hStdin);
196             Sleep(500);
197         }
198         rc = GetConsoleInput(hStdin);
199     }
200
201     sprintf(command, "fs trace -off");
202     rc = system(command);
203         return(0);
204
205 }
206
207 void usage(void)
208 {
209
210     printf("usage: wintorture [options]\n");
211     printf("where options can be:\n");
212     printf("\t-d <drive> Local drive letter where to place log files\n");
213     printf("\t-e         Enable fs minidump\n");
214     printf("\t-h <host>  Local host name.  Used to run cmdebug\n");
215     printf("\t-m <min>   Number of minutes to run this program\n");
216 //    printf("\t-n <num>  Stop processing on \"New Session lsn XX\" deadlock\n");
217     printf("\t-s <sec>   Seconds delay between dumping AFS logs\n");
218     printf("\t            15 seconds delay is the default\n");
219 }
220
221 int GetConsoleInput(HANDLE hStdin)
222 {
223     INPUT_RECORD    InputRecord[128];
224     DWORD           InputRecordCount;
225     BOOL            rc;
226     int             i;
227     int             RetCode;
228     char            ReadChar;
229
230     InputRecordCount = 0;
231     RetCode = 0;
232     if (!(rc = PeekConsoleInput(hStdin, InputRecord, 128, &InputRecordCount)))
233         return(0);
234     if (InputRecordCount == 0)
235         return(0);
236     rc = ReadConsoleInput(hStdin, InputRecord, 128, &InputRecordCount);
237     for (i = 0; i < (int)InputRecordCount; i++) 
238     {
239         switch(InputRecord[i].EventType) 
240         { 
241             case KEY_EVENT:
242                 if (InputRecord[i].Event.KeyEvent.bKeyDown)
243                 {
244                     ReadChar = InputRecord[i].Event.KeyEvent.uChar.AsciiChar;
245                     if ((ReadChar == 'q') || (ReadChar == 'Q'))
246                     {
247                         printf("Stop request received\n");
248                         exit(0);
249                     }
250                 }
251                 break; 
252             default: 
253                 break; 
254         }
255     }
256     return(RetCode);
257 }
258