1
/*****************************************************************************\
2
* cluster_report_functions.c - Interface to functions dealing with cluster
4
******************************************************************************
5
* Copyright (C) 2010 Lawrence Livermore National Security.
6
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7
* Written by Danny Auble da@llnl.gov, et. al.
8
* CODE-OCEC-09-009. All rights reserved.
10
* This file is part of SLURM, a resource management program.
11
* For details, see <https://computing.llnl.gov/linux/slurm/>.
12
* Please also read the included file: DISCLAIMER.
14
* SLURM is free software; you can redistribute it and/or modify it under
15
* the terms of the GNU General Public License as published by the Free
16
* Software Foundation; either version 2 of the License, or (at your option)
19
* In addition, as a special exception, the copyright holders give permission
20
* to link the code of portions of this program with the OpenSSL library under
21
* certain conditions as described in each individual source file, and
22
* distribute linked combinations including the two. You must obey the GNU
23
* General Public License in all respects for all of the code used other than
24
* OpenSSL. If you modify file(s) with this exception, you may extend this
25
* exception to your version of the file(s), but you are not obligated to do
26
* so. If you do not wish to do so, delete this exception statement from your
27
* version. If you delete this exception statement from all source files in
28
* the program, then also delete it here.
30
* SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
31
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
32
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
35
* You should have received a copy of the GNU General Public License along
36
* with SLURM; if not, write to the Free Software Foundation, Inc.,
37
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38
\*****************************************************************************/
44
#include <slurm/slurm.h>
45
#include <slurm/slurm_errno.h>
47
#include "src/common/slurmdb_defs.h"
48
#include "src/common/slurm_accounting_storage.h"
49
#include "src/common/xstring.h"
58
static void _process_ua(List user_list, slurmdb_association_rec_t *assoc)
60
ListIterator itr = NULL;
61
slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
62
slurmdb_accounting_rec_t *accting = NULL;
64
/* make sure we add all associations to this
65
user rec because we could have some in
66
partitions which would create another
69
itr = list_iterator_create(user_list);
70
while((slurmdb_report_user = list_next(itr))) {
71
if(!strcmp(slurmdb_report_user->name, assoc->user)
72
&& !strcmp(slurmdb_report_user->acct, assoc->acct))
75
list_iterator_destroy(itr);
77
if(!slurmdb_report_user) {
78
struct passwd *passwd_ptr = NULL;
80
passwd_ptr = getpwnam(assoc->user);
82
uid = passwd_ptr->pw_uid;
83
/* In this report we are using the slurmdb_report user
84
structure to store the information we want
85
since it is already available and will do
86
pretty much what we want.
89
xmalloc(sizeof(slurmdb_report_user_rec_t));
90
slurmdb_report_user->name = xstrdup(assoc->user);
91
slurmdb_report_user->uid = uid;
92
slurmdb_report_user->acct = xstrdup(assoc->acct);
94
list_append(user_list, slurmdb_report_user);
96
/* get the amount of time this assoc used
97
during the time we are looking at */
98
itr = list_iterator_create(assoc->accounting_list);
99
while((accting = list_next(itr))) {
100
slurmdb_report_user->cpu_secs +=
101
(uint64_t)accting->alloc_secs;
103
list_iterator_destroy(itr);
106
static void _process_au(List assoc_list, slurmdb_association_rec_t *assoc)
108
slurmdb_report_assoc_rec_t *slurmdb_report_assoc =
109
xmalloc(sizeof(slurmdb_report_assoc_rec_t));
110
ListIterator itr = NULL;
111
slurmdb_accounting_rec_t *accting = NULL;
113
list_append(assoc_list, slurmdb_report_assoc);
115
slurmdb_report_assoc->acct = xstrdup(assoc->acct);
116
slurmdb_report_assoc->cluster = xstrdup(assoc->cluster);
117
slurmdb_report_assoc->parent_acct = xstrdup(assoc->parent_acct);
118
slurmdb_report_assoc->user = xstrdup(assoc->user);
120
/* get the amount of time this assoc used
121
during the time we are looking at */
122
itr = list_iterator_create(assoc->accounting_list);
123
while((accting = list_next(itr))) {
124
slurmdb_report_assoc->cpu_secs +=
125
(uint64_t)accting->alloc_secs;
127
list_iterator_destroy(itr);
131
static void _process_uw(List user_list, slurmdb_wckey_rec_t *wckey)
133
ListIterator itr = NULL;
134
slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
135
slurmdb_accounting_rec_t *accting = NULL;
136
struct passwd *passwd_ptr = NULL;
139
passwd_ptr = getpwnam(wckey->user);
141
uid = passwd_ptr->pw_uid;
142
/* In this report we are using the slurmdb_report user
143
structure to store the information we want
144
since it is already available and will do
145
pretty much what we want.
147
slurmdb_report_user =
148
xmalloc(sizeof(slurmdb_report_user_rec_t));
149
slurmdb_report_user->name = xstrdup(wckey->user);
150
slurmdb_report_user->uid = uid;
151
slurmdb_report_user->acct = xstrdup(wckey->name);
153
list_append(user_list, slurmdb_report_user);
155
/* get the amount of time this wckey used
156
during the time we are looking at */
157
itr = list_iterator_create(wckey->accounting_list);
158
while((accting = list_next(itr))) {
159
slurmdb_report_user->cpu_secs +=
160
(uint64_t)accting->alloc_secs;
162
list_iterator_destroy(itr);
165
static void _process_wu(List assoc_list, slurmdb_wckey_rec_t *wckey)
167
slurmdb_report_assoc_rec_t *slurmdb_report_assoc = NULL,
168
*parent_assoc = NULL;
169
ListIterator itr = NULL;
170
slurmdb_accounting_rec_t *accting = NULL;
172
/* find the parent */
173
itr = list_iterator_create(assoc_list);
174
while((parent_assoc = list_next(itr))) {
175
if(!parent_assoc->user
176
&& !strcmp(parent_assoc->acct, wckey->name))
179
list_iterator_destroy(itr);
181
parent_assoc = xmalloc(sizeof(slurmdb_report_assoc_rec_t));
183
list_append(assoc_list,
185
parent_assoc->acct = xstrdup(wckey->name);
188
/* now add one for the user */
189
slurmdb_report_assoc = xmalloc(sizeof(slurmdb_report_assoc_rec_t));
190
list_append(assoc_list, slurmdb_report_assoc);
192
slurmdb_report_assoc->acct = xstrdup(wckey->name);
193
slurmdb_report_assoc->user = xstrdup(wckey->user);
195
/* get the amount of time this wckey used
196
during the time we are looking at */
197
itr = list_iterator_create(wckey->accounting_list);
198
while((accting = list_next(itr))) {
199
slurmdb_report_assoc->cpu_secs +=
200
(uint64_t)accting->alloc_secs;
201
parent_assoc->cpu_secs +=
202
(uint64_t)accting->alloc_secs;
204
list_iterator_destroy(itr);
207
static void _process_assoc_type(
209
slurmdb_report_cluster_rec_t *slurmdb_report_cluster,
211
cluster_report_t type)
213
slurmdb_association_rec_t *assoc = NULL;
215
/* now add the associations of interest here by user */
216
while((assoc = list_next(itr))) {
217
if(!assoc->accounting_list
218
|| !list_count(assoc->accounting_list)
219
|| ((type == CLUSTER_REPORT_UA) && !assoc->user)) {
220
list_delete_item(itr);
224
if(strcmp(cluster_name, assoc->cluster))
227
if(type == CLUSTER_REPORT_UA)
228
_process_ua(slurmdb_report_cluster->user_list,
230
else if(type == CLUSTER_REPORT_AU)
231
_process_au(slurmdb_report_cluster->assoc_list,
234
list_delete_item(itr);
238
static void _process_wckey_type(
240
slurmdb_report_cluster_rec_t *slurmdb_report_cluster,
242
cluster_report_t type)
244
slurmdb_wckey_rec_t *wckey = NULL;
246
/* now add the wckeyiations of interest here by user */
247
while((wckey = list_next(itr))) {
248
if(!wckey->accounting_list
249
|| !list_count(wckey->accounting_list)
250
|| ((type == CLUSTER_REPORT_UW) && !wckey->user)) {
251
list_delete_item(itr);
255
if(strcmp(cluster_name, wckey->cluster))
258
if(type == CLUSTER_REPORT_UW)
259
_process_uw(slurmdb_report_cluster->user_list,
261
else if(type == CLUSTER_REPORT_WU)
262
_process_wu(slurmdb_report_cluster->assoc_list,
265
list_delete_item(itr);
269
static List _process_util_by_report(void *db_conn, char *calling_name,
270
void *cond, cluster_report_t type)
271
{ ListIterator itr = NULL;
272
ListIterator itr2 = NULL;
273
ListIterator type_itr = NULL;
274
slurmdb_cluster_cond_t cluster_cond;
275
List type_list = NULL;
276
List cluster_list = NULL;
277
List first_list = NULL;
278
slurmdb_cluster_rec_t *cluster = NULL;
279
slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
280
time_t start_time, end_time;
284
uid_t my_uid = getuid();
285
List ret_list = list_create(slurmdb_destroy_report_cluster_rec);
287
slurmdb_init_cluster_cond(&cluster_cond, 0);
289
cluster_cond.with_deleted = 1;
290
cluster_cond.with_usage = 1;
291
if((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
292
start_time = ((slurmdb_association_cond_t *)cond)->usage_start;
293
end_time = ((slurmdb_association_cond_t *)cond)->usage_end;
295
cluster_cond.cluster_list =
296
((slurmdb_association_cond_t *)cond)->cluster_list;
297
} else if((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
298
start_time = ((slurmdb_wckey_cond_t *)cond)->usage_start;
299
end_time = ((slurmdb_wckey_cond_t *)cond)->usage_end;
301
cluster_cond.cluster_list =
302
((slurmdb_wckey_cond_t *)cond)->cluster_list;
304
error("unknown report type %d", type);
308
/* This needs to be done on some systems to make sure
309
cluster_cond isn't messed. This has happened on some 64
310
bit machines and this is here to be on the safe side.
312
slurmdb_report_set_start_end_time(&start_time, &end_time);
313
cluster_cond.usage_end = end_time;
314
cluster_cond.usage_start = start_time;
317
cluster_list = acct_storage_g_get_clusters(
318
db_conn, my_uid, &cluster_cond);
322
fprintf(stderr, "%s: Problem with cluster query.\n",
327
if((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
328
((slurmdb_association_cond_t *)cond)->usage_start = start_time;
329
((slurmdb_association_cond_t *)cond)->usage_end = end_time;
330
type_list = acct_storage_g_get_associations(
331
db_conn, my_uid, cond);
332
} else if((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
333
((slurmdb_wckey_cond_t *)cond)->usage_start = start_time;
334
((slurmdb_wckey_cond_t *)cond)->usage_end = end_time;
335
type_list = acct_storage_g_get_wckeys(
336
db_conn, my_uid, cond);
341
fprintf(stderr, "%s: Problem with get query.\n", calling_name);
345
if((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
346
first_list = type_list;
347
type_list = slurmdb_get_hierarchical_sorted_assoc_list(
351
/* set up the structures for easy retrieval later */
352
itr = list_iterator_create(cluster_list);
353
type_itr = list_iterator_create(type_list);
354
while((cluster = list_next(itr))) {
355
slurmdb_cluster_accounting_rec_t *accting = NULL;
357
/* check to see if this cluster is around during the
358
time we are looking at */
359
if(!cluster->accounting_list
360
|| !list_count(cluster->accounting_list))
363
slurmdb_report_cluster =
364
xmalloc(sizeof(slurmdb_report_cluster_rec_t));
366
list_append(ret_list, slurmdb_report_cluster);
368
slurmdb_report_cluster->name = xstrdup(cluster->name);
369
if((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_UW))
370
slurmdb_report_cluster->user_list =
371
list_create(slurmdb_destroy_report_user_rec);
372
else if((type == CLUSTER_REPORT_AU)
373
|| (type == CLUSTER_REPORT_WU))
374
slurmdb_report_cluster->assoc_list =
375
list_create(slurmdb_destroy_report_assoc_rec);
377
/* get the amount of time and the average cpu count
378
during the time we are looking at */
379
itr2 = list_iterator_create(cluster->accounting_list);
380
while((accting = list_next(itr2))) {
381
slurmdb_report_cluster->cpu_secs += accting->alloc_secs
382
+ accting->down_secs + accting->idle_secs
383
+ accting->resv_secs;
384
slurmdb_report_cluster->cpu_count += accting->cpu_count;
386
list_iterator_destroy(itr2);
388
slurmdb_report_cluster->cpu_count /=
389
list_count(cluster->accounting_list);
390
if((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU))
391
_process_assoc_type(type_itr, slurmdb_report_cluster,
392
cluster->name, type);
393
else if((type == CLUSTER_REPORT_UW)
394
|| (type == CLUSTER_REPORT_WU))
395
_process_wckey_type(type_itr, slurmdb_report_cluster,
396
cluster->name, type);
397
list_iterator_reset(type_itr);
399
list_iterator_destroy(type_itr);
400
list_iterator_destroy(itr);
405
list_destroy(type_list);
410
list_destroy(first_list);
415
list_destroy(cluster_list);
421
list_destroy(ret_list);
430
extern List slurmdb_report_cluster_account_by_user(void *db_conn,
431
slurmdb_association_cond_t *assoc_cond)
433
return _process_util_by_report(db_conn,
434
"slurmdb_report_cluster_account_by_user",
435
assoc_cond, CLUSTER_REPORT_AU);
438
extern List slurmdb_report_cluster_user_by_account(void *db_conn,
439
slurmdb_association_cond_t *assoc_cond)
441
return _process_util_by_report(db_conn,
442
"slurmdb_report_cluster_user_by_account",
443
assoc_cond, CLUSTER_REPORT_UA);
446
extern List slurmdb_report_cluster_wckey_by_user(void *db_conn,
447
slurmdb_wckey_cond_t *wckey_cond)
449
return _process_util_by_report(db_conn,
450
"slurmdb_report_cluster_wckey_by_user",
451
wckey_cond, CLUSTER_REPORT_WU);
454
extern List slurmdb_report_cluster_user_by_wckey(void *db_conn,
455
slurmdb_wckey_cond_t *wckey_cond)
457
return _process_util_by_report(db_conn,
458
"slurmdb_report_cluster_user_by_wckey",
459
wckey_cond, CLUSTER_REPORT_UW);