~ubuntu-branches/ubuntu/precise/slurm-llnl/precise

« back to all changes in this revision

Viewing changes to src/db_api/user_report_functions.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2011-04-08 11:21:17 UTC
  • mfrom: (3.3.16 sid)
  • Revision ID: james.westby@ubuntu.com-20110408112117-nfnyq9dtm55hqoaw
Tags: 2.2.4-1
* New upstream releases 
* Cleaning spare file and directories, not belonging to the sources
  generated by the building process and not removed by distclean.
  Added debian/clean with spare files and rm -rf inside debian/rules
  for directories.
* Added new packages libslurm-perl, libslurmdb-perl, slurm-llnl-torque
  (Closes: #575822) thanks to Julien Blache

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
8
 *
 
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.
 
12
 *
 
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)
 
16
 *  any later version.
 
17
 *
 
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.
 
28
 *
 
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
 
32
 *  details.
 
33
 *
 
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
\*****************************************************************************/
 
38
 
 
39
#ifdef HAVE_CONFIG_H
 
40
#  include "config.h"
 
41
#endif
 
42
 
 
43
#include <slurm/slurm.h>
 
44
#include <slurm/slurm_errno.h>
 
45
 
 
46
#include <slurm/slurmdb.h>
 
47
 
 
48
#include "src/common/slurmdb_defs.h"
 
49
#include "src/common/slurm_accounting_storage.h"
 
50
#include "src/common/xstring.h"
 
51
 
 
52
extern List slurmdb_report_user_top_usage(void *db_conn,
 
53
                                          slurmdb_user_cond_t *user_cond,
 
54
                                          bool group_accounts)
 
55
{
 
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;
 
64
        char *object = NULL;
 
65
        int exit_code = 0;
 
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;
 
76
 
 
77
        if(!user_cond) {
 
78
                delete_user_cond = 1;
 
79
                user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
 
80
        }
 
81
 
 
82
        if(!user_cond->assoc_cond) {
 
83
                delete_assoc_cond = 1;
 
84
                user_cond->assoc_cond =
 
85
                        xmalloc(sizeof(slurmdb_association_cond_t));
 
86
        }
 
87
 
 
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);
 
92
        }
 
93
 
 
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;
 
98
 
 
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.
 
102
        */
 
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;
 
108
 
 
109
        user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
 
110
        if(!user_list) {
 
111
                exit_code=1;
 
112
                fprintf(stderr, " Problem with user query.\n");
 
113
                goto end_it;
 
114
        }
 
115
 
 
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
 
118
           are not enforced.
 
119
        */
 
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;
 
126
 
 
127
        usage_cluster_list = acct_storage_g_get_clusters(
 
128
                db_conn, my_uid, &cluster_cond);
 
129
        if(!usage_cluster_list) {
 
130
                exit_code=1;
 
131
                fprintf(stderr, " Problem with cluster query.\n");
 
132
                goto end_it;
 
133
        }
 
134
 
 
135
        cluster_list = list_create(slurmdb_destroy_report_cluster_rec);
 
136
 
 
137
        itr = list_iterator_create(usage_cluster_list);
 
138
        while((cluster = list_next(itr))) {
 
139
                slurmdb_cluster_accounting_rec_t *accting = NULL;
 
140
 
 
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))
 
145
                        continue;
 
146
 
 
147
                slurmdb_report_cluster =
 
148
                        xmalloc(sizeof(slurmdb_report_cluster_rec_t));
 
149
 
 
150
                list_append(cluster_list, slurmdb_report_cluster);
 
151
 
 
152
                slurmdb_report_cluster->name = xstrdup(cluster->name);
 
153
                slurmdb_report_cluster->user_list =
 
154
                        list_create(slurmdb_destroy_report_user_rec);
 
155
 
 
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;
 
164
                }
 
165
                list_iterator_destroy(cluster_itr);
 
166
 
 
167
                slurmdb_report_cluster->cpu_count /=
 
168
                        list_count(cluster->accounting_list);
 
169
        }
 
170
        list_iterator_destroy(itr);
 
171
        list_destroy(usage_cluster_list);
 
172
 
 
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))
 
178
                        continue;
 
179
 
 
180
                passwd_ptr = getpwnam(user->name);
 
181
                if(passwd_ptr)
 
