Exemplo n.º 1
0
static List _process_util_by_report(void *db_conn, char *calling_name,
				    void *cond, cluster_report_t type)
{	ListIterator itr = NULL;
	ListIterator type_itr = NULL;
	slurmdb_cluster_cond_t cluster_cond;
	List type_list = NULL;
	List cluster_list = NULL;
	List first_list = NULL;
	slurmdb_cluster_rec_t *cluster = NULL;
	slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
	time_t start_time, end_time;

	int exit_code = 0;

	uid_t my_uid = getuid();
	List ret_list = list_create(slurmdb_destroy_report_cluster_rec);

	slurmdb_init_cluster_cond(&cluster_cond, 0);

	cluster_cond.with_deleted = 1;
	cluster_cond.with_usage = 1;
	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		start_time = ((slurmdb_assoc_cond_t *)cond)->usage_start;
		end_time = ((slurmdb_assoc_cond_t *)cond)->usage_end;

		cluster_cond.cluster_list =
			((slurmdb_assoc_cond_t *)cond)->cluster_list;
	} else if ((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
		start_time = ((slurmdb_wckey_cond_t *)cond)->usage_start;
		end_time = ((slurmdb_wckey_cond_t *)cond)->usage_end;

		cluster_cond.cluster_list =
			((slurmdb_wckey_cond_t *)cond)->cluster_list;
	} else {
		error("unknown report type %d", type);
		return NULL;
	}

	/* This needs to be done on some systems to make sure
	   cluster_cond isn't messed.  This has happened on some 64
	   bit machines and this is here to be on the safe side.
	*/
	slurmdb_report_set_start_end_time(&start_time, &end_time);
	cluster_cond.usage_end = end_time;
	cluster_cond.usage_start = start_time;


	cluster_list = acct_storage_g_get_clusters(
		db_conn, my_uid, &cluster_cond);

	if (!cluster_list) {
		exit_code=1;
		fprintf(stderr, "%s: Problem with cluster query.\n",
			calling_name);
		goto end_it;
	}

	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		((slurmdb_assoc_cond_t *)cond)->usage_start = start_time;
		((slurmdb_assoc_cond_t *)cond)->usage_end = end_time;
		type_list = acct_storage_g_get_assocs(
			db_conn, my_uid, cond);
	} else if ((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
		((slurmdb_wckey_cond_t *)cond)->usage_start = start_time;
		((slurmdb_wckey_cond_t *)cond)->usage_end = end_time;
		type_list = acct_storage_g_get_wckeys(
			db_conn, my_uid, cond);
	}

	if (!type_list) {
		exit_code=1;
		fprintf(stderr, "%s: Problem with get query.\n", calling_name);
		goto end_it;
	}

	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		first_list = type_list;
		type_list = slurmdb_get_hierarchical_sorted_assoc_list(
			first_list, true);
	}

	/* set up the structures for easy retrieval later */
	itr = list_iterator_create(cluster_list);
	type_itr = list_iterator_create(type_list);
	while((cluster = list_next(itr))) {
		/* check to see if this cluster is around during the
		   time we are looking at */
		if (!cluster->accounting_list
		   || !list_count(cluster->accounting_list))
			continue;

		slurmdb_report_cluster = slurmdb_cluster_rec_2_report(cluster);

		list_append(ret_list, slurmdb_report_cluster);

		if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_UW))
			slurmdb_report_cluster->user_list =
				list_create(slurmdb_destroy_report_user_rec);
		else if ((type == CLUSTER_REPORT_AU)
			|| (type == CLUSTER_REPORT_WU))
			slurmdb_report_cluster->assoc_list =
				list_create(slurmdb_destroy_report_assoc_rec);

		if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU))
			_process_assoc_type(type_itr, slurmdb_report_cluster,
					    cluster->name, type);
		else if ((type == CLUSTER_REPORT_UW)
			|| (type == CLUSTER_REPORT_WU))
			_process_wckey_type(type_itr, slurmdb_report_cluster,
					    cluster->name, type);
		list_iterator_reset(type_itr);
	}
	list_iterator_destroy(type_itr);
	list_iterator_destroy(itr);

