summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/radeon/r600_gpu_load.c
blob: 62f5d03462abac3ce31e0d586a7d5f9154db210e (plain)
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
/*
 * Copyright 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors: Marek Olšák <maraeo@gmail.com>
 *
 */

/* The GPU load is measured as follows.
 *
 * There is a thread which samples the GRBM_STATUS register at a certain
 * frequency and the "busy" or "idle" counter is incremented based on
 * whether the GUI_ACTIVE bit is set or not.
 *
 * Then, the user can sample the counters twice and calculate the average
 * GPU load between the two samples.
 */

#include "r600_pipe_common.h"
#include "r600_query.h"
#include "os/os_time.h"

/* For good accuracy at 1000 fps or lower. This will be inaccurate for higher
 * fps (there are too few samples per frame). */
#define SAMPLES_PER_SEC 10000

#define GRBM_STATUS		0x8010
#define SPI_BUSY(x)		(((x) >> 22) & 0x1)
#define GUI_ACTIVE(x)		(((x) >> 31) & 0x1)

#define UPDATE_COUNTER(field, mask)				\
	if (mask(value))					\
		p_atomic_inc(&counters->named.field.busy);	\
	else							\
		p_atomic_inc(&counters->named.field.idle);

static void r600_update_grbm_counters(struct r600_common_screen *rscreen,
				      union r600_grbm_counters *counters)
{
	uint32_t value = 0;

	rscreen->ws->read_registers(rscreen->ws, GRBM_STATUS, 1, &value);

	UPDATE_COUNTER(spi, SPI_BUSY);
	UPDATE_COUNTER(gui, GUI_ACTIVE);
}

static PIPE_THREAD_ROUTINE(r600_gpu_load_thread, param)
{
	struct r600_common_screen *rscreen = (struct r600_common_screen*)param;
	const int period_us = 1000000 / SAMPLES_PER_SEC;
	int sleep_us = period_us;
	int64_t cur_time, last_time = os_time_get();

	while (!p_atomic_read(&rscreen->gpu_load_stop_thread)) {
		if (sleep_us)
			os_time_sleep(sleep_us);

		/* Make sure we sleep the ideal amount of time to match
		 * the expected frequency. */
		cur_time = os_time_get();

		if (os_time_timeout(last_time, last_time + period_us,
				    cur_time))
			sleep_us = MAX2(sleep_us - 1, 1);
		else
			sleep_us += 1;

		/*printf("Hz: %.1f\n", 1000000.0 / (cur_time - last_time));*/
		last_time = cur_time;

		/* Update the counters. */
		r600_update_grbm_counters(rscreen, &rscreen->grbm_counters);
	}
	p_atomic_dec(&rscreen->gpu_load_stop_thread);
	return 0;
}

void r600_gpu_load_kill_thread(struct r600_common_screen *rscreen)
{
	if (!rscreen->gpu_load_thread)
		return;

	p_atomic_inc(&rscreen->gpu_load_stop_thread);
	pipe_thread_wait(rscreen->gpu_load_thread);
	rscreen->gpu_load_thread = 0;
}

static uint64_t r600_read_grbm_counter(struct r600_common_screen *rscreen,
				       unsigned busy_index)
{
	/* Start the thread if needed. */
	if (!rscreen->gpu_load_thread) {
		pipe_mutex_lock(rscreen->gpu_load_mutex);
		/* Check again inside the mutex. */
		if (!rscreen->gpu_load_thread)
			rscreen->gpu_load_thread =
				pipe_thread_create(r600_gpu_load_thread, rscreen);
		pipe_mutex_unlock(rscreen->gpu_load_mutex);
	}

	unsigned busy = p_atomic_read(&rscreen->grbm_counters.array[busy_index]);
	unsigned idle = p_atomic_read(&rscreen->grbm_counters.array[busy_index + 1]);

	return busy | ((uint64_t)idle << 32);
}

static unsigned r600_end_grbm_counter(struct r600_common_screen *rscreen,
				      uint64_t begin, unsigned busy_index)
{
	uint64_t end = r600_read_grbm_counter(rscreen, busy_index);
	unsigned busy = (end & 0xffffffff) - (begin & 0xffffffff);
	unsigned idle = (end >> 32) - (begin >> 32);

	/* Calculate the % of time the busy counter was being incremented.
	 *
	 * If no counters were incremented, return the current counter status.
	 * It's for the case when the load is queried faster than
	 * the counters are updated.
	 */
	if (idle || busy) {
		return busy*100 / (busy + idle);
	} else {
		union r600_grbm_counters counters;

		memset(&counters, 0, sizeof(counters));
		r600_update_grbm_counters(rscreen, &counters);
		return counters.array[busy_index] ? 100 : 0;
	}
}

#define BUSY_INDEX(rscreen, field) (&rscreen->grbm_counters.named.field.busy - \
				    rscreen->grbm_counters.array)

static unsigned busy_index_from_type(struct r600_common_screen *rscreen,
				     unsigned type)
{
	switch (type) {
	case R600_QUERY_GPU_LOAD:
		return BUSY_INDEX(rscreen, gui);
	case R600_QUERY_GPU_SHADERS_BUSY:
		return BUSY_INDEX(rscreen, spi);
	default:
		unreachable("query type does not correspond to grbm id");
	}
}

uint64_t r600_begin_counter(struct r600_common_screen *rscreen, unsigned type)
{
	unsigned busy_index = busy_index_from_type(rscreen, type);
	return r600_read_grbm_counter(rscreen, busy_index);
}

unsigned r600_end_counter(struct r600_common_screen *rscreen, unsigned type,
			  uint64_t begin)
{
	unsigned busy_index = busy_index_from_type(rscreen, type);
	return r600_end_grbm_counter(rscreen, begin, busy_index);
}