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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [or1ksim/] [mprofiler.c] - Rev 848

Go to most recent revision | Compare with Previous | Blame | View Log

/* mprofiler.c -- memory profiling utility
   Copyright (C) 2002 Marko Mlinar, markom@opencores.org
 
This file is part of OpenRISC 1000 Architectural Simulator.
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
/* Command line utility, that displays profiling information, generated
   by or1ksim. (use --mprofile option at command line, when running or1ksim.  */
 
#include <stdio.h>
#include <malloc.h>
#include "support/profile.h"
#include "mprofiler.h"
 
struct memory_hash {
  struct memory_hash *next;
  unsigned long addr;
  unsigned long cnt[3];    /* Various counters */
} *hash[HASH_SIZE];
 
/* Groups size -- how much addresses should be joined together */
int group_bits = 2;
 
/* Start address */
int start_addr = 0;
 
/* End address */
int end_addr = 0xffffffff;
 
/* File to read from */
static FILE *fprof = 0;
 
void mp_help ()
{
  printf ("mprofiler <-d|-p|-a|-w> [-f filename] [-g group] from to\n");
  printf ("\t-d\t--detail\t\tdetailed output\n");
  printf ("\t-p\t--pretty\t\tpretty output\n");
  printf ("\t-a\t--access\t\toutput accesses only\n");
  printf ("\t-w\t--width\t\t\toutput by width\n");
  printf ("\t-f\t--filename filename\tspecify mprofile file [sim.mprofile]\n");
  printf ("\t-g\t--group bits\t\tgroup 2^bits successive\n");
  printf ("\t\t\t\t\taddresses together [2]\n");
  printf ("\t-h\t--help\t\t\toutput this screen\n");
}
 
void hash_add (unsigned long addr, int index)
{
  struct memory_hash *h = hash[HASH_FUNC(addr)];
  while (h && h->addr != addr) h = h->next;
 
  if (!h) {
    h = (struct memory_hash *)malloc (sizeof (struct memory_hash));
    h->next = hash[HASH_FUNC(addr)];
    hash[HASH_FUNC(addr)] = h;
    h->addr = addr;
    h->cnt[0] = h->cnt[1] = h->cnt[2] = 0;
  }
  h->cnt[index]++;
}
 
unsigned long hash_get (unsigned long addr, int index)
{
  struct memory_hash *h = hash[HASH_FUNC(addr)];
  while (h && h->addr != addr) h = h->next;
 
  if (!h) return 0;
  return h->cnt[index];
}
 
void init ()
{
  int i;
  for (i = 0; i < HASH_SIZE; i++)
    hash[i] = NULL;
}
 
void read_file (FILE *f, int mode)
{
  struct mprofentry_struct buf[BUF_SIZE];
  int num_read;
  do {
    int i;
    num_read = fread (buf, sizeof (struct mprofentry_struct), BUF_SIZE, f);
    for (i = 0; i < num_read; i++) if (buf[i].addr >= start_addr && buf[i].addr <= end_addr) {
      int index;
      unsigned t = buf[i].type;
      if (t > 64) {
        printf ("!");
        t = 0;
      }
      if (mode == MODE_WIDTH) t >>= 3;
      else t &= 0x7;
 
      switch (t) {
        case 1: index = 0; break;
        case 2: index = 1; break;
        case 4: index = 2; break;
        default:
          index = 0;
          printf ("!!!!");
          break;
      }
      hash_add (buf[i].addr >> group_bits, index);
    }
  } while (num_read > 0);
}
 
static inline int nbits (unsigned long a)
{
  int cnt = 0;
  int b = a;
  if (!a) return 0;
 
  while (a) a >>= 1, cnt++;
  if (cnt > 1 && ((b >> (cnt - 2)) & 1))
    cnt = cnt * 2 + 1;
  else
    cnt *= 2;
 
  return cnt - 1;
}
 
void printout (int mode)
{
  unsigned long addr = start_addr & ~((1 << group_bits) - 1);
  printf ("start = %08x (%08x); end = %08x; group_bits = %08x\n", start_addr, addr, end_addr, (1 << group_bits) - 1);
  for (; addr <= end_addr; addr += (1 << group_bits)) {
    int i;
    unsigned long a = hash_get (addr >> group_bits, 0);
    unsigned long b = hash_get (addr >> group_bits, 1);
    unsigned long c = hash_get (addr >> group_bits, 2);
    printf ("%08x:", addr);
    switch (mode) {
      case MODE_DETAIL:
        if (a) printf (" %10i R", a);
        else printf ("            R");
        if (b) printf (" %10i W", b);
        else printf ("            W");
        if (c) printf (" %10i F", c);
        else printf ("            F");
        break;
      case MODE_ACCESS:
        printf (" %10i", a + b + c);
        break;
      case MODE_PRETTY:
        printf (" %10i ", a + b + c);
        for (i = 0; i < nbits (a + b + c); i++)
          printf ("#");
#if 0
        for (; i < 64; i++)
          printf (".");
#endif
        break;
      case MODE_WIDTH:
        if (a) printf (" %10i B", a);
        else printf ("            B");
        if (b) printf (" %10i H", b);
        else printf ("            H");
        if (c) printf (" %10i W", c);
        else printf ("            W");
        break;
    }
    printf ("\n");
    if (addr >= addr + (1 << group_bits)) break; /* Overflow? */
  }
}
 
int main_mprofiler (int argc, char *argv[])
{
  char fmprofname[50] = "sim.mprofile";
  int param = 0;
  int mode = MODE_DETAIL;
 
  argv++; argc--;
  while (argc > 0) {
    if (!strcmp(argv[0], "-d") || !strcmp(argv[0], "--detail")) {
      mode = MODE_DETAIL;
      argv++; argc--;
    } else if (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--pretty")) {
      mode = MODE_PRETTY;
      argv++; argc--;
    } else if (!strcmp(argv[0], "-a") || !strcmp(argv[0], "--access")) {
      mode = MODE_ACCESS;
      argv++; argc--;
    } else if (!strcmp(argv[0], "-w") || !strcmp(argv[0], "--width")) {
      mode = MODE_WIDTH;
      argv++; argc--;
    } else if (!strcmp(argv[0], "-g") || !strcmp(argv[0], "--group")) {
      argv++; argc--;
      group_bits = strtoul (argv[0], NULL, 0);
      argv++; argc--;
    } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
      mp_help ();
      return 0;
    } else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--filename")) {
      argv++; argc--;
      strcpy (&fmprofname[0], argv[0]);
      argv++; argc--;
    } else {
      switch (param) {
        case 0:
          start_addr = strtoul (argv[0], NULL, 0);
          break;
        case 1:
          end_addr = strtoul (argv[0], NULL, 0);
          break;
        default:
          fprintf (stderr, "Invalid number of parameters.\n");
          return -1;
      }
      argv++; argc--; param++;
    }
  }
 
  fprof = fopen (fmprofname, "rm");
 
  if (!fprof) {
    fprintf (stderr, "Cannot open profile file: %s\n", fmprofname);
    return 1;
  }
 
  init ();
  read_file (fprof, mode);
  fclose (fprof);
  printout (mode);
  return 0;
}
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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