summaryrefslogtreecommitdiff
path: root/native/utils/viv_gpu_top.c
blob: 97ee3a623c331512b346ae111a69e0f5abe081f9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Watch usage of Vivante GPU live.
 * Needs profiling support built-in (build kernel and etnaviv with VIVANTE_PROFILER=1).
 */

/* Uncomment if the platform has the clock_gettime call, to use a monotonic
 * clock */
/* #define HAVE_CLOCK */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef HAVE_CLOCK
#include <time.h>
#else
#include <sys/time.h>
#endif

#include <etnaviv/viv.h>
#include <etnaviv/viv_profile.h>

static const char *bars[] = {
	" ",
	"▏",
	"▎",
	"▍",
	"▌",
	"▋",
	"▊",
	"▉",
	"█"
};

static const char clear_screen[] = {0x1b, '[', 'H',
                                    0x1b, '[', 'J',
                                    0x0};

static const char color_num_zero[] = "\x1b[1;30m";
static const char color_num[] = "\x1b[1;33m";
static const char color_reset[] = "\x1b[0m";

#define STATS_LEN (20)
#define PERCENTAGE_BAR_END      (79 - STATS_LEN)

static void print_percentage_bar(float percent, int cur_line_len)
{
    int bar_avail_len = (PERCENTAGE_BAR_END - cur_line_len - 1) * 8;
    int bar_len = bar_avail_len * (percent + .5) / 100.0;
    int i;

    for (i = bar_len; i >= 8; i -= 8) {
        printf("%s", bars[8]);
        cur_line_len++;
    }
    if (i) {
        printf("%s", bars[i]);
        cur_line_len++;
    }

    /* NB: We can't use a field width with utf8 so we manually
    * guarantee a field with of 45 chars for any bar. */
    printf("%*s", PERCENTAGE_BAR_END - cur_line_len, "");
}

/* Get time in microseconds */
static unsigned long gettime(void)
{
#ifdef HAVE_CLOCK
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return (t.tv_nsec/1000 + (t.tv_sec * 1000000));
#else
    struct timeval t;
    gettimeofday(&t, NULL);
    return (t.tv_usec + (t.tv_sec * 1000000));
#endif
}

/* Return number of lines in the terminal */
static int get_screen_lines(void)
{
    struct winsize ws;
    if (ioctl(0, TIOCGWINSZ, &ws) != -1)
        return ws.ws_row;
    else
        return 25; /* default */
}

/* Format unsigned 64 bit number with thousands separators.
 * Result is always nul-terminated within outsz characters. */
static void format_number(char *out, int outsz, uint64_t num)
{
    char temp[100];
    int len;
    int groups, group_size;
    int in_ptr, out_ptr;
    if(outsz == 0)
        return;
    snprintf(temp, sizeof(temp), "%llu", num);
    len = strlen(temp);
    /* group digits per three */
    groups = (len+2) / 3;
    group_size = len - (groups - 1) * 3; /* First group */
    in_ptr = out_ptr = 0;
    outsz -= 1;
    for(int i=0; i<groups && out_ptr < outsz; ++i)
    {
        for(int j=0; j<group_size && out_ptr < outsz; ++j)
            out[out_ptr++] = temp[in_ptr++];
        if(i != (groups-1) && out_ptr < outsz)
            out[out_ptr++] = ',';
        group_size = 3;
    }
    out[out_ptr] = 0;
}

/****************************************************************************/

/* Counter sorting record */
struct counter_rec
{
    uint32_t id;
    uint64_t events_per_s;
};

/* Sort counters descending */
static int counter_rec_compar(const void *a, const void *b)
{
    uint64_t ca = ((const struct counter_rec*)a)->events_per_s;
    uint64_t cb = ((const struct counter_rec*)b)->events_per_s;
    if(ca < cb)
        return 1;
    else if(cb > ca)
        return -1;
    else return 0;
}

enum display_mode
{
    MODE_ALL = 0,
    MODE_SORTED = 1
};

