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

Subversion Repositories openrisc_me

[/] [openrisc/] [tags/] [gdb/] [gdb-6.8/] [gdb-6.8.openrisc-2.1/] [sim/] [common/] [sim-profile.c] - Diff between revs 24 and 33

Only display areas with differences | Details | Blame | View Log

Rev 24 Rev 33
/* Default profiling support.
/* Default profiling support.
   Copyright (C) 1996, 1997, 1998, 2000, 2001, 2007, 2008
   Copyright (C) 1996, 1997, 1998, 2000, 2001, 2007, 2008
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Contributed by Cygnus Support.
   Contributed by Cygnus Support.
 
 
This file is part of GDB, the GNU debugger.
This file is part of GDB, the GNU debugger.
 
 
This program is free software; you can redistribute it and/or modify
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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
#include "sim-main.h"
#include "sim-main.h"
#include "sim-io.h"
#include "sim-io.h"
#include "sim-options.h"
#include "sim-options.h"
#include "sim-assert.h"
#include "sim-assert.h"
 
 
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.h>
#endif
#endif
 
 
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#else
#else
#ifdef HAVE_STRINGS_H
#ifdef HAVE_STRINGS_H
#include <strings.h>
#include <strings.h>
#endif
#endif
#endif
#endif
#include <ctype.h>
#include <ctype.h>
 
 
#define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
#define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
 
 
static MODULE_INIT_FN profile_init;
static MODULE_INIT_FN profile_init;
static MODULE_UNINSTALL_FN profile_uninstall;
static MODULE_UNINSTALL_FN profile_uninstall;
 
 
static DECLARE_OPTION_HANDLER (profile_option_handler);
static DECLARE_OPTION_HANDLER (profile_option_handler);
 
 
enum {
enum {
  OPTION_PROFILE_INSN = OPTION_START,
  OPTION_PROFILE_INSN = OPTION_START,
  OPTION_PROFILE_MEMORY,
  OPTION_PROFILE_MEMORY,
  OPTION_PROFILE_MODEL,
  OPTION_PROFILE_MODEL,
  OPTION_PROFILE_FILE,
  OPTION_PROFILE_FILE,
  OPTION_PROFILE_CORE,
  OPTION_PROFILE_CORE,
  OPTION_PROFILE_CPU_FREQUENCY,
  OPTION_PROFILE_CPU_FREQUENCY,
  OPTION_PROFILE_PC,
  OPTION_PROFILE_PC,
  OPTION_PROFILE_PC_RANGE,
  OPTION_PROFILE_PC_RANGE,
  OPTION_PROFILE_PC_GRANULARITY,
  OPTION_PROFILE_PC_GRANULARITY,
  OPTION_PROFILE_RANGE,
  OPTION_PROFILE_RANGE,
  OPTION_PROFILE_FUNCTION
  OPTION_PROFILE_FUNCTION
};
};
 
 
static const OPTION profile_options[] = {
static const OPTION profile_options[] = {
  { {"profile", optional_argument, NULL, 'p'},
  { {"profile", optional_argument, NULL, 'p'},
      'p', "on|off", "Perform profiling",
      'p', "on|off", "Perform profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN},
  { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN},
      '\0', "on|off", "Perform instruction profiling",
      '\0', "on|off", "Perform instruction profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY},
  { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY},
      '\0', "on|off", "Perform memory profiling",
      '\0', "on|off", "Perform memory profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE},
  { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE},
      '\0', "on|off", "Perform CORE profiling",
      '\0', "on|off", "Perform CORE profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL},
  { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL},
      '\0', "on|off", "Perform model profiling",
      '\0', "on|off", "Perform model profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-cpu-frequency", required_argument, NULL,
  { {"profile-cpu-frequency", required_argument, NULL,
     OPTION_PROFILE_CPU_FREQUENCY},
     OPTION_PROFILE_CPU_FREQUENCY},
      '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
      '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
      profile_option_handler },
      profile_option_handler },
 
 
  { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
  { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
      '\0', "FILE NAME", "Specify profile output file",
      '\0', "FILE NAME", "Specify profile output file",
      profile_option_handler },
      profile_option_handler },
 
 
  { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC},
  { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC},
      '\0', "on|off", "Perform PC profiling",
      '\0', "on|off", "Perform PC profiling",
      profile_option_handler },
      profile_option_handler },
  { {"profile-pc-frequency", required_argument, NULL, 'F'},
  { {"profile-pc-frequency", required_argument, NULL, 'F'},
      'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
      'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
      profile_option_handler },
      profile_option_handler },
  { {"profile-pc-size", required_argument, NULL, 'S'},
  { {"profile-pc-size", required_argument, NULL, 'S'},
      'S', "PC PROFILE SIZE", "Specify PC profiling size",
      'S', "PC PROFILE SIZE", "Specify PC profiling size",
      profile_option_handler },
      profile_option_handler },
  { {"profile-pc-granularity", required_argument, NULL, OPTION_PROFILE_PC_GRANULARITY},
  { {"profile-pc-granularity", required_argument, NULL, OPTION_PROFILE_PC_GRANULARITY},
      '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
      '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
      profile_option_handler },
      profile_option_handler },
  { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE},
  { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE},
      '\0', "BASE,BOUND", "Specify PC profiling address range",
      '\0', "BASE,BOUND", "Specify PC profiling address range",
      profile_option_handler },
      profile_option_handler },
 
 
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
  { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE},
  { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE},
      '\0', "START,END", "Specify range of addresses for instruction and model profiling",
      '\0', "START,END", "Specify range of addresses for instruction and model profiling",
      profile_option_handler },
      profile_option_handler },
#if 0 /*wip*/
#if 0 /*wip*/
  { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION},
  { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION},
      '\0', "FUNCTION", "Specify function to profile",
      '\0', "FUNCTION", "Specify function to profile",
      profile_option_handler },
      profile_option_handler },
#endif
#endif
#endif
#endif
 
 
  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
};
};
 
 
/* Set/reset the profile options indicated in MASK.  */
/* Set/reset the profile options indicated in MASK.  */
 
 
SIM_RC
SIM_RC
set_profile_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg)
set_profile_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg)
{
{
  int profile_nr;
  int profile_nr;
  int cpu_nr;
  int cpu_nr;
  int profile_val = 1;
  int profile_val = 1;
 
 
  if (arg != NULL)
  if (arg != NULL)
    {
    {
      if (strcmp (arg, "yes") == 0
      if (strcmp (arg, "yes") == 0
          || strcmp (arg, "on") == 0
          || strcmp (arg, "on") == 0
          || strcmp (arg, "1") == 0)
          || strcmp (arg, "1") == 0)
        profile_val = 1;
        profile_val = 1;
      else if (strcmp (arg, "no") == 0
      else if (strcmp (arg, "no") == 0
               || strcmp (arg, "off") == 0
               || strcmp (arg, "off") == 0
               || strcmp (arg, "0") == 0)
               || strcmp (arg, "0") == 0)
        profile_val = 0;
        profile_val = 0;
      else
      else
        {
        {
          sim_io_eprintf (sd, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name);
          sim_io_eprintf (sd, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name);
          return SIM_RC_FAIL;
          return SIM_RC_FAIL;
        }
        }
    }
    }
 
 
  /* update applicable profile bits */
  /* update applicable profile bits */
  for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
  for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
    {
    {
      if ((mask & (1 << profile_nr)) == 0)
      if ((mask & (1 << profile_nr)) == 0)
        continue;
        continue;
 
 
#if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
#if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
      /* Set non-cpu specific values.  */
      /* Set non-cpu specific values.  */
      switch (profile_nr)
      switch (profile_nr)
        {
        {
        case ??? :
        case ??? :
          break;
          break;
        }
        }
#endif
#endif
 
 
      /* Set cpu values.  */
      /* Set cpu values.  */
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
        {
        {
          CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr] = profile_val;
          CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr] = profile_val;
        }
        }
    }
    }
 
 
  /* Re-compute the cpu profile summary.  */
  /* Re-compute the cpu profile summary.  */
  if (profile_val)
  if (profile_val)
    {
    {
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
        CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
        CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
    }
    }
  else
  else
    {
    {
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
        {
        {
          CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 0;
          CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 0;
          for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
          for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
            {
            {
              if (CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr])
              if (CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr])
                {
                {
                  CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
                  CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
                  break;
                  break;
                }
                }
            }
            }
        }
        }
    }
    }
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
/* Set one profile option based on its IDX value.
/* Set one profile option based on its IDX value.
   Not static as cgen-scache.c uses it.  */
   Not static as cgen-scache.c uses it.  */
 
 
