3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <gc_job_manager_impl.h>
27 #include <gc_proc_def_utils.h>
32 #include <atomic_dec_if_positive.h>
33 #include <memory_barrier.h>
36 #include <sys/types.h>
41 static const size_t CACHE_LINE_SIZE = 128;
43 static const unsigned int DEFAULT_MAX_JOBS = 128;
44 static const unsigned int DEFAULT_MAX_CLIENT_THREADS = 64;
46 // FIXME this really depends on the SPU code...
47 static const unsigned int MAX_TOTAL_INDIRECT_LENGTH = 16 * 1024;
50 static bool s_key_initialized = false;
51 static pthread_key_t s_client_key;
53 static int s_worker_debug = 0;
55 // custom deleter of gang_contexts for use with boost::shared_ptr
58 void operator()(spe_gang_context_ptr_t ctx) {
60 int r = spe_gang_context_destroy(ctx);
62 perror("spe_gang_context_destroy");
69 class spe_program_handle_deleter {
71 void operator()(spe_program_handle_t *program) {
73 int r = spe_image_close(program);
75 perror("spe_image_close");
82 // custom deleter of anything that can be freed with "free"
85 void operator()(void *p) {
92 * Called when client thread is destroyed.
93 * We mark our client info free.
96 client_key_destructor(void *p)
98 ((gc_client_thread_info *) p)->d_free = 1;
102 * Return pointer to cache-aligned chunk of storage of size size bytes.
103 * Throw if can't allocate memory. The storage should be freed
104 * with "free" when done. The memory is initialized to zero.
107 aligned_alloc(size_t size, size_t alignment = CACHE_LINE_SIZE)
110 if (posix_memalign(&p, alignment, size) != 0){
111 perror("posix_memalign");
112 throw std::runtime_error("memory");
114 memset(p, 0, size); // zero the memory
119 is_power_of_2(uint32_t x)
121 return (x != 0) && !(x & (x - 1));
124 ////////////////////////////////////////////////////////////////////////
127 gc_job_manager_impl::gc_job_manager_impl(const gc_jm_options *options)
128 : d_debug(0), d_spu_args(0),
129 d_eh_cond(&d_eh_mutex), d_eh_thread(0), d_eh_state(EHS_INIT),
130 d_shutdown_requested(false),
131 d_client_thread(0), d_ea_args_maxsize(0),
132 d_proc_def(0), d_proc_def_ls_addr(0), d_nproc_defs(0)
134 if (!s_key_initialized){
135 int r = pthread_key_create(&s_client_key, client_key_destructor);
137 throw std::runtime_error("pthread_key_create");
138 s_key_initialized = true;
142 pthread_setspecific(s_client_key, 0);
145 d_options = *options;
147 // provide the real default for those indicated with a zero
148 if (d_options.max_jobs == 0)
149 d_options.max_jobs = DEFAULT_MAX_JOBS;
150 if (d_options.max_client_threads == 0)
151 d_options.max_client_threads = DEFAULT_MAX_CLIENT_THREADS;
153 if (d_options.program_handle == 0){
154 fprintf(stderr, "gc_job_manager: options->program_handle must be non-zero\n");
155 throw std::runtime_error("gc_job_manager: options->program_handle must be non-zero");
158 int ncpu_nodes = spe_cpu_info_get(SPE_COUNT_PHYSICAL_CPU_NODES, -1);
159 int nusable_spes = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);
162 printf("cpu_nodes = %d\n", ncpu_nodes);
163 for (int i = 0; i < ncpu_nodes; i++){
164 printf("node[%d].physical_spes = %2d\n", i,
165 spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, i));
166 printf("node[%d].usable_spes = %2d\n", i,
167 spe_cpu_info_get(SPE_COUNT_USABLE_SPES, i));
172 d_options.nspes = std::min(d_options.nspes, (unsigned int) MAX_SPES);
173 nusable_spes = std::min(nusable_spes, (int) MAX_SPES);
176 // sanity check requested number of spes.
178 if (d_options.nspes == 0) // use all of them
179 d_options.nspes = nusable_spes;
181 if (d_options.nspes > (unsigned int) nusable_spes){
183 "gc_job_manager: warning: caller requested %d spes. There are only %d available.\n",
184 d_options.nspes, nusable_spes);
185 if (d_options.gang_schedule){
186 // If we're gang scheduling we'll never get scheduled if we
187 // ask for more than are available.
188 throw std::out_of_range("gang_scheduling: not enough spes available");
190 else { // FIXME clamp to usable. problem on PS3 when overcommited
191 fprintf(stderr, "gc_job_manager: clamping nspes to %d\n", nusable_spes);
192 d_options.nspes = nusable_spes;
197 if (d_options.use_affinity){
198 printf("gc_job_manager: warning: affinity request was ignored\n");
201 if (d_options.gang_schedule){
202 d_gang = spe_gang_context_sptr(spe_gang_context_create(0), gang_deleter());
204 perror("gc_job_manager_impl[spe_gang_context_create]");
205 throw std::runtime_error("spe_gang_context_create");
209 // ----------------------------------------------------------------
210 // initalize the job queue
212 d_queue = (gc_jd_queue_t *) aligned_alloc(sizeof(gc_jd_queue_t));
214 boost::shared_ptr<void>((void *) d_queue, free_deleter());
215 gc_jd_queue_init(d_queue);
218 // ----------------------------------------------------------------
219 // create the spe contexts
221 // 1 spu_arg struct for each SPE
222 assert(sizeof(gc_spu_args_t) % 16 == 0);
224 (gc_spu_args_t *) aligned_alloc(MAX_SPES * sizeof(gc_spu_args_t), 16);
226 boost::shared_ptr<void>((void *) d_spu_args, free_deleter());
228 // 2 completion info structs for each SPE (we double buffer them)
229 assert(sizeof(gc_comp_info_t) % CACHE_LINE_SIZE == 0);
231 (gc_comp_info_t *) aligned_alloc(2 * MAX_SPES * sizeof(gc_comp_info_t),
234 boost::shared_ptr<void>((void *) d_comp_info, free_deleter());
237 // get a handle to the spe program
239 spe_program_handle_t *spe_image = d_options.program_handle;
241 // fish proc_def table out of SPE ELF file
243 if (!gcpd_find_table(spe_image, &d_proc_def, &d_nproc_defs, &d_proc_def_ls_addr)){
244 fprintf(stderr, "gc_job_manager_impl: couldn't find gc_proc_defs in SPE ELF file.\n");
245 throw std::runtime_error("no gc_proc_defs");
247 // fprintf(stderr, "d_proc_def_ls_addr = 0x%0x\n", d_proc_def_ls_addr);
249 int spe_flags = (SPE_EVENTS_ENABLE
250 | SPE_CFG_SIGNOTIFY1_OR
251 | SPE_CFG_SIGNOTIFY2_OR);
253 for (unsigned int i = 0; i < d_options.nspes; i++){
254 // FIXME affinity stuff goes here
255 d_worker[i].spe_ctx = spe_context_create(spe_flags, d_gang.get());;
256 if (d_worker[i].spe_ctx == 0){
257 perror("spe_context_create");
258 throw std::runtime_error("spe_context_create");
260 d_worker[i].spe_idx = i;
261 d_worker[i].spu_args = &d_spu_args[i];
262 d_worker[i].spu_args->queue = ptr_to_ea(d_queue);
263 d_worker[i].spu_args->comp_info[0] = ptr_to_ea(&d_comp_info[2*i+0]);
264 d_worker[i].spu_args->comp_info[1] = ptr_to_ea(&d_comp_info[2*i+1]);
265 d_worker[i].spu_args->spu_idx = i;
266 d_worker[i].spu_args->nspus = d_options.nspes;
267 d_worker[i].spu_args->proc_def_ls_addr = d_proc_def_ls_addr;
268 d_worker[i].spu_args->nproc_defs = d_nproc_defs;
269 d_worker[i].spu_args->log.base = 0;
270 d_worker[i].spu_args->log.nentries = 0;
271 d_worker[i].state = WS_INIT;
273 int r = spe_program_load(d_worker[i].spe_ctx, spe_image);
275 perror("spe_program_load");
276 throw std::runtime_error("spe_program_load");
282 // ----------------------------------------------------------------
283 // initalize the free list of job descriptors
285 d_free_list = (gc_jd_stack_t *) aligned_alloc(sizeof(gc_jd_stack_t));
286 // This ensures that the memory associated with d_free_list is
287 // automatically freed in the destructor or if an exception occurs
288 // here in the constructor.
290 boost::shared_ptr<void>((void *) d_free_list, free_deleter());
291 gc_jd_stack_init(d_free_list);
294 printf("sizeof(d_jd[0]) = %d (0x%x)\n", sizeof(d_jd[0]), sizeof(d_jd[0]));
295 printf("max_jobs = %u\n", d_options.max_jobs);
298 // Initialize the array of job descriptors.
299 d_jd = (gc_job_desc_t *) aligned_alloc(sizeof(d_jd[0]) * d_options.max_jobs);
300 _d_jd_boost = boost::shared_ptr<void>((void *) d_jd, free_deleter());
304 for (int i = 0; i < (int) d_options.max_jobs; i++)
305 d_jd[i].sys.job_id = i;
307 // push them onto the free list
308 for (int i = d_options.max_jobs - 1; i >= 0; i--)
309 free_job_desc(&d_jd[i]);
311 // ----------------------------------------------------------------
312 // initialize d_client_thread
315 gc_client_thread_info_sa cti(
316 new gc_client_thread_info[d_options.max_client_threads]);
318 d_client_thread.swap(cti);
320 for (unsigned int i = 0; i < d_options.max_client_threads; i++)
321 d_client_thread[i].d_client_id = i;
324 // ----------------------------------------------------------------
325 // initialize bitvectors
327 // initialize d_bvlen, the number of longs in job related bitvectors.
328 int bits_per_long = sizeof(unsigned long) * 8;
329 d_bvlen = (d_options.max_jobs + bits_per_long - 1) / bits_per_long;
331 // allocate all bitvectors in a single cache-aligned chunk
332 size_t nlongs = d_bvlen * d_options.max_client_threads;
333 void *p = aligned_alloc(nlongs * sizeof(unsigned long));
334 _d_all_bitvectors = boost::shared_ptr<void>(p, free_deleter());
336 // Now point the gc_client_thread_info bitvectors into this storage
337 unsigned long *v = (unsigned long *) p;
339 for (unsigned int i = 0; i < d_options.max_client_threads; i++, v += d_bvlen)
340 d_client_thread[i].d_jobs_done = v;
343 // ----------------------------------------------------------------
344 // create the spe event handler & worker (SPE) threads
346 create_event_handler();
350 ////////////////////////////////////////////////////////////////////////
352 gc_job_manager_impl::~gc_job_manager_impl()
356 d_jd = 0; // handled via _d_jd_boost
357 d_free_list = 0; // handled via _d_free_list_boost
358 d_queue = 0; // handled via _d_queue_boost
360 // clear cti, since we've deleted the underlying data
361 pthread_setspecific(s_client_key, 0);
367 gc_job_manager_impl::shutdown()
369 omni_mutex_lock l(d_eh_mutex);
371 d_shutdown_requested = true; // set flag for event handler thread
373 // should only happens during early QA code
374 if (d_eh_thread == 0 && d_eh_state == EHS_INIT)
377 while (d_eh_state != EHS_DEAD) // wait for it to finish
384 gc_job_manager_impl::nspes() const
386 return d_options.nspes;
389 ////////////////////////////////////////////////////////////////////////
392 gc_job_manager_impl::bv_zero(unsigned long *bv)
394 memset(bv, 0, sizeof(unsigned long) * d_bvlen);
398 gc_job_manager_impl::bv_clr(unsigned long *bv, unsigned int bitno)
400 unsigned int wi = bitno / (sizeof (unsigned long) * 8);
401 unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
402 bv[wi] &= ~(1UL << bi);
406 gc_job_manager_impl::bv_set(unsigned long *bv, unsigned int bitno)
408 unsigned int wi = bitno / (sizeof (unsigned long) * 8);
409 unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
410 bv[wi] |= (1UL << bi);
414 gc_job_manager_impl::bv_isset(unsigned long *bv, unsigned int bitno)
416 unsigned int wi = bitno / (sizeof (unsigned long) * 8);
417 unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
418 return (bv[wi] & (1UL << bi)) != 0;
422 gc_job_manager_impl::bv_isclr(unsigned long *bv, unsigned int bitno)
424 unsigned int wi = bitno / (sizeof (unsigned long) * 8);
425 unsigned int bi = bitno & ((sizeof (unsigned long) * 8) - 1);
426 return (bv[wi] & (1UL << bi)) == 0;
429 ////////////////////////////////////////////////////////////////////////
432 gc_job_manager_impl::alloc_job_desc()
434 // stack is lock free, thus safe to call from any thread
435 return gc_jd_stack_pop(d_free_list);
439 gc_job_manager_impl::free_job_desc(gc_job_desc *jd)
441 // stack is lock free, thus safe to call from any thread
443 gc_jd_stack_push(d_free_list, jd);
446 ////////////////////////////////////////////////////////////////////////
449 * We check as much as we can here on the PPE side, so that the SPE
453 check_direct_args(gc_job_desc *jd, gc_job_direct_args *args)
455 if (args->nargs > MAX_ARGS_DIRECT){
456 jd->status = JS_BAD_N_DIRECT;
464 check_ea_args(gc_job_desc *jd, gc_job_ea_args *p)
466 if (p->nargs > MAX_ARGS_EA){
467 jd->status = JS_BAD_N_EA;
471 uint32_t dir_union = 0;
473 for (unsigned int i = 0; i < p->nargs; i++){
474 dir_union |= p->arg[i].direction;
475 switch(p->arg[i].direction){
481 jd->status = JS_BAD_DIRECTION;
487 unsigned int common_eah = (p->arg[0].ea_addr) >> 32;
488 for (unsigned int i = 1; i < p->nargs; i++){
489 if ((p->arg[i].ea_addr >> 32) != common_eah){
490 jd->status = JS_BAD_EAH;
496 jd->sys.direction_union = dir_union;
501 gc_job_manager_impl::submit_job(gc_job_desc *jd)
503 if (unlikely(d_shutdown_requested)){
504 jd->status = JS_SHUTTING_DOWN;
508 // Ensure it's one of our job descriptors
510 if (jd < d_jd || jd >= &d_jd[d_options.max_jobs]){
511 jd->status = JS_BAD_JOB_DESC;
515 // Ensure we've got a client_thread_info assigned to this thread.
517 gc_client_thread_info *cti =
518 (gc_client_thread_info *) pthread_getspecific(s_client_key);
519 if (unlikely(cti == 0)){
520 if ((cti = alloc_cti()) == 0){
521 fprintf(stderr, "gc_job_manager_impl::submit_job: Too many client threads.\n");
522 jd->status = JS_TOO_MANY_CLIENTS;
525 int r = pthread_setspecific(s_client_key, cti);
527 jd->status = JS_BAD_JUJU;
528 fprintf(stderr, "pthread_setspecific failed (return = %d)\n", r);
533 if (jd->proc_id == GCP_UNKNOWN_PROC){
534 jd->status = JS_UNKNOWN_PROC;
538 if (!check_direct_args(jd, &jd->input))
541 if (!check_direct_args(jd, &jd->output))
544 if (!check_ea_args(jd, &jd->eaa))
548 jd->sys.client_id = cti->d_client_id;
550 // FIXME keep count of jobs in progress?
552 gc_jd_queue_enqueue(d_queue, jd);
557 gc_job_manager_impl::wait_job(gc_job_desc *jd)
560 return wait_jobs(1, &jd, &done, GC_WAIT_ANY) == 1;
564 gc_job_manager_impl::wait_jobs(unsigned int njobs,
571 gc_client_thread_info *cti =
572 (gc_client_thread_info *) pthread_getspecific(s_client_key);
573 if (unlikely(cti == 0))
576 for (i = 0; i < njobs; i++){
578 if (unlikely(jd[i]->sys.client_id != cti->d_client_id)){
579 fprintf(stderr, "gc_job_manager_impl::wait_jobs: can't wait for a job you didn't submit\n");
585 omni_mutex_lock l(cti->d_mutex);
587 // setup info for event handler
588 cti->d_state = (mode == GC_WAIT_ANY) ? CT_WAIT_ANY : CT_WAIT_ALL;
589 cti->d_njobs_waiting_for = njobs;
590 cti->d_jobs_waiting_for = jd;
591 assert(cti->d_jobs_done != 0);
593 unsigned int ndone = 0;
595 // wait for jobs to complete
599 for (i= 0; i < njobs; i++){
602 else if (bv_isset(cti->d_jobs_done, jd[i]->sys.job_id)){
603 bv_clr(cti->d_jobs_done, jd[i]->sys.job_id);
609 if (mode == GC_WAIT_ANY && ndone > 0)
612 if (mode == GC_WAIT_ALL && ndone == njobs)
615 // FIXME what happens when somebody calls shutdown?
617 cti->d_cond.wait(); // wait for event handler to wake us up
620 cti->d_state = CT_NOT_WAITING;
621 cti->d_njobs_waiting_for = 0; // tidy up (not reqd)
622 cti->d_jobs_waiting_for = 0; // tidy up (not reqd)
627 ////////////////////////////////////////////////////////////////////////
630 gc_job_manager_impl::send_all_spes(uint32_t msg)
634 for (unsigned int i = 0; i < d_options.nspes; i++)
635 ok &= send_spe(i, msg);
641 gc_job_manager_impl::send_spe(unsigned int spe, uint32_t msg)
643 if (spe >= d_options.nspes)
646 int r = spe_in_mbox_write(d_worker[spe].spe_ctx, &msg, 1,
647 SPE_MBOX_ALL_BLOCKING);
649 perror("spe_in_mbox_write");
656 ////////////////////////////////////////////////////////////////////////
659 pthread_create_failure_msg(int r, const char *which)
665 case EAGAIN: s = "EAGAIN"; break;
666 case EINVAL: s = "EINVAL"; break;
667 case EPERM: s = "EPERM"; break;
669 snprintf(buf, sizeof(buf), "Unknown error %d", r);
673 fprintf(stderr, "pthread_create[%s] failed: %s\n", which, s);
678 start_thread(pthread_t *thread,
679 void *(*start_routine)(void *), void *arg,
683 pthread_attr_init(&attr);
684 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
686 // FIXME save sigprocmask
687 // FIXME set sigprocmask
689 int r = pthread_create(thread, &attr, start_routine, arg);
691 // FIXME restore sigprocmask
694 pthread_create_failure_msg(r, msg);
701 ////////////////////////////////////////////////////////////////////////
703 static void *start_worker(void *arg);
706 start_event_handler(void *arg)
708 gc_job_manager_impl *p = (gc_job_manager_impl *) arg;
709 p->event_handler_loop();
714 gc_job_manager_impl::create_event_handler()
716 // create the SPE event handler and register our interest in events
718 d_spe_event_handler.ptr = spe_event_handler_create();
719 if (d_spe_event_handler.ptr == 0){
720 perror("spe_event_handler_create");
721 throw std::runtime_error("spe_event_handler_create");
724 for (unsigned int i = 0; i < d_options.nspes; i++){
726 memset(&eu, 0, sizeof(eu));
727 eu.events = SPE_EVENT_OUT_INTR_MBOX | SPE_EVENT_SPE_STOPPED;
728 eu.spe = d_worker[i].spe_ctx;
729 eu.data.u32 = i; // set in events returned by spe_event_wait
731 if (spe_event_handler_register(d_spe_event_handler.ptr, &eu) != 0){
732 perror("spe_event_handler_register");
733 throw std::runtime_error("spe_event_handler_register");
737 // create our event handling thread
739 if (!start_thread(&d_eh_thread, start_event_handler, this, "event_handler")){
740 throw std::runtime_error("pthread_create");
743 // create the SPE worker threads
746 for (unsigned int i = 0; ok && i < d_options.nspes; i++){
748 snprintf(name, sizeof(name), "worker[%d]", i);
749 ok &= start_thread(&d_worker[i].thread, start_worker,
755 // FIXME Clean up the mess. Need to terminate event handler and all workers.
757 // this should cause the workers to exit, unless they're seriously broken
758 send_all_spes(MK_MBOX_MSG(OP_EXIT, 0));
762 throw std::runtime_error("pthread_create");
766 ////////////////////////////////////////////////////////////////////////
769 gc_job_manager_impl::set_eh_state(evt_handler_state s)
771 omni_mutex_lock l(d_eh_mutex);
773 d_eh_cond.broadcast();
777 gc_job_manager_impl::set_ea_args_maxsize(int maxsize)
779 omni_mutex_lock l(d_eh_mutex);
780 d_ea_args_maxsize = maxsize;
781 d_eh_cond.broadcast();
785 gc_job_manager_impl::print_event(spe_event_unit_t *evt)
787 printf("evt: spe = %d events = (0x%x)", evt->data.u32, evt->events);
789 if (evt->events & SPE_EVENT_OUT_INTR_MBOX)
790 printf(" OUT_INTR_MBOX");
792 if (evt->events & SPE_EVENT_IN_MBOX)
795 if (evt->events & SPE_EVENT_TAG_GROUP)
796 printf(" TAG_GROUP");
798 if (evt->events & SPE_EVENT_SPE_STOPPED)
799 printf(" SPE_STOPPED");
804 struct job_client_info {
810 compare_jci_clients(const void *va, const void *vb)
812 const job_client_info *a = (job_client_info *) va;
813 const job_client_info *b = (job_client_info *) vb;
815 return a->client_id - b->client_id;
819 gc_job_manager_impl::notify_clients_jobs_are_done(unsigned int spe_num,
820 unsigned int completion_info_idx)
822 const char *msg = "gc_job_manager_impl::notify_client_job_is_done (INTERNAL ERROR)";
824 smp_rmb(); // order reads so we know that data sent from SPE is here
826 gc_comp_info_t *ci = &d_comp_info[2 * spe_num + (completion_info_idx & 0x1)];
828 if (ci->ncomplete == 0){ // never happens, but ensures code below is correct
834 static int total_jobs;
835 static int total_msgs;
837 total_jobs += ci->ncomplete;
838 printf("ppe: tj = %6d tm = %6d\n", total_jobs, total_msgs);
841 job_client_info gci[GC_CI_NJOBS];
844 * Make one pass through and sanity check everything while filling in gci
846 for (unsigned int i = 0; i < ci->ncomplete; i++){
847 unsigned int job_id = ci->job_id[i];
849 if (job_id >= d_options.max_jobs){
850 // internal error, shouldn't happen
851 fprintf(stderr,"%s: invalid job_id = %d\n", msg, job_id);
852 ci->in_use = 0; // clear flag so SPE knows we're done with it
855 gc_job_desc *jd = &d_jd[job_id];
857 if (jd->sys.client_id >= d_options.max_client_threads){
858 // internal error, shouldn't happen
859 fprintf(stderr, "%s: invalid client_id = %d\n", msg, jd->sys.client_id);
860 ci->in_use = 0; // clear flag so SPE knows we're done with it
864 gci[i].job_id = job_id;
865 gci[i].client_id = jd->sys.client_id;
868 // sort by client_id so we only have to lock & signal once / client
870 if (ci->ncomplete > 1)
871 qsort(gci, ci->ncomplete, sizeof(gci[0]), compare_jci_clients);
875 gc_client_thread_info *last_cti = &d_client_thread[gci[0].client_id];
876 last_cti->d_mutex.lock();
877 bv_set(last_cti->d_jobs_done, gci[0].job_id); // mark job done
879 for (unsigned int i = 1; i < ci->ncomplete; i++){
881 gc_client_thread_info *cti = &d_client_thread[gci[i].client_id];
883 if (cti != last_cti){ // new client?
885 // yes. signal old client, unlock old, lock new
887 // FIXME we could distinguish between CT_WAIT_ALL & CT_WAIT_ANY
889 if (last_cti->d_state == CT_WAIT_ANY || last_cti->d_state == CT_WAIT_ALL)
890 last_cti->d_cond.signal(); // wake client thread up
892 last_cti->d_mutex.unlock();
898 bv_set(cti->d_jobs_done, gci[i].job_id);
903 if (last_cti->d_state == CT_WAIT_ANY || last_cti->d_state == CT_WAIT_ALL)
904 last_cti->d_cond.signal(); // wake client thread up
905 last_cti->d_mutex.unlock();
907 ci->in_use = 0; // clear flag so SPE knows we're done with it
911 gc_job_manager_impl::handle_event(spe_event_unit_t *evt)
915 int spe_num = evt->data.u32;
917 // only a single event type can be signaled at a time
919 if (evt->events == SPE_EVENT_OUT_INTR_MBOX) { // SPE sent us 1 or more msgs
920 static const int NMSGS = 32;
921 unsigned int msg[NMSGS];
922 int n = spe_out_intr_mbox_read(evt->spe, msg, NMSGS, SPE_MBOX_ANY_BLOCKING);
923 // printf("spe_out_intr_mbox_read = %d\n", n);
925 perror("spe_out_intr_mbox_read");
928 for (int i = 0; i < n; i++){
929 switch(MBOX_MSG_OP(msg[i])){
932 printf("eh: job_done (0x%08x) from spu[%d]\n", msg[i], spe_num);
933 notify_clients_jobs_are_done(spe_num, MBOX_MSG_ARG(msg[i]));
937 set_ea_args_maxsize(MBOX_MSG_ARG(msg[i]));
942 printf("eh: Unexpected msg (0x%08x) from spu[%d]\n", msg[i], spe_num);
948 else if (evt->events == SPE_EVENT_SPE_STOPPED){ // the SPE stopped
950 int r = spe_stop_info_read(evt->spe, &si);
952 perror("spe_stop_info_read");
955 switch (si.stop_reason){
958 printf("eh: spu[%d] SPE_EXIT w/ exit_code = %d\n",
959 spe_num, si.result.spe_exit_code);
962 case SPE_STOP_AND_SIGNAL:
963 printf("eh: spu[%d] SPE_STOP_AND_SIGNAL w/ spe_signal_code = 0x%x\n",
964 spe_num, si.result.spe_signal_code);
966 case SPE_RUNTIME_ERROR:
967 printf("eh: spu[%d] SPE_RUNTIME_ERROR w/ spe_runtime_error = 0x%x\n",
968 spe_num, si.result.spe_runtime_error);
970 case SPE_RUNTIME_EXCEPTION:
971 printf("eh: spu[%d] SPE_RUNTIME_EXCEPTION w/ spe_runtime_exception = 0x%x\n",
972 spe_num, si.result.spe_runtime_exception);
974 case SPE_RUNTIME_FATAL:
975 printf("eh: spu[%d] SPE_RUNTIME_FATAL w/ spe_runtime_fatal = 0x%x\n",
976 spe_num, si.result.spe_runtime_fatal);
978 case SPE_CALLBACK_ERROR:
979 printf("eh: spu[%d] SPE_CALLBACK_ERROR w/ spe_callback_error = 0x%x\n",
980 spe_num, si.result.spe_callback_error);
982 case SPE_ISOLATION_ERROR:
983 printf("eh: spu[%d] SPE_ISOLATION_ERROR w/ spe_isolation_error = 0x%x\n",
984 spe_num, si.result.spe_isolation_error);
987 printf("eh: spu[%d] UNKNOWN STOP REASON (%d) w/ spu_status = 0x%x\n",
988 spe_num, si.stop_reason, si.spu_status);
994 else if (evt->events == SPE_EVENT_IN_MBOX){ // there's room to write to SPE
995 // spe_in_mbox_write (ignore)
997 else if (evt->events == SPE_EVENT_TAG_GROUP){ // our DMA completed
998 // spe_mfcio_tag_status_read
1002 fprintf(stderr, "handle_event: unexpected evt->events = 0x%x\n", evt->events);
1008 // This is the "main program" of the event handling thread
1011 gc_job_manager_impl::event_handler_loop()
1013 static const int MAX_EVENTS = 16;
1014 static const int TIMEOUT = 20; // how long to block in milliseconds
1016 spe_event_unit_t events[MAX_EVENTS];
1019 printf("event_handler_loop: starting\n");
1021 set_eh_state(EHS_RUNNING);
1023 // ask the first spe for its max bufsize
1024 send_spe(0, MK_MBOX_MSG(OP_GET_SPU_BUFSIZE, 0));
1029 case EHS_RUNNING: // normal stuff
1030 if (d_shutdown_requested) {
1031 set_eh_state(EHS_SHUTTING_DOWN);
1035 case EHS_SHUTTING_DOWN:
1037 // FIXME wait until job queue is empty, then tell them to exit
1039 send_all_spes(MK_MBOX_MSG(OP_EXIT, 0));
1040 set_eh_state(EHS_WAITING_FOR_WORKERS_TO_DIE);
1043 case EHS_WAITING_FOR_WORKERS_TO_DIE:
1045 bool all_dead = true;
1046 for (unsigned int i = 0; i < d_options.nspes; i++)
1047 all_dead &= d_worker[i].state == WS_DEAD;
1050 set_eh_state(EHS_DEAD);
1052 printf("event_handler_loop: exiting\n");
1059 set_eh_state(EHS_DEAD);
1060 printf("event_handler_loop(default): exiting\n");
1064 // block waiting for events...
1065 int nevents = spe_event_wait(d_spe_event_handler.ptr,
1066 events, MAX_EVENTS, TIMEOUT);
1068 perror("spe_wait_event");
1071 for (int i = 0; i < nevents; i++){
1072 handle_event(&events[i]);
1077 ////////////////////////////////////////////////////////////////////////
1078 // This is the top of the SPE worker threads
1081 start_worker(void *arg)
1083 worker_ctx *w = (worker_ctx *) arg;
1086 w->state = WS_RUNNING;
1088 printf("worker[%d]: WS_RUNNING\n", w->spe_idx);
1090 unsigned int entry = SPE_DEFAULT_ENTRY;
1091 int r = spe_context_run(w->spe_ctx, &entry, 0, w->spu_args, 0, &si);
1093 if (r < 0){ // error
1095 snprintf(buf, sizeof(buf), "worker[%d]: spe_context_run", w->spe_idx);
1099 // spe program called exit.
1101 printf("worker[%d]: SPE_EXIT w/ exit_code = %d\n",
1102 w->spe_idx, si.result.spe_exit_code);
1105 // called stop_and_signal
1107 // I'm not sure we'll ever get here. I think the event
1108 // handler will catch this...
1109 printf("worker[%d]: SPE_STOP_AND_SIGNAL w/ spe_signal_code = 0x%x\n",
1110 w->spe_idx, si.result.spe_signal_code);
1113 // in any event, we're committing suicide now ;)
1115 printf("worker[%d]: WS_DEAD\n", w->spe_idx);
1121 ////////////////////////////////////////////////////////////////////////
1123 gc_client_thread_info *
1124 gc_job_manager_impl::alloc_cti()
1126 for (unsigned int i = 0; i < d_options.max_client_threads; i++){
1127 if (d_client_thread[i].d_free){
1128 // try to atomically grab it
1129 if (_atomic_dec_if_positive(ptr_to_ea(&d_client_thread[i].d_free)) == 0){
1131 gc_client_thread_info *cti = &d_client_thread[i];
1132 cti->d_state = CT_NOT_WAITING;
1133 bv_zero(cti->d_jobs_done);
1134 cti->d_njobs_waiting_for = 0;
1135 cti->d_jobs_waiting_for = 0;
1145 gc_job_manager_impl::free_cti(gc_client_thread_info *cti)
1147 assert((size_t) (cti - d_client_thread.get()) < d_options.max_client_threads);
1152 gc_job_manager_impl::ea_args_maxsize()
1154 omni_mutex_lock l(d_eh_mutex);
1156 while (d_ea_args_maxsize == 0) // wait for it to be initialized
1159 return d_ea_args_maxsize;
1163 gc_job_manager_impl::set_debug(int debug)
1166 s_worker_debug = debug;
1170 gc_job_manager_impl::debug()
1175 ////////////////////////////////////////////////////////////////////////
1178 gc_job_manager_impl::setup_logfiles()
1180 if (!d_options.enable_logging)
1183 if (d_options.log2_nlog_entries == 0)
1184 d_options.log2_nlog_entries = 12;
1186 // must end up a multiple of the page size
1188 size_t pagesize = getpagesize();
1189 size_t s = (1 << d_options.log2_nlog_entries) * sizeof(gc_log_entry_t);
1190 s = ((s + pagesize - 1) / pagesize) * pagesize;
1191 size_t nentries = s / sizeof(gc_log_entry_t);
1192 assert(is_power_of_2(nentries));
1194 for (unsigned int i = 0; i < d_options.nspes; i++){
1196 snprintf(filename, sizeof(filename), "spu_log.%02d", i);
1197 int fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0664);
1202 lseek(fd, s - 1, SEEK_SET);
1204 void *p = mmap(0, s, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1205 if (p == MAP_FAILED){
1206 perror("gc_job_manager_impl::setup_logfiles: mmap");
1212 d_spu_args[i].log.base = ptr_to_ea(p);
1213 d_spu_args[i].log.nentries = nentries;
1218 gc_job_manager_impl::sync_logfiles()
1220 for (unsigned int i = 0; i < d_options.nspes; i++){
1221 if (d_spu_args[i].log.base)
1222 msync(ea_to_ptr(d_spu_args[i].log.base),
1223 d_spu_args[i].log.nentries * sizeof(gc_log_entry_t),
1229 gc_job_manager_impl::unmap_logfiles()
1231 for (unsigned int i = 0; i < d_options.nspes; i++){
1232 if (d_spu_args[i].log.base)
1233 munmap(ea_to_ptr(d_spu_args[i].log.base),
1234 d_spu_args[i].log.nentries * sizeof(gc_log_entry_t));
1238 ////////////////////////////////////////////////////////////////////////
1240 // lookup proc names in d_proc_def table
1243 gc_job_manager_impl::lookup_proc(const std::string &proc_name)
1245 for (int i = 0; i < d_nproc_defs; i++)
1246 if (proc_name == d_proc_def[i].name)
1249 return GCP_UNKNOWN_PROC;
1252 std::vector<std::string>
1253 gc_job_manager_impl::proc_names()
1255 std::vector<std::string> r;
1256 for (int i = 0; i < d_nproc_defs; i++)
1257 r.push_back(d_proc_def[i].name);
1262 ////////////////////////////////////////////////////////////////////////
1264 worker_ctx::~worker_ctx()
1267 int r = spe_context_destroy(spe_ctx);
1269 perror("spe_context_destroy");