1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "thread_cpu_data.h"
16
17 #include <cstdlib>
18
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 constexpr int ADD_COUNT = 10;
25 }
ThreadCpuData(int magic,int pid,unsigned int threadCount)26 ThreadCpuData::ThreadCpuData(int magic, int pid, unsigned int threadCount): entry_(nullptr), current_(0)
27 {
28 Init(magic, threadCount + ADD_COUNT, pid);
29 }
30
Init(int magic,unsigned int totalCount,int pid)31 void ThreadCpuData::Init(int magic, unsigned int totalCount, int pid)
32 {
33 auto totalSize = sizeof(struct ucollection_thread_cpu_entry)
34 + sizeof(struct ucollection_thread_cpu_item) * totalCount;
35 entry_ = (struct ucollection_thread_cpu_entry *)malloc(totalSize);
36 if (entry_ == NULL) {
37 return;
38 }
39 memset_s(entry_, totalSize, 0, totalSize);
40 entry_->magic = magic;
41 entry_->total_count = totalCount;
42 entry_->cur_count = 0;
43 entry_->filter.pid = pid;
44 }
45
~ThreadCpuData()46 ThreadCpuData::~ThreadCpuData()
47 {
48 if (entry_ == NULL) {
49 return;
50 }
51 free(entry_);
52 entry_ = nullptr;
53 }
54
GetNextThread()55 struct ucollection_thread_cpu_item* ThreadCpuData::GetNextThread()
56 {
57 if (entry_ == NULL || current_ >= entry_->cur_count) {
58 return nullptr;
59 }
60
61 struct ucollection_thread_cpu_item *item = &entry_->datas[current_];
62 current_++;
63 return item;
64 }
65 } // HiviewDFX
66 } // OHOS