1 /*
2 * Copyright (C) 2021 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
16 #include <cstring>
17 #include <unistd.h>
18 #include <iostream>
19 #include <thread>
20 #include "hitrace_meter.h"
21
22 using namespace std;
23 namespace {
24 constexpr int SLEEP_ONE_SECOND = 1;
25 constexpr int SLEEP_TWO_SECOND = 2;
26 constexpr int CYCLE_TIMES = 5;
27 constexpr int32_t TASK_ID = 111;
28 constexpr uint64_t LABEL = HITRACE_TAG_OHOS;
29
FuncA()30 void FuncA()
31 {
32 cout << "funcA" << endl;
33 sleep(SLEEP_ONE_SECOND);
34 }
35
FuncB()36 void FuncB()
37 {
38 cout << "funcB" << endl;
39 sleep(SLEEP_TWO_SECOND);
40 }
41
FuncC()42 void FuncC()
43 {
44 cout << "funcC" << endl;
45 int num = 0;
46 for (int i = 0; i < CYCLE_TIMES; i++) {
47 CountTrace(HITRACE_TAG_OHOS, "count number", ++num);
48 sleep(SLEEP_ONE_SECOND);
49 }
50 }
51
ThreadFunc1()52 void ThreadFunc1()
53 {
54 StartAsyncTrace(LABEL, "testAsync", TASK_ID);
55 for (int i = 0; i < CYCLE_TIMES; ++i) {
56 cout << "t1" << endl;
57 sleep(SLEEP_ONE_SECOND);
58 }
59 }
60
ThreadFunc2()61 void ThreadFunc2()
62 {
63 for (int i = 0; i < CYCLE_TIMES; ++i) {
64 cout << "t2" << endl;
65 sleep(SLEEP_ONE_SECOND);
66 }
67 FinishAsyncTrace(LABEL, "testAsync", TASK_ID);
68 }
69 } // namespace
70
main()71 int main()
72 {
73 thread t1(ThreadFunc1);
74 t1.join();
75
76 StartTrace(LABEL, "testStart");
77 sleep(SLEEP_ONE_SECOND);
78
79 StartTrace(LABEL, "funcAStart", SLEEP_ONE_SECOND); // 打印起始点
80 FuncA();
81 FinishTrace(LABEL);
82 sleep(SLEEP_TWO_SECOND);
83
84 thread t2(ThreadFunc2);
85 t2.join();
86
87 StartTrace(LABEL, "funcBStart", SLEEP_TWO_SECOND);
88 FuncB();
89 FinishTrace(LABEL);
90 sleep(SLEEP_TWO_SECOND);
91
92 sleep(SLEEP_ONE_SECOND);
93 FinishTrace(LABEL);
94 FuncC();
95
96 return 0;
97 }
98