📄 rlogutil.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* $Id: rlogutil.c,v 1.8 2007/07/31 23:23:02 chan Exp $ * * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "rlog.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <stdarg.h>static int ReadFileData(char *pBuffer, int length, FILE *fin){ int num_read; while (length) { num_read = fread(pBuffer, 1, length, fin); if (num_read == -1) { printf("Error: fread failed - %s\n", strerror(errno)); return errno; } if (num_read == 0 && length) return -1; /*printf("fread(%d)", num_read);fflush(stdout);*/ length -= num_read; pBuffer += num_read; } return 0;}static int WriteFileData(const char *pBuffer, int length, FILE *fout){ int num_written; while (length) { num_written = fwrite(pBuffer, 1, length, fout); if (num_written == -1) { printf("Error: fwrite failed - %s\n", strerror(errno)); return errno; } if (num_written == 0 && length) return -1; /*printf("fwrite(%d)", num_written);fflush(stdout);*/ length -= num_written; pBuffer += num_written; } return 0;}static int rlog_err_printf(char *str, ...);static int rlog_err_printf(char *str, ...){ int n; va_list list; va_start(list, str); n = vprintf(str, list); va_end(list); fflush(stdout); return n;}RLOG_IOStruct *RLOG_CreateInputStruct(const char *filename){ int i, j, rank_index, cur_rank, min_rank = 0; RLOG_IOStruct *pInput; int type, length; /* allocate an input structure */ pInput = (RLOG_IOStruct*)malloc(sizeof(RLOG_IOStruct)); if (pInput == NULL) { printf("malloc failed - %s\n", strerror(errno)); return NULL; } pInput->ppCurEvent = NULL; pInput->ppCurGlobalEvent = NULL; pInput->gppCurEvent = NULL; pInput->gppPrevEvent = NULL; pInput->ppEventOffset = NULL; pInput->ppNumEvents = NULL; pInput->nNumArrows = 0; /* open the input rlog file */ pInput->f = fopen(filename, "rb"); if (pInput->f == NULL) { printf("fopen(%s) failed, error: %s\n", filename, strerror(errno)); free(pInput); return NULL; } pInput->nNumRanks = 0; /* read the sections */ while (fread(&type, sizeof(int), 1, pInput->f)) { fread(&length, sizeof(int), 1, pInput->f); switch (type) { case RLOG_HEADER_SECTION: /*printf("type: RLOG_HEADER_SECTION, length: %d\n", length);*/ if (length != sizeof(RLOG_FILE_HEADER)) { printf("error in header size %d != %d\n", length, (int) sizeof(RLOG_FILE_HEADER)); } if (ReadFileData((char*)&pInput->header, sizeof(RLOG_FILE_HEADER), pInput->f)) { rlog_err_printf("reading rlog header failed\n"); return NULL; } pInput->nNumRanks = pInput->header.nMaxRank + 1 - pInput->header.nMinRank; min_rank = pInput->header.nMinRank; pInput->pRank = (int*)malloc(pInput->nNumRanks * sizeof(int)); pInput->pNumEventRecursions = (int*)malloc(pInput->nNumRanks * sizeof(int)); pInput->ppNumEvents = (int**)malloc(pInput->nNumRanks * sizeof(int*)); pInput->ppCurEvent = (int**)malloc(pInput->nNumRanks * sizeof(int*)); pInput->ppCurGlobalEvent = (int**)malloc(pInput->nNumRanks * sizeof(int*)); pInput->gppCurEvent = (RLOG_EVENT**)malloc(pInput->nNumRanks * sizeof(RLOG_EVENT*)); pInput->gppPrevEvent = (RLOG_EVENT**)malloc(pInput->nNumRanks * sizeof(RLOG_EVENT*)); pInput->ppEventOffset = (long**)malloc(pInput->nNumRanks * sizeof(long*)); for (i=0; i<pInput->nNumRanks; i++) { pInput->pRank[i] = -1; pInput->pNumEventRecursions[i] = 0; pInput->ppNumEvents[i] = NULL; pInput->ppCurEvent[i] = NULL; pInput->ppCurGlobalEvent[i] = NULL; pInput->gppCurEvent[i] = NULL; pInput->gppPrevEvent[i] = NULL; pInput->ppEventOffset[i] = NULL; } break; case RLOG_STATE_SECTION: /*printf("type: RLOG_STATE_SECTION, length: %d\n", length);*/ pInput->nNumStates = length / sizeof(RLOG_STATE); pInput->nStateOffset = ftell(pInput->f); fseek(pInput->f, length, SEEK_CUR); break; case RLOG_ARROW_SECTION: /*printf("type: RLOG_ARROW_SECTION, length: %d\n", length);*/ pInput->nNumArrows = length / sizeof(RLOG_ARROW); pInput->nArrowOffset = ftell(pInput->f); fseek(pInput->f, length, SEEK_CUR); break; case RLOG_EVENT_SECTION: /*printf("type: RLOG_EVENT_SECTION, length: %d, ", length);*/ fread(&cur_rank, sizeof(int), 1, pInput->f); if (cur_rank - min_rank >= pInput->nNumRanks) { printf("Error: event section out of range - %d <= %d <= %d\n", pInput->header.nMinRank, cur_rank, pInput->header.nMaxRank); free(pInput); return NULL; } rank_index = cur_rank - min_rank; fread(&pInput->pNumEventRecursions[rank_index], sizeof(int), 1, pInput->f); /*printf("levels: %d\n", pInput->nNumEventRecursions);*/ if (pInput->pNumEventRecursions[rank_index]) { pInput->ppCurEvent[rank_index] = (int*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(int)); pInput->ppCurGlobalEvent[rank_index] = (int*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(int)); pInput->gppCurEvent[rank_index] = (RLOG_EVENT*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(RLOG_EVENT)); pInput->gppPrevEvent[rank_index] = (RLOG_EVENT*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(RLOG_EVENT)); pInput->ppNumEvents[rank_index] = (int*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(int)); pInput->ppEventOffset[rank_index] = (long*)malloc(pInput->pNumEventRecursions[rank_index] * sizeof(long)); } for (i=0; i<pInput->pNumEventRecursions[rank_index]; i++) { fread(&pInput->ppNumEvents[rank_index][i], sizeof(int), 1, pInput->f); /*printf(" level %2d: %d events\n", i, pInput->pNumEvents[i]);*/ } if (pInput->pNumEventRecursions[rank_index]) { pInput->ppEventOffset[rank_index][0] = ftell(pInput->f); for (i=1; i<pInput->pNumEventRecursions[rank_index]; i++) { pInput->ppEventOffset[rank_index][i] = pInput->ppEventOffset[rank_index][i-1] + (pInput->ppNumEvents[rank_index][i-1] * sizeof(RLOG_EVENT)); } } length -= ((pInput->pNumEventRecursions[rank_index] + 2) * sizeof(int)); fseek(pInput->f, length, SEEK_CUR); break; default: /*printf("unknown section: type %d, length %d\n", type, length);*/ fseek(pInput->f, length, SEEK_CUR); break; } } /* reset the iterators */ RLOG_ResetStateIter(pInput); RLOG_ResetArrowIter(pInput); for (j=0; j<pInput->nNumRanks; j++) { for (i=0; i<pInput->pNumEventRecursions[j]; i++) RLOG_ResetEventIter(pInput, j+pInput->header.nMinRank, i); } RLOG_ResetGlobalIter(pInput); return pInput;}static int compareArrows(const RLOG_ARROW *pLeft, const RLOG_ARROW *pRight){ if (pLeft->end_time < pRight->end_time) return -1; if (pLeft->end_time == pRight->end_time) return 0; return 1;}static int ModifyArrows(FILE *f, int nNumArrows, int nMin, double *pOffsets, int n){ RLOG_ARROW arrow, *pArray; int i, index, bModified; long arrow_pos; int error; double temp_time; fseek(f, 0, SEEK_CUR); arrow_pos = ftell(f); if (arrow_pos == -1) return errno; pArray = (RLOG_ARROW*)malloc(nNumArrows * sizeof(RLOG_ARROW)); if (pArray) { printf("Modifying %d arrows\n", nNumArrows); /* read the arrows */ fseek(f, 0, SEEK_CUR); error = ReadFileData((char*)pArray, nNumArrows * sizeof(RLOG_ARROW), f); if (error) { free(pArray); return error; } /* modify the arrows */ for (i=0; i<nNumArrows; i++) { arrow = pArray[i]; bModified = RLOG_FALSE; index = (arrow.leftright == RLOG_ARROW_RIGHT) ? arrow.src - nMin : arrow.dest - nMin; if (index >= 0 && index < n && pOffsets[index] != 0) { arrow.start_time += pOffsets[index]; bModified = RLOG_TRUE; } index = (arrow.leftright == RLOG_ARROW_RIGHT) ? arrow.dest - nMin : arrow.src - nMin; if (index >= 0 && index < n && pOffsets[index] != 0) { arrow.end_time += pOffsets[index]; bModified = RLOG_TRUE; } if (bModified) { if (arrow.start_time > arrow.end_time) { temp_time = arrow.start_time; arrow.start_time = arrow.end_time; arrow.end_time = temp_time; arrow.leftright = (arrow.leftright == RLOG_ARROW_LEFT) ? RLOG_ARROW_RIGHT : RLOG_ARROW_LEFT; } pArray[i] = arrow; } } /* sort the arrows */ qsort(pArray, (size_t)nNumArrows, sizeof(RLOG_ARROW), (int (*)(const void *,const void*))compareArrows); /* write the arrows back */ fseek(f, arrow_pos, SEEK_SET); error = WriteFileData((char*)pArray, nNumArrows * sizeof(RLOG_ARROW), f); if (error) { free(pArray); return error; } fseek(f, 0, SEEK_CUR); free(pArray); } else { printf("Error: unable to allocate an array big enough to hold %d arrows\n", nNumArrows); return -1; } return 0;}#if 0static int ModifyArrows(FILE *f, int nNumArrows, int nMin, double *pOffsets, int n){ RLOG_ARROW arrow, *pArray; int i, index, bModified; int num_bytes; long arrow_pos; int error; double temp_time; printf("Modifying %d arrows\n", nNumArrows); arrow_pos = ftell(f); for (i=0; i<nNumArrows; i++) { num_bytes = fread(&arrow, 1, sizeof(RLOG_ARROW), f); if (num_bytes != sizeof(RLOG_ARROW)) { printf("reading arrow failed - num_bytes %d != %d, error %d\n", num_bytes, sizeof(RLOG_ARROW), ferror(f)); return -1; } bModified = RLOG_FALSE; index = (arrow.leftright == RLOG_ARROW_RIGHT) ? arrow.src - nMin : arrow.dest - nMin; if (index >= 0 && index < n && pOffsets[index] != 0) { arrow.start_time += pOffsets[index]; bModified = RLOG_TRUE; } index = (arrow.leftright == RLOG_ARROW_RIGHT) ? arrow.dest - nMin : arrow.src - nMin; if (index >= 0 && index < n && pOffsets[index] != 0) { arrow.end_time += pOffsets[index]; bModified = RLOG_TRUE; } if (bModified) { if (arrow.start_time > arrow.end_time) { temp_time = arrow.start_time; arrow.start_time = arrow.end_time; arrow.end_time = temp_time; arrow.leftright = (arrow.leftright = RLOG_ARROW_LEFT) ? RLOG_ARROW_RIGHT : RLOG_ARROW_LEFT; } fseek(f, -(int)sizeof(RLOG_ARROW), SEEK_CUR); if (fwrite(&arrow, 1, sizeof(RLOG_ARROW), f) != sizeof(RLOG_ARROW)) { printf("writing modified arrow failed - error %d\n", ferror(f)); return -1; } fseek(f, 0, SEEK_CUR); } } pArray = (RLOG_ARROW*)malloc(nNumArrows * sizeof(RLOG_ARROW)); if (pArray) { /* read the arrows */ fseek(f, arrow_pos, SEEK_SET); error = ReadFileData((char*)pArray, nNumArrows * sizeof(RLOG_ARROW), f); if (error) { free(pArray); return error; } /* sort the arrows */ qsort(pArray, (size_t)nNumArrows, sizeof(RLOG_ARROW), (int (*)(const void *,const void*))compareArrows); /* write the arrows back */ fseek(f, arrow_pos, SEEK_SET); error = WriteFileData((char*)pArray, nNumArrows * sizeof(RLOG_ARROW), f); if (error) { free(pArray); return error; } fseek(f, 0, SEEK_CUR); free(pArray); } else { printf("Error: unable to allocate an array big enough to hold %d arrows\n", nNumArrows); return -1; } return 0;}#endifstatic int ModifyEvents(FILE *f, int nNumEvents, int nMin, double *pOffsets, int n);static int ModifyEvents(FILE *f, int nNumEvents, int nMin, double *pOffsets, int n){ RLOG_EVENT event; int i, index; int error; printf("Modifying %d events\n", nNumEvents); fseek(f, 0, SEEK_CUR); for (i=0; i<nNumEvents; i++) { error = ReadFileData((char*)&event, sizeof(RLOG_EVENT), f); if (error) { rlog_err_printf("reading event failed.\n"); return -1; } index = event.rank - nMin; if (index >= 0 && index < n && pOffsets[index] != 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -