1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci// utils for multi-thread and multi-process test
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ci#include "mt_utils.h"
19f6603c60Sopenharmony_ci#include <pthread.h>
20f6603c60Sopenharmony_ci#include <unistd.h>
21f6603c60Sopenharmony_ci#include <stdlib.h>
22f6603c60Sopenharmony_ci#include <errno.h>
23f6603c60Sopenharmony_ci#include <sys/types.h>
24f6603c60Sopenharmony_ci#include <sys/shm.h>
25f6603c60Sopenharmony_ci#include "log.h"
26f6603c60Sopenharmony_ci
27f6603c60Sopenharmony_ci#define MAX_INDEX 12
28f6603c60Sopenharmony_ciconst uint32_t MSECOND_INDEX[MAX_INDEX] = {10, 20, 30, 60, 100, 150, 200, 300, 400, 600, 1000, 2000};
29f6603c60Sopenharmony_ciconst uint32_t NUMBER_INDEX[MAX_INDEX]  = {2500, 3600, 4500, 6600, 8800, 1000, 12000, 15000, 18000, 2000, 3000, 42000 };
30f6603c60Sopenharmony_ci
31f6603c60Sopenharmony_ciint g_shmidCheckStep = 0;
32f6603c60Sopenharmony_ciuint64_t CheckStep(int value)
33f6603c60Sopenharmony_ci{
34f6603c60Sopenharmony_ci    if (value == 1) {
35f6603c60Sopenharmony_ci        shmctl(g_shmidCheckStep, IPC_RMID, nullptr);
36f6603c60Sopenharmony_ci        g_shmidCheckStep = shmget(IPC_PRIVATE, 1024, 0666 | IPC_CREAT);
37f6603c60Sopenharmony_ci    }
38f6603c60Sopenharmony_ci    if (g_shmidCheckStep != -1) {
39f6603c60Sopenharmony_ci
40f6603c60Sopenharmony_ci        uint64_t *shared = (uint64_t *)shmat(g_shmidCheckStep, nullptr, 0);
41f6603c60Sopenharmony_ci
42f6603c60Sopenharmony_ci        if (value == 1) {
43f6603c60Sopenharmony_ci            *shared = 1;
44f6603c60Sopenharmony_ci        } else {
45f6603c60Sopenharmony_ci            *shared = (*shared << 4) + value;
46f6603c60Sopenharmony_ci        }
47f6603c60Sopenharmony_ci        uint64_t state = *shared;
48f6603c60Sopenharmony_ci        shmdt(shared);
49f6603c60Sopenharmony_ci        return state;
50f6603c60Sopenharmony_ci    }
51f6603c60Sopenharmony_ci    return 0;
52f6603c60Sopenharmony_ci}
53f6603c60Sopenharmony_ci
54f6603c60Sopenharmony_ciint CountPrimes(uint32_t maxNumber)
55f6603c60Sopenharmony_ci{
56f6603c60Sopenharmony_ci    uint32_t primesCount = 0;
57f6603c60Sopenharmony_ci    uint32_t i, num;
58f6603c60Sopenharmony_ci    for (num = 2; num <= maxNumber; ++num) {
59f6603c60Sopenharmony_ci        for (i = 2; i <= num; i++) {
60f6603c60Sopenharmony_ci            if (num % i == 0) {
61f6603c60Sopenharmony_ci                break;
62f6603c60Sopenharmony_ci            }
63f6603c60Sopenharmony_ci        }
64f6603c60Sopenharmony_ci        if (i == num) {
65f6603c60Sopenharmony_ci            primesCount++;
66f6603c60Sopenharmony_ci        }
67f6603c60Sopenharmony_ci    }
68f6603c60Sopenharmony_ci    return primesCount;
69f6603c60Sopenharmony_ci}
70f6603c60Sopenharmony_ci
71f6603c60Sopenharmony_cistatic uint32_t GetNumberFromMS(uint32_t ms)
72f6603c60Sopenharmony_ci{
73f6603c60Sopenharmony_ci    for (uint32_t i = 0; i < MAX_INDEX; i++) {
74f6603c60Sopenharmony_ci        if (MSECOND_INDEX[i] == ms) {
75f6603c60Sopenharmony_ci            return NUMBER_INDEX[i];
76f6603c60Sopenharmony_ci        }
77f6603c60Sopenharmony_ci    }
78f6603c60Sopenharmony_ci    return 0;
79f6603c60Sopenharmony_ci}
80f6603c60Sopenharmony_ci
81f6603c60Sopenharmony_ciint BusyRun(uint32_t ms)
82f6603c60Sopenharmony_ci{
83f6603c60Sopenharmony_ci    uint32_t n = GetNumberFromMS(ms);
84f6603c60Sopenharmony_ci    if (n == 0) {
85f6603c60Sopenharmony_ci        LOG("BusyRun Error: %d ms not support", n);
86f6603c60Sopenharmony_ci        _exit(1);
87f6603c60Sopenharmony_ci    }
88f6603c60Sopenharmony_ci    return CountPrimes(n);
89f6603c60Sopenharmony_ci}
90f6603c60Sopenharmony_ci
91f6603c60Sopenharmony_ci// pipe for sync in ipc
92f6603c60Sopenharmony_cistatic int g_pipeFd[2];
93f6603c60Sopenharmony_ciint InitPipe()
94f6603c60Sopenharmony_ci{
95f6603c60Sopenharmony_ci    return pipe(g_pipeFd);
96f6603c60Sopenharmony_ci}
97f6603c60Sopenharmony_civoid UnBlockPipe()
98f6603c60Sopenharmony_ci{
99f6603c60Sopenharmony_ci    close(g_pipeFd[0]);
100f6603c60Sopenharmony_ci    close(g_pipeFd[1]);
101f6603c60Sopenharmony_ci}
102f6603c60Sopenharmony_civoid BlockOnPipe()
103f6603c60Sopenharmony_ci{
104f6603c60Sopenharmony_ci    close(g_pipeFd[1]);
105f6603c60Sopenharmony_ci    char buffer[2];
106f6603c60Sopenharmony_ci    LOGD("before pipe read");
107f6603c60Sopenharmony_ci    read(g_pipeFd[0], buffer, 1); // will block until close(fd[1]) in another port
108f6603c60Sopenharmony_ci    LOGD("after pipe read");
109f6603c60Sopenharmony_ci    close(g_pipeFd[0]);
110f6603c60Sopenharmony_ci}
111f6603c60Sopenharmony_ci
112f6603c60Sopenharmony_ci// shm for process
113f6603c60Sopenharmony_ciint g_shmidGlobal;
114f6603c60Sopenharmony_ciint InitGlobalVariable(void)
115f6603c60Sopenharmony_ci{
116f6603c60Sopenharmony_ci    g_shmidGlobal = shmget(IPC_PRIVATE, 1024, 0666 | IPC_CREAT);
117f6603c60Sopenharmony_ci    if (g_shmidGlobal == -1) {
118f6603c60Sopenharmony_ci        LOG("> shmget errno = %d", errno);
119f6603c60Sopenharmony_ci        return -1;
120f6603c60Sopenharmony_ci    }
121f6603c60Sopenharmony_ci    return 0;
122f6603c60Sopenharmony_ci}
123f6603c60Sopenharmony_ci
124f6603c60Sopenharmony_ciint SetGlobalVariable(int value)
125f6603c60Sopenharmony_ci{
126f6603c60Sopenharmony_ci    int *shared = (int *)shmat(g_shmidGlobal, nullptr, 0);
127f6603c60Sopenharmony_ci    if (shared == (int *)(-1)) {
128f6603c60Sopenharmony_ci        LOG("> shmat errno = %d", errno);
129f6603c60Sopenharmony_ci        return -1;
130f6603c60Sopenharmony_ci    }
131f6603c60Sopenharmony_ci    *shared = value;
132f6603c60Sopenharmony_ci    if ((shmdt(shared)) == -1) {
133f6603c60Sopenharmony_ci        LOG("> shmdt errno = %d", errno);
134f6603c60Sopenharmony_ci        return -1;
135f6603c60Sopenharmony_ci    }
136f6603c60Sopenharmony_ci    return 0;
137f6603c60Sopenharmony_ci}
138f6603c60Sopenharmony_ci
139f6603c60Sopenharmony_ciint GetGlobalVariable(void)
140f6603c60Sopenharmony_ci{
141f6603c60Sopenharmony_ci    int re;
142f6603c60Sopenharmony_ci    int *shared = (int *)shmat(g_shmidGlobal, nullptr, 0);
143f6603c60Sopenharmony_ci    if (shared == (int*)(-1)) {
144f6603c60Sopenharmony_ci        LOG("> shmat errno = %d", errno);
145f6603c60Sopenharmony_ci        return -1;
146f6603c60Sopenharmony_ci    }
147f6603c60Sopenharmony_ci    re = *shared;
148f6603c60Sopenharmony_ci    if ((shmdt(shared)) == -1) {
149f6603c60Sopenharmony_ci        LOG("> shmdt errno = %d", errno);
150f6603c60Sopenharmony_ci        return -1;
151f6603c60Sopenharmony_ci    }
152f6603c60Sopenharmony_ci    return re;
153f6603c60Sopenharmony_ci}
154f6603c60Sopenharmony_ci
155f6603c60Sopenharmony_ciint DeleteGlobalVariable(void)
156f6603c60Sopenharmony_ci{
157f6603c60Sopenharmony_ci    if (shmctl(g_shmidGlobal, IPC_RMID, nullptr) == -1) {
158f6603c60Sopenharmony_ci        LOG("> shmctl errno = %d", errno);
159f6603c60Sopenharmony_ci        return -1;
160f6603c60Sopenharmony_ci    }
161f6603c60Sopenharmony_ci    return 1;
162f6603c60Sopenharmony_ci}
163