Skip to content

Instantly share code, notes, and snippets.

@croepha
Created August 28, 2018 02:21
Show Gist options
  • Save croepha/62a7220c610b7933b86a0b0e3e91ebb4 to your computer and use it in GitHub Desktop.
Save croepha/62a7220c610b7933b86a0b0e3e91ebb4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#ifdef USE_TEST_FILE
static const char* reg_file = "junk/v3d_test_file";
#else
static const char* reg_file = "/debug/dri/0/v3d_regs";
#endif
static char file_buf[4096];
static const char* set0name0 = "CT0CA";
static const char* set0name1 = "CT0EA";
static const char* set1name0 = "CT1CA";
static const char* set1name1 = "CT1EA";
char* set0name0_location = 0;
char* set0name1_location = 0;
char* set1name0_location = 0;
char* set1name1_location = 0;
#define _scan(name) __scan(name, name ## _location, c)
static void __scan(const char*name, char*&location, char*c) {
if (strncmp(c, name, 5)==0) {
assert(!location);
location = c + 18;
assert(*(location - 4)== ':');
assert(*(location - 1)== 'x');
assert(*(location + 8)=='\n');
}
}
int main () {
auto reg_f = open(reg_file, O_RDONLY);
if (reg_f == -1) {
fprintf(stderr, "Could not open %s (%s)\n", reg_file, strerror(errno));
fprintf(stderr, "Maybe you need to run as root or run:\n");
fprintf(stderr, " sudo mount -t debugfs none /debug\n");
exit(-1);
}
int columns;
{
struct winsize ws;
auto r1 = ioctl(1, TIOCGWINSZ, &ws);
if (isatty(1) && !r1) {
columns = ws.ws_col;
} else {
fprintf(stderr, "Couldn't determine terminal width\n");
columns = 20;
}
}
auto reg_f_size = read(reg_f, file_buf, sizeof file_buf);
assert(reg_f_size >=0);
// V3D_BPCS (0x0304): 0xdeadbeef$
for(char*c = file_buf; c<file_buf+reg_f_size; c++) {
_scan(set0name0);
_scan(set0name1);
_scan(set1name0);
_scan(set1name1);
}
assert(set0name0_location);
assert(set0name1_location);
assert(set1name0_location);
assert(set1name1_location);
for (;;) {
static const int counts_total = 100;
int counts_idle = 0;
for(int count_i=0; count_i < counts_total; count_i++) {
if ((memcmp(set0name0_location, set0name1_location, 8) == 0) &&
(memcmp(set1name0_location, set1name1_location, 8) == 0)) {
counts_idle++;
}
auto r1 = lseek(reg_f, 0, SEEK_SET);
assert(!r1);
auto reg_f_size2 = read(reg_f, file_buf, sizeof file_buf);
assert(reg_f_size2==reg_f_size);
usleep(1000*1);
}
float ratio_active = (float)(counts_total-counts_idle)/(float)counts_total;
int columns_active = (float)(columns - 8)*ratio_active;
int percent_active = 100.0 *ratio_active;
printf("%3d%%: ", percent_active);
for (int i=0;i<columns_active;i++) {
putchar('#');
}
putchar('\n');
fflush(stdout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment