summaryrefslogtreecommitdiff
path: root/gcell/lib/runtime/gc_job_manager_impl.cc
blob: 58597cf2766c173506dd75e1b4572678889bf671 (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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
/* -*- c++ -*- */
/*
 * Copyright 2007,2008,2009,2010 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gc_job_manager_impl.h"
#include <gcell/gc_mbox.h>
#include <gcell/gc_aligned_alloc.h>
#include <gcell/memory_barrier.h>
#include <gc_proc_def_utils.h>
#include <atomic_dec_if_positive.h>
#include <stdio.h>
#include <stdexcept>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sched.h>

typedef boost::unique_lock<boost::mutex> scoped_lock;

#define __nop() __asm__ volatile ("ori 0,0,0" : : : "memory")
#define __cctpl() __asm__ volatile ("or 1,1,1" : : : "memory")
#define __cctpm() __asm__ volatile ("or 2,2,2" : : : "memory")
#define __cctph() __asm__ volatile ("or 3,3,3" : : : "memory")
#define __db8cyc() __asm__ volatile ("or 28,28,28" : : : "memory")
#define __db10cyc() __asm__ volatile ("or 29,29,29" : : : "memory")
#define __db12cyc() __asm__ volatile ("or 30,30,30" : : : "memory")
#define __db16cyc() __asm__ volatile ("or 31,31,31" : : : "memory")


#if 1
#define CCTPL() __cctpl()
#define CCTPM() __cctpm()
#else
#define CCTPL() (void) 0
#define CCTPM() (void) 0
#endif

static const size_t CACHE_LINE_SIZE = 128;

static const unsigned int DEFAULT_MAX_JOBS = 128;
static const unsigned int DEFAULT_MAX_CLIENT_THREADS = 64;

// FIXME this really depends on the SPU code...
static const unsigned int MAX_TOTAL_INDIRECT_LENGTH = 16 * 1024;


static bool          s_key_initialized = false;
static pthread_key_t s_client_key;

static int s_worker_debug = 0;

// custom deleter of gang_contexts for use with boost::shared_ptr
class gang_deleter {
public:
  void operator()(spe_gang_context_ptr_t ctx) {
    if (ctx){
      int r = spe_gang_context_destroy(ctx);
      if (r != 0){
	perror("spe_gang_context_destroy");
      }
    }
  }
};


// custom deleter of anything that can be freed with "free"
class free_deleter {
public:
  void operator()(void *p) {
    free(p);
  }
};


/*
 * Called when client thread is destroyed.
 * We mark our client info free.
 */
static void
client_key_destructor(void *p)
{
  ((gc_client_thread_info *) p)->d_free = 1;
}

static bool
is_power_of_2(uint32_t x)
{
  return (x != 0) && !(x & (x - 1));
}

////////////////////////////////////////////////////////////////////////


gc_job_manager_impl::gc_job_manager_impl(const gc_jm_options *options)
  : d_debug(0), d_spu_args(0),
    d_eh_cond(), d_eh_thread(0), d_eh_state(EHS_INIT),
    d_shutdown_requested(false),
    d_jc_cond(), d_jc_thread(0), d_jc_state(JCS_INIT), d_jc_njobs_active(0),
    d_ntell(0), d_tell_start(0),
    d_client_thread(0), d_ea_args_maxsize(0),
    d_proc_def(0), d_proc_def_ls_addr(0), d_nproc_defs(0)
{
  if (!s_key_initialized){
    int r = pthread_key_create(&s_client_key, client_key_destructor);
    if (r != 0)
      throw std::runtime_error("pthread_key_create");
    s_key_initialized = true;
  }

  // ensure it's zero
  pthread_setspecific(s_client_key, 0);

  if (options != 0)
    d_options = *options;

  // provide the real default for those indicated with a zero
  if (d_options.max_jobs == 0)
    d_options.max_jobs = DEFAULT_MAX_JOBS;
  if (d_options.max_client_threads == 0)
    d_options.max_client_threads = DEFAULT_MAX_CLIENT_THREADS;

  if (!d_options.program_handle){
    fprintf(stderr, "gc_job_manager: options->program_handle must be non-zero\n");
    throw std::runtime_error("gc_job_manager: options->program_handle must be non-zero");
  }

  int ncpu_nodes = spe_cpu_info_get(SPE_COUNT_PHYSICAL_CPU_NODES, -1);
  int nusable_spes = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);

  if (debug()){
    printf("cpu_nodes = %d\n", ncpu_nodes);
    for (int i = 0; i < ncpu_nodes; i++){
      printf("node[%d].physical_spes = %2d\n", i,
	     spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, i));
      printf("node[%d].usable_spes   = %2d\n", i,
	     spe_cpu_info_get(SPE_COUNT_USABLE_SPES, i));
    }
  }

  // clamp nspes
  d_options.nspes = std::min(d_options.nspes, (unsigned int) MAX_SPES);
  nusable_spes = std::min(nusable_spes, (int) MAX_SPES);

  //
  // sanity check requested number of spes.
  //
  if (d_options.nspes == 0)	// use all of them
    d_options.nspes = nusable_spes;
  else {
    if (d_options.nspes > (unsigned int) nusable_spes){
      fprintf(stderr,
	      "gc_job_manager: warning: caller requested %d spes.  There are only %d available.\n",
	      d_options.nspes, nusable_spes);
      if (d_options.gang_schedule){
	// If we're gang scheduling we'll never get scheduled if we
	// ask for more than are available.
	throw std::out_of_range("gang_scheduling: not enough spes available");
      }
      else {	// FIXME clamp to usable.  problem on PS3 when overcommited
	fprintf(stderr, "gc_job_manager: clamping nspes to %d\n", nusable_spes);
	d_options.nspes = nusable_spes;
      }
    }
  }

  if (d_options.use_affinity){
    printf("gc_job_manager: warning: affinity request was ignored\n");
  }

  if (d_options.gang_schedule){
    d_gang = spe_gang_context_sptr(spe_gang_context_create(0), gang_deleter());
    if (!d_gang){
      perror("gc_job_manager_impl[spe_gang_context_create]");
      throw std::runtime_error("spe_gang_context_create");
    }
  }

  d_ntell = std::min(d_options.nspes, 2U);

  // ----------------------------------------------------------------
  // initalize the job queue
  
  d_queue = (gc_jd_queue_t *) gc_aligned_alloc(sizeof(gc_jd_queue_t), CACHE_LINE_SIZE);
  _d_queue_boost =
    boost::shared_ptr<void>((void *) d_queue, free_deleter());
  gc_jd_queue_init(d_queue);


  // ----------------------------------------------------------------
  // create the spe contexts

  // 1 spu_arg struct for each SPE
  assert(sizeof(gc_spu_args_t) % 16 == 0);
  d_spu_args =
    (gc_spu_args_t *) gc_aligned_alloc(MAX_SPES * sizeof(gc_spu_args_t), 16);
  _d_spu_args_boost =
    boost::shared_ptr<void>((void *) d_spu_args, free_deleter());

  // 2 completion info structs for each SPE (we double buffer them)
  assert(sizeof(gc_comp_info_t) % CACHE_LINE_SIZE == 0);
  d_comp_info =
    (gc_comp_info_t *) gc_aligned_alloc(2 * MAX_SPES * sizeof(gc_comp_info_t),
					CACHE_LINE_SIZE);
  _d_comp_info_boost =
    boost::shared_ptr<void>((void *) d_comp_info, free_deleter());


  // get a handle to the spe program

  spe_program_handle_t *spe_image = d_options.program_handle.get();

  // fish proc_def table out of SPE ELF file

  if (!gcpd_find_table(spe_image, &d_proc_def, &d_nproc_defs, &d_proc_def_ls_addr)){
    fprintf(stderr, "gc_job_manager_impl: couldn't find gc_proc_defs in SPE ELF file.\n");
    throw std::runtime_error("no gc_proc_defs");
  }
  // fprintf(stderr, "d_proc_def_ls_addr = 0x%0x\n", d_proc_def_ls_addr);

  int spe_flags = (SPE_EVENTS_ENABLE
		   | SPE_MAP_PS
		   | SPE_CFG_SIGNOTIFY1_OR
		   | SPE_CFG_SIGNOTIFY2_OR);
  
  for (unsigned int i = 0; i < d_options.nspes; i++){
    // FIXME affinity stuff goes here
    d_worker[i].spe_ctx = spe_context_create(spe_flags, d_gang.get());;
    if (d_worker[i].spe_ctx == 0){
      perror("spe_context_create");
      throw std::runtime_error("spe_context_create");
    }

    d_worker[i].spe_ctrl = 
      (spe_spu_control_area_t *)spe_ps_area_get(d_worker[i].spe_ctx, SPE_CONTROL_AREA);
    if (d_worker[i].spe_ctrl == 0){
      perror("spe_ps_area_get(SPE_CONTROL_AREA)");
      throw std::runtime_error("spe_ps_area_get(SPE_CONTROL_AREA)");
    }

    d_worker[i].spe_idx = i;
    d_worker[i].spu_args = &d_spu_args[i];
    d_worker[i].spu_args->queue = ptr_to_ea(d_queue);
    d_worker[i].spu_args->comp_info[0] = ptr_to_ea(&d_comp_info[2*i+0]);
    d_worker[i].spu_args->comp_info[1] = ptr_to_ea(&d_comp_info[2*i+1]);
    d_worker[i].spu_args->spu_idx = i;
    d_worker[i].spu_args->nspus = d_options.nspes;
    d_worker[i].spu_args->proc_def_ls_addr = d_proc_def_ls_addr;
    d_worker[i].spu_args->nproc_defs = d_nproc_defs;
    d_worker[i].spu_args->log.base = 0;
    d_worker[i].spu_args->log.nentries = 0;
    d_worker[i].state = WS_INIT;

    int r = spe_program_load(d_worker[i].spe_ctx, spe_image);
    if (r != 0){
      perror("spe_program_load");
      throw std::runtime_error("spe_program_load");
    }
  }

  setup_logfiles();

  // ----------------------------------------------------------------
  // initalize the free list of job descriptors
  
  d_free_list = (gc_jd_stack_t *) gc_aligned_alloc(sizeof(gc_jd_stack_t), CACHE_LINE_SIZE);
  // This ensures that the memory associated with d_free_list is
  // automatically freed in the destructor or if an exception occurs
  // here in the constructor.
  _d_free_list_boost =
    boost::shared_ptr<void>((void *) d_free_list, free_deleter());
  gc_jd_stack_init(d_free_list);

  if (debug()){
    printf("sizeof(d_jd[0]) = %d (0x%x)\n", sizeof(d_jd[0]), sizeof(d_jd[0]));
    printf("max_jobs = %u\n", d_options.max_jobs);
  }

  // Initialize the array of job descriptors.
  d_jd = (gc_job_desc_t *) gc_aligned_alloc(sizeof(d_jd[0]) * d_options.max_jobs, CACHE_LINE_SIZE);
  _d_jd_boost = boost::shared_ptr<void>((void *) d_jd, free_deleter());


  // set unique job_id
  for (int i = 0; i < (int) d_options.max_jobs; i++)
    d_jd[i].sys.job_id = i;

  // push them onto the free list
  for (int i = d_options.max_jobs - 1; i >= 0; i--)
    free_job_desc(&d_jd[i]);

  // ----------------------------------------------------------------
  // initialize d_client_thread

  {
    gc_client_thread_info_sa cti(
         new gc_client_thread_info[d_options.max_client_threads]);

    d_client_thread.swap(cti);

    for (unsigned int i = 0; i < d_options.max_client_threads; i++)
      d_client_thread[i].d_client_id = i;
  }

  // ----------------------------------------------------------------
  // initialize bitvectors

  // initialize d_bvlen, the number of longs in job related bitvectors.
  int bits_per_long = sizeof(unsigned long) * 8;
  d_bvlen = (d_options.max_jobs + bits_per_long - 1) / bits_per_long;

  // allocate all bitvectors in a single cache-aligned chunk
  size_t nlongs = d_bvlen * d_options.max_client_threads;
  void *p = gc_aligned_alloc(nlongs * sizeof(unsigned long), CACHE_LINE_SIZE);
  _d_all_bitvectors = boost::shared_ptr<void>(p, free_deleter());

  // Now point the gc_client_thread_info bitvectors into this storage
  unsigned long *v = (unsigned long *) p;

  for (unsigned int i = 0; i < d_options.max_client_threads; i++, v += d_bvlen)
    d_client_thread[i].d_jobs_done = v;


  // ----------------------------------------------------------------
  // create the spe event handler & worker (SPE) threads

  create_event_handler();
}

