param_persist.c 8.9 KB
Newer Older
M
Mupceet 已提交
1
/*
M
Mupceet 已提交
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
M
Mupceet 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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 "param_persist.h"

#include <time.h>

#include "init_param.h"
#include "init_utils.h"
#include "param_manager.h"
#include "param_trie.h"
M
Mupceet 已提交
23 24
#include "param_osadp.h"
#include "securec.h"
M
Mupceet 已提交
25 26

static ParamPersistWorkSpace g_persistWorkSpace = {0, 0, NULL, 0, {}};
M
Mupceet 已提交
27 28 29 30 31 32 33 34
static int IsNeedToSave(const char *name)
{
#if defined(__LITEOS_M__) || defined(__LITEOS_A__)
    return IS_READY_ONLY(name) ? 0 : 1;
#else
    return (strncmp(name, PARAM_PERSIST_PREFIX, strlen(PARAM_PERSIST_PREFIX)) == 0) ? 1 : 0;
#endif
}
M
Mupceet 已提交
35

M
Mupceet 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static long long GetPersistCommitId(void)
{
    ParamWorkSpace *paramSpace = GetParamWorkSpace();
    PARAM_CHECK(paramSpace != NULL, return 0, "Invalid paramSpace");
    PARAM_WORKSPACE_CHECK(paramSpace, return 0, "Invalid space");
    WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
    if (space == NULL || space->area == NULL) {
        return 0;
    }
    PARAMSPACE_AREA_RD_LOCK(space);
    long long globalCommitId =  ATOMIC_LOAD_EXPLICIT(&space->area->commitPersistId, memory_order_acquire);
    PARAMSPACE_AREA_RW_UNLOCK(space);
    return globalCommitId;
}

static void UpdatePersistCommitId(void)
{
    ParamWorkSpace *paramSpace = GetParamWorkSpace();
    PARAM_CHECK(paramSpace != NULL, return, "Invalid paramSpace");
    PARAM_WORKSPACE_CHECK(paramSpace, return, "Invalid space");
    WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
    if (space == NULL || space->area == NULL) {
        return;
    }
    PARAMSPACE_AREA_RW_LOCK(space);
    long long globalCommitId = ATOMIC_LOAD_EXPLICIT(&space->area->commitPersistId, memory_order_relaxed);
    ATOMIC_STORE_EXPLICIT(&space->area->commitPersistId, ++globalCommitId, memory_order_release);
    PARAMSPACE_AREA_RW_UNLOCK(space);
}
M
Mupceet 已提交
65 66 67 68 69 70 71 72 73 74 75

static int SavePersistParam(const WorkSpace *workSpace, const ParamTrieNode *node, const void *cookie)
{
    ParamTrieNode *current = (ParamTrieNode *)node;
    if (current == NULL || current->dataIndex == 0) {
        return 0;
    }
    ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, current->dataIndex);
    if (entry == NULL) {
        return 0;
    }
M
Mupceet 已提交
76 77

    if (!IsNeedToSave(entry->data)) {
M
Mupceet 已提交
78 79 80 81 82 83
        return 0;
    }
    static char name[PARAM_NAME_LEN_MAX] = {0};
    int ret = memcpy_s(name, PARAM_NAME_LEN_MAX - 1, entry->data, entry->keyLength);
    PARAM_CHECK(ret == EOK, return -1, "Failed to read param name %s", entry->data);
    name[entry->keyLength] = '\0';
M
Mupceet 已提交
84 85
    ret = g_persistWorkSpace.persistParamOps.batchSave(
        (PERSIST_SAVE_HANDLE)cookie, name, entry->data + entry->keyLength + 1);
M
Mupceet 已提交
86 87 88 89
    PARAM_CHECK(ret == 0, return -1, "Failed to write param %s", current->key);
    return ret;
}

90
static int BatchSavePersistParam(void)
M
Mupceet 已提交
91 92 93 94 95 96 97 98 99 100 101
{
    PARAM_LOGV("BatchSavePersistParam");
    if (g_persistWorkSpace.persistParamOps.batchSaveBegin == NULL ||
        g_persistWorkSpace.persistParamOps.batchSave == NULL ||
        g_persistWorkSpace.persistParamOps.batchSaveEnd == NULL) {
        return 0;
    }

    PERSIST_SAVE_HANDLE handle;
    int ret = g_persistWorkSpace.persistParamOps.batchSaveBegin(&handle);
    PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Failed to save persist");
M
Mupceet 已提交
102 103 104 105 106
#if defined(__LITEOS_M__) || defined(__LITEOS_A__)
    const char *prefix = "";
#else
    const char *prefix = PARAM_PERSIST_PREFIX;
#endif
M
Mupceet 已提交
107
    // walk and save persist param
M
Mupceet 已提交
108
    WorkSpace *workSpace = GetFirstWorkSpace();
M
Mupceet 已提交
109 110
    while (workSpace != NULL) {
        WorkSpace *next = GetNextWorkSpace(workSpace);
M
Mupceet 已提交
111
        ParamTrieNode *root = FindTrieNode(workSpace, prefix, strlen(prefix), NULL);
M
Mupceet 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124
        PARAMSPACE_AREA_RD_LOCK(workSpace);
        TraversalTrieNode(workSpace, root, SavePersistParam, (void *)handle);
        PARAMSPACE_AREA_RW_UNLOCK(workSpace);
        workSpace = next;
    }
    g_persistWorkSpace.persistParamOps.batchSaveEnd(handle);
    PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Save persist param fail");

    PARAM_CLEAR_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE);
    (void)time(&g_persistWorkSpace.lastSaveTimer);
    return ret;
}

125
INIT_LOCAL_API int InitPersistParamWorkSpace(void)
M
Mupceet 已提交
126 127 128 129 130 131 132 133 134 135
{
    if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT)) {
        return 0;
    }
    (void)time(&g_persistWorkSpace.lastSaveTimer);
    RegisterPersistParamOps(&g_persistWorkSpace.persistParamOps);
    PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT);
    return 0;
}

M
Mupceet 已提交
136
INIT_LOCAL_API void ClosePersistParamWorkSpace(void)
M
Mupceet 已提交
137 138 139 140 141 142 143
{
    if (g_persistWorkSpace.saveTimer != NULL) {
        ParamTimerClose(g_persistWorkSpace.saveTimer);
    }
    g_persistWorkSpace.flags = 0;
}

M
Mupceet 已提交
144 145 146 147 148 149 150 151 152 153 154 155
void CheckAndSavePersistParam(void)
{
    // check commit
    long long commit = GetPersistCommitId();
    PARAM_LOGV("CheckAndSavePersistParam commit %lld %lld", commit, g_persistWorkSpace.commitId);
    if (g_persistWorkSpace.commitId == commit) {
        return;
    }
    g_persistWorkSpace.commitId = commit;
    (void)BatchSavePersistParam();
}

M
Mupceet 已提交
156 157 158 159
PARAM_STATIC void TimerCallbackForSave(const ParamTaskPtr timer, void *context)
{
    UNUSED(context);
    UNUSED(timer);
M
Mupceet 已提交
160
    PARAM_LOGV("TimerCallbackForSave ");
C
cheng_jinsong 已提交
161
    // for liteos-a must cycle check
M
Mupceet 已提交
162
#if (!defined(PARAM_SUPPORT_CYCLE_CHECK) || defined(PARAM_SUPPORT_REAL_CHECK))
M
Mupceet 已提交
163 164 165 166 167 168
    ParamTimerClose(g_persistWorkSpace.saveTimer);
    g_persistWorkSpace.saveTimer = NULL;
    if (!PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE)) {
        return;
    }
#endif
M
Mupceet 已提交
169
    CheckAndSavePersistParam();
M
Mupceet 已提交
170 171
}

M
Mupceet 已提交
172
INIT_LOCAL_API int WritePersistParam(const char *name, const char *value)
M
Mupceet 已提交
173 174
{
    PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
M
Mupceet 已提交
175
    if (!IsNeedToSave(name)) {
M
Mupceet 已提交
176 177 178 179 180 181 182 183
        return 0;
    }
    PARAM_LOGV("WritePersistParam name %s ", name);
    if (g_persistWorkSpace.persistParamOps.save != NULL) {
        g_persistWorkSpace.persistParamOps.save(name, value);
    }
    // update commit for check
    UpdatePersistCommitId();
M
Mupceet 已提交
184 185 186 187
    // for liteos-m, start task to check and save parameter
    // for linux, start timer after set persist parameter
    // for liteos-a, start timer in init to check and save parameter
#ifdef PARAM_SUPPORT_REAL_CHECK
M
Mupceet 已提交
188 189 190 191 192 193 194 195 196 197 198
    if (!PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
        PARAM_LOGE("Can not save persist param before load %s ", name);
        return 0;
    }
    if (g_persistWorkSpace.persistParamOps.batchSave == NULL) {
        return 0;
    }

    // check timer for save all
    time_t currTimer;
    (void)time(&currTimer);
M
Mupceet 已提交
199
    uint32_t diff = Difftime(currTimer, g_persistWorkSpace.lastSaveTimer);
M
Mupceet 已提交
200 201 202 203 204 205 206
    if (diff > PARAM_MUST_SAVE_PARAM_DIFF) {
        if (g_persistWorkSpace.saveTimer != NULL) {
            ParamTimerClose(g_persistWorkSpace.saveTimer);
            g_persistWorkSpace.saveTimer = NULL;
        }
        return BatchSavePersistParam();
    }
M
Mupceet 已提交
207

M
Mupceet 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_UPDATE);
    if (g_persistWorkSpace.saveTimer == NULL) {
        ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
        ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
    }
#endif
    return 0;
}

int LoadPersistParams(void)
{
    // get persist parameter
    int ret = InitPersistParamWorkSpace();
    PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
#ifndef STARTUP_INIT_TEST
    if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
        PARAM_LOGE("Persist param has been loaded");
        return 0;
    }
#endif
    if (g_persistWorkSpace.persistParamOps.load != NULL) {
C
codex  
chengjinsong 已提交
229
        (void)g_persistWorkSpace.persistParamOps.load();
M
Mupceet 已提交
230 231 232 233 234
        PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED);
    }
    // save new persist param
    ret = BatchSavePersistParam();
    PARAM_CHECK(ret == 0, return ret, "Failed to load persist param");
M
Mupceet 已提交
235
    // for liteos-a, start time to check in init
M
Mupceet 已提交
236
#ifdef PARAM_SUPPORT_CYCLE_CHECK
M
Mupceet 已提交
237
    PARAM_LOGV("LoadPersistParams start check time ");
M
Mupceet 已提交
238 239 240 241 242 243 244
    if (g_persistWorkSpace.saveTimer == NULL) {
        ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
        ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
    }
#endif
    return 0;
}