1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <trace/trace_marker.h>
17570af302Sopenharmony_ci
18570af302Sopenharmony_ci#include <fcntl.h>
19570af302Sopenharmony_ci#include <stdbool.h>
20570af302Sopenharmony_ci#include <stdio.h>
21570af302Sopenharmony_ci#include <stdlib.h>
22570af302Sopenharmony_ci#include <test.h>
23570af302Sopenharmony_ci#include <unistd.h>
24570af302Sopenharmony_ci
25570af302Sopenharmony_ci#define BUFFER_LEN 1000000
26570af302Sopenharmony_cistatic const int count = 10;
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci/**
29570af302Sopenharmony_ci * @tc.name      : trace_marker
30570af302Sopenharmony_ci * @tc.desc      : The stress test of trace_marker.
31570af302Sopenharmony_ci * @tc.level     : Level 0
32570af302Sopenharmony_ci */
33570af302Sopenharmony_cistatic void trace_marker_stresstest_0010(void)
34570af302Sopenharmony_ci{
35570af302Sopenharmony_ci    int pidChild = 0;
36570af302Sopenharmony_ci    int pidCParent = 0;
37570af302Sopenharmony_ci    int pidCChild = 0;
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci    pid_t fpid;
40570af302Sopenharmony_ci    fpid = fork();
41570af302Sopenharmony_ci    if (fpid < 0) {
42570af302Sopenharmony_ci        printf("error in fork! \n");
43570af302Sopenharmony_ci    } else if (fpid == 0) {
44570af302Sopenharmony_ci        int pidChild = getpid();
45570af302Sopenharmony_ci        int traceCount = 0;
46570af302Sopenharmony_ci        bool trace_async_sucess = false;
47570af302Sopenharmony_ci        char buf[BUFFER_LEN] = {0};
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci        while (traceCount <= 5000) {
50570af302Sopenharmony_ci            snprintf(buf, BUFFER_LEN, "%d", traceCount);
51570af302Sopenharmony_ci            system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
52570af302Sopenharmony_ci            trace_marker_async_begin(HITRACE_TAG_MUSL, "Trace_Marker_Async_Begin", buf, 1);
53570af302Sopenharmony_ci            trace_marker_async_end(HITRACE_TAG_MUSL, "Trace_Marker_Async_End", buf, 1);
54570af302Sopenharmony_ci            system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
55570af302Sopenharmony_ci            printf("trace_marker_async_begin has been running times is:%d\n", traceCount);
56570af302Sopenharmony_ci            traceCount++;
57570af302Sopenharmony_ci        }
58570af302Sopenharmony_ci        exit(pidChild);
59570af302Sopenharmony_ci    } else {
60570af302Sopenharmony_ci        pid_t pidCChild;
61570af302Sopenharmony_ci        int traceCount = 0;
62570af302Sopenharmony_ci        char buf[BUFFER_LEN] = {0};
63570af302Sopenharmony_ci
64570af302Sopenharmony_ci        // start process again
65570af302Sopenharmony_ci        pidCChild = fork();
66570af302Sopenharmony_ci        if (pidCChild < 0) {
67570af302Sopenharmony_ci            t_printf("error in fork!");
68570af302Sopenharmony_ci        } else if (pidCChild == 0) {
69570af302Sopenharmony_ci            while (traceCount <= 5000) {
70570af302Sopenharmony_ci                snprintf(buf, BUFFER_LEN, "%d", traceCount);
71570af302Sopenharmony_ci                system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
72570af302Sopenharmony_ci                trace_marker_begin(HITRACE_TAG_MUSL, "Trace_Marker", buf);
73570af302Sopenharmony_ci                trace_marker_end(HITRACE_TAG_MUSL);
74570af302Sopenharmony_ci                system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
75570af302Sopenharmony_ci                printf("trace_marker_begin has been running times is:%d\n", traceCount);
76570af302Sopenharmony_ci                traceCount++;
77570af302Sopenharmony_ci            }
78570af302Sopenharmony_ci            exit(pidCChild);
79570af302Sopenharmony_ci        } else {
80570af302Sopenharmony_ci            pidCParent = getpid();
81570af302Sopenharmony_ci            int traceCount = 0;
82570af302Sopenharmony_ci
83570af302Sopenharmony_ci            while (traceCount <= 5000) {
84570af302Sopenharmony_ci                system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
85570af302Sopenharmony_ci                trace_marker_count(HITRACE_TAG_MUSL, "traceCount", traceCount);
86570af302Sopenharmony_ci                system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
87570af302Sopenharmony_ci                printf("trace_marker_count has been running times is:%d\n", traceCount);
88570af302Sopenharmony_ci                traceCount++;
89570af302Sopenharmony_ci            }
90570af302Sopenharmony_ci            exit(pidCParent);
91570af302Sopenharmony_ci        }
92570af302Sopenharmony_ci    }
93570af302Sopenharmony_ci}
94570af302Sopenharmony_ci
95570af302Sopenharmony_ciint main(void)
96570af302Sopenharmony_ci{
97570af302Sopenharmony_ci    trace_marker_stresstest_0010();
98570af302Sopenharmony_ci
99570af302Sopenharmony_ci    return t_status;
100570af302Sopenharmony_ci}
101