////////////////////////////////////////////////////////////////////////

gc_job_manager_impl::~gc_job_manager_impl()
{
  shutdown();

  d_jd = 0;		// handled via _d_jd_boost
  d_free_list = 0;	// handled via _d_free_list_boost
  d_queue = 0;		// handled via _d_queue_boost

  // clear cti, since we've deleted the underlying data
  pthread_setspecific(s_client_key, 0);

  unmap_logfiles();
}

bool
gc_job_manager_impl::shutdown()
{
  scoped_lock 	l(d_eh_mutex);

  {
    scoped_lock 	l2(d_jc_mutex);
    d_shutdown_requested = true;	// set flag for event handler thread
    d_jc_cond.notify_one();			// wake up job completer
  }

  // should only happens during early QA code
  if (d_eh_thread == 0 && d_eh_state == EHS_INIT)
    return false;

  while (d_eh_state != EHS_DEAD)	// wait for it to finish
    d_eh_cond.wait(l);

  return true;
}

int
gc_job_manager_impl::nspes() const
{
  return d_options.nspes;
}

////////////////////////////////////////////////////////////////////////

void
gc_job_manager_impl::bv_zero(unsigned long *bv)
{
  memset(bv, 0, sizeof(unsigned long) * d_bvlen);
}

inline void
gc_job_manager_impl::bv_clr(unsigned long *bv, unsigned int bitno)
{
  unsigned int wi = bitno / (sizeof (unsigned long) * 8);
  unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
  bv[wi] &= ~(1UL << bi);
}

