param_test_cmds.c 12.4 KB
Newer Older
X
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <fcntl.h>
M
Mupceet 已提交
16
#include <pthread.h>
X
xionglei6 已提交
17
#include <string.h>
M
Mupceet 已提交
18
#include <sys/time.h>
X
xionglei6 已提交
19 20

#include "begetctl.h"
M
Mupceet 已提交
21
#include "init_param.h"
C
watcher  
cheng_jinsong 已提交
22
#include "init_utils.h"
X
xionglei6 已提交
23
#include "loop_event.h"
M
Mupceet 已提交
24
#include "parameter.h"
X
xionglei6 已提交
25
#include "plugin_test.h"
X
xionglei6 已提交
26
#include "service_watcher.h"
C
watcher  
cheng_jinsong 已提交
27
#include "parameter.h"
X
xionglei6 已提交
28

C
watcher  
cheng_jinsong 已提交
29 30
#define MAX_THREAD_NUMBER 100
#define MAX_NUMBER 10
X
xionglei6 已提交
31
#define READ_DURATION 100000
C
watcher  
cheng_jinsong 已提交
32 33
#define CMD_INDEX 2

X
xionglei6 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
static char *GetLocalBuffer(uint32_t *buffSize)
{
    static char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX] = {0};
    if (buffSize != NULL) {
        *buffSize = sizeof(buffer);
    }
    return buffer;
}

int g_stop = 0;
static void *CmdReader(void *args)
{
    (void)srand((unsigned)time(NULL));
    uint32_t buffSize = 0;
    char *buffer = GetLocalBuffer(&buffSize);
    while (g_stop == 0) {
M
Mupceet 已提交
50
        int wait = READ_DURATION + READ_DURATION;  // 100ms rand
X
xionglei6 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        uint32_t size = buffSize;
        int ret = SystemGetParameter("test.randrom.read", buffer, &size);
        if (ret == 0) {
            printf("SystemGetParameter value %s %d \n", buffer, wait);
        } else {
            printf("SystemGetParameter fail %d \n", wait);
        }
        usleep(wait);
    }
    return NULL;
}

static int32_t BShellParamCmdRead(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    static pthread_t thread = 0;
    PLUGIN_LOGV("BShellParamCmdWatch %s, threadId %d", argv[1], thread);
    if (strcmp(argv[1], "start") == 0) {
        if (thread != 0) {
            return 0;
        }
        SystemSetParameter("test.randrom.read.start", "1");
        pthread_create(&thread, NULL, CmdReader, argv[1]);
    } else if (strcmp(argv[1], "stop") == 0) {
        if (thread == 0) {
            return 0;
        }
        SystemSetParameter("test.randrom.read.start", "0");
        g_stop = 1;
        pthread_join(thread, NULL);
        thread = 0;
    }
    return 0;
}

C
watcher  
cheng_jinsong 已提交
86 87 88 89 90 91
typedef struct {
    char name[PARAM_NAME_LEN_MAX];
    pthread_t thread;
} TestWatchContext;

static void HandleParamChange2(const char *key, const char *value, void *context)
X
xionglei6 已提交
92 93
{
    PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
X
xionglei6 已提交
94
    long long commit = GetSystemCommitId();
C
watcher  
cheng_jinsong 已提交
95
    size_t index = (size_t)context;
C
codex  
chengjinsong 已提交
96
    printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
C
watcher  
cheng_jinsong 已提交
97 98 99 100 101 102
    static int addWatcher = 0;
    int ret = 0;
    if ((index == 4) && !addWatcher) { // 4 when context == 4 add
        index = 5; // 5 add context
        ret = SystemWatchParameter(key, HandleParamChange2, (void *)index);
        if (ret != 0) {
103
            printf("Add watcher %s fail %zu \n", key, index);
C
watcher  
cheng_jinsong 已提交
104 105 106 107 108 109 110 111
        }
        addWatcher = 1;
        return;
    }
    if (index == 2) { // 2 when context == 2 delete 3
        index = 3; // 3 delete context
        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
        if (ret != 0) {
112
            printf("Remove watcher fail %zu  \n", index);
C
watcher  
cheng_jinsong 已提交
113 114 115 116 117 118
        }
        return;
    }
    if (index == 1) { // 1 when context == 1 delete 1
        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
        if (ret != 0) {
119
            printf("Remove watcher fail %zu  \n", index);
C
watcher  
cheng_jinsong 已提交
120 121 122 123 124 125
        }
        return;
    }
    if ((index == 5) && (addWatcher == 1)) {  // 5 when context == 5 delete 5
        RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
        if (ret != 0) {
126
            printf("Remove watcher fail %zu  \n", index);
C
watcher  
cheng_jinsong 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        }
        addWatcher = 0;
    }
}

static void HandleParamChange1(const char *key, const char *value, void *context)
{
    PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
    long long commit = GetSystemCommitId();
    size_t index = (size_t)context;
    printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
}

