1
/*****************************************************************************\
2
* user_report_functions.c - Interface to functions dealing with user reports.
3
******************************************************************************
4
* Copyright (C) 2010 Lawrence Livermore National Security.
5
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6
* Written by Danny Auble da@llnl.gov, et. al.
7
* CODE-OCEC-09-009. All rights reserved.
9
* This file is part of SLURM, a resource management program.
10
* For details, see <https://computing.llnl.gov/linux/slurm/>.
11
* Please also read the included file: DISCLAIMER.
13
* SLURM is free software; you can redistribute it and/or modify it under
14
* the terms of the GNU General Public License as published by the Free
15
* Software Foundation; either version 2 of the License, or (at your option)
18
* In addition, as a special exception, the copyright holders give permission
19
* to link the code of portions of this program with the OpenSSL library under
20
* certain conditions as described in each individual source file, and
21
* distribute linked combinations including the two. You must obey the GNU
22
* General Public License in all respects for all of the code used other than
23
* OpenSSL. If you modify file(s) with this exception, you may extend this
24
* exception to your version of the file(s), but you are not obligated to do
25
* so. If you do not wish to do so, delete this exception statement from your
26
* version. If you delete this exception statement from all source files in
27
* the program, then also delete it here.
29
* SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
30
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
34
* You should have received a copy of the GNU General Public License along
35
* with SLURM; if not, write to the Free Software Foundation, Inc.,
36
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37
\*****************************************************************************/
43
#include <slurm/slurm.h>
44
#include <slurm/slurm_errno.h>
46
#include <slurm/slurmdb.h>
48
#include "src/common/slurmdb_defs.h"
49
#include "src/common/slurm_accounting_storage.h"
50
#include "src/common/xstring.h"
52
extern List slurmdb_report_user_top_usage(void *db_conn,
53
slurmdb_user_cond_t *user_cond,
56
List cluster_list = NULL;
57
ListIterator itr = NULL;
58
ListIterator itr2 = NULL;
59
ListIterator itr3 = NULL;
60
ListIterator cluster_itr = NULL;
61
slurmdb_cluster_cond_t cluster_cond;
62
List user_list = NULL;
63
List usage_cluster_list = NULL;
66
slurmdb_user_rec_t *user = NULL;
67
slurmdb_cluster_rec_t *cluster = NULL;
68
slurmdb_association_rec_t *assoc = NULL;
69
slurmdb_accounting_rec_t *assoc_acct = NULL;
70
slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
71
slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
72
uid_t my_uid = getuid();
73
bool delete_user_cond = 0, delete_assoc_cond = 0,
74
delete_cluster_list = 0;
75
time_t start_time, end_time;
79
user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
82
if(!user_cond->assoc_cond) {
83
delete_assoc_cond = 1;
84
user_cond->assoc_cond =
85
xmalloc(sizeof(slurmdb_association_cond_t));
88
if(!user_cond->assoc_cond->cluster_list) {
89
delete_cluster_list = 1;
90
user_cond->assoc_cond->cluster_list =
91
list_create(slurm_destroy_char);
94
user_cond->with_deleted = 1;
95
user_cond->with_assocs = 1;
96
user_cond->assoc_cond->with_usage = 1;
97
user_cond->assoc_cond->without_parent_info = 1;
99
/* This needs to be done on some systems to make sure
100
assoc_cond isn't messed up. This has happened on some 64
101
bit machines and this is here to be on the safe side.
103
start_time = user_cond->assoc_cond->usage_start;
104
end_time = user_cond->assoc_cond->usage_end;
105
slurmdb_report_set_start_end_time(&start_time, &end_time);
106
user_cond->assoc_cond->usage_start = start_time;
107
user_cond->assoc_cond->usage_end = end_time;
109
user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
112
fprintf(stderr, " Problem with user query.\n");
116
/* We have to get the clusters here or we will be unable to
117
get the correct total time for the cluster if associations
120
slurmdb_init_cluster_cond(&cluster_cond, 0);
121
cluster_cond.with_usage = 1;
122
cluster_cond.with_deleted = 1;
123
cluster_cond.usage_end = user_cond->assoc_cond->usage_end;
124
cluster_cond.usage_start = user_cond->assoc_cond->usage_start;
125
cluster_cond.cluster_list = user_cond->assoc_cond->cluster_list;
127
usage_cluster_list = acct_storage_g_get_clusters(
128
db_conn, my_uid, &cluster_cond);
129
if(!usage_cluster_list) {
131
fprintf(stderr, " Problem with cluster query.\n");
135
cluster_list = list_create(slurmdb_destroy_report_cluster_rec);
137
itr = list_iterator_create(usage_cluster_list);
138
while((cluster = list_next(itr))) {
139
slurmdb_cluster_accounting_rec_t *accting = NULL;
141
/* check to see if this cluster is around during the
142
time we are looking at */
143
if(!cluster->accounting_list
144
|| !list_count(cluster->accounting_list))
147
slurmdb_report_cluster =
148
xmalloc(sizeof(slurmdb_report_cluster_rec_t));
150
list_append(cluster_list, slurmdb_report_cluster);
152
slurmdb_report_cluster->name = xstrdup(cluster->name);
153
slurmdb_report_cluster->user_list =
154
list_create(slurmdb_destroy_report_user_rec);
156
/* get the amount of time and the average cpu count
157
during the time we are looking at */
158
cluster_itr = list_iterator_create(cluster->accounting_list);
159
while((accting = list_next(cluster_itr))) {
160
slurmdb_report_cluster->cpu_secs += accting->alloc_secs
161
+ accting->down_secs + accting->idle_secs
162
+ accting->resv_secs;
163
slurmdb_report_cluster->cpu_count += accting->cpu_count;
165
list_iterator_destroy(cluster_itr);
167
slurmdb_report_cluster->cpu_count /=
168
list_count(cluster->accounting_list);
170
list_iterator_destroy(itr);
171
list_destroy(usage_cluster_list);
173
itr = list_iterator_create(user_list);
174
cluster_itr = list_iterator_create(cluster_list);
175
while((user = list_next(itr))) {
176
struct passwd *passwd_ptr = NULL;
177
if(!user->assoc_list || !list_count(user->assoc_list))
180
passwd_ptr = getpwnam(user->name);
182
user->uid = passwd_ptr->pw_uid;
184
user->uid = (uint32_t)NO_VAL;
186
itr2 = list_iterator_create(user->assoc_list);
187
while((assoc = list_next(itr2))) {
189
if(!assoc->accounting_list
190
|| !list_count(assoc->accounting_list))
193
while((slurmdb_report_cluster =
194
list_next(cluster_itr))) {
195
if(!strcmp(slurmdb_report_cluster->name,
197
ListIterator user_itr = NULL;
198
if(!group_accounts) {
199
slurmdb_report_user = NULL;
202
user_itr = list_iterator_create(
203
slurmdb_report_cluster->
205
while((slurmdb_report_user
206
= list_next(user_itr))) {
207
if(slurmdb_report_user->uid
209
if(slurmdb_report_user->
213
} else if(slurmdb_report_user->
216
slurmdb_report_user->
221
list_iterator_destroy(user_itr);
223
if(!slurmdb_report_user) {
224
slurmdb_report_user = xmalloc(
226
(slurmdb_report_user_rec_t));
227
slurmdb_report_user->name =
228
xstrdup(assoc->user);
229
slurmdb_report_user->uid =
231
slurmdb_report_user->acct_list =
233
(slurm_destroy_char);
234
list_append(slurmdb_report_cluster->
236
slurmdb_report_user);
241
if(!slurmdb_report_cluster) {
242
error("This cluster '%s' hasn't "
243
"registered yet, but we have jobs "
244
"that ran?", assoc->cluster);
245
slurmdb_report_cluster =
246
xmalloc(sizeof(slurmdb_report_cluster_rec_t));
247
list_append(cluster_list, slurmdb_report_cluster);
249
slurmdb_report_cluster->name = xstrdup(assoc->cluster);
250
slurmdb_report_cluster->user_list =
251
list_create(slurmdb_destroy_report_user_rec);
252
slurmdb_report_user =
253
xmalloc(sizeof(slurmdb_report_user_rec_t));
254
slurmdb_report_user->name = xstrdup(assoc->user);
255
slurmdb_report_user->uid = user->uid;
256
slurmdb_report_user->acct_list =
257
list_create(slurm_destroy_char);
258
list_append(slurmdb_report_cluster->user_list,
259
slurmdb_report_user);
261
list_iterator_reset(cluster_itr);
263
itr3 = list_iterator_create(
264
slurmdb_report_user->acct_list);
265
while((object = list_next(itr3))) {
266
if(!strcmp(object, assoc->acct))
269
list_iterator_destroy(itr3);
272
list_append(slurmdb_report_user->acct_list,
273
xstrdup(assoc->acct));
274
itr3 = list_iterator_create(assoc->accounting_list);
275
while((assoc_acct = list_next(itr3))) {
276
slurmdb_report_user->cpu_secs +=
277
(uint64_t)assoc_acct->alloc_secs;
278
/* slurmdb_report_cluster->cpu_secs += */
279
/* (uint64_t)assoc_acct->alloc_secs; */
281
list_iterator_destroy(itr3);
283
list_iterator_destroy(itr2);
285
list_iterator_destroy(itr);
286
list_iterator_destroy(cluster_itr);
289
if(delete_cluster_list) {
290
list_destroy(user_cond->assoc_cond->cluster_list);
291
user_cond->assoc_cond->cluster_list = NULL;
294
if(delete_assoc_cond) {
295
slurmdb_destroy_association_cond(user_cond->assoc_cond);
296
user_cond->assoc_cond = NULL;
299
if(delete_user_cond) {
300
slurmdb_destroy_user_cond(user_cond);
305
list_destroy(user_list);
311
list_destroy(cluster_list);