/*
 * Copyright 1999, SGI
 *
 * Written by Dimitris Michailidis (dimitris@engr.sgi.com)
 *
 * Parts of this file are covered by the copyright below.
 */

/*
 * Copyright (c) 1982, 1986, 1992, 1993
 *     The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *     This product includes software developed by the University of
 *     California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef        _PROFILE_H_
#define        _PROFILE_H_     1

#include <linux/types.h>

/*
 * histogram counters are unsigned shorts (according to the kernel).
 */
#define        HISTCOUNTER     unsigned short

/*
 * fraction of text space to allocate for histogram counters here, 1/2
 */
#define        HISTFRACTION    2

struct tostruct {
       u_long  selfpc;
       long    count;
       u_short link;
       u_short pad;
};

/*
 * a raw arc, with pointers to the calling site and
 * the called site and a count.
 */
struct rawarc {
       u_long  raw_frompc;
       u_long  raw_selfpc;
       long    raw_count;
};

/*
 * general rounding functions.
 */
#define ROUNDDOWN(x,y) (((x)/(y))*(y))
#define ROUNDUP(x,y)   ((((x)+(y)-1)/(y))*(y))

/*
 * Fraction of text space to allocate for from hash buckets.
 * The value of HASHFRACTION is based on the minimum number of bytes
 * of separation between two subroutine call points in the object code.
 * Given MIN_SUBR_SEPARATION bytes of separation the value of
 * HASHFRACTION is calculated as:
 *
 *     HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1);
 *
 * For example, on the VAX, the shortest two call sequence is:
 *
 *     calls   $0,(r0)
 *     calls   $0,(r0)
 *
 * which is separated by only three bytes, thus HASHFRACTION is
 * calculated as:
 *
 *     HASHFRACTION = 3 / (2 * 2 - 1) = 1
 *
 * Note that the division above rounds down, thus if MIN_SUBR_FRACTION
 * is less than three, this algorithm will not work!
 *
 * In practice, however, call instructions are rarely at a minimal
 * distance.  Hence, we will define HASHFRACTION to be 2 across all
 * architectures.  This saves a reasonable amount of space for
 * profiling data structures without (in practice) sacrificing
 * any granularity.
 */
#define        HASHFRACTION            2

#ifdef __KERNEL__

/* This constant is the base 2 log of (HASHFRACTION * sizeof(u_short)) */
#define LOG_HASHFRACTION       2

/*
 * Maximum number of processors we can handle.  This is important for the
 * proper operation of mcount, so if we ever need more processore we must
 * inrease this.
 */
#define MAX_PROCS      256

/*
 * percent of text space to allocate for tostructs with a minimum.
 */
#define ARCDENSITY     2
#define MINARCS                50
#define MAXARCS                ((1 << (8 * sizeof(HISTCOUNTER))) - (MAX_PROCS + 1))

/*
 * The profiling data structures are housed in this structure.
 */
struct profparam {
       int              state;           /* this is a bitfield */
       unsigned int     freq;            /* multiplier for local APIC */
       volatile u_short *froms;
       u_long           fromssize;
       struct tostruct  *tos;
       u_long           tossize;
       long             tolimit;
       u_long           lowpc;
       u_long           highpc;
       u_long           textsize;
};
extern struct profparam prof_params;

#endif

/*
 * Possible states of profiling.
 */
#define PROFILE_OFF            0
#define PROFILE_ON             1
#define PROF_MCOUNT_OVERFLOW   128
#define PROF_MCOUNT            256
#define PROF_PC                        512
#define PROF_CALL_TRACE                1024

#define PROF_TYPE_ALL  (PROF_MCOUNT | PROF_PC | PROF_CALL_TRACE)

#define PROF_SET_FREQ  0x0
#define PROF_ENABLE    0x01000000
#define PROF_DISABLE   0x02000000
#define PROF_RESET     0x03000000
#define PROF_TYPE      0x04000000
#define PROF_DOMAIN    0x05000000

#endif /* !_PROFILE_H_ */