static void *CmdThreadWatcher(void *args)
{
    TestWatchContext *context = (TestWatchContext *)args;
    for (size_t i = 1; i <= MAX_NUMBER; i++) {
        int ret = SystemWatchParameter(context->name, HandleParamChange2, (void *)i);
        if (ret != 0) {
146
            printf("Add watcher %s fail %zu \n", context->name, i);
C
watcher  
cheng_jinsong 已提交
147 148 149
        }
        ret = SetParameter(context->name, context->name);
        if (ret != 0) {
150
            printf("Set parameter %s fail %zu \n", context->name, i);
C
watcher  
cheng_jinsong 已提交
151 152 153 154 155 156
        }
    }
    sleep(1);
    for (size_t i = 1; i <= MAX_NUMBER; i++) {
        int ret = RemoveParameterWatcher(context->name, HandleParamChange2, (void *)i);
        if (ret != 0) {
157
            printf("Remove watcher %s fail %zu \n", context->name, i);
C
watcher  
cheng_jinsong 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        }
    }
    free(context);
    return NULL;
}

static void StartWatcherInThread(const char *prefix)
{
    TestWatchContext *context[MAX_THREAD_NUMBER] = { NULL };
    for (size_t i = 0; i < MAX_THREAD_NUMBER; i++) {
        context[i] = calloc(1, sizeof(TestWatchContext));
        PLUGIN_CHECK(context[i] != NULL, return, "Failed to alloc context");
        int len = sprintf_s(context[i]->name, sizeof(context[i]->name), "%s.%zu", prefix, i);
        if (len > 0) {
            printf("Add watcher %s \n", context[i]->name);
            pthread_create(&context[i]->thread, NULL, CmdThreadWatcher, context[i]);
        }
    }
}

static void StartWatcher(const char *prefix)
{
    static size_t index = 0;
    int ret = SystemWatchParameter(prefix, HandleParamChange1, (void *)index);
    if (ret != 0) {
        printf("Add watcher %s fail \n", prefix);
        return;
    }
    index++;
X
xionglei6 已提交
187 188 189 190 191 192
}

static int32_t BShellParamCmdWatch(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGV("BShellParamCmdWatch %s", argv[1]);
C
watcher  
cheng_jinsong 已提交
193 194 195
    StartWatcher(argv[1]);

    if (argc <= CMD_INDEX) {
C
cheng_jinsong 已提交
196 197
        return 0;
    }
C
watcher  
cheng_jinsong 已提交
198 199 200 201 202 203 204
    if (strcmp(argv[CMD_INDEX], "thread") == 0) { // 2 cmd index
        StartWatcherInThread(argv[1]);
        return 0;
    }

    int maxCount = StringToInt(argv[CMD_INDEX], -1); // 2 cmd index
    if (maxCount <= 0 || maxCount > 65535) { // 65535 max count
C
codex  
chengjinsong 已提交
205
        PLUGIN_LOGE("Invalid input %s", argv[CMD_INDEX]);
C
watcher  
cheng_jinsong 已提交
206 207 208 209 210 211
        return 0;
    }
    uint32_t buffSize = 0;
    char *buffer = GetLocalBuffer(&buffSize);
    size_t count = 0;
    while (count < (size_t)maxCount) { // 100 max count
C
codex  
chengjinsong 已提交
212
        int len = sprintf_s(buffer, buffSize, "%s.%zu", argv[1], count);
C
watcher  
cheng_jinsong 已提交
213 214 215 216 217
        PLUGIN_CHECK(len > 0, return -1, "Invalid buffer");
        buffer[len] = '\0';
        StartWatcher(buffer);
        count++;
    }
X
xionglei6 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    return 0;
}

static int32_t BShellParamCmdInstall(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGV("BShellParamCmdInstall %s %s", argv[0], argv[1]);
    uint32_t buffSize = 0;
    char *buffer = GetLocalBuffer(&buffSize);
    int ret = sprintf_s(buffer, buffSize, "ohos.servicectrl.%s", argv[0]);
    PLUGIN_CHECK(ret > 0, return -1, "Invalid buffer");
    buffer[ret] = '\0';
    SystemSetParameter(buffer, argv[1]);
    return 0;
}

X
xionglei6 已提交
234
static int32_t BShellParamCmdDisplay(BShellHandle shell, int32_t argc, char *argv[])
X
xionglei6 已提交
235
{
X
xionglei6 已提交
236 237 238 239 240 241 242 243
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGV("BShellParamCmdDisplay %s %s", argv[0], argv[1]);
    SystemSetParameter("ohos.servicectrl.display", argv[1]);
    return 0;
}

void ServiceStatusChangeTest(const char *key, ServiceStatus status)
{
X
xionglei6 已提交
244
    PLUGIN_LOGI("group-test-stage3: wait service %s status: %d", key, status);
X
xionglei6 已提交
245 246 247 248 249 250 251 252 253
    if (status == SERVICE_READY || status == SERVICE_STARTED) {
        PLUGIN_LOGI("Service %s start work", key);
    }
}

