summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/remote/remote_comms.c
blob: bc50cf037476cbbb6efeeb7af2aec31c71fa0636 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>

#include "pipe/p_compiler.h"

#include "winsys_bindings.h"
#include "tr_screen.h"
#include "remote_messages.h"
#include "remote_comms.h"
#include "remote_debug.h"

int last_alloc_is_in_place = 0;
// XXX this relies on no two calls to allocate_message_memory without an
// enqueue_message in between. If this changes, need to start properly tracking
// whether each alloc is in-place or not.

int last_read_is_in_place = 0;

extern int analyse_waits;

void check_for_asynchronous_failures(void);
void wait_for_synchronous_message(void);

static int drain_events(int fd) {

  int flags = fcntl(fd, F_GETFL);

  int nbflags = flags | O_NONBLOCK;

  fcntl(fd, F_SETFL, nbflags);

  char waste[1024];

  int ret;
  while((ret = recv(fd, waste, 1024, 0)) > 0) {

  }

  fcntl(fd, F_SETFL, flags);

  if(ret == -1 && errno == EAGAIN)
    return 0;
  else {
    printf("Failed draining events: return %d, errno %d\n", ret, errno);
    return -1;
  }

}

static int wait_for_char(int fd, char c) {

  char received = c + 1;

  DBG("Waiting for a %c\n", c);

  while(received != c) {
    int ret = recv(fd, &received, 1, 0);
    DBG("Got a char: %c\n", received);
    if(ret <= 0) {
      printf("Error waiting for a %c (return %d, errno %d)\n", c, ret, errno);
      return -1;
    }
  }

  DBG("That's what I was looking for; returning success\n");

  return 0;

}

static int wait_for_tx_not_full() {

  DBG("Waiting for an event on the transmit ring\n");
  return wait_for_char(singleton_screen->socketfd, 'T');

}

static int wait_for_rx_not_empty() {

  DBG("Waiting for an event on the read-ring\n");
  return wait_for_char(singleton_screen->socketfd, 'R');

}

void* allocate_message_memory(size_t bytes) {

  uint32_t bufsize = ((uint32_t*)singleton_screen->tx_buffer)[0];
  uint32_t readoff = ((uint32_t*)singleton_screen->tx_buffer)[1];
  uint32_t writeoff = ((uint32_t*)singleton_screen->tx_buffer)[2];
  char* writebuf = (char*)&(((uint32_t*)singleton_screen->tx_buffer)[3]);

  int contiguous_available_space;

  if(readoff > writeoff)
    contiguous_available_space = (readoff - writeoff) - 1;
  else {
    contiguous_available_space = (bufsize - writeoff);
    if(readoff == 0)
      contiguous_available_space--;
    /* We can write up to the end of the buffer, except if the read pointer is at 0;
       if it is, writing to the end would set write == read, and therefore empty the buffer,
       discarding a full ring. */
  }

  struct message_header* new_message = 0;

  if(contiguous_available_space >= bytes) {
    new_message = (struct message_header*)(writebuf + writeoff);
    last_alloc_is_in_place = 1;
  }
  else {
    new_message = (struct message_header*)malloc(bytes);
    if(!new_message) {
      printf("Malloc failed allocating a message (wanted %d bytes)\n", (int)bytes);
      exit(1);
    }
    last_alloc_is_in_place = 0;
  }

   new_message->length = bytes;

   return new_message;

}

#ifdef DEBUG_RING_WRITE
static void debug_print_bytes(char* bytes, int n) {

  for(int i = 0; i < n; i++) {
    printf("%x ", (unsigned int)(bytes[i]));
    if(i && (!(i % 16)))
      printf("\n");
  }

  printf("\n");

}
#endif // DEBUG_RING_WRITE

static int sendsome(uint32_t* readoff, uint32_t* writeoff, uint32_t bufsize, char* writebuf, char* data, int length) {

  uint32_t base = *writeoff;
  uint32_t limit = *readoff;
  uint32_t space;
  int writewrapsbuffer;

  if(limit > base) {
    space = (limit - base) - 1;
  }
  else {
    space = base - limit;
    space = (bufsize - space) - 1;
    // Care! > rather than >= because if it == buffersize, the write does   
    // not wrap, but the pointer does                                       
  }

  int towrite = space >= length ? length : space;

  DBG("WRITING: Will write %d bytes (current r/w %u/%u)\n", towrite, limit, base);
  //DBG("Those bytes of the user's buffer:\n");
  //debug_print_bytes(data, towrite);

  if((base + towrite) > bufsize)
    writewrapsbuffer = 1;
  else
    writewrapsbuffer = 0;

  if(towrite == 0) {
    // Looks like the buffer is full: let's wait
    return 0;
  }

  // Okay, we have (towrite) bytes to write: let's do that. 
  if(!writewrapsbuffer) {
    DBG("Wrote %d bytes (contiguous)\n", towrite);
    memcpy(writebuf + base, data, towrite);
  }
  else {
    int bytestoend = bufsize - base;
    memcpy(writebuf + base, data, bytestoend);
    DBG("Wrote %d bytes at buffer end\n", bytestoend);
    memcpy(writebuf, data + bytestoend, towrite - bytestoend);
    DBG("Wrote %d bytes at buffer start\n", towrite - bytestoend);
  }

  return towrite;

}

