Coverage Report

Created: 2025-05-01 03:10

/root/doris/be/src/pipeline/task_scheduler.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <stddef.h>
21
22
#include <atomic>
23
#include <condition_variable>
24
#include <list>
25
#include <memory>
26
#include <mutex>
27
#include <utility>
28
#include <vector>
29
30
#include "common/status.h"
31
#include "gutil/ref_counted.h"
32
#include "pipeline_task.h"
33
#include "runtime/workload_group/workload_group.h"
34
#include "util/thread.h"
35
36
namespace doris {
37
class ExecEnv;
38
class ThreadPool;
39
40
namespace pipeline {
41
class TaskQueue;
42
} // namespace pipeline
43
} // namespace doris
44
45
namespace doris::pipeline {
46
47
class BlockedTaskScheduler {
48
public:
49
    explicit BlockedTaskScheduler(std::string name);
50
51
0
    ~BlockedTaskScheduler() = default;
52
53
    Status start();
54
    void shutdown();
55
    Status add_blocked_task(PipelineTask* task);
56
57
private:
58
    std::mutex _task_mutex;
59
    std::string _name;
60
    std::condition_variable _task_cond;
61
    std::list<PipelineTask*> _blocked_tasks;
62
63
    scoped_refptr<Thread> _thread;
64
    std::atomic<bool> _started;
65
    std::atomic<bool> _shutdown;
66
67
    static constexpr auto EMPTY_TIMES_TO_YIELD = 64;
68
69
    void _schedule();
70
    void _make_task_run(std::list<PipelineTask*>& local_tasks,
71
                        std::list<PipelineTask*>::iterator& task_itr,
72
                        PipelineTaskState state = PipelineTaskState::RUNNABLE);
73
};
74
75
class TaskScheduler {
76
public:
77
    TaskScheduler(ExecEnv* exec_env, std::shared_ptr<BlockedTaskScheduler> b_scheduler,
78
                  std::shared_ptr<TaskQueue> task_queue, std::string name,
79
                  std::shared_ptr<CgroupCpuCtl> cgroup_cpu_ctl)
80
            : _task_queue(std::move(task_queue)),
81
              _blocked_task_scheduler(std::move(b_scheduler)),
82
              _shutdown(false),
83
              _name(name),
84
0
              _cgroup_cpu_ctl(cgroup_cpu_ctl) {}
85
86
    ~TaskScheduler();
87
88
    Status schedule_task(PipelineTask* task);
89
90
    Status start();
91
92
    void stop();
93
94
0
    TaskQueue* task_queue() const { return _task_queue.get(); }
95
96
0
    std::vector<int> thread_debug_info() { return _fix_thread_pool->debug_info(); }
97
98
private:
99
    std::unique_ptr<ThreadPool> _fix_thread_pool;
100
    std::shared_ptr<TaskQueue> _task_queue;
101
    std::vector<std::unique_ptr<std::atomic<bool>>> _markers;
102
    std::shared_ptr<BlockedTaskScheduler> _blocked_task_scheduler;
103
    std::atomic<bool> _shutdown;
104
    std::string _name;
105
    std::weak_ptr<CgroupCpuCtl> _cgroup_cpu_ctl;
106
107
    void _do_work(size_t index);
108
};
109
} // namespace doris::pipeline