pid_t eat_cpu(int (test_function)(void)) { union pipe read_pipe, write_pipe; int cpu, rc; pid_t pid; cpu = pick_online_cpu(); FAIL_IF(cpu < 0); FAIL_IF(bind_to_cpu(cpu)); if (pipe(read_pipe.fds) == -1) return -1; if (pipe(write_pipe.fds) == -1) return -1; pid = fork(); if (pid == 0) exit(eat_cpu_child(write_pipe, read_pipe)); if (sync_with_child(read_pipe, write_pipe)) { rc = -1; goto out; } printf("main test running as pid %d\n", getpid()); rc = test_function(); out: kill(pid, SIGKILL); return rc; }
static int server(int offset) { struct sockaddr_in saddr_in; struct sockaddr addr; unsigned int len; int sk, opt; bind_to_cpu(offset); if (nice(-20) < 0) perror("nice"); sk = socket(PF_INET, SOCK_STREAM, 0); if (sk < 0) return error("socket"); opt = 1; if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) return error("setsockopt"); saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); saddr_in.sin_port = htons(net_port + offset); if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) return error("bind"); if (listen(sk, 1) < 0) return error("listen"); len = sizeof(addr); if (getsockname(sk, &addr, &len) < 0) return error("getsockname"); return accept_loop(sk); }
int cpu_event_pinned_vs_ebb(void) { union pipe read_pipe, write_pipe; struct event event; int cpu, rc; pid_t pid; SKIP_IF(!ebb_is_supported()); cpu = pick_online_cpu(); FAIL_IF(cpu < 0); FAIL_IF(bind_to_cpu(cpu)); FAIL_IF(pipe(read_pipe.fds) == -1); FAIL_IF(pipe(write_pipe.fds) == -1); pid = fork(); if (pid == 0) { /* NB order of pipes looks reversed */ exit(ebb_child(write_pipe, read_pipe)); } /* We setup the cpu event first */ rc = setup_cpu_event(&event, cpu); if (rc) { kill_child_and_wait(pid); return rc; } /* Signal the child to install its EBB event and wait */ if (sync_with_child(read_pipe, write_pipe)) /* If it fails, wait for it to exit */ goto wait; /* Signal the child to run */ FAIL_IF(sync_with_child(read_pipe, write_pipe)); wait: /* We expect it to fail to read the event */ FAIL_IF(wait_for_child(pid) != 2); FAIL_IF(event_disable(&event)); FAIL_IF(event_read(&event)); event_report(&event); /* The cpu event should have run */ FAIL_IF(event.result.value == 0); FAIL_IF(event.result.enabled != event.result.running); return 0; }
void gpu_worker(void *arg) { hs_worker *worker_arg = (hs_worker *) arg; bind_to_cpu(worker_arg); init_cuda(worker_arg->device_id); // printf("GPU I am id:%d\n", worker_arg->worker_id); pthread_mutex_lock(&worker_arg->mutex); worker_arg->initialized = 1; pthread_cond_signal(&worker_arg->ready); pthread_mutex_unlock(&worker_arg->mutex); _task_t task; while (is_running()) { lock_queue(worker_arg->task_queue); task = pop_task(worker_arg->task_queue); if (task == NULL) { if (is_running()) sleep_worker(worker_arg); unlock_queue(worker_arg->task_queue); continue; } unlock_queue(worker_arg->task_queue); if ((task->task->arch_type & worker_arg->arch) != worker_arg->arch) { push_task(worker_arg->task_queue, task); continue; } execute_task(worker_arg, task); } deinit_cuda(); pthread_exit((void*) 0); }
int worker(int num) { char *local, *remote; //int id; int k; fprintf(stderr, "\tbind process %d to cpu %ld\n", num, cpu_num - num - 1); if (bind_to_cpu(cpu_num - num - 1) < 0) { //(*total_e)++; kill(0, SIGUSR1); exit(-1); } local = (char *)create_shm(key[num], shm_size); if (local == NULL) { kill(0, SIGUSR1); exit(-1); } k = (num % 2 == 0 ? num + 1 : num - 1); remote = (char *)wait_remote_shm(key[k], shm_size); if (remote == NULL) { kill(0, SIGUSR1); exit(-1); } while (1) { sem_wait(sem[num]); fill_mem(local, data, shm_size); sem_wait(write_lock); (*total_w)++; sem_post(write_lock); sem_post(sem[num]); sem_wait(sem[k]); if (read_mem(remote, data, shm_size) < 0) { sem_post(sem[k]); (*total_e)++; kill(0, SIGUSR1); exit(-1); } sem_wait(read_lock); (*total_r)++; sem_post(read_lock); sem_post(sem[k]); } }
static int client(int offset) { int file_fd, out_fd; char fname[64]; bind_to_cpu(offset); if (nice(-20) < 0) perror("nice"); if (!write_to_null) out_fd = client_open_net(offset); else out_fd = open("/dev/null", O_WRONLY); if (out_fd < 0) return error("socket"); sprintf(fname, "%s%d", filename, same_file ? 0 : offset); file_fd = open(fname, O_RDONLY); if (file_fd < 0) return error("open"); return do_client(out_fd, file_fd, offset); }