void enqueue_message(void* message) {

  struct message_header* header = (struct message_header*)message;
  uint32_t bufsize = ((uint32_t*)singleton_screen->tx_buffer)[0];
  uint32_t* readoff = &(((uint32_t*)singleton_screen->tx_buffer)[1]);
  uint32_t* writeoff = &(((uint32_t*)singleton_screen->tx_buffer)[2]);
  char* writebuf = (char*)&(((uint32_t*)singleton_screen->tx_buffer)[3]);

  DBG("Sending %s (length %u)\n", optotext(header->opcode), header->length);

  check_for_asynchronous_failures();

  if(last_alloc_is_in_place) {

    uint32_t newwriteoff = *writeoff + header->length;
    if(newwriteoff >= bufsize)
      newwriteoff -= bufsize;
    
    *writeoff = newwriteoff;

    send(singleton_screen->socketfd, "T", 1, 0);
    return;
  }
    
  int bytesSent = 0;

  while(bytesSent < header->length) {

    if(drain_events(singleton_screen->socketfd) == -1) {
      printf("Socket error draining events\n");
      exit(1);
    }

#ifdef DEBUG_RING_WRITE
    uint32_t base = *readoff;
    uint32_t limit = *writeoff;

    int bytes_available_for_readers;
    if(limit > base)
      bytes_available_for_readers = limit - base;
    else
      bytes_available_for_readers = bufsize - (base - limit);

    int read_would_wrap = 0;
    if((base + bytes_available_for_readers) > bufsize)
      read_would_wrap = 1;

    printf("Writing with readoff %u, writeoff %u: dumping unread bytes\n", base, limit);

    if(!read_would_wrap) {
      debug_print_bytes(writebuf + base, bytes_available_for_readers);
    }
    else {
      int bytes_to_end = bufsize - base;
      debug_print_bytes(writebuf + base, bytes_to_end);
      printf("---buffer end---\n");
      debug_print_bytes(writebuf, bytes_available_for_readers - bytes_to_end);
    }

#endif // DEBUG_RING_WRITE
     
    int sent = sendsome(readoff, writeoff, bufsize, writebuf, ((char*)header + bytesSent), header->length - bytesSent);

    if(sent == 0) {
      if(analyse_waits)
	printf("Wait: transmit buffer full... ");
      if(wait_for_tx_not_full() == -1) {
	printf("Socket error waiting for tx-not-full\n");
	exit(1);
      }
      if(analyse_waits)
	printf("Woke to find read %u, write %u\n", *readoff, *writeoff);
    }

    uint32_t newwriteoff = *writeoff + sent;
    if(newwriteoff >= bufsize)
      newwriteoff -= bufsize;

    *writeoff = newwriteoff;

    bytesSent += sent;

    // Notify the remote that we have sent something
    send(singleton_screen->socketfd, "T", 1, 0);

  }
   
  free(header);

}

static uint32_t read_avail(uint32_t* readoff, uint32_t* writeoff, uint32_t buffersize) {

  uint32_t limit = *writeoff;
  uint32_t base = *readoff;

  DBG("read-avail: found limit = %u, base = %u\n", limit, base);

  if(limit > base)
    return limit - base;
  else if(limit == base)
    return 0;
  else
    return buffersize - (base - limit);

}

static int read_would_wrap(uint32_t* readoff, uint32_t buffersize, uint32_t bytes) {

  return (((*readoff) + bytes) > buffersize);

}

static int recvsome(uint32_t* readoff, uint32_t* writeoff, uint32_t bufsize, char* readbuf, void* outbuf, int length) {

  int available = (int)read_avail(readoff, writeoff, bufsize);

  if(!available)
    return 0;

  int tocopy = available < length ? available : length;

  if(read_would_wrap(readoff, bufsize, tocopy)) {
    int bytestoend = (bufsize - *readoff);
    memcpy(outbuf, readbuf + (*readoff), bytestoend);
    memcpy(((char*)outbuf) + bytestoend, readbuf, tocopy - bytestoend);
  }
  else {
    memcpy(outbuf, readbuf + (*readoff), tocopy);
  }

  return tocopy;

}

static void discard_ring_bytes(uint32_t* readoff, uint32_t bufsize, int bytes_to_discard) {

  uint32_t newreadoff = *readoff + bytes_to_discard;
  if(newreadoff >= bufsize)
    newreadoff -= bufsize;
  
  *readoff = newreadoff;
  send(singleton_screen->socketfd, "R", 1, 0);
  
}


