106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "profiler_data_repeater.h"
1606f6ba60Sopenharmony_ci
1706f6ba60Sopenharmony_ciProfilerDataRepeater::ProfilerDataRepeater(size_t maxSize)
1806f6ba60Sopenharmony_ci{
1906f6ba60Sopenharmony_ci    maxSize_ = maxSize;
2006f6ba60Sopenharmony_ci    closed_ = false;
2106f6ba60Sopenharmony_ci}
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_ciProfilerDataRepeater::~ProfilerDataRepeater()
2406f6ba60Sopenharmony_ci{
2506f6ba60Sopenharmony_ci    Close();
2606f6ba60Sopenharmony_ci}
2706f6ba60Sopenharmony_ci
2806f6ba60Sopenharmony_cisize_t ProfilerDataRepeater::Size()
2906f6ba60Sopenharmony_ci{
3006f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
3106f6ba60Sopenharmony_ci    return dataQueue_.size();
3206f6ba60Sopenharmony_ci}
3306f6ba60Sopenharmony_ci
3406f6ba60Sopenharmony_civoid ProfilerDataRepeater::Reset()
3506f6ba60Sopenharmony_ci{
3606f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
3706f6ba60Sopenharmony_ci    closed_ = false;
3806f6ba60Sopenharmony_ci}
3906f6ba60Sopenharmony_ci
4006f6ba60Sopenharmony_civoid ProfilerDataRepeater::Close()
4106f6ba60Sopenharmony_ci{
4206f6ba60Sopenharmony_ci    {
4306f6ba60Sopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
4406f6ba60Sopenharmony_ci        dataQueue_.clear();
4506f6ba60Sopenharmony_ci        closed_ = true;
4606f6ba60Sopenharmony_ci    }
4706f6ba60Sopenharmony_ci    slotCondVar_.notify_all();
4806f6ba60Sopenharmony_ci    itemCondVar_.notify_all();
4906f6ba60Sopenharmony_ci}
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_cibool ProfilerDataRepeater::PutPluginData(const ProfilerPluginDataPtr& pluginData)
5206f6ba60Sopenharmony_ci{
5306f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
5406f6ba60Sopenharmony_ci
5506f6ba60Sopenharmony_ci    if ((pluginData == nullptr) && (dataQueue_.size() > 0)) {
5606f6ba60Sopenharmony_ci        PROFILER_LOG_INFO(LOG_CORE, "no need put nullptr if queue has data, dataQueue_.size() = %zu",
5706f6ba60Sopenharmony_ci                          dataQueue_.size());
5806f6ba60Sopenharmony_ci        return true;
5906f6ba60Sopenharmony_ci    }
6006f6ba60Sopenharmony_ci
6106f6ba60Sopenharmony_ci    while (dataQueue_.size() >= maxSize_ && !closed_) {
6206f6ba60Sopenharmony_ci        slotCondVar_.wait(lock);
6306f6ba60Sopenharmony_ci    }
6406f6ba60Sopenharmony_ci    if (closed_) {
6506f6ba60Sopenharmony_ci        return false;
6606f6ba60Sopenharmony_ci    }
6706f6ba60Sopenharmony_ci
6806f6ba60Sopenharmony_ci    dataQueue_.push_back(pluginData);
6906f6ba60Sopenharmony_ci    lock.unlock();
7006f6ba60Sopenharmony_ci
7106f6ba60Sopenharmony_ci    itemCondVar_.notify_one();
7206f6ba60Sopenharmony_ci    return true;
7306f6ba60Sopenharmony_ci}
7406f6ba60Sopenharmony_ci
7506f6ba60Sopenharmony_ciProfilerPluginDataPtr ProfilerDataRepeater::TakePluginData()
7606f6ba60Sopenharmony_ci{
7706f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
7806f6ba60Sopenharmony_ci    while (dataQueue_.empty() && !closed_) {
7906f6ba60Sopenharmony_ci        itemCondVar_.wait(lock);
8006f6ba60Sopenharmony_ci    }
8106f6ba60Sopenharmony_ci    if (closed_) {
8206f6ba60Sopenharmony_ci        return nullptr;
8306f6ba60Sopenharmony_ci    }
8406f6ba60Sopenharmony_ci
8506f6ba60Sopenharmony_ci    auto result = dataQueue_.front();
8606f6ba60Sopenharmony_ci    dataQueue_.pop_front();
8706f6ba60Sopenharmony_ci    lock.unlock();
8806f6ba60Sopenharmony_ci
8906f6ba60Sopenharmony_ci    slotCondVar_.notify_one();
9006f6ba60Sopenharmony_ci    return result;
9106f6ba60Sopenharmony_ci}
9206f6ba60Sopenharmony_ci
9306f6ba60Sopenharmony_ciint ProfilerDataRepeater::TakePluginData(std::vector<ProfilerPluginDataPtr>& pluginDataVec)
9406f6ba60Sopenharmony_ci{
9506f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
9606f6ba60Sopenharmony_ci    while (dataQueue_.empty() && !closed_) {
9706f6ba60Sopenharmony_ci        itemCondVar_.wait(lock);
9806f6ba60Sopenharmony_ci    }
9906f6ba60Sopenharmony_ci    if (closed_) {
10006f6ba60Sopenharmony_ci        return -1;
10106f6ba60Sopenharmony_ci    }
10206f6ba60Sopenharmony_ci
10306f6ba60Sopenharmony_ci    int count = 0;
10406f6ba60Sopenharmony_ci    while (dataQueue_.size() > 0) {
10506f6ba60Sopenharmony_ci        auto result = dataQueue_.front();
10606f6ba60Sopenharmony_ci        pluginDataVec.push_back(result);
10706f6ba60Sopenharmony_ci        dataQueue_.pop_front();
10806f6ba60Sopenharmony_ci        count++;
10906f6ba60Sopenharmony_ci    }
11006f6ba60Sopenharmony_ci    lock.unlock();
11106f6ba60Sopenharmony_ci
11206f6ba60Sopenharmony_ci    slotCondVar_.notify_one();
11306f6ba60Sopenharmony_ci    return count;
11406f6ba60Sopenharmony_ci}
11506f6ba60Sopenharmony_ci
11606f6ba60Sopenharmony_civoid ProfilerDataRepeater::ClearQueue()
11706f6ba60Sopenharmony_ci{
11806f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
11906f6ba60Sopenharmony_ci    dataQueue_.clear();
12006f6ba60Sopenharmony_ci}
121