static void init_mpb() { unsigned i; shm_master_mpb = shm_alloc(SHM_MPB_KEY, sizeof(struct mpb_master_t), 0666); for (i = 0; i < SLAVES; i++) { shm_slave_mpb[i] = shm_alloc(SHM_MPB_KEY+1+i, sizeof(struct mpb_t), 0666); } }
static ngx_int_t delete_callback_handler(ngx_int_t code, nchan_channel_t *chan, delete_data_t *d) { nchan_channel_t *chan_info; d->code = code; if (chan) { if((chan_info = shm_alloc(nchan_memstore_get_shm(), sizeof(*chan_info), "channel info for delete IPC response")) == NULL) { d->shm_channel_info = NULL; //yeah ERR("unable to allocate chan_info"); } else { d->shm_channel_info= chan_info; chan_info->messages = chan->messages; chan_info->subscribers = chan->subscribers; chan_info->last_seen = chan->last_seen; if(chan->last_published_msg_id.tagcount > NCHAN_FIXED_MULTITAG_MAX) { //meh, this can't be triggered... can it?... nchan_msg_id_t zeroid = NCHAN_ZERO_MSGID; chan_info->last_published_msg_id = zeroid; } else { chan_info->last_published_msg_id = chan->last_published_msg_id; } } } else { d->shm_channel_info = NULL; } ipc_cmd(delete_reply, d->sender, d); return NGX_OK; }
/** * 初始化 */ int donkeyid_init(int _isshm) { isshm = _isshm; //是否使用共享内存 if (isshm <= 0) { ctxaddr = malloc(sizeof(dtypes)); if (!ctxaddr) { return -1; } bzero(ctxaddr, sizeof(dtypes)); lock = (mlocks *) ctxaddr; } else { shmctx.size = sizeof(dtypes); if (shm_alloc(&shmctx) == -1) { return -1; } bzero(shmctx.addr, sizeof(dtypes)); lock = (mlocks *) shmctx.addr; } //获取cpu核心数量 ncpu = (int) sysconf(_SC_NPROCESSORS_ONLN); if (ncpu <= 0) { ncpu = 1; } return 0; }
/*ARGSUSED*/ static int ctl_open(int using_stdin, int using_stdout) { shm_alloc(); k_pipe_open(); if (child_pid == 0) start_panel(); signal(SIGCHLD, get_child); signal(SIGTERM, shm_free); signal(SIGINT, shm_free); signal(SIGHUP, shm_free); ctl.opened=1; return 0; }
int main(){ shm_t buf; int s; int runtime, timeout; buf.size = 2048; shm_alloc(&buf); buf.addr[0] = 0; runtime = 2000; timeout = 1000; s = process(proc_handle, &buf, &runtime, timeout); printf("status[%d] shm.addr[%s]\n", s, buf.addr); shm_free(&buf); }
static void _gst_load_image(int size_w, int size_h) { GstBuffer *buffer; D("load image\n"); gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, duration / 2); g_signal_emit_by_name(sink, "pull-preroll", &buffer, NULL); D("load image : %p %d\n", GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer)); shm_alloc(width * height * sizeof(DATA32)); if (!shm_addr) return; data = shm_addr; memcpy(data, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer)); }
void poppler_load_image(int size_w, int size_h) { SplashOutputDev *output_dev; SplashColor white; SplashColorPtr color_ptr; DATA32 *src, *dst; int y; white[0] = 255; white[1] = 255; white[2] = 255; white[3] = 255; output_dev = new SplashOutputDev(splashModeXBGR8, 4, gFalse, white); if (!output_dev) return; output_dev->startDoc(pdfdoc->getXRef()); if (dpi <= 0.0) dpi = DEF_DPI; page->displaySlice(output_dev, dpi, dpi, 0, false, false, 0, 0, width, height, false, pdfdoc->getCatalog()); color_ptr = output_dev->getBitmap()->getDataPtr(); shm_alloc(crop_width * crop_height * sizeof(DATA32)); if (!shm_addr) goto del_outpput_dev; data = shm_addr; src = (DATA32 *)color_ptr; dst = (DATA32 *)data; for (y = 0; y < crop_height; y++) { memcpy(dst, src, crop_width * sizeof(DATA32)); src += width; dst += crop_width; } del_outpput_dev: delete output_dev; }
void *shm_realloc(void *ptr, size_t sz) { int i; size_t tocopy; void *n; /* Find the chunk */ for(i = 0; i < MAX_CHUNKS; i++) { if(chunklist[i].start == ptr) { break; } } /* We didn't allocate this chunk */ CHECK_ERROR(i == MAX_CHUNKS); /* How much do we need to copy? */ if(sz > chunklist[i].size) { tocopy = chunklist[i].size; } else if(sz < chunklist[i].size) { tocopy = sz; } else { /* If we're reallocing the same amount of memory, don't bother. */ return ptr; } /* Allocate a new chunk */ n = shm_alloc(sz); if(n == NULL) { errno = ENOMEM; return NULL; } /* Copy over the data and free the old chunk */ /* NOTE: we could temporarily copy the data into local memory, then free, * then allocate and copy, to reduce failures here. --awg */ mem_memcpy(n, ptr, tocopy); shm_free(ptr); return n; }
int main(int argc, char** argv) { if (argc < 4) { printf("Usage: %s ip port path2worker [shmsize]\n", argv[0]); return EXIT_FAILURE; } struct listener_s listener; listener.argc = argc; listener.argv = argv; listener.stop_moniter = 0; listener.shm_id = -1; listener.shm_size = -1; if (argc > 4) { listener.shm_size = atoi(argv[4]); if (listener.shm_size > 0) { listener.shm_id = shm_alloc(0, listener.shm_size); } if (listener.shm_id != -1) { shm_free(listener.shm_id); // lazy free } } // get loop struct ev_loop* loop = ev_default_loop(EVBACKEND_EPOLL); ev_set_userdata(loop, &listener); // setup signal handler struct ev_signal quitwatcher; struct ev_signal hupwatcher; ev_signal_init(&quitwatcher, listener_stop, SIGQUIT); ev_signal_init(&hupwatcher, restart_workers, SIGHUP); ev_signal_start(loop, &quitwatcher); ev_unref(loop); // 将loop中的watchercnt--,保证不停止此watcher的情况下loop也能正常退出 ev_signal_start(loop, &hupwatcher); ev_unref(loop); // 将loop中的watchercnt--,保证不停止此watcher的情况下loop也能正常退出 // get cpu number listener.worker_count = (int)sysconf(_SC_NPROCESSORS_CONF); // init workers struct worker_s workers[listener.worker_count]; listener.workers = &workers[0]; if (0 != setup_workers(loop, &listener)) { return EXIT_FAILURE; } int r = ev_run(loop, 0); ev_ref(loop); ev_signal_stop(loop, &quitwatcher); ev_ref(loop); ev_signal_stop(loop, &hupwatcher); return r; }
/* System calls. */ int sys_shm_open(struct thread *td, struct shm_open_args *uap) { struct filedesc *fdp; struct shmfd *shmfd; struct file *fp; char *path; Fnv32_t fnv; mode_t cmode; int fd, error; #ifdef CAPABILITY_MODE /* * shm_open(2) is only allowed for anonymous objects. */ if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON)) return (ECAPMODE); #endif if ((uap->flags & O_ACCMODE) != O_RDONLY && (uap->flags & O_ACCMODE) != O_RDWR) return (EINVAL); if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) return (EINVAL); fdp = td->td_proc->p_fd; cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; error = falloc(td, &fp, &fd, 0); if (error) return (error); /* A SHM_ANON path pointer creates an anonymous object. */ if (uap->path == SHM_ANON) { /* A read-only anonymous object is pointless. */ if ((uap->flags & O_ACCMODE) == O_RDONLY) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (EINVAL); } shmfd = shm_alloc(td->td_ucred, cmode); } else { path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); error = copyinstr(uap->path, path, MAXPATHLEN, NULL); /* Require paths to start with a '/' character. */ if (error == 0 && path[0] != '/') error = EINVAL; if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); free(path, M_SHMFD); return (error); } fnv = fnv_32_str(path, FNV1_32_INIT); sx_xlock(&shm_dict_lock); shmfd = shm_lookup(path, fnv); if (shmfd == NULL) { /* Object does not yet exist, create it if requested. */ if (uap->flags & O_CREAT) { #ifdef MAC error = mac_posixshm_check_create(td->td_ucred, path); if (error == 0) { #endif shmfd = shm_alloc(td->td_ucred, cmode); shm_insert(path, fnv, shmfd); #ifdef MAC } #endif } else { free(path, M_SHMFD); error = ENOENT; } } else { /* * Object already exists, obtain a new * reference if requested and permitted. */ free(path, M_SHMFD); if ((uap->flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) error = EEXIST; else { #ifdef MAC error = mac_posixshm_check_open(td->td_ucred, shmfd, FFLAGS(uap->flags & O_ACCMODE)); if (error == 0) #endif error = shm_access(shmfd, td->td_ucred, FFLAGS(uap->flags & O_ACCMODE)); } /* * Truncate the file back to zero length if * O_TRUNC was specified and the object was * opened with read/write. */ if (error == 0 && (uap->flags & (O_ACCMODE | O_TRUNC)) == (O_RDWR | O_TRUNC)) { #ifdef MAC error = mac_posixshm_check_truncate( td->td_ucred, fp->f_cred, shmfd); if (error == 0) #endif shm_dotruncate(shmfd, 0); } if (error == 0) shm_hold(shmfd); } sx_xunlock(&shm_dict_lock); if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (error); } } finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); FILEDESC_XLOCK(fdp); if (fdp->fd_ofiles[fd] == fp) fdp->fd_ofileflags[fd] |= UF_EXCLOSE; FILEDESC_XUNLOCK(fdp); td->td_retval[0] = fd; fdrop(fp, td); return (0); }
static void read_tables(int mode = SHM_NO) { int i, j, k, r; FOR (i, PATT_NUM) { char name[2*DATEI_MAX]; sprintf(name, "%s/%s", DataPath, pattern_files[i]); FILE *fp = fopen(name, "r"); if (!fp) { fprintf(stderr, name); Error("file not found"); } cout << name << endl; int len = getc(fp); if (len < 1 || len > 12) Error("length corrupt"); r = fgetc(fp); if (r < 1 || r > 60) Error("interval_num corrupt"); if (interval_num && interval_num != r) Error("interval_num different"); interval_num = r; r = fgetc(fp); if (r < 4 || r >= 60) Error("disc_min corrupt"); if (disc_min && disc_min != r) Error("disc_min different"); disc_min = r; r = fgetc(fp); if (r < 1 || r > 60) Error("interval_len corrupt"); if (interval_len && interval_len != r) Error("interval_len different"); interval_len = r; int config_num = Pot3[len]; FOR (k, interval_num) { if (mode == SHM_NO) { pattern_tabs[i][k] = (TTYPE*) calloc(sizeof(TTYPE), config_num); } else { if (mode == SHM_CREATE) pattern_tabs[i][k] = (TTYPE*) shm_alloc(sizeof(TTYPE) * config_num); else if (mode == SHM_ACCESS) { pattern_tabs[i][k] = (TTYPE*) shm_next_alloc(sizeof(TTYPE) * config_num); } else Error("illegal SHM mode"); } FOR (j, config_num) { short val = fgetc(fp); val += fgetc(fp) << 8; if (mode != SHM_ACCESS) pattern_tabs[i][k][j] = val; else if (i != 1 && pattern_tabs[i][k][j] != val) Error("shm val different!"); } pattern_tabs[i][k] += (config_num-1)/2; }
int main(int argc, char** argv) { int N /* Number of workers */, L /* Number of locks */, T /* Timeout */, i, shm_id /* Shared memory segment id */, lifesign_fds[2] /* Driver life sign */, status /* Worker exit status */; struct timeval timeval; struct rusage rusage /* Worker resource usage */; char *end; pid_t pid; pid_t worker_pid[WORKERS_MAX] /* Worker pid */; int worker_lock[WORKERS_MAX] /* Worker to lock mapping */; long long total_cycles = 0 /* Total number of cycles */; double max_wall = 0.0 /* Maximum wall time */; double total_user = 0.0 /* Total user time */; double total_sys = 0.0 /* Total system time */; /* Initialize variables */ for(i=0; i < WORKERS_MAX; ++i) { worker_pid[i] = 0; worker_lock[i] = 0; } /* Parser arguments */ if (argc < 4) usage(NULL); count_per_cycle = strtol(argv[1], &end, 10); if ((*end != '\0') || (count_per_cycle <= 0) || (errno == ERANGE)) usage("counter increments per cycle '%s' invalid", argv[1]); N = strtol(argv[2], &end, 10); if ((*end != '\0') || (N <= 0) || (errno == ERANGE)) usage("worker count '%s' invalid", argv[2]); if (N > WORKERS_MAX) die(0, "too many workers"); T = strtol(argv[3], &end, 10); if ((*end != '\0') || (T <= 0) || (errno == ERANGE)) usage("timeout '%s' invalid", argv[3]); for(i=4; i < argc; ++i) { int w, l; w = strtol(argv[i], &end, 10); if ((*end != ':') || (w < 0) || (errno == ERANGE)) usage("worker invalid in worker to lock assignment '%s'", argv[i]); if (w >= N) { printf("Ignoring lock assignment '%s' for non-existing worker %d\n", argv[i], w); continue; } l = strtol(end+1, &end, 10); if ((*end != '\0') || (l < 0) || (errno == ERANGE)) usage("lock invalid in worker to lock assignment '%s'", argv[i]); worker_lock[w] = l; } /* Compute necessary number of locks */ L = 0; for(i=0; i < N; ++i) { if (L < worker_lock[i] + 1) L = worker_lock[i] + 1; } /* Create driver life sign. * Once all copies of the pipe's writing end are closed, the process * groups get sent a SIGIO. Since we close the writing end in all * workers, and register a handler for SIGIO with sends a SIGTERM, * this effectively causes the workers to be killed if the driver exits */ if (signal(SIGIO, sighandler_exit) != 0) die(errno, "failed to register life sign signal handler"); if (pipe(lifesign_fds) != 0) die(errno, "failed to create pipe to use as driver life sign"); if ((i = fcntl(lifesign_fds[0], F_GETFL)) < 0) die(errno, "failed to get flags of the driver life sign pipe's watching end"); i |= FNONBLOCK | FASYNC; if (fcntl(lifesign_fds[0], F_SETFL, i) != 0) die(errno, "failed to set flags of the driver life sign pipe's watching end"); if (fcntl(lifesign_fds[0], F_SETOWN, -getpgrp()) != 0) die(errno, "failed to notification pid of the driver life sign pipe's watching end"); /* Create and attach shared memory segment */ if ((shm_id = shmget(IPC_PRIVATE, SHM_SIZE, 0600)) < 0) die(errno, "failed to create shared memory segment"); shm_base = (char*)shmat(shm_id, NULL, 0); if (shm_base == (char*)-1) die(errno, "failed to attach shared memory segment"); if (shmctl(shm_id, IPC_RMID, NULL) != 0) die(errno, "failed to mark shared memory segment for destruction at exit"); shm_next = shm_base; /* Allocate shared memory slices to all shared structured. * All structured are aligned on cache line boundaries. */ shm_worker_wall = (double*) shm_alloc(sizeof(double) * N, sizeof(double)); shm_worker_user = (double*) shm_alloc(sizeof(double) * N, sizeof(double)); shm_worker_sys = (double*) shm_alloc(sizeof(double) * N, sizeof(double)); shm_worker_counter = (counter_t*) shm_alloc(sizeof(counter_t) * N, -1); shm_lock_refs = (lock_ref_t) shm_alloc(sizeof(lock_t) * L, -1); /* Initialize shared memory */ for(i=0; i < N; ++i) { shm_worker_wall[i] = 0.0; shm_worker_user[i] = 0.0; shm_worker_sys[i] = 0.0; shm_worker_counter[i].value = 0; } locks_init(L, N); for(i=0; i < L; ++i) lock_init(shm_lock_refs + i); /* Print configuration */ printf("Cache line size in bytes: %d\n", CACHELINE_SIZE); printf("Counter size in bytes: %d\n", (int)sizeof(counter_t)); printf("Lock size in bytes: %d\n", lock_size()); printf("--\n"); printf("Number of workers: %d\n", N); printf("Number of locks: %d\n", L); /* Launch workers */ for(i=0; i < N; ++i) { pid = fork(); if (pid < 0) die(errno, "failed to fork()"); if (pid == 0) { /* Worker */ volatile counter_t *counter = &shm_worker_counter[i]; lock_ref_t lock = &shm_lock_refs[worker_lock[i]]; /* Global variable, require for sighandler_alarm */ worker_index = i; /* Close owning end of driver life sign pipe */ close(lifesign_fds[1]); /* Initialize locking infrastructure for worker */ worker_init(worker_index); /* Output *before* waiting */ printf("Worker %d uses counter at %p, lock %d at %p\n", i, counter, worker_lock[i], lock_addr(lock)); /* Arrage for elapsed time and resource usage measurement * on worker exit */ if (signal(SIGALRM, sighandler_alarm) != 0) die(errno, "failed to register alarm signal handler"); /* Wait for driver to tell us to start */ kill(getpid(), SIGSTOP); /* Initial time and resource usage measurement. */ if (gettimeofday(&timeval, NULL) != 0) die(errno, "failed to query wall time in worker after launch"); shm_worker_wall[i] -= (double)(timeval.tv_sec) + (double)(timeval.tv_usec)*1e-6; if (getrusage(RUSAGE_SELF, &rusage) != 0) die(errno, "failed to query worker resource usage after launch"); shm_worker_user[i] -= (double)(rusage.ru_utime.tv_sec) + (double)(rusage.ru_utime.tv_usec)*1e-6; shm_worker_sys[i] -= (double)(rusage.ru_stime.tv_sec) + (double)(rusage.ru_stime.tv_usec)*1e-6; child(counter, lock); exit(1); } else { /* Driver */ worker_pid[i] = pid; } } /* Wait for workers to finish launching */ for(i=0; i < N; ++i) { if ((wait4(worker_pid[i], &status, WUNTRACED, NULL) < 0) || !WIFSTOPPED(status) ) { die(0, "worker %d with pid %d failed", i, worker_pid[i]); } } /* Tell workers to start */ printf("Starting workers\n"); kill(-getpgrp(), SIGCONT); /* Let workers run for a while */ sleep(T); /* Terminate workers */ for(i=0; i < N; ++i) kill(worker_pid[i], SIGALRM); /* Aggregate data * Must wait for worker to exit before reading its stats */ for(i=0; i < N; ++i) { if ((wait4(worker_pid[i], &status, WUNTRACED, NULL) < 0) || !WIFEXITED(status) || (WEXITSTATUS(status) != 0) ) { die(0, "worker %d with pid %d failed (%s %d)", i, worker_pid[i], WIFEXITED(status) ? "exitcode" : "signal", WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status) ); } total_cycles += shm_worker_counter[i].value / count_per_cycle; if (max_wall < shm_worker_wall[i]) max_wall = shm_worker_wall[i]; total_user += shm_worker_user[i]; total_sys += shm_worker_sys[i]; } /* Show data */ for(i=0; i < N; ++i) { printf(" Worker %d cycles: %llu\n", i, shm_worker_counter[i].value / count_per_cycle); printf(" Worker %d wall time: %f\n", i, shm_worker_wall[i]); printf(" Worker %d user time: %f\n", i, shm_worker_user[i]); printf(" Worker %d system time: %f\n", i, shm_worker_sys[i]); printf(" Worker %d per-cycle wall time: %.1e\n", i, shm_worker_wall[i] * (double)count_per_cycle / (double)shm_worker_counter[i].value); printf(" Worker %d per-cycle user time: %.1e\n", i, shm_worker_user[i] * (double)count_per_cycle / (double)shm_worker_counter[i].value); printf(" Worker %d per-cycle system time: %.1e\n", i, shm_worker_sys[i] * (double)count_per_cycle / (double)shm_worker_counter[i].value); } printf("Total cycles: %.1e (%llu)\n", (double)total_cycles, total_cycles); printf("Maximum wall time: %f\n", max_wall); printf("Total user time: %f\n", total_user); printf("Total system time: %f\n", total_sys); printf("Concurrency (process vs. wall time): %f\n", (total_user + total_sys) / max_wall); printf("Average per-cycle wall time: %.1e\n", max_wall / (double)total_cycles); printf("Average per-cycle user time: %.1e\n", total_user / (double)total_cycles); printf("Average per-cycle system time: %.1e\n", total_sys / (double)total_cycles); return 0; }