SIM_RC
SIM_RC
sim_profile_set_option (SIM_DESC sd, const char *name, int idx, const char *arg)
sim_profile_set_option (SIM_DESC sd, const char *name, int idx, const char *arg)
{
{
  return set_profile_option_mask (sd, name, 1 << idx, arg);
  return set_profile_option_mask (sd, name, 1 << idx, arg);
}
}
 
 
static SIM_RC
static SIM_RC
parse_frequency (SIM_DESC sd, const char *arg, unsigned long *freq)
parse_frequency (SIM_DESC sd, const char *arg, unsigned long *freq)
{
{
  const char *ch;
  const char *ch;
  /* First, parse a decimal number.  */
  /* First, parse a decimal number.  */
  *freq = 0;
  *freq = 0;
  ch = arg;
  ch = arg;
  if (isdigit (*arg))
  if (isdigit (*arg))
    {
    {
      for (/**/; *ch != '\0'; ++ch)
      for (/**/; *ch != '\0'; ++ch)
        {
        {
          if (! isdigit (*ch))
          if (! isdigit (*ch))
            break;
            break;
          *freq = *freq * 10 + (*ch - '0');
          *freq = *freq * 10 + (*ch - '0');
        }
        }
 
 
      /* Accept KHz, MHz or Hz as a suffix.  */
      /* Accept KHz, MHz or Hz as a suffix.  */
      if (tolower (*ch) == 'm')
      if (tolower (*ch) == 'm')
        {
        {
          *freq *= 1000000;
          *freq *= 1000000;
          ++ch;
          ++ch;
        }
        }
      else if (tolower (*ch) == 'k')
      else if (tolower (*ch) == 'k')
        {
        {
          *freq *= 1000;
          *freq *= 1000;
          ++ch;
          ++ch;
        }
        }
 
 
      if (tolower (*ch) == 'h')
      if (tolower (*ch) == 'h')
        {
        {
          ++ch;
          ++ch;
          if (tolower (*ch) == 'z')
          if (tolower (*ch) == 'z')
            ++ch;
            ++ch;
        }
        }
    }
    }
 
 
  if (*ch != '\0')
  if (*ch != '\0')
    {
    {
      sim_io_eprintf (sd, "Invalid argument for --profile-cpu-frequency: %s\n",
      sim_io_eprintf (sd, "Invalid argument for --profile-cpu-frequency: %s\n",
                      arg);
                      arg);
      *freq = 0;
      *freq = 0;
      return SIM_RC_FAIL;
      return SIM_RC_FAIL;
    }
    }
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
static SIM_RC
static SIM_RC
profile_option_handler (SIM_DESC sd,
profile_option_handler (SIM_DESC sd,
                        sim_cpu *cpu,
                        sim_cpu *cpu,
                        int opt,
                        int opt,
                        char *arg,
                        char *arg,
                        int is_command)
                        int is_command)
{
{
  int cpu_nr;
  int cpu_nr;
 
 
  /* FIXME: Need to handle `cpu' arg.  */
  /* FIXME: Need to handle `cpu' arg.  */
 
 
  switch (opt)
  switch (opt)
    {
    {
    case 'p' :
    case 'p' :
      if (! WITH_PROFILE)
      if (! WITH_PROFILE)
        sim_io_eprintf (sd, "Profiling not compiled in, `-p' ignored\n");
        sim_io_eprintf (sd, "Profiling not compiled in, `-p' ignored\n");
      else
      else
        return set_profile_option_mask (sd, "profile", PROFILE_USEFUL_MASK,
        return set_profile_option_mask (sd, "profile", PROFILE_USEFUL_MASK,
                                        arg);
                                        arg);
      break;
      break;
 
 
    case OPTION_PROFILE_INSN :
    case OPTION_PROFILE_INSN :
      if (WITH_PROFILE_INSN_P)
      if (WITH_PROFILE_INSN_P)
        return sim_profile_set_option (sd, "-insn", PROFILE_INSN_IDX, arg);
        return sim_profile_set_option (sd, "-insn", PROFILE_INSN_IDX, arg);
      else
      else
        sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
        sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_MEMORY :
    case OPTION_PROFILE_MEMORY :
      if (WITH_PROFILE_MEMORY_P)
      if (WITH_PROFILE_MEMORY_P)
        return sim_profile_set_option (sd, "-memory", PROFILE_MEMORY_IDX, arg);
        return sim_profile_set_option (sd, "-memory", PROFILE_MEMORY_IDX, arg);
      else
      else
        sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
        sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_CORE :
    case OPTION_PROFILE_CORE :
      if (WITH_PROFILE_CORE_P)
      if (WITH_PROFILE_CORE_P)
        return sim_profile_set_option (sd, "-core", PROFILE_CORE_IDX, arg);
        return sim_profile_set_option (sd, "-core", PROFILE_CORE_IDX, arg);
      else
      else
        sim_io_eprintf (sd, "CORE profiling not compiled in, `--profile-core' ignored\n");
        sim_io_eprintf (sd, "CORE profiling not compiled in, `--profile-core' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_MODEL :
    case OPTION_PROFILE_MODEL :
      if (WITH_PROFILE_MODEL_P)
      if (WITH_PROFILE_MODEL_P)
        return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, arg);
        return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, arg);
      else
      else
        sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
        sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_CPU_FREQUENCY :
    case OPTION_PROFILE_CPU_FREQUENCY :
      {
      {
        unsigned long val;
        unsigned long val;
        SIM_RC rc = parse_frequency (sd, arg, &val);
        SIM_RC rc = parse_frequency (sd, arg, &val);
        if (rc == SIM_RC_OK)
        if (rc == SIM_RC_OK)
          {
          {
            for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
              PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd,cpu_nr))) = val;
              PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd,cpu_nr))) = val;
          }
          }
        return rc;
        return rc;
      }
      }
 
 
    case OPTION_PROFILE_FILE :
    case OPTION_PROFILE_FILE :
      /* FIXME: Might want this to apply to pc profiling only,
      /* FIXME: Might want this to apply to pc profiling only,
         or have two profile file options.  */
         or have two profile file options.  */
      if (! WITH_PROFILE)
      if (! WITH_PROFILE)
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
      else
      else
        {
        {
          FILE *f = fopen (arg, "w");
          FILE *f = fopen (arg, "w");
 
 
          if (f == NULL)
          if (f == NULL)
            {
            {
              sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
              sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
              return SIM_RC_FAIL;
              return SIM_RC_FAIL;
            }
            }
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = f;
            PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = f;
        }
        }
      break;
      break;
 
 
    case OPTION_PROFILE_PC:
    case OPTION_PROFILE_PC:
      if (WITH_PROFILE_PC_P)
      if (WITH_PROFILE_PC_P)
        return sim_profile_set_option (sd, "-pc", PROFILE_PC_IDX, arg);
        return sim_profile_set_option (sd, "-pc", PROFILE_PC_IDX, arg);
      else
      else
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc' ignored\n");
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc' ignored\n");
      break;
      break;
 
 
    case 'F' :
    case 'F' :
      if (WITH_PROFILE_PC_P)
      if (WITH_PROFILE_PC_P)
        {
        {
          /* FIXME: Validate arg.  */
          /* FIXME: Validate arg.  */
          int val = atoi (arg);
          int val = atoi (arg);
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
            PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
        }
        }
      else
      else
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
      break;
      break;
 
 
    case 'S' :
    case 'S' :
      if (WITH_PROFILE_PC_P)
      if (WITH_PROFILE_PC_P)
        {
        {
          /* FIXME: Validate arg.  */
          /* FIXME: Validate arg.  */
          int val = atoi (arg);
          int val = atoi (arg);
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
            PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
        }
        }
      else
      else
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_PC_GRANULARITY:
    case OPTION_PROFILE_PC_GRANULARITY:
      if (WITH_PROFILE_PC_P)
      if (WITH_PROFILE_PC_P)
        {
        {
          int shift;
          int shift;
          int val = atoi (arg);
          int val = atoi (arg);
          /* check that the granularity is a power of two */
          /* check that the granularity is a power of two */
          shift = 0;
          shift = 0;
          while (val > (1 << shift))
          while (val > (1 << shift))
            {
            {
              shift += 1;
              shift += 1;
            }
            }
          if (val != (1 << shift))
          if (val != (1 << shift))
            {
            {
              sim_io_eprintf (sd, "PC profiling granularity not a power of two\n");
              sim_io_eprintf (sd, "PC profiling granularity not a power of two\n");
              return SIM_RC_FAIL;
              return SIM_RC_FAIL;
            }
            }
          if (shift == 0)
          if (shift == 0)
            {
            {
              sim_io_eprintf (sd, "PC profiling granularity too small");
              sim_io_eprintf (sd, "PC profiling granularity too small");
              return SIM_RC_FAIL;
              return SIM_RC_FAIL;
            }
            }
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = shift;
            PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = shift;
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
        }
        }
      else
      else
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_PC_RANGE:
    case OPTION_PROFILE_PC_RANGE:
      if (WITH_PROFILE_PC_P)
      if (WITH_PROFILE_PC_P)
        {
        {
          /* FIXME: Validate args */
          /* FIXME: Validate args */
          char *chp = arg;
          char *chp = arg;
          unsigned long base;
          unsigned long base;
          unsigned long bound;
          unsigned long bound;
          base = strtoul (chp, &chp, 0);
          base = strtoul (chp, &chp, 0);
          if (*chp != ',')
          if (*chp != ',')
            {
            {
              sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n");
              sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n");
              return SIM_RC_FAIL;
              return SIM_RC_FAIL;
            }
            }
          bound = strtoul (chp + 1, NULL, 0);
          bound = strtoul (chp + 1, NULL, 0);
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            {
            {
              PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = base;
              PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = base;
              PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = bound;
              PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = bound;
            }
            }
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
          for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
            CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
        }
        }
      else
      else
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
        sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
      break;
      break;
 
 
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
    case OPTION_PROFILE_RANGE :
    case OPTION_PROFILE_RANGE :
      if (WITH_PROFILE)
      if (WITH_PROFILE)
        {
        {
          char *chp = arg;
          char *chp = arg;
          unsigned long start,end;
          unsigned long start,end;
          start = strtoul (chp, &chp, 0);
          start = strtoul (chp, &chp, 0);
          if (*chp != ',')
          if (*chp != ',')
            {
            {
              sim_io_eprintf (sd, "--profile-range missing END argument\n");
              sim_io_eprintf (sd, "--profile-range missing END argument\n");
              return SIM_RC_FAIL;
              return SIM_RC_FAIL;
            }
            }
          end = strtoul (chp + 1, NULL, 0);
          end = strtoul (chp + 1, NULL, 0);
          /* FIXME: Argument validation.  */
          /* FIXME: Argument validation.  */
          if (cpu != NULL)
          if (cpu != NULL)
            sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
            sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
                                start, end);
                                start, end);
          else
          else
            for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
            for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
              sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))),
              sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))),
                                  start, end);
                                  start, end);
        }
        }
      else
      else
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-range' ignored\n");
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-range' ignored\n");
      break;
      break;
 
 
    case OPTION_PROFILE_FUNCTION :
    case OPTION_PROFILE_FUNCTION :
      if (WITH_PROFILE)
      if (WITH_PROFILE)
        {
        {
          /*wip: need to compute function range given name*/
          /*wip: need to compute function range given name*/
        }
        }
      else
      else
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-function' ignored\n");
        sim_io_eprintf (sd, "Profiling not compiled in, `--profile-function' ignored\n");
      break;
      break;
