File: cs_sporadic.cpp

package info (click to toggle)
boinc 8.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 106,832 kB
  • sloc: cpp: 167,537; php: 111,699; pascal: 56,262; ansic: 49,284; xml: 18,762; python: 7,938; javascript: 6,538; sh: 5,719; makefile: 2,183; java: 2,041; objc: 1,867; perl: 1,843; sql: 830; lisp: 47; csh: 30
file content (246 lines) | stat: -rw-r--r-- 9,067 bytes parent folder | download | duplicates (2)
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2023 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/>.

// logic for handling sporadic jobs
//
// Currently sporadic jobs have priority over others.
// In particular, they can preempt jobs that
// - are in danger of missing their deadline
// - have done a lot of computing and haven't checkpointed
// - are from projects with a resource share debt
// At some point we should fix this.

// Apps can be
// regular: jobs compute when running
// sporadic: jobs run all the time but compute only part of the time
// non-CPU-intensive (NCI): jobs run all the time but don't compute
//
// Projects can have any or all of these, and this can change over time.
// A project is flagged as NCI if it has only NCI apps;
// in that case it's omitted from resource share calculations.

// Note: the client and app communicate via 1-way streams
// that are polled once/sec.
// This introduces potential uncertainty:
// if we send the app a message,
// once second later we don't know if it received the message and responded.
// To avoid this problem, when we send a message to an app
// we ignore its messages for the next 2.5 seconds.
// Perhaps a better approach would be to use sequence numbers and acks.
//
// states and transitions:
// CA_DONT_COMPUTE
//  computing is suspended, or insufficient resources
//  transitions:
//  to COULD_COMPUTE when these no longer hold
// CA_COULD_COMPUTE
//  not computing, but could
//  transitions:
//  to CA_DONT_COMPUTE if computing suspended or insufficient resources
//  to CA_COMPUTING if get AC_WANT_COMPUTE
// CA_COMPUTING
//  job can compute (and is, as far as we know)
//  transitions:
//  to CA_DONT_COMPUTE if computing suspended or insufficient resources
//  to CA_DONT_COMPUTE if get AC_DONT_WANT_COMPUTE or AC_NONE
//      (after timeout - see above)
//
// Interaction with the batch scheduler:
//  If we make a transition that changes resource usage,
//  request a reschedule to start/stop batch jobs
//  The batch scheduler subtracts resources used by sporadic jobs
// Coprocs:
//  If batch jobs are using GPUs, it may take them a few seconds to exit.
//  Sporadic jobs that use GPUs should delay for a few seconds at start,
//  and retry failed VRAM allocations.
//

#include "coproc.h"

#include "client_state.h"
#include "client_msgs.h"
#include "coproc_sched.h"
#include "result.h"
#include "app.h"

#define SPORADIC_MSG_DELAY  2.5

SPORADIC_RESOURCES sporadic_resources;

void SPORADIC_RESOURCES::print() {
    if (!ncpus_used) return;
    msg_printf(NULL, MSG_INFO, "Sporadic resources:");
    msg_printf(NULL, MSG_INFO, "   %f CPUs", ncpus_used);
    msg_printf(NULL, MSG_INFO, "   %f MB RAM", mem_used/MEGA);
    for (int i=1; i<sr_coprocs.n_rsc; i++) {
        COPROC& cp = sr_coprocs.coprocs[i];
        for (int j=0; j<cp.count; j++) {
            if (cp.usage[j] > 0) {
                msg_printf(NULL, MSG_INFO, "   GPU %s instance %d: %f\n",
                    cp.type, j, cp.usage[j]
                );
            }
        }
    }
}

// is computing suspended for this job?
//
static bool computing_suspended(ACTIVE_TASK *atp) {
    if (gstate.suspend_reason) return true;
    if (atp->result->uses_gpu() && gpu_suspend_reason) return true;
    return false;
}

