void sorted_insert_slist_front(slist_t* slist, void* object, int ascending, int (*compare)(void*,void*)){ void *cur=NULL; cur=head_slist(slist); /* Search */ if (ascending){ // Find while(cur!=NULL && compare(cur,object)<0) { cur=next_slist(slist,cur); } }else{ // Find while(cur!=NULL && compare(cur,object)>0) { cur=next_slist(slist,cur); } } insert_before_slist(slist,cur,object); }
void sort_slist(slist_t* slist, int ascending, int (*compare)(void*,void*)) { void *cur=NULL,*selected_node=NULL,*prev_selected=NULL; int i=0; /* Check if the list is already trivially sorted */ if (slist->size<=1) return; cur=head_slist(slist); /* Insertion sort */ for (i=0; i<slist->size-1 && cur!=NULL; i++) { /* Search */ selected_node=cur; if (ascending) { // Search for min while(cur!=NULL) { if (compare(cur,selected_node)<0) selected_node=cur; cur=next_slist(slist,cur); } } else { // Search for max while(cur!=NULL) { if (compare(cur,selected_node)>0) selected_node=cur; cur=next_slist(slist,cur); } } remove_slist(slist,selected_node); insert_after_slist(slist,prev_selected,selected_node); prev_selected=selected_node; cur=next_slist(slist,selected_node); } }
static void print_task_log_registers(task_t* task) { sched_log_t* cur=head_slist(&task->sched_regs); sched_log_t* next; /* Disable debug mode for this */ debug_mode=FALSE; while(!is_empty_slist(&task->sched_regs)) { write_cpu_log(cur->cpu,"%s\t%d\t%d\t%s\n",task->task_name,cur->when,cur->when+cur->how_long,strstate[cur->state]); /* Keep track of next, since we will remove cur from the list */ next=next_slist(&task->sched_regs,cur); remove_slist(&task->sched_regs,cur); free(cur); cur=next;// Go to next item } }
static task_t* pick_next_task_lote(runqueue_t* rq,int cpu) { task_t* t=head_slist(&rq->tasks); //List sorted by CPU burst lenght (just pick the first one) if (t) { /* Current is not on the rq*/ remove_slist(&rq->tasks,t); t->on_rq=FALSE; rq->cur_task=t; } srand(time(NULL)); int i; for(i=0; i<rq->nr_runnable; i++){ next_slist(&rq->tasks, t); t->prio = i; } return t; }
static task_t* steal_task_lote(runqueue_t* rq,int cpu){ task_t* t=rq->cur_task; int loteria = rand()%100; int i=0, encontrado=0; while(encontrado==0 && i<rq->nr_runnable){ next_slist(&rq->tasks, t); if(t->prio == loteria) encontrado=1; } //task_t* t=tail_slist(&rq->tasks); if (t) { remove_slist(&rq->tasks,t); t->on_rq=FALSE; rq->nr_runnable--; } return t; }
/* This function enables to start up the simulator with * a given scheduling algorithm and specified task list. * */ void sched_start(slist_t* task_list, struct sched_class* sc) { task_t *cur,*next; int task_cnt=0; pthread_t sim_cpu[MAX_CPUS]; long cpu=0; int i=0; sched_init(sc); /* Traverse the task_list to wake up new tasks*/ cur=head_slist(task_list); while(task_cnt<task_list->size) { cur->last_cpu=task_cnt%nr_cpus; /* Hack to automatize load balancing at the beginning */ if (init_task_sched(cur)) { perror("Couldn't initialize class specific data for thread "); exit(1); } schedule_wake_up_new_task(cur); /*Just inserts the sched_event in the event_queue */ if (debug_mode) print_task(cur); cur=next_slist(task_list,cur); task_cnt++; } if (debug_mode) { printf("Scheduler initialized. Press ENTER to start simulation.\n"); getchar(); } /* Create per-CPU simulation threads */ for (cpu=0; cpu<nr_cpus; cpu++) pthread_create(&sim_cpu[cpu],NULL,sched_cpu,(void*)cpu); /* Wait for completion of per-CPU simulation threads*/ for (cpu=0; cpu<nr_cpus; cpu++) pthread_join(sim_cpu[cpu],NULL); /* Print log information to aid in * the construction of the gantt diagram * */ cur=head_slist(task_list); for(i=0; i<task_cnt; i++) { print_task_log_registers(cur); cur=next_slist(task_list,cur); } /* Free up class-specific task data and task structures */ cur=head_slist(task_list); for(i=0; i<task_cnt; i++) { next=next_slist(task_list,cur); if (active_sched_class->task_free) active_sched_class->task_free(cur); free(cur); cur=next; } /* Free up sched-class specific resources if necessary */ if (active_sched_class->sched_destroy) active_sched_class->sched_destroy(); printf("Simulation completed\n"); sched_terminate(0); }
specific functions. */ unsigned long ModInitialize(void) { IXFModule *ixf; IXFSYSDEP *sysdep; l4exec_section_t *section; slist_t *r; l4_uint32_t rights; l4dm_dataspace_t ds; l4_addr_t addr; l4_addr_t size; l4_offs_t offset; l4_threadid_t pager; unsigned i; int rc; int (*dl_init)(CORBA_Environment *e); void *execsym, *hdl; struct module_rec * new_module_el; static IXFMODULEENTRY *entries; // Create root node module_root.mod_name = "root"; module_root.module_struct = 0; module_root.next = 0; // Register DL.DLL ixf=malloc(sizeof(IXFModule)); ixf->name = (char *)malloc(4); strcpy(ixf->name, "DL"); ixf->Load = NULL; ixf->Fixup = NULL; ixf->FormatStruct = NULL; if (rc = dl_get_funcs (&ixf->cbEntries, &ixf->Entries)) return rc; ixf->cbModules=0; ixf->Modules=NULL; ixf->cbFixups=0; ixf->Stack=NULL; ixf->EntryPoint=NULL; ixf->PIC=0; /* add execsrv sections from l4env infopage as DL.DLL sections list */ sysdep = (IXFSYSDEP *)malloc(sizeof(IXFSYSDEP)); memset(sysdep, 0, sizeof(IXFSYSDEP)); ixf->hdlSysDep = sysdep; sysdep->secnum = 0; sysdep->seclist = 0; #if 0 r = next_slist(0); sysdep->secnum = 1; sysdep->seclist = r; section = r->section; rc = l4rm_lookup_region(&_prog_img_start, &addr, &size, &ds, &offset, &pager); if (rc < 0) return rc; section->addr = addr; section->size = size; section->info.type = L4_DSTYPE_READ | L4_DSTYPE_WRITE | L4_DSTYPE_EXECUTE; section->info.id = 0; rights = L4DM_READ | L4DM_WRITE; rc = l4dm_mem_open(L4DM_DEFAULT_DSM, section->size, 4096, rights, "", §ion->ds); if (rc) { LOG("error allocating dataspace!"); return rc; } rc = l4rm_attach(§ion->ds, section->size, 0, rights, &addr); if (rc) { LOG("error attaching dataspace %x", section->ds); return rc; } memmove(addr, section->addr, section->size); l4rm_detach(addr); //// .... region = l4rm_get_region_list(); for (i = 0, r = 0; region; i++, region = region->next) { LOG("addr=%x", region->start); LOG("end=%x", region->end); LOG("flags=%x", region->flags); if (!(region->flags & REGION_DATASPACE)) continue; LOG("ds=%x", region->data.ds.ds.id); if ((void *)region->start <= dl_init && dl_init <= (void *)region->end) { LOG("is code section"); s = r; r = next_slist(r); if (!s) sysdep->seclist = r; section = r->section; section->addr = region->start; section->size = region->end - region->start; section->ds = region->data.ds.ds; section->info.type = L4_DSTYPE_READ | L4_DSTYPE_EXECUTE; section->info.id = 0; } else if ((void *)region->start <= execsym && execsym <= (void *)region->end) { LOG("is data section"); s = r; r = next_slist(r); if (!s) sysdep->seclist = r; section = r->section; section->addr = region->start; section->size = region->end - region->start; section->ds = region->data.ds.ds; section->info.type = L4_DSTYPE_READ | L4_DSTYPE_WRITE; section->info.id = 1; } else continue; rights = L4DM_READ | L4DM_WRITE; rc = l4dm_mem_open(L4DM_DEFAULT_DSM, section->size, 4096, rights, "", §ion->ds); if (rc) { LOG("error allocating dataspace!"); return rc; } rc = l4rm_attach(§ion->ds, section->size, 0, rights, &addr); if (rc) { LOG("error attaching dataspace %x", section->ds); return rc; } memmove(addr, section->addr, section->size); l4rm_detach(addr); } #endif new_module_el = ModRegister("DL", ixf, 0); //new_module_el->load_status = DONE_LOADING;