#endif /* SIM_HAVE_ADDR_RANGE */
#endif /* SIM_HAVE_ADDR_RANGE */
    }
    }
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}


/* PC profiling support */
/* PC profiling support */
 
 
#if WITH_PROFILE_PC_P
#if WITH_PROFILE_PC_P
 
 
static void
static void
profile_pc_cleanup (SIM_DESC sd)
profile_pc_cleanup (SIM_DESC sd)
{
{
  int n;
  int n;
  for (n = 0; n < MAX_NR_PROCESSORS; n++)
  for (n = 0; n < MAX_NR_PROCESSORS; n++)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, n);
      sim_cpu *cpu = STATE_CPU (sd, n);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      if (PROFILE_PC_COUNT (data) != NULL)
      if (PROFILE_PC_COUNT (data) != NULL)
        zfree (PROFILE_PC_COUNT (data));
        zfree (PROFILE_PC_COUNT (data));
      PROFILE_PC_COUNT (data) = NULL;
      PROFILE_PC_COUNT (data) = NULL;
      if (PROFILE_PC_EVENT (data) != NULL)
      if (PROFILE_PC_EVENT (data) != NULL)
        sim_events_deschedule (sd, PROFILE_PC_EVENT (data));
        sim_events_deschedule (sd, PROFILE_PC_EVENT (data));
      PROFILE_PC_EVENT (data) = NULL;
      PROFILE_PC_EVENT (data) = NULL;
    }
    }
}
}
 
 
 
 
static void
static void
profile_pc_uninstall (SIM_DESC sd)
profile_pc_uninstall (SIM_DESC sd)
{
{
  profile_pc_cleanup (sd);
  profile_pc_cleanup (sd);
}
}
 
 
static void
static void
profile_pc_event (SIM_DESC sd,
profile_pc_event (SIM_DESC sd,
                  void *data)
                  void *data)
{
{
  sim_cpu *cpu = (sim_cpu*) data;
  sim_cpu *cpu = (sim_cpu*) data;
  PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
  address_word pc;
  address_word pc;
  unsigned i;
  unsigned i;
  switch (STATE_WATCHPOINTS (sd)->sizeof_pc)
  switch (STATE_WATCHPOINTS (sd)->sizeof_pc)
    {
    {
    case 2: pc = *(unsigned_2*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    case 2: pc = *(unsigned_2*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    case 4: pc = *(unsigned_4*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    case 4: pc = *(unsigned_4*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    case 8: pc = *(unsigned_8*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    case 8: pc = *(unsigned_8*)(STATE_WATCHPOINTS (sd)->pc) ; break;
    default: pc = 0;
    default: pc = 0;
    }
    }
  i = (pc - PROFILE_PC_START (profile)) >> PROFILE_PC_SHIFT (profile);
  i = (pc - PROFILE_PC_START (profile)) >> PROFILE_PC_SHIFT (profile);
  if (i < PROFILE_PC_NR_BUCKETS (profile))
  if (i < PROFILE_PC_NR_BUCKETS (profile))
    PROFILE_PC_COUNT (profile) [i] += 1; /* Overflow? */
    PROFILE_PC_COUNT (profile) [i] += 1; /* Overflow? */
  else
  else
    PROFILE_PC_COUNT (profile) [PROFILE_PC_NR_BUCKETS (profile)] += 1;
    PROFILE_PC_COUNT (profile) [PROFILE_PC_NR_BUCKETS (profile)] += 1;
  PROFILE_PC_EVENT (profile) =
  PROFILE_PC_EVENT (profile) =
    sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu);
    sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu);
}
}
 
 
static SIM_RC
static SIM_RC
profile_pc_init (SIM_DESC sd)
profile_pc_init (SIM_DESC sd)
{
{
  int n;
  int n;
  profile_pc_cleanup (sd);
  profile_pc_cleanup (sd);
  for (n = 0; n < MAX_NR_PROCESSORS; n++)
  for (n = 0; n < MAX_NR_PROCESSORS; n++)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, n);
      sim_cpu *cpu = STATE_CPU (sd, n);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX]
      if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX]
          && STATE_WATCHPOINTS (sd)->pc != NULL)
          && STATE_WATCHPOINTS (sd)->pc != NULL)
        {
        {
          int bucket_size;
          int bucket_size;
          /* fill in the frequency if not specified */
          /* fill in the frequency if not specified */
          if (PROFILE_PC_FREQ (data) == 0)
          if (PROFILE_PC_FREQ (data) == 0)
            PROFILE_PC_FREQ (data) = 257;
            PROFILE_PC_FREQ (data) = 257;
          /* fill in the start/end if not specified */
          /* fill in the start/end if not specified */
          if (PROFILE_PC_END (data) == 0)
          if (PROFILE_PC_END (data) == 0)
            {
            {
              PROFILE_PC_START (data) = STATE_TEXT_START (sd);
              PROFILE_PC_START (data) = STATE_TEXT_START (sd);
              PROFILE_PC_END (data) = STATE_TEXT_END (sd);
              PROFILE_PC_END (data) = STATE_TEXT_END (sd);
            }
            }
          /* Compute the number of buckets if not specified. */
          /* Compute the number of buckets if not specified. */
          if (PROFILE_PC_NR_BUCKETS (data) == 0)
          if (PROFILE_PC_NR_BUCKETS (data) == 0)
            {
            {
              if (PROFILE_PC_BUCKET_SIZE (data) == 0)
              if (PROFILE_PC_BUCKET_SIZE (data) == 0)
                PROFILE_PC_NR_BUCKETS (data) = 16;
                PROFILE_PC_NR_BUCKETS (data) = 16;
              else
              else
                {
                {
                  if (PROFILE_PC_END (data) == 0)
                  if (PROFILE_PC_END (data) == 0)
                    {
                    {
                      /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
                      /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
                      PROFILE_PC_NR_BUCKETS (data) =
                      PROFILE_PC_NR_BUCKETS (data) =
                        ((1 << (STATE_WATCHPOINTS (sd)->sizeof_pc) * (8 - 1))
                        ((1 << (STATE_WATCHPOINTS (sd)->sizeof_pc) * (8 - 1))
                         / (PROFILE_PC_BUCKET_SIZE (data) / 2));
                         / (PROFILE_PC_BUCKET_SIZE (data) / 2));
                    }
                    }
                  else
                  else
                    {
                    {
                      PROFILE_PC_NR_BUCKETS (data) =
                      PROFILE_PC_NR_BUCKETS (data) =
                        ((PROFILE_PC_END (data)
                        ((PROFILE_PC_END (data)
                          - PROFILE_PC_START (data)
                          - PROFILE_PC_START (data)
                          + PROFILE_PC_BUCKET_SIZE (data) - 1)
                          + PROFILE_PC_BUCKET_SIZE (data) - 1)
                         / PROFILE_PC_BUCKET_SIZE (data));
                         / PROFILE_PC_BUCKET_SIZE (data));
                    }
                    }
                }
                }
            }
            }
          /* Compute the bucket size if not specified.  Ensure that it
          /* Compute the bucket size if not specified.  Ensure that it
             is rounded up to the next power of two */
             is rounded up to the next power of two */
          if (PROFILE_PC_BUCKET_SIZE (data) == 0)
          if (PROFILE_PC_BUCKET_SIZE (data) == 0)
            {
            {
              if (PROFILE_PC_END (data) == 0)
              if (PROFILE_PC_END (data) == 0)
                /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
                /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
                bucket_size = ((1 << ((STATE_WATCHPOINTS (sd)->sizeof_pc * 8) - 1))
                bucket_size = ((1 << ((STATE_WATCHPOINTS (sd)->sizeof_pc * 8) - 1))
                               / (PROFILE_PC_NR_BUCKETS (data) / 2));
                               / (PROFILE_PC_NR_BUCKETS (data) / 2));
              else
              else
                bucket_size = ((PROFILE_PC_END (data)
                bucket_size = ((PROFILE_PC_END (data)
                                - PROFILE_PC_START (data)
                                - PROFILE_PC_START (data)
                                + PROFILE_PC_NR_BUCKETS (data) - 1)
                                + PROFILE_PC_NR_BUCKETS (data) - 1)
                               / PROFILE_PC_NR_BUCKETS (data));
                               / PROFILE_PC_NR_BUCKETS (data));
              PROFILE_PC_SHIFT (data) = 0;
              PROFILE_PC_SHIFT (data) = 0;
              while (bucket_size > PROFILE_PC_BUCKET_SIZE (data))
              while (bucket_size > PROFILE_PC_BUCKET_SIZE (data))
                {
                {
                  PROFILE_PC_SHIFT (data) += 1;
                  PROFILE_PC_SHIFT (data) += 1;
                }
                }
            }
            }
          /* Align the end address with bucket size */
          /* Align the end address with bucket size */
          if (PROFILE_PC_END (data) != 0)
          if (PROFILE_PC_END (data) != 0)
            PROFILE_PC_END (data) = (PROFILE_PC_START (data)
            PROFILE_PC_END (data) = (PROFILE_PC_START (data)
                                     + (PROFILE_PC_BUCKET_SIZE (data)
                                     + (PROFILE_PC_BUCKET_SIZE (data)
                                        * PROFILE_PC_NR_BUCKETS (data)));
                                        * PROFILE_PC_NR_BUCKETS (data)));
          /* create the relevant buffers */
          /* create the relevant buffers */
          PROFILE_PC_COUNT (data) =
          PROFILE_PC_COUNT (data) =
            NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data) + 1);
            NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data) + 1);
          PROFILE_PC_EVENT (data) =
          PROFILE_PC_EVENT (data) =
            sim_events_schedule (sd,
            sim_events_schedule (sd,
                                 PROFILE_PC_FREQ (data),
                                 PROFILE_PC_FREQ (data),
                                 profile_pc_event,
                                 profile_pc_event,
                                 cpu);
                                 cpu);
        }
        }
    }
    }
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
static void
static void
profile_print_pc (sim_cpu *cpu, int verbose)
profile_print_pc (sim_cpu *cpu, int verbose)
{
{
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
  char comma_buf[20];
  char comma_buf[20];
  unsigned max_val;
  unsigned max_val;
  unsigned total;
  unsigned total;
  unsigned i;
  unsigned i;
 
 
  if (PROFILE_PC_COUNT (profile) == 0)
  if (PROFILE_PC_COUNT (profile) == 0)
    return;
    return;
 
 
  sim_io_printf (sd, "Program Counter Statistics:\n\n");
  sim_io_printf (sd, "Program Counter Statistics:\n\n");
 
 
  /* First pass over data computes various things.  */
  /* First pass over data computes various things.  */
  max_val = 0;
  max_val = 0;
  total = 0;
  total = 0;
  for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
  for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
    {
    {
      total += PROFILE_PC_COUNT (profile) [i];
      total += PROFILE_PC_COUNT (profile) [i];
      if (PROFILE_PC_COUNT (profile) [i] > max_val)
      if (PROFILE_PC_COUNT (profile) [i] > max_val)
        max_val = PROFILE_PC_COUNT (profile) [i];
        max_val = PROFILE_PC_COUNT (profile) [i];
    }
    }
 
 
  sim_io_printf (sd, "  Total samples: %s\n",
  sim_io_printf (sd, "  Total samples: %s\n",
                 COMMAS (total));
                 COMMAS (total));
  sim_io_printf (sd, "  Granularity: %s bytes per bucket\n",
  sim_io_printf (sd, "  Granularity: %s bytes per bucket\n",
                 COMMAS (PROFILE_PC_BUCKET_SIZE (profile)));
                 COMMAS (PROFILE_PC_BUCKET_SIZE (profile)));
  sim_io_printf (sd, "  Size: %s buckets\n",
  sim_io_printf (sd, "  Size: %s buckets\n",
                 COMMAS (PROFILE_PC_NR_BUCKETS (profile)));
                 COMMAS (PROFILE_PC_NR_BUCKETS (profile)));
  sim_io_printf (sd, "  Frequency: %s cycles per sample\n",
  sim_io_printf (sd, "  Frequency: %s cycles per sample\n",
                 COMMAS (PROFILE_PC_FREQ (profile)));
                 COMMAS (PROFILE_PC_FREQ (profile)));
 
 
  if (PROFILE_PC_END (profile) != 0)
  if (PROFILE_PC_END (profile) != 0)
    sim_io_printf (sd, "  Range: 0x%lx 0x%lx\n",
    sim_io_printf (sd, "  Range: 0x%lx 0x%lx\n",
                   (long) PROFILE_PC_START (profile),
                   (long) PROFILE_PC_START (profile),
                   (long) PROFILE_PC_END (profile));
                   (long) PROFILE_PC_END (profile));
 
 
  if (verbose && max_val != 0)
  if (verbose && max_val != 0)
    {
    {
      /* Now we can print the histogram.  */
      /* Now we can print the histogram.  */
      sim_io_printf (sd, "\n");
      sim_io_printf (sd, "\n");
      for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
      for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
        {
        {
          if (PROFILE_PC_COUNT (profile) [i] != 0)
          if (PROFILE_PC_COUNT (profile) [i] != 0)
            {
            {
              sim_io_printf (sd, "  ");
              sim_io_printf (sd, "  ");
              if (i == PROFILE_PC_NR_BUCKETS (profile))
              if (i == PROFILE_PC_NR_BUCKETS (profile))
                sim_io_printf (sd, "%10s:", "overflow");
                sim_io_printf (sd, "%10s:", "overflow");
              else
              else
                sim_io_printf (sd, "0x%08lx:",
                sim_io_printf (sd, "0x%08lx:",
                               (long) (PROFILE_PC_START (profile)
                               (long) (PROFILE_PC_START (profile)
                                       + (i * PROFILE_PC_BUCKET_SIZE (profile))));
                                       + (i * PROFILE_PC_BUCKET_SIZE (profile))));
              sim_io_printf (sd, " %*s",
              sim_io_printf (sd, " %*s",
                             max_val < 10000 ? 5 : 10,
                             max_val < 10000 ? 5 : 10,
                             COMMAS (PROFILE_PC_COUNT (profile) [i]));
                             COMMAS (PROFILE_PC_COUNT (profile) [i]));
              sim_io_printf (sd, " %4.1f",
              sim_io_printf (sd, " %4.1f",
                             (PROFILE_PC_COUNT (profile) [i] * 100.0) / total);
                             (PROFILE_PC_COUNT (profile) [i] * 100.0) / total);
              sim_io_printf (sd, ": ");
              sim_io_printf (sd, ": ");
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
                                     PROFILE_PC_COUNT (profile) [i],
                                     PROFILE_PC_COUNT (profile) [i],
                                     max_val);
                                     max_val);
              sim_io_printf (sd, "\n");
              sim_io_printf (sd, "\n");
            }
            }
        }
        }
    }
    }
 
 
  /* dump the histogram to the file "gmon.out" using BSD's gprof file
  /* dump the histogram to the file "gmon.out" using BSD's gprof file
     format */
     format */
  /* Since a profile data file is in the native format of the host on
  /* Since a profile data file is in the native format of the host on
     which the profile is being, endian issues are not considered in
     which the profile is being, endian issues are not considered in
     the code below. */
     the code below. */
  /* FIXME: Is this the best place for this code? */
  /* FIXME: Is this the best place for this code? */
  {
  {
    FILE *pf = fopen ("gmon.out", "wb");
    FILE *pf = fopen ("gmon.out", "wb");
 
 
    if (pf == NULL)
    if (pf == NULL)
      sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
      sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
    else
    else
      {
      {
        int ok;
        int ok;
        /* FIXME: what if the target has a 64 bit PC? */
        /* FIXME: what if the target has a 64 bit PC? */
        unsigned32 header[3];
        unsigned32 header[3];
        unsigned loop;
        unsigned loop;
        if (PROFILE_PC_END (profile) != 0)
        if (PROFILE_PC_END (profile) != 0)
          {
          {
            header[0] = PROFILE_PC_START (profile);
            header[0] = PROFILE_PC_START (profile);
            header[1] = PROFILE_PC_END (profile);
            header[1] = PROFILE_PC_END (profile);
          }
          }
        else
        else
          {
          {
            header[0] = 0;
            header[0] = 0;
            header[1] = 0;
            header[1] = 0;
          }
          }
        /* size of sample buffer (+ header) */
        /* size of sample buffer (+ header) */
        header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
        header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
 
 
        /* Header must be written out in target byte order.  */
        /* Header must be written out in target byte order.  */
        H2T (header[0]);
        H2T (header[0]);
        H2T (header[1]);
        H2T (header[1]);
        H2T (header[2]);
        H2T (header[2]);
 
 
        ok = fwrite (&header, sizeof (header), 1, pf);
        ok = fwrite (&header, sizeof (header), 1, pf);
        for (loop = 0;
        for (loop = 0;
             ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
             ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
             loop++)
             loop++)
          {
          {
            signed16 sample;
            signed16 sample;
            if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
            if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
              sample = 0xffff;
              sample = 0xffff;
            else
            else
              sample = PROFILE_PC_COUNT (profile) [loop];
              sample = PROFILE_PC_COUNT (profile) [loop];
            H2T (sample);
            H2T (sample);
            ok = fwrite (&sample, sizeof (sample), 1, pf);
            ok = fwrite (&sample, sizeof (sample), 1, pf);
          }
          }
        if (ok == 0)
        if (ok == 0)
          sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
          sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
        fclose(pf);
        fclose(pf);
      }
      }
  }
  }
 
 
  sim_io_printf (sd, "\n");
  sim_io_printf (sd, "\n");
}
}
 
 
#endif
#endif