inline void
gc_job_manager_impl::bv_set(unsigned long *bv, unsigned int bitno)
{
  unsigned int wi = bitno / (sizeof (unsigned long) * 8);
  unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
  bv[wi] |= (1UL << bi);
}

inline bool
gc_job_manager_impl::bv_isset(unsigned long *bv, unsigned int bitno)
{
  unsigned int wi = bitno / (sizeof (unsigned long) * 8);
  unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
  return (bv[wi] & (1UL << bi)) != 0;
}

inline bool
gc_job_manager_impl::bv_isclr(unsigned long *bv, unsigned int bitno)
{
  unsigned int wi = bitno / (sizeof (unsigned long) * 8);
  unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
  return (bv[wi] & (1UL << bi)) == 0;
}

////////////////////////////////////////////////////////////////////////

gc_job_desc *
gc_job_manager_impl::alloc_job_desc()
{
  // stack is lock free, and safe to call from any thread
  gc_job_desc *jd = gc_jd_stack_pop(d_free_list);
  if (jd == 0)
    throw gc_bad_alloc("alloc_job_desc: none available");

  return jd;
}

void
gc_job_manager_impl::free_job_desc(gc_job_desc *jd)
{
  // stack is lock free, thus safe to call from any thread
  if (jd != 0)
    gc_jd_stack_push(d_free_list, jd);
}

////////////////////////////////////////////////////////////////////////


inline bool
gc_job_manager_impl::incr_njobs_active()
{
  scoped_lock	l(d_jc_mutex);

  if (d_shutdown_requested)
    return false;

  if (d_jc_njobs_active++ == 0)	// signal on 0 to 1 transition
    d_jc_cond.notify_one();

  return true;
}

inline void
gc_job_manager_impl::decr_njobs_active(int n)
{
  scoped_lock	l(d_jc_mutex);
  d_jc_njobs_active -= n;
}


/*
 * We check as much as we can here on the PPE side, so that the SPE
 * doesn't have to.
 */
static bool
check_direct_args(gc_job_desc *jd, gc_job_direct_args *args)
{
  if (args->nargs > MAX_ARGS_DIRECT){
    jd->status = JS_BAD_N_DIRECT;
    return false;
  }

  return true;
}

