int main(void){ root = (proc_tree*)malloc(sizeof(proc_tree)); root=NULL; //Open file aznd ready for reading FILE *f; f = fopen("processes_tree.txt","r"); char line[1024]; const char* s = ", "; char *token; char* t_parent[256]; char* t_name[256]; int* t_data[4]; //Read in CSV file while(fgets(line,1024,f)){ token = strtok(line, s); for(int i=0;i<4;i++) { if(i==0) { strcpy(t_parent,token); } else if(i==1){ token = strtok(NULL, s); strcpy(t_name,token); } else{ token = strtok(NULL, s); t_data[i]=atoi(token); } } //Create process proc new_proc = (proc){.parent="",.name="",.priority=t_data[2],.memory=t_data[3]}; strcpy(new_proc.parent,t_parent); strcpy(new_proc.name,t_name); proc_tree* new_tree = (proc_tree*)malloc(sizeof(proc_tree)); new_tree->process = new_proc; new_tree->left=NULL; new_tree->right=NULL; printf("Adding: %s\n",new_proc.name); add_proc(new_tree); } }
static void read_procs(void) { DIR *proc_dir, *task_dir; struct dirent *pid_dir, *tid_dir; char filename[64]; FILE *file; int proc_num; struct proc_info *proc; pid_t pid, tid; int i; proc_dir = opendir("/proc"); if (!proc_dir) die("Could not open /proc.\n"); new_procs = calloc(INIT_PROCS * (threads ? THREAD_MULT : 1), sizeof(struct proc_info *)); num_new_procs = INIT_PROCS * (threads ? THREAD_MULT : 1); file = fopen("/proc/stat", "r"); if (!file) die("Could not open /proc/stat.\n"); fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime, &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime); fclose(file); proc_num = 0; while ((pid_dir = readdir(proc_dir))) { if (!isdigit(pid_dir->d_name[0])) continue; pid = atoi(pid_dir->d_name); struct proc_info cur_proc; if (!threads) { proc = alloc_proc(); proc->pid = proc->tid = pid; sprintf(filename, "/proc/%d/stat", pid); read_stat(filename, proc); sprintf(filename, "/proc/%d/cmdline", pid); read_cmdline(filename, proc); sprintf(filename, "/proc/%d/status", pid); read_status(filename, proc); read_policy(pid, proc); proc->num_threads = 0; } else { sprintf(filename, "/proc/%d/cmdline", pid); read_cmdline(filename, &cur_proc); sprintf(filename, "/proc/%d/status", pid); read_status(filename, &cur_proc); proc = NULL; } sprintf(filename, "/proc/%d/task", pid); task_dir = opendir(filename); if (!task_dir) continue; while ((tid_dir = readdir(task_dir))) { if (!isdigit(tid_dir->d_name[0])) continue; if (threads) { tid = atoi(tid_dir->d_name); proc = alloc_proc(); proc->pid = pid; proc->tid = tid; sprintf(filename, "/proc/%d/task/%d/stat", pid, tid); read_stat(filename, proc); read_policy(tid, proc); strcpy(proc->name, cur_proc.name); proc->uid = cur_proc.uid; proc->gid = cur_proc.gid; add_proc(proc_num++, proc); } else { proc->num_threads++; } } closedir(task_dir); if (!threads) add_proc(proc_num++, proc); } for (i = proc_num; i < num_new_procs; i++) new_procs[i] = NULL; closedir(proc_dir); }
void timer_handler() { uint64_t tmp = usec; uint32_t tmpSec = 0; uint32_t tmpMin = 0; uint32_t sec = 0; uint32_t min = 0; uint32_t hour = 0; if(tmp >= 100) { sec = tmp / 100; tmpSec = sec % 60; } if(sec >= 60) { min = sec / 60; tmpMin = min % 60; } if(min >= 60) { hour = min / 60; } tmp %= 100; tmp += (tmpSec*100); tmp += (tmpMin*10000); tmp += (hour*1000000); time_write(tmp); usec++; pic_sendEOI(32); offset = 0; colon = 0; //---------------------- scheduler -------------------------- uint64_t cur_rsp; task_struct *next_proc; task_struct *prev_proc = cur_proc; //when scheduler is on if(schedule_flag) { sleep_cnt_update(); if(!cur_proc) { next_proc = get_next_proc(); //printf("next_proc: %p\n", next_proc); //printf("next_proc->proc_name: %s\n", next_proc->proc_name); //printf("next_proc->rsp: %p\n", next_proc->rsp); //context_switch(next_proc); load_cr3(next_proc->mm_struct->pgd_t); //in_rsp(next_proc->rsp); if (next_proc->mode == USER) { //tss.rsp0 = (uint64_t)&next_proc->k_stack[511]; //the top addr of the stack //switch_to_ring3(); } } else { //store the rsp position before switch cur_rsp = out_rsp(); prev_proc->rsp = cur_rsp; add_proc(prev_proc); next_proc = get_next_proc(); //context_switch(next_proc); load_cr3(next_proc->mm_struct->pgd_t); //in_rsp(next_proc->rsp); if (next_proc->mode == USER) { //tss.rsp0 = (uint64_t)&next_proc->k_stack[511]; //the top addr of the stack //switch_to_ring3(); } } } }
void _CONNECT(int sd,proc_data* data){ printf("CONNECT(%d) (QUEUESIZE :%d)\n",sd,queue_size()); printf(" (NUM OF PROCS:%d)\n",dem.procCounter); proc* p; int i; if(data->pos == -1){ dem.procCounter++; p = create_proc(sd); add_proc(p); p->data = (proc_data*)malloc(sizeof(proc_data)); memcpy(p->data,data,sizeof(proc_data)); if(dem.procCounter > MAXPROC){ if(!(p->data->flag&CANMIG)){ dem.staying_procs ++; p->queued = STAYED_QUEUED; }else{ p->queued = QUEUED; } return; } }else{ p = get_proc(sd); memcpy(p->data,data,sizeof(proc_data)); } printf("\tPID : %d\n",p->data->pid); size_t _mem; size_t _req; size_t _sym; nvmlReturn_t res; nvmlMemory_t mem; for(i = 0 ; i < dem.ndev ; i ++){ if(!(p->data->flag&CANMIG)) if(dem.flags[i].stayed) continue; _mem = p->data->mem; _req = p->data->req; _sym = p->data->sym; res = nvmlDeviceGetMemoryInfo(dem.devs[i],&mem); #if 0 printf("mem.free : %lu\n",mem.free); printf("reserved : %lu\n",dem.flags[i].reserved); printf("_mem : %lu\n",_mem); printf("_req : %lu\n",_req); #endif if(p->data->pos == i){ if(mem.free > _req + dem.flags[i].reserved + M64){ printf("\tGOAHEAD(%d)\n",i); if(!(p->data->flag&CANMIG)) dem.flags[i].stayed = 1; dem.flags[i].reserved += _req; MSEND(sd,CONNECT,0,0,i,0,0); return ; } }else{ if(mem.free > _mem + dem.flags[i].reserved + M64){ printf("\tGOAHEAD(%d)*\n",i); if(!(p->data->flag&CANMIG)) dem.flags[i].stayed = 1; dem.flags[p->data->pos].reserved -= ( _mem - _req ); dem.flags[i].reserved += _mem; p->data->pos = i; MSEND(sd,CONNECT,0,0,i,0,0); return ; } } } printf("\tOOPS\n"); printf("mem.free : %lu\n",mem.free); printf("_req : %lu\n",_req); if(!(p->data->flag&CANMIG)){ dem.staying_procs ++; p->queued = STAYED_QUEUED; printf("Queued staying procs[%d]\n",dem.staying_procs); }else{ p->queued = QUEUED; dem.flags[p->data->pos].reserved -= ( _mem -_req ); } }
static void prstat_scandir(DIR *procdir) { char *pidstr; pid_t pid; id_t lwpid; size_t entsz; long nlwps, nent, i; char *buf, *ptr; fds_t *fds; lwp_info_t *lwp; dirent_t *direntp; prheader_t header; psinfo_t psinfo; prusage_t usage; lwpsinfo_t *lwpsinfo; prusage_t *lwpusage; total_procs = 0; total_lwps = 0; total_cpu = 0; total_mem = 0; convert_zone(&zone_tbl); for (rewinddir(procdir); (direntp = readdir(procdir)); ) { pidstr = direntp->d_name; if (pidstr[0] == '.') /* skip "." and ".." */ continue; pid = atoi(pidstr); if (pid == 0 || pid == 2 || pid == 3) continue; /* skip sched, pageout and fsflush */ if (has_element(&pid_tbl, pid) == 0) continue; /* check if we really want this pid */ fds = fds_get(pid); /* get ptr to file descriptors */ if (read_procfile(&fds->fds_psinfo, pidstr, "psinfo", &psinfo, sizeof (psinfo_t)) != 0) continue; if (!has_uid(&ruid_tbl, psinfo.pr_uid) || !has_uid(&euid_tbl, psinfo.pr_euid) || !has_element(&prj_tbl, psinfo.pr_projid) || !has_element(&tsk_tbl, psinfo.pr_taskid) || !has_zone(&zone_tbl, psinfo.pr_zoneid)) { fd_close(fds->fds_psinfo); continue; } nlwps = psinfo.pr_nlwp + psinfo.pr_nzomb; if (nlwps > 1 && (opts.o_outpmode & (OPT_LWPS | OPT_PSETS))) { int rep_lwp = 0; if (read_procfile(&fds->fds_lpsinfo, pidstr, "lpsinfo", &header, sizeof (prheader_t)) != 0) { fd_close(fds->fds_psinfo); continue; } nent = header.pr_nent; entsz = header.pr_entsize * nent; ptr = buf = Malloc(entsz); if (pread(fd_getfd(fds->fds_lpsinfo), buf, entsz, sizeof (struct prheader)) != entsz) { fd_close(fds->fds_lpsinfo); fd_close(fds->fds_psinfo); free(buf); continue; } nlwps = 0; for (i = 0; i < nent; i++, ptr += header.pr_entsize) { /*LINTED ALIGNMENT*/ lwpsinfo = (lwpsinfo_t *)ptr; if (!has_element(&cpu_tbl, lwpsinfo->pr_onpro) || !has_element(&set_tbl, lwpsinfo->pr_bindpset)) continue; nlwps++; if ((opts.o_outpmode & (OPT_PSETS | OPT_LWPS)) == OPT_PSETS) { /* * If one of process's LWPs is bound * to a given processor set, report the * whole process. We may be doing this * a few times but we'll get an accurate * lwp count in return. */ add_proc(&psinfo); } else { if (rep_lwp == 0) { rep_lwp = 1; add_lwp(&psinfo, lwpsinfo, LWP_REPRESENT); } else { add_lwp(&psinfo, lwpsinfo, 0); } } } free(buf); if (nlwps == 0) { fd_close(fds->fds_lpsinfo); fd_close(fds->fds_psinfo); continue; } } else { if (!has_element(&cpu_tbl, psinfo.pr_lwp.pr_onpro) || !has_element(&set_tbl, psinfo.pr_lwp.pr_bindpset)) { fd_close(fds->fds_psinfo); continue; } add_proc(&psinfo); } if (!(opts.o_outpmode & OPT_MSACCT)) { total_procs++; total_lwps += nlwps; continue; } /* * Get more information about processes from /proc/pid/usage. * If process has more than one lwp, then we may have to * also look at the /proc/pid/lusage file. */ if ((opts.o_outpmode & OPT_LWPS) && (nlwps > 1)) { if (read_procfile(&fds->fds_lusage, pidstr, "lusage", &header, sizeof (prheader_t)) != 0) { fd_close(fds->fds_lpsinfo); fd_close(fds->fds_psinfo); continue; } nent = header.pr_nent; entsz = header.pr_entsize * nent; buf = Malloc(entsz); if (pread(fd_getfd(fds->fds_lusage), buf, entsz, sizeof (struct prheader)) != entsz) { fd_close(fds->fds_lusage); fd_close(fds->fds_lpsinfo); fd_close(fds->fds_psinfo); free(buf); continue; } for (i = 1, ptr = buf + header.pr_entsize; i < nent; i++, ptr += header.pr_entsize) { /*LINTED ALIGNMENT*/ lwpusage = (prusage_t *)ptr; lwpid = lwpusage->pr_lwpid; /* * New LWPs created after we read lpsinfo * will be ignored. Don't want to do * everything all over again. */ if ((lwp = lwpid_get(pid, lwpid)) == NULL) continue; lwp_update(lwp, pid, lwpid, lwpusage); } free(buf); } else { if (read_procfile(&fds->fds_usage, pidstr, "usage", &usage, sizeof (prusage_t)) != 0) { fd_close(fds->fds_lpsinfo); fd_close(fds->fds_psinfo); continue; } lwpid = psinfo.pr_lwp.pr_lwpid; if ((lwp = lwpid_get(pid, lwpid)) == NULL) continue; lwp_update(lwp, pid, lwpid, &usage); } total_procs++; total_lwps += nlwps; } fd_update(); }
int main(int argc,char* argv[]){ /**Initialize signal**/ signal(SIGINT ,_end_server); signal(SIGUSR1,_end_server); /**Initialize struct proc**/ init_proc(); /**Process becomes dem**/ pid_t process_id = 0; pid_t sid = 0; if(argc >= 2){ process_id = fork(); if(process_id < 0){ printf("fork failed ..\n"); exit(1); } if(process_id > 0){ exit(0); } umask(0); sid = setsid(); if(sid < 0){ exit(1); } close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); }else{ sid = getpid(); } /**Setup the log file**/ char log[32]; sprintf(log,"log.%u",sid); fp = fopen(log,"w+"); // printf("Start constructing deamon for Mobile CUDA processes\n"); /**Start Initialize nvidia management library from Here!!**/ nvmlReturn_t nres; int i; nres = nvmlInit(); if(nres != NVML_SUCCESS){ perror("Failed to initialize Nvidia Managerment Library...\n"); exit(-1); } nres = nvmlDeviceGetCount(&dem.ndev); if(nres != NVML_SUCCESS){ perror("Failed to get num of device...\n"); exit(-1); } dem.devs = (nvmlDevice_t*)malloc(sizeof(nvmlDevice_t)*dem.ndev); dem.flags = (dflag*)malloc(sizeof(dflag)*dem.ndev); for(i = 0 ; i < dem.ndev ; i ++){ nres = nvmlDeviceGetHandleByIndex(i,&dem.devs[i]); if(nres != NVML_SUCCESS){ perror("Failed to get device handle\n"); exit(-1); } dem.flags[i].sd = -1; dem.flags[i].flag = 0; } dem.procCounter = 0; // printf("Success to get handle of each device (num of dev == %d)\n",dem.ndev); /**Setup the socket**/ int len,rc,on = 1; int listen_sd,max_sd,new_sd; int desc_ready; int close_conn; struct sockaddr_un addr; struct timeval timeout; fd_set master_set,working_set; listen_sd = socket(AF_UNIX,SOCK_STREAM,0); if(listen_sd < 0){ perror("socket() failed\n"); exit(-1); } rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char*)&on,sizeof(on)); if(rc < 0){ perror("setsockopt() failed\n"); exit(-1); } unlink("mocu_server"); memset(&addr,0,sizeof(addr)); addr.sun_family = AF_UNIX; strcpy(addr.sun_path,"mocu_server"); rc = bind(listen_sd,(struct sockaddr*)&addr,sizeof(addr)); if(rc < 0){ perror("bind() failed"); close(listen_sd); exit(-1); } rc = listen(listen_sd,10); if(rc < 0){ perror("listen() failed"); close(listen_sd); exit(-1); } FD_ZERO(&master_set); max_sd = listen_sd; FD_SET(listen_sd,&master_set); timeout.tv_sec = 3*60; timeout.tv_usec = 0; // printf("Success to construct deamon, Entering loop...\n"); // printf("SOMAXCONN : %d\n",SOMAXCONN); long counter = 0; /**Entering main loop**/ do{ // printf("[Loop(%u)]\n",++counter); memcpy(&working_set,&master_set,sizeof(master_set)); rc = select(max_sd+1, &working_set, NULL, NULL, NULL); if(rc < 0){ perror("select() failed\n"); break; } if(rc == 0){ printf("select() time out. End program.\n"); break; } desc_ready = rc; for(i = 0 ; i < max_sd+1 && desc_ready > 0 ; ++i){ if(FD_ISSET(i,&working_set)){ // printf("%d tried to connect to me!!\n",i); desc_ready = -1; if(i == listen_sd){ // printf("Listening socket is readable\n"); new_sd = accept(listen_sd,NULL,NULL); if(new_sd < 0){ printf("accept() failed"); end_server = TRUE; } FD_SET(new_sd,&master_set); if(new_sd > max_sd){ max_sd = new_sd; } proc* p = create_proc(new_sd); add_proc(p); }else{ proc_data* receivedProc = (proc_data*)malloc(sizeof(proc_data)); rc = recv(i,receivedProc,sizeof(proc_data),0); if(rc <= 0){ FD_CLR(i,&master_set); _FIN(i); }else{ if(receivedProc->REQUEST == CONNECT){ _CONNECT(i); }else if(receivedProc->REQUEST == RENEW){ _RENEW(i,receivedProc); }else if(receivedProc->REQUEST == MIGDONE){ _MIGDONE(i,receivedProc); }else if(receivedProc->REQUEST == CANRECEIVE){ _CANRECEIVE(i,receivedProc); }else if(receivedProc->REQUEST == FAILEDTOGET){ _FAILEDTOGET(i,receivedProc); }else if(receivedProc->REQUEST == CUDAMALLOC){ _CUDAMALLOC(i,receivedProc); } } } } } mocu_check(); }while(end_server == FALSE); int closed = 0; for(i = 0 ; i < max_sd ; i ++){ if(FD_ISSET(i,&master_set)){ close(i); closed = 1; } } fclose(fp); if(!closed){ semctl(sem_id,0,IPC_RMID); } return 0; }
void read_file(char *filename,struct Process **pproc) { FILE *fp; char *endfile,line[MAXSTR],*p,save_token[MAXSTR]; int label_bool=0,line_count=0,num_code=0,n_args; struct instruction_node *new_instr; struct Process *proc; struct expr_node *new_expr; proc=(struct Process*)malloc(sizeof(struct Process)); if(proc==NULL) die("errore nell'allocare struct Process"); proc->pt=NULL; proc->pc=NULL; proc->prev=NULL; proc->next=NULL; proc->processID=get_processID(); fp=fopen(filename,"r"); if(fp==NULL) die("error opening file"); proc->pc=(struct process_construct*)malloc(sizeof(struct process_construct)); if(proc->pc==NULL) die("errore nell'allocare process_construct"); proc->pc->first=NULL; proc->pc->last=NULL; proc->pc->len=0; proc->pc->org[0]='\0'; proc->pc->vt_first=NULL; proc->pc->vt_last=NULL; while((endfile=fgets(line,MAXSTR,fp))!=NULL) { line_count++; p=&line[0]; if(*p==';') continue; p=skip_space(p); if(*p=='\n') continue; p=get_token(p); p=skip_space(p); if((strcmp(my_token,"org"))==0) { p=get_word(p);strncpy(proc->pc->org,my_token,MAXSTR); if(*p!='\n') {sprintf(save_token,"parse error at line %d. not an end line after the org argument",line_count);die(save_token);} continue; } if((strcmp(my_token,"end"))==0) { p=skip_space(p); if(*p!='\n') {sprintf(save_token,"parse error after 'end' at line %d.",line_count);die(save_token);} break; } if((strcmp(my_token,"assert"))==0) { take_assert(p); new_expr=(struct expr_node*)malloc(sizeof(struct expr_node)); if(new_expr==NULL) {printf("at line %d, ",line_count);die("error allocating new_expr");} p=skip_space(p); p=get_b_arg(&new_expr,p,line_count); insert_in_vt(ASSERT_STR,new_expr,line_count,proc); continue; } if(*p==':') { insert_label(my_token,num_code,line_count,proc);p=skip_space(++p); if(*p=='\n') continue; p=get_token(p); } n_args=is_instr(my_token,line_count); if(n_args!=-1) { new_instr=(struct instruction_node*)malloc(sizeof(struct instruction_node)); if(new_instr==NULL) {printf("at line %d , ",line_count);die("error allocating new_instr");} strncpy(new_instr->instr,my_token,MAXSTR); new_instr->num_node=num_code++; new_instr->line_count=line_count; new_instr->prev=NULL; new_instr->next=NULL; new_instr->code=NULL; new_instr->left=NULL; new_instr->right=NULL; new_instr->laddr[0]='#';new_instr->laddr[1]='\0'; new_instr->raddr[0]='#';new_instr->raddr[1]='\0'; strcpy(new_instr->modifier,"NULL"); if(*p=='.'){ p=get_word(++p);is_modifier(my_token,line_count); strncpy(new_instr->modifier,my_token,MAXMOD); } p=skip_space(p); if(n_args>0) { p=get_addr_mode(p); //$ by default. the result is in my_token new_instr->laddr[0]=my_token[0]; new_instr->laddr[1]='\0'; //p=get_token(p); //new_instr->left=(struct expr_node*)malloc(sizeof(struct expr_node)); //if(new_instr->left==NULL){ //printf("at line %d , ",line_count);die("error alocating left expr");} p=get_arg(&new_instr->left,p,line_count); p=skip_space(p); if(n_args>1) { if(*p!=',') {printf("at line %d , ",line_count);die("a comma expected (,)");} p=skip_space(++p); p=get_addr_mode(p); //$ by default. the result is in my_token new_instr->raddr[0]=my_token[0]; new_instr->raddr[1]='\0'; //p=get_token(p); //new_instr->right=(struct expr_node*)malloc(sizeof(struct expr_node)); //if(new_instr->right==NULL){ //printf("at line %d , ",line_count);die("error allocating right expr");} p=get_arg(&new_instr->right,p,line_count); p=skip_space(p); } } if(*p!='\n') {printf("at line %d , ",line_count);die("not an ending line after command");} //add the node add_node(new_instr,proc); continue; } strncpy(save_token,my_token,MAXSTR); p=get_token(p); if((strcmp(my_token,"equ"))==0) { new_expr=(struct expr_node*)malloc(sizeof(struct expr_node)); if(new_expr==NULL) {printf("at line %d, ",line_count);die("error allocating new_expr");} p=skip_space(p); p=get_arg(&new_expr,p,line_count); insert_in_vt(save_token,new_expr,line_count,proc); continue; } } fclose(fp); proc->pc->len=num_code; add_proc(proc); *pproc=proc; }