int main()
{
    struct viv_conn *conn = 0;
    int rv;
    rv = viv_open(VIV_HW_3D, &conn);
    if(rv!=0)
    {
        fprintf(stderr, "Error opening device\n");
        exit(1);
    }
    uint32_t num_profile_counters = viv_get_num_profile_counters();

    /* XXX parameter parsing */
    int samples_per_second = 100;
    bool interactive = true;
    int mode = MODE_ALL;
    //int mode = MODE_SORTED;
    bool color = true;

    uint32_t *counter_data = calloc(num_profile_counters, 4);
    uint32_t *counter_data_last = calloc(num_profile_counters, 4);
    uint64_t *events_per_s = calloc(num_profile_counters, 8);
    struct counter_rec *sorted = calloc(num_profile_counters, sizeof(struct counter_rec));
    /* reset counters and initial values */
    if(viv_read_profile_counters_3d(conn, counter_data_last) != 0)
    {
        fprintf(stderr, "Error querying counters (probably unsupported with this kernel, or not built into libetnaviv)\n");
        exit(1);
    }
    uint32_t begin_time = gettime();
    useconds_t interval = 1000000 / samples_per_second;
    while(true)
    {
        /* Scale counters by real elapsed time */
        for(int c=0; c<num_profile_counters; ++c)
        {
            events_per_s[c] = 0;
        }

        for(int sample=0; sample<samples_per_second; ++sample)
        {
            if(viv_read_profile_counters_3d(conn, counter_data) != 0)
            {
                fprintf(stderr, "Error querying counters (probably unsupported with this kernel, or not built into libetnaviv)\n");
                exit(1);
            }
            for(int c=0; c<num_profile_counters; ++c)
            {
                /* some counters don't reset when read */
                if(c == VIV_PROF_PS_INST_COUNTER ||
                   c == VIV_PROF_VS_INST_COUNTER ||
                   c == VIV_PROF_RENDERED_PIXEL_COUNTER ||
                   c == VIV_PROF_RENDERED_VERTICE_COUNTER ||
                   c == VIV_PROF_PXL_TEXLD_INST_COUNTER ||
                   c == VIV_PROF_PXL_BRANCH_INST_COUNTER ||
                   c == VIV_PROF_VTX_TEXLD_INST_COUNTER ||
                   c == VIV_PROF_VTX_BRANCH_INST_COUNTER ||
                   c == VIV_PROF_SE_CULLED_TRIANGLE_COUNT ||
                   c == VIV_PROF_SE_CULLED_LINES_COUNT)
                    events_per_s[c] += counter_data[c] - counter_data_last[c];
                else
                    events_per_s[c] += counter_data[c];
            }
            for(int c=0; c<num_profile_counters; ++c)
                counter_data_last[c] = counter_data[c];

            usleep(interval);
        }
        uint32_t end_time = gettime();
        uint32_t diff_time = end_time - begin_time;

        /* Scale counters by real elapsed time */
        for(int c=0; c<num_profile_counters; ++c)
        {
            events_per_s[c] = events_per_s[c] * 1000000LL / (uint64_t)diff_time;
        }

        /* Sort counters descending */
        for(int c=0; c<num_profile_counters; ++c)
        {
            sorted[c].id = c;
            sorted[c].events_per_s = events_per_s[c];
        }
        qsort(sorted, num_profile_counters, sizeof(struct counter_rec), &counter_rec_compar);

        if(interactive)
        {
            int line = 0; /* current screen line */
            printf("%s", clear_screen);
            int max_lines = get_screen_lines() - line - 1;
            if(mode == MODE_SORTED)
            {
                int count = (num_profile_counters > max_lines) ? max_lines : num_profile_counters;
                for(int c=0; c<count; ++c)
                {
                    char num[100];
                    struct viv_profile_counter_info *info = viv_get_profile_counter_info(sorted[c].id);
                    format_number(num, sizeof(num), sorted[c].events_per_s);
                    if(color)
                        printf("%s", sorted[c].events_per_s == 0 ? color_num_zero : color_num);
                    printf("%15.15s", num);
                    if(color)
                        printf("%s", color_reset);
                    printf(" ");
                    printf("%-30.30s", info->name);
                    printf("\n");
                }
            } else if(mode == MODE_ALL)
            {
                /* XXX check that width doesn't exceed screen width */
                for(int l=0; l<max_lines; ++l)
                {
                    int c = VIV_PROF_GPU_CYCLES_COUNTER + l;
                    while(c < num_profile_counters)
                    {
                        char num[100];
                        struct viv_profile_counter_info *info = viv_get_profile_counter_info(c);
                        format_number(num, sizeof(num), events_per_s[c]);
                        if(color)
                            printf("%s", events_per_s[c] == 0 ? color_num_zero : color_num);
                        printf("%15.15s", num);
                        if(color)
                            printf("%s", color_reset);
                        printf(" ");
                        printf("%-30.30s", info->name);
                        printf("  ");
                        c += max_lines;
                    }
                    printf("\n");
                }
            }
        }
        begin_time = end_time;
    }
    /*
     * XXX define new mode MODE_OCCUPANCY and some derived percentage bars:
     * - [PA] Number of primitives per vertex (max 1)
     * - [PA] % of primitives culled
     * - VS -> PA -> SE -> RA primitives/vertices in each stage
     * - RA -> PS -> PE pixels/quads in each stage
     * - Pixels per PS inst
     * - Vertices per VS inst
     * - % of texture requests trilinear/bilinear
     * - overdraw (killed by depth)
     */

    return 0;
}