void nbio_shmem(int n) { nprocs = n; children = (struct children *)shm_setup(sizeof(*children) * nprocs); if (!children) { printf("Failed to setup shared memory!\n"); exit(1); } }
void nbio_shmem(int n, int t_timelimit, int t_warmup) { nprocs = n; children = shm_setup(sizeof(*children) * nprocs); if (!children) { printf("Failed to setup shared memory!\n"); nb_exit(1); } memset(children, 0, sizeof(*children) * nprocs); timelimit = t_timelimit; warmup = t_warmup; in_cleanup = 0; tv_start = timeval_current(); }
int main(int argc, char *argv[]) { volatile char *buf; int size; if (argc < 2) { printf("shm_size <size>\n"); exit(1); } size = atoi(argv[1]); buf = shm_setup(size); if (!buf) { printf("shm_setup(%d) failed\n", size); exit(1); } return 0; }
int main(int argc, char * const argv[]) { int opt, i; const char *progname = argv[0]; struct stat st; /* parse command-line options */ while ((opt = getopt(argc, argv, "LSN:F:t:s:M:U:AhCED")) != -1) { switch (opt) { case 'L': options.use_lease = true; break; case 'S': options.use_sharemode = true; break; case 'A': options.use_aio = true; break; case 'C': options.skip_file_creation = true; break; case 'N': options.nprocesses = atoi(optarg); break; case 'F': options.nfiles = atoi(optarg); break; case 'M': options.migrate_cmd = strdup(optarg); break; case 's': options.fsize = atoi(optarg); break; case 'U': options.io_uid = atoi(optarg); break; case 't': options.timelimit = atoi(optarg); break; case 'E': options.exit_child_on_error = true; break; case 'D': options.die_on_error = true; options.exit_child_on_error = true; break; default: usage(); break; } } if ((options.use_lease || options.use_sharemode) && strstr(progname, "smbd") == NULL) { printf("ERROR: you must invoke as smbd to use leases or share modes - use a symlink\n"); exit(1); } setlinebuf(stdout); argv += optind; argc -= optind; if (argc == 0) { usage(); } options.dir = argv[0]; if (stat(options.dir, &st) != 0 || !S_ISDIR(st.st_mode)) { printf("'%s' must exist and be a directory\n", options.dir); exit(1); } children = shm_setup(sizeof(*children) * options.nprocesses); buf = malloc(options.fsize); if (!options.skip_file_creation) { printf("Creating %u files of size %u in '%s'\n", options.nfiles, options.fsize, options.dir); for (i=0; i<options.nfiles; i++) { int fd; char *fname = filename(i); fd = open(fname, O_CREAT|O_RDWR, 0600); if (fd == -1) { perror(fname); exit(1); } ftruncate(fd, options.fsize); memset(buf, 1+(i%255), options.fsize); if (write(fd, buf, options.fsize) != options.fsize) { printf("Failed to write '%s'\n", fname); exit(1); } fsync(fd); close(fd); free(fname); } } parent_pid = getpid(); printf("Starting %u child processes for %u seconds\n", options.nprocesses, options.timelimit); printf("Results shown as: offline=numoffline/total latencies: current/worst\n"); for (i=0; i<options.nprocesses; i++) { pid_t pid = fork(); if (pid == 0) { children[i].pid = getpid(); children[i].child_num = i; run_child(&children[i]); } else { children[i].pid = pid; } } /* show status once a second */ signal(SIGALRM, sig_alarm); tv_start = timeval_current(); alarm(1); /* wait for the children to finish */ for (i=0; i<options.nprocesses; i++) { int status; while (waitpid(-1, &status, 0) != 0 && errno != ECHILD) ; if (WEXITSTATUS(status) != 0 && options.die_on_error) { exit(1); } } return 0; }
/* this creates the specified number of child processes and runs fn() in all of them */ static double create_procs(int nprocs, void (*fn)(struct child_struct * )) { int i, status; int synccount; #ifdef OS2 int cRunning; #endif #ifndef OS2 signal(SIGCONT, sigcont); #endif start_timer(); synccount = 0; if (nprocs < 1) { fprintf(stderr, "create %d procs? you must be kidding.\n", nprocs); return 1; } children = shm_setup(sizeof(struct child_struct)*nprocs); if (!children) { printf("Failed to setup shared memory\n"); return end_timer(); } #ifdef OS2 sem_create_os2(); #endif memset(children, 0, sizeof(*children)*nprocs); for (i=0;i<nprocs;i++) { children[i].id = i; children[i].nprocs = nprocs; } for (i=0;i<nprocs;i++) { if (fork() == 0) { #ifdef OS2 shm_attach_os2(); #endif setbuffer(stdout, NULL, 0); nb_setup(&children[i]); children[i].status = getpid(); #ifndef OS2 pause(); #else sem_wait_os2(); #endif fn(&children[i]); _exit(0); } } do { synccount = 0; for (i=0;i<nprocs;i++) { if (children[i].status) synccount++; } if (synccount == nprocs) break; sleep(1); } while (end_timer() < 30); if (synccount != nprocs) { printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount); return end_timer(); } start_timer(); #ifndef OS2 kill(0, SIGCONT); signal(SIGALRM, sig_alarm); alarm(PRINT_FREQ); printf("%d clients started\n", nprocs); for (i=0;i<nprocs;) { if (waitpid(0, &status, 0) == -1) continue; if (WEXITSTATUS(status) != 0) { printf("Child failed with status %d\n", WEXITSTATUS(status)); exit(1); } i++; } alarm(0); sig_alarm(); #else sem_signal_os2(); printf("%d clients started\n", nprocs); do { sleep(1); sig_alarm(); for (cRunning = i = 0; i < nprocs; i++) if (!children[i].done) cRunning++; } while (cRunning > 0); status = status; #endif printf("\n"); return end_timer(); }
int main( int argc, char *argv[] ){ Socket **localsocks; CamConfig *camcfg; char *shm_segment, tmpbuf[ 1024 ], cfg_path[ MAXPATHLEN ]; int fd, shm_alloc, donecfg; extern int errno; donecfg = 0; if (argc >= 2) { strncpy( cfg_path, argv[ 1 ], sizeof( cfg_path ) ); cfg_path[ sizeof( cfg_path ) - 1 ] = '\0'; camserv_log( "main", "Trying to read config file \"%s\": ", cfg_path); if( (camcfg = read_ccfg( cfg_path )) == NULL ){ camserv_log( "main", "Error reading config \"%s\": %s", cfg_path, strerror( errno )); } else { camserv_log( "main", "Success reading config \"%s\"", cfg_path); donecfg=1; } } else { fprintf( stderr, "camserv v%s - by Jon Travis ([email protected])\n", VERSION ); fprintf( stderr, "Syntax: %s <cfg file>\n", argv[0] ); fprintf( stderr, "Will try %s/camserv.cfg\n", DATDIR); if (!donecfg) { snprintf( cfg_path, sizeof( cfg_path ), "%s/camserv.cfg", DATDIR ); cfg_path[ sizeof( cfg_path ) - 1 ] = '\0'; camserv_log( "main", "Trying to read config file \"%s\": ", cfg_path); if( (camcfg = read_ccfg( cfg_path )) == NULL ){ camserv_log( "main", "Error reading config \"%s\": %s", cfg_path, strerror( errno )); } else { camserv_log( "main", "Success reading config \"%s\"", cfg_path); donecfg=1; } } } if (!donecfg) { camserv_log( "main", "Error finding config file, exit!"); return(-1); } /* If we took a single snapshot, we are all done */ if( snap_single( camcfg )) return 0; if( (localsocks = socket_unix_pair( SOCK_DGRAM )) == NULL ){ camserv_log( "main", "Error creating communication sockets between procs"); return -1; } /* Setup a temp file for making our shm */ strcpy( tmpbuf, "/tmp/CAMSERV_XXXXXX" ); if( (fd = mkstemp( tmpbuf )) == -1 ){ camserv_log( "main", "Couldn't create temporary file: %s", tmpbuf ); strcpy( tmpbuf, argv[ 0 ] ); /* Last resort */ } else { close( fd ); } shm_alloc = camconfig_query_def_int( camcfg, SEC_MAIN, "shm_alloc", PICTURE_MALLOC ); if( shm_alloc < PICTURE_MALLOC ) camserv_log( "main", "Allocated %d bytes for SHM [RISKY RISKY!]", shm_alloc); if( (Shmid = shm_setup( tmpbuf, /* Allocate generous ammount */ shm_alloc, &shm_segment) ) == -1 ){ socket_unix_pair_dest( localsocks ); return -1; } unlink( tmpbuf ); /* Start the picture taker thread */ CPid = picture_taker( shm_segment, PICTURE_MALLOC, camcfg, localsocks[ 0 ]); if( CPid == -1 ){ /* Failure setting up camerastuffs */ camserv_log( "main", "Picture taker could not be created!"); socket_unix_pair_dest( localsocks ); return -1; } if( main_loop( camcfg, localsocks[ 1 ], shm_segment ) == -1 ){ camserv_log( "main", "Main loop exited abnormally"); socket_unix_pair_dest( localsocks ); if( CPid != -1 ) kill( CPid, SIGINT ); return -1; } socket_unix_pair_dest( localsocks ); return 0; }
/* this creates the specified number of child processes and runs fn() in all of them */ static void create_procs(int nprocs, void (*fn)(struct child_struct *, const char *)) { int nclients = nprocs * options.clients_per_process; int i, status; int synccount; struct timeval tv; gzFile *load; struct sembuf sbuf; double t; load = open_loadfile(); if (load == NULL) { exit(1); } if (nprocs < 1) { fprintf(stderr, "create %d procs? you must be kidding.\n", nprocs); return; } children = shm_setup(sizeof(struct child_struct)*nclients); if (!children) { printf("Failed to setup shared memory\n"); return; } memset(children, 0, sizeof(*children)*nclients); for (i=0;i<nclients;i++) { children[i].id = i; children[i].num_clients = nclients; children[i].cleanup = 0; children[i].directory = options.directory; children[i].starttime = timeval_current(); children[i].lasttime = timeval_current(); } if (atexit(sem_cleanup) != 0) { printf("can't register cleanup function on exit\n"); exit(1); } sbuf.sem_num = 0; if ( !(barrier = semget(IPC_PRIVATE,1,IPC_CREAT | S_IRUSR | S_IWUSR)) ) { printf("failed to create barrier semaphore \n"); } sbuf.sem_flg = SEM_UNDO; sbuf.sem_op = 1; if (semop(barrier, &sbuf, 1) == -1) { printf("failed to initialize the barrier semaphore\n"); exit(1); } sbuf.sem_flg = 0; for (i=0;i<nprocs;i++) { if (fork() == 0) { int j; setlinebuf(stdout); srandom(getpid() ^ time(NULL)); for (j=0;j<options.clients_per_process;j++) { nb_ops->setup(&children[i*options.clients_per_process + j]); } sbuf.sem_op = 0; if (semop(barrier, &sbuf, 1) == -1) { printf("failed to use the barrier semaphore in child %d\n",getpid()); exit(1); } fn(&children[i*options.clients_per_process], options.loadfile); _exit(0); } } synccount = 0; tv = timeval_current(); do { synccount = semctl(barrier,0,GETZCNT); t = timeval_elapsed(&tv); printf("%d of %d processes prepared for launch %3.0f sec\n", synccount, nprocs, t); if (synccount == nprocs) break; usleep(100*1000); } while (timeval_elapsed(&tv) < 30); if (synccount != nprocs) { printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount); return; } printf("releasing clients\n"); tv_start = timeval_current(); sbuf.sem_op = -1; if (semop(barrier, &sbuf, 1) == -1) { printf("failed to release barrier\n"); exit(1); } signal(SIGALRM, sig_alarm); alarm(PRINT_FREQ); for (i=0;i<nprocs;) { if (waitpid(0, &status, 0) == -1) continue; if (WEXITSTATUS(status) != 0) { printf("Child failed with status %d\n", WEXITSTATUS(status)); exit(1); } i++; } alarm(0); sig_alarm(SIGALRM); semctl(barrier,0,IPC_RMID); printf("\n"); report_latencies(); }