static bool
check_ea_args(gc_job_desc *jd, gc_job_ea_args *p)
{
  if (p->nargs > MAX_ARGS_EA){
    jd->status = JS_BAD_N_EA;
    return false;
  }

  uint32_t dir_union = 0;

  for (unsigned int i = 0; i < p->nargs; i++){
    dir_union |= p->arg[i].direction;
    switch(p->arg[i].direction){
    case GCJD_DMA_GET:
    case GCJD_DMA_PUT:
      break;

    default:
      jd->status = JS_BAD_DIRECTION;
      return false;
    }
  }

  if (p->nargs > 1){
    unsigned int common_eah = (p->arg[0].ea_addr) >> 32;
    for (unsigned int i = 1; i < p->nargs; i++){
      if ((p->arg[i].ea_addr >> 32) != common_eah){
	jd->status = JS_BAD_EAH;
	return false;
      }
    }
  }

  jd->sys.direction_union = dir_union;
  return true;
}

bool
gc_job_manager_impl::submit_job(gc_job_desc *jd)
{
  // Ensure it's one of our job descriptors

  if (jd < d_jd || jd >= &d_jd[d_options.max_jobs]){
    jd->status = JS_BAD_JOB_DESC;
    return false;
  }

  // Ensure we've got a client_thread_info assigned to this thread.
  
  gc_client_thread_info *cti =
    (gc_client_thread_info *) pthread_getspecific(s_client_key);
  if (unlikely(cti == 0)){
    if ((cti = alloc_cti()) == 0){
      fprintf(stderr, "gc_job_manager_impl::submit_job: Too many client threads.\n");
      jd->status = JS_TOO_MANY_CLIENTS;
      return false;
    }
    int r = pthread_setspecific(s_client_key, cti);
    if (r != 0){
      jd->status = JS_BAD_JUJU;
      fprintf(stderr, "pthread_setspecific failed (return = %d)\n", r);
      return false;
    }
  }

  if (jd->proc_id == GCP_UNKNOWN_PROC){
    jd->status = JS_UNKNOWN_PROC;
    return false;
  }

  if (!check_direct_args(jd, &jd->input))
    return false;

  if (!check_direct_args(jd, &jd->output))
    return false;

  if (!check_ea_args(jd, &jd->eaa))
    return false;

  jd->status = JS_OK;
  jd->sys.client_id = cti->d_client_id;

  if (!incr_njobs_active()){
    jd->status = JS_SHUTTING_DOWN;
    return false;
  }
  
  gc_jd_queue_enqueue(d_queue, jd);
  // tell_spes_to_check_queue();
  return true;
}

bool
gc_job_manager_impl::wait_job(gc_job_desc *jd)
{
  bool done;
  return wait_jobs(1, &jd, &done, GC_WAIT_ANY) == 1 && jd->status == JS_OK;
}

int
gc_job_manager_impl::wait_jobs(unsigned int njobs,
			       gc_job_desc *jd[],
			       bool done[],
			       gc_wait_mode mode)
{
  unsigned int i;

  gc_client_thread_info *cti =
    (gc_client_thread_info *) pthread_getspecific(s_client_key);
  if (unlikely(cti == 0))
    return -1;

  for (i = 0; i < njobs; i++){
    done[i] = false;
    if (unlikely(jd[i]->sys.client_id != cti->d_client_id)){
      fprintf(stderr, "gc_job_manager_impl::wait_jobs: can't wait for a job you didn't submit\n");
      return -1;
    }
  }

  {
    scoped_lock	l(cti->d_mutex);

    // setup info for event handler
    cti->d_state = (mode == GC_WAIT_ANY) ? CT_WAIT_ANY : CT_WAIT_ALL;
    cti->d_njobs_waiting_for = njobs;
    cti->d_jobs_waiting_for = jd;
    assert(cti->d_jobs_done != 0);

    unsigned int ndone = 0;

    // wait for jobs to complete
    
    while (1){
      ndone = 0;
      for (i= 0; i < njobs; i++){
	if (done[i])
	  ndone++;
	else if (bv_isset(cti->d_jobs_done, jd[i]->sys.job_id)){
	  bv_clr(cti->d_jobs_done, jd[i]->sys.job_id);
	  done[i] = true;
	  ndone++;
	}
      }

      if (mode == GC_WAIT_ANY && ndone > 0)
	break;

      if (mode == GC_WAIT_ALL && ndone == njobs)
	break;

      // FIXME what happens when somebody calls shutdown?

      cti->d_cond.wait(l);	// wait for event handler to wake us up
    }

    cti->d_state = CT_NOT_WAITING;  
    cti->d_njobs_waiting_for = 0;	// tidy up (not reqd)
    cti->d_jobs_waiting_for = 0;	// tidy up (not reqd)
    return ndone;
  }
}

////////////////////////////////////////////////////////////////////////

bool
gc_job_manager_impl::send_all_spes(uint32_t msg)
{
  bool ok = true;

  for (unsigned int i = 0; i < d_options.nspes; i++)
    ok &= send_spe(i, msg);

  return ok;
}

bool
gc_job_manager_impl::send_spe(unsigned int spe, uint32_t msg)
{
  if (spe >= d_options.nspes)
    return false;

  int r = spe_in_mbox_write(d_worker[spe].spe_ctx, &msg, 1,
			    SPE_MBOX_ALL_BLOCKING);
  if (r < 0){
    perror("spe_in_mbox_write");
    return false;
  }

  return r == 1;
}

