This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanner.c
More file actions
1275 lines (1161 loc) · 40.1 KB
/
planner.c
File metadata and controls
1275 lines (1161 loc) · 40.1 KB
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
/*****************************************************************************\
* Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS).
* LLNL-CODE-658032 All rights reserved.
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* This program 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.
*
* Flux 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 terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses/
\*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <czmq.h>
#include "rbtree.h"
#include "rbtree_augmented.h"
#include "xzmalloc.h"
#include "planner.h"
#define START(node) ((node)->start)
#define LAST(node) ((node)->last)
typedef struct span span_t;
typedef int64_t resource_array_t[PLANNER_NUM_TYPES];
typedef char *resource_type_array_t[PLANNER_NUM_TYPES];
typedef struct request {
int64_t on_or_after;
uint64_t duration;
resource_array_t resources;
size_t dimension;
} request_t;
/*! Scheduled point: time at which resource state changes. Each point's resource
* requirements are tracked as a node in a min-time resource (MTR) binary search
* tree.
*/
typedef struct scheduled_point {
struct rb_node point_rb; /* BST node for scheduled point tree */
struct rb_node resource_rb; /* BST node for min-time resource tree */
int64_t subtree_min; /* Min time of the subtree of this node */
int64_t at; /* Resource-state changing time */
int in_mt_resource_tree; /* 1 when inserted in min-time resource tree */
int new_point; /* 1 when this point is newly created */
int ref_count; /* reference counter */
resource_array_t scheduled; /* scheduled resources at this point */
resource_array_t remaining; /* remaining resources (available) */
} scheduled_point_t;
/*! Node in a span interval tree to enable fast retrieval of intercepting spans.
*/
struct span {
int64_t start; /* start time of the span */
int64_t last; /* end time of the span */
int64_t span_id; /* unique span id */
resource_array_t planned; /* required resources */
size_t dimension; /* vector size of required resources */
int in_system; /* 1 when inserted into the system */
scheduled_point_t *start_p; /* scheduled point object at start */
scheduled_point_t *last_p; /* scheduled point object at last */
};
/*! Planner context
*/
struct planner {
resource_array_t total_resources;
resource_type_array_t resource_types;
size_t dimension; /* size of the above arrays */
int64_t plan_start; /* base time of the planner */
int64_t plan_end; /* end time of the planner */
struct rb_root sched_point_tree; /* scheduled point rb tree */
struct rb_root mt_resource_tree; /* min-time resrouce rb tree */
scheduled_point_t *p0; /* system's scheduled point at base time */
zhashx_t *span_lookup; /* span lookup table by string id */
zhash_t *avail_time_iter; /* tracking nodes temporarily deleted from MTR */
request_t *current_request; /* the req copy for avail time iteration */
int avail_time_iter_set; /* iterator set flag */
uint64_t span_counter; /* current span counter */
};
/*******************************************************************************
* *
* INTERNAL PLANNER API *
* *
*******************************************************************************/
/*******************************************************************************
* *
* Scheduled Points Binary Search Tree: O(log n) Scheduled Points Search *
* *
*******************************************************************************/
static scheduled_point_t *scheduled_point_search (int64_t t, struct rb_root *root)
{
struct rb_node *node = root->rb_node;
while (node) {
scheduled_point_t *this_data = NULL;
this_data = container_of (node, scheduled_point_t, point_rb);
int64_t result = t - this_data->at;
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return this_data;
}
return NULL;
}
static inline scheduled_point_t *recent_state (scheduled_point_t *new_data,
scheduled_point_t *old_data)
{
if (!old_data)
return new_data;
return (new_data->at > old_data->at)? new_data : old_data;
}
/*! While scheduled_point_search returns the exact match scheduled_point_state
* returns the most recent scheduled point, representing the accurate resource
* state at the time t.
*/
static scheduled_point_t *scheduled_point_state (int64_t at, struct rb_root *root)
{
scheduled_point_t *last_state = NULL;
struct rb_node *node = root->rb_node;
while (node) {
scheduled_point_t *this_data = NULL;
this_data = container_of (node, scheduled_point_t, point_rb);
int64_t result = at - this_data->at;
if (result < 0) {
node = node->rb_left;
} else if (result > 0) {
last_state = recent_state (this_data, last_state);
node = node->rb_right;
} else {
return this_data;
}
}
return last_state;
}
static int scheduled_point_insert (scheduled_point_t *new_data,
struct rb_root *root)
{
struct rb_node **link = &(root->rb_node);
struct rb_node *parent = NULL;
while (*link) {
scheduled_point_t *this_data = NULL;
this_data = container_of (*link, scheduled_point_t, point_rb);
int64_t result = new_data->at - this_data->at;
parent = *link;
if (result < 0)
link = &((*link)->rb_left);
else if (result > 0)
link = &((*link)->rb_right);
else
return -1;
}
rb_link_node (&(new_data->point_rb), parent, link);
rb_insert_color (&(new_data->point_rb), root);
return 0;
}
static int scheduled_point_remove (scheduled_point_t *data, struct rb_root *root)
{
int rc = -1;
scheduled_point_t *n = scheduled_point_search (data->at, root);
if (n) {
rb_erase (&(n->point_rb), root);
// Note: this must only remove the node from the scheduled point tree:
// DO NOT free memory allocated to the node
rc = 0;
}
return rc;
}
static void scheduled_points_destroy (struct rb_node *node)
{
if (node->rb_left)
scheduled_points_destroy (node->rb_left);
if (node->rb_right)
scheduled_points_destroy (node->rb_right);
scheduled_point_t *data = container_of (node, scheduled_point_t, point_rb);
free (data);
}
/*******************************************************************************
* *
* Minimum Time Resource Tree: O(log n) Earliest Schedulable Point Search *
* *
*******************************************************************************/
static int64_t mintime_resource_subtree_min (scheduled_point_t *point)
{
int64_t min = point->at;
scheduled_point_t *p = NULL;
if (point->resource_rb.rb_left) {
p = rb_entry (point->resource_rb.rb_left, scheduled_point_t, resource_rb);
if (min > p->subtree_min)
min = p->subtree_min;
}
if (point->resource_rb.rb_right) {
p = rb_entry (point->resource_rb.rb_right, scheduled_point_t, resource_rb);
if (min > p->subtree_min)
min = p->subtree_min;
}
return min;
}
static void mintime_resource_propagate (struct rb_node *n, struct rb_node *stop)
{
int64_t subtree_min;
while (n != stop) {
scheduled_point_t *point = rb_entry (n, scheduled_point_t, resource_rb);
subtree_min = mintime_resource_subtree_min (point);
if (point->subtree_min == subtree_min)
break;
point->subtree_min = subtree_min;
n = rb_parent (&(point->resource_rb));
}
}
static void mintime_resource_copy (struct rb_node *src, struct rb_node *dst)
{
scheduled_point_t *o = rb_entry (src, scheduled_point_t, resource_rb);
scheduled_point_t *n = rb_entry (dst, scheduled_point_t, resource_rb);
n->subtree_min = o->subtree_min;
}
static void mintime_resource_rotate (struct rb_node *src, struct rb_node *dst)
{
scheduled_point_t *o = rb_entry (src, scheduled_point_t, resource_rb);
scheduled_point_t *n = rb_entry (dst, scheduled_point_t, resource_rb);
n->subtree_min = o->subtree_min;
o->subtree_min = mintime_resource_subtree_min (o);
}
static const struct rb_augment_callbacks mintime_resource_aug_cb = {
mintime_resource_propagate, mintime_resource_copy, mintime_resource_rotate
};
static int64_t rescmp (const int64_t *s1, const resource_array_t s2, size_t len)
{
int i = 0;
int less = 0;
int64_t r = 0;
for (i = 0; i < len; ++i) {
if ( (r = s1[i] - s2[i]) > 0)
break;
less += r;
}
return (r > 0)? r : less;
}
static void mintime_resource_insert (scheduled_point_t *new_data,
unsigned int len, struct rb_root *root)
{
struct rb_node **link = &(root->rb_node);
scheduled_point_t *this_data = NULL;
struct rb_node *parent = NULL;
while (*link) {
this_data = rb_entry (*link, scheduled_point_t, resource_rb);
parent = *link;
if (this_data->subtree_min > new_data->at)
this_data->subtree_min = new_data->at;
if (rescmp (new_data->remaining, this_data->remaining, len) < 0)
link = &(this_data->resource_rb.rb_left);
else
link = &(this_data->resource_rb.rb_right);
}
new_data->subtree_min = new_data->at;
new_data->in_mt_resource_tree = 1;
rb_link_node (&(new_data->resource_rb), parent, link);
rb_insert_augmented (&(new_data->resource_rb), root,
&mintime_resource_aug_cb);
}
static void mintime_resource_remove (scheduled_point_t *data,
struct rb_root *root)
{
rb_erase_augmented (&data->resource_rb, root, &mintime_resource_aug_cb);
data->in_mt_resource_tree = 0;
}
static int64_t right_branch_mintime (struct rb_node *n)
{
int64_t min_time = INT64_MAX;
struct rb_node *right = n->rb_right;
if (right)
min_time = rb_entry (right, scheduled_point_t, resource_rb)->subtree_min;
scheduled_point_t *this_data = rb_entry (n, scheduled_point_t, resource_rb);
return (this_data->at < min_time)? this_data->at : min_time;
}
static scheduled_point_t *find_mintime_point (struct rb_node *anchor,
int64_t min_time)
{
if (!anchor)
return NULL;
scheduled_point_t *this_data = NULL;
this_data = rb_entry (anchor, scheduled_point_t, resource_rb);
if (this_data->at == min_time)
return this_data;
struct rb_node *node = anchor->rb_right;
while (node) {
this_data = rb_entry (node, scheduled_point_t, resource_rb);
if (this_data->at == min_time)
return this_data;
if (node->rb_left
&& (rb_entry(node->rb_left, scheduled_point_t,
resource_rb)->subtree_min == min_time))
node = node->rb_left;
else
node = node->rb_right;
}
// Error condition: when an anchor was found, there must be
// a point that meets the requirements.
errno = ENOTSUP;
return NULL;
}
static int64_t find_mintime_anchor (const int64_t *ra, size_t len,
struct rb_root *mtrt,
struct rb_node **anchor_p)
{
struct rb_node *node = mtrt->rb_node;
int64_t min_time = INT64_MAX;
int64_t right_min_time = INT64_MAX;
while (node) {
scheduled_point_t *this_data = NULL;
this_data = rb_entry (node, scheduled_point_t, resource_rb);
int64_t result = 0;
result = rescmp (ra, this_data->remaining, len);
if (result <= 0) {
// visiting node satisfies the resource requirements. This means all
// nodes at its subtree also satisfy the requirements. Thus,
// right_min_time is the best min time.
right_min_time = right_branch_mintime (node);
if (right_min_time < min_time) {
min_time = right_min_time;
*anchor_p = node;
}
// next, we should search the left subtree for potentially better
// then current min_time;
node = node->rb_left;
} else {
// visiting node does not satisfy the resource requirements. This
// means that nothing in its left branch will meet these requirements:
// time to search the right subtree.
node = node->rb_right;
}
}
return min_time;
}
static scheduled_point_t *mintime_resource_mintime (const int64_t *ra, size_t len,
struct rb_root *mtrt)
{
struct rb_node *anchor = NULL;
int64_t min_time = find_mintime_anchor (ra, len, mtrt, &anchor);
return find_mintime_point (anchor, min_time);
}
/*******************************************************************************
* *
* Scheduled Point and Resource Update APIs *
* *
*******************************************************************************/
static int track_points (zhash_t *tracker, scheduled_point_t *point)
{
char key[32];
sprintf (key, "%jd", (intmax_t)point->at);
// caller will rely on the fact that rc == -1 when key already exists.
// don't need to register free */
return zhash_insert (tracker, key, point);
}
static void restore_track_points (planner_t *ctx)
{
scheduled_point_t *point = NULL;
struct rb_root *root = &(ctx->mt_resource_tree);
zlist_t *keys = zhash_keys (ctx->avail_time_iter);
const char *k = NULL;
for (k = zlist_first (keys); k; k = zlist_next (keys)) {
point = zhash_lookup (ctx->avail_time_iter, k);
mintime_resource_insert (point, ctx->dimension, root);
zhash_delete (ctx->avail_time_iter, k);
}
zlist_destroy (&keys);
}
static void update_mintime_resource_tree (planner_t *ctx, zlist_t *list)
{
scheduled_point_t *point = NULL;
struct rb_root *mtrt = &(ctx->mt_resource_tree);
for (point = zlist_first (list); point; point = zlist_next (list)) {
if (point->in_mt_resource_tree)
mintime_resource_remove (point, mtrt);
if (point->ref_count && !(point->in_mt_resource_tree))
mintime_resource_insert (point, ctx->dimension, mtrt);
}
}
static void copy_req (request_t *dest, int64_t on_or_after, uint64_t duration,
const uint64_t *resource_counts, size_t len)
{
int i = 0;
dest->on_or_after = on_or_after;
dest->duration = duration;
dest->dimension = len;
for (i = 0; i < len; ++i)
dest->resources[i] = (int64_t)resource_counts[i];
}
static scheduled_point_t *get_or_new_point (planner_t *ctx, int64_t at)
{
struct rb_root *spt = &(ctx->sched_point_tree);
scheduled_point_t *point = NULL;
if ( !(point = scheduled_point_search (at, spt))) {
struct rb_root *mtrt = &(ctx->mt_resource_tree);
scheduled_point_t *state = scheduled_point_state (at, spt);
point = xzmalloc (sizeof (*point));
point->at = at;
point->in_mt_resource_tree = 0;
point->new_point = 1;
point->ref_count = 1;
memcpy (point->scheduled, state->scheduled, sizeof (point->scheduled));
memcpy (point->remaining, state->remaining, sizeof(point->remaining));
scheduled_point_insert (point, spt);
mintime_resource_insert (point, ctx->dimension, mtrt);
}
return point;
}
static void fetch_overlap_points (planner_t *ctx, int64_t at, uint64_t duration,
zlist_t *list)
{
struct rb_root *spr = &(ctx->sched_point_tree);
scheduled_point_t *point = scheduled_point_state (at, spr);
while (point) {
if (point->at >= (at + (int64_t)duration))
break;
else if (point->at >= at)
zlist_append (list, (void *)point);
struct rb_node *n = rb_next (&(point->point_rb));
point = rb_entry (n, scheduled_point_t, point_rb);
}
}
static int update_points_add_span (planner_t *ctx, zlist_t *list, span_t *span)
{
int rc = 0;
scheduled_point_t *point = NULL;
for (point = zlist_first (list); point; point = zlist_next (list)) {
int i = 0;
if (!(point->new_point))
point->ref_count++;
for (i = 0; i < span->dimension; ++i) {
point->scheduled[i] += span->planned[i];
point->remaining[i] -= span->planned[i];
if ( (point->scheduled[i] > ctx->total_resources[i])
|| (point->remaining[i] < 0)) {
errno = ERANGE;
rc = -1;
}
}
}
return rc;
}
static int update_points_subtract_span (planner_t *ctx, zlist_t *list,
span_t *span)
{
int rc = 0;
scheduled_point_t *point = NULL;
for (point = zlist_first (list); point; point = zlist_next (list)) {
int i = 0;
point->ref_count--;
for (i = 0; i < span->dimension; ++i) {
point->scheduled[i] -= span->planned[i];
point->remaining[i] += span->planned[i];
if ( (point->scheduled[i] < 0)
|| (point->remaining[i] > ctx->total_resources[i])) {
errno = ERANGE;
rc = -1;
}
}
}
return rc;
}
static bool span_ok (planner_t *ctx, scheduled_point_t *start_point,
uint64_t duration, const int64_t *resource_counts,
size_t len)
{
bool ok = true;
struct rb_root *mtrt = &(ctx->mt_resource_tree);
scheduled_point_t *next_point = NULL;
struct rb_node *n = &(start_point->point_rb);
while ((next_point = rb_entry (n, scheduled_point_t, point_rb))) {
if (next_point->at >= (start_point->at + (int64_t)duration)) {
ok = true;
break;
} else if (rescmp (resource_counts, next_point->remaining, len) > 0) {
mintime_resource_remove (start_point, mtrt);
track_points (ctx->avail_time_iter, start_point);
ok = false;
break;
}
n = rb_next (&(next_point->point_rb));
}
return ok;
}
static int64_t avail_at (planner_t *ctx, int64_t on_or_after, uint64_t duration,
const int64_t *resource_counts, size_t len)
{
int64_t at = -1;
scheduled_point_t *start_point = NULL;
struct rb_root *mt = &(ctx->mt_resource_tree);
while ((start_point = mintime_resource_mintime (resource_counts, len, mt))) {
at = start_point->at;
if (at < on_or_after) {
mintime_resource_remove (start_point, mt);
track_points (ctx->avail_time_iter, start_point);
at = -1;
} else if (span_ok (ctx, start_point, duration, resource_counts, len)) {
mintime_resource_remove (start_point, mt);
track_points (ctx->avail_time_iter, start_point);
if ((at + duration) > ctx->plan_end)
at = -1;
break;
}
}
return at;
}
static bool avail_during (planner_t *ctx, int64_t at, uint64_t duration,
const int64_t *resource_counts, size_t len)
{
bool ok = true;
struct rb_root *spr = NULL;
if ((at + duration) > ctx->plan_end) {
errno = ERANGE;
return -1;
}
spr = &(ctx->sched_point_tree);
scheduled_point_t *point = scheduled_point_state (at, spr);
while (point) {
if (point->at >= (at + (int64_t)duration)) {
ok = true;
break;
} else if (rescmp (resource_counts, point->remaining, len) > 0) {
ok = false;
break;
}
struct rb_node *n = rb_next (&(point->point_rb));
point = rb_entry (n, scheduled_point_t, point_rb);
}
return ok;
}
static scheduled_point_t *avail_resources_during (planner_t *ctx, int64_t at,
uint64_t duration)
{
struct rb_root *spr = NULL;
if ((at + duration) > ctx->plan_end) {
errno = ERANGE;
return NULL;
}
spr = &(ctx->sched_point_tree);
scheduled_point_t *point = scheduled_point_state (at, spr);
scheduled_point_t *min = point;
while (point) {
if (point->at >= (at + (int64_t)duration)) {
break;
} else if (rescmp(min->remaining, point->remaining,
PLANNER_NUM_TYPES) > 0) {
min = point;
}
struct rb_node *n = rb_next (&(point->point_rb));
point = rb_entry (n, scheduled_point_t, point_rb);
}
return min;
}
/*******************************************************************************
* *
* Utilities *
* *
*******************************************************************************/
static void initialize (planner_t *ctx, int64_t base_time, uint64_t duration)
{
int i = 0;
ctx->plan_start = base_time;
ctx->plan_end = base_time + (int64_t)duration;
ctx->sched_point_tree = RB_ROOT;
ctx->mt_resource_tree = RB_ROOT;
ctx->p0 = xzmalloc (sizeof (*(ctx->p0)));
ctx->p0->at = base_time;
ctx->p0->ref_count = 1;
memset (ctx->p0->scheduled, '\0', sizeof (ctx->p0->scheduled));
memset (ctx->p0->remaining, '\0', sizeof (ctx->p0->remaining));
for (i = 0; i < ctx->dimension; ++i)
ctx->p0->remaining[i] = ctx->total_resources[i];
scheduled_point_insert (ctx->p0, &(ctx->sched_point_tree));
mintime_resource_insert (ctx->p0, ctx->dimension, &(ctx->mt_resource_tree));
ctx->span_lookup = zhashx_new ();
ctx->avail_time_iter = zhash_new ();
ctx->current_request = xzmalloc (sizeof (*(ctx->current_request)));
ctx->avail_time_iter_set = 0;
ctx->span_counter = 0;
}
static inline void erase (planner_t *ctx)
{
int i = 0;
struct rb_node *n = NULL;
if (ctx->span_lookup)
zhashx_purge (ctx->span_lookup);
zhashx_destroy (&(ctx->span_lookup));
for (i = 0; i < ctx->dimension; i++)
if (ctx->resource_types[i])
free (ctx->resource_types[i]);
if (ctx->avail_time_iter) {
zhash_destroy (&ctx->avail_time_iter);
ctx->avail_time_iter = NULL;
}
if (ctx->current_request) {
free (ctx->current_request);
ctx->current_request = NULL;
}
if (ctx->p0 && ctx->p0->in_mt_resource_tree)
mintime_resource_remove (ctx->p0, &(ctx->mt_resource_tree));
if ((n = ctx->sched_point_tree.rb_node))
scheduled_points_destroy (n);
}
static inline bool not_feasable (planner_t *ctx, int64_t start_time,
uint64_t duration,
const int64_t *resource_counts, size_t len)
{
bool rc = (start_time < ctx->plan_start) || (duration < 1)
|| ((start_time + duration - 1) > ctx->plan_end)
|| !resource_counts || (len > PLANNER_NUM_TYPES);
return rc;
}
static int span_input_check (planner_t *ctx, int64_t start_time,
uint64_t duration, const int64_t *resource_counts,
size_t len)
{
int i = 0;
int rc = -1;
if (!ctx || not_feasable (ctx, start_time, duration, resource_counts, len)) {
errno = EINVAL;
goto done;
} else {
int64_t sum = 0;
for (i = 0; i < len; ++i) {
if (resource_counts[i] > ctx->total_resources[i]) {
errno = ERANGE;
goto done;
}
sum += resource_counts[i];
}
if (sum <= 0) {
errno = ERANGE;
goto done;
}
}
rc = 0;
done:
return rc;
}
static span_t *span_new (planner_t *ctx, int64_t start_time, uint64_t duration,
const uint64_t *resource_counts, size_t len)
{
int i = 0;
char key[32];
span_t *span = NULL;
if (span_input_check (ctx, start_time, duration,
(const int64_t *)resource_counts, len) == -1)
goto done;
span = xzmalloc (sizeof (*span));
span->start = start_time;
span->last = start_time + duration;
ctx->span_counter++;
span->span_id = ctx->span_counter;
memset (span->planned, '\0', sizeof (span->planned));
span->dimension = len;
for (i = 0; i < len; ++i)
span->planned[i] = (int64_t)resource_counts[i];
span->in_system = 0;
span->start_p = NULL;
span->last_p = NULL;
sprintf (key, "%jd", (intmax_t)span->span_id);
zhashx_insert (ctx->span_lookup, key, span);
zhashx_freefn (ctx->span_lookup, key, free);
done:
return span;
}
/*******************************************************************************
* *
* PUBLIC PLANNER API *
* *
*******************************************************************************/
planner_t *planner_new (int64_t base_time, uint64_t duration,
const uint64_t *resource_totals,
const char **resource_types, size_t len)
{
int i = 0;
planner_t *ctx = NULL;
if (duration < 1 || !resource_totals
|| !resource_types || len > PLANNER_NUM_TYPES) {
errno = EINVAL;
goto done;
} else {
for (i = 0; i < len; ++i) {
if (resource_totals[i] > INT64_MAX) {
errno = ERANGE;
goto done;
}
}
}
ctx = xzmalloc (sizeof (*ctx));
memset (ctx->total_resources, '\0', sizeof (ctx->total_resources));
memset (ctx->resource_types, '\0', sizeof (ctx->resource_types));
for (i = 0; i < len; ++i) {
ctx->total_resources[i] = (int64_t)resource_totals[i];
ctx->resource_types[i] = xstrdup (resource_types[i]);
}
ctx->dimension = len;
initialize (ctx, base_time, duration);
done:
return ctx;
}
int planner_reset (planner_t *ctx, int64_t base_time, uint64_t duration)
{
if (duration < 1) {
errno = EINVAL;
return -1;
}
erase (ctx);
initialize (ctx, base_time, duration);
return 0;
}
void planner_destroy (planner_t **ctx_p)
{
if (ctx_p && *ctx_p) {
restore_track_points (*ctx_p);
erase (*ctx_p);
zhashx_destroy (&((*ctx_p)->span_lookup));
free (*ctx_p);
*ctx_p = NULL;
}
}
int64_t planner_base_time (planner_t *ctx)
{
if (!ctx) {
errno = EINVAL;
return -1;
}
return ctx->plan_start;
}
int64_t planner_duration (planner_t *ctx)
{
if (!ctx) {
errno = EINVAL;
return -1;
}
return ctx->plan_end - ctx->plan_start;
}
size_t planner_resources_len (planner_t *ctx)
{
if (!ctx) {
errno = EINVAL;
return 0;
}
return ctx->dimension;
}
int64_t planner_resource_total_at (planner_t *ctx, unsigned int i)
{
if (!ctx || i >= ctx->dimension) {
errno = EINVAL;
return -1;
}
return ctx->total_resources[i];
}
int64_t planner_resource_total_by_type (planner_t *ctx, const char *resource_type)
{
int i = 0;
if (!ctx || !resource_type) {
errno = EINVAL;
return -1;
}
for (i = 0; i < ctx->dimension; ++i) {
if (strcmp (ctx->resource_types[i], resource_type) == 0)
break;
}
return (i < ctx->dimension)? ctx->total_resources[i] : -1;
}
const char **planner_resource_types (planner_t *ctx)
{
if (!ctx) {
errno = EINVAL;
return NULL;
}
return (const char **)ctx->resource_types;
}
int planner_resource_index_of_type (planner_t *ctx, const char *resource_type)
{
int i = 0;
if (!ctx || !resource_type) {
errno = EINVAL;
return -1;
}
for (i = 0; i < ctx->dimension; ++i) {
if (strcmp (ctx->resource_types[i], resource_type) == 0)
break;
}
return (i < ctx->dimension)? i : -1;
}
const char *planner_resource_type_at (planner_t *ctx, unsigned int i)
{
if (!ctx || i >= ctx->dimension) {
errno = EINVAL;
return NULL;
}
return ctx->resource_types[i];
}
int64_t planner_avail_time_first (planner_t *ctx, int64_t on_or_after,
uint64_t duration,
const uint64_t *resource_counts, size_t len)
{
if (!ctx || on_or_after < ctx->plan_start
|| on_or_after >= ctx->plan_end || duration < 1
|| !resource_counts || len > ctx->dimension) {
errno = EINVAL;
return -1;
}
if (rescmp ((const int64_t *)resource_counts,
ctx->total_resources, len) > 0) {
errno = ERANGE;
return -1;
}
restore_track_points (ctx);
ctx->avail_time_iter_set = 1;
copy_req (ctx->current_request, on_or_after, duration, resource_counts, len);
return avail_at (ctx, on_or_after, duration,
(const int64_t *)resource_counts, len);
}
int64_t planner_avail_time_next (planner_t *ctx)
{
size_t len = 0;
int64_t on_or_after = -1;
uint64_t duration = 0;
const int64_t *resource_counts = NULL;
if (!ctx || !ctx->avail_time_iter_set) {
errno = EINVAL;
return -1;
}
len = ctx->current_request->dimension;
resource_counts = ctx->current_request->resources;
on_or_after = ctx->current_request->on_or_after;
duration = ctx->current_request->duration;
if (rescmp (resource_counts, ctx->total_resources, len) > 0) {
errno = ERANGE;
return -1;
}
return avail_at (ctx, on_or_after, duration,
(const int64_t *)resource_counts, len);
}
int planner_avail_during (planner_t *ctx, int64_t start_time, uint64_t duration,
const uint64_t *resource_counts, size_t len)
{
bool ok = false;
int64_t result = -1;
if (!ctx || duration < 1 || !resource_counts|| len > ctx->dimension) {
errno = EINVAL;
return -1;
}
result = rescmp ((const int64_t *)resource_counts, ctx->total_resources, len);
if (result > 0) {
errno = ERANGE;
return -1;
}
ok = avail_during (ctx, start_time, duration,
(const int64_t *)resource_counts, len);
return ok? 0 : -1;
}
int64_t planner_avail_resources_during (planner_t *ctx, int64_t at,
uint64_t duration, unsigned int i)
{
scheduled_point_t *min_point = NULL;
if (!ctx || at > ctx->plan_end
|| duration < 1 || i >= PLANNER_NUM_TYPES) {
errno = EINVAL;
return -1;
}
min_point = avail_resources_during (ctx, at, duration);
return min_point->remaining[i];
}
int64_t planner_avail_resources_during_by_type (planner_t *ctx, int64_t at,
uint64_t duration,
const char *resource_type)
{
unsigned int i = 0;
scheduled_point_t *min_point = NULL;
if (!ctx || at > ctx->plan_end || duration < 1) {
errno = EINVAL;
return -1;
}
if ((i = planner_resource_index_of_type (ctx, resource_type)) == -1) {
errno = EINVAL;
return -1;
}
min_point = avail_resources_during (ctx, at, duration);
return min_point->remaining[i];
}
int planner_avail_resources_array_during (planner_t *ctx, int64_t at,
uint64_t duration, int64_t *resources,
size_t len)
{
scheduled_point_t *min_point = NULL;
if (!ctx || at > ctx->plan_end || duration < 1
|| !resources || len >= PLANNER_NUM_TYPES) {
errno = EINVAL;
return -1;
}
min_point = avail_resources_during (ctx, at, duration);
memcpy (resources, min_point->remaining, len * sizeof (*resources));
return 0;
}
int64_t planner_avail_resources_at (planner_t *ctx, int64_t at, unsigned int i)
{
struct rb_root *spt = NULL;
scheduled_point_t *state = NULL;
if (!ctx || at > ctx->plan_end || i >= PLANNER_NUM_TYPES) {
errno = EINVAL;
return -1;