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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// CPU scheduling logic.
//
// - create an ordered "run list" (make_run_list()).
// The ordering is roughly as follows:
// - GPU jobs first, then CPU jobs
// - for a given resource, jobs in deadline danger first
// - jobs from projects with lower recent est. credit first
// In principle, the run list could include all runnable jobs.
// For efficiency, we stop adding:
// - GPU jobs: when all GPU instances used
// - CPU jobs: when the # of CPUs allocated to single-thread jobs,
// OR the # allocated to multi-thread jobs, exceeds # CPUs
// (ensure we have enough single-thread jobs
// in case we can't run the multi-thread jobs)
// NOTE: RAM usage is not taken into consideration
// in the process of building this list.
// It's possible that we include a bunch of jobs that can't run
// because of memory limits,
// even though there are other jobs that could run.
// - add running jobs to the list
// (in case they haven't finished time slice or checkpointed)
// - sort the list according to "more_important()"
// - shuffle the list to avoid starving multi-thread jobs
//
// - scan through the resulting list, running the jobs and preempting
// other jobs (enforce_run_list).
// Don't run a job if
// - its GPUs can't be assigned (possible if need >1 GPU)
// - it's a multi-thread job, and CPU usage would be #CPUs+1 or more
// - it's a single-thread job, don't oversaturate CPU
// (details depend on whether a MT job is running)
// - its memory usage would exceed RAM limits
// If there's a running job using a given app version,
// unstarted jobs using that app version
// are assumed to have the same working set size.
#include "cpp.h"
#ifdef _WIN32
#include "boinc_win.h"
#include "sysmon_win.h"
#else
#include "config.h"
#include <string>
#include <cstring>
#include <list>
#endif
#include "coproc.h"
#include "error_numbers.h"
#include "filesys.h"
#include "str_util.h"
#include "util.h"
#include "app.h"
#include "app_config.h"
#include "client_msgs.h"
#include "client_state.h"
#include "coproc_sched.h"
#include "log_flags.h"
#include "project.h"
#include "result.h"
using std::vector;
using std::list;
static double rec_sum;
// used in make_run_list() to keep track of resources used
// by jobs tentatively scheduled so far
//
struct PROC_RESOURCES {
int ncpus;
double ncpus_used_st; // #CPUs of GPU or single-thread jobs
double ncpus_used_mt; // #CPUs of multi-thread jobs
COPROCS pr_coprocs;
void init() {
ncpus = gstate.ncpus;
ncpus_used_st = 0;
ncpus_used_mt = 0;
pr_coprocs.clone(coprocs, false);
pr_coprocs.clear_usage();
if (have_max_concurrent) {
max_concurrent_init();
}
}
// should we stop scanning jobs?
//
inline bool stop_scan_cpu() {
if (ncpus_used_st >= ncpus) return true;
if (ncpus_used_mt >= 2*ncpus) return true;
// kind of arbitrary, but need to have some limit
// in case there are only MT jobs, and lots of them
return false;
}
inline bool stop_scan_coproc(int rsc_type) {
COPROC& cp = pr_coprocs.coprocs[rsc_type];
for (int i=0; i<cp.count; i++) {
if (cp.usage[i] < 1) return false;
}
return true;
}
// should we consider scheduling this job?
// (i.e add it to the runnable list; not actually run it)
//
bool can_schedule(RESULT* rp, ACTIVE_TASK* atp) {
if (max_concurrent_exceeded(rp)) return false;
if (atp) {
// don't schedule if something's pending
//
switch (atp->task_state()) {
case PROCESS_ABORT_PENDING:
case PROCESS_QUIT_PENDING:
return false;
}
if (gstate.retry_shmem_time > gstate.now) {
if (atp->app_client_shm.shm == NULL) {
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] waiting for shared mem: %s",
rp->name
);
}
atp->needs_shmem = true;
return false;
}
atp->needs_shmem = false;
}
}
if (rp->schedule_backoff > gstate.now) return false;
if (rp->uses_gpu()) {
if (gpu_suspend_reason) return false;
}
if (rp->uses_coprocs()) {
if (sufficient_coprocs(*rp)) {
return true;
} else {
return false;
}
} else if (rp->avp->avg_ncpus > 1) {
if (ncpus_used_mt == 0) return true;
return (ncpus_used_mt + rp->avp->avg_ncpus <= ncpus);
} else {
return (ncpus_used_st < ncpus);
}
}
// we've decided to add this to the runnable list; update bookkeeping
//
void schedule(RESULT* rp, ACTIVE_TASK* atp, bool is_edf) {
int rt = rp->avp->gpu_usage.rsc_type;
// see if it's possible this job will be ruled out
// when we try to actually run it
// (e.g. it won't fit in RAM, or it uses GPU type w/ exclusions)
// If so, don't reserve CPU/GPU for it, to avoid starvation scenario
//
bool may_not_run = false;
if (atp && atp->too_large) {
may_not_run = true;
}
if (rt && rsc_work_fetch[rt].has_exclusions) {
may_not_run = true;
}
if (!may_not_run) {
if (rt) {
reserve_coprocs(*rp);
// don't increment CPU usage.
// This may seem odd; the reason is the following scenario:
// - this job uses lots of CPU (say, a whole one)
// - there's an uncheckpointed GPU job that uses little CPU
// - we end up running the uncheckpointed job
// - this causes all or part of a CPU to be idle
//
} else if (rp->avp->avg_ncpus > 1) {
ncpus_used_mt += rp->avp->avg_ncpus;
} else {
ncpus_used_st += rp->avp->avg_ncpus;
}
}
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] add to run list: %s (%s, %s) (prio %f)",
rp->name,
rsc_name_long(rt),
is_edf?"EDF":"FIFO",
rp->project->sched_priority
);
}
adjust_rec_sched(rp);
max_concurrent_inc(rp);
}
bool sufficient_coprocs(RESULT& r) {
APP_VERSION& av = *r.avp;
int rt = av.gpu_usage.rsc_type;
if (!rt) return true;
double x = av.gpu_usage.usage;
COPROC& cp = pr_coprocs.coprocs[rt];
for (int i=0; i<cp.count; i++) {
if (gpu_excluded(r.app, cp, i)) continue;
double unused = 1 - cp.usage[i];
x -= unused;
if (x <= 0) return true;
}
if (log_flags.cpu_sched_debug) {
msg_printf(r.project, MSG_INFO,
"[cpu_sched_debug] insufficient %s for %s",
cp.type, r.name
);
}
return false;
}
void reserve_coprocs(RESULT& r) {
double x;
APP_VERSION& av = *r.avp;
int rt = av.gpu_usage.rsc_type;
COPROC& cp = pr_coprocs.coprocs[rt];
x = av.gpu_usage.usage;
for (int i=0; i<cp.count; i++) {
if (gpu_excluded(r.app, cp, i)) continue;
double unused = 1 - cp.usage[i];
if (unused >= x) {
cp.usage[i] += x;
break;
} else {
cp.usage[i] = 1;
x -= unused;
}
}
if (log_flags.cpu_sched_debug) {
msg_printf(r.project, MSG_INFO,
"[cpu_sched_debug] reserving %f of coproc %s",
av.gpu_usage.usage, cp.type
);
}
}
};
bool gpus_usable = true;
#ifndef SIM
// see whether there's been a change in coproc usability;
// if so set or clear "coproc_missing" flags and return true.
//
bool check_coprocs_usable() {
#ifdef _WIN32
unsigned int i;
bool new_usable = !is_remote_desktop();
if (gpus_usable) {
if (!new_usable) {
gpus_usable = false;
for (i=0; i<gstate.results.size(); i++) {
RESULT* rp = gstate.results[i];
if (rp->avp->gpu_usage.rsc_type) {
rp->coproc_missing = true;
}
}
msg_printf(NULL, MSG_INFO,
"Remote desktop in use; disabling GPU tasks"
);
return true;
}
} else {
if (new_usable) {
gpus_usable = true;
for (i=0; i<gstate.results.size(); i++) {
RESULT* rp = gstate.results[i];
if (rp->avp->gpu_usage.rsc_type) {
rp->coproc_missing = false;
}
}
msg_printf(NULL, MSG_INFO,
"Remote desktop not in use; enabling GPU tasks"
);
return true;
}
}
#endif
return false;
}
#endif
// return true if the task has finished its time slice
// and has checkpointed since the end of the time slice
// (called only for scheduled tasks)
//
static inline bool finished_time_slice(ACTIVE_TASK* atp) {
double time_slice_end = atp->run_interval_start_wall_time + gstate.global_prefs.cpu_scheduling_period();
bool running_beyond_sched_period = gstate.now > time_slice_end;
bool checkpointed = atp->checkpoint_wall_time > time_slice_end;
if (running_beyond_sched_period && !checkpointed) {
atp->overdue_checkpoint = true;
}
return (running_beyond_sched_period && checkpointed);
}
// Choose a "best" runnable CPU job for each project
//
// Values are returned in project->next_runnable_result
// (skip projects for which this is already non-NULL)
//
// Don't choose results with already_selected == true;
// mark chosen results as already_selected.
//
// The preference order:
// 1. results with active tasks that are running
// 2. results with active tasks that are preempted (but have a process)
// 3. results with active tasks that have no process
// 4. results with no active task
//
// TODO: this is called in a loop over NCPUs, which is silly.
// Should call it once, and have it make an ordered list per project.
//
void CLIENT_STATE::assign_results_to_projects() {
unsigned int i;
RESULT* rp;
PROJECT* project;
// scan results with an ACTIVE_TASK
//
for (i=0; i<active_tasks.active_tasks.size(); i++) {
ACTIVE_TASK *atp = active_tasks.active_tasks[i];
if (!atp->runnable()) continue;
rp = atp->result;
if (rp->already_selected) continue;
if (rp->uses_coprocs()) continue;
if (!rp->runnable()) continue;
project = rp->project;
if (!project->next_runnable_result) {
project->next_runnable_result = rp;
continue;
}
// see if this task is "better" than the one currently
// selected for this project
//
ACTIVE_TASK *next_atp = lookup_active_task_by_result(
project->next_runnable_result
);
if ((next_atp->task_state() == PROCESS_UNINITIALIZED && atp->process_exists())
|| (next_atp->scheduler_state == CPU_SCHED_PREEMPTED
&& atp->scheduler_state == CPU_SCHED_SCHEDULED)
) {
project->next_runnable_result = atp->result;
}
}
// Now consider results that don't have an active task
//
for (i=0; i<results.size(); i++) {
rp = results[i];
if (rp->already_selected) continue;
if (rp->uses_coprocs()) continue;
if (lookup_active_task_by_result(rp)) continue;
if (!rp->runnable()) continue;
project = rp->project;
if (project->next_runnable_result) continue;
project->next_runnable_result = rp;
}
// mark selected results, so CPU scheduler won't try to consider
// a result more than once
//
for (i=0; i<projects.size(); i++) {
project = projects[i];
if (project->next_runnable_result) {
project->next_runnable_result->already_selected = true;
}
}
}
// Among projects with a "next runnable result",
// find the project P with the largest priority,
// and return its next runnable result
//
RESULT* CLIENT_STATE::highest_prio_project_best_result() {
PROJECT *best_project = NULL;
double best_prio = 0;
bool first = true;
unsigned int i;
for (i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
if (!p->next_runnable_result) continue;
if (p->non_cpu_intensive) continue;
if (first || p->sched_priority > best_prio) {
first = false;
best_project = p;
best_prio = p->sched_priority;
}
}
if (!best_project) return NULL;
RESULT* rp = best_project->next_runnable_result;
best_project->next_runnable_result = 0;
return rp;
}
// Return a job of the given type according to the following criteria
// (desc priority):
// - from project with higher priority
// - already-started job
// - earlier received_time
// - lexicographically earlier name
//
// Give priority to already-started jobs because of the following scenario:
// - client gets several jobs in a sched reply and starts downloading files
// - a later job finishes downloading and starts
// - an earlier finishes downloading and preempts
//
RESULT* first_coproc_result(int rsc_type) {
unsigned int i;
RESULT* best = NULL;
double best_prio=0, prio;
for (i=0; i<gstate.results.size(); i++) {
RESULT* rp = gstate.results[i];
if (rp->resource_type() != rsc_type) continue;
if (!rp->runnable()) {
//msg_printf(rp->project, MSG_INFO, "not runnable: %s", rp->name);
continue;
}
if (rp->non_cpu_intensive()) continue;
if (rp->already_selected) continue;
prio = rp->project->sched_priority;
if (!best) {
best = rp;
best_prio = prio;
continue;
}
if (prio < best_prio) {
continue;
}
if (prio > best_prio) {
best = rp;
best_prio = prio;
continue;
}
bool bs = !best->not_started;
bool rs = !rp->not_started;
if (rs && !bs) {
best = rp;
best_prio = prio;
continue;
}
if (!rs && bs) {
continue;
}
// else used "arrived first" order
//
if (rp->index < best->index) {
best = rp;
best_prio = prio;
}
}
return best;
}
// Return earliest-deadline result for given resource type;
// return only results projected to miss their deadline,
// or from projects with extreme DCF
//
static RESULT* earliest_deadline_result(int rsc_type) {
RESULT *best_result = NULL;
ACTIVE_TASK* best_atp = NULL;
unsigned int i;
for (i=0; i<gstate.results.size(); i++) {
RESULT* rp = gstate.results[i];
if (rp->resource_type() != rsc_type) continue;
if (rp->already_selected) continue;
if (!rp->runnable()) continue;
if (rp->non_cpu_intensive()) continue;
PROJECT* p = rp->project;
// Skip this job if the project's deadline-miss count is zero.
// If the project's DCF is > 90 (and we're not ignoring it)
// treat all jobs as deadline misses
//
if (p->dont_use_dcf || p->duration_correction_factor < 90.0) {
if (p->rsc_pwf[rsc_type].deadlines_missed_copy <= 0) {
continue;
}
}
bool new_best = false;
if (best_result) {
if (rp->report_deadline < best_result->report_deadline) {
new_best = true;
}
} else {
new_best = true;
}
if (new_best) {
best_result = rp;
best_atp = gstate.lookup_active_task_by_result(rp);
continue;
}
if (rp->report_deadline > best_result->report_deadline) {
continue;
}
// If there's a tie, pick the job with the least remaining time
// (but don't pick an unstarted job over one that's started)
//
ACTIVE_TASK* atp = gstate.lookup_active_task_by_result(rp);
if (best_atp && !atp) continue;
if (rp->estimated_runtime_remaining() < best_result->estimated_runtime_remaining()
|| (!best_atp && atp)
) {
best_result = rp;
best_atp = atp;
}
}
if (!best_result) return NULL;
return best_result;
}
void CLIENT_STATE::reset_rec_accounting() {
unsigned int i;
for (i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
for (int j=0; j<coprocs.n_rsc; j++) {
p->rsc_pwf[j].reset_rec_accounting();
}
}
for (int j=0; j<coprocs.n_rsc; j++) {
rsc_work_fetch[j].reset_rec_accounting();
}
rec_interval_start = now;
}
// update per-project accounting:
// - recent estimated credit (EC)
// - total CPU and GPU EC
// - total CPU and GPU time
//
static void update_rec() {
double f = gstate.host_info.p_fpops;
double on_frac = gstate.global_prefs.cpu_usage_limit / 100;
for (unsigned int i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
double x = 0;
for (int j=0; j<coprocs.n_rsc; j++) {
double dt = p->rsc_pwf[j].secs_this_rec_interval * on_frac;
double flops = dt * f * rsc_work_fetch[j].relative_speed;
x += flops;
if (j) {
p->gpu_ec += flops*COBBLESTONE_SCALE;
p->gpu_time += dt;
} else {
p->cpu_ec += flops*COBBLESTONE_SCALE;
p->cpu_time += dt;
}
}
x *= COBBLESTONE_SCALE;
double old = p->pwf.rec;
// start averages at zero
//
if (p->pwf.rec_time == 0) {
p->pwf.rec_time = gstate.rec_interval_start;
}
update_average(
gstate.now,
gstate.rec_interval_start,
x,
cc_config.rec_half_life,
p->pwf.rec,
p->pwf.rec_time
);
if (log_flags.priority_debug) {
double dt = gstate.now - gstate.rec_interval_start;
msg_printf(p, MSG_INFO,
"[prio] recent est credit: %.2fG in %.2f sec, %f + %f ->%f",
x, dt, old, p->pwf.rec-old, p->pwf.rec
);
}
}
}
static double peak_flops(APP_VERSION* avp) {
double f = gstate.host_info.p_fpops;
double x = f * avp->avg_ncpus;
int rt = avp->gpu_usage.rsc_type;
if (rt) {
x += f * avp->gpu_usage.usage * rsc_work_fetch[rt].relative_speed;
}
return x;
}
double total_peak_flops() {
static bool first=true;
static double tpf;
if (first) {
first = false;
tpf = gstate.host_info.p_fpops * gstate.ncpus;
for (int i=1; i<coprocs.n_rsc; i++) {
COPROC& cp = coprocs.coprocs[i];
tpf += rsc_work_fetch[i].relative_speed * gstate.host_info.p_fpops * cp.count;
}
}
return tpf;
}
// Initialize project "priorities" based on REC:
// compute resource share and REC fractions
// among compute-intensive, non-suspended projects
//
void project_priority_init(bool for_work_fetch) {
double rs_sum = 0;
rec_sum = 0;
for (unsigned int i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
if (p->non_cpu_intensive) continue;
if (for_work_fetch) {
if (!p->can_request_work()) continue;
} else {
if (!p->runnable(RSC_TYPE_ANY)) continue;
}
p->pwf.rec_temp = p->pwf.rec;
rs_sum += p->resource_share;
rec_sum += p->pwf.rec_temp;
}
if (rec_sum == 0) {
rec_sum = 1;
}
for (unsigned int i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
if (p->non_cpu_intensive || p->suspended_via_gui || rs_sum==0) {
p->resource_share_frac = 0;
p->sched_priority = 0;
} else {
p->resource_share_frac = p->resource_share/rs_sum;
p->compute_sched_priority();
if (log_flags.priority_debug) {
msg_printf(p, MSG_INFO, "[prio] %f rsf %f rt %f rs %f",
p->sched_priority, p->resource_share_frac,
p->pwf.rec_temp, rec_sum
);
}
}
}
}
void PROJECT::compute_sched_priority() {
double rec_frac = pwf.rec_temp/rec_sum;
// projects with zero resource share are always lower priority
// than those with positive resource share
//
if (resource_share == 0) {
sched_priority = -1e3 - rec_frac;
} else {
sched_priority = - rec_frac/resource_share_frac;
}
}
// called from the scheduler's job-selection loop;
// we plan to run this job;
// bump the project's temp REC by the estimated credit for 1 hour.
// This encourages a mixture jobs from different projects.
//
void adjust_rec_sched(RESULT* rp) {
PROJECT* p = rp->project;
p->pwf.rec_temp += peak_flops(rp->avp)/total_peak_flops() * rec_sum/24;
p->compute_sched_priority();
}
// make this a variable so simulator can change it
//
double rec_adjust_period = REC_ADJUST_PERIOD;
// adjust project REC
//
void CLIENT_STATE::adjust_rec() {
unsigned int i;
double elapsed_time = now - rec_interval_start;
// If the elapsed time is negative or more than 2*REC_ADJUST_PERIOD
// it must be because either
// - the system clock was changed.
// - the host was suspended for a long time.
// In either case, ignore the last period
//
if (elapsed_time > 2*rec_adjust_period || elapsed_time < 0) {
if (log_flags.priority_debug) {
msg_printf(NULL, MSG_INFO,
"[priority] adjust_rec: elapsed time (%.0f) negative or longer than sched enforce period(%.0f). Ignoring this period.",
elapsed_time, rec_adjust_period
);
}
reset_rec_accounting();
return;
}
// skip small intervals
//
if (elapsed_time < 1) {
return;
}
// total up how many instance-seconds projects got
//
for (i=0; i<active_tasks.active_tasks.size(); i++) {
ACTIVE_TASK* atp = active_tasks.active_tasks[i];
if (atp->scheduler_state != CPU_SCHED_SCHEDULED) continue;
PROJECT* p = atp->result->project;
if (p->non_cpu_intensive) continue;
work_fetch.accumulate_inst_sec(atp, elapsed_time);
}
update_rec();
reset_rec_accounting();
}
// Possibly do job scheduling.
// This is called periodically.
//
bool CLIENT_STATE::schedule_cpus() {
double elapsed_time;
static double last_reschedule=0;
vector<RESULT*> run_list;
if (projects.size() == 0) return false;
if (results.size() == 0) return false;
// Reschedule every CPU_SCHED_PERIOD seconds,
// or if must_schedule_cpus is set
// (meaning a new result is available, or a CPU has been freed).
//
elapsed_time = now - last_reschedule;
if (gstate.clock_change || elapsed_time >= CPU_SCHED_PERIOD) {
request_schedule_cpus("periodic CPU scheduling");
}
if (!must_schedule_cpus) return false;
last_reschedule = now;
must_schedule_cpus = false;
// NOTE: there's an assumption that REC is adjusted at
// least as often as the CPU sched period (see client_state.h).
// If you remove the following, make changes accordingly
//
adjust_rec();
make_run_list(run_list);
return enforce_run_list(run_list);
}
// Mark a job J as a deadline miss if either
// - it once ran in EDF, and its project has another job
// of the same resource type marked as deadline miss.
// This avoids domino-effect preemption
// - it was recently marked as a deadline miss by RR sim.
// This avoids "thrashing" if a job oscillates between miss and not miss.
//
static void promote_once_ran_edf() {
for (unsigned int i=0; i<gstate.active_tasks.active_tasks.size(); i++) {
ACTIVE_TASK* atp = gstate.active_tasks.active_tasks[i];
if (atp->result->rr_sim_misses_deadline) continue;
if (atp->once_ran_edf) {
RESULT* rp = atp->result;
PROJECT* p = rp->project;
if (p->deadlines_missed(rp->avp->rsc_type())) {
if (log_flags.cpu_sched_debug) {
msg_printf(p, MSG_INFO,
"[cpu_sched_debug] domino prevention: mark %s as deadline miss",
rp->name
);
}
rp->rr_sim_misses_deadline = true;
continue;
}
}
if (gstate.now - atp->last_deadline_miss_time < gstate.global_prefs.cpu_scheduling_period()) {
if (log_flags.cpu_sched_debug) {
RESULT* rp = atp->result;
PROJECT* p = rp->project;
msg_printf(p, MSG_INFO,
"[cpu_sched_debug] thrashing prevention: mark %s as deadline miss",
rp->name
);
}
atp->result->rr_sim_misses_deadline = true;
}
}
}
void add_coproc_jobs(
vector<RESULT*>& run_list, int rsc_type, PROC_RESOURCES& proc_rsc
) {
ACTIVE_TASK* atp;
RESULT* rp;
#ifdef SIM
if (!cpu_sched_rr_only) {
#endif
// choose coproc jobs from projects with coproc deadline misses
//
while (!proc_rsc.stop_scan_coproc(rsc_type)) {
rp = earliest_deadline_result(rsc_type);
if (!rp) break;
rp->already_selected = true;
atp = gstate.lookup_active_task_by_result(rp);
if (!proc_rsc.can_schedule(rp, atp)) continue;
proc_rsc.schedule(rp, atp, true);
rp->project->rsc_pwf[rsc_type].deadlines_missed_copy--;
rp->edf_scheduled = true;
run_list.push_back(rp);
}
#ifdef SIM
}
#endif
// then coproc jobs in FIFO order
//
while (!proc_rsc.stop_scan_coproc(rsc_type)) {
rp = first_coproc_result(rsc_type);
if (!rp) break;
rp->already_selected = true;
atp = gstate.lookup_active_task_by_result(rp);
if (!proc_rsc.can_schedule(rp, atp)) continue;
proc_rsc.schedule(rp, atp, false);
run_list.push_back(rp);
}
}
// Make an ordered list of jobs to run.
//
void CLIENT_STATE::make_run_list(vector<RESULT*>& run_list) {
RESULT* rp;
PROJECT* p;
unsigned int i;
PROC_RESOURCES proc_rsc;
ACTIVE_TASK* atp;
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] schedule_cpus(): start");
}
proc_rsc.init();
// do round-robin simulation to find what results miss deadline
//
rr_simulation();
if (log_flags.rr_simulation) {
print_deadline_misses();
}
// avoid preemption of jobs that once ran EDF
//
promote_once_ran_edf();
// set temporary variables
//
project_priority_init(false);
for (i=0; i<results.size(); i++) {
rp = results[i];
rp->already_selected = false;
rp->edf_scheduled = false;
rp->not_started = !rp->computing_done();
}
for (i=0; i<projects.size(); i++) {
p = projects[i];
p->next_runnable_result = NULL;
for (int j=0; j<coprocs.n_rsc; j++) {
p->rsc_pwf[j].deadlines_missed_copy = p->rsc_pwf[j].deadlines_missed;
}
}
// compute max working set size for app versions
// (max of working sets of currently running jobs)
//
for (i=0; i<app_versions.size(); i++) {
app_versions[i]->max_working_set_size = 0;
}
for (i=0; i<active_tasks.active_tasks.size(); i++) {
atp = active_tasks.active_tasks[i];
atp->too_large = false;
double w = atp->procinfo.working_set_size_smoothed;
APP_VERSION* avp = atp->app_version;
if (w > avp->max_working_set_size) {
avp->max_working_set_size = w;
}
atp->result->not_started = false;
}
// first, add GPU jobs
for (int j=1; j<coprocs.n_rsc; j++) {
add_coproc_jobs(run_list, j, proc_rsc);
}
// then add CPU jobs.
// Note: the jobs that actually get run are not necessarily
// an initial segment of this list;
// e.g. a multithread job may not get run because it has
// a high-priority single-thread job ahead of it.
// choose CPU jobs from projects with CPU deadline misses
//
#ifdef SIM
if (!cpu_sched_rr_only) {
#endif
while (!proc_rsc.stop_scan_cpu()) {
rp = earliest_deadline_result(RSC_TYPE_CPU);
if (!rp) break;
rp->already_selected = true;
atp = lookup_active_task_by_result(rp);
if (!proc_rsc.can_schedule(rp, atp)) continue;
proc_rsc.schedule(rp, atp, true);
rp->project->rsc_pwf[0].deadlines_missed_copy--;
rp->edf_scheduled = true;
run_list.push_back(rp);
}
#ifdef SIM
}
#endif
// Next, choose CPU jobs from highest priority projects
//
while (!proc_rsc.stop_scan_cpu()) {
assign_results_to_projects();
rp = highest_prio_project_best_result();
if (!rp) break;
atp = lookup_active_task_by_result(rp);
if (!proc_rsc.can_schedule(rp, atp)) continue;
proc_rsc.schedule(rp, atp, false);
run_list.push_back(rp);
}
}
static inline bool in_run_list(vector<RESULT*>& run_list, ACTIVE_TASK* atp) {
for (unsigned int i=0; i<run_list.size(); i++) {
if (atp->result == run_list[i]) return true;
}
return false;
}
#if 0
// scan the runnable list, keeping track of CPU usage X.
// if find a MT job J, and X < ncpus, move J before all non-MT jobs
// But don't promote a MT job ahead of a job in EDF
//
// This is needed because there may always be a 1-CPU job
// in the middle of its time-slice, and MT jobs could starve.
//
static void promote_multi_thread_jobs(vector<RESULT*>& runnable_jobs) {
double cpus_used = 0;
vector<RESULT*>::iterator first_non_mt = runnable_jobs.end();
vector<RESULT*>::iterator cur = runnable_jobs.begin();
while(1) {
if (cur == runnable_jobs.end()) break;
if (cpus_used >= gstate.ncpus) break;
RESULT* rp = *cur;
if (rp->rr_sim_misses_deadline) break;
double nc = rp->avp->avg_ncpus;
if (nc > 1) {
if (first_non_mt != runnable_jobs.end()) {
cur = runnable_jobs.erase(cur);
runnable_jobs.insert(first_non_mt, rp);
cpus_used = 0;
first_non_mt = runnable_jobs.end();
cur = runnable_jobs.begin();
continue;
}
} else {
if (first_non_mt == runnable_jobs.end()) {
first_non_mt = cur;
}
}
cpus_used += nc;
cur++;
}
}
#endif
// return true if r0 is more important to run than r1
//
static inline bool more_important(RESULT* r0, RESULT* r1) {
// favor jobs in danger of deadline miss
//
bool miss0 = r0->edf_scheduled;
bool miss1 = r1->edf_scheduled;
if (miss0 && !miss1) return true;
if (!miss0 && miss1) return false;
// favor coproc jobs, so that e.g. if we're RAM-limited
// we'll use the GPU instead of the CPU
//
bool cp0 = r0->uses_coprocs();
bool cp1 = r1->uses_coprocs();
if (cp0 && !cp1) return true;
if (!cp0 && cp1) return false;
// favor jobs in the middle of time slice,
// or that haven't checkpointed since start of time slice
//
bool unfin0 = r0->unfinished_time_slice;
bool unfin1 = r1->unfinished_time_slice;
if (unfin0 && !unfin1) return true;
if (!unfin0 && unfin1) return false;
// for CPU jobs, favor jobs that use more CPUs
//
if (!cp0) {
if (r0->avp->avg_ncpus > r1->avp->avg_ncpus) return true;
if (r1->avp->avg_ncpus > r0->avp->avg_ncpus) return false;
}
// favor jobs selected first by schedule_cpus()
// (e.g., because their project has high sched priority)
//
if (r0->seqno < r1->seqno) return true;
if (r0->seqno > r1->seqno) return false;
// tie breaker
return (r0 < r1);
}
static void print_job_list(vector<RESULT*>& jobs) {
for (unsigned int i=0; i<jobs.size(); i++) {
RESULT* rp = jobs[i];
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] %d: %s (MD: %s; UTS: %s)",
i, rp->name,
rp->edf_scheduled?"yes":"no",
rp->unfinished_time_slice?"yes":"no"
);
}
}
// find running jobs that haven't finished their time slice.
// Mark them as such, and add to list if not already there
//
void CLIENT_STATE::append_unfinished_time_slice(vector<RESULT*> &run_list) {
unsigned int i;
int seqno = (int)run_list.size();
for (i=0; i<active_tasks.active_tasks.size(); i++) {
ACTIVE_TASK* atp = active_tasks.active_tasks[i];
atp->overdue_checkpoint = false;
if (!atp->result->runnable()) continue;
if (atp->result->uses_gpu() && gpu_suspend_reason) continue;
if (atp->result->non_cpu_intensive()) continue;
if (atp->scheduler_state != CPU_SCHED_SCHEDULED) continue;
if (finished_time_slice(atp)) continue;
atp->result->unfinished_time_slice = true;
if (in_run_list(run_list, atp)) continue;
run_list.push_back(atp->result);
atp->result->seqno = seqno;
}
}
// Enforce the CPU schedule.
// Inputs:
// ordered_scheduled_results
// List of tasks that should (ideally) run, set by schedule_cpus().
// Most important tasks (e.g. early deadline) are first.
// The set of tasks that actually run may be different:
// - if a task hasn't checkpointed recently we avoid preempting it
// - we don't run tasks that would exceed working-set limits
// Details:
// Initially, each task's scheduler_state is PREEMPTED or SCHEDULED
// depending on whether or not it is running.
// This function sets each task's next_scheduler_state,
// and at the end it starts/resumes and preempts tasks
// based on scheduler_state and next_scheduler_state.
//
bool CLIENT_STATE::enforce_run_list(vector<RESULT*>& run_list) {
unsigned int i;
int retval;
double ncpus_used=0;
ACTIVE_TASK* atp;
bool action = false;
if (have_max_concurrent) max_concurrent_init();
#ifndef SIM
// check whether GPUs are usable
//
if (check_coprocs_usable()) {
request_schedule_cpus("GPU usability change");
return true;
}
#endif
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] enforce_run_list(): start");
msg_printf(0, MSG_INFO, "[cpu_sched_debug] preliminary job list:");
print_job_list(run_list);
}
// Set next_scheduler_state to PREEMPT for all tasks
//
for (i=0; i< active_tasks.active_tasks.size(); i++) {
atp = active_tasks.active_tasks[i];
atp->next_scheduler_state = CPU_SCHED_PREEMPTED;
}
for (i=0; i<run_list.size(); i++) {
RESULT* rp = run_list[i];
rp->seqno = i;
rp->unfinished_time_slice = false;
}
// append running jobs not done with time slice to the to-run list
//
append_unfinished_time_slice(run_list);
// sort to-run list by decreasing importance
//
std::sort(
run_list.begin(),
run_list.end(),
more_important
);
#if 0
promote_multi_thread_jobs(run_list);
#endif
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] final job list:");
print_job_list(run_list);
}
double ram_left = available_ram();
double swap_left = (global_prefs.vm_max_used_frac)*host_info.m_swap;
if (log_flags.mem_usage_debug) {
msg_printf(0, MSG_INFO,
"[mem_usage] enforce: available RAM %.2fMB swap %.2fMB",
ram_left/MEGA, swap_left/MEGA
);
}
// schedule non-CPU-intensive tasks,
// and look for backed-off GPU jobs
//
for (i=0; i<results.size(); i++) {
RESULT* rp = results[i];
if (rp->non_cpu_intensive() && rp->runnable()) {
atp = get_task(rp);
if (!atp) {
msg_printf(rp->project, MSG_INTERNAL_ERROR,
"Can't create task for %s", rp->name
);
continue;
}
atp->next_scheduler_state = CPU_SCHED_SCHEDULED;
// don't count RAM usage because it's used sporadically,
// and doing so can starve other jobs
//
//ram_left -= atp->procinfo.working_set_size_smoothed;
swap_left -= atp->procinfo.swap_size;
}
}
// assign coprocessors to coproc jobs,
// and prune those that can't be assigned
//
assign_coprocs(run_list);
bool scheduled_mt = false;
// prune jobs that don't fit in RAM or that exceed CPU usage limits.
// Mark the rest as SCHEDULED
//
for (i=0; i<run_list.size(); i++) {
RESULT* rp = run_list[i];
if (max_concurrent_exceeded(rp)) {
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] skipping %s; max concurrent limit %d reached",
rp->name, rp->app->max_concurrent
);
}
continue;
}
atp = lookup_active_task_by_result(rp);
// if we're already using all the CPUs,
// don't allow additional CPU jobs;
// allow coproc jobs if the resulting CPU load is at most ncpus+1
//
if (ncpus_used >= ncpus) {
if (rp->uses_coprocs()) {
if (ncpus_used + rp->avp->avg_ncpus > ncpus+1) {
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] skipping GPU job %s; CPU committed",
rp->name
);
}
continue;
}
} else {
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] all CPUs used (%.2f >= %d), skipping %s",
ncpus_used, ncpus,
rp->name
);
}
continue;
}
}
#if 0
// Don't overcommit CPUs by > 1 if a MT job is scheduled.
// Skip this check for coproc jobs.
//
if (!rp->uses_coprocs()
&& (scheduled_mt || (rp->avp->avg_ncpus > 1))
&& (ncpus_used + rp->avp->avg_ncpus > ncpus + 1)
) {
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] avoid MT overcommit: skipping %s",
rp->name
);
}
continue;
}
#endif
// skip jobs whose working set is too large to fit in available RAM
//
double wss = 0;
if (atp) {
atp->too_large = false;
wss = atp->procinfo.working_set_size_smoothed;
} else {
wss = rp->avp->max_working_set_size;
}
if (wss == 0) {
wss = rp->wup->rsc_memory_bound;
}
if (wss > ram_left) {
if (atp) {
atp->too_large = true;
}
if (log_flags.cpu_sched_debug || log_flags.mem_usage_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] enforce: task %s can't run, too big %.2fMB > %.2fMB",
rp->name, wss/MEGA, ram_left/MEGA
);
}
continue;
}
if (log_flags.cpu_sched_debug) {
msg_printf(rp->project, MSG_INFO,
"[cpu_sched_debug] scheduling %s%s",
rp->name,
rp->edf_scheduled?" (high priority)":""
);
}
// We've decided to run this job; create an ACTIVE_TASK if needed.
//
if (!atp) {
atp = get_task(rp);
}
if (!atp) {
msg_printf(rp->project, MSG_INTERNAL_ERROR,
"Can't create task for %s", rp->name
);
continue;
}
if (rp->avp->avg_ncpus > 1) {
scheduled_mt = true;
}
ncpus_used += rp->avp->avg_ncpus;
atp->next_scheduler_state = CPU_SCHED_SCHEDULED;
ram_left -= wss;
max_concurrent_inc(rp);
}
if (log_flags.cpu_sched_debug && ncpus_used < ncpus) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] using %.2f out of %d CPUs",
ncpus_used, ncpus
);
if (ncpus_used < ncpus) {
request_work_fetch("CPUs idle");
}
}
bool check_swap = (host_info.m_swap != 0);
// in case couldn't measure swap on this host
// TODO: enforcement of swap space is broken right now
// preempt tasks as needed, and note whether there are any coproc jobs
// in QUIT_PENDING state (in which case we won't start new coproc jobs)
//
bool coproc_quit_pending = false;
for (i=0; i<active_tasks.active_tasks.size(); i++) {
atp = active_tasks.active_tasks[i];
#if 0
if (log_flags.cpu_sched_debug) {
msg_printf(atp->result->project, MSG_INFO,
"[cpu_sched_debug] %s sched state %d next %d task state %d",
atp->result->name, atp->scheduler_state,
atp->next_scheduler_state, atp->task_state()
);
}
#endif
int preempt_type = REMOVE_MAYBE_SCHED;
switch (atp->next_scheduler_state) {
case CPU_SCHED_PREEMPTED:
switch (atp->task_state()) {
case PROCESS_EXECUTING:
action = true;
if (check_swap && swap_left < 0) {
if (log_flags.mem_usage_debug) {
msg_printf(atp->result->project, MSG_INFO,
"[mem_usage] out of swap space, will preempt by quit"
);
}
preempt_type = REMOVE_ALWAYS;
}
if (atp->too_large) {
if (log_flags.mem_usage_debug) {
msg_printf(atp->result->project, MSG_INFO,
"[mem_usage] job using too much memory, will preempt by quit"
);
}
preempt_type = REMOVE_ALWAYS;
}
atp->preempt(preempt_type);
break;
case PROCESS_SUSPENDED:
// remove from memory GPU jobs that were suspended by CPU throttling
// and are now unscheduled.
//
if (atp->result->uses_gpu()) {
atp->preempt(REMOVE_ALWAYS);
request_schedule_cpus("removed suspended GPU task");
break;
}
// Handle the case where user changes prefs from
// "leave in memory" to "remove from memory";
// need to quit suspended tasks.
//
if (atp->checkpoint_cpu_time && !global_prefs.leave_apps_in_memory) {
atp->preempt(REMOVE_ALWAYS);
}
break;
}
atp->scheduler_state = CPU_SCHED_PREEMPTED;
break;
}
if (atp->result->uses_coprocs() && atp->task_state() == PROCESS_QUIT_PENDING) {
coproc_quit_pending = true;
}
}
bool coproc_start_deferred = false;
for (i=0; i<active_tasks.active_tasks.size(); i++) {
atp = active_tasks.active_tasks[i];
if (atp->next_scheduler_state != CPU_SCHED_SCHEDULED) continue;
int ts = atp->task_state();
if (ts == PROCESS_UNINITIALIZED || ts == PROCESS_SUSPENDED) {
// If there's a quit pending for a coproc job,
// don't start new ones since they may bomb out
// on memory allocation. Instead, trigger a retry
//
if (atp->result->uses_coprocs() && coproc_quit_pending) {
coproc_start_deferred = true;
continue;
}
action = true;
bool first_time;
// GPU tasks can get suspended before they're ever run,
// so the only safe way of telling whether this is the
// first time the app is run is to check
// whether the slot dir is empty
//
#ifdef SIM
first_time = atp->scheduler_state == CPU_SCHED_UNINITIALIZED;
#else
first_time = is_dir_empty(atp->slot_dir);
#endif
retval = atp->resume_or_start(first_time);
if ((retval == ERR_SHMGET) || (retval == ERR_SHMAT)) {
// Assume no additional shared memory segs
// will be available in the next 10 seconds
// (run only tasks which are already attached to shared memory).
//
if (gstate.retry_shmem_time < gstate.now) {
request_schedule_cpus("no more shared memory");
}
gstate.retry_shmem_time = gstate.now + 10.0;
continue;
}
if (retval) {
char err_msg[4096];
snprintf(err_msg, sizeof(err_msg),
"Couldn't start or resume: %d", retval
);
report_result_error(*(atp->result), err_msg);
request_schedule_cpus("start failed");
continue;
}
if (atp->result->rr_sim_misses_deadline) {
atp->once_ran_edf = true;
}
atp->run_interval_start_wall_time = now;
app_started = now;
}
if (log_flags.cpu_sched_status) {
msg_printf(atp->result->project, MSG_INFO,
"[css] running %s (%s)",
atp->result->name, atp->result->resources
);
}
atp->scheduler_state = CPU_SCHED_SCHEDULED;
swap_left -= atp->procinfo.swap_size;
#ifndef SIM
// if we've been in this loop for > 10 secs,
// break out of it and arrange for another schedule()
// so that we don't miss GUI RPCs, heartbeats etc.
//
if (dtime() - now > MAX_STARTUP_TIME) {
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO,
"[cpu_sched_debug] app startup took %f secs", dtime() - now
);
}
request_schedule_cpus("slow app startup");
break;
}
#endif
}
if (action) {
set_client_state_dirty("enforce_cpu_schedule");
}
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] enforce_run_list: end");
}
if (coproc_start_deferred) {
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO,
"[cpu_sched_debug] coproc quit pending, deferring start"
);
}
request_schedule_cpus("coproc quit retry");
}
return action;
}
// trigger CPU scheduling.
// Called when a result is completed,
// when new results become runnable,
// or when the user performs a UI interaction
// (e.g. suspending or resuming a project or result).
//
void CLIENT_STATE::request_schedule_cpus(const char* where) {
if (log_flags.cpu_sched_debug) {
msg_printf(0, MSG_INFO, "[cpu_sched_debug] Request CPU reschedule: %s", where);
}
must_schedule_cpus = true;
}
// Find the active task for a given result
//
ACTIVE_TASK* CLIENT_STATE::lookup_active_task_by_result(RESULT* rep) {
for (unsigned int i = 0; i < active_tasks.active_tasks.size(); i ++) {
if (active_tasks.active_tasks[i]->result == rep) {
return active_tasks.active_tasks[i];
}
}
return NULL;
}
// find total resource shares of all CPU-intensive projects
//
double CLIENT_STATE::total_resource_share() {
double x = 0;
for (unsigned int i=0; i<projects.size(); i++) {
if (!projects[i]->non_cpu_intensive ) {
x += projects[i]->resource_share;
}
}
return x;
}
// same, but only runnable projects (can use CPU right now)
//
double CLIENT_STATE::runnable_resource_share(int rsc_type) {
double x = 0;
for (unsigned int i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
if (p->non_cpu_intensive) continue;
if (p->runnable(rsc_type)) {
x += p->resource_share;
}
}
return x;
}
// same, but potentially runnable (could ask for work right now)
//
double CLIENT_STATE::potentially_runnable_resource_share() {
double x = 0;
for (unsigned int i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
if (p->non_cpu_intensive) continue;
if (p->potentially_runnable()) {
x += p->resource_share;
}
}
return x;
}
// same, but nearly runnable (could be downloading work right now)
//
double CLIENT_STATE::nearly_runnable_resource_share() {
double x = 0;
for (unsigned int i=0; i<projects.size(); i++) {
PROJECT* p = projects[i];
if (p->non_cpu_intensive) continue;
if (p->nearly_runnable()) {
x += p->resource_share;
}
}
return x;
}
// if there's not an active task for the result, make one
//
ACTIVE_TASK* CLIENT_STATE::get_task(RESULT* rp) {
ACTIVE_TASK *atp = lookup_active_task_by_result(rp);
if (!atp) {
atp = new ACTIVE_TASK;
int retval = atp->get_free_slot(rp);
if (retval) {
delete atp;
return NULL;
}
atp->init(rp);
active_tasks.active_tasks.push_back(atp);
}
return atp;
}
// called at startup (after get_host_info())
// and when general prefs have been parsed.
// NOTE: GSTATE.NCPUS MUST BE 1 OR MORE; WE DIVIDE BY IT IN A COUPLE OF PLACES
//
void CLIENT_STATE::set_ncpus() {
int ncpus_old = ncpus;
if (cc_config.ncpus>0) {
ncpus = cc_config.ncpus;
host_info.p_ncpus = ncpus;
} else if (host_info.p_ncpus>0) {
ncpus = host_info.p_ncpus;
} else {
ncpus = 1;
}
if (global_prefs.max_ncpus_pct) {
ncpus = (int)((ncpus * global_prefs.max_ncpus_pct)/100);
if (ncpus == 0) ncpus = 1;
}
if (initialized && ncpus != ncpus_old) {
msg_printf(0, MSG_INFO,
"Number of usable CPUs has changed from %d to %d.",
ncpus_old, ncpus
);
request_schedule_cpus("Number of usable CPUs has changed");
request_work_fetch("Number of usable CPUs has changed");
work_fetch.init();
}
}
// The given result has just completed successfully.
// Update the correction factor used to predict
// completion time for this project's results
//
void PROJECT::update_duration_correction_factor(ACTIVE_TASK* atp) {
if (dont_use_dcf) return;
RESULT* rp = atp->result;
double raw_ratio = atp->elapsed_time/rp->estimated_runtime_uncorrected();
double adj_ratio = atp->elapsed_time/rp->estimated_runtime();
double old_dcf = duration_correction_factor;
// it's OK to overestimate completion time,
// but bad to underestimate it.
// So make it easy for the factor to increase,
// but decrease it with caution
//
if (adj_ratio > 1.1) {
duration_correction_factor = raw_ratio;
} else {
// in particular, don't give much weight to results
// that completed a lot earlier than expected
//
if (adj_ratio < 0.1) {
duration_correction_factor = duration_correction_factor*0.99 + 0.01*raw_ratio;
} else {
duration_correction_factor = duration_correction_factor*0.9 + 0.1*raw_ratio;
}
}
// limit to [.01 .. 100]
//
if (duration_correction_factor > 100) duration_correction_factor = 100;
if (duration_correction_factor < 0.01) duration_correction_factor = 0.01;
if (log_flags.dcf_debug) {
msg_printf(this, MSG_INFO,
"[dcf] DCF: %f->%f, raw_ratio %f, adj_ratio %f",
old_dcf, duration_correction_factor, raw_ratio, adj_ratio
);
}
}
|