void 
gc_job_manager_impl::tell_spes_to_check_queue()
{
  int nspes = d_options.nspes;

  for (int i = 0, ntold = 0; ntold < d_ntell && i < nspes ; ++i){
    volatile spe_spu_control_area_t *spe_ctrl = d_worker[d_tell_start].spe_ctrl;
    int nfree = (spe_ctrl->SPU_Mbox_Stat >> 8) & 0xFF;
    if (nfree == 4){
      spe_ctrl->SPU_In_Mbox = MK_MBOX_MSG(OP_CHECK_QUEUE, 0);
      ntold++;
    }

    unsigned int t = d_tell_start + 1;
    if (t >= d_options.nspes)
      t = 0;
    d_tell_start = t;
  }
}


////////////////////////////////////////////////////////////////////////

static void
pthread_create_failure_msg(int r, const char *which)
{
  char buf[256];
  const char *s = 0;

  switch (r){
  case EAGAIN: s = "EAGAIN"; break;
  case EINVAL: s = "EINVAL"; break;
  case EPERM:  s = "EPERM";  break;
  default:
    snprintf(buf, sizeof(buf), "Unknown error %d", r);
    s = buf;
    break;
  }
  fprintf(stderr, "pthread_create[%s] failed: %s\n", which, s);
}


static bool
start_thread(pthread_t *thread,
	     void *(*start_routine)(void *),  void *arg,
	     const char *msg)
{
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

  // FIXME save sigprocmask
  // FIXME set sigprocmask

  int r = pthread_create(thread, &attr, start_routine, arg);
    
  // FIXME restore sigprocmask

  if (r != 0){
    pthread_create_failure_msg(r, msg);
    return false;
  }
  return true;
}


////////////////////////////////////////////////////////////////////////

static void *start_worker(void *arg);

static void *
start_event_handler(void *arg)
{
  gc_job_manager_impl *p = (gc_job_manager_impl *) arg;
  p->event_handler_loop();
  return 0;
}

static void *
start_job_completer(void *arg)
{
  gc_job_manager_impl *p = (gc_job_manager_impl *) arg;
  p->job_completer_loop();
  return 0;
}

void
gc_job_manager_impl::create_event_handler()
{
  // create the SPE event handler and register our interest in events

  d_spe_event_handler.ptr = spe_event_handler_create();
  if (d_spe_event_handler.ptr == 0){
    perror("spe_event_handler_create");
    throw std::runtime_error("spe_event_handler_create");
  }

  for (unsigned int i = 0; i < d_options.nspes; i++){
    spe_event_unit_t	eu;
    memset(&eu, 0, sizeof(eu));
    eu.events = SPE_EVENT_OUT_INTR_MBOX | SPE_EVENT_SPE_STOPPED;
    eu.spe = d_worker[i].spe_ctx;
    eu.data.u32 = i;	// set in events returned by spe_event_wait

    if (spe_event_handler_register(d_spe_event_handler.ptr, &eu) != 0){
      perror("spe_event_handler_register");
      throw std::runtime_error("spe_event_handler_register");
    }
  }

  // create the event handling thread

  if (!start_thread(&d_eh_thread, start_event_handler, this, "event_handler")){
    throw std::runtime_error("pthread_create");
  }

  // create the job completion thread

  if (!start_thread(&d_jc_thread, start_job_completer, this, "job_completer")){
    throw std::runtime_error("pthread_create");
  }

  // create the SPE worker threads

  bool ok = true;
  for (unsigned int i = 0; ok && i < d_options.nspes; i++){
    char name[256];
    snprintf(name, sizeof(name), "worker[%d]", i);
    ok &= start_thread(&d_worker[i].thread, start_worker,
		       &d_worker[i], name);
  }

  if (!ok){
    //
    // FIXME Clean up the mess.  Need to terminate event handler and all workers.
    //
    // this should cause the workers to exit, unless they're seriously broken
    send_all_spes(MK_MBOX_MSG(OP_EXIT, 0));

    shutdown();

    throw std::runtime_error("pthread_create");
  }
}

////////////////////////////////////////////////////////////////////////

void
gc_job_manager_impl::set_eh_state(evt_handler_state s)
{
  scoped_lock	l(d_eh_mutex);
  d_eh_state = s;
  d_eh_cond.notify_all();
}

void
gc_job_manager_impl::set_ea_args_maxsize(int maxsize)
{
  scoped_lock	l(d_eh_mutex);
  d_ea_args_maxsize = maxsize;
  d_eh_cond.notify_all();
}

void
gc_job_manager_impl::print_event(spe_event_unit_t *evt)
{
  printf("evt: spe = %d events = (0x%x)", evt->data.u32, evt->events);

  if (evt->events & SPE_EVENT_OUT_INTR_MBOX)
    printf(" OUT_INTR_MBOX");
  
  if (evt->events & SPE_EVENT_IN_MBOX)
    printf(" IN_MBOX");
  
  if (evt->events & SPE_EVENT_TAG_GROUP)
    printf(" TAG_GROUP");
  
  if (evt->events & SPE_EVENT_SPE_STOPPED)
    printf(" SPE_STOPPED");

  printf("\n");
}

struct job_client_info {
  uint16_t	job_id;
  uint16_t	client_id;
};

static int
compare_jci_clients(const void *va, const void *vb)
{
  const job_client_info *a = (job_client_info *) va;
  const job_client_info *b = (job_client_info *) vb;

  return a->client_id - b->client_id;
}

