1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /* Allow the printf's that describe the contents of the file.  Error
7    messages use the printf routines */
8 /* style: allow:printf:9 sig:0 */
9 
10 #include "rlog.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 
15 #define HEADER_BIT   0x01
16 #define STATE_BIT    0x02
17 #define COMM_BIT     0x04
18 #define ARROW_BIT    0x08
19 #define EVENT_BIT    0x10
20 
PrintState(RLOG_STATE * pState)21 void PrintState(RLOG_STATE *pState)
22 {
23     printf("RLOG_STATE -");
24     printf(" id: %3d, %12s : %s\n",
25 	pState->event,
26 	pState->color,
27 	pState->description);
28 }
29 
PrintEvent(RLOG_EVENT * pEvent)30 void PrintEvent(RLOG_EVENT *pEvent)
31 {
32     printf("RLOG_EVENT -");
33     printf(" %3d:%04d, start: %4.07g, end: %4.07g, %d\n",
34 	pEvent->rank,
35 	pEvent->event,
36 	pEvent->start_time,
37 	pEvent->end_time,
38 	pEvent->recursion);
39 }
40 
PrintEventAndIndex(RLOG_EVENT * pEvent,int index)41 void PrintEventAndIndex(RLOG_EVENT *pEvent, int index)
42 {
43     printf("RLOG_EVENT -");
44     printf(" %3d:%04d, start: % 10f, end: % 10f, %d, #%d\n",
45 	pEvent->rank,
46 	pEvent->event,
47 	pEvent->start_time,
48 	pEvent->end_time,
49 	pEvent->recursion,
50 	index);
51 }
52 
PrintArrow(RLOG_ARROW * pArrow)53 void PrintArrow(RLOG_ARROW *pArrow)
54 {
55     printf("RLOG_ARROW -");
56     printf(" %2d -> %2d, %s tag:%3d, len: %d, start: %g, end: %g\n",
57 	pArrow->src,
58 	pArrow->dest,
59 	(pArrow->leftright == RLOG_ARROW_RIGHT) ? "RIGHT" : "LEFT ",
60 	pArrow->tag,
61 	pArrow->length,
62 	pArrow->start_time,
63 	pArrow->end_time);
64 }
65 
PrintComm(RLOG_COMM * pComm)66 void PrintComm(RLOG_COMM *pComm)
67 {
68     printf("RLOG_COMM  -");
69     printf(" %3d, newcomm: %d\n",
70 	pComm->rank,
71 	pComm->newcomm);
72 }
73 /* FIXME: Add a structured comment for the man page generate to
74  * create the basic documentation on this routine, particularly
75  * since this routine only prints a subset of information by default */
main(int argc,char * argv[])76 int main(int argc, char *argv[])
77 {
78     RLOG_IOStruct *pInput;
79     RLOG_FILE_HEADER header;
80     int num_levels;
81     int num_states;
82     int num_arrows;
83     int num_events;
84     int total_num_events = 0;
85     int i, j, k;
86     unsigned int nMask = 0;
87     int bSummary = 1;
88     RLOG_STATE state;
89     RLOG_EVENT event, lastevent;
90     RLOG_ARROW arrow, lastarrow;
91     int bFindEvent = 0;
92     double dFindTime = 0.0;
93     int bValidate = 0;
94     int bValidateArrows = 0;
95     int bOrder = 0;
96     int bJumpCheck = 0;
97     double dJump = 0.0;
98 
99     /* FIXME: This should also check for the GNU-standard --help, --usage,
100      * and -h options.  */
101     if (argc < 2)
102     {
103 	/* FIXME: What is the default behavior with just an rlogfile? */
104 	printf("printrlog rlogfile [EVENTS | STATES | ARROWS | HEADER | COMM | ALL | SUMMARY ]\n");
105 	printf("printrlog rlogfile find endtime\n");
106 	printf("printrlog rlogfile validate\n");
107 	printf("printrlog rlogfile order\n");
108 	printf("printrlog rlogfile arroworder\n");
109 	return -1;
110     }
111 
112     if (argc > 2)
113     {
114 	nMask = 0;
115 	bSummary = 0;
116 	for (i=2; i<argc; i++)
117 	{
118 	    if (strcmp(argv[i], "EVENTS") == 0)
119 		nMask |= EVENT_BIT;
120 	    if (strcmp(argv[i], "STATES") == 0)
121 		nMask |= STATE_BIT;
122 	    if (strcmp(argv[i], "ARROWS") == 0)
123 		nMask |= ARROW_BIT;
124 	    if (strcmp(argv[i], "HEADER") == 0)
125 		nMask |= HEADER_BIT;
126 	    if (strcmp(argv[i], "COMM") == 0)
127 		nMask |= COMM_BIT;
128 	    if (strcmp(argv[i], "ALL") == 0)
129 		nMask = HEADER_BIT | STATE_BIT | COMM_BIT | ARROW_BIT | EVENT_BIT;
130 	    if (strcmp(argv[i], "SUMMARY") == 0)
131 	    {
132 		bSummary = 1;
133 		nMask = 0;
134 	    }
135 	    if (strcmp(argv[i], "find") == 0)
136 	    {
137 		bFindEvent = 1;
138 		dFindTime = atof(argv[i+1]);
139 	    }
140 	    if (strcmp(argv[i], "validate") == 0)
141 	    {
142 		bValidate = 1;
143 	    }
144 	    if (strcmp(argv[i], "order") == 0)
145 	    {
146 		bOrder = 1;
147 	    }
148 	    if (strcmp(argv[i], "arroworder") == 0)
149 	    {
150 		bValidateArrows = 1;
151 	    }
152 	    if (strcmp(argv[i], "jump") == 0)
153 	    {
154 		bJumpCheck = 1;
155 		if (argv[i+1])
156 		    dJump = atof(argv[i+1]);
157 	    }
158 	}
159     }
160 
161     pInput = RLOG_CreateInputStruct(argv[1]);
162     if (pInput == NULL)
163     {
164 	printf("Error opening '%s'\n", argv[1]);
165 	return -1;
166     }
167 
168     if (bValidateArrows)
169     {
170 	num_arrows = RLOG_GetNumArrows(pInput);
171 	if (num_arrows)
172 	{
173 	    printf("num arrows: %d\n", num_arrows);
174 	    RLOG_GetNextArrow(pInput, &lastarrow);
175 	    if (lastarrow.start_time > lastarrow.end_time)
176 		printf("start > end: %g > %g\n", lastarrow.start_time, lastarrow.end_time);
177 	    while (RLOG_GetNextArrow(pInput, &arrow) == 0)
178 	    {
179 		if (arrow.start_time > arrow.end_time)
180 		    printf("start > end: %g > %g\n", arrow.start_time, arrow.end_time);
181 		if (arrow.end_time < lastarrow.end_time)
182 		    printf("arrows out of order: %d < %d\n", arrow.end_time, lastarrow.end_time);
183 		lastarrow = arrow;
184 	    }
185 	}
186 	RLOG_CloseInputStruct(&pInput);
187 	return 0;
188     }
189 
190     if (bValidate)
191     {
192 	num_arrows = RLOG_GetNumArrows(pInput);
193 	if (num_arrows)
194 	{
195 	    printf("num arrows: %d\n", num_arrows);
196 	    RLOG_GetNextArrow(pInput, &lastarrow);
197 	    if (lastarrow.start_time > lastarrow.end_time)
198 	    {
199 		printf("Error, arrows endtime before starttime: %g < %g\n", lastarrow.end_time, lastarrow.start_time);
200 		PrintArrow(&arrow);
201 	    }
202 	    while (RLOG_GetNextArrow(pInput, &arrow) == 0)
203 	    {
204 		if (lastarrow.end_time > arrow.end_time)
205 		{
206 		    printf("Error, arrows out of order: %g > %g\n", lastarrow.end_time, arrow.end_time);
207 		    PrintArrow(&lastarrow);
208 		    PrintArrow(&arrow);
209 		}
210 		if (arrow.start_time > arrow.end_time)
211 		{
212 		    printf("Error, arrows endtime before starttime: %g < %g\n", arrow.end_time, arrow.start_time);
213 		    PrintArrow(&arrow);
214 		}
215 		lastarrow = arrow;
216 	    }
217 	}
218 	for (j=pInput->header.nMinRank; j<=pInput->header.nMaxRank; j++)
219 	{
220 	    num_levels = RLOG_GetNumEventRecursions(pInput, j);
221 	    for (i=0; i<num_levels; i++)
222 	    {
223 		printf("Validating events in level %d:%d\n", j, i);
224 		if (RLOG_GetNextEvent(pInput, j, i, &event) == 0)
225 		{
226 		    if (event.end_time < event.start_time)
227 		    {
228 			printf("Error, event endtime before starttime: %g < %g\n", event.end_time, event.start_time);
229 		    }
230 		    lastevent = event;
231 		    while (RLOG_GetNextEvent(pInput, j, i, &event) == 0)
232 		    {
233 			if (lastevent.start_time > event.start_time)
234 			{
235 			    printf("Error, events out of order: %g > %g\n", lastevent.start_time, event.start_time);
236 			    PrintEvent(&lastevent);
237 			    PrintEvent(&event);
238 			}
239 			else if (lastevent.end_time > event.start_time)
240 			{
241 			    printf("Error, starttime before previous endtime: %g > %g\n", lastevent.end_time, event.start_time);
242 			    PrintEvent(&lastevent);
243 			    PrintEvent(&event);
244 			}
245 			if (event.end_time < event.start_time)
246 			{
247 			    printf("Error, event endtime before starttime: %g < %g\n", event.end_time, event.start_time);
248 			    PrintEvent(&event);
249 			}
250 			lastevent = event;
251 		    }
252 		}
253 	    }
254 	}
255 	RLOG_CloseInputStruct(&pInput);
256 	return 0;
257     }
258 
259     if (bOrder)
260     {
261 	int count = 0;
262 	if (RLOG_GetNextGlobalEvent(pInput, &event) != 0)
263 	{
264 	    RLOG_CloseInputStruct(&pInput);
265 	    return 0;
266 	}
267 	count++;
268 	lastevent = event;
269 	PrintEvent(&event);
270 	while (RLOG_GetNextGlobalEvent(pInput, &event) == 0)
271 	{
272 	    if (lastevent.start_time > event.start_time)
273 	    {
274 		printf("Error, events out of order: %g > %g\n", lastevent.start_time, event.start_time);
275 		PrintEvent(&lastevent);
276 		PrintEvent(&event);
277 	    }
278 	    if (event.end_time < event.start_time)
279 	    {
280 		printf("Error, event endtime before starttime: %g < %g\n", event.end_time, event.start_time);
281 		PrintEvent(&event);
282 	    }
283 	    lastevent = event;
284 	    PrintEvent(&event);
285 	    count++;
286 	}
287 	RLOG_CloseInputStruct(&pInput);
288 	printf("%d events traversed\n", count);
289 	return 0;
290     }
291 
292     if (bFindEvent)
293     {
294 	for (j=pInput->header.nMinRank; j<=pInput->header.nMaxRank; j++)
295 	{
296 	    printf("rank %d\n", j);
297 	    num_levels = RLOG_GetNumEventRecursions(pInput, j);
298 	    for (i=0; i<num_levels; i++)
299 	    {
300 		RLOG_FindEventBeforeTimestamp(pInput, j, i, dFindTime, &event, &k);
301 		PrintEventAndIndex(&event, k);
302 	    }
303 	}
304 	RLOG_CloseInputStruct(&pInput);
305 	return 0;
306     }
307 
308     if (bJumpCheck)
309     {
310 	printf("Start:\n");
311 	RLOG_FindGlobalEventBeforeTimestamp(pInput, dJump, &event);
312 	PrintEvent(&event);
313 	/*RLOG_PrintGlobalState(pInput);*/
314 	printf("Previous 10:\n");
315 	for (i=0; i<10; i++)
316 	{
317 	    RLOG_GetPreviousGlobalEvent(pInput, &event);
318 	    PrintEvent(&event);
319 	}
320 	printf("Start:\n");
321 	RLOG_FindGlobalEventBeforeTimestamp(pInput, dJump, &event);
322 	PrintEvent(&event);
323 	printf("Next 10:\n");
324 	for (i=0; i<10; i++)
325 	{
326 	    RLOG_GetNextGlobalEvent(pInput, &event);
327 	    PrintEvent(&event);
328 	}
329 	return 0;
330     }
331 
332     if (RLOG_GetFileHeader(pInput, &header))
333     {
334 	printf("unable to read the file header\n");
335 	RLOG_CloseInputStruct(&pInput);
336 	return -1;
337     }
338     if (nMask & HEADER_BIT || bSummary)
339     {
340 	printf("min rank: %d\n", header.nMinRank);
341 	printf("max rank: %d\n", header.nMaxRank);
342     }
343 
344     if (nMask & STATE_BIT || bSummary)
345     {
346 	num_states = RLOG_GetNumStates(pInput);
347 	if (num_states)
348 	{
349 	    printf("num states: %d\n", num_states);
350 	    if (nMask & STATE_BIT)
351 	    {
352 		for (i=0; i<num_states; i++)
353 		{
354 		    RLOG_GetNextState(pInput, &state);
355 		    PrintState(&state);
356 		}
357 	    }
358 	}
359     }
360 
361     if (nMask & ARROW_BIT || bSummary)
362     {
363 	num_arrows = RLOG_GetNumArrows(pInput);
364 	if (num_arrows)
365 	{
366 	    printf("num arrows: %d\n", num_arrows);
367 	    if (nMask & ARROW_BIT)
368 	    {
369 		for (i=0; i<num_arrows; i++)
370 		{
371 		    RLOG_GetNextArrow(pInput, &arrow);
372 		    PrintArrow(&arrow);
373 		}
374 	    }
375 	}
376     }
377 
378     if (nMask & EVENT_BIT || bSummary)
379     {
380 	for (k=pInput->header.nMinRank; k<=pInput->header.nMaxRank; k++)
381 	{
382 	    total_num_events = 0;
383 	    num_levels = RLOG_GetNumEventRecursions(pInput, k);
384 	    if (num_levels > 0)
385 	    {
386 		printf("rank %d\n", k);
387 		printf("num event recursions: %d\n", num_levels);
388 		for (i=0; i<num_levels; i++)
389 		{
390 		    num_events = RLOG_GetNumEvents(pInput, k, i);
391 		    total_num_events += num_events;
392 		    printf(" level %d, num events: %d\n", i, num_events);
393 		    if (nMask & EVENT_BIT)
394 		    {
395 			for (j=0; j<num_events; j++)
396 			{
397 			    RLOG_GetNextEvent(pInput, k, i, &event);
398 			    PrintEvent(&event);
399 			}
400 		    }
401 		}
402 		printf("num events total: %d\n", total_num_events);
403 	    }
404 	}
405     }
406 
407     RLOG_CloseInputStruct(&pInput);
408 
409     return 0;
410 }
411