void GangWorker::loop() { while (true) { WorkData data = wait_for_task(); run_task(data); signal_task_done(); } }
int main( int argc, char *argv[]) { int c; int err = 0; int ncopies = -1; int onenode = -1; int rc; struct tm_roots rootrot; int nspawned = 0; tm_node_id *nodelist; int start; int stop; int sync = 0; int pernode = 0; char *targethost = NULL; char *allnodes; struct sigaction act; char **ioenv; extern int optind; extern char *optarg; int posixly_correct_set_by_caller = 0; char *envstr; id = malloc(60 * sizeof(char)); if (id == NULL) { fprintf(stderr, "%s: malloc failed, (%d)\n", id, errno); return(1); } sprintf(id, "pbsdsh%s", ((getenv("PBSDEBUG") != NULL) && (getenv("PBS_TASKNUM") != NULL)) ? getenv("PBS_TASKNUM") : ""); #ifdef __GNUC__ /* If it's already set, we won't unset it later */ if (getenv("POSIXLY_CORRECT") != NULL) posixly_correct_set_by_caller = 1; envstr = strdup("POSIXLY_CORRECT=1"); putenv(envstr); #endif while ((c = getopt(argc, argv, "c:n:h:osuv")) != EOF) { switch (c) { case 'c': ncopies = atoi(optarg); if (ncopies <= 0) { err = 1; } break; case 'h': targethost = strdup(optarg); /* run on this 1 hostname */ break; case 'n': onenode = atoi(optarg); if (onenode < 0) { err = 1; } break; case 'o': grabstdio = 1; break; case 's': sync = 1; /* force synchronous spawns */ break; case 'u': pernode = 1; /* run once per node (unique hostnames) */ break; case 'v': verbose = 1; /* turn on verbose output */ break; default: err = 1; break; } /* END switch (c) */ } /* END while ((c = getopt()) != EOF) */ if ((err != 0) || ((onenode >= 0) && (ncopies >= 1))) { fprintf(stderr, "Usage: %s [-c copies][-o][-s][-u][-v] program [args]...]\n", argv[0]); fprintf(stderr, " %s [-n nodenumber][-o][-s][-u][-v] program [args]...\n", argv[0]); fprintf(stderr, " %s [-h hostname][-o][-v] program [args]...\n", argv[0]); fprintf(stderr, "Where -c copies = run copy of \"args\" on the first \"copies\" nodes,\n"); fprintf(stderr, " -n nodenumber = run a copy of \"args\" on the \"nodenumber\"-th node,\n"); fprintf(stderr, " -o = capture stdout of processes,\n"); fprintf(stderr, " -s = forces synchronous execution,\n"); fprintf(stderr, " -u = run on unique hostnames,\n"); fprintf(stderr, " -h = run on this specific hostname,\n"); fprintf(stderr, " -v = forces verbose output.\n"); exit(1); } #ifdef __GNUC__ if (!posixly_correct_set_by_caller) { putenv("POSIXLY_CORRECT"); free(envstr); } #endif if (getenv("PBS_ENVIRONMENT") == NULL) { fprintf(stderr, "%s: not executing under PBS\n", id); return(1); } /* * Set up interface to the Task Manager */ if ((rc = tm_init(0, &rootrot)) != TM_SUCCESS) { fprintf(stderr, "%s: tm_init failed, rc = %s (%d)\n", id, get_ecname(rc), rc); return(1); } sigemptyset(&allsigs); sigaddset(&allsigs, SIGHUP); sigaddset(&allsigs, SIGINT); sigaddset(&allsigs, SIGTERM); act.sa_mask = allsigs; act.sa_flags = 0; /* We want to abort system calls and call a function. */ #ifdef SA_INTERRUPT act.sa_flags |= SA_INTERRUPT; #endif act.sa_handler = bailout; sigaction(SIGHUP, &act, NULL); sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); #ifdef DEBUG if (rootrot.tm_parent == TM_NULL_TASK) { fprintf(stderr, "%s: I am the mother of all tasks\n", id); } else { fprintf(stderr, "%s: I am but a child in the scheme of things\n", id); } #endif /* DEBUG */ if ((rc = tm_nodeinfo(&nodelist, &numnodes)) != TM_SUCCESS) { fprintf(stderr, "%s: tm_nodeinfo failed, rc = %s (%d)\n", id, get_ecname(rc), rc); return(1); } /* nifty unique/hostname code */ if (pernode || targethost) { allnodes = gethostnames(nodelist); if (targethost) { onenode = findtargethost(allnodes, targethost); } else { numnodes = uniquehostlist(nodelist, allnodes); } free(allnodes); if (targethost) free(targethost); } /* We already checked the lower bounds in the argument processing, now we check the upper bounds */ if ((onenode >= numnodes) || (ncopies > numnodes)) { fprintf(stderr, "%s: only %d nodes available\n", id, numnodes); return(1); } /* malloc space for various arrays based on number of nodes/tasks */ tid = (tm_task_id *)calloc(numnodes, sizeof(tm_task_id)); events_spawn = (tm_event_t *)calloc(numnodes, sizeof(tm_event_t)); events_obit = (tm_event_t *)calloc(numnodes, sizeof(tm_event_t)); ev = (int *)calloc(numnodes, sizeof(int)); if ((tid == NULL) || (events_spawn == NULL) || (events_obit == NULL) || (ev == NULL)) { /* FAILURE - cannot alloc memory */ fprintf(stderr, "%s: memory alloc of task ids failed\n", id); return(1); } for (c = 0; c < numnodes; c++) { *(tid + c) = TM_NULL_TASK; *(events_spawn + c) = TM_NULL_EVENT; *(events_obit + c) = TM_NULL_EVENT; *(ev + c) = 0; } /* END for (c) */ /* Now spawn the program to where it goes */ if (onenode >= 0) { /* Spawning one copy onto logical node "onenode" */ start = onenode; stop = onenode + 1; } else if (ncopies >= 0) { /* Spawn a copy of the program to the first "ncopies" nodes */ start = 0; stop = ncopies; } else { /* Spawn a copy on all nodes */ start = 0; stop = numnodes; } if ((ioenv = calloc(2, sizeof(char *)))==NULL) { /* FAILURE - cannot alloc memory */ fprintf(stderr,"%s: memory alloc of ioenv failed\n", id); return(1); } if (grabstdio != 0) { stdoutfd = build_listener(&stdoutport); if ((*ioenv = calloc(50,sizeof(char *))) == NULL) { /* FAILURE - cannot alloc memory */ fprintf(stderr,"%s: memory alloc of *ioenv failed\n", id); return(1); } snprintf(*ioenv,49,"TM_STDOUT_PORT=%d", stdoutport); FD_ZERO(&permrfsd); } sigprocmask(SIG_BLOCK, &allsigs, NULL); for (c = start; c < stop; ++c) { if ((rc = tm_spawn( argc - optind, argv + optind, ioenv, *(nodelist + c), tid + c, events_spawn + c)) != TM_SUCCESS) { fprintf(stderr, "%s: spawn failed on node %d err %s\n", id, c, get_ecname(rc)); } else { if (verbose) fprintf(stderr, "%s: spawned task %d\n", id, c); ++nspawned; if (sync) wait_for_task(&nspawned); /* one at a time */ } } /* END for (c) */ if (sync == 0) wait_for_task(&nspawned); /* wait for all to finish */ /* * Terminate interface with Task Manager */ tm_finalize(); return 0; } /* END main() */
int main(int argc, char **argv) { printf("%s: HelenOS IPC Naming Service\n", NAME); int rc = service_init(); if (rc != EOK) return rc; rc = clonable_init(); if (rc != EOK) return rc; rc = task_init(); if (rc != EOK) return rc; printf("%s: Accepting connections\n", NAME); while (true) { process_pending_conn(); process_pending_wait(); ipc_call_t call; ipc_callid_t callid = ipc_wait_for_call(&call); task_id_t id; sysarg_t retval; switch (IPC_GET_IMETHOD(call)) { case IPC_M_PHONE_HUNGUP: retval = ns_task_disconnect(&call); break; case IPC_M_CONNECT_TO_ME: /* * Server requests service registration. */ if (service_clonable(IPC_GET_ARG1(call))) { register_clonable(IPC_GET_ARG1(call), IPC_GET_ARG5(call), &call, callid); continue; } else { retval = register_service(IPC_GET_ARG1(call), IPC_GET_ARG5(call), &call); } break; case IPC_M_CONNECT_ME_TO: /* * Client requests to be connected to a service. */ if (service_clonable(IPC_GET_ARG1(call))) { connect_to_clonable(IPC_GET_ARG1(call), &call, callid); continue; } else { connect_to_service(IPC_GET_ARG1(call), &call, callid); continue; } break; case NS_PING: retval = EOK; break; case NS_TASK_WAIT: id = (task_id_t) MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call)); wait_for_task(id, &call, callid); continue; case NS_ID_INTRO: retval = ns_task_id_intro(&call); break; case NS_RETVAL: retval = ns_task_retval(&call); break; default: retval = ENOENT; break; } if (!(callid & IPC_CALLID_NOTIFICATION)) ipc_answer_0(callid, retval); } /* Not reached */ return 0; }
int main(int argc, char *argv[]) { struct work_queue *q; int port = WORK_QUEUE_DEFAULT_PORT; if(argc != 4) { printf("Usage: work_queue_workload_simulator <workload_spec> <logfile> <proj_name> \n"); exit(1); } struct list *specs = get_workload_specs(argv[1]); if(!specs) { printf("Failed to load a non-empty workload specification.\n"); exit(1); } created_files = list_create(); if(!created_files) { printf("Failed to allocate memory for a list to store created files.\n"); exit(1); } // open log file logfile = fopen(argv[2], "a"); if(!logfile) { printf("Couldn't open logfile %s: %s\n", argv[2], strerror(errno)); exit(1); } q = work_queue_create(port); if(!q) { printf("couldn't listen on port %d: %s\n", port, strerror(errno)); goto fail; exit(1); } printf("listening on port %d...\n", work_queue_port(q)); // specifying the right modes work_queue_specify_master_mode(q, WORK_QUEUE_MASTER_MODE_CATALOG); work_queue_specify_name(q, argv[3]); work_queue_specify_estimate_capacity_on(q, 1); // report capacity on int time_elapsed = 0; // in seconds int series_id = 0; time_t start_time = time(0); log_work_queue_status(q); while(1) { struct task_series *ts = (struct task_series *)list_peek_tail(specs); if(!ts) { while(!work_queue_empty(q)) { // wait until all tasks to finish wait_for_task(q, 5); } break; } else { time_elapsed = time(0) - start_time; int time_until_next_submit = ts->submit_time - time_elapsed; if(time_until_next_submit <=0) { list_pop_tail(specs); printf("time elapsed: %d seconds\n", time_elapsed); if(!submit_task_series(q, ts, series_id)) { // failed to submit tasks fprintf(stderr, "Failed to submit tasks.\n"); goto fail; } free(ts); series_id++; } else { time_t stoptime = start_time + ts->submit_time; while(!work_queue_empty(q)) { int timeout = stoptime - time(0); if(timeout > 0) { wait_for_task(q, timeout); } else { break; } } time_t current_time = time(0); if(current_time < stoptime) { sleep(stoptime - current_time); } } } } printf("all tasks complete!\n"); work_queue_delete(q); remove_created_files(); fclose(logfile); return 0; fail: remove_created_files(); fclose(logfile); exit(1); }