void
gc_job_manager_impl::notify_clients_jobs_are_done(unsigned int spe_num,
						  unsigned int completion_info_idx)
{
  const char *msg = "gc_job_manager_impl::notify_client_job_is_done (INTERNAL ERROR)";

  smp_rmb();  // order reads so we know that data sent from SPE is here

  gc_comp_info_t *ci = &d_comp_info[2 * spe_num + (completion_info_idx & 0x1)];

  if (ci->ncomplete == 0){	// never happens, but ensures code below is correct
    ci->in_use = 0;
    return;
  }

  decr_njobs_active(ci->ncomplete);

  if (0){
    static int total_jobs;
    static int total_msgs;
    total_msgs++;
    total_jobs += ci->ncomplete;
    printf("ppe:     tj = %6d  tm = %6d\n", total_jobs, total_msgs);
  }

  job_client_info gci[GC_CI_NJOBS];

  /*
   * Make one pass through and sanity check everything while filling in gci
   */
  for (unsigned int i = 0; i < ci->ncomplete; i++){
    unsigned int job_id = ci->job_id[i];

    if (job_id >= d_options.max_jobs){
      // internal error, shouldn't happen
      fprintf(stderr,"%s: invalid job_id = %d\n", msg, job_id);
      ci->in_use = 0;		// clear flag so SPE knows we're done with it
      return;
    }
    gc_job_desc *jd = &d_jd[job_id];

    if (jd->sys.client_id >= d_options.max_client_threads){
      // internal error, shouldn't happen
      fprintf(stderr, "%s: invalid client_id = %d\n", msg, jd->sys.client_id);
      ci->in_use = 0;		// clear flag so SPE knows we're done with it
      return;
    }

    gci[i].job_id = job_id;
    gci[i].client_id = jd->sys.client_id;
  }

  // sort by client_id so we only have to lock & signal once / client

  if (ci->ncomplete > 1)
    qsort(gci, ci->ncomplete, sizeof(gci[0]), compare_jci_clients);

  // "wind-in" 

  gc_client_thread_info *last_cti = &d_client_thread[gci[0].client_id];
  last_cti->d_mutex.lock();
  bv_set(last_cti->d_jobs_done, gci[0].job_id);  // mark job done

  for (unsigned int i = 1; i < ci->ncomplete; i++){

    gc_client_thread_info *cti = &d_client_thread[gci[i].client_id];

    if (cti != last_cti){	// new client?

      // yes.  signal old client, unlock old, lock new

      // FIXME we could distinguish between CT_WAIT_ALL & CT_WAIT_ANY

      if (last_cti->d_state == CT_WAIT_ANY || last_cti->d_state == CT_WAIT_ALL)
	last_cti->d_cond.notify_one();	// wake client thread up

      last_cti->d_mutex.unlock();
      cti->d_mutex.lock();
      last_cti = cti;
    }

    // mark job done
    bv_set(cti->d_jobs_done, gci[i].job_id);
  }

  // "wind-out"

  if (last_cti->d_state == CT_WAIT_ANY || last_cti->d_state == CT_WAIT_ALL)
    last_cti->d_cond.notify_one();	// wake client thread up
  last_cti->d_mutex.unlock();

  ci->in_use = 0;		// clear flag so SPE knows we're done with it
}

void
gc_job_manager_impl::handle_event(spe_event_unit_t *evt)
{
  // print_event(evt);

  int spe_num = evt->data.u32;

  // only a single event type can be signaled at a time
  
  if (evt->events == SPE_EVENT_OUT_INTR_MBOX) { // SPE sent us 1 or more msgs
    static const int NMSGS = 32;
    unsigned int msg[NMSGS];
    int n = spe_out_intr_mbox_read(evt->spe, msg, NMSGS, SPE_MBOX_ANY_BLOCKING);
    // printf("spe_out_intr_mbox_read = %d\n", n);
    if (n < 0){
      perror("spe_out_intr_mbox_read");
    }
    else {
      for (int i = 0; i < n; i++){
	switch(MBOX_MSG_OP(msg[i])){
#if 0
	case OP_JOBS_DONE:
	  if (debug())
	    printf("eh: job_done (0x%08x) from spu[%d]\n", msg[i], spe_num);
	  notify_clients_jobs_are_done(spe_num, MBOX_MSG_ARG(msg[i]));
	  break;
#endif
	case OP_SPU_BUFSIZE:
	  set_ea_args_maxsize(MBOX_MSG_ARG(msg[i]));
	  break;

	case OP_EXIT:
	default:
	  printf("eh: Unexpected msg (0x%08x) from spu[%d]\n", msg[i], spe_num);
	  break;
	}
      }
    }
  }
  else if (evt->events == SPE_EVENT_SPE_STOPPED){ // the SPE stopped
    spe_stop_info_t si;
    int r = spe_stop_info_read(evt->spe, &si);
    if (r < 0){
      perror("spe_stop_info_read");
    }
    else {
      switch (si.stop_reason){
      case SPE_EXIT:
	if (debug()){
	  printf("eh: spu[%d] SPE_EXIT w/ exit_code = %d\n",
		 spe_num, si.result.spe_exit_code);
	}
	break;
      case SPE_STOP_AND_SIGNAL:
	printf("eh: spu[%d] SPE_STOP_AND_SIGNAL w/ spe_signal_code = 0x%x\n",
	       spe_num, si.result.spe_signal_code);
	break;
      case SPE_RUNTIME_ERROR:
	printf("eh: spu[%d] SPE_RUNTIME_ERROR w/ spe_runtime_error = 0x%x\n",
	       spe_num, si.result.spe_runtime_error);
	break;
      case SPE_RUNTIME_EXCEPTION:
	printf("eh: spu[%d] SPE_RUNTIME_EXCEPTION w/ spe_runtime_exception = 0x%x\n",
	       spe_num, si.result.spe_runtime_exception);
	break;
      case SPE_RUNTIME_FATAL:
	printf("eh: spu[%d] SPE_RUNTIME_FATAL w/ spe_runtime_fatal = 0x%x\n",
	       spe_num, si.result.spe_runtime_fatal);
	break;
      case SPE_CALLBACK_ERROR:
	printf("eh: spu[%d] SPE_CALLBACK_ERROR w/ spe_callback_error = 0x%x\n",
	       spe_num, si.result.spe_callback_error);
	break;
      case SPE_ISOLATION_ERROR:
	printf("eh: spu[%d] SPE_ISOLATION_ERROR w/ spe_isolation_error = 0x%x\n",
	       spe_num, si.result.spe_isolation_error);
	break;
      default:
	printf("eh: spu[%d] UNKNOWN STOP REASON (%d) w/ spu_status = 0x%x\n",
	       spe_num, si.stop_reason, si.spu_status);
	break;
      }
    }
  }
#if 0 // not enabled
  else if (evt->events == SPE_EVENT_IN_MBOX){	 // there's room to write to SPE
    // spe_in_mbox_write (ignore)
  }
  else if (evt->events == SPE_EVENT_TAG_GROUP){	 // our DMA completed
    // spe_mfcio_tag_status_read
  }
#endif
  else {
    fprintf(stderr, "handle_event: unexpected evt->events = 0x%x\n", evt->events);
    return;
  }
}