end_it:
	FREE_NULL_LIST(type_list);
	FREE_NULL_LIST(first_list);
	FREE_NULL_LIST(cluster_list);
	if (exit_code)
		FREE_NULL_LIST(ret_list);
	return ret_list;
}
Exemplo n.º 2
0
extern List slurmdb_report_user_top_usage(void *db_conn,
        slurmdb_user_cond_t *user_cond,
        bool group_accounts)
{
    List cluster_list = NULL;
    ListIterator itr = NULL;
    ListIterator itr2 = NULL;
    ListIterator itr3 = NULL;
    ListIterator cluster_itr = NULL;
    slurmdb_cluster_cond_t cluster_cond;
    List user_list = NULL;
    List usage_cluster_list = NULL;
    char *object = NULL;
    int exit_code = 0;
    slurmdb_user_rec_t *user = NULL;
    slurmdb_cluster_rec_t *cluster = NULL;
    slurmdb_assoc_rec_t *assoc = NULL;
    slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
    slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
    uid_t my_uid = getuid();
    bool delete_user_cond = 0, delete_assoc_cond = 0,
         delete_cluster_list = 0;
    time_t start_time, end_time;

    if (!user_cond) {
        delete_user_cond = 1;
        user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
    }

    if (!user_cond->assoc_cond) {
        delete_assoc_cond = 1;
        user_cond->assoc_cond =
            xmalloc(sizeof(slurmdb_assoc_cond_t));
    }

    if (!user_cond->assoc_cond->cluster_list) {
        delete_cluster_list = 1;
        user_cond->assoc_cond->cluster_list =
            list_create(slurm_destroy_char);
    }

    user_cond->with_deleted = 1;
    user_cond->with_assocs = 1;
    user_cond->assoc_cond->with_usage = 1;
    user_cond->assoc_cond->without_parent_info = 1;

    /* This needs to be done on some systems to make sure
       assoc_cond isn't messed up.  This has happened on some 64
       bit machines and this is here to be on the safe side.
    */
    start_time = user_cond->assoc_cond->usage_start;
    end_time = user_cond->assoc_cond->usage_end;
    slurmdb_report_set_start_end_time(&start_time, &end_time);
    user_cond->assoc_cond->usage_start = start_time;
    user_cond->assoc_cond->usage_end = end_time;

    user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
    if (!user_list) {
        exit_code=1;
        fprintf(stderr, " Problem with user query.\n");
        goto end_it;
    }

    /* We have to get the clusters here or we will be unable to
       get the correct total time for the cluster if associations
       are not enforced.
    */
    slurmdb_init_cluster_cond(&cluster_cond, 0);
    cluster_cond.with_usage = 1;
    cluster_cond.with_deleted = 1;
    cluster_cond.usage_end = user_cond->assoc_cond->usage_end;
    cluster_cond.usage_start = user_cond->assoc_cond->usage_start;
    cluster_cond.cluster_list = user_cond->assoc_cond->cluster_list;

    usage_cluster_list = acct_storage_g_get_clusters(
                             db_conn, my_uid, &cluster_cond);
    if (!usage_cluster_list) {
        exit_code=1;
        fprintf(stderr, " Problem with cluster query.\n");
        goto end_it;
    }

    cluster_list = list_create(slurmdb_destroy_report_cluster_rec);

    itr = list_iterator_create(usage_cluster_list);
    while((cluster = list_next(itr))) {
        /* check to see if this cluster is around during the
           time we are looking at */
        if (!cluster->accounting_list
                || !list_count(cluster->accounting_list))
            continue;

        slurmdb_report_cluster = slurmdb_cluster_rec_2_report(cluster);

        list_append(cluster_list, slurmdb_report_cluster);

        slurmdb_report_cluster->user_list =
            list_create(slurmdb_destroy_report_user_rec);
    }
    list_iterator_destroy(itr);
    list_destroy(usage_cluster_list);

    itr = list_iterator_create(user_list);
    cluster_itr = list_iterator_create(cluster_list);
    while((user = list_next(itr))) {
        struct passwd *passwd_ptr = NULL;
        if (!user->assoc_list || !list_count(user->assoc_list))
            continue;

        passwd_ptr = getpwnam(user->name);
        if (passwd_ptr)
            user->uid = passwd_ptr->pw_uid;
        else
            user->uid = (uint32_t)NO_VAL;

        itr2 = list_iterator_create(user->assoc_list);
        while((assoc = list_next(itr2))) {

            if (!assoc->accounting_list
                    || !list_count(assoc->accounting_list))
                continue;

            while((slurmdb_report_cluster =
                        list_next(cluster_itr))) {
                if (!strcmp(slurmdb_report_cluster->name,
                            assoc->cluster)) {
                    ListIterator user_itr = NULL;
                    if (!group_accounts) {
                        slurmdb_report_user = NULL;
                        goto new_user;
                    }
                    user_itr = list_iterator_create(
                                   slurmdb_report_cluster->
                                   user_list);
                    while((slurmdb_report_user
                            = list_next(user_itr))) {
                        if (slurmdb_report_user->uid
                                != NO_VAL) {
                            if (slurmdb_report_user->
                                    uid
                                    == user->uid)
                                break;
                        } else if (slurmdb_report_user->
                                   name
                                   && !strcasecmp(
                                       slurmdb_report_user->
                                       name,
                                       user->name))
                            break;
                    }
                    list_iterator_destroy(user_itr);
new_user:
                    if (!slurmdb_report_user) {
                        slurmdb_report_user = xmalloc(
                                                  sizeof
                                                  (slurmdb_report_user_rec_t));
                        slurmdb_report_user->name =
                            xstrdup(assoc->user);
                        slurmdb_report_user->uid =
                            user->uid;
                        slurmdb_report_user->acct_list =
                            list_create
                            (slurm_destroy_char);
                        list_append(slurmdb_report_cluster->
                                    user_list,
                                    slurmdb_report_user);
                    }
                    break;
                }
            }
            if (!slurmdb_report_cluster) {
                error("This cluster '%s' hasn't "
                      "registered yet, but we have jobs "
                      "that ran?", assoc->cluster);
                slurmdb_report_cluster =
                    xmalloc(sizeof(slurmdb_report_cluster_rec_t));
                list_append(cluster_list, slurmdb_report_cluster);

                slurmdb_report_cluster->name = xstrdup(assoc->cluster);
                slurmdb_report_cluster->user_list =
                    list_create(slurmdb_destroy_report_user_rec);
                slurmdb_report_user =
                    xmalloc(sizeof(slurmdb_report_user_rec_t));
                slurmdb_report_user->name = xstrdup(assoc->user);
                slurmdb_report_user->uid = user->uid;
                slurmdb_report_user->acct_list =
                    list_create(slurm_destroy_char);
                list_append(slurmdb_report_cluster->user_list,
                            slurmdb_report_user);
            }
            list_iterator_reset(cluster_itr);

            itr3 = list_iterator_create(
                       slurmdb_report_user->acct_list);
            while((object = list_next(itr3))) {
                if (!strcmp(object, assoc->acct))
                    break;
            }
            list_iterator_destroy(itr3);

            if (!object)
                list_append(slurmdb_report_user->acct_list,
                            xstrdup(assoc->acct));
            slurmdb_transfer_acct_list_2_tres(
                assoc->accounting_list,
                &slurmdb_report_user->tres_list);
        }
        list_iterator_destroy(itr2);
    }
    list_iterator_destroy(itr);
    list_iterator_destroy(cluster_itr);

end_it:
    if (delete_cluster_list) {
        list_destroy(user_cond->assoc_cond->cluster_list);
        user_cond->assoc_cond->cluster_list = NULL;
    }

    if (delete_assoc_cond) {
        slurmdb_destroy_assoc_cond(user_cond->assoc_cond);
        user_cond->assoc_cond = NULL;
    }

    if (delete_user_cond) {
        slurmdb_destroy_user_cond(user_cond);
        user_cond = NULL;
    }

    if (user_list) {
        list_destroy(user_list);
        user_list = NULL;
    }

    if (exit_code) {
        if (cluster_list) {
            list_destroy(cluster_list);
            cluster_list = NULL;
        }
    }

    return cluster_list;
}