windows-tests-torture-enforce-querypath-info-errors-20090201
[openafs.git] / src / WINNT / tests / torture / Source / output.c
1 #include <windows.h>
2 #include "includes.h"
3 #include "common.h"
4
5 extern int  ChronLog;
6 extern int  CurrentLoop;
7 extern HANDLE ChronMutexHandle;
8 extern HANDLE MutexHandle;
9 extern HANDLE FileMutexHandle;
10
11 void LogStats(char *FileName, int ToLog, int Iteration, int NumberOfProcesses, int NumberOfThreads, 
12               char *HostName, int ProcessNumber, struct cmd_struct CommandInfo[],
13               char *CommandLine, char *TargetDirectory)
14 {
15     struct cmd_struct TotalCommandInfo[CMD_MAX_CMD + 1];
16     int         i;
17     int         j;
18     int         LineCount;
19     unsigned    grand_total = 0;
20     char        AsciiTime[32];
21     FILE        *fp;
22     struct tm   *newtime;
23     time_t      aclock;
24
25     LineCount = 1;
26     time(&aclock);
27     newtime = localtime(&aclock);
28     strcpy(AsciiTime, asctime(newtime));
29     AsciiTime[strlen(AsciiTime) - 1] = '\0';
30
31     fp = stdout;
32     if (ToLog)
33         fp = fopen(FileName, "a+");
34
35     for (j = 0; j <= CMD_MAX_CMD; j++)
36     {
37         TotalCommandInfo[j].count = 0;
38         TotalCommandInfo[j].min_sec = 0;
39         TotalCommandInfo[j].ErrorTime = 0;
40         TotalCommandInfo[j].max_sec = 0;
41         TotalCommandInfo[j].MilliSeconds = 0;
42         TotalCommandInfo[j].total_sec = 0;
43         TotalCommandInfo[j].total_sum_of_squares = 0;
44         TotalCommandInfo[j].ErrorCount = 0;
45         TotalCommandInfo[j].ErrorTime = 0;
46     }
47
48     for (i = 0; i <= CMD_MAX_CMD; i++)
49     {
50         TotalCommandInfo[i].count += CommandInfo[i].count;
51         TotalCommandInfo[i].total_sec += CommandInfo[i].total_sec; 
52         TotalCommandInfo[i].ErrorCount += CommandInfo[i].ErrorCount; 
53         TotalCommandInfo[i].ErrorTime += CommandInfo[i].ErrorTime; 
54         grand_total += CommandInfo[i].total_sec;
55         if (!TotalCommandInfo[i].min_sec || (TotalCommandInfo[i].min_sec > CommandInfo[i].min_sec))
56             TotalCommandInfo[i].min_sec = CommandInfo[i].min_sec;
57         if (TotalCommandInfo[i].min_sec == 1000)
58             TotalCommandInfo[i].min_sec = 0;
59         if (TotalCommandInfo[i].max_sec < CommandInfo[i].max_sec)
60             TotalCommandInfo[i].max_sec = CommandInfo[i].max_sec;
61     }
62     if (ProcessNumber > -1)
63     {
64         fprintf(fp, "Iteration %d Statistics\n", Iteration);
65         ++LineCount;
66     }
67     else if (ProcessNumber == -1)
68     {
69         fprintf(fp, "Process Statistics for Iteration %d\n", Iteration);
70         ++LineCount;
71     }
72     else if (ProcessNumber == -2)
73     {
74         fprintf(fp, "Test Statistics for all processes - %d Iterations \n", Iteration);
75         ++LineCount;
76     }
77     fprintf(fp, "Date: %s\n", AsciiTime);
78     ++LineCount;
79     fprintf(fp, "Total Time: %d seconds (%4.1f minutes)\n", grand_total, grand_total/60.0);
80     ++LineCount;
81     if ((ProcessNumber == -2) || (ProcessNumber == -1))
82     {
83         fprintf(fp, "Number of Processes: %d\n", NumberOfProcesses);
84         fprintf(fp, "Number of Threads/Process: %d\n", NumberOfThreads);
85         ++LineCount;
86         ++LineCount;
87     }
88
89     if (strlen(TargetDirectory) != 0)
90     {
91         fprintf(fp, "Target directory: %s\n", TargetDirectory);
92         ++LineCount;
93     }
94     if (strlen(CommandLine) != 0)
95     {
96         fprintf(fp, "Command Line: %s\n", CommandLine);
97         ++LineCount;
98     }
99     fprintf(fp, "\n  Command:                          Count  Min Latency Max Latency Ave Latency    Cost      Seconds     Error     Error  \n");
100     fprintf(fp, "                                            (seconds)   (seconds)   (seconds)                           Count    seconds\n");
101     ++LineCount;
102     ++LineCount;
103
104     for (i = LineCount; i < 12; i++)
105         fprintf(fp, "\n");
106
107     for (i = 0; i <= CMD_MAX_CMD; i++)
108     {
109         fprintf(fp, "%-30s   %8d    %8d  %10d  %10.2f   %5.1f%%   %10d  %8d  %8d  %s\n",
110                 cmd_names[i].name, 
111                 TotalCommandInfo[i].count, 
112                 TotalCommandInfo[i].min_sec,
113                 TotalCommandInfo[i].max_sec,
114                 (TotalCommandInfo[i].count ? (float)TotalCommandInfo[i].total_sec/(float)TotalCommandInfo[i].count : 0),
115                 (grand_total ? 100.0*TotalCommandInfo[i].total_sec/grand_total : 0),
116                 TotalCommandInfo[i].total_sec,
117                 TotalCommandInfo[i].ErrorCount,
118                 TotalCommandInfo[i].ErrorTime,
119                 cmd_names[i].ms_api);
120     }
121     fprintf(fp, "\n");
122     if (ToLog)
123         fclose(fp);
124 }
125
126 void LogMessage(int ProcessNumber, char *HostName, char *FileName, char *message, int LogID)
127 {
128     char        NewMessage[1024];
129     char        AsciiTime[32];
130     char        NewFileName[256];
131     char        WorkingDirectory[512];
132     FILE        *fp;
133     struct tm   *newtime;
134     time_t      aclock;
135     DWORD       rc;
136
137     if (ChronLog)
138     {
139         while(1)
140         {
141             rc = WaitForSingleObject(ChronMutexHandle, 4 * 1000);
142             if ((rc == WAIT_OBJECT_0) || (rc == WAIT_TIMEOUT))
143                 break;
144         }
145     }
146     time(&aclock);
147     newtime = localtime(&aclock);
148     strcpy(AsciiTime, asctime(newtime));
149     AsciiTime[strlen(AsciiTime) - 1] = '\0';
150     GetCurrentDirectory(sizeof(WorkingDirectory), WorkingDirectory);
151     sprintf(NewMessage, "%s - %s", AsciiTime, message);
152     sprintf(NewFileName, "%s\\log%05d\\%s\\%s", WorkingDirectory, LogID, HostName, FileName);
153     fp = fopen(NewFileName, "a+");
154     if (fp != NULL)
155     {
156         fprintf(fp, "%s", NewMessage);
157         fclose(fp);
158     }
159     if (ChronLog)
160     {
161         sprintf(NewMessage, "%s %s:%d- %s", AsciiTime, HostName, ProcessNumber, message);
162         sprintf(NewFileName, "%s\\log%05d\\Chron.log", WorkingDirectory, LogID);
163         fp = fopen(NewFileName, "a+");
164         fprintf(fp, "%s", NewMessage);
165         fclose(fp);
166         ReleaseMutex(ChronMutexHandle);
167     }
168 }
169
170 void DumpAFSLog(char *HostName, int LogID)
171 {
172     char    EnvVariable[512];
173     char    WorkingDirectory[512];
174     char    command[512];
175     int     rc;
176
177     WaitForSingleObject(FileMutexHandle, 4 * 1000);
178     memset(WorkingDirectory, '\0', sizeof(WorkingDirectory));
179     GetCurrentDirectory(sizeof(WorkingDirectory), WorkingDirectory);
180     sprintf(command, "fs trace -dump > .\\test\\%s", HostName);
181     rc = system(command);
182     rc = GetWindowsDirectory(EnvVariable, sizeof(EnvVariable));
183     strcat(EnvVariable, "\\TEMP\\afsd.log");
184     sprintf(command, "move %s %s\\log%05d\\%s > .\\test\\%s", EnvVariable, WorkingDirectory, LogID, HostName, HostName);
185     rc = system(command);
186     sprintf(command, "rename %s\\log%05d\\%s\\afsd.log afsd_%s_iteration%d.log", 
187             WorkingDirectory, LogID, HostName, HostName, CurrentLoop);
188     rc = system(command);
189     ReleaseMutex(FileMutexHandle);
190 }
191
192 int UpdateMasterLog(char *FileName, struct cmd_struct CommandInfo[])
193 {
194     FILE    *fp;
195     char    Buffer[32];
196     int     i;
197     struct cmd_struct TotalCommandInfo[CMD_MAX_CMD + 1];
198
199     WaitForSingleObject(FileMutexHandle, 4 * 1000);
200
201     for (i = 0; i <= CMD_MAX_CMD; i++)
202     {
203         TotalCommandInfo[i].count = 0;
204         TotalCommandInfo[i].min_sec = 0;
205         TotalCommandInfo[i].max_sec = 0;
206         TotalCommandInfo[i].MilliSeconds = 0;
207         TotalCommandInfo[i].total_sec = 0;
208         TotalCommandInfo[i].ErrorCount = 0;
209         TotalCommandInfo[i].ErrorTime = 0;
210         TotalCommandInfo[i].total_sum_of_squares = 0;
211     }
212
213     fp = fopen(FileName, "r");
214     if (fp != NULL)
215     {
216         for (i = 0; i <= CMD_MAX_CMD; i++)
217         {
218             fgets(Buffer, 16, fp);
219             TotalCommandInfo[i].count = atoi(Buffer);
220             fgets(Buffer, 16, fp);
221             TotalCommandInfo[i].min_sec = atoi(Buffer);
222             fgets(Buffer, 16, fp);
223             TotalCommandInfo[i].max_sec = atoi(Buffer);
224             fgets(Buffer, 16, fp);
225             TotalCommandInfo[i].total_sec = atoi(Buffer);
226             fgets(Buffer, 16, fp);
227             TotalCommandInfo[i].total_sum_of_squares = atoi(Buffer);
228             fgets(Buffer, 16, fp);
229             TotalCommandInfo[i].ErrorCount = atoi(Buffer);
230             fgets(Buffer, 16, fp);
231             TotalCommandInfo[i].ErrorTime = atoi(Buffer);
232         }
233         fclose(fp);
234     }
235
236     for (i = 0; i <= CMD_MAX_CMD; i++)
237     {
238         TotalCommandInfo[i].count += CommandInfo[i].count;
239         if (!TotalCommandInfo[i].min_sec || (TotalCommandInfo[i].min_sec > CommandInfo[i].min_sec))
240             TotalCommandInfo[i].min_sec = CommandInfo[i].min_sec;
241         if (TotalCommandInfo[i].max_sec < CommandInfo[i].max_sec)
242             TotalCommandInfo[i].max_sec = CommandInfo[i].max_sec;
243         TotalCommandInfo[i].total_sec += CommandInfo[i].total_sec;
244         TotalCommandInfo[i].total_sum_of_squares += CommandInfo[i].total_sum_of_squares;
245         TotalCommandInfo[i].ErrorCount += CommandInfo[i].ErrorCount;
246         TotalCommandInfo[i].ErrorTime += CommandInfo[i].ErrorTime;
247     }
248
249
250     fp = fopen(FileName, "w");
251     for (i = 0; i <= CMD_MAX_CMD; i++)
252     {
253         fprintf(fp, "%ld\n", TotalCommandInfo[i].count);
254         fprintf(fp, "%ld\n", TotalCommandInfo[i].min_sec);
255         fprintf(fp, "%ld\n", TotalCommandInfo[i].max_sec);
256         fprintf(fp, "%ld\n", TotalCommandInfo[i].total_sec);
257         fprintf(fp, "%ld\n", TotalCommandInfo[i].total_sum_of_squares);
258         fprintf(fp, "%ld\n", TotalCommandInfo[i].ErrorCount);
259         fprintf(fp, "%ld\n", TotalCommandInfo[i].ErrorTime);
260     }
261     fclose(fp);
262     ReleaseMutex(FileMutexHandle);
263     return(0);
264 }
265
266 int BuildMasterStatLog(char *FileName, char*MoveFileName, int NumberOfProcesses, 
267                        int NumberOfThreads, char *CommandLine, int Iterations, 
268                        char *TargetDirectory, int ProcessNumber)
269 {
270     FILE    *fp;
271     char    Buffer[32];
272     int     i;
273     struct cmd_struct TotalCommandInfo[CMD_MAX_CMD + 1];
274
275     for (i = 0; i <= CMD_MAX_CMD; i++)
276     {
277         TotalCommandInfo[i].count = 0;
278         TotalCommandInfo[i].min_sec = 0;
279         TotalCommandInfo[i].MilliSeconds = 0;
280         TotalCommandInfo[i].max_sec = 0;
281         TotalCommandInfo[i].total_sec = 0;
282         TotalCommandInfo[i].total_sum_of_squares = 0;
283         TotalCommandInfo[i].ErrorCount = 0;
284         TotalCommandInfo[i].ErrorTime = 0;
285     }
286
287     fp = fopen(FileName, "r");
288     if (fp != NULL)
289     {
290         for (i = 0; i <= CMD_MAX_CMD; i++)
291         {
292             if (fgets(Buffer, 16, fp) != NULL)
293                 TotalCommandInfo[i].count = atoi(Buffer);
294             if (fgets(Buffer, 16, fp) != NULL)
295                 TotalCommandInfo[i].min_sec = atoi(Buffer);
296             if (fgets(Buffer, 16, fp) != NULL)
297                 TotalCommandInfo[i].max_sec = atoi(Buffer);
298             if (fgets(Buffer, 16, fp) != NULL)
299                 TotalCommandInfo[i].total_sec = atoi(Buffer);
300             if (fgets(Buffer, 16, fp) != NULL)
301                 TotalCommandInfo[i].total_sum_of_squares = atoi(Buffer);
302             if (fgets(Buffer, 16, fp) != NULL)
303                 TotalCommandInfo[i].ErrorCount = atoi(Buffer);
304             if (fgets(Buffer, 16, fp) != NULL)
305                 TotalCommandInfo[i].ErrorTime = atoi(Buffer);
306         }
307         fclose(fp);
308         MoveFile(FileName, MoveFileName);
309     }
310     
311     fp = fopen(FileName, "w");
312     fclose(fp);
313     LogStats(FileName, 1, Iterations, NumberOfProcesses, NumberOfThreads, "", ProcessNumber, TotalCommandInfo, 
314              CommandLine, TargetDirectory);
315     return(0);
316 }