//
// This is the "main program" of the event handling thread
//
void
gc_job_manager_impl::event_handler_loop()
{
  static const int MAX_EVENTS = 16;
  static const int TIMEOUT = 20;	// how long to block in milliseconds

  spe_event_unit_t events[MAX_EVENTS];

  if (d_debug)
    printf("event_handler_loop: starting\n");

  set_eh_state(EHS_RUNNING);

  // ask the first spe for its max bufsize
  send_spe(0, MK_MBOX_MSG(OP_GET_SPU_BUFSIZE, 0));

  while (1){
    switch(d_eh_state){

    case EHS_RUNNING:      		// normal stuff
      if (d_shutdown_requested) {
	set_eh_state(EHS_SHUTTING_DOWN);
      }
      break;

    case EHS_SHUTTING_DOWN:
      if (d_jc_state == JCS_DEAD){
	send_all_spes(MK_MBOX_MSG(OP_EXIT, 0));
	set_eh_state(EHS_WAITING_FOR_WORKERS_TO_DIE);
      }
      break;

    case EHS_WAITING_FOR_WORKERS_TO_DIE:
      {
	bool all_dead = true;
	for (unsigned int i = 0; i < d_options.nspes; i++)
	  all_dead &= d_worker[i].state == WS_DEAD;

	if (all_dead){
	  set_eh_state(EHS_DEAD);
	  if (d_debug)
	    printf("event_handler_loop: exiting\n");
	  return;
	}
      }
      break;

    default:
      set_eh_state(EHS_DEAD);
      printf("event_handler_loop(default): exiting\n");
      return;
    }

    // block waiting for events...
    int nevents = spe_event_wait(d_spe_event_handler.ptr,
				 events, MAX_EVENTS, TIMEOUT);
    if (nevents < 0){
      perror("spe_wait_event");
      // FIXME bail?
    }
    for (int i = 0; i < nevents; i++){
      handle_event(&events[i]);
    }
  }
}

////////////////////////////////////////////////////////////////////////

void
gc_job_manager_impl::poll_for_job_completion()
{
  static const int niter = 10000;

  CCTPL();		// change current (h/w) thread priority to low

  for (int n = 0; n < niter; n++){

    for (unsigned int spe_num = 0; spe_num < d_options.nspes; spe_num++){
      volatile spe_spu_control_area_t *spe_ctrl = d_worker[spe_num].spe_ctrl;
      int nentries = spe_ctrl->SPU_Mbox_Stat & 0xFF;
      while (nentries-- > 0){
	unsigned int msg = spe_ctrl->SPU_Out_Mbox;
	switch(MBOX_MSG_OP(msg)){
	case OP_JOBS_DONE:
	  if (debug())
	    printf("jc: job_done (0x%08x) from spu[%d]\n", msg, spe_num);

	  CCTPM();		// change current thread priority to medium
	  notify_clients_jobs_are_done(spe_num, MBOX_MSG_ARG(msg));
	  CCTPL();
	  break;

	default:
	  printf("jc: Unexpected msg (0x%08x) from spu[%d]\n", msg, spe_num);
	  break;
	}
      }
    }
  }
  CCTPM();
}

//
// This is the "main program" of the job completer thread
//
void
gc_job_manager_impl::job_completer_loop()
{
  d_jc_state = JCS_RUNNING;

  while (1){
    {
      scoped_lock l(d_jc_mutex);
      if (d_jc_njobs_active == 0){
	if (d_shutdown_requested){
	  d_jc_state = JCS_DEAD;
	  return;
	}
	d_jc_cond.wait(l);
      }
    }

    poll_for_job_completion();
  }
}

////////////////////////////////////////////////////////////////////////
// this is the top of the SPE worker threads