/* Summary printing support.  */
/* Summary printing support.  */
 
 
#if WITH_PROFILE_INSN_P
#if WITH_PROFILE_INSN_P
 
 
static SIM_RC
static SIM_RC
profile_insn_init (SIM_DESC sd)
profile_insn_init (SIM_DESC sd)
{
{
  int c;
  int c;
 
 
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, c);
      sim_cpu *cpu = STATE_CPU (sd, c);
 
 
      if (CPU_MAX_INSNS (cpu) > 0)
      if (CPU_MAX_INSNS (cpu) > 0)
        PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu));
        PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu));
    }
    }
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
static void
static void
profile_print_insn (sim_cpu *cpu, int verbose)
profile_print_insn (sim_cpu *cpu, int verbose)
{
{
  unsigned int i, n, total, max_val, max_name_len;
  unsigned int i, n, total, max_val, max_name_len;
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  char comma_buf[20];
  char comma_buf[20];
 
 
  /* If MAX_INSNS not set, insn profiling isn't supported.  */
  /* If MAX_INSNS not set, insn profiling isn't supported.  */
  if (CPU_MAX_INSNS (cpu) == 0)
  if (CPU_MAX_INSNS (cpu) == 0)
    return;
    return;
 
 
  sim_io_printf (sd, "Instruction Statistics");
  sim_io_printf (sd, "Instruction Statistics");
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
  if (PROFILE_RANGE (data)->ranges)
  if (PROFILE_RANGE (data)->ranges)
    sim_io_printf (sd, " (for selected address range(s))");
    sim_io_printf (sd, " (for selected address range(s))");
#endif
#endif
  sim_io_printf (sd, "\n\n");
  sim_io_printf (sd, "\n\n");
 
 
  /* First pass over data computes various things.  */
  /* First pass over data computes various things.  */
  max_val = 0;
  max_val = 0;
  total = 0;
  total = 0;
  max_name_len = 0;
  max_name_len = 0;
  for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
  for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
    {
    {
      const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
      const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
 
 
      if (name == NULL)
      if (name == NULL)
        continue;
        continue;
      total += PROFILE_INSN_COUNT (data) [i];
      total += PROFILE_INSN_COUNT (data) [i];
      if (PROFILE_INSN_COUNT (data) [i] > max_val)
      if (PROFILE_INSN_COUNT (data) [i] > max_val)
        max_val = PROFILE_INSN_COUNT (data) [i];
        max_val = PROFILE_INSN_COUNT (data) [i];
      n = strlen (name);
      n = strlen (name);
      if (n > max_name_len)
      if (n > max_name_len)
        max_name_len = n;
        max_name_len = n;
    }
    }
  /* set the total insn count, in case client is being lazy */
  /* set the total insn count, in case client is being lazy */
  if (! PROFILE_TOTAL_INSN_COUNT (data))
  if (! PROFILE_TOTAL_INSN_COUNT (data))
    PROFILE_TOTAL_INSN_COUNT (data) = total;
    PROFILE_TOTAL_INSN_COUNT (data) = total;
 
 
  sim_io_printf (sd, "  Total: %s insns\n", COMMAS (total));
  sim_io_printf (sd, "  Total: %s insns\n", COMMAS (total));
 
 
  if (verbose && max_val != 0)
  if (verbose && max_val != 0)
    {
    {
      /* Now we can print the histogram.  */
      /* Now we can print the histogram.  */
      sim_io_printf (sd, "\n");
      sim_io_printf (sd, "\n");
      for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
      for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
        {
        {
          const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
          const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
 
 
          if (name == NULL)
          if (name == NULL)
            continue;
            continue;
          if (PROFILE_INSN_COUNT (data) [i] != 0)
          if (PROFILE_INSN_COUNT (data) [i] != 0)
            {
            {
              sim_io_printf (sd, "   %*s: %*s: ",
              sim_io_printf (sd, "   %*s: %*s: ",
                             max_name_len, name,
                             max_name_len, name,
                             max_val < 10000 ? 5 : 10,
                             max_val < 10000 ? 5 : 10,
                             COMMAS (PROFILE_INSN_COUNT (data) [i]));
                             COMMAS (PROFILE_INSN_COUNT (data) [i]));
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
                                     PROFILE_INSN_COUNT (data) [i],
                                     PROFILE_INSN_COUNT (data) [i],
                                     max_val);
                                     max_val);
              sim_io_printf (sd, "\n");
              sim_io_printf (sd, "\n");
            }
            }
        }
        }
    }
    }
 
 
  sim_io_printf (sd, "\n");
  sim_io_printf (sd, "\n");
}
}
 
 
#endif
#endif
 
 
#if WITH_PROFILE_MEMORY_P
#if WITH_PROFILE_MEMORY_P
 
 
static void
static void
profile_print_memory (sim_cpu *cpu, int verbose)
profile_print_memory (sim_cpu *cpu, int verbose)
{
{
  unsigned int i, n;
  unsigned int i, n;
  unsigned int total_read, total_write;
  unsigned int total_read, total_write;
  unsigned int max_val, max_name_len;
  unsigned int max_val, max_name_len;
  /* FIXME: Need to add smp support.  */
  /* FIXME: Need to add smp support.  */
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  char comma_buf[20];
  char comma_buf[20];
 
 
  sim_io_printf (sd, "Memory Access Statistics\n\n");
  sim_io_printf (sd, "Memory Access Statistics\n\n");
 
 
  /* First pass over data computes various things.  */
  /* First pass over data computes various things.  */
  max_val = total_read = total_write = max_name_len = 0;
  max_val = total_read = total_write = max_name_len = 0;
  for (i = 0; i < MODE_TARGET_MAX; ++i)
  for (i = 0; i < MODE_TARGET_MAX; ++i)
    {
    {
      total_read += PROFILE_READ_COUNT (data) [i];
      total_read += PROFILE_READ_COUNT (data) [i];
      total_write += PROFILE_WRITE_COUNT (data) [i];
      total_write += PROFILE_WRITE_COUNT (data) [i];
      if (PROFILE_READ_COUNT (data) [i] > max_val)
      if (PROFILE_READ_COUNT (data) [i] > max_val)
        max_val = PROFILE_READ_COUNT (data) [i];
        max_val = PROFILE_READ_COUNT (data) [i];
      if (PROFILE_WRITE_COUNT (data) [i] > max_val)
      if (PROFILE_WRITE_COUNT (data) [i] > max_val)
        max_val = PROFILE_WRITE_COUNT (data) [i];
        max_val = PROFILE_WRITE_COUNT (data) [i];
      n = strlen (MODE_NAME (i));
      n = strlen (MODE_NAME (i));
      if (n > max_name_len)
      if (n > max_name_len)
        max_name_len = n;
        max_name_len = n;
    }
    }
 
 
  /* One could use PROFILE_LABEL_WIDTH here.  I chose not to.  */
  /* One could use PROFILE_LABEL_WIDTH here.  I chose not to.  */
  sim_io_printf (sd, "  Total read:  %s accesses\n",
  sim_io_printf (sd, "  Total read:  %s accesses\n",
                 COMMAS (total_read));
                 COMMAS (total_read));
  sim_io_printf (sd, "  Total write: %s accesses\n",
  sim_io_printf (sd, "  Total write: %s accesses\n",
                 COMMAS (total_write));
                 COMMAS (total_write));
 
 
  if (verbose && max_val != 0)
  if (verbose && max_val != 0)
    {
    {
      /* FIXME: Need to separate instruction fetches from data fetches
      /* FIXME: Need to separate instruction fetches from data fetches
         as the former swamps the latter.  */
         as the former swamps the latter.  */
      /* Now we can print the histogram.  */
      /* Now we can print the histogram.  */
      sim_io_printf (sd, "\n");
      sim_io_printf (sd, "\n");
      for (i = 0; i < MODE_TARGET_MAX; ++i)
      for (i = 0; i < MODE_TARGET_MAX; ++i)
        {
        {
          if (PROFILE_READ_COUNT (data) [i] != 0)
          if (PROFILE_READ_COUNT (data) [i] != 0)
            {
            {
              sim_io_printf (sd, "   %*s read:  %*s: ",
              sim_io_printf (sd, "   %*s read:  %*s: ",
                             max_name_len, MODE_NAME (i),
                             max_name_len, MODE_NAME (i),
                             max_val < 10000 ? 5 : 10,
                             max_val < 10000 ? 5 : 10,
                             COMMAS (PROFILE_READ_COUNT (data) [i]));
                             COMMAS (PROFILE_READ_COUNT (data) [i]));
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
                                     PROFILE_READ_COUNT (data) [i],
                                     PROFILE_READ_COUNT (data) [i],
                                     max_val);
                                     max_val);
              sim_io_printf (sd, "\n");
              sim_io_printf (sd, "\n");
            }
            }
          if (PROFILE_WRITE_COUNT (data) [i] != 0)
          if (PROFILE_WRITE_COUNT (data) [i] != 0)
            {
            {
              sim_io_printf (sd, "   %*s write: %*s: ",
              sim_io_printf (sd, "   %*s write: %*s: ",
                             max_name_len, MODE_NAME (i),
                             max_name_len, MODE_NAME (i),
                             max_val < 10000 ? 5 : 10,
                             max_val < 10000 ? 5 : 10,
                             COMMAS (PROFILE_WRITE_COUNT (data) [i]));
                             COMMAS (PROFILE_WRITE_COUNT (data) [i]));
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
                                     PROFILE_WRITE_COUNT (data) [i],
                                     PROFILE_WRITE_COUNT (data) [i],
                                     max_val);
                                     max_val);
              sim_io_printf (sd, "\n");
              sim_io_printf (sd, "\n");
            }
            }
        }
        }
    }
    }
 
 
  sim_io_printf (sd, "\n");
  sim_io_printf (sd, "\n");
}
}
 
 
#endif
#endif
 
 
#if WITH_PROFILE_CORE_P
#if WITH_PROFILE_CORE_P
 
 
static void
static void
profile_print_core (sim_cpu *cpu, int verbose)
profile_print_core (sim_cpu *cpu, int verbose)
{
{
  unsigned int total;
  unsigned int total;
  unsigned int max_val;
  unsigned int max_val;
  /* FIXME: Need to add smp support.  */
  /* FIXME: Need to add smp support.  */
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  char comma_buf[20];
  char comma_buf[20];
 
 
  sim_io_printf (sd, "CORE Statistics\n\n");
  sim_io_printf (sd, "CORE Statistics\n\n");
 
 
  /* First pass over data computes various things.  */
  /* First pass over data computes various things.  */
  {
  {
    unsigned map;
    unsigned map;
    total = 0;
    total = 0;
    max_val = 0;
    max_val = 0;
    for (map = 0; map < nr_maps; map++)
    for (map = 0; map < nr_maps; map++)
      {
      {
        total += PROFILE_CORE_COUNT (data) [map];
        total += PROFILE_CORE_COUNT (data) [map];
        if (PROFILE_CORE_COUNT (data) [map] > max_val)
        if (PROFILE_CORE_COUNT (data) [map] > max_val)
          max_val = PROFILE_CORE_COUNT (data) [map];
          max_val = PROFILE_CORE_COUNT (data) [map];
      }
      }
  }
  }
 
 
  /* One could use PROFILE_LABEL_WIDTH here.  I chose not to.  */
  /* One could use PROFILE_LABEL_WIDTH here.  I chose not to.  */
  sim_io_printf (sd, "  Total:  %s accesses\n",
  sim_io_printf (sd, "  Total:  %s accesses\n",
                 COMMAS (total));
                 COMMAS (total));
 
 
  if (verbose && max_val != 0)
  if (verbose && max_val != 0)
    {
    {
      unsigned map;
      unsigned map;
      /* Now we can print the histogram.  */
      /* Now we can print the histogram.  */
      sim_io_printf (sd, "\n");
      sim_io_printf (sd, "\n");
      for (map = 0; map < nr_maps; map++)
      for (map = 0; map < nr_maps; map++)
        {
        {
          if (PROFILE_CORE_COUNT (data) [map] != 0)
          if (PROFILE_CORE_COUNT (data) [map] != 0)
            {
            {
              sim_io_printf (sd, "%10s:", map_to_str (map));
              sim_io_printf (sd, "%10s:", map_to_str (map));
              sim_io_printf (sd, "%*s: ",
              sim_io_printf (sd, "%*s: ",
                             max_val < 10000 ? 5 : 10,
                             max_val < 10000 ? 5 : 10,
                             COMMAS (PROFILE_CORE_COUNT (data) [map]));
                             COMMAS (PROFILE_CORE_COUNT (data) [map]));
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
              sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
                                     PROFILE_CORE_COUNT (data) [map],
                                     PROFILE_CORE_COUNT (data) [map],
                                     max_val);
                                     max_val);
              sim_io_printf (sd, "\n");
              sim_io_printf (sd, "\n");
            }
            }
        }
        }
    }
    }
 
 
  sim_io_printf (sd, "\n");
  sim_io_printf (sd, "\n");
}
}
 
 
#endif
#endif
 
 
#if WITH_PROFILE_MODEL_P
#if WITH_PROFILE_MODEL_P
 
 
static void
static void
profile_print_model (sim_cpu *cpu, int verbose)
profile_print_model (sim_cpu *cpu, int verbose)
{
{
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  unsigned long cti_stall_cycles = PROFILE_MODEL_CTI_STALL_CYCLES (data);
  unsigned long cti_stall_cycles = PROFILE_MODEL_CTI_STALL_CYCLES (data);
  unsigned long load_stall_cycles = PROFILE_MODEL_LOAD_STALL_CYCLES (data);
  unsigned long load_stall_cycles = PROFILE_MODEL_LOAD_STALL_CYCLES (data);
  unsigned long total_cycles = PROFILE_MODEL_TOTAL_CYCLES (data);
  unsigned long total_cycles = PROFILE_MODEL_TOTAL_CYCLES (data);
  char comma_buf[20];
  char comma_buf[20];
 
 
  sim_io_printf (sd, "Model %s Timing Information",
  sim_io_printf (sd, "Model %s Timing Information",
                 MODEL_NAME (CPU_MODEL (cpu)));
                 MODEL_NAME (CPU_MODEL (cpu)));
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
  if (PROFILE_RANGE (data)->ranges)
  if (PROFILE_RANGE (data)->ranges)
    sim_io_printf (sd, " (for selected address range(s))");
    sim_io_printf (sd, " (for selected address range(s))");
#endif
#endif
  sim_io_printf (sd, "\n\n");
  sim_io_printf (sd, "\n\n");
  sim_io_printf (sd, "  %-*s %s\n",
  sim_io_printf (sd, "  %-*s %s\n",
                 PROFILE_LABEL_WIDTH, "Taken branches:",
                 PROFILE_LABEL_WIDTH, "Taken branches:",
                 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data)));
                 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data)));
  sim_io_printf (sd, "  %-*s %s\n",
  sim_io_printf (sd, "  %-*s %s\n",
                 PROFILE_LABEL_WIDTH, "Untaken branches:",
                 PROFILE_LABEL_WIDTH, "Untaken branches:",
                 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data)));
                 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data)));
  sim_io_printf (sd, "  %-*s %s\n",
  sim_io_printf (sd, "  %-*s %s\n",
                 PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:",
                 PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:",
                 COMMAS (cti_stall_cycles));
                 COMMAS (cti_stall_cycles));
  sim_io_printf (sd, "  %-*s %s\n",
  sim_io_printf (sd, "  %-*s %s\n",
                 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
                 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
                 COMMAS (load_stall_cycles));
                 COMMAS (load_stall_cycles));
  sim_io_printf (sd, "  %-*s %s\n",
  sim_io_printf (sd, "  %-*s %s\n",
                 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
                 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
                 COMMAS (total_cycles));
                 COMMAS (total_cycles));
  sim_io_printf (sd, "\n");
  sim_io_printf (sd, "\n");
}
}
 
 
#endif
#endif
 
 
void
void
sim_profile_print_bar (SIM_DESC sd, unsigned int width,
sim_profile_print_bar (SIM_DESC sd, unsigned int width,
                       unsigned int val, unsigned int max_val)
                       unsigned int val, unsigned int max_val)
{
{
  unsigned int i, count;
  unsigned int i, count;
 
 
  count = ((double) val / (double) max_val) * (double) width;
  count = ((double) val / (double) max_val) * (double) width;
 
 
  for (i = 0; i < count; ++i)
  for (i = 0; i < count; ++i)
    sim_io_printf (sd, "*");
    sim_io_printf (sd, "*");
}
}
 
 
/* Print the simulator's execution speed for CPU.  */
/* Print the simulator's execution speed for CPU.  */
 
 
static void
static void
profile_print_speed (sim_cpu *cpu)
profile_print_speed (sim_cpu *cpu)
{
{
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
  unsigned long milliseconds = sim_events_elapsed_time (sd);
  unsigned long milliseconds = sim_events_elapsed_time (sd);
  unsigned long total = PROFILE_TOTAL_INSN_COUNT (data);
  unsigned long total = PROFILE_TOTAL_INSN_COUNT (data);
  double clock;
  double clock;
  double secs;
  double secs;
  char comma_buf[20];
  char comma_buf[20];
 
 
  sim_io_printf (sd, "Simulator Execution Speed\n\n");
  sim_io_printf (sd, "Simulator Execution Speed\n\n");
 
 
  if (total != 0)
  if (total != 0)
    sim_io_printf (sd, "  Total instructions:      %s\n", COMMAS (total));
    sim_io_printf (sd, "  Total instructions:      %s\n", COMMAS (total));
 
 
  if (milliseconds < 1000)
  if (milliseconds < 1000)
    sim_io_printf (sd, "  Total execution time:    < 1 second\n\n");
    sim_io_printf (sd, "  Total execution time:    < 1 second\n\n");
  else
  else
    {
    {
      /* The printing of the time rounded to 2 decimal places makes the speed
      /* The printing of the time rounded to 2 decimal places makes the speed
         calculation seem incorrect [even though it is correct].  So round
         calculation seem incorrect [even though it is correct].  So round
         MILLISECONDS first. This can marginally affect the result, but it's
         MILLISECONDS first. This can marginally affect the result, but it's
         better that the user not perceive there's a math error.  */
         better that the user not perceive there's a math error.  */
      secs = (double) milliseconds / 1000;
      secs = (double) milliseconds / 1000;
      secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
      secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
      sim_io_printf (sd, "  Total execution time   : %.2f seconds\n", secs);
      sim_io_printf (sd, "  Total execution time   : %.2f seconds\n", secs);
      /* Don't confuse things with data that isn't useful.
      /* Don't confuse things with data that isn't useful.
         If we ran for less than 2 seconds, only use the data if we
         If we ran for less than 2 seconds, only use the data if we
         executed more than 100,000 insns.  */
         executed more than 100,000 insns.  */
      if (secs >= 2 || total >= 100000)
      if (secs >= 2 || total >= 100000)
        sim_io_printf (sd, "  Simulator speed:         %s insns/second\n",
        sim_io_printf (sd, "  Simulator speed:         %s insns/second\n",
                       COMMAS ((unsigned long) ((double) total / secs)));
                       COMMAS ((unsigned long) ((double) total / secs)));
    }
    }
 
 
  /* Print simulated execution time if the cpu frequency has been specified.  */
  /* Print simulated execution time if the cpu frequency has been specified.  */
  clock = PROFILE_CPU_FREQ (data);
  clock = PROFILE_CPU_FREQ (data);
  if (clock != 0)
  if (clock != 0)
    {
    {
      if (clock >= 1000000)
      if (clock >= 1000000)
        sim_io_printf (sd, "  Simulated cpu frequency: %.2f MHz\n",
        sim_io_printf (sd, "  Simulated cpu frequency: %.2f MHz\n",
                       clock / 1000000);
                       clock / 1000000);
      else
      else
        sim_io_printf (sd, "  Simulated cpu frequency: %.2f Hz\n", clock);
        sim_io_printf (sd, "  Simulated cpu frequency: %.2f Hz\n", clock);
 
 
#if WITH_PROFILE_MODEL_P
#if WITH_PROFILE_MODEL_P
      if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
        {
        {
          /* The printing of the time rounded to 2 decimal places makes the
          /* The printing of the time rounded to 2 decimal places makes the
             speed calculation seem incorrect [even though it is correct].
             speed calculation seem incorrect [even though it is correct].
             So round    SECS first. This can marginally affect the result,
             So round    SECS first. This can marginally affect the result,
             but it's    better that the user not perceive there's a math
             but it's    better that the user not perceive there's a math
             error.  */
             error.  */
          secs = PROFILE_MODEL_TOTAL_CYCLES (data) / clock;
          secs = PROFILE_MODEL_TOTAL_CYCLES (data) / clock;
          secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
          secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
          sim_io_printf (sd, "  Simulated execution time: %.2f seconds\n",
          sim_io_printf (sd, "  Simulated execution time: %.2f seconds\n",
                         secs);
                         secs);
        }
        }
#endif /* WITH_PROFILE_MODEL_P */
#endif /* WITH_PROFILE_MODEL_P */
    }
    }
}
}
 
 
/* Print selected address ranges.  */
/* Print selected address ranges.  */
 
 
static void
static void
profile_print_addr_ranges (sim_cpu *cpu)
profile_print_addr_ranges (sim_cpu *cpu)
{
{
  ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges;
  ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges;
  SIM_DESC sd = CPU_STATE (cpu);
  SIM_DESC sd = CPU_STATE (cpu);
 
 
  if (asr)
  if (asr)
    {
    {
      sim_io_printf (sd, "Selected address ranges\n\n");
      sim_io_printf (sd, "Selected address ranges\n\n");
      while (asr != NULL)
      while (asr != NULL)
        {
        {
          sim_io_printf (sd, "  0x%lx - 0x%lx\n",
          sim_io_printf (sd, "  0x%lx - 0x%lx\n",
                         (long) asr->start, (long) asr->end);
                         (long) asr->start, (long) asr->end);
          asr = asr->next;
          asr = asr->next;
        }
        }
      sim_io_printf (sd, "\n");
      sim_io_printf (sd, "\n");
    }
    }
}
}
 
 
/* Top level function to print all summary profile information.
/* Top level function to print all summary profile information.
   It is [currently] intended that all such data is printed by this function.
   It is [currently] intended that all such data is printed by this function.
   I'd rather keep it all in one place for now.  To that end, MISC_CPU and
   I'd rather keep it all in one place for now.  To that end, MISC_CPU and
   MISC are callbacks used to print any miscellaneous data.
   MISC are callbacks used to print any miscellaneous data.
 
 
   One might want to add a user option that allows printing by type or by cpu
   One might want to add a user option that allows printing by type or by cpu
   (i.e. print all insn data for each cpu first, or print data cpu by cpu).
   (i.e. print all insn data for each cpu first, or print data cpu by cpu).
   This may be a case of featuritis so it's currently left out.
   This may be a case of featuritis so it's currently left out.
 
 
   Note that results are indented two spaces to distinguish them from
   Note that results are indented two spaces to distinguish them from
   section titles.  */
   section titles.  */
 
 