// polling routine, called once/sec
void CLIENT_STATE::sporadic_poll() {
    sporadic_resources.init_poll();
    sporadic_resources.mem_max = available_ram();
    sporadic_resources.ncpus_max = n_usable_cpus;

    bool changed_active = false;
        // whether we need to reschedule regular jobs

    // find jobs that are active but shouldn't be
    // (CA_COMPUTING -> CA_NONE transitions)
    //
    for (ACTIVE_TASK *atp: active_tasks.active_tasks) {
        if (!atp->sporadic()) continue;
        if (atp->sporadic_ca_state != CA_COMPUTING) continue;

        // the job is in state CA_COMPUTING

        // see if the job needs to stop computing
        if (computing_suspended(atp)) {
            atp->sporadic_ca_state = CA_NONE;
            changed_active = true;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] preempting %s: computing suspended",
                    atp->result->name
                );
            }
        } else if (!sporadic_resources.enough(atp)) {
            // this could happen if user prefs change
            atp->sporadic_ca_state = CA_NONE;
            changed_active = true;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] preempting %s: insufficient resources",
                    atp->result->name
                );
            }
        } else if (atp->sporadic_ac_state != AC_WANT_COMPUTE) {
            if (now > atp->sporadic_ignore_until) {
                atp->sporadic_ca_state = CA_NONE;
                changed_active = true;
                if (log_flags.sporadic_debug) {
                    msg_printf(atp->result->project, MSG_INFO,
                        "[sporadic] %s: app is done computing",
                        atp->result->name
                    );
                }
            }
        }
        // the job can keep computing - reserve its resources
        if (atp->sporadic_ca_state == CA_COMPUTING) {
            sporadic_resources.reserve(atp);
        }
    }

    // activate jobs as needed
    // (CA_COULD_COMPUTE -> CA_COMPUTING transitions)
    //
    for (ACTIVE_TASK *atp: active_tasks.active_tasks) {
        if (!atp->sporadic()) continue;
        if (atp->sporadic_ca_state != CA_COULD_COMPUTE) continue;
        if (computing_suspended(atp)) {
            atp->sporadic_ca_state = CA_DONT_COMPUTE;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] %s can no longer compute: suspended",
                    atp->result->name
                );
            }
        } else if (!sporadic_resources.enough(atp)) {
            atp->sporadic_ca_state = CA_DONT_COMPUTE;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] %s can no longer compute: insufficient resources",
                    atp->result->name
                );
            }
        } else if (atp->sporadic_ac_state == AC_WANT_COMPUTE) {
            if (now > atp->sporadic_ignore_until) {
                atp->sporadic_ca_state = CA_COMPUTING;
                atp->sporadic_ignore_until = now + SPORADIC_MSG_DELAY;
                sporadic_resources.reserve(atp);
                changed_active = true;
                if (log_flags.sporadic_debug) {
                    msg_printf(atp->result->project, MSG_INFO,
                        "[sporadic] starting %s",
                        atp->result->name
                    );
                }
            }
        }
    }

    // assign states to initial, preempted, and done jobs
    //
    for (ACTIVE_TASK *atp: active_tasks.active_tasks) {
        if (!atp->sporadic()) continue;
        if (atp->sporadic_ca_state != CA_NONE) continue;
        if (computing_suspended(atp)) {
            atp->sporadic_ca_state = CA_DONT_COMPUTE;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] %s can't compute: suspended",
                    atp->result->name
                );
            }
        } else if (!sporadic_resources.enough(atp)) {
            atp->sporadic_ca_state = CA_DONT_COMPUTE;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] %s can't compute: insufficient resources",
                    atp->result->name
                );
            }
        } else {
            atp->sporadic_ca_state = CA_COULD_COMPUTE;
            if (log_flags.sporadic_debug) {
                msg_printf(atp->result->project, MSG_INFO,
                    "[sporadic] %s can compute",
                    atp->result->name
                );
            }
        }
    }

    if (changed_active) {
        request_schedule_cpus("sporadic apps changed state");
    }

    if (log_flags.sporadic_debug) {
        sporadic_resources.print();
    }
}

void CLIENT_STATE::sporadic_init() {
    sporadic_resources.init();
}