static int32_t BShellParamCmdGroupTest(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGI("BShellParamCmdGroupTest %s stage: %s", argv[0], argv[1]);
M
Mupceet 已提交
254 255 256
    if (argc > 2 && strcmp(argv[1], "wait") == 0) {                  // 2 service name index
        PLUGIN_LOGI("group-test-stage3: wait service %s", argv[2]);  // 2 service name index
        ServiceWatchForStatus(argv[2], ServiceStatusChangeTest);     // 2 service name index
X
xionglei6 已提交
257
        LE_RunLoop(LE_GetDefaultLoop());
258
        LE_CloseLoop(LE_GetDefaultLoop());
X
xionglei6 已提交
259
    }
X
xionglei6 已提交
260 261 262
    return 0;
}

263 264 265 266
static int32_t BShellParamCmdUdidGet(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGI("BShellParamCmdUdidGet ");
M
Mupceet 已提交
267 268
    char localDeviceId[65] = {0};      // 65 udid len
    AclGetDevUdid(localDeviceId, 65);  // 65 udid len
269 270 271 272
    BShellEnvOutput(shell, "    udid: %s\r\n", localDeviceId);
    return 0;
}

C
watcher  
cheng_jinsong 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static int CalcValue(const char *value)
{
    char *begin = (char *)value;
    while (*begin != '\0') {
        if (*begin == ' ') {
            begin++;
        } else {
            break;
        }
    }
    char *end = begin + strlen(begin);
    while (end > begin) {
        if (*end > '9' || *end < '0') {
            *end = '\0';
        }
        end--;
    }
    return StringToInt(begin, -1);
}

static int32_t BShellParamCmdMemGet(BShellHandle shell, int32_t argc, char *argv[])
{
    PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
    uint32_t buffSize = 0;  // 1024 max buffer for decode
    char *buff = GetLocalBuffer(&buffSize);
C
codex  
chengjinsong 已提交
298
    PLUGIN_CHECK(buff != NULL, return -1, "Failed to get local buffer");
C
watcher  
cheng_jinsong 已提交
299 300 301
    int ret = sprintf_s(buff, buffSize - 1, "/proc/%s/smaps", argv[1]);
    PLUGIN_CHECK(ret > 0, return -1, "Failed to format path %s", argv[1]);
    buff[ret] = '\0';
C
codex  
chengjinsong 已提交
302 303
    char *realPath = GetRealPath(buff);
    PLUGIN_CHECK(realPath != NULL, return -1, "Failed to get real path");
C
watcher  
cheng_jinsong 已提交
304 305 306 307
    int all = 0;
    if (argc > 2 && strcmp(argv[2], "all") == 0) {  // 2 2 max arg
        all = 1;
    }
C
codex  
chengjinsong 已提交
308 309
    FILE *fp = fopen(realPath, "r");
    free(realPath);
C
watcher  
cheng_jinsong 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    int value = 0;
    while (fp != NULL && buff != NULL && fgets(buff, buffSize, fp) != NULL) {
        buff[buffSize - 1] = '\0';
        if (strncmp(buff, "Pss:", strlen("Pss:")) == 0) {
            int v = CalcValue(buff + strlen("Pss:"));
            if (all) {
                printf("Pss:     %d kb\n", v);
            }
            value += v;
        } else if (strncmp(buff, "SwapPss:", strlen("SwapPss:")) == 0) {
            int v = CalcValue(buff + strlen("SwapPss:"));
            if (all) {
                printf("SwapPss: %d kb\n", v);
            }
            value += v;
        }
    }
    if (fp != NULL) {
        fclose(fp);
        printf("Total mem %d kb\n", value);
    } else {
        printf("Failed to get memory for %s %s \n", argv[1], buff);
    }
    return 0;
}

X
xionglei6 已提交
336 337 338
int32_t BShellCmdRegister(BShellHandle shell, int execMode)
{
    if (execMode == 0) {
M
Mupceet 已提交
339
        const CmdInfo infos[] = {
X
xionglei6 已提交
340
            {"init", BShellParamCmdGroupTest, "init group test", "init group test [stage]", "init group test"},
C
watcher  
cheng_jinsong 已提交
341
            {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
X
xionglei6 已提交
342 343
        };
        for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
C
cheng_jinsong 已提交
344
            BShellEnvRegisterCmd(shell, &infos[i]);
X
xionglei6 已提交
345 346
        }
    } else {
M
Mupceet 已提交
347
        const CmdInfo infos[] = {
X
xionglei6 已提交
348 349 350 351 352 353
            {"display", BShellParamCmdDisplay, "display system service", "display service", "display service"},
            {"read", BShellParamCmdRead, "read system parameter", "read [start | stop]", ""},
            {"watcher", BShellParamCmdWatch, "watcher system parameter", "watcher [name]", ""},
            {"install", BShellParamCmdInstall, "install plugin", "install [name]", ""},
            {"uninstall", BShellParamCmdInstall, "uninstall plugin", "uninstall [name]", ""},
            {"group", BShellParamCmdGroupTest, "group test", "group test [stage]", "group test"},
354
            {"display", BShellParamCmdUdidGet, "display udid", "display udid", "display udid"},
C
watcher  
cheng_jinsong 已提交
355
            {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
X
xionglei6 已提交
356 357
        };
        for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
C
cheng_jinsong 已提交
358
            BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
X
xionglei6 已提交
359 360 361
        }
    }
    return 0;
X
xionglei6 已提交
362
}