static void partial_recv(uint32_t* readoff, uint32_t* writeoff, uint32_t bufsize, char* readbuf, struct message_header* message, int* current_bytes, int target_bytes) {

  while(*current_bytes < target_bytes) {

    if(drain_events(singleton_screen->socketfd) == -1) {
      printf("Socket error draining events\n");
      exit(1);
    }

    int this_recv = recvsome(readoff, writeoff, bufsize, readbuf, 
			 (void*)((char*)message + *current_bytes), 
			     target_bytes - *current_bytes);

    if(this_recv == 0) {
      if(analyse_waits)
	printf("Wait: receive buffer empty\n");
      if(wait_for_rx_not_empty() == -1) {
	printf("Socket error waiting for rx-not-empty\n");
	exit(1);
      }
    }

    discard_ring_bytes(readoff, bufsize, this_recv);
    
    *current_bytes += this_recv;

  }

}

struct message_header* get_message(void) {

  uint32_t bufsize = ((uint32_t*)singleton_screen->rx_buffer)[0];
  uint32_t* readoff = &(((uint32_t*)singleton_screen->rx_buffer)[1]);
  uint32_t* writeoff = &(((uint32_t*)singleton_screen->rx_buffer)[2]);
  char* readbuf = (char*)&(((uint32_t*)singleton_screen->rx_buffer)[3]);

  int size;

  DBG("Entering get_message: readoff = %u, writeoff = %u\n", *readoff, *writeoff);

  int avail;

  wait_for_synchronous_message();

  if(!read_would_wrap(readoff, bufsize, sizeof(struct message_header))) {

    DBG("Looking for a message header's worth of data\n");

    while((avail = read_avail(readoff, writeoff, bufsize)) < sizeof(struct message_header))  { 
      DBG("Waiting for more to be written (currently have %d, wanting %d\n)\n", avail, sizeof(struct message_header)); 
      wait_for_rx_not_empty(); 
    }

    size = ((struct message_header*)(readbuf + *readoff))->length;
    DBG("Found message has length %d\n", size);

    if(read_would_wrap(readoff, bufsize, size)) {
      DBG("Read would wrap; not reading in place\n");
      last_read_is_in_place = 0;
    }
    else {
      DBG("Read would not wrap; reading in place\n");
      last_read_is_in_place = 1;
    }

  }
  else {
    DBG("Reading only a message_header would wrap; not reading in place\n");
    last_read_is_in_place = 0;
  }
  
  if(last_read_is_in_place) {

    DBG("Reading in place\n");

    // Pass out a pointer to the ring buffer in-place, once it is full enough
    while((avail = read_avail(readoff, writeoff, bufsize)) < size)  {
      DBG("Waiting for more to be written (got %d, wanting %d)\n", avail, size);
      wait_for_rx_not_empty();
    }

    DBG("Got enough; passing out in-place pointer\n");
    return (struct message_header*)(readbuf + *readoff);

  }
  else {

    DBG("Not reading in place\n");

    // Malloc a temporary and progressively copy into it
    struct message_header* new_message = (struct message_header*)malloc(sizeof(struct message_header));

    int bytes_received = 0;

    partial_recv(readoff, writeoff, bufsize, readbuf, new_message, &bytes_received, sizeof(struct message_header));

    if(new_message->length < sizeof(struct message_header)) {
      printf("Unreasonably short message encountered whilst receiving (length %d)\n", new_message->length);
      exit(1);
    }

    new_message = (struct message_header*)realloc(new_message, new_message->length);

    partial_recv(readoff, writeoff, bufsize, readbuf, new_message, &bytes_received, new_message->length);
  
    DBG("Received %s (length %u)\n", optotext(new_message->opcode), new_message->length);

    return new_message;
  }

}

void free_message(void* message) {


  if(last_read_is_in_place) {
    // Move the read pointer forwards
    uint32_t bufsize = ((uint32_t*)singleton_screen->rx_buffer)[0];
    uint32_t* readoff = &(((uint32_t*)singleton_screen->rx_buffer)[1]);

    int size = ((struct message_header*)message)->length;

    uint32_t newreadoff = *readoff + size;
    if(newreadoff >= bufsize)
      newreadoff -= bufsize;
    *readoff = newreadoff;

    send(singleton_screen->socketfd, "R", 1, 0);    
    
  }
  else {

    free(message);

  }

}

static int message_is_asynchronous(uint32_t opcode) {

  if(opcode == REMREP_TEXTURE_CREATE ||
     opcode == REMREP_TEXTURE_BLANKET ||
     opcode == REMREP_GET_TEX_SURFACE ||
     opcode == REMREP_BUFFER_SET_DATA)
    return 1;
  else
    return 0;

}

