/*___INFO__MARK_BEGIN__*/ /************************************************************************* * * The Contents of this file are made available subject to the terms of * the Sun Industry Standards Source License Version 1.2 * * Sun Microsystems Inc., March, 2001 * * * Sun Industry Standards Source License Version 1.2 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.2 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2001 by Sun Microsystems, Inc. * * All Rights Reserved. * ************************************************************************/ /*___INFO__MARK_END__*/ #include #include #include #include #include #include #include #include #ifndef NO_SGE_COMPILE_DEBUG # define NO_SGE_COMPILE_DEBUG #endif #include "rmon/sgermon.h" #include "comm/commlib.h" #include "uti/sge_prog.h" #include "uti/sge_time.h" #include "sgeobj/sge_schedd_conf.h" #include "sgeobj/sge_usage.h" #include "sgeobj/sge_userprj.h" #include "sgeobj/sge_sharetree.h" #include "sgeobj/sge_job.h" #include "sgeobj/sge_ja_task.h" #include "sge_orders.h" #include "sge_job_schedd.h" #include "sgeee.h" #include "sge_support.h" #include "valid_queue_user.h" #include "sge_eejob_SGEJ_L.h" static const long sge_usage_interval = SGE_USAGE_INTERVAL; /*-------------------------------------------------------------------- * decay_usage - decay usage for the passed usage list *--------------------------------------------------------------------*/ static void decay_usage(lList *usage_list, const lList *decay_list, double interval) { lListElem *usage = NULL; if (usage_list) { double decay = 0; double default_decay = 0; default_decay = pow(sconf_get_decay_constant(), interval / (double)sge_usage_interval); for_each(usage, usage_list) { lListElem *decay_elem; if (decay_list && ((decay_elem = lGetElemStr(decay_list, UA_name, lGetPosString(usage, UA_name_POS))))) { decay = pow(lGetPosDouble(decay_elem, UA_value_POS), interval / (double)sge_usage_interval); } else { decay = default_decay; } lSetPosDouble(usage, UA_value_POS, lGetPosDouble(usage, UA_value_POS) * decay); } } return; } /*-------------------------------------------------------------------- * decay_userprj_usage - decay usage for the passed user/project object *--------------------------------------------------------------------*/ void decay_userprj_usage( lListElem *userprj, bool is_user, const lList *decay_list, u_long seqno, u_long curr_time ) { u_long usage_time_stamp; int obj_usage_seqno_POS = is_user ? UU_usage_seqno_POS : PR_usage_seqno_POS; int obj_usage_time_stamp_POS = is_user ? UU_usage_time_stamp_POS : PR_usage_time_stamp_POS; int obj_usage_POS = is_user ? UU_usage_POS : PR_usage_POS; int obj_project_POS = is_user ? UU_project_POS : PR_project_POS; if (userprj && seqno != lGetPosUlong(userprj, obj_usage_seqno_POS)) { /*------------------------------------------------------------- * Note: In order to decay usage once per decay interval, we * keep a time stamp in the user/project of when it was last * decayed and then apply the approriate decay based on the time * stamp. This allows the usage to be decayed on the scheduling * interval, even though the decay interval is different than * the scheduling interval. *-------------------------------------------------------------*/ usage_time_stamp = lGetPosUlong(userprj, obj_usage_time_stamp_POS); if (usage_time_stamp > 0 && (curr_time > usage_time_stamp)) { lListElem *upp; double interval = curr_time - usage_time_stamp; decay_usage(lGetPosList(userprj, obj_usage_POS), decay_list, interval); for_each(upp, lGetPosList(userprj, obj_project_POS)) { decay_usage(lGetPosList(upp, UPP_usage_POS), decay_list, interval); } } lSetPosUlong(userprj, obj_usage_time_stamp_POS, curr_time); if (seqno != (u_long) -1) { lSetPosUlong(userprj, obj_usage_seqno_POS, seqno); } } return; } /*-------------------------------------------------------------------- * calculate_decay_constant - calculates decay rate and constant based * on the decay half life and usage interval. The halftime argument * is in minutes. *--------------------------------------------------------------------*/ void calculate_decay_constant( double halftime, double *decay_rate, double *decay_constant ) { if (halftime < 0) { *decay_rate = 1.0; *decay_constant = 0; } else if (halftime == 0) { *decay_rate = 0; *decay_constant = 1.0; } else { *decay_rate = - log(0.5) / (halftime * 60); *decay_constant = 1 - (*decay_rate * sge_usage_interval); } return; } /*-------------------------------------------------------------------- * calculate_default_decay_constant - calculates the default decay * rate and constant based on the decay half life and usage interval. * The halftime argument is in hours. *--------------------------------------------------------------------*/ void calculate_default_decay_constant( int halftime ) { double sge_decay_rate = 0.0; double sge_decay_constant = 0.0; calculate_decay_constant(halftime*60.0, &sge_decay_rate, &sge_decay_constant); sconf_set_decay_constant(sge_decay_constant); } /*-------------------------------------------------------------------- * sge_for_each_node - visit each node and call the supplied function * until a non-zero return code is returned. *--------------------------------------------------------------------*/ int sge_for_each_share_tree_node( lListElem *node, sge_node_func_t func, void *ptr ) { int retcode=0; lList *children = NULL; lListElem *child_node = NULL; if (node == NULL) { return 0; } if ((retcode = (*func)(node, ptr))) { return retcode; } if ((children = lGetPosList(node, STN_children_POS))) { for_each(child_node, children) { if ((retcode = sge_for_each_share_tree_node(child_node, func, ptr))) { break; } } } return retcode; } /*-------------------------------------------------------------------- * zero_node_fields - zero out the share tree node fields that are * passed to the qmaster from schedd and are displayed at qmon *--------------------------------------------------------------------*/ int sge_zero_node_fields( lListElem *node, void *ptr ) { lSetPosDouble(node, STN_m_share_POS, 0); lSetPosDouble(node, STN_adjusted_current_proportion_POS, 0); lSetPosUlong(node, STN_job_ref_count_POS, 0); return 0; } /*-------------------------------------------------------------------- * sge_init_node_fields - zero out the share tree node fields that are * passed to the qmaster from schedd and are displayed at qmon *--------------------------------------------------------------------*/ int sge_init_node_fields( lListElem *root ) { return sge_for_each_share_tree_node(root, sge_zero_node_fields, NULL); } /*-------------------------------------------------------------------- * sge_calc_node_usage - calculate usage for this share tree node * and all descendant nodes. *--------------------------------------------------------------------*/ double sge_calc_node_usage( lListElem *node, const lList *user_list, const lList *project_list, const lList *decay_list, u_long curr_time, const char *projname, u_long seqno ) { double usage_value = 0; int project_node = 0; lListElem *child_node; lList *children; lListElem *userprj = NULL; lList *usage_list=NULL; lListElem *usage_weight, *usage_elem; double sum_of_usage_weights = 0; const char *usage_name; bool is_user = false; DENTER(TOP_LAYER, "sge_calc_node_usage"); children = lGetPosList(node, STN_children_POS); if (!children) { if (projname) { /*------------------------------------------------------------- * Get usage from project usage sub-list in user object *-------------------------------------------------------------*/ if ((userprj = user_list_locate(user_list, lGetPosString(node, STN_name_POS)))) { lList *projects = lGetList(userprj, UU_project); lListElem *upp; is_user = true; if (projects) { if ((upp=lGetElemStr(projects, UPP_name, projname))) { usage_list = lGetList(upp, UPP_usage); } } } } else { /*------------------------------------------------------------- * Get usage directly from corresponding user or project object *-------------------------------------------------------------*/ if ((userprj = user_list_locate(user_list, lGetPosString(node, STN_name_POS)))) { is_user = true; usage_list = lGetList(userprj, UU_usage); } else if ((userprj = prj_list_locate(project_list, lGetPosString(node, STN_name_POS)))) { is_user = false; usage_list = lGetList(userprj, PR_usage); } } } else { /*------------------------------------------------------------- * If this is a project node, then return the project usage * rather than the children's usage *-------------------------------------------------------------*/ if (!projname) { if ((userprj = prj_list_locate(project_list, lGetPosString(node, STN_name_POS)))) { project_node = 1; is_user = false; usage_list = lGetList(userprj, PR_usage); projname = lGetString(userprj, PR_name); } } } if (usage_list) { lList *usage_weight_list = NULL; /*------------------------------------------------------------- * Decay usage *-------------------------------------------------------------*/ if (curr_time && userprj) { decay_userprj_usage(userprj, is_user, decay_list, seqno, curr_time); } /*------------------------------------------------------------- * Sum usage weighting factors *-------------------------------------------------------------*/ if (sconf_is()) { usage_weight_list = sconf_get_usage_weight_list(); if (usage_weight_list) { for_each(usage_weight, usage_weight_list) sum_of_usage_weights += lGetPosDouble(usage_weight, UA_value_POS); } } /*------------------------------------------------------------- * Combine user/project usage based on usage weighting factors *-------------------------------------------------------------*/ if (usage_weight_list) { for_each(usage_elem, usage_list) { usage_name = lGetPosString(usage_elem, UA_name_POS); usage_weight = lGetElemStr(usage_weight_list, UA_name, usage_name); if (usage_weight && sum_of_usage_weights>0) { usage_value += lGetPosDouble(usage_elem, UA_value_POS) * (lGetPosDouble(usage_weight, UA_value_POS) / sum_of_usage_weights); } } } lFreeList(&usage_weight_list); /*------------------------------------------------------------- * Store other usage values in node usage list *-------------------------------------------------------------*/ for_each(usage_elem, usage_list) { const char *nm = lGetPosString(usage_elem, UA_name_POS); lListElem *u; if (strcmp(nm, USAGE_ATTR_CPU) != 0 && strcmp(nm, USAGE_ATTR_MEM) != 0 && strcmp(nm, USAGE_ATTR_IO) != 0) { if (((u=lGetElemStr(lGetPosList(node, STN_usage_list_POS), UA_name, nm))) || ((u = lAddSubStr(node, UA_name, nm, STN_usage_list, UA_Type)))) lSetPosDouble(u, UA_value_POS, lGetPosDouble(u, UA_value_POS) + lGetPosDouble(usage_elem, UA_value_POS)); } } } if (children) { double child_usage = 0; /*------------------------------------------------------------- * Sum child usage *-------------------------------------------------------------*/ for_each(child_node, children) { lListElem *nu; child_usage += sge_calc_node_usage(child_node, user_list, project_list, decay_list, curr_time, projname, seqno); /*------------------------------------------------------------- * Sum other usage values *-------------------------------------------------------------*/ if (!project_node) for_each(nu, lGetPosList(child_node, STN_usage_list_POS)) { const char *nm = lGetPosString(nu, UA_name_POS); lListElem *u; if (((u=lGetElemStr(lGetPosList(node, STN_usage_list_POS), UA_name, nm))) || ((u=lAddSubStr(node, UA_name, nm, STN_usage_list, UA_Type)))) lSetPosDouble(u, UA_value_POS, lGetPosDouble(u, UA_value_POS) + lGetPosDouble(nu, UA_value_POS)); } } if (!project_node) /* if this is not a project node, we include the child usage */ usage_value += child_usage; else { /* If this is a project node, then we calculate the usage being used by all users which map to the "default" user node by subtracting the sum of all the child usage from the project usage. Then, we add this usage to all of the nodes leading to the "default" user node. */ ancestors_t ancestors; int i; if (search_ancestors(node, "default", &ancestors, 1)) { double default_usage = usage_value - child_usage; if (default_usage > 1.0) { for(i=1; i