aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / ck_gpu_bench.c
| 1 | /* ck_gpu_bench.c -- Spark-side GPU bench console tool (LAB BUILD: CK_X86_REPORT=1 kernel). |
| 2 | * |
| 3 | * Usage: ck_gpu_bench [--key FILE] [--target IP] [--serial PATH] [--transcript FILE] <script.bench> |
| 4 | * ck_gpu_bench ping [same options] |
| 5 | * |
| 6 | * Transports: |
| 7 | * default (UDP): datagrams to the iMac, port 47035. This is the hardware path. The kernel side |
| 8 | * uses tg3 RX/TX, which is compiled in the lab build but is NOT tested in this repo. |
| 9 | * --serial PATH: a unix socket to the kernel's COM1 (QEMU: -serial unix:PATH,server=on,wait=on). |
| 10 | * The test build (CK_X86_BENCH_TEST=1) answers the same packets as text lines: |
| 11 | * request "ABCHREQ " + 128 hex digits |
| 12 | * reply "ABCHRESP " + 128 hex digits |
| 13 | * The tool waits for "bench: GPU bench console ready" before it sends. |
| 14 | * |
| 15 | * Script lines (one command per line, # comments, numbers decimal or 0x hex): |
| 16 | * ping |
| 17 | * pci_cfg <offset> bar5_read <reg> bar5_write <reg> <value> |
| 18 | * bar0_read <word> bar0_write <word> <value> |
| 19 | * bar5_poll <reg> <mask> <expected> <timeout_ms> |
| 20 | * bar5_rmw <reg> <clear_mask> <set_bits> |
| 21 | * badauth <command ...> the same command with one HMAC byte flipped (kernel must answer ERR_AUTH) |
| 22 | * stale <command ...> the same command with counter 1 (not above the kernel's last: ERR_AUTH) |
| 23 | * replay the previous request again, byte for byte (same counter: ERR_AUTH) |
| 24 | * |
| 25 | * Counter: starts from the clock (seconds * 1000) so a second run is still above the kernel's |
| 26 | * last counter. A status of ERR_ALLONES stops the script (device gone); the tool sends nothing after it. |
| 27 | * |
| 28 | * Output: one line per transaction, "<label> <STATUS>: result=0x.. before=0x..". Every byte the |
| 29 | * serial link carries is copied to --transcript FILE if given. |
| 30 | * Exit: 0 all OK; 1 an error status or an ALLONES stop; 2 transport failure. |
| 31 | * |
| 32 | * Build: cc -O2 -std=c11 -o ck_gpu_bench ck_gpu_bench.c (make -C native/kernel ck-gpu-bench) */ |
| 33 | #define _POSIX_C_SOURCE 200809L /* clock_gettime */ |
| 34 | #include <fcntl.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <stdint.h> |
| 39 | #include <errno.h> |
| 40 | #include <time.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/un.h> |
| 43 | #include <netinet/in.h> |
| 44 | #include <arpa/inet.h> |
| 45 | #include <unistd.h> |
| 46 | #include <sys/select.h> |
| 47 | |
| 48 | #include "../x86/bench_proto.h" |
| 49 | |
| 50 | /* Minimal SHA-256 for HMAC (sufficient for test; production uses OpenSSL or libsodium). */ |
| 51 | static uint32_t sha256_k[64] = { |
| 52 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
| 53 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
| 54 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
| 55 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, |
| 56 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, |
| 57 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, |
| 58 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, |
| 59 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
| 60 | }; |
| 61 | #define ROR32(v,n) (((v)>>(n))|((v)<<(32-(n)))) |
| 62 | static void sha256_block(uint32_t h[8], const uint8_t *b) |
| 63 | { |
| 64 | uint32_t w[64]; unsigned i; |
| 65 | for (i=0;i<16;i++) w[i]=((uint32_t)b[4*i]<<24)|((uint32_t)b[4*i+1]<<16)|((uint32_t)b[4*i+2]<<8)|b[4*i+3]; |
| 66 | for (i=16;i<64;i++){uint32_t s0=ROR32(w[i-15],7)^ROR32(w[i-15],18)^(w[i-15]>>3);uint32_t s1=ROR32(w[i-2],17)^ROR32(w[i-2],19)^(w[i-2]>>10);w[i]=w[i-16]+s0+w[i-7]+s1;} |
| 67 | uint32_t a=h[0],b2=h[1],c=h[2],d=h[3],e=h[4],f=h[5],g=h[6],hh=h[7]; |
| 68 | for(i=0;i<64;i++){uint32_t S1=ROR32(e,6)^ROR32(e,11)^ROR32(e,25);uint32_t ch=(e&f)^(~e&g);uint32_t t1=hh+S1+ch+sha256_k[i]+w[i];uint32_t S0=ROR32(a,2)^ROR32(a,13)^ROR32(a,22);uint32_t maj=(a&b2)^(a&c)^(b2&c);uint32_t t2=S0+maj;hh=g;g=f;f=e;e=d+t1;d=c;c=b2;b2=a;a=t1+t2;} |
| 69 | h[0]+=a;h[1]+=b2;h[2]+=c;h[3]+=d;h[4]+=e;h[5]+=f;h[6]+=g;h[7]+=hh; |
| 70 | } |
| 71 | static void sha256(const uint8_t *data, size_t len, uint8_t out[32]) |
| 72 | { |
| 73 | uint32_t h[8]={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}; |
| 74 | uint8_t buf[64]; size_t off=0; uint64_t bit_len=(uint64_t)len*8; |
| 75 | while(off+64<=len){sha256_block(h,data+off);off+=64;} |
| 76 | size_t rem=len-off; memcpy(buf,data+off,rem); buf[rem++]=0x80; |
| 77 | if(rem>56){while(rem<64)buf[rem++]=0;sha256_block(h,buf);rem=0;} |
| 78 | while(rem<56)buf[rem++]=0; |
| 79 | for(int i=7;i>=0;i--){buf[56+(7-i)]=(uint8_t)(bit_len>>(8*i));} |
| 80 | sha256_block(h,buf); |
| 81 | for(int i=0;i<8;i++){out[4*i]=(uint8_t)(h[i]>>24);out[4*i+1]=(uint8_t)(h[i]>>16);out[4*i+2]=(uint8_t)(h[i]>>8);out[4*i+3]=(uint8_t)h[i];} |
| 82 | } |
| 83 | static void hmac_sha256(const uint8_t key[32], const uint8_t *msg, size_t mlen, uint8_t out[32]) |
| 84 | { |
| 85 | uint8_t ipad[64],opad[64],inner[32]; |
| 86 | for(int i=0;i<32;i++){ipad[i]=key[i]^0x36;opad[i]=key[i]^0x5c;} |
| 87 | for(int i=32;i<64;i++){ipad[i]=0x36;opad[i]=0x5c;} |
| 88 | uint8_t *tmp=malloc(64+mlen); if(!tmp)exit(1); |
| 89 | memcpy(tmp,ipad,64); memcpy(tmp+64,msg,mlen); |
| 90 | sha256(tmp,64+mlen,inner); free(tmp); |
| 91 | uint8_t tmp2[96]; memcpy(tmp2,opad,64); memcpy(tmp2+64,inner,32); |
| 92 | sha256(tmp2,96,out); |
| 93 | } |
| 94 | |
| 95 | static const uint8_t test_key[32] = { BENCH_TEST_KEY_INIT }; |
| 96 | static uint8_t bench_key[32]; |
| 97 | static uint64_t counter; |
| 98 | static int sock = -1; /* UDP socket */ |
| 99 | static struct sockaddr_in target; |
| 100 | static int serial_fd = -1; /* COM1 socket (QEMU test) */ |
| 101 | static int use_serial; |
| 102 | static FILE *transcript; |
| 103 | static uint8_t last_req[BENCH_REQ_BYTES]; |
| 104 | static int have_last; |
| 105 | |
| 106 | enum mode { M_NORMAL, M_BADAUTH, M_STALE, M_REPLAY }; |
| 107 | |
| 108 | /* ---- transports ---- */ |
| 109 | static void init_net(const char *ip) |
| 110 | { |
| 111 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 112 | if (sock < 0) { perror("socket"); exit(2); } |
| 113 | int bcast = 1; |
| 114 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof bcast); |
| 115 | memset(&target, 0, sizeof target); |
| 116 | target.sin_family = AF_INET; |
| 117 | target.sin_port = htons(CK_X86_BENCH_PORT); |
| 118 | target.sin_addr.s_addr = inet_addr(ip); |
| 119 | struct sockaddr_in local; memset(&local, 0, sizeof local); |
| 120 | local.sin_family = AF_INET; |
| 121 | local.sin_port = htons(CK_X86_BENCH_PORT); |
| 122 | local.sin_addr.s_addr = INADDR_ANY; |
| 123 | bind(sock, (struct sockaddr *)&local, sizeof local); |
| 124 | } |
| 125 | |
| 126 | static void nap_ms(int ms) |
| 127 | { |
| 128 | struct timeval tv = { ms / 1000, (ms % 1000) * 1000 }; |
| 129 | select(0, NULL, NULL, NULL, &tv); |
| 130 | } |
| 131 | |
| 132 | static int serial_connect(const char *path) |
| 133 | { |
| 134 | for (int i = 0; i < 300; i++) { /* QEMU may still be starting: retry for 60 s */ |
| 135 | int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 136 | if (fd < 0) return -1; |
| 137 | struct sockaddr_un a; memset(&a, 0, sizeof a); |
| 138 | a.sun_family = AF_UNIX; |
| 139 | strncpy(a.sun_path, path, sizeof a.sun_path - 1); |
| 140 | if (connect(fd, (struct sockaddr *)&a, sizeof a) == 0) return fd; |
| 141 | close(fd); |
| 142 | nap_ms(200); |
| 143 | } |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* Serial receive buffer: text from the kernel (console lines and replies). */ |
| 148 | static char rx[1 << 16]; |
| 149 | static size_t rxn, rxpos; |
| 150 | |
| 151 | /* Reads whatever arrives within timeout_ms into rx. Returns bytes read, or -1. */ |
| 152 | static int serial_fill(int timeout_ms) |
| 153 | { |
| 154 | fd_set fds; FD_ZERO(&fds); FD_SET(serial_fd, &fds); |
| 155 | struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 }; |
| 156 | if (select(serial_fd + 1, &fds, NULL, NULL, &tv) <= 0) return -1; |
| 157 | if (rxpos) { memmove(rx, rx + rxpos, rxn - rxpos); rxn -= rxpos; rxpos = 0; } |
| 158 | if (rxn >= sizeof rx - 1) rxn = 0; /* nothing useful can be this long: drop it */ |
| 159 | ssize_t n = read(serial_fd, rx + rxn, sizeof rx - 1 - rxn); |
| 160 | if (n <= 0) return -1; |
| 161 | if (transcript) fwrite(rx + rxn, 1, (size_t)n, transcript); |
| 162 | rxn += (size_t)n; |
| 163 | rx[rxn] = 0; |
| 164 | return (int)n; |
| 165 | } |
| 166 | |
| 167 | /* The needle and the end of its line: a request sent while the kernel is still |
| 168 | * printing the line would land inside it in the transcript. */ |
| 169 | static int text_line_done(const char *needle) |
| 170 | { |
| 171 | const char *at = strstr(rx + rxpos, needle); |
| 172 | return at && strchr(at, '\n') != NULL; |
| 173 | } |
| 174 | |
| 175 | static int wait_for_text(const char *needle, int seconds) |
| 176 | { |
| 177 | for (int i = 0; i < seconds * 5; i++) { |
| 178 | if (text_line_done(needle)) return 1; |
| 179 | serial_fill(200); |
| 180 | } |
| 181 | return text_line_done(needle); |
| 182 | } |
| 183 | |
| 184 | static void hex_encode(const uint8_t *b, size_t n, char *out) |
| 185 | { |
| 186 | static const char hx[] = "0123456789abcdef"; |
| 187 | for (size_t i = 0; i < n; i++) { out[2 * i] = hx[b[i] >> 4]; out[2 * i + 1] = hx[b[i] & 15]; } |
| 188 | out[2 * n] = 0; |
| 189 | } |
| 190 | |
| 191 | static int hexnib(char c) |
| 192 | { |
| 193 | if (c >= '0' && c <= '9') return c - '0'; |
| 194 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 195 | if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | static int64_t mono_ms(void) |
| 200 | { |
| 201 | struct timespec ts; |
| 202 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 203 | return (int64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; |
| 204 | } |
| 205 | |
| 206 | /* Finds "ABCHRESP " + 128 hex digits in the buffer and decodes it into resp. The reply line |
| 207 | * arrives a few bytes per read, so the wait is bounded by time, not by the number of reads. */ |
| 208 | #define SERIAL_REPLY_MS 30000 |
| 209 | static int serial_recv_resp(uint8_t resp[BENCH_RESP_BYTES]) |
| 210 | { |
| 211 | static const char tag[] = "ABCHRESP "; |
| 212 | for (int64_t until = mono_ms() + SERIAL_REPLY_MS; mono_ms() < until;) { |
| 213 | char *p = strstr(rx + rxpos, tag); |
| 214 | if (p) { |
| 215 | size_t need = strlen(tag) + 2 * BENCH_RESP_BYTES; |
| 216 | if ((size_t)(p - rx) + need <= rxn) { |
| 217 | char *h = p + strlen(tag); |
| 218 | for (unsigned i = 0; i < BENCH_RESP_BYTES; i++) { |
| 219 | int hi = hexnib(h[2 * i]), lo = hexnib(h[2 * i + 1]); |
| 220 | if (hi < 0 || lo < 0) { rxpos = (size_t)(p - rx) + 1; goto next; } |
| 221 | resp[i] = (uint8_t)(hi << 4 | lo); |
| 222 | } |
| 223 | rxpos = (size_t)(p - rx) + need; |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 227 | next: |
| 228 | serial_fill(1000); |
| 229 | } |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | /* ---- one transaction ---- */ |
| 234 | static int transact(uint8_t cmd, uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, enum mode m, |
| 235 | uint32_t *result, uint32_t *before, char *status_name) |
| 236 | { |
| 237 | uint8_t req[BENCH_REQ_BYTES]; |
| 238 | if (m == M_REPLAY) { |
| 239 | if (!have_last) { fprintf(stderr, "replay: nothing was sent yet\n"); return -1; } |
| 240 | memcpy(req, last_req, sizeof req); |
| 241 | } else { |
| 242 | memset(req, 0, sizeof req); |
| 243 | memcpy(req, BENCH_MAGIC_REQ, 4); |
| 244 | req[4] = BENCH_VERSION; req[5] = cmd; |
| 245 | bench_w64(req + 8, m == M_STALE ? 1u : counter++); |
| 246 | bench_w32(req + 16, a0); bench_w32(req + 20, a1); |
| 247 | bench_w32(req + 24, a2); bench_w32(req + 28, a3); |
| 248 | hmac_sha256(bench_key, req, BENCH_HMAC_OFF, req + BENCH_HMAC_OFF); |
| 249 | if (m == M_BADAUTH) req[BENCH_HMAC_OFF] ^= 0x01; |
| 250 | } |
| 251 | memcpy(last_req, req, sizeof req); |
| 252 | have_last = 1; |
| 253 | |
| 254 | uint8_t resp[BENCH_RESP_BYTES]; |
| 255 | if (use_serial) { |
| 256 | char line[8 + 2 * BENCH_REQ_BYTES + 2]; |
| 257 | char hx[2 * BENCH_REQ_BYTES + 1]; |
| 258 | hex_encode(req, sizeof req, hx); |
| 259 | snprintf(line, sizeof line, "ABCHREQ %s\n", hx); |
| 260 | if (write(serial_fd, line, strlen(line)) < 0) { perror("serial write"); return -1; } |
| 261 | if (transcript) fputs(line, transcript); |
| 262 | if (serial_recv_resp(resp) != 0) { fprintf(stderr, "no reply on the serial link\n"); return -1; } |
| 263 | } else { |
| 264 | if (sendto(sock, req, BENCH_REQ_BYTES, 0, (struct sockaddr *)&target, sizeof target) < 0) |
| 265 | { perror("sendto"); return -1; } |
| 266 | fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds); |
| 267 | struct timeval tv = {2, 0}; |
| 268 | if (select(sock + 1, &fds, NULL, NULL, &tv) <= 0) { fprintf(stderr, "timeout\n"); return -1; } |
| 269 | ssize_t n = recv(sock, resp, sizeof resp, 0); |
| 270 | if (n != BENCH_RESP_BYTES) { fprintf(stderr, "bad resp len %zd\n", n); return -1; } |
| 271 | } |
| 272 | if (memcmp(resp, BENCH_MAGIC_RESP, 4)) { fprintf(stderr, "bad magic\n"); return -1; } |
| 273 | /* Every response is HMAC'd by the kernel, auth errors included. */ |
| 274 | uint8_t expected[32]; |
| 275 | hmac_sha256(bench_key, resp, BENCH_HMAC_OFF, expected); |
| 276 | if (memcmp(expected, resp + BENCH_HMAC_OFF, 32)) { fprintf(stderr, "HMAC mismatch\n"); return -1; } |
| 277 | uint8_t st = resp[5]; |
| 278 | if (result) *result = bench_r32(resp + 24); |
| 279 | if (before) *before = bench_r32(resp + 28); |
| 280 | static const char *names[] = {"OK","ERR_AUTH","ERR_RANGE","ERR_ALLONES","ERR_TIMEOUT","ERR_NODEV","ERR_BUSY","ERR_CMD"}; |
| 281 | if (status_name) strcpy(status_name, st < 8 ? names[st] : "ERR_UNKNOWN"); |
| 282 | return (int)st; |
| 283 | } |
| 284 | |
| 285 | /* ---- script ---- */ |
| 286 | struct cmdspec { const char *name; uint8_t cmd; int nargs; }; |
| 287 | static const struct cmdspec specs[] = { |
| 288 | {"ping", BENCH_CMD_PING, 0}, |
| 289 | {"pci_cfg", BENCH_CMD_PCI_CFG, 1}, |
| 290 | {"bar5_read", BENCH_CMD_BAR5_READ, 1}, |
| 291 | {"bar5_write", BENCH_CMD_BAR5_WRITE, 2}, |
| 292 | {"bar0_read", BENCH_CMD_BAR0_READ, 1}, |
| 293 | {"bar0_write", BENCH_CMD_BAR0_WRITE, 2}, |
| 294 | {"bar5_poll", BENCH_CMD_BAR5_POLL, 4}, |
| 295 | {"bar5_rmw", BENCH_CMD_BAR5_RMW, 3}, |
| 296 | }; |
| 297 | |
| 298 | /* Returns 0 to go on, 1 when the script must stop (device gone), 2 on transport failure. */ |
| 299 | static int run_script(FILE *f, int *any_error) |
| 300 | { |
| 301 | char line[256]; int lineno = 0; |
| 302 | while (fgets(line, sizeof line, f)) { |
| 303 | lineno++; |
| 304 | char *hash = strchr(line, '#'); |
| 305 | if (hash) *hash = 0; |
| 306 | char *tok[10]; int nt = 0; |
| 307 | for (char *p = strtok(line, " \t\r\n"); p && nt < 10; p = strtok(NULL, " \t\r\n")) |
| 308 | tok[nt++] = p; |
| 309 | if (!nt) continue; |
| 310 | |
| 311 | enum mode m = M_NORMAL; |
| 312 | int i = 0; |
| 313 | if (!strcmp(tok[0], "badauth")) { m = M_BADAUTH; i = 1; } |
| 314 | else if (!strcmp(tok[0], "stale")) { m = M_STALE; i = 1; } |
| 315 | else if (!strcmp(tok[0], "replay")) m = M_REPLAY; |
| 316 | |
| 317 | const char *label = tok[0]; |
| 318 | uint32_t a[4] = {0, 0, 0, 0}; |
| 319 | const struct cmdspec *spec = NULL; |
| 320 | if (m == M_REPLAY) { |
| 321 | if (nt != 1) { fprintf(stderr, "line %d: replay takes no arguments\n", lineno); *any_error = 1; continue; } |
| 322 | spec = &specs[0]; /* placeholder: replay sends the stored bytes */ |
| 323 | } else { |
| 324 | if (i >= nt) { fprintf(stderr, "line %d: missing command\n", lineno); *any_error = 1; continue; } |
| 325 | for (size_t k = 0; k < sizeof specs / sizeof specs[0]; k++) |
| 326 | if (!strcmp(tok[i], specs[k].name)) spec = &specs[k]; |
| 327 | if (!spec) { fprintf(stderr, "line %d: unknown command %s\n", lineno, tok[i]); *any_error = 1; continue; } |
| 328 | if (nt - (i + 1) != spec->nargs) { |
| 329 | fprintf(stderr, "line %d: %s takes %d argument(s)\n", lineno, spec->name, spec->nargs); |
| 330 | *any_error = 1; continue; |
| 331 | } |
| 332 | int bad = 0; |
| 333 | for (int k = 0; k < spec->nargs; k++) { |
| 334 | char *end = NULL; |
| 335 | unsigned long v = strtoul(tok[i + 1 + k], &end, 0); |
| 336 | if (!end || *end || tok[i + 1 + k][0] == '-') bad = 1; |
| 337 | a[k] = (uint32_t)v; |
| 338 | } |
| 339 | if (bad) { fprintf(stderr, "line %d: bad number in %s\n", lineno, spec->name); *any_error = 1; continue; } |
| 340 | label = (m == M_BADAUTH) ? "badauth" : (m == M_STALE) ? "stale" : ""; |
| 341 | } |
| 342 | |
| 343 | uint32_t result = 0, before = 0; char sname[32] = ""; |
| 344 | int st = transact(spec->cmd, a[0], a[1], a[2], a[3], m, &result, &before, sname); |
| 345 | if (st < 0) { fprintf(stderr, "line %d: %s: no response\n", lineno, tok[0]); return 2; } |
| 346 | char what[96]; |
| 347 | if (m == M_REPLAY) snprintf(what, sizeof what, "replay"); |
| 348 | else if (m == M_NORMAL) snprintf(what, sizeof what, "%s", spec->name); |
| 349 | else snprintf(what, sizeof what, "%s %s", label, spec->name); |
| 350 | printf("%s %s: result=0x%08x before=0x%08x\n", what, sname, result, before); |
| 351 | fflush(stdout); |
| 352 | if (transcript) fprintf(transcript, "tool: %s %s result=0x%08x before=0x%08x\n", what, sname, result, before); |
| 353 | if (st == BENCH_ERR_ALLONES) { |
| 354 | fprintf(stderr, "line %d: device gone (all-ones); stopping\n", lineno); |
| 355 | return 1; |
| 356 | } |
| 357 | if (st != BENCH_OK) *any_error = 1; |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int main(int argc, char **argv) |
| 363 | { |
| 364 | const char *key_path = NULL, *target_ip = "255.255.255.255", *script_path = NULL; |
| 365 | const char *serial_path = NULL, *transcript_path = NULL; |
| 366 | int ping_only = 0; |
| 367 | for (int i = 1; i < argc; i++) { |
| 368 | if (!strcmp(argv[i], "--key") && i + 1 < argc) key_path = argv[++i]; |
| 369 | else if (!strcmp(argv[i], "--target") && i + 1 < argc) target_ip = argv[++i]; |
| 370 | else if (!strcmp(argv[i], "--serial") && i + 1 < argc) serial_path = argv[++i]; |
| 371 | else if (!strcmp(argv[i], "--transcript") && i + 1 < argc) transcript_path = argv[++i]; |
| 372 | else if (!strcmp(argv[i], "ping")) ping_only = 1; |
| 373 | else script_path = argv[i]; |
| 374 | } |
| 375 | |
| 376 | if (key_path) { |
| 377 | FILE *kf = fopen(key_path, "rb"); |
| 378 | if (!kf) { perror(key_path); return 2; } |
| 379 | if (fread(bench_key, 1, 32, kf) != 32) { fprintf(stderr, "key too short\n"); return 2; } |
| 380 | fclose(kf); |
| 381 | } else { |
| 382 | memcpy(bench_key, test_key, 32); |
| 383 | fprintf(stderr, "bench: using built-in test key\n"); |
| 384 | } |
| 385 | if (transcript_path) { |
| 386 | int transcript_fd = open(transcript_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 387 | transcript = transcript_fd < 0 ? NULL : fdopen(transcript_fd, "w"); |
| 388 | if (!transcript) { perror(transcript_path); return 2; } |
| 389 | } |
| 390 | counter = (uint64_t)time(NULL) * 1000u; |
| 391 | |
| 392 | if (serial_path) { |
| 393 | serial_fd = serial_connect(serial_path); |
| 394 | if (serial_fd < 0) { fprintf(stderr, "cannot connect to %s\n", serial_path); return 2; } |
| 395 | use_serial = 1; |
| 396 | if (!wait_for_text("bench: GPU bench console ready", 900)) { |
| 397 | fprintf(stderr, "the kernel never said the bench console is ready\n"); |
| 398 | return 2; |
| 399 | } |
| 400 | } else { |
| 401 | init_net(target_ip); |
| 402 | } |
| 403 | |
| 404 | int rc = 0; |
| 405 | if (ping_only) { |
| 406 | uint32_t result = 0; char sname[32] = ""; |
| 407 | int st = transact(BENCH_CMD_PING, 0, 0, 0, 0, M_NORMAL, &result, NULL, sname); |
| 408 | if (st < 0) { printf("ping: no response\n"); rc = 2; } |
| 409 | else { printf("ping: %s\n", st == 0 ? "PONG" : sname); rc = st == 0 ? 0 : 1; } |
| 410 | } else { |
| 411 | if (!script_path) { |
| 412 | fprintf(stderr, "usage: ck_gpu_bench [--key F] [--target IP | --serial PATH] [--transcript F] <script.bench> | ping\n"); |
| 413 | return 2; |
| 414 | } |
| 415 | FILE *f = strcmp(script_path, "-") ? fopen(script_path, "r") : stdin; |
| 416 | if (!f) { perror(script_path); return 2; } |
| 417 | int any_error = 0; |
| 418 | int r = run_script(f, &any_error); |
| 419 | if (f != stdin) fclose(f); |
| 420 | rc = r == 2 ? 2 : (r == 1 || any_error) ? 1 : 0; |
| 421 | } |
| 422 | if (transcript) fclose(transcript); |
| 423 | return rc; |
| 424 | } |