182
                        user->uid = passwd_ptr->pw_uid;
 
183
                else
 
184
                        user->uid = (uint32_t)NO_VAL;
 
185
 
 
186
                itr2 = list_iterator_create(user->assoc_list);
 
187
                while((assoc = list_next(itr2))) {
 
188
 
 
189
                        if(!assoc->accounting_list
 
190
                           || !list_count(assoc->accounting_list))
 
191
                                continue;
 
192
 
 
193
                        while((slurmdb_report_cluster =
 
194
                               list_next(cluster_itr))) {
 
195
                                if(!strcmp(slurmdb_report_cluster->name,
 
196
                                           assoc->cluster)) {
 
197
                                        ListIterator user_itr = NULL;
 
198
                                        if(!group_accounts) {
 
199
                                                slurmdb_report_user = NULL;
 
200
                                                goto new_user;
 
201
                                        }
 
202
                                        user_itr = list_iterator_create(
 
203
                                                slurmdb_report_cluster->
 
204
                                                user_list);
 
205
                                        while((slurmdb_report_user
 
206
                                               = list_next(user_itr))) {
 
207
                                                if(slurmdb_report_user->uid
 
208
                                                   != NO_VAL) {
 
209
                                                        if(slurmdb_report_user->
 
210
                                                           uid
 
211
                                                           == user->uid)
 
212
                                                                break;
 
213
                                                } else if(slurmdb_report_user->
 
214
                                                          name
 
215
                                                          && !strcasecmp(
 
216
                                                                  slurmdb_report_user->
 
217
                                                                  name,
 
218
                                                                  user->name))
 
219
                                                        break;
 
220
                                        }
 
221
                                        list_iterator_destroy(user_itr);
 
222
                                new_user:
 
223
                                        if(!slurmdb_report_user) {
 
224
                                                slurmdb_report_user = xmalloc(
 
225
                                                        sizeof
 
226
                                                        (slurmdb_report_user_rec_t));
 
227
                                                slurmdb_report_user->name =
 
228
                                                        xstrdup(assoc->user);
 
229
                                                slurmdb_report_user->uid =
 
230
                                                        user->uid;
 
231
                                                slurmdb_report_user->acct_list =
 
232
                                                        list_create
 
233
                                                        (slurm_destroy_char);
 
234
                                                list_append(slurmdb_report_cluster->
 
235
                                                            user_list,
 
236
                                                            slurmdb_report_user);
 
237
                                        }
 
238
                                        break;
 
239
                                }
 
240
                        }
 
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);
 
248
 
 
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);
 
260
                        }
 
261
                        list_iterator_reset(cluster_itr);
 
262
 
 
263
                        itr3 = list_iterator_create(
 
264
                                slurmdb_report_user->acct_list);
 
265
                        while((object = list_next(itr3))) {
 
266
                                if(!strcmp(object, assoc->acct))
 
267
                                        break;
 
268
                        }
 
269
                        list_iterator_destroy(itr3);
 
270
 
 
271
                        if(!object)
 
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; */
 
280
                        }
 
281
                        list_iterator_destroy(itr3);
 
282
                }
 
283
                list_iterator_destroy(itr2);
 
284
        }
 
285
        list_iterator_destroy(itr);
 
286
        list_iterator_destroy(cluster_itr);
 
287
 
 
288
end_it:
 
289
        if(delete_cluster_list) {
 
290
                list_destroy(user_cond->assoc_cond->cluster_list);
 
291
                user_cond->assoc_cond->cluster_list = NULL;
 
292
        }
 
293
 
 
294
        if(delete_assoc_cond) {
 
295
                slurmdb_destroy_association_cond(user_cond->assoc_cond);
 
296
                user_cond->assoc_cond = NULL;
 
297
        }
 
298
 
 
299
        if(delete_user_cond) {
 
300
                slurmdb_destroy_user_cond(user_cond);
 
301
                user_cond = NULL;
 
302
        }
 
303
 
 
304
        if(user_list) {
 
305
                list_destroy(user_list);
 
306
                user_list = NULL;
 
307
        }
 
308
 
 
309
        if(exit_code) {
 
310
                if(cluster_list) {
 
311
                        list_destroy(cluster_list);
 
312
                        cluster_list = NULL;
 
313
                }
 
314
        }
 
315
 
 
316
        return cluster_list;
 
317
}