OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_40/] [or1ksim/] [profiler.c] - Diff between revs 264 and 533

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 264 Rev 533
Line 6... Line 6...
 
 
#define MAX_STACK 1024
#define MAX_STACK 1024
#define MAX_FUNCS 1024
#define MAX_FUNCS 1024
 
 
struct stack_struct {
struct stack_struct {
 
  /* Function address */
 
  unsigned int addr;
 
 
 
  /* Cycles of function start; cycles of subfunctions are added later */
  unsigned int cycles;
  unsigned int cycles;
 
 
 
  /* Return address */
  unsigned int raddr;
  unsigned int raddr;
  unsigned int addr;
 
 
  /* Name of the function */
  char name[33];
  char name[33];
} stack[MAX_STACK];
} stack[MAX_STACK];
 
 
struct func_struct {
struct func_struct {
 
  /* Start address of function */
  unsigned int addr;
  unsigned int addr;
 
 
 
  /* Name of the function */
  char name[33];
  char name[33];
 
 
 
  /* Total cycles spent in function */
  long cum_cycles;
  long cum_cycles;
 
 
 
  /* Calls to this function */
  long calls;
  long calls;
} func[MAX_FUNCS];
} func[MAX_FUNCS];
 
 
 
/* Total number of functions */
int nfuncs = 0;
int nfuncs = 0;
 
 
 
/* Current depth */
int nstack = 0;
int nstack = 0;
int maxstack = 0, cycles = 0;
 
int cumulative = 0;
 
 
 
FILE *fprof = 0;
/* Max depth */
 
int maxstack = 0;
 
 
 
/* Number of total calls */
 
int ntotcalls = 0;
 
 
 
/* Number of covered calls */
 
int nfunccalls = 0;
 
 
 
/* Current cycles */
 
static int cycles = 0;
 
 
 
/* Whether we are in cumulative mode */
 
static int cumulative = 0;
 
 
 
/* Whether we should not report warnings */
 
static int quiet = 0;
 
 
 
/* File to read from */
 