static void
static void
profile_info (SIM_DESC sd, int verbose)
profile_info (SIM_DESC sd, int verbose)
{
{
  int i,c;
  int i,c;
  int print_title_p = 0;
  int print_title_p = 0;
 
 
  /* Only print the title if some data has been collected.  */
  /* Only print the title if some data has been collected.  */
  /* ??? Why don't we just exit if no data collected?  */
  /* ??? Why don't we just exit if no data collected?  */
  /* FIXME: If the number of processors can be selected on the command line,
  /* FIXME: If the number of processors can be selected on the command line,
     then MAX_NR_PROCESSORS will need to take an argument of `sd'.  */
     then MAX_NR_PROCESSORS will need to take an argument of `sd'.  */
 
 
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, c);
      sim_cpu *cpu = STATE_CPU (sd, c);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
 
 
      for (i = 0; i < MAX_PROFILE_VALUES; ++i)
      for (i = 0; i < MAX_PROFILE_VALUES; ++i)
        if (PROFILE_FLAGS (data) [i])
        if (PROFILE_FLAGS (data) [i])
          print_title_p = 1;
          print_title_p = 1;
      /* One could break out early if print_title_p is set.  */
      /* One could break out early if print_title_p is set.  */
    }
    }
  if (print_title_p)
  if (print_title_p)
    sim_io_printf (sd, "Summary profiling results:\n\n");
    sim_io_printf (sd, "Summary profiling results:\n\n");
 
 
  /* Loop, cpu by cpu, printing results.  */
  /* Loop, cpu by cpu, printing results.  */
 
 
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
  for (c = 0; c < MAX_NR_PROCESSORS; ++c)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, c);
      sim_cpu *cpu = STATE_CPU (sd, c);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
 
 
      if (MAX_NR_PROCESSORS > 1
      if (MAX_NR_PROCESSORS > 1
          && (0
          && (0
#if WITH_PROFILE_INSN_P
#if WITH_PROFILE_INSN_P
              || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]
              || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]
#endif
#endif
#if WITH_PROFILE_MEMORY_P
#if WITH_PROFILE_MEMORY_P
              || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]
              || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]
#endif
#endif
#if WITH_PROFILE_CORE_P
#if WITH_PROFILE_CORE_P
              || PROFILE_FLAGS (data) [PROFILE_CORE_IDX]
              || PROFILE_FLAGS (data) [PROFILE_CORE_IDX]
#endif
#endif
#if WITH_PROFILE_MODEL_P
#if WITH_PROFILE_MODEL_P
              || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]
              || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]
#endif
#endif
#if WITH_PROFILE_SCACHE_P && WITH_SCACHE
#if WITH_PROFILE_SCACHE_P && WITH_SCACHE
              || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
              || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
#endif
#endif
#if WITH_PROFILE_PC_P
#if WITH_PROFILE_PC_P
              || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
              || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
#endif
#endif
              ))
              ))
        {
        {
          sim_io_printf (sd, "CPU %d\n\n", c);
          sim_io_printf (sd, "CPU %d\n\n", c);
        }
        }
 
 
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
      if (print_title_p
      if (print_title_p
          && (PROFILE_INSN_P (cpu)
          && (PROFILE_INSN_P (cpu)
              || PROFILE_MODEL_P (cpu)))
              || PROFILE_MODEL_P (cpu)))
        profile_print_addr_ranges (cpu);
        profile_print_addr_ranges (cpu);
#endif
#endif
 
 
#if WITH_PROFILE_INSN_P
#if WITH_PROFILE_INSN_P
      if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
        profile_print_insn (cpu, verbose);
        profile_print_insn (cpu, verbose);
#endif
#endif
 
 
#if WITH_PROFILE_MEMORY_P
#if WITH_PROFILE_MEMORY_P
      if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
        profile_print_memory (cpu, verbose);
        profile_print_memory (cpu, verbose);
#endif
#endif
 
 
#if WITH_PROFILE_CORE_P
#if WITH_PROFILE_CORE_P
      if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
        profile_print_core (cpu, verbose);
        profile_print_core (cpu, verbose);
#endif
#endif
 
 
#if WITH_PROFILE_MODEL_P
#if WITH_PROFILE_MODEL_P
      if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
        profile_print_model (cpu, verbose);
        profile_print_model (cpu, verbose);
#endif
#endif
 
 
#if WITH_PROFILE_SCACHE_P && WITH_SCACHE
#if WITH_PROFILE_SCACHE_P && WITH_SCACHE
      if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
        scache_print_profile (cpu, verbose);
        scache_print_profile (cpu, verbose);
#endif
#endif
 
 
#if WITH_PROFILE_PC_P
#if WITH_PROFILE_PC_P
      if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
      if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
        profile_print_pc (cpu, verbose);
        profile_print_pc (cpu, verbose);
#endif
#endif
 
 
      /* Print cpu-specific data before the execution speed.  */
      /* Print cpu-specific data before the execution speed.  */
      if (PROFILE_INFO_CPU_CALLBACK (data) != NULL)
      if (PROFILE_INFO_CPU_CALLBACK (data) != NULL)
        PROFILE_INFO_CPU_CALLBACK (data) (cpu, verbose);
        PROFILE_INFO_CPU_CALLBACK (data) (cpu, verbose);
 
 
      /* Always try to print execution time and speed.  */
      /* Always try to print execution time and speed.  */
      if (verbose
      if (verbose
          || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
          || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
        profile_print_speed (cpu);
        profile_print_speed (cpu);
    }
    }
 
 
  /* Finally print non-cpu specific miscellaneous data.  */
  /* Finally print non-cpu specific miscellaneous data.  */
  if (STATE_PROFILE_INFO_CALLBACK (sd))
  if (STATE_PROFILE_INFO_CALLBACK (sd))
    STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose);
    STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose);
 
 
}
}


/* Install profiling support in the simulator.  */
/* Install profiling support in the simulator.  */
 
 
SIM_RC
SIM_RC
profile_install (SIM_DESC sd)
profile_install (SIM_DESC sd)
{
{
  int i;
  int i;
 
 
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  sim_add_option_table (sd, NULL, profile_options);
  sim_add_option_table (sd, NULL, profile_options);
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0,
    memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0,
            sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i))));
            sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i))));
#if WITH_PROFILE_INSN_P
#if WITH_PROFILE_INSN_P
  sim_module_add_init_fn (sd, profile_insn_init);
  sim_module_add_init_fn (sd, profile_insn_init);
#endif
#endif
#if WITH_PROFILE_PC_P
#if WITH_PROFILE_PC_P
  sim_module_add_uninstall_fn (sd, profile_pc_uninstall);
  sim_module_add_uninstall_fn (sd, profile_pc_uninstall);
  sim_module_add_init_fn (sd, profile_pc_init);
  sim_module_add_init_fn (sd, profile_pc_init);
#endif
#endif
  sim_module_add_init_fn (sd, profile_init);
  sim_module_add_init_fn (sd, profile_init);
  sim_module_add_uninstall_fn (sd, profile_uninstall);
  sim_module_add_uninstall_fn (sd, profile_uninstall);
  sim_module_add_info_fn (sd, profile_info);
  sim_module_add_info_fn (sd, profile_info);
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
static SIM_RC
static SIM_RC
profile_init (SIM_DESC sd)
profile_init (SIM_DESC sd)
{
{
#ifdef SIM_HAVE_ADDR_RANGE
#ifdef SIM_HAVE_ADDR_RANGE
  /* Check if a range has been specified without specifying what to
  /* Check if a range has been specified without specifying what to
     collect.  */
     collect.  */
  {
  {
    int i;
    int i;
 
 
    for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    for (i = 0; i < MAX_NR_PROCESSORS; ++i)
      {
      {
        sim_cpu *cpu = STATE_CPU (sd, i);
        sim_cpu *cpu = STATE_CPU (sd, i);
 
 
        if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)))
        if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)))
            && ! (PROFILE_INSN_P (cpu)
            && ! (PROFILE_INSN_P (cpu)
                  || PROFILE_MODEL_P (cpu)))
                  || PROFILE_MODEL_P (cpu)))
          {
          {
            sim_io_eprintf_cpu (cpu, "Profiling address range specified without --profile-insn or --profile-model.\n");
            sim_io_eprintf_cpu (cpu, "Profiling address range specified without --profile-insn or --profile-model.\n");
            sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
            sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
            sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
            sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
                                   0, ~ (address_word) 0);
                                   0, ~ (address_word) 0);
          }
          }
      }
      }
  }
  }
#endif
#endif
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
static void
static void
profile_uninstall (SIM_DESC sd)
profile_uninstall (SIM_DESC sd)
{
{
  int i,j;
  int i,j;
 
 
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    {
    {
      sim_cpu *cpu = STATE_CPU (sd, i);
      sim_cpu *cpu = STATE_CPU (sd, i);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
      PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
 
 
      if (PROFILE_FILE (data) != NULL)
      if (PROFILE_FILE (data) != NULL)
        {
        {
          /* If output from different cpus is going to the same file,
          /* If output from different cpus is going to the same file,
             avoid closing the file twice.  */
             avoid closing the file twice.  */
          for (j = 0; j < i; ++j)
          for (j = 0; j < i; ++j)
            if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j)))
            if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j)))
                == PROFILE_FILE (data))
                == PROFILE_FILE (data))
              break;
              break;
          if (i == j)
          if (i == j)
            fclose (PROFILE_FILE (data));
            fclose (PROFILE_FILE (data));
        }
        }
 
 
      if (PROFILE_INSN_COUNT (data) != NULL)
      if (PROFILE_INSN_COUNT (data) != NULL)
        zfree (PROFILE_INSN_COUNT (data));
        zfree (PROFILE_INSN_COUNT (data));
    }
    }
}
}
 
 

powered by: WebSVN 2.1.0

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