1 /*****************************************************************************\
2  *  process.c -  process the return from get_share_info.
3  *****************************************************************************
4  *  Copyright (C) 2008-2010 Lawrence Livermore National Security.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Danny Auble <da@llnl.gov>
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://slurm.schedmd.com/>.
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 #include "src/common/slurm_priority.h"
40 #include "src/sshare/sshare.h"
41 #include <math.h>
42 
43 static void _print_tres(print_field_t *field, uint64_t *tres_cnts,
44 			int last);
45 
46 int long_flag;		/* exceeds 80 character limit with more info */
47 char **tres_names = NULL;
48 uint32_t tres_cnt = 0;
49 char *opt_field_list = NULL;
50 
51 print_field_t fields[] = {
52 	{-20, "Account", print_fields_str, PRINT_ACCOUNT},
53 	{10, "Cluster", print_fields_str, PRINT_CLUSTER},
54 	{13, "EffectvUsage", print_fields_double, PRINT_EUSED},
55 	{10, "FairShare", print_fields_double, PRINT_FSFACTOR},
56 	{30, "GrpTRESMins", _print_tres, PRINT_TRESMINS},
57 	{30, "GrpTRESRaw", _print_tres, PRINT_GRPTRESRAW},
58 	{6,  "ID", print_fields_uint, PRINT_ID},
59 	{10, "LevelFS", print_fields_double, PRINT_LEVELFS},
60 	{11, "NormShares", print_fields_double, PRINT_NORMS},
61 	{11, "NormUsage", print_fields_double, PRINT_NORMU},
62 	{12, "Partition", print_fields_str, PRINT_PART},
63 	{10, "RawShares", print_fields_uint32, PRINT_RAWS},
64 	{11, "RawUsage", print_fields_uint64, PRINT_RAWU},
65 	{30, "TRESRunMins", _print_tres, PRINT_RUNMINS},
66 	{10, "User", print_fields_str, PRINT_USER},
67 	{0,  NULL, NULL, 0}
68 };
69 
_print_tres(print_field_t * field,uint64_t * tres_cnts,int last)70 static void _print_tres(print_field_t *field, uint64_t *tres_cnts,
71 			int last)
72 {
73 	int abs_len = abs(field->len);
74 	char *print_this;
75 
76 	print_this = slurmdb_make_tres_string_from_arrays(
77 		tres_names, tres_cnts, tres_cnt, TRES_STR_FLAG_REMOVE);
78 
79 	if (!print_this)
80 		print_this = xstrdup("");
81 
82 	if (print_fields_parsable_print == PRINT_FIELDS_PARSABLE_NO_ENDING
83 	    && last)
84 		printf("%s", print_this);
85 	else if (print_fields_parsable_print)
86 		printf("%s|", print_this);
87 	else {
88 		if (strlen(print_this) > abs_len)
89 			print_this[abs_len-1] = '+';
90 
91 		if (field->len == abs_len)
92 			printf("%*.*s ", abs_len, abs_len, print_this);
93 		else
94 			printf("%-*.*s ", abs_len, abs_len, print_this);
95 	}
96 	xfree(print_this);
97 }
98 
process(shares_response_msg_t * resp,uint16_t options)99 extern int process(shares_response_msg_t *resp, uint16_t options)
100 {
101 	uint32_t flags = slurmctld_conf.priority_flags;
102 	int rc = SLURM_SUCCESS, i;
103 	assoc_shares_object_t *share = NULL;
104 	ListIterator itr = NULL;
105 	ListIterator itr2 = NULL;
106 	char *object = NULL;
107 	char *print_acct = NULL;
108 	List tree_list = NULL;
109 	char *tmp_char = NULL;
110 
111 	int field_count = 0;
112 
113 	List format_list;
114 	List print_fields_list; /* types are of print_field_t */
115 
116 	if (!resp)
117 		return SLURM_ERROR;
118 
119 	tres_names = resp->tres_names;
120 	tres_cnt = resp->tres_cnt;
121 
122 	format_list = list_create(xfree_ptr);
123 	if (opt_field_list) {
124 		slurm_addto_char_list(format_list, opt_field_list);
125 	} else if (flags & PRIORITY_FLAGS_FAIR_TREE) {
126 		if (long_flag) {
127 			if (options & PRINT_PARTITIONS)
128 				slurm_addto_char_list(
129 					format_list,
130 					"A,User,P,RawShares,NormShares,"
131 					"RawUsage,NormUsage,Eff,"
132 					"Fairshare,LevelFS,GrpTRESMins,"
133 					"TRESRunMins");
134 			else
135 				slurm_addto_char_list(
136 					format_list,
137 					"A,User,RawShares,NormShares,"
138 					"RawUsage,NormUsage,Eff,"
139 					"Fairshare,LevelFS,GrpTRESMins,"
140 					"TRESRunMins");
141 
142 		} else {
143 			if (options & PRINT_PARTITIONS)
144 				slurm_addto_char_list(
145 					format_list,
146 					"A,User,P,RawShares,NormShares,"
147 					"RawUsage,Eff,Fairshare");
148 			else
149 				slurm_addto_char_list(
150 					format_list,
151 					"A,User,RawShares,NormShares,"
152 					"RawUsage,Eff,Fairshare");
153 		}
154 	} else {
155 		if (long_flag) {
156 			if (options & PRINT_PARTITIONS)
157 				slurm_addto_char_list(
158 					format_list,
159 					"A,User,P,RawShares,NormShares,"
160 					"RawUsage,NormUsage,Eff,"
161 					"Fairshare,GrpTRESMins,TRESRunMins");
162 			else
163 				slurm_addto_char_list(
164 					format_list,
165 					"A,User,RawShares,NormShares,"
166 					"RawUsage,NormUsage,Eff,"
167 					"Fairshare,GrpTRESMins,TRESRunMins");
168 		} else {
169 			if (options & PRINT_PARTITIONS)
170 				slurm_addto_char_list(
171 					format_list,
172 					"A,User,P,RawShares,NormShares,"
173 					"RawUsage,Eff,Fairshare");
174 			else
175 				slurm_addto_char_list(
176 					format_list,
177 					"A,User,RawShares,NormShares,"
178 					"RawUsage,Eff,Fairshare");
179 		}
180 	}
181 
182 
183 	print_fields_list = list_create(NULL);
184 	itr = list_iterator_create(format_list);
185 	while ((object = list_next(itr))) {
186 		int newlen = 0;
187 		if ((tmp_char = strstr(object, "\%"))) {
188 			tmp_char[0] = '\0';
189 			newlen = atoi(tmp_char+1);
190 		}
191 		for (i = 0; fields[i].name; i++) {
192 			if (!xstrncasecmp(fields[i].name, object,
193 					  strlen(object))) {
194 				if (newlen)
195 					fields[i].len = newlen;
196 
197 				list_append(print_fields_list, &fields[i]);
198 				break;
199 			}
200 		}
201 
202 		if (!fields[i].name) {
203 			error("Invalid field requested: \"%s\"", object);
204 			exit(1);
205 		}
206 	}
207 	list_iterator_destroy(itr);
208 	FREE_NULL_LIST(format_list);
209 
210 	if (exit_code) {
211 		FREE_NULL_LIST(print_fields_list);
212 		return SLURM_ERROR;
213 	}
214 
215 	itr2 = list_iterator_create(print_fields_list);
216 	print_fields_header(print_fields_list);
217 
218 	field_count = list_count(print_fields_list);
219 
220 	if (!resp->assoc_shares_list || !list_count(resp->assoc_shares_list))
221 		return SLURM_SUCCESS;
222 
223 	tree_list = list_create(slurmdb_destroy_print_tree);
224 	itr = list_iterator_create(resp->assoc_shares_list);
225 	while ((share = list_next(itr))) {
226 		int curr_inx = 1;
227 		char *tmp_char = NULL;
228 		char *local_acct = NULL;
229 		print_field_t *field = NULL;
230 		uint64_t tres_raw[tres_cnt];
231 
232 		if ((options & PRINT_USERS_ONLY) && share->user == 0)
233 			continue;
234 
235 		while ((field = list_next(itr2))) {
236 			switch(field->type) {
237 			case PRINT_ACCOUNT:
238 				if (share->user) {
239 					local_acct = xstrdup_printf(
240 						"|%s", share->name);
241 				} else
242 					local_acct = xstrdup(share->name);
243 
244 				print_acct = slurmdb_tree_name_get(
245 					local_acct,
246 					share->parent, tree_list);
247 				xfree(local_acct);
248 				field->print_routine(
249 					field,
250 					print_acct,
251 					(curr_inx == field_count));
252 				break;
253 			case PRINT_CLUSTER:
254 				field->print_routine(
255 					field,
256 					share->cluster,
257 					(curr_inx == field_count));
258 				break;
259 			case PRINT_EUSED:
260 				field->print_routine(field,
261 						     share->usage_efctv,
262 						     (curr_inx == field_count));
263 				break;
264 			case PRINT_FSFACTOR:
265 				if (flags & PRIORITY_FLAGS_FAIR_TREE) {
266 					if(share->user)
267 						field->print_routine(
268 						field,
269 						share->fs_factor,
270 						(curr_inx == field_count));
271 					else
272 						print_fields_str(
273 							field,
274 							NULL,
275 							(curr_inx ==
276 							 field_count)
277 						);
278 				}
279 				else
280 					field->print_routine(field,
281 						     priority_g_calc_fs_factor(
282 							     (long double)
283 							     share->usage_efctv,
284 							     (long double)
285 							     share->
286 							     shares_norm),
287 						     (curr_inx == field_count));
288 				break;
289 			case PRINT_LEVELFS:
290 				if (share->shares_raw == SLURMDB_FS_USE_PARENT)
291 					print_fields_str(field, NULL,
292 							 (curr_inx ==
293 							  field_count));
294 				else
295 					field->print_routine(field,
296 						     (double) share->level_fs,
297 						     (curr_inx == field_count));
298 				break;
299 			case PRINT_ID:
300 				field->print_routine(field,
301 						     share->assoc_id,
302 						     (curr_inx == field_count));
303 				break;
304 			case PRINT_NORMS:
305 				field->print_routine(field,
306 						     share->shares_norm,
307 						     (curr_inx == field_count));
308 				break;
309 			case PRINT_NORMU:
310 				field->print_routine(field,
311 						     share->usage_norm,
312 						     (curr_inx == field_count));
313 				break;
314 			case PRINT_RAWS:
315 				if (share->shares_raw == SLURMDB_FS_USE_PARENT)
316 					print_fields_str(field, "parent",
317 							 (curr_inx ==
318 							  field_count));
319 				else
320 					field->print_routine(field,
321 							     share->shares_raw,
322 							     (curr_inx ==
323 							      field_count));
324 				break;
325 			case PRINT_RAWU:
326 				field->print_routine(field,
327 						     share->usage_raw,
328 						     (curr_inx == field_count));
329 				break;
330 			case PRINT_USER:
331 				if (share->user)
332 					tmp_char = share->name;
333 				field->print_routine(field,
334 						     tmp_char,
335 						     (curr_inx == field_count));
336 				break;
337 			case PRINT_PART:
338 				field->print_routine(field,
339 						     share->partition,
340 						     (curr_inx == field_count));
341 				break;
342 			case PRINT_TRESMINS:
343 				field->print_routine(field,
344 						     share->tres_grp_mins,
345 						     (curr_inx == field_count));
346 				break;
347 			case PRINT_GRPTRESRAW:
348 				/* convert to ints and minutes */
349 				for (i=0; i<tres_cnt; i++)
350 					tres_raw[i] = (uint64_t)
351 						(share->usage_tres_raw[i] / 60);
352 				field->print_routine(field,
353 						     tres_raw,
354 						     (curr_inx == field_count));
355 				break;
356 			case PRINT_RUNMINS:
357 				/* convert to minutes */
358 				for (i=0; i<tres_cnt; i++)
359 					share->tres_run_secs[i] /= 60;
360 				field->print_routine(field,
361 						     share->tres_run_secs,
362 						     (curr_inx == field_count));
363 				break;
364 			default:
365 				field->print_routine(
366 					field, NULL,
367 					(curr_inx == field_count));
368 				break;
369 			}
370 			curr_inx++;
371 		}
372 		list_iterator_reset(itr2);
373 		printf("\n");
374 	}
375 
376 	FREE_NULL_LIST(tree_list);
377 	list_iterator_destroy(itr2);
378 	list_iterator_destroy(itr);
379 	FREE_NULL_LIST(print_fields_list);
380 	return rc;
381 }
382