static FILE *fprof = 0;
 
 
int main (int argc, char *argv[]) {
int main (int argc, char *argv[]) {
  char fprofname[50] = "sim.profile";
  char fprofname[50] = "sim.profile";
  int line = 0;
  int line = 0;
  if (argc > 4 || argc < 2) {
  if (argc > 4 || argc < 2) {
    fprintf (stderr, "USAGE: profiler [--cumulative|-c] --generate|-g [profile_file_name]\n");
    fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--quiet|-q] --generate|-g [profile_file_name]\n");
    exit(1);
    exit(1);
  }
  }
  argv++; argc--;
  argv++; argc--;
  while (argc > 0) {
  while (argc > 0) {
    if (!strcmp(argv[0], "-c") || !strcmp(argv[0], "--cumulative")) {
    if (!strcmp(argv[0], "-q") || !strcmp(argv[0], "--quiet")) {
 
      quiet = 1;
 
      argv++; argc--;
 
    } else if (!strcmp(argv[0], "-c") || !strcmp(argv[0], "--cumulative")) {
      cumulative = 1;
      cumulative = 1;
      argv++; argc--;
      argv++; argc--;
    } else if (strcmp(argv[0], "-g") && strcmp(argv[0], "--generate")) {
    } else if (strcmp(argv[0], "-g") && strcmp(argv[0], "--generate")) {
      fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--generate|-g] [profile_file_name]\n");
      fprintf (stderr, "USAGE: profiler [--cumulative|-c] [--generate|-g] [profile_file_name]\n");
      exit(1);
      exit(1);
Line 70... Line 107...
              cycles = stack[nstack].cycles;
              cycles = stack[nstack].cycles;
              nstack++;
              nstack++;
              if (nstack > maxstack)
              if (nstack > maxstack)
                maxstack = nstack;
                maxstack = nstack;
        }
        }
 
      ntotcalls++;
      } else if (dir == '-') {
      } else if (dir == '-') {
      struct stack_struct s;
      struct stack_struct s;
      if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
      if (fscanf (fprof, "%08X %08X\n", &s.cycles, &s.raddr) != 2)
        fprintf (stderr, "Error reading line #%i\n", line);
        fprintf (stderr, "Error reading line #%i\n", line);
      else {
      else {
        int i;
        int i;
        cycles = s.cycles;
        cycles = s.cycles;
        for (i = nstack - 1; i >= 0; i--)
        for (i = nstack - 1; i >= 0; i--)
          if (stack[i].raddr == s.raddr) break;
          if (stack[i].raddr == s.raddr) break;
        if (i >= 0) {
        if (i >= 0) {
 
          /* pop everything above current from stack,
 
             if more than one, something went wrong */
 
          while (nstack > i) {
          int j;
          int j;
          long time = s.cycles - stack[nstack - 1].cycles;
            long time;
 
            nstack--;
 
            time = s.cycles - stack[nstack].cycles;
 
            if (!quiet && time < 0) {
 
              fprintf (stderr, "WARNING: Negative time at %s (return addr = %08X).\n", stack[i].name, stack[i].raddr);
 
              time = 0;
 
            }
 
 
          /* Whether in normal mode, we must substract called function from execution time.  */
          /* Whether in normal mode, we must substract called function from execution time.  */
          if (!cumulative)
          if (!cumulative)
            for (j = 0; j < nstack - 1; j++)
              for (j = 0; j < nstack; j++)
              stack[j].cycles += time;
              stack[j].cycles += time;
 
 
          /* pop everything above from current from stack,
            if (!quiet && i != nstack)
             if more than one, something went wrong */
 
          while (i < nstack) {
 
            nstack--;
 
            if (i != nstack)
 
              fprintf (stderr, "WARNING: Missaligned return call for %s (%08X) (found %s @ %08X), closing.\n", stack[nstack].name, stack[nstack].raddr, stack[i].name, stack[i].raddr);
              fprintf (stderr, "WARNING: Missaligned return call for %s (%08X) (found %s @ %08X), closing.\n", stack[nstack].name, stack[nstack].raddr, stack[i].name, stack[i].raddr);
 
 
            for (j = 0; j < nfuncs; j++)
            for (j = 0; j < nfuncs; j++)
              if (stack[nstack].addr == func[j].addr) { /* function exists, append. */
              if (stack[nstack].addr == func[j].addr) { /* function exists, append. */
                func[j].cum_cycles += s.cycles - stack[nstack].cycles;
                func[j].cum_cycles += time;
                func[j].calls++;
                func[j].calls++;
 
                nfunccalls++;
                break;
                break;
              }
              }
            if (j >= nfuncs) { /* function does not yet exist, create new. */
            if (j >= nfuncs) { /* function does not yet exist, create new. */
              func[nfuncs].cum_cycles = s.cycles - stack[nstack].cycles;
              func[nfuncs].cum_cycles = time;
              func[nfuncs].calls = 1;
              func[nfuncs].calls = 1;
 
              nfunccalls++;
              func[nfuncs].addr = stack[nstack].addr;
              func[nfuncs].addr = stack[nstack].addr;
              strcpy (func[nfuncs].name, stack[nstack].name);
              strcpy (func[nfuncs].name, stack[nstack].name);
              nfuncs++;
              nfuncs++;
            }
            }
          }
          }
        } else fprintf (stderr, "WARNING: Cannot find return call for (%08X), ignoring.\n", s.raddr);
        } else if (!quiet) fprintf (stderr, "WARNING: Cannot find return call for (%08X), ignoring.\n", s.raddr);
      }
      }
    } else
    } else
      break;
      break;
  }
  }
  fclose(fprof);
  fclose(fprof);
Line 137... Line 183...
              func[i].name, func[i].addr, func[i].calls, ((double)func[i].cum_cycles / func[i].calls), func[i].cum_cycles, (100. * func[i].cum_cycles / cycles));
              func[i].name, func[i].addr, func[i].calls, ((double)func[i].cum_cycles / func[i].calls), func[i].cum_cycles, (100. * func[i].cum_cycles / cycles));
      func[i].cum_cycles = -1;
      func[i].cum_cycles = -1;
    }
    }
    printf ("---------------------------------------------------------------------------\n");
    printf ("---------------------------------------------------------------------------\n");
  }
  }
  printf("Total %i functions (max depth %i), %i cycles.\n", nfuncs, maxstack, cycles);
  printf ("Total %i functions, %i cycles.\n", nfuncs, cycles);
 
  printf ("Total function calls %i/%i (max depth %i).\n", nfunccalls, ntotcalls, maxstack);
 
  return 0;
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.