static void check_async_result(char* message, uint32_t opcode) {

  switch(opcode) {
  case REMREP_TEXTURE_CREATE: {
    struct remrep_texture_create* rep = (struct remrep_texture_create*)message;
    if(rep->success) {
      DBG("Ack: texture_create\n");
    }
    else {
      printf("!!! TEXTURE_CREATE FAILED. This may cause subsequent failures and incorrect rendering\n");
    }
    break;
  }
  case REMREP_TEXTURE_BLANKET: {
    struct remrep_texture_blanket* rep = (struct remrep_texture_blanket*)message;
    if(rep->success) {
      DBG("Ack: texture_blanket\n");
    }
    else {
      printf("!!! TEXTURE_BLANKET FAILED. This may cause subsequent failures and incorrect rendering\n");
    }
    break;
  }
  case REMREP_GET_TEX_SURFACE: {
    struct remrep_get_tex_surface* rep = (struct remrep_get_tex_surface*)message;
    if(rep->success) {
      DBG("Ack: get_tex_surface\n");
    }
    else {
      printf("!!! GET_TEX_SURFACE FAILED. This may cause subsequent failures and incorrect rendering\n");
    }
    break;
  }
  case REMREP_BUFFER_SET_DATA: {
    struct remrep_buffer_set_data* rep = (struct remrep_buffer_set_data*)message;
    if(rep->success) {
      DBG("Ack: buffer_set_data\n");
    }
    else {
      printf("!!! BUFFER_SET_DATA FAILED. This may cause subsequent failures and incorrect rendering\n");
    }
    break;
  }
  default:
    printf("BUG! Check async result handed opcode %s\n", optotext(opcode));
  }
    

}

static void discard_asynchronous_messages(int block) {

  uint32_t bufsize = ((uint32_t*)singleton_screen->rx_buffer)[0];
  uint32_t* readoff = &(((uint32_t*)singleton_screen->rx_buffer)[1]);
  uint32_t* writeoff = &(((uint32_t*)singleton_screen->rx_buffer)[2]);
  char* readbuf = (char*)&(((uint32_t*)singleton_screen->rx_buffer)[3]);

  if(block) {
    DBG("Discarding async messages (blocking)\n");
  }
  else {
    DBG("Discarding async messages (non-blocking)\n");
  }
  // WARNING HERE: This method assumes that no async failure notification
  // is bigger than the ring. If one were, we would wait fruitlessly until
  // the entire thing were present before deleting it from the ring.

  /* If block is set, we should wait until a synchronous message arrives.
     If block is not set, we should return as soon as we're short of data. */

  int need_more_bytes = 0;

  while(1) {

    struct message_header head;

    if(need_more_bytes) {
      if(block) {
	DBG("Insufficient data, but invoked blocking: sleeping\n");
	wait_for_rx_not_empty();
	need_more_bytes = 0;
	DBG("Woken\n");
      }
      else {
	DBG("Non-blocking discard_async_messages: exiting\n");
	return;
      }
    }

    if(drain_events(singleton_screen->socketfd) == -1) {
      printf("Socket error draining events in discard_async\n");
      return;
    }

    int bytes_peeked = recvsome(readoff, writeoff, bufsize, readbuf, (char*)&head, sizeof(struct message_header));

    if(bytes_peeked < sizeof(struct message_header)) {
      DBG("  Less than a header available (%d bytes); exiting\n", bytes_peeked);
      need_more_bytes = 1;
      continue;
    }

    if(!message_is_asynchronous(head.opcode)) {
      DBG("  Next message (%s) not asynchronous: exiting\n", optotext(head.opcode));
      return;
    }

    int bytes_available = read_avail(readoff, writeoff, bufsize);

    if(bytes_available < head.length) {
      DBG("  Message had length %d but only %d available: exiting\n", head.length, bytes_available);
      need_more_bytes = 1;
      continue;
    }

    char* message_proper = malloc(head.length);
    if(!message_proper) {
      printf("OOM checking for asynchronous failures\n");
      return;
    }

    int bytes_read = recvsome(readoff, writeoff, bufsize, readbuf, message_proper, head.length);

    if(bytes_read < head.length) {
      printf("BUG! read_avail returned %d, yet we could only actually read %d bytes\n", bytes_available, bytes_read);
      free(message_proper);
      return;
    }

    DBG("  Got a message: checking its result\n");

    check_async_result(message_proper, head.opcode);

    free(message_proper);

    discard_ring_bytes(readoff, bufsize, head.length);

  }

}

void wait_for_synchronous_message(void) {

  /* This is like check_for_asynchronous_failures, but we must block
     until a synchronous message comes along */

  discard_asynchronous_messages(1);

}

void check_for_asynchronous_failures(void) {

  discard_asynchronous_messages(0);

}