aboutsummaryrefslogtreecommitdiff
path: root/sys/mips/rmi/perfmon.h
blob: 555b1818cd32b2428eae3effaa4c3f2ce74fe5df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * Copyright (c) 2003-2009 RMI Corporation
 * 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. Neither the name of RMI Corporation, 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 AUTHOR 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 AUTHOR 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.
 *
 * RMI_BSD */

#ifndef PERFMON_H
#define PERFMON_H

#include <mips/rmi/perfmon_xlrconfig.h>

/*
 * category events reported by the perfmon library
 */
enum event_category_t {
	PERF_CP0_COUNTER = 1, PERF_CP2_CREDITS, PERF_L2_COUNTER,
	PERF_SBC_COUNTER, PERF_SBC_CREDITS, PERF_GMAC0_COUNTER, PERF_GMAC1_COUNTER,
	PERF_GMAC2_COUNTER, PERF_GMAC_STAT_COM, PERF_GMAC_STAT_TX,
PERF_GMAC_STAT_RX, PERF_DRAM_COUNTER, PERF_PARAMETER_CONF = 127};


enum perf_param_t {
	PERF_CPU_SAMPLING_INTERVAL, PERF_SYS_SAMPLING_INTERVAL, PERF_CC_SAMPLE_RATE, PERF_CP0_FLAGS
};

#define CPO_EVENTS_TEMPLATE  0x06	/* enable kernel and user events */

#define PERFMON_ACTIVE_MAGIC 0xc001
#define PERFMON_ENABLED_MAGIC 0xb007
#define PERFMON_INITIAL_GENERATION 0x0101

#define PERFMON_SERVER_PORT 7007

enum system_bridge_credits_t {
	PCIX_CREDITS, HT_CREDITS, GIO_CREDITS, OTHER_CREDITS
};

struct perf_config_data {
	uint16_t magic;		/* monitor start when this is initialized */
	uint16_t generation;	/* incremented when the config changes */
	uint16_t flags;
	uint16_t cc_sample_rate;/* rate at which credit counters are sampled
				 * relative to sampling_rate */
	uint32_t sampling_rate;	/* rate at which events are sampled */
	uint32_t cc_register_mask;	/* credit counters registers to be
					 * sampled */
	uint64_t events[NTHREADS];	/* events bitmap for each thread */
};

struct perf_sample {
	uint32_t counter;
	uint32_t timestamp;
	uint32_t sample_tag;
	uint32_t duration;
};

struct sample_q {
	int32_t head, tail;
	struct perf_sample samples[PERF_SAMPLE_BUFSZ];
	uint32_t overflows;
};

struct perf_area {
	struct perf_config_data perf_config;
	struct sample_q sample_fifo;
};

/*
 * We have a shared location to keep a global tick counter for all the
 * CPUS  - TODO is this optimal? effect on cache?
 */
extern uint32_t *xlr_perfmon_timer_loc;

#define PERFMON_TIMESTAMP_LOC (xlr_perfmon_timer_loc)

static __inline__ uint32_t 
perfmon_timestamp_get(void)
{
	return *PERFMON_TIMESTAMP_LOC;
}

static __inline__ void 
perfmon_timestamp_set(uint32_t val)
{
	*PERFMON_TIMESTAMP_LOC = val;
}

static __inline__ void 
perfmon_timestamp_incr(int val)
{
	(*PERFMON_TIMESTAMP_LOC) += val;
}

static __inline__ void 
send_sample_gts(uint32_t tag, uint32_t value, uint32_t td)
{
	xlr_send_sample(tag, value, perfmon_timestamp_get(), td);
}

/*
 * Simple FIFO, one producer - one consumer - circlar queue - no locking
 */

static __inline__ void 
init_fifo(struct sample_q *q)
{
	q->head = q->tail = 0;
}

static __inline__ void 
put_sample(struct sample_q *q, uint32_t sample_tag, uint32_t counter,
    uint32_t duration)
{
	uint32_t timestamp = perfmon_timestamp_get();
	int new_tail = (q->tail + 1) % PERF_SAMPLE_BUFSZ;

	if (q->head == new_tail) {
		q->overflows++;
		return;
	}
	q->samples[new_tail].sample_tag = sample_tag;
	q->samples[new_tail].counter = counter;
	q->samples[new_tail].timestamp = timestamp;
	q->samples[new_tail].duration = duration;

	q->tail = new_tail;
}

static __inline__ int 
get_sample(struct sample_q *q, uint32_t * sample_tag, uint32_t * counter,
    uint32_t * timestamp, uint32_t * duration)
{
	int head = q->head;

	if (head == q->tail)
		return 0;
	*sample_tag = q->samples[head].sample_tag;
	*counter = q->samples[head].counter;
	*timestamp = q->samples[head].timestamp;
	*duration = q->samples[head].duration;

	q->head = (head + 1) % PERF_SAMPLE_BUFSZ;
	return 1;
}

static __inline__ void 
clear_queue(struct sample_q *q)
{
	q->head = q->tail;
}
void xlr_perfmon_init_cpu(void *);
void xlr_perfmon_sampler(void *);
void log_active_core(int core);
int get_start_generation(void);

void xlr_perfmon_clockhandler(void);
extern int xlr_perfmon_started;

#endif				/* PERFMON_H */