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
|
/*
Copyright (c) 2006, 2007, 2008 Z RESEARCH, Inc. <http://www.zresearch.com>
This file is part of GlusterFS.
GlusterFS 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 3 of the License,
or (at your option) any later version.
GlusterFS 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 this program. If not, see
<http://www.gnu.org/licenses/>.
*/
/* ALU code needs a complete re-write. This is one of the most important
* part of GlusterFS and so needs more and more reviews and testing
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include <sys/time.h>
#include <stdint.h>
#include "stack.h"
#include "alu.h"
#define ALU_DISK_USAGE_ENTRY_THRESHOLD_DEFAULT (1 * GF_UNIT_GB)
#define ALU_DISK_USAGE_EXIT_THRESHOLD_DEFAULT (512 * GF_UNIT_MB)
#define ALU_WRITE_USAGE_ENTRY_THRESHOLD_DEFAULT 25
#define ALU_WRITE_USAGE_EXIT_THRESHOLD_DEFAULT 5
#define ALU_READ_USAGE_ENTRY_THRESHOLD_DEFAULT 25
#define ALU_READ_USAGE_EXIT_THRESHOLD_DEFAULT 5
#define ALU_OPEN_FILES_USAGE_ENTRY_THRESHOLD_DEFAULT 1000
#define ALU_OPEN_FILES_USAGE_EXIT_THRESHOLD_DEFAULT 100
#define ALU_LIMITS_TOTAL_DISK_SIZE_DEFAULT 100
#define ALU_REFRESH_INTERVAL_DEFAULT 5
#define ALU_REFRESH_CREATE_COUNT_DEFAULT 5
static int64_t
get_stats_disk_usage (struct xlator_stats *this)
{
return this->disk_usage;
}
static int64_t
get_stats_write_usage (struct xlator_stats *this)
{
return this->write_usage;
}
static int64_t
get_stats_read_usage (struct xlator_stats *this)
{
return this->read_usage;
}
static int64_t
get_stats_disk_speed (struct xlator_stats *this)
{
return this->disk_speed;
}
static int64_t
get_stats_file_usage (struct xlator_stats *this)
{
/* Avoid warning "defined but not used" */
(void) &get_stats_file_usage;
return this->nr_files;
}
static int64_t
get_stats_free_disk (struct xlator_stats *this)
{
if (this->total_disk_size > 0)
return (this->free_disk * 100) / this->total_disk_size;
return 0;
}
static int64_t
get_max_diff_write_usage (struct xlator_stats *max, struct xlator_stats *min)
{
return (max->write_usage - min->write_usage);
}
static int64_t
get_max_diff_read_usage (struct xlator_stats *max, struct xlator_stats *min)
{
return (max->read_usage - min->read_usage);
}
static int64_t
get_max_diff_disk_usage (struct xlator_stats *max, struct xlator_stats *min)
{
return (max->disk_usage - min->disk_usage);
}
static int64_t
get_max_diff_disk_speed (struct xlator_stats *max, struct xlator_stats *min)
{
return (max->disk_speed - min->disk_speed);
}
static int64_t
get_max_diff_file_usage (struct xlator_stats *max, struct xlator_stats *min)
{
return (max->nr_files - min->nr_files);
}
int
alu_parse_options (xlator_t *xl, struct alu_sched *alu_sched)
{
data_t *order = dict_get (xl->options, "scheduler.alu.order");
if (!order) {
gf_log (xl->name, GF_LOG_ERROR,
"option 'scheduler.alu.order' not specified");
return -1;
}
struct alu_threshold *_threshold_fn;
struct alu_threshold *tmp_threshold;
data_t *entry_fn = NULL;
data_t *exit_fn = NULL;
char *tmp_str = NULL;
char *order_str = strtok_r (order->data, ":", &tmp_str);
/* Get the scheduling priority order, specified by the user. */
while (order_str) {
gf_log ("alu", GF_LOG_DEBUG,
"alu_init: order string: %s",
order_str);
if (strcmp (order_str, "disk-usage") == 0) {
/* Disk usage */
_threshold_fn =
CALLOC (1, sizeof (struct alu_threshold));
ERR_ABORT (_threshold_fn);
_threshold_fn->diff_value = get_max_diff_disk_usage;
_threshold_fn->sched_value = get_stats_disk_usage;
entry_fn =
dict_get (xl->options,
"scheduler.alu.disk-usage.entry-threshold");
if (entry_fn) {
if (gf_string2bytesize (entry_fn->data,
&alu_sched->entry_limit.disk_usage) != 0) {
gf_log (xl->name, GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"disk-usage.entry-threshold\"",
entry_fn->data);
return -1;
}
} else {
alu_sched->entry_limit.disk_usage = ALU_DISK_USAGE_ENTRY_THRESHOLD_DEFAULT;
}
_threshold_fn->entry_value = get_stats_disk_usage;
exit_fn = dict_get (xl->options,
"scheduler.alu.disk-usage.exit-threshold");
if (exit_fn) {
if (gf_string2bytesize (exit_fn->data, &alu_sched->exit_limit.disk_usage) != 0) {
gf_log (xl->name, GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"disk-usage.exit-threshold\"",
exit_fn->data);
return -1;
}
} else {
alu_sched->exit_limit.disk_usage = ALU_DISK_USAGE_EXIT_THRESHOLD_DEFAULT;
}
_threshold_fn->exit_value = get_stats_disk_usage;
tmp_threshold = alu_sched->threshold_fn;
if (!tmp_threshold) {
alu_sched->threshold_fn = _threshold_fn;
} else {
while (tmp_threshold->next) {
tmp_threshold = tmp_threshold->next;
}
tmp_threshold->next = _threshold_fn;
}
gf_log ("alu",
GF_LOG_DEBUG, "alu_init: = %"PRId64",%"PRId64"",
alu_sched->entry_limit.disk_usage,
alu_sched->exit_limit.disk_usage);
} else if (strcmp (order_str, "write-usage") == 0) {
/* Handle "write-usage" */
_threshold_fn = CALLOC (1, sizeof (struct alu_threshold));
ERR_ABORT (_threshold_fn);
_threshold_fn->diff_value = get_max_diff_write_usage;
_threshold_fn->sched_value = get_stats_write_usage;
entry_fn = dict_get (xl->options,
"scheduler.alu.write-usage.entry-threshold");
if (entry_fn) {
if (gf_string2bytesize (entry_fn->data,
&alu_sched->entry_limit.write_usage) != 0) {
gf_log (xl->name, GF_LOG_ERROR,
"invalid number format \"%s\" "
"of option scheduler.alu."
"write-usage.entry-threshold",
entry_fn->data);
return -1;
}
} else {
alu_sched->entry_limit.write_usage = ALU_WRITE_USAGE_ENTRY_THRESHOLD_DEFAULT;
}
_threshold_fn->entry_value = get_stats_write_usage;
exit_fn = dict_get (xl->options,
"scheduler.alu.write-usage.exit-threshold");
if (exit_fn) {
if (gf_string2bytesize (exit_fn->data,
&alu_sched->exit_limit.write_usage) != 0) {
gf_log (xl->name, GF_LOG_ERROR,
"invalid number format \"%s\""
" of \"option scheduler.alu."
"write-usage.exit-threshold\"",
exit_fn->data);
return -1;
}
} else {
alu_sched->exit_limit.write_usage = ALU_WRITE_USAGE_EXIT_THRESHOLD_DEFAULT;
}
_threshold_fn->exit_value = get_stats_write_usage;
tmp_threshold = alu_sched->threshold_fn;
if (!tmp_threshold) {
alu_sched->threshold_fn = _threshold_fn;
} else {
while (tmp_threshold->next) {
tmp_threshold = tmp_threshold->next;
}
tmp_threshold->next = _threshold_fn;
}
gf_log (xl->name, GF_LOG_DEBUG,
"alu_init: = %"PRId64",%"PRId64"",
alu_sched->entry_limit.write_usage,
alu_sched->exit_limit.write_usage);
} else if (strcmp (order_str, "read-usage") == 0) {
/* Read usage */
_threshold_fn = CALLOC (1, sizeof (struct alu_threshold));
ERR_ABORT (_threshold_fn);
_threshold_fn->diff_value = get_max_diff_read_usage;
_threshold_fn->sched_value = get_stats_read_usage;
entry_fn = dict_get (xl->options,
"scheduler.alu.read-usage.entry-threshold");
if (entry_fn) {
if (gf_string2bytesize (entry_fn->data,
&alu_sched->entry_limit.read_usage) != 0) {
gf_log (xl->name,
GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"read-usage.entry-threshold\"",
entry_fn->data);
return -1;
}
} else {
alu_sched->entry_limit.read_usage = ALU_READ_USAGE_ENTRY_THRESHOLD_DEFAULT;
}
_threshold_fn->entry_value = get_stats_read_usage;
exit_fn = dict_get (xl->options,
"scheduler.alu.read-usage.exit-threshold");
if (exit_fn)
{
if (gf_string2bytesize (exit_fn->data,
&alu_sched->exit_limit.read_usage) != 0)
{
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"read-usage.exit-threshold\"",
exit_fn->data);
return -1;
}
}
else
{
alu_sched->exit_limit.read_usage = ALU_READ_USAGE_EXIT_THRESHOLD_DEFAULT;
}
_threshold_fn->exit_value = get_stats_read_usage;
tmp_threshold = alu_sched->threshold_fn;
if (!tmp_threshold) {
alu_sched->threshold_fn = _threshold_fn;
}
else {
while (tmp_threshold->next) {
tmp_threshold = tmp_threshold->next;
}
tmp_threshold->next = _threshold_fn;
}
gf_log ("alu", GF_LOG_DEBUG,
"alu_init: = %"PRId64",%"PRId64"",
alu_sched->entry_limit.read_usage,
alu_sched->exit_limit.read_usage);
} else if (strcmp (order_str, "open-files-usage") == 0) {
/* Open files counter */
_threshold_fn = CALLOC (1, sizeof (struct alu_threshold));
ERR_ABORT (_threshold_fn);
_threshold_fn->diff_value = get_max_diff_file_usage;
_threshold_fn->sched_value = get_stats_file_usage;
entry_fn = dict_get (xl->options,
"scheduler.alu.open-files-usage.entry-threshold");
if (entry_fn) {
if (gf_string2uint64 (entry_fn->data,
&alu_sched->entry_limit.nr_files) != 0)
{
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"open-files-usage.entry-"
"threshold\"", entry_fn->data);
return -1;
}
}
else
{
alu_sched->entry_limit.nr_files = ALU_OPEN_FILES_USAGE_ENTRY_THRESHOLD_DEFAULT;
}
_threshold_fn->entry_value = get_stats_file_usage;
exit_fn = dict_get (xl->options,
"scheduler.alu.open-files-usage.exit-threshold");
if (exit_fn)
{
if (gf_string2uint64 (exit_fn->data,
&alu_sched->exit_limit.nr_files) != 0)
{
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.alu."
"open-files-usage.exit-"
"threshold\"", exit_fn->data);
return -1;
}
}
else
{
alu_sched->exit_limit.nr_files = ALU_OPEN_FILES_USAGE_EXIT_THRESHOLD_DEFAULT;
}
_threshold_fn->exit_value = get_stats_file_usage;
tmp_threshold = alu_sched->threshold_fn;
if (!tmp_threshold) {
alu_sched->threshold_fn = _threshold_fn;
}
else {
while (tmp_threshold->next) {
tmp_threshold = tmp_threshold->next;
}
tmp_threshold->next = _threshold_fn;
}
gf_log ("alu", GF_LOG_DEBUG,
"alu.c->alu_init: = %"PRIu64",%"PRIu64"",
alu_sched->entry_limit.nr_files,
alu_sched->exit_limit.nr_files);
} else if (strcmp (order_str, "disk-speed-usage") == 0) {
/* Disk speed */
_threshold_fn = CALLOC (1, sizeof (struct alu_threshold));
ERR_ABORT (_threshold_fn);
_threshold_fn->diff_value = get_max_diff_disk_speed;
_threshold_fn->sched_value = get_stats_disk_speed;
entry_fn = dict_get (xl->options,
"scheduler.alu.disk-speed-usage.entry-threshold");
if (entry_fn) {
gf_log ("alu", GF_LOG_DEBUG,
"entry-threshold is given, "
"value is constant");
}
_threshold_fn->entry_value = NULL;
exit_fn = dict_get (xl->options,
"scheduler.alu.disk-speed-usage.exit-threshold");
if (exit_fn) {
gf_log ("alu", GF_LOG_DEBUG,
"exit-threshold is given, "
"value is constant");
}
_threshold_fn->exit_value = NULL;
tmp_threshold = alu_sched->threshold_fn;
if (!tmp_threshold) {
alu_sched->threshold_fn = _threshold_fn;
}
else {
while (tmp_threshold->next) {
tmp_threshold = tmp_threshold->next;
}
tmp_threshold->next = _threshold_fn;
}
} else {
gf_log ("alu", GF_LOG_DEBUG,
"%s, unknown option provided to scheduler",
order_str);
}
order_str = strtok_r (NULL, ":", &tmp_str);
}
return 0;
}
static int32_t
alu_init (xlator_t *xl)
{
struct alu_sched *alu_sched = NULL;
struct alu_limits *_limit_fn = NULL;
struct alu_limits *tmp_limits = NULL;
uint32_t min_free_disk = 0;
data_t *limits = NULL;
alu_sched = CALLOC (1, sizeof (struct alu_sched));
ERR_ABORT (alu_sched);
{
alu_parse_options (xl, alu_sched);
}
/* Get the limits */
limits = dict_get (xl->options,
"scheduler.limits.min-free-disk");
if (limits) {
_limit_fn = CALLOC (1, sizeof (struct alu_limits));
ERR_ABORT (_limit_fn);
_limit_fn->min_value = get_stats_free_disk;
_limit_fn->cur_value = get_stats_free_disk;
tmp_limits = alu_sched->limits_fn ;
_limit_fn->next = tmp_limits;
alu_sched->limits_fn = _limit_fn;
if (gf_string2percent (limits->data,
&min_free_disk) != 0) {
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" "
"of \"option scheduler.limits."
"min-free-disk\"", limits->data);
return -1;
}
alu_sched->spec_limit.free_disk = min_free_disk;
if (alu_sched->spec_limit.free_disk >= 100) {
gf_log ("alu", GF_LOG_ERROR,
"check the \"option scheduler."
"limits.min-free-disk\", it should "
"be percentage value");
return -1;
}
alu_sched->spec_limit.total_disk_size = ALU_LIMITS_TOTAL_DISK_SIZE_DEFAULT; /* Its in % */
gf_log ("alu", GF_LOG_DEBUG,
"alu.limit.min-disk-free = %"PRId64"",
_limit_fn->cur_value (&(alu_sched->spec_limit)));
}
limits = dict_get (xl->options,
"scheduler.limits.max-open-files");
if (limits) {
// Update alu_sched->priority properly
_limit_fn = CALLOC (1, sizeof (struct alu_limits));
ERR_ABORT (_limit_fn);
_limit_fn->max_value = get_stats_file_usage;
_limit_fn->cur_value = get_stats_file_usage;
tmp_limits = alu_sched->limits_fn ;
_limit_fn->next = tmp_limits;
alu_sched->limits_fn = _limit_fn;
if (gf_string2uint64_base10 (limits->data,
&alu_sched->spec_limit.nr_files) != 0)
{
gf_log ("alu", GF_LOG_ERROR,
"invalid number format '%s' of option "
"scheduler.limits.max-open-files",
limits->data);
return -1;
}
gf_log ("alu", GF_LOG_DEBUG,
"alu_init: limit.max-open-files = %"PRId64"",
_limit_fn->cur_value (&(alu_sched->spec_limit)));
}
/* Stats refresh options */
limits = dict_get (xl->options,
"scheduler.refresh-interval");
if (limits) {
if (gf_string2time (limits->data,
&alu_sched->refresh_interval) != 0) {
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" of "
"option scheduler.refresh-interval",
limits->data);
return -1;
}
} else {
alu_sched->refresh_interval = ALU_REFRESH_INTERVAL_DEFAULT;
}
gettimeofday (&(alu_sched->last_stat_fetch), NULL);
limits = dict_get (xl->options,
"scheduler.alu.stat-refresh.num-file-create");
if (limits) {
if (gf_string2uint32 (limits->data,
&alu_sched->refresh_create_count) != 0)
{
gf_log ("alu", GF_LOG_ERROR,
"invalid number format \"%s\" of \"option "
"alu.stat-refresh.num-file-create\"",
limits->data);
return -1;
}
} else {
alu_sched->refresh_create_count = ALU_REFRESH_CREATE_COUNT_DEFAULT;
}
{
/* Build an array of child_nodes */
struct alu_sched_struct *sched_array = NULL;
xlator_list_t *trav_xl = xl->children;
data_t *data = NULL;
int32_t index = 0;
while (trav_xl) {
index++;
trav_xl = trav_xl->next;
}
alu_sched->child_count = index;
sched_array = CALLOC (index, sizeof (struct alu_sched_struct));
ERR_ABORT (sched_array);
trav_xl = xl->children;
index = 0;
while (trav_xl) {
sched_array[index].xl = trav_xl->xlator;
sched_array[index].eligible = 1;
index++;
trav_xl = trav_xl->next;
}
alu_sched->array = sched_array;
data = dict_get (xl->options,
"scheduler.read-only-subvolumes");
if (data) {
char *child = NULL;
char *tmp = NULL;
char *childs_data = strdup (data->data);
child = strtok_r (childs_data, ",", &tmp);
while (child) {
for (index = 1; index < alu_sched->child_count; index++) {
if (strcmp (alu_sched->array[index -1].xl->name, child) == 0) {
memcpy (&(alu_sched->array[index -1]),
&(alu_sched->array[alu_sched->child_count -1]),
sizeof (struct alu_sched_struct));
alu_sched->child_count--;
break;
}
}
child = strtok_r (NULL, ",", &tmp);
}
}
}
*((long *)xl->private) = (long)alu_sched;
/* Initialize all the alu_sched structure's elements */
{
alu_sched->sched_nodes_pending = 0;
alu_sched->min_limit.free_disk = 0x00FFFFFF;
alu_sched->min_limit.disk_usage = 0xFFFFFFFF;
alu_sched->min_limit.total_disk_size = 0xFFFFFFFF;
alu_sched->min_limit.disk_speed = 0xFFFFFFFF;
alu_sched->min_limit.write_usage = 0xFFFFFFFF;
alu_sched->min_limit.read_usage = 0xFFFFFFFF;
alu_sched->min_limit.nr_files = 0xFFFFFFFF;
alu_sched->min_limit.nr_clients = 0xFFFFFFFF;
}
pthread_mutex_init (&alu_sched->alu_mutex, NULL);
return 0;
}
static void
alu_fini (xlator_t *xl)
{
if (!xl)
return;
struct alu_sched *alu_sched = (struct alu_sched *)*((long *)xl->private);
struct alu_limits *limit = alu_sched->limits_fn;
struct alu_threshold *threshold = alu_sched->threshold_fn;
void *tmp = NULL;
pthread_mutex_destroy (&alu_sched->alu_mutex);
free (alu_sched->array);
while (limit) {
tmp = limit;
limit = limit->next;
free (tmp);
}
while (threshold) {
tmp = threshold;
threshold = threshold->next;
free (tmp);
}
free (alu_sched);
}
static int32_t
update_stat_array_cbk (call_frame_t *frame,
void *cookie,
xlator_t *xl,
int32_t op_ret,
int32_t op_errno,
struct xlator_stats *trav_stats)
{
struct alu_sched *alu_sched = (struct alu_sched *)*((long *)xl->private);
struct alu_limits *limits_fn = alu_sched->limits_fn;
int32_t idx = 0;
pthread_mutex_lock (&alu_sched->alu_mutex);
for (idx = 0; idx < alu_sched->child_count; idx++) {
if (alu_sched->array[idx].xl == (xlator_t *)cookie)
break;
}
pthread_mutex_unlock (&alu_sched->alu_mutex);
if (op_ret == -1) {
alu_sched->array[idx].eligible = 0;
} else {
memcpy (&(alu_sched->array[idx].stats), trav_stats, sizeof (struct xlator_stats));
/* Get stats from all the child node */
/* Here check the limits specified by the user to
consider the nodes to be used by scheduler */
alu_sched->array[idx].eligible = 1;
limits_fn = alu_sched->limits_fn;
while (limits_fn){
if (limits_fn->max_value &&
(limits_fn->cur_value (trav_stats) >
limits_fn->max_value (&(alu_sched->spec_limit)))) {
alu_sched->array[idx].eligible = 0;
}
if (limits_fn->min_value &&
(limits_fn->cur_value (trav_stats) <
limits_fn->min_value (&(alu_sched->spec_limit)))) {
alu_sched->array[idx].eligible = 0;
}
limits_fn = limits_fn->next;
}
/* Select minimum and maximum disk_usage */
if (trav_stats->disk_usage > alu_sched->max_limit.disk_usage) {
alu_sched->max_limit.disk_usage = trav_stats->disk_usage;
}
if (trav_stats->disk_usage < alu_sched->min_limit.disk_usage) {
alu_sched->min_limit.disk_usage = trav_stats->disk_usage;
}
/* Select minimum and maximum disk_speed */
if (trav_stats->disk_speed > alu_sched->max_limit.disk_speed) {
alu_sched->max_limit.disk_speed = trav_stats->disk_speed;
}
if (trav_stats->disk_speed < alu_sched->min_limit.disk_speed) {
alu_sched->min_limit.disk_speed = trav_stats->disk_speed;
}
/* Select minimum and maximum number of open files */
if (trav_stats->nr_files > alu_sched->max_limit.nr_files) {
alu_sched->max_limit.nr_files = trav_stats->nr_files;
}
if (trav_stats->nr_files < alu_sched->min_limit.nr_files) {
alu_sched->min_limit.nr_files = trav_stats->nr_files;
}
/* Select minimum and maximum write-usage */
if (trav_stats->write_usage > alu_sched->max_limit.write_usage) {
alu_sched->max_limit.write_usage = trav_stats->write_usage;
}
if (trav_stats->write_usage < alu_sched->min_limit.write_usage) {
alu_sched->min_limit.write_usage = trav_stats->write_usage;
}
/* Select minimum and maximum read-usage */
if (trav_stats->read_usage > alu_sched->max_limit.read_usage) {
alu_sched->max_limit.read_usage = trav_stats->read_usage;
}
if (trav_stats->read_usage < alu_sched->min_limit.read_usage) {
alu_sched->min_limit.read_usage = trav_stats->read_usage;
}
/* Select minimum and maximum free-disk */
if (trav_stats->free_disk > alu_sched->max_limit.free_disk) {
alu_sched->max_limit.free_disk = trav_stats->free_disk;
}
if (trav_stats->free_disk < alu_sched->min_limit.free_disk) {
alu_sched->min_limit.free_disk = trav_stats->free_disk;
}
}
STACK_DESTROY (frame->root);
return 0;
}
static void
update_stat_array (xlator_t *xl)
{
/* This function schedules the file in one of the child nodes */
struct alu_sched *alu_sched = (struct alu_sched *)*((long *)xl->private);
int32_t idx = 0;
call_frame_t *frame = NULL;
call_pool_t *pool = xl->ctx->pool;
for (idx = 0 ; idx < alu_sched->child_count; idx++) {
frame = create_frame (xl, pool);
STACK_WIND_COOKIE (frame,
update_stat_array_cbk,
alu_sched->array[idx].xl, //cookie
alu_sched->array[idx].xl,
(alu_sched->array[idx].xl)->mops->stats,
0); //flag
}
return;
}
static void
alu_update (xlator_t *xl)
{
struct timeval tv;
struct alu_sched *alu_sched = (struct alu_sched *)*((long *)xl->private);
gettimeofday (&tv, NULL);
if (tv.tv_sec > (alu_sched->refresh_interval + alu_sched->last_stat_fetch.tv_sec)) {
/* Update the stats from all the server */
update_stat_array (xl);
alu_sched->last_stat_fetch.tv_sec = tv.tv_sec;
}
}
static xlator_t *
alu_scheduler (xlator_t *xl, const void *path)
{
/* This function schedules the file in one of the child nodes */
struct alu_sched *alu_sched = (struct alu_sched *)*((long *)xl->private);
int32_t sched_index = 0;
int32_t sched_index_orig = 0;
int32_t idx = 0;
alu_update (xl);
/* Now check each threshold one by one if some nodes are classified */
{
struct alu_threshold *trav_threshold = alu_sched->threshold_fn;
struct alu_threshold *tmp_threshold = alu_sched->sched_method;
struct alu_sched_node *tmp_sched_node;
/* This pointer 'trav_threshold' contains function pointers according to spec file
give by user, */
while (trav_threshold) {
/* This check is needed for seeing if already there are nodes in this criteria
to be scheduled */
if (!alu_sched->sched_nodes_pending) {
for (idx = 0; idx < alu_sched->child_count; idx++) {
if (!alu_sched->array[idx].eligible) {
continue;
}
if (trav_threshold->entry_value) {
if (trav_threshold->diff_value (&(alu_sched->max_limit),
&(alu_sched->array[idx].stats)) <
trav_threshold->entry_value (&(alu_sched->entry_limit))) {
continue;
}
}
tmp_sched_node = CALLOC (1, sizeof (struct alu_sched_node));
ERR_ABORT (tmp_sched_node);
tmp_sched_node->index = idx;
if (!alu_sched->sched_node) {
alu_sched->sched_node = tmp_sched_node;
} else {
pthread_mutex_lock (&alu_sched->alu_mutex);
tmp_sched_node->next = alu_sched->sched_node;
alu_sched->sched_node = tmp_sched_node;
pthread_mutex_unlock (&alu_sched->alu_mutex);
}
alu_sched->sched_nodes_pending++;
}
} /* end of if (sched_nodes_pending) */
/* This loop is required to check the eligible nodes */
struct alu_sched_node *trav_sched_node;
while (alu_sched->sched_nodes_pending) {
trav_sched_node = alu_sched->sched_node;
sched_index = trav_sched_node->index;
if (alu_sched->array[sched_index].eligible)
break;
alu_sched->sched_node = trav_sched_node->next;
free (trav_sched_node);
alu_sched->sched_nodes_pending--;
}
if (alu_sched->sched_nodes_pending) {
/* There are some node in this criteria to be scheduled, no need
* to sort and check other methods
*/
if (tmp_threshold && tmp_threshold->exit_value) {
/* verify the exit value && whether node is eligible or not */
if (tmp_threshold->diff_value (&(alu_sched->max_limit),
&(alu_sched->array[sched_index].stats)) >
tmp_threshold->exit_value (&(alu_sched->exit_limit))) {
/* Free the allocated info for the node :) */
pthread_mutex_lock (&alu_sched->alu_mutex);
alu_sched->sched_node = trav_sched_node->next;
free (trav_sched_node);
trav_sched_node = alu_sched->sched_node;
alu_sched->sched_nodes_pending--;
pthread_mutex_unlock (&alu_sched->alu_mutex);
}
} else {
/* if there is no exit value, then exit after scheduling once */
pthread_mutex_lock (&alu_sched->alu_mutex);
alu_sched->sched_node = trav_sched_node->next;
free (trav_sched_node);
trav_sched_node = alu_sched->sched_node;
alu_sched->sched_nodes_pending--;
pthread_mutex_unlock (&alu_sched->alu_mutex);
}
alu_sched->sched_method = tmp_threshold; /* this is the method used for selecting */
/* */
if (trav_sched_node) {
tmp_sched_node = trav_sched_node;
while (trav_sched_node->next) {
trav_sched_node = trav_sched_node->next;
}
if (tmp_sched_node->next) {
pthread_mutex_lock (&alu_sched->alu_mutex);
alu_sched->sched_node = tmp_sched_node->next;
tmp_sched_node->next = NULL;
trav_sched_node->next = tmp_sched_node;
pthread_mutex_unlock (&alu_sched->alu_mutex);
}
}
/* return the scheduled node */
return alu_sched->array[sched_index].xl;
} /* end of if (pending_nodes) */
tmp_threshold = trav_threshold;
trav_threshold = trav_threshold->next;
}
}
/* This is used only when there is everything seems ok, or no eligible nodes */
sched_index_orig = alu_sched->sched_index;
alu_sched->sched_method = NULL;
while (1) {
//lock
pthread_mutex_lock (&alu_sched->alu_mutex);
sched_index = alu_sched->sched_index++;
alu_sched->sched_index = alu_sched->sched_index % alu_sched->child_count;
pthread_mutex_unlock (&alu_sched->alu_mutex);
//unlock
if (alu_sched->array[sched_index].eligible)
break;
if (sched_index_orig == (sched_index + 1) % alu_sched->child_count) {
gf_log ("alu", GF_LOG_WARNING, "No node is eligible to schedule");
//lock
pthread_mutex_lock (&alu_sched->alu_mutex);
alu_sched->sched_index++;
alu_sched->sched_index = alu_sched->sched_index % alu_sched->child_count;
pthread_mutex_unlock (&alu_sched->alu_mutex);
//unlock
break;
}
}
return alu_sched->array[sched_index].xl;
}
/**
* notify
*/
void
alu_notify (xlator_t *xl, int32_t event, void *data)
{
struct alu_sched *alu_sched = NULL;
int32_t idx = 0;
alu_sched = (struct alu_sched *)*((long *)xl->private);
if (!alu_sched)
return;
for (idx = 0; idx < alu_sched->child_count; idx++) {
if (alu_sched->array[idx].xl == (xlator_t *)data)
break;
}
switch (event)
{
case GF_EVENT_CHILD_UP:
{
//alu_sched->array[idx].eligible = 1;
}
break;
case GF_EVENT_CHILD_DOWN:
{
alu_sched->array[idx].eligible = 0;
}
break;
default:
{
;
}
break;
}
}
struct sched_ops sched = {
.init = alu_init,
.fini = alu_fini,
.update = alu_update,
.schedule = alu_scheduler,
.notify = alu_notify
};
struct volume_options options[] = {
{ .key = { "scheduler.alu.order", "alu.order" },
.type = GF_OPTION_TYPE_ANY
},
{ .key = { "scheduler.alu.disk-usage.entry-threshold",
"alu.disk-usage.entry-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.disk-usage.exit-threshold",
"alu.disk-usage.exit-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.write-usage.entry-threshold",
"alu.write-usage.entry-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.write-usage.exit-threshold",
"alu.write-usage.exit-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.read-usage.entry-threshold",
"alu.read-usage.entry-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.read-usage.exit-threshold",
"alu.read-usage.exit-threshold" },
.type = GF_OPTION_TYPE_SIZET
},
{ .key = { "scheduler.alu.open-files-usage.entry-threshold",
"alu.open-files-usage.entry-threshold" },
.type = GF_OPTION_TYPE_INT
},
{ .key = { "scheduler.alu.open-files-usage.exit-threshold",
"alu.open-files-usage.exit-threshold" },
.type = GF_OPTION_TYPE_INT
},
{ .key = { "scheduler.read-only-subvolumes",
"alu.read-only-subvolumes" },
.type = GF_OPTION_TYPE_ANY
},
{ .key = { "scheduler.refresh-interval",
"alu.refresh-interval",
"alu.stat-refresh.interval" },
.type = GF_OPTION_TYPE_TIME
},
{ .key = { "scheduler.limits.min-free-disk",
"alu.limits.min-free-disk" },
.type = GF_OPTION_TYPE_PERCENT
},
{ .key = { "scheduler.alu.stat-refresh.num-file-create"
"alu.stat-refresh.num-file-create"},
.type = GF_OPTION_TYPE_INT
},
{ .key = {NULL}, }
};
|