static void *
start_worker(void *arg)
{
  worker_ctx *w = (worker_ctx *) arg;
  spe_stop_info_t	si;

  w->state = WS_RUNNING;
  if (s_worker_debug)
    printf("worker[%d]: WS_RUNNING\n", w->spe_idx);

  unsigned int entry = SPE_DEFAULT_ENTRY;
  int r = spe_context_run(w->spe_ctx,  &entry, 0, w->spu_args, 0, &si);

  if (r < 0){			// error
    char buf[64];
    snprintf(buf, sizeof(buf), "worker[%d]: spe_context_run", w->spe_idx);
    perror(buf);
  }
  else if (r == 0){
    // spe program called exit.
    if (s_worker_debug)
      printf("worker[%d]: SPE_EXIT w/ exit_code = %d\n",
	     w->spe_idx, si.result.spe_exit_code);
  }
  else {
    // called stop_and_signal
    //
    // I'm not sure we'll ever get here.  I think the event
    // handler will catch this...
    printf("worker[%d]: SPE_STOP_AND_SIGNAL w/ spe_signal_code = 0x%x\n",
	   w->spe_idx, si.result.spe_signal_code);
  }

  // in any event, we're committing suicide now ;)
  if (s_worker_debug)
    printf("worker[%d]: WS_DEAD\n", w->spe_idx);

  w->state = WS_DEAD;
  return 0;
}

////////////////////////////////////////////////////////////////////////

gc_client_thread_info *
gc_job_manager_impl::alloc_cti()
{
  for (unsigned int i = 0; i < d_options.max_client_threads; i++){
    if (d_client_thread[i].d_free){
      // try to atomically grab it
      if (_atomic_dec_if_positive(ptr_to_ea(&d_client_thread[i].d_free)) == 0){
	// got it...
	gc_client_thread_info *cti = &d_client_thread[i];
	cti->d_state = CT_NOT_WAITING;
	bv_zero(cti->d_jobs_done);
	cti->d_njobs_waiting_for = 0;
	cti->d_jobs_waiting_for = 0;
	
	return cti;
      }
    }
  }
  return 0;
}

void
gc_job_manager_impl::free_cti(gc_client_thread_info *cti)
{
  assert((size_t) (cti - d_client_thread.get()) < d_options.max_client_threads);
  cti->d_free = 1;
}

int
gc_job_manager_impl::ea_args_maxsize()
{
  scoped_lock	l(d_eh_mutex);

  while (d_ea_args_maxsize == 0)	// wait for it to be initialized
    d_eh_cond.wait(l);

  return d_ea_args_maxsize;
}

void
gc_job_manager_impl::set_debug(int debug)
{
  d_debug = debug;
  s_worker_debug = debug;
}

int
gc_job_manager_impl::debug()
{
  return d_debug;
}

////////////////////////////////////////////////////////////////////////

void
gc_job_manager_impl::setup_logfiles()
{
  if (!d_options.enable_logging)
    return;

  if (d_options.log2_nlog_entries == 0)
    d_options.log2_nlog_entries = 12;

  // must end up a multiple of the page size

  size_t pagesize = getpagesize();
  size_t s = (1 << d_options.log2_nlog_entries) * sizeof(gc_log_entry_t);
  s = ((s + pagesize - 1) / pagesize) * pagesize;
  size_t nentries = s / sizeof(gc_log_entry_t);
  assert(is_power_of_2(nentries));

  for (unsigned int i = 0; i < d_options.nspes; i++){
    char filename[100];
    snprintf(filename, sizeof(filename), "spu_log.%02d", i);
    int fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0664);
    if (fd == -1){
      perror(filename);
      return;
    }
    lseek(fd, s - 1, SEEK_SET);
    write(fd, "\0", 1);
    void *p = mmap(0, s, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED){
      perror("gc_job_manager_impl::setup_logfiles: mmap");
      close(fd);
      return;
    }
    close(fd);
    memset(p, 0, s);
    d_spu_args[i].log.base = ptr_to_ea(p);
    d_spu_args[i].log.nentries = nentries;
  }
}

void
gc_job_manager_impl::sync_logfiles()
{
  for (unsigned int i = 0; i < d_options.nspes; i++){
    if (d_spu_args[i].log.base)
      msync(ea_to_ptr(d_spu_args[i].log.base),
	    d_spu_args[i].log.nentries * sizeof(gc_log_entry_t),
	    MS_ASYNC);
  }
}

void
gc_job_manager_impl::unmap_logfiles()
{
  for (unsigned int i = 0; i < d_options.nspes; i++){
    if (d_spu_args[i].log.base)
      munmap(ea_to_ptr(d_spu_args[i].log.base),
	     d_spu_args[i].log.nentries * sizeof(gc_log_entry_t));
  }
}

////////////////////////////////////////////////////////////////////////
//
// lookup proc names in d_proc_def table

gc_proc_id_t 
gc_job_manager_impl::lookup_proc(const std::string &proc_name)
{
  for (int i = 0; i < d_nproc_defs; i++)
    if (proc_name == d_proc_def[i].name)
      return i;

  throw gc_unknown_proc(proc_name);
}

std::vector<std::string>
gc_job_manager_impl::proc_names()
{
  std::vector<std::string> r;
  for (int i = 0; i < d_nproc_defs; i++)
    r.push_back(d_proc_def[i].name);

  return r;
}

////////////////////////////////////////////////////////////////////////

worker_ctx::~worker_ctx()
{
  if (spe_ctx){
    int r = spe_context_destroy(spe_ctx);
    if (r != 0){
      perror("spe_context_destroy");
    }
    spe_ctx = 0;
  }
  state = WS_FREE;
}