📄 usersort.c
字号:
}void set_priorities(void){ int i; Job *job_ptr; for (i=0; i < nnormalQ; ++i) { job_ptr = normalQ[i]; job_ptr->priority = get_queue_priority(job_ptr->oqueue); }}void sort_jobs(void){ int (*criterion) (const void *, const void *); criterion = compare_nonprime_batch; if (schd_ENFORCE_PRIME_TIME && schd_TimeNow >= schd_ENFORCE_PRIME_TIME) { if (schd_prime_time(0)) criterion = compare_prime_batch; } criterion = compare_by_priority; /* * Sort the list of "normal" jobs, based upon the proper criterion * for the current time of day. */ qsort(normalQ, nnormalQ, sizeof *normalQ, criterion); /* * Sort the list of running jobs, in ascending order by the expected * time to completion. */ qsort(runningJobs, nJRs, sizeof *runningJobs, compare_running); return;}/* * qsort() function to order jobs during primetime. * * Order shortest jobs first (ascending order of walltime requested) * Break ties by sorting in ascending order of requested number of CPUs. */static intcompare_prime_batch(const void *e1, const void *e2){ Job *job1 = *(Job **)e1; Job *job2 = *(Job **)e2; /* Shortest to Longest */ if (job1->walltime > job2->walltime) return 1; if (job1->walltime < job2->walltime) return -1; /* Largest to Smallest */ if (job1->ncpus > job2->ncpus) return -1; if (job1->ncpus < job2->ncpus) return 1; /* Oldest to youngest. */ if (job1->eligible > job2->eligible) return -1; if (job1->eligible < job2->eligible) return 1; return 0;}/* * qsort() function to order jobs during non-primetime. * * Order largest jobs first (descending order of CPUs requested) * Break ties by sorting in ascending order of walltime requested. */static intcompare_nonprime_batch(const void *e1, const void *e2){ Job *job1 = *(Job **)e1; Job *job2 = *(Job **)e2; /* Largest to Smallest */ if (job1->ncpus > job2->ncpus) return -1; if (job1->ncpus < job2->ncpus) return 1; /* Shortest to Longest */ if (job1->walltime > job2->walltime) return 1; if (job1->walltime < job2->walltime) return -1; /* Oldest to youngest. */ if (job1->eligible > job2->eligible) return -1; if (job1->eligible < job2->eligible) return 1; return 0;}/* * qsort() function to order running jobs. * * Order running jobs by remaining runtime from soonest-ending to latest- * ending. Jobs end up in order of time of completion (first to complete * first). */static intcompare_running(const void *e1, const void *e2){ Job *job1 = *(Job **)e1; Job *job2 = *(Job **)e2; if (job1->time_left > job2->time_left) return 1; if (job1->time_left < job2->time_left) return -1; return 0;}/* * qsort() function to order jobs by priority. * * Order queued jobs by descending priority, breaking ties by sorting by * requested walltime from shortest to longest. Jobs with the same priority * end up in order of increasing waltime requests. */static intcompare_by_priority(const void *e1, const void *e2){ Job *job1 = *(Job **)e1; Job *job2 = *(Job **)e2; /* descending by priority */ if (job1->priority > job2->priority) return -1; if (job1->priority < job2->priority) return 1; /* ascending walltime */ if (job1->walltime > job2->walltime) return 1; if (job1->walltime < job2->walltime) return -1; return 0;}/* * This function creates a new linked list from the Job structs pointed to * by the arrays of Job pointers. Note that the reassembly is carried out * in place - only the links are modified, there is no allocation or freeing * other than at the end to free all the sublists. * * The final list will be reassembled and ordered as: * * running, normal */static Job *make_job_list(void){ Job list_seed, *joblist, *jobtail; int i; memset(&list_seed, 0, sizeof(list_seed)); /* * "Seed" the linked list by pointing to a bogus initial element. * Since the jobtail->next pointer will always be valid (either it * hangs off the seed or a real job) this simplifies the following * list operations considerably. */ joblist = &list_seed; jobtail = &list_seed; jobtail->next = NULL; /* Walk the running jobs and place them on the list. */ for (i = 0; i < nJRs; i++) jobtail = jobtail->next = runningJobs[i]; /* Walk the normal jobs and place them on the list. */ for (i = 0; i < nnormalQ; i++) jobtail = jobtail->next = normalQ[i]; /* Place any remaining jobs on the end of the list. */ for (i = 0; i < notherQ; i++) jobtail = jobtail->next = otherQ[i]; /* Terminate the last element on the list with a NULL next pointer. */ jobtail->next = NULL; /* Free any storage allocated for the lists. */ if (runningJobs) free(runningJobs); if (normalQ) free(normalQ); if (otherQ) free(otherQ); /* And reset all the values. */ runningJobs = normalQ = otherQ = NULL; nJRs = nJQs = nnormalQ = notherQ = 0; /* * The first element on joblist is the pointer to the list_seed. It's * next pointer points to the head of the real list - return that. */ return (joblist->next);}/* Search the list of users, looking for 'user'. */static intis_new_user(char *user, struct Uinfo *list, int len, int *which){ struct Uinfo *list_ptr = list; int i; for (i = 0; i < len; ++i) { if (!strcmp(list_ptr->name, user)) { *which = i; return 0; } ++list_ptr; } return 1;}/* Build a record of the pertinent data about a job owner. */static intmake_uinfo(char *user, struct Uinfo *uinfo){ strncpy(uinfo->name, user, sizeof(uinfo->name) - 1); uinfo->running_jobs = 0; uinfo->remaining_time = 0; uinfo->ncpus_used = 0; uinfo->mem_used = 0; return 0;}/* * Construct a list of users who own one or more "normal" jobs */void get_users(void){ char *id = "get_users"; Job *job_ptr; char *uname; char *name; int which; int i; int j; /* * Destroy any previously created list. */ if (Users) free(Users); Users = NULL; nUsers = 0; /* * Walk the list of "normal" jobs, adding any new users along the way. */ for (i = 0; i < nnormalQ; ++i) { job_ptr = normalQ[i]; name = job_ptr->owner; /* Is this a new entry in the list? */ if (is_new_user(name, Users, nUsers, &which)) { ++nUsers; Users = realloc(Users, nUsers * sizeof *Users); if (!Users) { log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, "realloc(Users)"); return; } make_uinfo(name, &(Users[nUsers - 1])); } } /* Now, walk the list of running jobs and record each user's count. */ for (i = 0; i < nUsers; ++i) { uname = Users[i].name; for (j = 0; j < nJRs; ++j) { job_ptr = runningJobs[j]; name = job_ptr->owner; if (!strcmp(name, uname)) { Users[i].running_jobs ++; Users[i].remaining_time += job_ptr->time_left; Users[i].ncpus_used += job_ptr->ncpus; Users[i].mem_used += job_ptr->memory; } ++job_ptr; } } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -