int main() { LIST *jlist = init_list(); assert(gen_job_id(jlist) == 1); assert(get_fg(jlist) == (job *)NULL); assert(jlist->size == 0); //print_bg(jlist); job *jb1 = tstjob(1); push(jlist, JOB, jb1); assert(gen_job_id(jlist) == 2); assert(get_fg(jlist) == jb1); //print_bg(jlist); job *jb2 = tstjob(3); push(jlist, JOB, jb2); assert(gen_job_id(jlist) == 2); job *jb3 = tstjob(2); push(jlist, JOB, jb3); assert(gen_job_id(jlist) == 4); push(jlist, JOB, tstjob(7)); assert(gen_job_id(jlist) == 4); print_bg(jlist); pop(jlist); assert(gen_job_id(jlist) == 4); assert(get_job_pgid(jlist, 11) == (job *)NULL); assert(get_job_pgid(jlist, 7) == jb2); assert(get_job_pgid(jlist, 3) == jb3); assert(get_job(jlist, 3) == jb2); assert(del_job(jlist, 7) == jb2); assert(get_job(jlist, 3) == (job *)NULL); assert(get_job(jlist, 55) == jb3); assert(get_job(jlist, 10) == (job *)NULL); assert(set_complete(jb2, 3) == 0); assert(set_complete(jb2, 4) == -1); assert(set_complete(jb3, 55) == 0); assert(((process *)jb2->pid_list->head->node_ptr)->complete == 0); assert(jb2->complete == 0); assert(jb3->complete == 1); assert(jlist->size == 2); assert(get_job_jid(jlist, 1) == jb1); assert(get_job_jid(jlist, 11) == (job *)NULL); printf("[job_handler_tst] All tests pass!\n"); return 0; }
int list_fragments(int fd, u64 flags, char *dir) { int ret; struct btrfs_ioctl_search_args args; struct btrfs_ioctl_search_key *sk = &args.key; int i; struct btrfs_ioctl_search_header *sh; unsigned long off = 0; int bgnum = 0; u64 bgstart = 0; u64 bglen = 0; u64 bgend = 0; u64 bgflags = 0; u64 bgused = 0; u64 saved_extent = 0; u64 saved_len = 0; int saved_color = 0; u64 last_end = 0; u64 areas = 0; long px; char name[1000]; FILE *html; int colors[COLOR_MAX]; gdImagePtr im = NULL; int black = 0; int width = 800; snprintf(name, sizeof(name), "%s/index.html", dir); html = fopen(name, "w"); if (!html) { printf("unable to create %s\n", name); exit(1); } fprintf(html, "<html><header>\n"); fprintf(html, "<title>Btrfs Block Group Allocation Map</title>\n"); fprintf(html, "<style type=\"text/css\">\n"); fprintf(html, "img {margin-left: 1em; margin-bottom: 2em;}\n"); fprintf(html, "</style>\n"); fprintf(html, "</header><body>\n"); memset(&args, 0, sizeof(args)); sk->tree_id = 2; sk->max_type = -1; sk->min_type = 0; sk->max_objectid = (u64)-1; sk->max_offset = (u64)-1; sk->max_transid = (u64)-1; /* just a big number, doesn't matter much */ sk->nr_items = 4096; while(1) { ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); if (ret < 0) { fprintf(stderr, "ERROR: can't perform the search\n"); goto out_close; } /* the ioctl returns the number of item it found in nr_items */ if (sk->nr_items == 0) break; off = 0; for (i = 0; i < sk->nr_items; i++) { int j; sh = (struct btrfs_ioctl_search_header *)(args.buf + off); off += sizeof(*sh); if (sh->type == BTRFS_BLOCK_GROUP_ITEM_KEY) { struct btrfs_block_group_item *bg; if (im) { push_im(im, name, dir); im = NULL; print_bg(html, name, bgstart, bglen, bgused, bgflags, areas); } ++bgnum; bg = (struct btrfs_block_group_item *) (args.buf + off); bgflags = btrfs_block_group_flags(bg); bgused = btrfs_block_group_used(bg); printf("found block group %lld len %lld " "flags %lld\n", sh->objectid, sh->offset, bgflags); if (!(bgflags & flags)) { /* skip this block group */ sk->min_objectid = sh->objectid + sh->offset; sk->min_type = 0; sk->min_offset = 0; break; } im = gdImageCreate(width, (sh->offset / 4096 + 799) / width); black = gdImageColorAllocate(im, 0, 0, 0); for (j = 0; j < ARRAY_SIZE(colors); ++j) colors[j] = black; init_colors(im, colors); bgstart = sh->objectid; bglen = sh->offset; bgend = bgstart + bglen; snprintf(name, sizeof(name), "bg%d.png", bgnum); last_end = bgstart; if (saved_len) { px = (saved_extent - bgstart) / 4096; for (j = 0; j < saved_len / 4096; ++j) { int x = (px + j) % width; int y = (px + j) / width; gdImageSetPixel(im, x, y, saved_color); } last_end += saved_len; } areas = 0; saved_len = 0; } if (im && sh->type == BTRFS_EXTENT_ITEM_KEY) { int c; struct btrfs_extent_item *item; item = (struct btrfs_extent_item *) (args.buf + off); if (use_color) c = colors[get_color(item, sh->len)]; else c = black; if (sh->objectid > bgend) { printf("WARN: extent %lld is without " "block group\n", sh->objectid); goto skip; } if (sh->objectid == bgend) { saved_extent = sh->objectid; saved_len = sh->offset; saved_color = c; goto skip; } px = (sh->objectid - bgstart) / 4096; for (j = 0; j < sh->offset / 4096; ++j) { int x = (px + j) % width; int y = (px + j) / width; gdImageSetPixel(im, x, y, c); } if (sh->objectid != last_end) ++areas; last_end = sh->objectid + sh->offset; skip:; } off += sh->len; /* * record the mins in sk so we can make sure the * next search doesn't repeat this root */ sk->min_objectid = sh->objectid; sk->min_type = sh->type; sk->min_offset = sh->offset; } sk->nr_items = 4096; /* increment by one */ if (++sk->min_offset == 0) if (++sk->min_type == 0) if (++sk->min_objectid == 0) break; } if (im) { push_im(im, name, dir); print_bg(html, name, bgstart, bglen, bgused, bgflags, areas); } if (use_color) { fprintf(html, "<p>"); fprintf(html, "data - dark red, "); fprintf(html, "fs tree - black, "); fprintf(html, "extent tree - green, "); fprintf(html, "csum tree - blue, "); fprintf(html, "reloc tree - grey, "); fprintf(html, "other trees - red, "); fprintf(html, "unknown tree - dark grey"); fprintf(html, "</p>"); } fprintf(html, "</body></html>\n"); out_close: fclose(html); return ret; }
int main(int argc, char** argv) { ssize_t cmd_size; int i; int pipefd[2], fin, fout; char buf[BUFFER_SIZE]; char *cmd; char **args; job *j; job_list = init_list(); msg_q = init_list(); pid_t pgid; /* initial setup */ shell_pgid = getpgid(0); sync_pwd(); /* disable (ignore) job control signals */ signal(SIGTTOU, SIG_IGN); signal(SIGTTIN, SIG_IGN); signal(SIGTERM, SIG_IGN); signal(SIGTSTP, SIG_IGN); signal(SIGINT, SIG_IGN); set_handler(); /*** Shell loop ***/ for(;;) { /* Print out queued messages */ for(i=0;i<msg_q->size;i++) { message *m = (message *) get(msg_q, i); printf("%s: %s\n", m->status ? "Finished" : "Stopped", m->msg); } // clear the queue while(msg_q->size > 0) free(del(msg_q,0)); /* TODO: shell print macro */ printf("%s %s %s ", SHELL, pwd, PROMPT); fflush(stdout); do cmd_size = read(STDIN_FILENO, buf, BUFFER_SIZE); while(cmd_size == -1 && errno == EINTR); // ignore system call interrupts if(cmd_size == -1) { perror("read"); continue; } else if(cmd_size == 0) { // EOF (quit) write(STDOUT_FILENO, EXIT_MSG, STRING_SIZE(EXIT_MSG)); cleanup(); _exit(EXIT_SUCCESS); } else if(cmd_size == 1 && buf[0] == '\n') { continue; } if(buf[cmd_size-1] != '\n') { // overflow write(STDOUT_FILENO, OFLOW_MSG, STRING_SIZE(OFLOW_MSG)); pflush(); continue; } buf[cmd_size-1] = '\0'; // strip the newline j = parse(buf); if(j == (job *) NULL) { printf("Invalid redirections you moron!\n"); continue; } args = j->cmds[0]; if (!my_strcmp(args[0], "fg")) { do_fg(args); free_job(j); continue; } else if (!my_strcmp(args[0], "bg")) { do_bg(args); free_job(j); continue; } else if (!my_strcmp(args[0], "jobs")) { print_bg(job_list); free_job(j); continue; } else if (!my_strcmp(args[0], "cd")) { do_cd(args); free_job(j); continue; } j->job_id = gen_job_id(job_list); j->running = 1; j->complete = 0; push(job_list, JOB, (void *)j); pgid = 0; // set the job pgid to be the first child's pid fin = STDIN_FILENO; for(i=0;i<(j->numcmds);i++) { args = j->cmds[i]; cmd = args[0]; if(i + 1 < (j->numcmds)) { // not last process in job //printf("Creating a pipe!\n"); if(pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } fout = pipefd[1]; } else fout = STDOUT_FILENO; /*printf("Forking %s\n", cmd); k = 0; while( args[k] != (char *) NULL ) { printf("\targv[%d] = %s\n", k, args[k]); ++k; }*/ pid = fork(); if(pid == -1) { perror("fork"); continue; } /*** CHILD ***/ if(!pid) { // < if(j->fin[i] != (char *) NULL) { if( (fin = open(j->fin[i], O_RDONLY)) == -1 ) { perror("open"); } } // > if(j->fout[i] != (char *) NULL) { if( (fout = open(j->fout[i], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1 ) { perror("open"); } } exec_process(cmd, args, pgid, fin, fout, j->fg); } /*** PARENT ***/ else { if(!pgid) { // set to first child's pid (process group leader) pgid = pid; j->pgid = pgid; //printf("Set job's pgid to %d\n", pgid); } process *proc = malloc(sizeof(process)); proc->complete = 0; proc->pid = pid; proc->pgid = pgid; push(j->pid_list, PROCESS, (void *)proc); if( setpgid(pid, pgid) == -1 ) { perror("setpgid"); _exit(EXIT_FAILURE); } } if(fin != STDIN_FILENO) close(fin); if(fout != STDOUT_FILENO) close(fout); fin = pipefd[0]; } //print_jobs(); if(j->fg) { // foreground // give terminal control to job if( tcsetpgrp(STDIN_FILENO, pgid) == -1 ) { perror("tcsetpgrp"); _exit(EXIT_FAILURE); } // wait for job to finish jwait(j); // give pmsh terminal control again if( tcsetpgrp(STDIN_FILENO, shell_pgid) == -1 ) { perror("tcsetpgrp"); _exit(EXIT_FAILURE); } } else { // background printf("Running: %s\n", j->rawcmd); } } }
int main() { printf("Variante %d: %s\n", VARIANTE, VARIANTE_STRING); #ifdef USE_GUILE scm_init_guile(); /* register "executer" function in scheme */ scm_c_define_gsubr("executer", 1, 0, 0, executer_wrapper); #endif while (1) { //struct cmdline *l; char *line=0; //int i, j; char *prompt = "ensishell>"; /* Readline use some internal memory structure that can not be cleaned at the end of the program. Thus one memory leak per command seems unavoidable yet */ line = readline(prompt); if (line == 0 || ! strncmp(line,"exit", 4)) { terminate(line); } if(!strncmp(line,"jobs",4)) { print_bg(liste_children_bg); } #ifdef USE_GNU_READLINE add_history(line); #endif #ifdef USE_GUILE /* The line is a scheme command */ if (line[0] == '(') { char catchligne[strlen(line) + 256]; sprintf(catchligne, "(catch #t (lambda () %s) (lambda (key . parameters) (display \"mauvaise expression/bug en scheme\n\")))", line); scm_eval_string(scm_from_locale_string(catchligne)); free(line); continue; } #endif update_bg(&liste_children_bg); executer(line); /* parsecmd free line and set it up to 0 */ //l = parsecmd( & line); ///* If input stream closed, normal termination */ //if (!l) { // terminate(0); //} //if (l->err) { // /* Syntax error, read another command */ // printf("error: %s\n", l->err); // continue; //} //if (l->in) printf("in: %s\n", l->in); //if (l->out) printf("out: %s\n", l->out); //if (l->bg) printf("background (&)\n"); ///* Display each command of the pipe */ //for (i=0; l->seq[i]!=0; i++) { // char **cmd = l->seq[i]; // printf("seq[%d]: ", i); // for (j=0; cmd[j]!=0; j++) { // printf("'%s' ", cmd[j]); // } // printf("\n"); //} } }
RunParams *init_params(char *fname_ini) { FILE *fi; int n,ii,stat,ibin; double *x,*a,*y,dchi; RunParams *par=param_new(); par->cpar=csm_params_new(); read_parameter_file(fname_ini,par); csm_unset_gsl_eh(); if(par->has_bg) { double hub; csm_background_set(par->cpar,par->om,par->ol,par->ob,par->w0,par->wa,par->h0,D_TCMB); par->chi_horizon=csm_radial_comoving_distance(par->cpar,0.); par->chi_kappa=csm_radial_comoving_distance(par->cpar,1./(1+par->z_kappa)); par->chi_isw=csm_radial_comoving_distance(par->cpar,1./(1+par->z_isw)); hub=csm_hubble(par->cpar,1.); par->prefac_lensing=1.5*hub*hub*par->om; n=(int)(par->chi_horizon/par->dchi)+1; dchi=par->chi_horizon/n; par->dchi=dchi; x=(double *)my_malloc(n*sizeof(double)); a=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); for(ii=0;ii<n;ii++) x[ii]=dchi*ii; printf("Setting up background splines\n"); //Set chi <-> a correspondence const gsl_root_fdfsolver_type *T=gsl_root_fdfsolver_newton; gsl_root_fdfsolver *s=gsl_root_fdfsolver_alloc(T); double a_old=1.0; for(ii=0;ii<n;ii++) a[ii]=a_of_chi(x[ii],par->cpar,&a_old,s); gsl_root_fdfsolver_free(s); par->aofchi=spline_init(n,x,a,1.0,0.0); //Compute redshift for(ii=0;ii<n;ii++) y[ii]=1./a[ii]-1; par->zofchi=spline_init(n,x,y,y[0],y[n-1]); //Compute hubble scale for(ii=0;ii<n;ii++) y[ii]=csm_hubble(par->cpar,a[ii]); par->hofchi=spline_init(n,x,y,y[0],y[n-1]); //Compute growth factor double g0=csm_growth_factor(par->cpar,1.0); for(ii=0;ii<n;ii++) y[ii]=csm_growth_factor(par->cpar,a[ii])/g0; par->gfofchi=spline_init(n,x,y,1.,0.); //Compute growth rate for(ii=0;ii<n;ii++) y[ii]=csm_f_growth(par->cpar,a[ii]); par->fgofchi=spline_init(n,x,y,y[0],1.); free(x); free(a); free(y); } //Allocate power spectra if(par->do_nc) { par->cl_dd=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_shear) { par->cl_d1l2=(double *)my_malloc((par->lmax+1)*sizeof(double)); par->cl_d2l1=(double *)my_malloc((par->lmax+1)*sizeof(double)); } if(par->do_cmblens) par->cl_dc=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_isw) par->cl_di=(double *)my_malloc((par->lmax+1)*sizeof(double)); } if(par->do_shear) { par->cl_ll=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_cmblens) par->cl_lc=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_isw) par->cl_li=(double *)my_malloc((par->lmax+1)*sizeof(double)); } if(par->do_cmblens) { par->cl_cc=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_isw) par->cl_ci=(double *)my_malloc((par->lmax+1)*sizeof(double)); } if(par->do_isw) par->cl_ii=(double *)my_malloc((par->lmax+1)*sizeof(double)); if(par->do_w_theta) { if(par->do_nc) { par->wt_dd=(double *)my_malloc(par->n_th*sizeof(double)); if(par->do_shear) { par->wt_d1l2=(double *)my_malloc(par->n_th*sizeof(double)); par->wt_d2l1=(double *)my_malloc(par->n_th*sizeof(double)); } if(par->do_cmblens) par->wt_dc=(double *)my_malloc(par->n_th*sizeof(double)); if(par->do_isw) par->wt_di=(double *)my_malloc(par->n_th*sizeof(double)); } if(par->do_shear) { par->wt_ll_pp=(double *)my_malloc(par->n_th*sizeof(double)); par->wt_ll_mm=(double *)my_malloc(par->n_th*sizeof(double)); if(par->do_cmblens) par->wt_lc=(double *)my_malloc(par->n_th*sizeof(double)); if(par->do_isw) par->wt_li=(double *)my_malloc(par->n_th*sizeof(double)); } if(par->do_cmblens) { par->wt_cc=(double *)my_malloc(par->n_th*sizeof(double)); if(par->do_isw) par->wt_ci=(double *)my_malloc(par->n_th*sizeof(double)); } if(par->do_isw) par->wt_ii=(double *)my_malloc(par->n_th*sizeof(double)); } if(par->do_nc || par->do_shear || par->do_cmblens || par->do_isw) csm_set_linear_pk(par->cpar,par->fname_pk,D_LKMIN,D_LKMAX,0.01,par->ns,par->s8); if(par->do_nc || par->do_shear) { par->wind_0=my_malloc(2*sizeof(SplPar *)); for(ibin=0;ibin<2;ibin++) { printf("Reading window function %s\n",par->fname_window[ibin]); fi=my_fopen(par->fname_window[ibin],"r"); n=my_linecount(fi); rewind(fi); //Read unnormalized window x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); for(ii=0;ii<n;ii++) { stat=fscanf(fi,"%lE %lE",&(x[ii]),&(y[ii])); if(stat!=2) report_error(1,"Error reading file, line %d\n",ii+1); } fclose(fi); par->wind_0[ibin]=spline_init(n,x,y,0.,0.); //Normalize window double norm,enorm; gsl_function F; gsl_integration_workspace *w=gsl_integration_workspace_alloc(1000); F.function=&speval_bis; F.params=par->wind_0[ibin]; gsl_integration_qag(&F,x[0],x[n-1],0,1E-4,1000,GSL_INTEG_GAUSS41,w,&norm,&enorm); gsl_integration_workspace_free(w); for(ii=0;ii<n;ii++) y[ii]/=norm; spline_free(par->wind_0[ibin]); par->wind_0[ibin]=spline_init(n,x,y,0.,0.); double zmin,zmax; double ymax=-1000; for(ii=0;ii<n;ii++) { if(y[ii]>ymax) ymax=y[ii]; } ii=0; while(y[ii]<1E-3*ymax) ii++; zmin=x[ii]; ii=n-1; while(y[ii]<1E-3*ymax) ii--; zmax=x[ii]; par->chimin_nc[ibin]=csm_radial_comoving_distance(par->cpar,1./(1+zmin)); par->chimax_nc[ibin]=csm_radial_comoving_distance(par->cpar,1./(1+zmax)); #ifdef _DEBUG printf("%d %lE %lE %lE %lE\n", ibin,zmin,zmax,par->chimin_nc[ibin],par->chimax_nc[ibin]); #endif //_DEBUG free(x); free(y); } } if(par->do_nc) { if(par->has_dens==1) { printf("Reading bias function %s\n",par->fname_bias); fi=my_fopen(par->fname_bias,"r"); n=my_linecount(fi); rewind(fi); //Read bias x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); for(ii=0;ii<n;ii++) { stat=fscanf(fi,"%lE %lE",&(x[ii]),&(y[ii])); if(stat!=2) report_error(1,"Error reading file, line %d\n",ii+1); } fclose(fi); par->bias=spline_init(n,x,y,y[0],y[n-1]); free(x); free(y); } if(par->has_lensing==1) { printf("Reading s-bias function %s\n",par->fname_sbias); fi=my_fopen(par->fname_sbias,"r"); n=my_linecount(fi); rewind(fi); //Read s-bias x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); for(ii=0;ii<n;ii++) { stat=fscanf(fi,"%lE %lE",&(x[ii]),&(y[ii])); if(stat!=2) report_error(1,"Error reading file, line %d\n",ii+1); } fclose(fi); par->sbias=spline_init(n,x,y,y[0],y[n-1]); free(x); free(y); printf("Computing lensing magnification window function\n"); par->wind_M=my_malloc(2*sizeof(SplPar *)); for(ibin=0;ibin<2;ibin++) { double dchi_here; double zmax=par->wind_0[ibin]->xf; double chimax=csm_radial_comoving_distance(par->cpar,1./(1+zmax)); n=(int)(chimax/par->dchi)+1; dchi_here=chimax/n; x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); #ifdef _HAVE_OMP #pragma omp parallel default(none) shared(n,x,y,par,dchi_here,ibin) { #endif //_HAVE_OMP int j; #ifdef _HAVE_OMP #pragma omp for #endif //_HAVE_OMP for(j=0;j<n;j++) { x[j]=dchi_here*j; y[j]=window_magnification(x[j],par,ibin); } //end omp for #ifdef _HAVE_OMP } //end omp parallel #endif //_HAVE_OMP par->wind_M[ibin]=spline_init(n,x,y,y[0],0); free(x); free(y); } } } if(par->do_shear) { if(par->has_intrinsic_alignment==1) { printf("Reading IA bias function %s\n",par->fname_abias); fi=my_fopen(par->fname_abias,"r"); n=my_linecount(fi); rewind(fi); //Read bias x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); for(ii=0;ii<n;ii++) { stat=fscanf(fi,"%lE %lE",&(x[ii]),&(y[ii])); if(stat!=2) report_error(1,"Error reading file, line %d\n",ii+1); } fclose(fi); par->abias=spline_init(n,x,y,y[0],y[n-1]); free(x); free(y); } printf("Computing lensing window function\n"); par->wind_L=my_malloc(2*sizeof(SplPar *)); for(ibin=0;ibin<2;ibin++) { double dchi_here; double zmax=par->wind_0[ibin]->xf; double chimax=csm_radial_comoving_distance(par->cpar,1./(1+zmax)); n=(int)(chimax/par->dchi)+1; dchi_here=chimax/n; x=(double *)my_malloc(n*sizeof(double)); y=(double *)my_malloc(n*sizeof(double)); #ifdef _HAVE_OMP #pragma omp parallel default(none) shared(n,x,y,par,dchi_here,ibin) { #endif //_HAVE_OMP int j; #ifdef _HAVE_OMP #pragma omp for #endif //_HAVE_OMP for(j=0;j<n;j++) { x[j]=dchi_here*j; y[j]=window_lensing(x[j],par,ibin); } #ifdef _HAVE_OMP } //end omp parallel #endif //_HAVE_OMP par->wind_L[ibin]=spline_init(n,x,y,y[0],0); free(x); free(y); } } #ifdef _DEBUG print_bg(par); #endif //_DEBUG return par; }