📄 fairshare.c
字号:
* the "unknown" group. This group is for any user that * is not specified in the resource group file. * * returns success/failure * */int preload_tree(){ group_info *unknown; /* pointer to the "unknown" group */ if( ( conf.group_root = new_group_info() ) == NULL ) return 0; if( (unknown = new_group_info() ) == NULL ) return 0; if( ( conf.group_root -> name = malloc( 5 * sizeof(char) ) ) == NULL ) { perror("Memory allocation error"); return 0; } if( (unknown -> name = (char *) malloc( 8 * sizeof(char) ) ) == NULL ) { perror("Memory Allocation Error"); return 0; } strcpy(conf.group_root -> name, "root"); conf.group_root -> resgroup = -1; conf.group_root -> cresgroup = 0; conf.group_root -> percentage = 1.0; strcpy(unknown -> name, "unknown"); unknown -> shares = conf.unknown_shares; unknown -> resgroup = 0; unknown -> cresgroup = 1; unknown -> parent = conf.group_root; conf.group_root -> child = unknown; return 1;}/* * * count_shares - count the shares in a resource group * a resource group is a group_info and all of its * siblings * * grp - The start of a sibling chain * * returns the number of shares * */int count_shares( group_info *grp ){ int shares = 0; /* accumulator to count the shares */ group_info *cur_grp; /* the current group in a sibling chain */ cur_grp = grp; while( cur_grp != NULL ) { shares += cur_grp -> shares; cur_grp = cur_grp -> sibling; } return shares;}/* * * calc_fair_share_perc - walk the fair share group tree and calculate * the overall percentage of the machine a user/ * group gets if all usage is equal * * root - the root of the current subtree * shares - the number of total shares in the group * * returns success/failure * */int calc_fair_share_perc( group_info *root, int shares ){ int cur_shares; /* total number of shares in the resgrp */ if( root == NULL ) return 0; if( shares == UNSPECIFIED ) cur_shares = count_shares( root ); else cur_shares = shares; root -> percentage = (float) root -> shares / (float) cur_shares * root -> parent -> percentage; calc_fair_share_perc( root -> sibling, cur_shares ); calc_fair_share_perc( root -> child, UNSPECIFIED ); return 1;}/* * * test_perc - a debugging function to check if the fair_share * percentanges calculated add up to 100% * * root - root of the current subtree * * returns total percentage (hopefully 1.0) * */float test_perc( group_info *root ){ if( root == NULL ) return 0; return ( root -> child == NULL ? root -> percentage : 0 ) + test_perc( root -> sibling ) + test_perc( root -> child );}/* * * update_usage_on_run - update a users usage information when a * job is run * * jinfo - the job which just started running * * returns nothing * */void update_usage_on_run( job_info *jinfo ){ jinfo -> ginfo -> temp_usage += calculate_usage_value( jinfo -> resreq );}/* * * calculate_usage_value - calcualte a value that represents the usage * information * * resreq - a resource_req list that holds the resource info * * returns the calculated value * * NOTE: currently it will only return the number of cpu seconds used. * This function can be more complicated * */usage_t calculate_usage_value( resource_req *resreq ){ resource_req *tmp; if( resreq != NULL ) { tmp = find_resource_req( resreq, "cput" ); if( tmp != NULL ) return tmp -> amount; } return 0L;}/* * * decay_fairshare_tree - decay the usage information kept in the fair * share tree * * root - the root of the fairshare tree * * returns nothing * */void decay_fairshare_tree( group_info *root ){ if( root == NULL ) return; decay_fairshare_tree( root -> sibling ); decay_fairshare_tree( root -> child ); root -> usage /= 2; if( root -> usage == 0 ) root -> usage = 1;}/* * * extract_fairshare - extract the first job from the user with the * least percentage / usage ratio * * jobs - array of jobs to search through * * return the found job or NULL on error * */job_info *extract_fairshare( job_info **jobs ){ job_info *max = NULL; /* job with the max shares / percentage */ float max_value; /* max shares / percentage */ float cur_value; /* the current shares / percentage value */ int i; if( jobs != NULL ) { max_value = -1; for( i = 0; jobs[i] != NULL; i++) { cur_value=jobs[i] -> ginfo -> percentage / jobs[i] -> ginfo -> temp_usage; if( max_value < cur_value && !jobs[i] -> is_running && !jobs[i] -> can_not_run ) { max = jobs[i]; max_value = cur_value; } } } return max;}/* * * print_fairshare - print out the fair share tree * * root - root of subtree * * returns nothing * */void print_fairshare( group_info *root ){ if( root == NULL ) return; if( root -> child == NULL ) printf("User: %-10s Grp: %d cgrp: %d Usage %ld Perc: %f\n", root -> name, root -> resgroup, root -> cresgroup, root -> usage, root -> percentage); print_fairshare( root -> sibling ); print_fairshare( root -> child );}/* * * write_usage - write the usage information to the usage file * This fuction uses a recursive helper function * * * returns success/failure * */int write_usage(){ FILE *fp; /* file pointer to usage file */ if( ( fp = fopen(USAGE_FILE, "wb") ) == NULL ) { perror("Error opening file " USAGE_FILE); return 0; } rec_write_usage(conf.group_root, fp); fclose(fp); return 1;}/* * * rec_write_usage - recursive helper function which will write out all * the group_info structs of the resgroup tree * * root - the root of the current subtree * fp - the file to write the ginfo out to * * returns nothing * */void rec_write_usage( group_info *root, FILE *fp ){ struct group_node_usage grp; /* used to write out usage info */ if( root == NULL ) return; if( root -> usage != 1 ) /* usage defaults to 1 */ { strcpy(grp.name, root -> name); grp.usage = root -> usage; fwrite(&grp, sizeof(struct group_node_usage), 1, fp); } rec_write_usage(root -> sibling, fp); rec_write_usage(root -> child, fp);}/* * * read_usage - read the usage information and load it into the * resgroup tree. * * returns success/failure * */int read_usage( ){ FILE *fp; /* file pointer to usage file */ struct group_node_usage grp; /* struct used to read in usage info */ group_info *ginfo; /* ptr to current group usage */ if( ( fp = fopen(USAGE_FILE, "r") ) == NULL ) { perror("Can not open file " USAGE_FILE); return 0; } while( fread(&grp, sizeof( struct group_node_usage ), 1, fp ) ) { ginfo = find_alloc_ginfo(grp.name); if( ginfo != NULL ) ginfo -> usage = grp.usage; } fclose(fp); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -