-
Notifications
You must be signed in to change notification settings - Fork 628
/
select_cons_res.c
3400 lines (3003 loc) · 99.8 KB
/
select_cons_res.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* select_cons_res.c - node selection plugin supporting consumable
* resources policies.
*****************************************************************************\
*
* The following example below illustrates how four jobs are allocated
* across a cluster using when a processor consumable resource approach.
*
* The example cluster is composed of 4 nodes (10 cpus in total):
* linux01 (with 2 processors),
* linux02 (with 2 processors),
* linux03 (with 2 processors), and
* linux04 (with 4 processors).
*
* The four jobs are the following:
* 1. srun -n 4 -N 4 sleep 120 &
* 2. srun -n 3 -N 3 sleep 120 &
* 3. srun -n 1 sleep 120 &
* 4. srun -n 3 sleep 120 &
* The user launches them in the same order as listed above.
*
* Using a processor consumable resource approach we get the following
* job allocation and scheduling:
*
* The output of squeue shows that we have 3 out of the 4 jobs allocated
* and running. This is a 2 running job increase over the default SLURM
* approach.
*
* Job 2, Job 3, and Job 4 are now running concurrently on the cluster.
*
* [<snip>]# squeue
* JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
* 5 lsf sleep root PD 0:00 1 (Resources)
* 2 lsf sleep root R 0:13 4 linux[01-04]
* 3 lsf sleep root R 0:09 3 linux[01-03]
* 4 lsf sleep root R 0:05 1 linux04
* [<snip>]#
*
* Once Job 2 finishes, Job 5, which was pending, is allocated
* available resources and is then running as illustrated below:
*
* [<snip>]# squeue
* JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
* 3 lsf sleep root R 1:58 3 linux[01-03]
* 4 lsf sleep root R 1:54 1 linux04
* 5 lsf sleep root R 0:02 3 linux[01-03]
* [<snip>]#
*
* Job 3, Job 4, and Job 5 are now running concurrently on the cluster.
*
* [<snip>]# squeue
* JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
* 5 lsf sleep root R 1:52 3 linux[01-03]
* [<snip>]#
*
* The advantage of the consumable resource scheduling policy is that
* the job throughput can increase dramatically.
*
*****************************************************************************
* Copyright (C) 2005-2008 Hewlett-Packard Development Company, L.P.
* Written by Susanne M. Balle <susanne.balle@hp.com>, who borrowed heavily
* from select/linear
*
* This file is part of SLURM, a resource management program.
* For details, see <http://slurm.schedmd.com/>.
* Please also read the included file: DISCLAIMER.
*
* SLURM is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two. You must obey the GNU
* General Public License in all respects for all of the code used other than
* OpenSSL. If you modify file(s) with this exception, you may extend this
* exception to your version of the file(s), but you are not obligated to do
* so. If you do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source files in
* the program, then also delete it here.
*
* SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with SLURM; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
# if HAVE_STDINT_H
# include <stdint.h>
# endif
# if HAVE_INTTYPES_H
# include <inttypes.h>
# endif
#endif
#include "src/common/slurm_xlator.h"
#include "src/common/slurm_selecttype_info.h"
#include "select_cons_res.h"
#include "dist_tasks.h"
#include "job_test.h"
#define NODEINFO_MAGIC 0x82aa
/* These are defined here so when we link with something other than
* the slurmctld we will have these symbols defined. They will get
* overwritten when linking with the slurmctld.
*/
#if defined (__APPLE__)
slurm_ctl_conf_t slurmctld_conf __attribute__((weak_import));
struct node_record *node_record_table_ptr __attribute__((weak_import));
List part_list __attribute__((weak_import));
List job_list __attribute__((weak_import));
int node_record_count __attribute__((weak_import));
time_t last_node_update __attribute__((weak_import));
struct switch_record *switch_record_table __attribute__((weak_import));
int switch_record_cnt __attribute__((weak_import));
bitstr_t *avail_node_bitmap __attribute__((weak_import));
bitstr_t *idle_node_bitmap __attribute__((weak_import));
uint16_t *cr_node_num_cores __attribute__((weak_import));
uint32_t *cr_node_cores_offset __attribute__((weak_import));
#else
slurm_ctl_conf_t slurmctld_conf;
struct node_record *node_record_table_ptr;
List part_list;
List job_list;
int node_record_count;
time_t last_node_update;
struct switch_record *switch_record_table;
int switch_record_cnt;
bitstr_t *avail_node_bitmap;
bitstr_t *idle_node_bitmap;
uint16_t *cr_node_num_cores;
uint32_t *cr_node_cores_offset;
#endif
/*
* These variables are required by the generic plugin interface. If they
* are not found in the plugin, the plugin loader will ignore it.
*
* plugin_name - a string giving a human-readable description of the
* plugin. There is no maximum length, but the symbol must refer to
* a valid string.
*
* plugin_type - a string suggesting the type of the plugin or its
* applicability to a particular form of data or method of data handling.
* If the low-level plugin API is used, the contents of this string are
* unimportant and may be anything. SLURM uses the higher-level plugin
* interface which requires this string to be of the form
*
* <application>/<method>
*
* where <application> is a description of the intended application of
* the plugin (e.g., "select" for SLURM node selection) and <method>
* is a description of how this plugin satisfies that application. SLURM will
* only load select plugins if the plugin_type string has a
* prefix of "select/".
*
* plugin_version - an unsigned 32-bit integer containing the Slurm version
* (major.minor.micro combined into a single number).
*/
const char plugin_name[] = "Consumable Resources (CR) Node Selection plugin";
const char plugin_type[] = "select/cons_res";
const uint32_t plugin_id = 101;
const uint32_t plugin_version = SLURM_VERSION_NUMBER;
const uint32_t pstate_version = 7; /* version control on saved state */
uint16_t cr_type = CR_CPU; /* cr_type is overwritten in init() */
bool backfill_busy_nodes = false;
bool have_dragonfly = false;
bool pack_serial_at_end = false;
bool preempt_by_part = false;
bool preempt_by_qos = false;
uint64_t select_debug_flags = 0;
uint16_t select_fast_schedule = 0;
struct part_res_record *select_part_record = NULL;
struct node_res_record *select_node_record = NULL;
struct node_use_record *select_node_usage = NULL;
static bool select_state_initializing = true;
static int select_node_cnt = 0;
static int preempt_reorder_cnt = 1;
static bool preempt_strict_order = false;
struct select_nodeinfo {
uint16_t magic; /* magic number */
uint16_t alloc_cpus;
uint32_t alloc_memory;
};
extern select_nodeinfo_t *select_p_select_nodeinfo_alloc(void);
extern int select_p_select_nodeinfo_free(select_nodeinfo_t *nodeinfo);
/* Procedure Declarations */
static int _add_job_to_res(struct job_record *job_ptr, int action);
static int _job_expand(struct job_record *from_job_ptr,
struct job_record *to_job_ptr);
static int _rm_job_from_one_node(struct job_record *job_ptr,
struct node_record *node_ptr);
static int _rm_job_from_res(struct part_res_record *part_record_ptr,
struct node_use_record *node_usage,
struct job_record *job_ptr, int action);
static int _run_now(struct job_record *job_ptr, bitstr_t *bitmap,
uint32_t min_nodes, uint32_t max_nodes,
uint32_t req_nodes, uint16_t job_node_req,
List preemptee_candidates, List *preemptee_job_list,
bitstr_t *exc_core_bitmap);
static int _sort_usable_nodes_dec(void *, void *);
static int _test_only(struct job_record *job_ptr, bitstr_t *bitmap,
uint32_t min_nodes, uint32_t max_nodes,
uint32_t req_nodes, uint16_t job_node_req);
static int _will_run_test(struct job_record *job_ptr, bitstr_t *bitmap,
uint32_t min_nodes, uint32_t max_nodes,
uint32_t req_nodes, uint16_t job_node_req,
List preemptee_candidates, List *preemptee_job_list,
bitstr_t *exc_core_bitmap);
struct sort_support {
int jstart;
struct job_resources *tmpjobs;
};
static int _compare_support(const void *, const void *);
static void _dump_job_res(struct job_resources *job) {
char str[64];
if (job->core_bitmap)
bit_fmt(str, sizeof(str), job->core_bitmap);
else
sprintf(str, "[no core_bitmap]");
info("DEBUG: Dump job_resources: nhosts %u cb %s", job->nhosts, str);
}
static void _dump_nodes(void)
{
struct node_record *node_ptr;
List gres_list;
int i;
for (i=0; i<select_node_cnt; i++) {
node_ptr = select_node_record[i].node_ptr;
info("node:%s cpus:%u c:%u s:%u t:%u mem:%u a_mem:%u state:%d",
node_ptr->name,
select_node_record[i].cpus,
select_node_record[i].cores,
select_node_record[i].sockets,
select_node_record[i].vpus,
select_node_record[i].real_memory,
select_node_usage[i].alloc_memory,
select_node_usage[i].node_state);
if (select_node_usage[i].gres_list)
gres_list = select_node_usage[i].gres_list;
else
gres_list = node_ptr->gres_list;
if (gres_list)
gres_plugin_node_state_log(gres_list, node_ptr->name);
}
}
static void _dump_part(struct part_res_record *p_ptr)
{
uint16_t i;
info("part:%s rows:%u prio:%u ", p_ptr->part_ptr->name, p_ptr->num_rows,
p_ptr->part_ptr->priority_tier);
if (!p_ptr->row)
return;
for (i = 0; i < p_ptr->num_rows; i++) {
char str[64]; /* print first 64 bits of bitmaps */
if (p_ptr->row[i].row_bitmap) {
bit_fmt(str, sizeof(str), p_ptr->row[i].row_bitmap);
} else {
sprintf(str, "[no row_bitmap]");
}
info(" row%u: num_jobs %u: bitmap: %s", i,
p_ptr->row[i].num_jobs, str);
}
}
static void _dump_state(struct part_res_record *p_ptr)
{
_dump_nodes();
/* dump partition data */
for (; p_ptr; p_ptr = p_ptr->next) {
_dump_part(p_ptr);
}
return;
}
/* Helper function for _dup_part_data: create a duplicate part_row_data array */
static struct part_row_data *_dup_row_data(struct part_row_data *orig_row,
uint16_t num_rows)
{
struct part_row_data *new_row;
int i;
if (num_rows == 0 || !orig_row)
return NULL;
new_row = xmalloc(num_rows * sizeof(struct part_row_data));
for (i = 0; i < num_rows; i++) {
new_row[i].num_jobs = orig_row[i].num_jobs;
new_row[i].job_list_size = orig_row[i].job_list_size;
if (orig_row[i].row_bitmap)
new_row[i].row_bitmap = bit_copy(orig_row[i].
row_bitmap);
if (new_row[i].job_list_size == 0)
continue;
/* copy the job list */
new_row[i].job_list = xmalloc(new_row[i].job_list_size *
sizeof(struct job_resources *));
memcpy(new_row[i].job_list, orig_row[i].job_list,
(sizeof(struct job_resources *) * new_row[i].num_jobs));
}
return new_row;
}
/* Create a duplicate part_res_record list */
static struct part_res_record *_dup_part_data(struct part_res_record *orig_ptr)
{
struct part_res_record *new_part_ptr, *new_ptr;
if (orig_ptr == NULL)
return NULL;
new_part_ptr = xmalloc(sizeof(struct part_res_record));
new_ptr = new_part_ptr;
while (orig_ptr) {
new_ptr->part_ptr = orig_ptr->part_ptr;
new_ptr->num_rows = orig_ptr->num_rows;
new_ptr->row = _dup_row_data(orig_ptr->row,
orig_ptr->num_rows);
if (orig_ptr->next) {
new_ptr->next = xmalloc(sizeof(struct part_res_record));
new_ptr = new_ptr->next;
}
orig_ptr = orig_ptr->next;
}
return new_part_ptr;
}
/* Create a duplicate part_res_record list */
static struct node_use_record *_dup_node_usage(struct node_use_record *orig_ptr)
{
struct node_use_record *new_use_ptr, *new_ptr;
List gres_list;
uint32_t i;
if (orig_ptr == NULL)
return NULL;
new_use_ptr = xmalloc(select_node_cnt * sizeof(struct node_use_record));
new_ptr = new_use_ptr;
for (i = 0; i < select_node_cnt; i++) {
new_ptr[i].node_state = orig_ptr[i].node_state;
new_ptr[i].alloc_memory = orig_ptr[i].alloc_memory;
if (orig_ptr[i].gres_list)
gres_list = orig_ptr[i].gres_list;
else
gres_list = node_record_table_ptr[i].gres_list;
new_ptr[i].gres_list = gres_plugin_node_state_dup(gres_list);
}
return new_use_ptr;
}
/* delete the given row data */
static void _destroy_row_data(struct part_row_data *row, uint16_t num_rows) {
uint16_t i;
for (i = 0; i < num_rows; i++) {
FREE_NULL_BITMAP(row[i].row_bitmap);
xfree(row[i].job_list);
}
xfree(row);
}
/* delete the given list of partition data */
static void _destroy_part_data(struct part_res_record *this_ptr)
{
while (this_ptr) {
struct part_res_record *tmp = this_ptr;
this_ptr = this_ptr->next;
tmp->part_ptr = NULL;
if (tmp->row) {
_destroy_row_data(tmp->row, tmp->num_rows);
tmp->row = NULL;
}
xfree(tmp);
}
}
/* (re)create the global select_part_record array */
static void _create_part_data(void)
{
ListIterator part_iterator;
struct part_record *p_ptr;
struct part_res_record *this_ptr;
int num_parts;
_destroy_part_data(select_part_record);
select_part_record = NULL;
num_parts = list_count(part_list);
if (!num_parts)
return;
info("cons_res: preparing for %d partitions", num_parts);
select_part_record = xmalloc(sizeof(struct part_res_record));
this_ptr = select_part_record;
part_iterator = list_iterator_create(part_list);
while ((p_ptr = (struct part_record *) list_next(part_iterator))) {
this_ptr->part_ptr = p_ptr;
this_ptr->num_rows = p_ptr->max_share;
if (this_ptr->num_rows & SHARED_FORCE)
this_ptr->num_rows &= (~SHARED_FORCE);
if (preempt_by_qos) /* Add row for QOS preemption */
this_ptr->num_rows++;
/* SHARED=EXCLUSIVE sets max_share = 0 */
if (this_ptr->num_rows < 1)
this_ptr->num_rows = 1;
/* we'll leave the 'row' array blank for now */
this_ptr->row = NULL;
num_parts--;
if (num_parts) {
this_ptr->next =xmalloc(sizeof(struct part_res_record));
this_ptr = this_ptr->next;
}
}
list_iterator_destroy(part_iterator);
/* should we sort the select_part_record list by priority here? */
}
/* List sort function: sort by the job's expected end time */
static int _cr_job_list_sort(void *x, void *y)
{
struct job_record *job1_ptr = *(struct job_record **) x;
struct job_record *job2_ptr = *(struct job_record **) y;
return (int) SLURM_DIFFTIME(job1_ptr->end_time, job2_ptr->end_time);
}
/* delete the given select_node_record and select_node_usage arrays */
static void _destroy_node_data(struct node_use_record *node_usage,
struct node_res_record *node_data)
{
int i;
xfree(node_data);
if (node_usage) {
for (i = 0; i < select_node_cnt; i++) {
FREE_NULL_LIST(node_usage[i].gres_list);
}
xfree(node_usage);
}
}
static void _add_job_to_row(struct job_resources *job,
struct part_row_data *r_ptr)
{
/* add the job to the row_bitmap */
if (r_ptr->row_bitmap && r_ptr->num_jobs == 0) {
/* if no jobs, clear the existing row_bitmap first */
uint32_t size = bit_size(r_ptr->row_bitmap);
bit_nclear(r_ptr->row_bitmap, 0, size-1);
}
add_job_to_cores(job, &(r_ptr->row_bitmap), cr_node_num_cores);
/* add the job to the job_list */
if (r_ptr->num_jobs >= r_ptr->job_list_size) {
r_ptr->job_list_size += 8;
xrealloc(r_ptr->job_list, r_ptr->job_list_size *
sizeof(struct job_resources *));
}
r_ptr->job_list[r_ptr->num_jobs++] = job;
}
/* test for conflicting core_bitmap bits */
static int _can_job_fit_in_row(struct job_resources *job,
struct part_row_data *r_ptr)
{
if ((r_ptr->num_jobs == 0) || !r_ptr->row_bitmap)
return 1;
return job_fits_into_cores(job, r_ptr->row_bitmap, cr_node_num_cores);
}
/* helper script for cr_sort_part_rows() */
static void _swap_rows(struct part_row_data *a, struct part_row_data *b)
{
struct part_row_data tmprow;
tmprow.row_bitmap = a->row_bitmap;
tmprow.num_jobs = a->num_jobs;
tmprow.job_list = a->job_list;
tmprow.job_list_size = a->job_list_size;
a->row_bitmap = b->row_bitmap;
a->num_jobs = b->num_jobs;
a->job_list = b->job_list;
a->job_list_size = b->job_list_size;
b->row_bitmap = tmprow.row_bitmap;
b->num_jobs = tmprow.num_jobs;
b->job_list = tmprow.job_list;
b->job_list_size = tmprow.job_list_size;
return;
}
/* sort the rows of a partition from "most allocated" to "least allocated" */
extern void cr_sort_part_rows(struct part_res_record *p_ptr)
{
uint32_t i, j, a, b;
if (!p_ptr->row)
return;
for (i = 0; i < p_ptr->num_rows; i++) {
if (p_ptr->row[i].row_bitmap)
a = bit_set_count(p_ptr->row[i].row_bitmap);
else
a = 0;
for (j = i+1; j < p_ptr->num_rows; j++) {
if (!p_ptr->row[j].row_bitmap)
continue;
b = bit_set_count(p_ptr->row[j].row_bitmap);
if (b > a) {
_swap_rows(&(p_ptr->row[i]), &(p_ptr->row[j]));
}
}
}
return;
}
/*
* _build_row_bitmaps: A job has been removed from the given partition,
* so the row_bitmap(s) need to be reconstructed.
* Optimize the jobs into the least number of rows,
* and make the lower rows as dense as possible.
*
* IN/OUT: p_ptr - the partition that has jobs to be optimized
*/
static void _build_row_bitmaps(struct part_res_record *p_ptr,
struct job_record *job_ptr)
{
uint32_t i, j, num_jobs, size;
int x;
struct part_row_data *this_row, *orig_row;
struct sort_support *ss;
if (!p_ptr->row)
return;
if (p_ptr->num_rows == 1) {
this_row = &(p_ptr->row[0]);
if (this_row->num_jobs == 0) {
if (this_row->row_bitmap) {
size = bit_size(this_row->row_bitmap);
bit_nclear(this_row->row_bitmap, 0, size-1);
}
} else {
if (job_ptr) { /* just remove the job */
xassert(job_ptr->job_resrcs);
remove_job_from_cores(job_ptr->job_resrcs,
&(this_row->row_bitmap),
cr_node_num_cores);
} else { /* totally rebuild the bitmap */
size = bit_size(this_row->row_bitmap);
bit_nclear(this_row->row_bitmap, 0, size-1);
for (j = 0; j < this_row->num_jobs; j++) {
add_job_to_cores(this_row->job_list[j],
&(this_row->row_bitmap),
cr_node_num_cores);
}
}
}
return;
}
/* gather data */
num_jobs = 0;
for (i = 0; i < p_ptr->num_rows; i++) {
if (p_ptr->row[i].num_jobs) {
num_jobs += p_ptr->row[i].num_jobs;
}
}
if (num_jobs == 0) {
size = bit_size(p_ptr->row[0].row_bitmap);
for (i = 0; i < p_ptr->num_rows; i++) {
if (p_ptr->row[i].row_bitmap) {
bit_nclear(p_ptr->row[i].row_bitmap, 0,
size-1);
}
}
return;
}
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE) {
info("DEBUG: _build_row_bitmaps (before):");
_dump_part(p_ptr);
}
debug3("cons_res: build_row_bitmaps reshuffling %u jobs", num_jobs);
/* make a copy, in case we cannot do better than this */
orig_row = _dup_row_data(p_ptr->row, p_ptr->num_rows);
if (orig_row == NULL)
return;
/* get row_bitmap size from first row (we can safely assume that the
* first row_bitmap exists because there exists at least one job. */
size = bit_size(p_ptr->row[0].row_bitmap);
/* create a master job list and clear out ALL row data */
ss = xmalloc(num_jobs * sizeof(struct sort_support));
x = 0;
for (i = 0; i < p_ptr->num_rows; i++) {
for (j = 0; j < p_ptr->row[i].num_jobs; j++) {
ss[x].tmpjobs = p_ptr->row[i].job_list[j];
p_ptr->row[i].job_list[j] = NULL;
ss[x].jstart = bit_ffs(ss[x].tmpjobs->node_bitmap);
ss[x].jstart = cr_get_coremap_offset(ss[x].jstart);
ss[x].jstart += bit_ffs(ss[x].tmpjobs->core_bitmap);
x++;
}
p_ptr->row[i].num_jobs = 0;
if (p_ptr->row[i].row_bitmap) {
bit_nclear(p_ptr->row[i].row_bitmap, 0, size-1);
}
}
/* VERY difficult: Optimal placement of jobs in the matrix
* - how to order jobs to be added to the matrix?
* - "by size" does not guarantee optimal placement
*
* - for now, try sorting jobs by first bit set
* - if job allocations stay "in blocks", then this should work OK
* - may still get scenarios where jobs should switch rows
* - fixme: JOB SHUFFLING BETWEEN ROWS NEEDS TESTING
*/
qsort(ss, num_jobs, sizeof(struct sort_support), _compare_support);
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE) {
for (i = 0; i < num_jobs; i++) {
char cstr[64], nstr[64];
if (ss[i].tmpjobs->core_bitmap) {
bit_fmt(cstr, (sizeof(cstr)-1) ,
ss[i].tmpjobs->core_bitmap);
} else
sprintf(cstr, "[no core_bitmap]");
if (ss[i].tmpjobs->node_bitmap) {
bit_fmt(nstr, (sizeof(nstr)-1),
ss[i].tmpjobs->node_bitmap);
} else
sprintf(nstr, "[no node_bitmap]");
info("DEBUG: jstart %d job nb %s cb %s",
ss[i].jstart, nstr, cstr);
}
}
/* add jobs to the rows */
for (j = 0; j < num_jobs; j++) {
for (i = 0; i < p_ptr->num_rows; i++) {
if (_can_job_fit_in_row(ss[j].tmpjobs,
&(p_ptr->row[i]))) {
/* job fits in row, so add it */
_add_job_to_row(ss[j].tmpjobs,&(p_ptr->row[i]));
ss[j].tmpjobs = NULL;
break;
}
}
/* job should have been added, so shuffle the rows */
cr_sort_part_rows(p_ptr);
}
/* test for dangling jobs */
for (j = 0; j < num_jobs; j++) {
if (ss[j].tmpjobs)
break;
}
if (j < num_jobs) {
/* we found a dangling job, which means our packing
* algorithm couldn't improve apon the existing layout.
* Thus, we'll restore the original layout here */
debug3("cons_res: build_row_bitmap: dangling job found");
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE) {
info("DEBUG: _build_row_bitmaps (post-algorithm):");
_dump_part(p_ptr);
}
_destroy_row_data(p_ptr->row, p_ptr->num_rows);
p_ptr->row = orig_row;
orig_row = NULL;
/* still need to rebuild row_bitmaps */
for (i = 0; i < p_ptr->num_rows; i++) {
if (p_ptr->row[i].row_bitmap)
bit_nclear(p_ptr->row[i].row_bitmap, 0,
size-1);
if (p_ptr->row[i].num_jobs == 0)
continue;
for (j = 0; j < p_ptr->row[i].num_jobs; j++) {
add_job_to_cores(p_ptr->row[i].job_list[j],
&(p_ptr->row[i].row_bitmap),
cr_node_num_cores);
}
}
}
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE) {
info("DEBUG: _build_row_bitmaps (after):");
_dump_part(p_ptr);
}
if (orig_row)
_destroy_row_data(orig_row, p_ptr->num_rows);
xfree(ss);
return;
/* LEFTOVER DESIGN THOUGHTS, PRESERVED HERE */
/* 1. sort jobs by size
* 2. only load core bitmaps with largest jobs that conflict
* 3. sort rows by set count
* 4. add remaining jobs, starting with fullest rows
* 5. compute set count: if disparity between rows got closer, then
* switch non-conflicting jobs that were added
*/
/*
* Step 1: remove empty rows between non-empty rows
* Step 2: try to collapse rows
* Step 3: sort rows by size
* Step 4: try to swap jobs from different rows to pack rows
*/
/* WORK IN PROGRESS - more optimization should go here, such as:
*
* - try collapsing jobs from higher rows to lower rows
*
* - produce a load array to identify cores with less load. Test
* to see if those cores are in the lower row. If not, try to swap
* those jobs with jobs in the lower row. If the job can be swapped
* AND the lower row set_count increases, then SUCCESS! else swap
* back. The goal is to pack the lower rows and "bubble up" clear
* bits to the higher rows.
*/
}
/* allocate resources to the given job
* - add 'struct job_resources' resources to 'struct part_res_record'
* - add job's memory requirements to 'struct node_res_record'
*
* if action = 0 then add cores, memory + GRES (starting new job)
* if action = 1 then add memory + GRES (adding suspended job)
* if action = 2 then only add cores (suspended job is resumed)
*/
static int _add_job_to_res(struct job_record *job_ptr, int action)
{
struct job_resources *job = job_ptr->job_resrcs;
struct node_record *node_ptr;
struct part_res_record *p_ptr;
List gres_list;
int i, n;
bitstr_t *core_bitmap;
if (!job || !job->core_bitmap) {
error("%s: job %u has no job_resrcs info",
__func__, job_ptr->job_id);
return SLURM_ERROR;
}
debug3("cons_res: _add_job_to_res: job %u act %d ", job_ptr->job_id,
action);
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE)
_dump_job_res(job);
for (i = 0, n = -1; i < select_node_cnt; i++) {
if (!bit_test(job->node_bitmap, i))
continue;
n++;
if (job->cpus[n] == 0)
continue; /* node lost by job resize */
node_ptr = select_node_record[i].node_ptr;
if (action != 2) {
if (select_node_usage[i].gres_list)
gres_list = select_node_usage[i].gres_list;
else
gres_list = node_ptr->gres_list;
core_bitmap = copy_job_resources_node(job, n);
gres_plugin_job_alloc(job_ptr->gres_list, gres_list,
job->nhosts, n, job->cpus[n],
job_ptr->job_id, node_ptr->name,
core_bitmap);
gres_plugin_node_state_log(gres_list, node_ptr->name);
FREE_NULL_BITMAP(core_bitmap);
}
if (action != 2) {
if (job->memory_allocated[n] == 0)
continue; /* node lost by job resizing */
select_node_usage[i].alloc_memory +=
job->memory_allocated[n];
if ((select_node_usage[i].alloc_memory >
select_node_record[i].real_memory)) {
error("cons_res: node %s memory is "
"overallocated (%u) for job %u",
node_ptr->name,
select_node_usage[i].alloc_memory,
job_ptr->job_id);
}
}
if ((powercap_get_cluster_current_cap() != 0) &&
(which_power_layout() == 2)) {
adapt_layouts(job, job_ptr->details->cpu_freq_max, n,
node_ptr->name, true);
}
}
/* add cores */
if (action != 1) {
for (p_ptr = select_part_record; p_ptr; p_ptr = p_ptr->next) {
if (p_ptr->part_ptr == job_ptr->part_ptr)
break;
}
if (!p_ptr) {
error("cons_res: could not find cr partition %s",
job_ptr->part_ptr->name);
return SLURM_ERROR;
}
if (!p_ptr->row) {
p_ptr->row = xmalloc(p_ptr->num_rows *
sizeof(struct part_row_data));
}
/* find a row to add this job */
for (i = 0; i < p_ptr->num_rows; i++) {
if (!_can_job_fit_in_row(job, &(p_ptr->row[i])))
continue;
debug3("cons_res: adding job %u to part %s row %u",
job_ptr->job_id, p_ptr->part_ptr->name, i);
_add_job_to_row(job, &(p_ptr->row[i]));
break;
}
if (i >= p_ptr->num_rows) {
/* Job started or resumed and it's allocated resources
* are already in use by some other job. Typically due
* to manually resuming a job. */
error("cons_res: job overflow: "
"could not find idle resources for job %u",
job_ptr->job_id);
/* No row available to record this job */
}
/* update the node state */
for (i = 0, n = -1; i < select_node_cnt; i++) {
if (bit_test(job->node_bitmap, i)) {
n++;
if (job->cpus[n] == 0)
continue; /* node lost by job resize */
select_node_usage[i].node_state +=
job->node_req;
}
}
if (select_debug_flags & DEBUG_FLAG_SELECT_TYPE) {
info("DEBUG: _add_job_to_res (after):");
_dump_part(p_ptr);
}
}
return SLURM_SUCCESS;
}
static job_resources_t *_create_job_resources(int node_cnt)
{
job_resources_t *job_resrcs_ptr;
job_resrcs_ptr = create_job_resources();
job_resrcs_ptr->cpu_array_reps = xmalloc(sizeof(uint32_t) * node_cnt);
job_resrcs_ptr->cpu_array_value = xmalloc(sizeof(uint16_t) * node_cnt);
job_resrcs_ptr->cpus = xmalloc(sizeof(uint16_t) * node_cnt);
job_resrcs_ptr->cpus_used = xmalloc(sizeof(uint16_t) * node_cnt);
job_resrcs_ptr->memory_allocated = xmalloc(sizeof(uint32_t) * node_cnt);
job_resrcs_ptr->memory_used = xmalloc(sizeof(uint32_t) * node_cnt);
job_resrcs_ptr->nhosts = node_cnt;
return job_resrcs_ptr;
}
/* Move all resources from one job to another */
static int _job_expand(struct job_record *from_job_ptr,
struct job_record *to_job_ptr)
{
job_resources_t *from_job_resrcs_ptr, *to_job_resrcs_ptr,
*new_job_resrcs_ptr;
struct node_record *node_ptr;
int first_bit, last_bit, i, node_cnt;
bool from_node_used, to_node_used;
int from_node_offset, to_node_offset, new_node_offset;
bitstr_t *tmp_bitmap, *tmp_bitmap2;
xassert(from_job_ptr);
xassert(to_job_ptr);
if (from_job_ptr->job_id == to_job_ptr->job_id) {
error("select/cons_res: attempt to merge job %u with self",
from_job_ptr->job_id);
return SLURM_ERROR;
}
from_job_resrcs_ptr = from_job_ptr->job_resrcs;
if ((from_job_resrcs_ptr == NULL) ||
(from_job_resrcs_ptr->cpus == NULL) ||
(from_job_resrcs_ptr->core_bitmap == NULL) ||
(from_job_resrcs_ptr->node_bitmap == NULL)) {
error("select/cons_res: job %u lacks a job_resources struct",
from_job_ptr->job_id);
return SLURM_ERROR;
}
to_job_resrcs_ptr = to_job_ptr->job_resrcs;
if ((to_job_resrcs_ptr == NULL) ||
(to_job_resrcs_ptr->cpus == NULL) ||
(to_job_resrcs_ptr->core_bitmap == NULL) ||
(to_job_resrcs_ptr->node_bitmap == NULL)) {
error("select/cons_res: job %u lacks a job_resources struct",
to_job_ptr->job_id);
return SLURM_ERROR;
}
(void) _rm_job_from_res(select_part_record, select_node_usage,
from_job_ptr, 0);
(void) _rm_job_from_res(select_part_record, select_node_usage,
to_job_ptr, 0);
if (to_job_resrcs_ptr->core_bitmap_used) {
i = bit_size(to_job_resrcs_ptr->core_bitmap_used);
bit_nclear(to_job_resrcs_ptr->core_bitmap_used, 0, i-1);
}
tmp_bitmap = bit_copy(to_job_resrcs_ptr->node_bitmap);
bit_or(tmp_bitmap, from_job_resrcs_ptr->node_bitmap);
tmp_bitmap2 = bit_copy(to_job_ptr->node_bitmap);
bit_or(tmp_bitmap2, from_job_ptr->node_bitmap);
bit_and(tmp_bitmap, tmp_bitmap2);
bit_free(tmp_bitmap2);
node_cnt = bit_set_count(tmp_bitmap);
new_job_resrcs_ptr = _create_job_resources(node_cnt);
new_job_resrcs_ptr->ncpus = from_job_resrcs_ptr->ncpus +
to_job_resrcs_ptr->ncpus;
new_job_resrcs_ptr->node_req = to_job_resrcs_ptr->node_req;
new_job_resrcs_ptr->node_bitmap = tmp_bitmap;
new_job_resrcs_ptr->nodes = bitmap2node_name(new_job_resrcs_ptr->
node_bitmap);
new_job_resrcs_ptr->whole_node = to_job_resrcs_ptr->whole_node;
build_job_resources(new_job_resrcs_ptr, node_record_table_ptr,
select_fast_schedule);
xfree(to_job_ptr->node_addr);
to_job_ptr->node_addr = xmalloc(sizeof(slurm_addr_t) * node_cnt);
to_job_ptr->total_cpus = 0;
first_bit = MIN(bit_ffs(from_job_resrcs_ptr->node_bitmap),
bit_ffs(to_job_resrcs_ptr->node_bitmap));
last_bit = MAX(bit_fls(from_job_resrcs_ptr->node_bitmap),
bit_fls(to_job_resrcs_ptr->node_bitmap));
from_node_offset = to_node_offset = new_node_offset = -1;
for (i = first_bit; i <= last_bit; i++) {
from_node_used = to_node_used = false;
if (bit_test(from_job_resrcs_ptr->node_bitmap, i)) {
from_node_used = bit_test(from_job_ptr->node_bitmap,i);
from_node_offset++;
}