It_vfs_fat_mutipthread_046.cpp 14.0 KB
Newer Older
L
lnlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "It_vfs_fat.h"

static VOID *PthreadF01(void *arg)
{
F
Far 已提交
36
    INT32 i, j, ret, len, index;
L
lnlan 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    INT32 fd[100] = {};
    CHAR pathname[FAT_STANDARD_NAME_LENGTH] = "";
    CHAR readbuf[FAT_STANDARD_NAME_LENGTH] = "";
    CHAR filebuf[260] = "01234567890123456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123"
        "456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123456789abcedfgh"
        "ij9876543210abcdeabcde0123456789abcedfghij9876543210lalalalalalalala";
    CHAR writebuf[20] = "0123456789";
    CHAR *bufWrite = nullptr;

    bufWrite = (CHAR *)malloc(BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB
    ICUNIT_ASSERT_NOT_EQUAL_NULL(bufWrite, NULL, NULL);
    (void)memset_s(bufWrite, BYTES_PER_MBYTES + 1, 0, BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB

    for (i = 0; i < BYTES_PER_KBYTES * 4; i++) { // loop in 4 Kb
F
Far 已提交
51
        (void)strcat_s(bufWrite, BYTES_PER_MBYTES + 1, filebuf);
L
lnlan 已提交
52 53 54 55 56 57 58 59
    }

    index = 0;
    for (i = 0; i < FAT_MAX_CYCLES; i++) { // 100个文件
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file1%d.txt", FAT_PATH_NAME,
            index);
        fd[index] = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);
        if (fd[index] == -1) {
F
Far 已提交
60 61
			index--;
            ICUNIT_GOTO_NOT_EQUAL(fd[index], -1, fd[index], EXIT1);
L
lnlan 已提交
62 63 64 65 66
        }
        switch (index % 3) { // mod 3
            case 0:
                for (j = 0; j < FAT_MAX_CYCLES; j++) { // 写满100M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
67
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
68 69 70 71 72 73
                }
                LosTaskDelay(3); // delay 3 s
                break;
            case 1:
                for (j = 0; j < FAT_MIDDLE_CYCLES; j++) { // 写满10M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
74
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
75 76 77 78 79
                }
                LosTaskDelay(5); // delay 5 s
                break;
            case 2: // the 2 nd case
                len = write(fd[index], writebuf, strlen(writebuf));
F
Far 已提交
80
                ICUNIT_GOTO_EQUAL(len, strlen(writebuf), len, EXIT1);
L
lnlan 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                LosTaskDelay(7); // delay 7 s
                break;
            default:
                break;
        }

        ret = lseek(fd[index], 0, SEEK_SET);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
        len = read(fd[index], readbuf, 10);     // read 10 bytes
        ICUNIT_GOTO_EQUAL(len, 10, len, EXIT1); // len must be 10
        ICUNIT_ASSERT_STRING_EQUAL_RET(readbuf, "0123456789", readbuf, NULL);
        ret = close(fd[index]);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
        index++;
    }

F
Far 已提交
97
    for (i = index - 1; i >= 0; i--) {
L
lnlan 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file1%d.txt", FAT_PATH_NAME,
            i);
        ret = unlink(pathname);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
    }
    g_testCount++;
    free(bufWrite);
    return NULL;
EXIT1:
    for (i = index; i >= 0; i--) {
        close(fd[i]);
    }
EXIT:
    for (i = index; i >= 0; i--) {
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file1%d.txt", FAT_PATH_NAME,
            i);
        unlink(pathname);
    }
    g_testCount = 0;
    free(bufWrite);
    return NULL;
}

static VOID *PthreadF02(void *arg)
{
F
Far 已提交
123
    INT32 i, j, ret, len, index;
L
lnlan 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
    INT32 fd[100] = {};
    CHAR pathname[FAT_STANDARD_NAME_LENGTH] = "";
    CHAR filebuf[260] = "01234567890123456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123"
        "456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123456789abcedfgh"
        "ij9876543210abcdeabcde0123456789abcedfghij9876543210lalalalalalalala";
    CHAR writebuf[20] = "0123456789";
    CHAR *bufWrite = nullptr;
    struct stat statbuf;

    bufWrite = (CHAR *)malloc(BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB
    ICUNIT_ASSERT_NOT_EQUAL_NULL(bufWrite, NULL, NULL);
    (void)memset_s(bufWrite, BYTES_PER_MBYTES + 1, 0, BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB

    for (i = 0; i < BYTES_PER_KBYTES * 4; i++) { // loop in 4 Kb
F
Far 已提交
138
        (void)strcat_s(bufWrite, BYTES_PER_MBYTES + 1, filebuf);
L
lnlan 已提交
139 140 141 142 143 144 145 146
    }

    index = 0;
    for (i = 0; i < FAT_MAX_CYCLES; i++) { // 100个文件
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file2%d.txt", FAT_PATH_NAME,
            index);
        fd[index] = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);
        if (fd[index] == -1) {
F
Far 已提交
147 148
			index--;
            ICUNIT_GOTO_NOT_EQUAL(fd[index], -1, fd[index], EXIT1);
L
lnlan 已提交
149 150 151 152 153
        }
        switch (index % 3) { // mod 3
            case 0:
                for (j = 0; j < FAT_MAX_CYCLES; j++) { // 写满100M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
154
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
155 156 157 158 159 160
                }
                LosTaskDelay(3); // delay 3 s
                break;
            case 1:
                for (j = 0; j < FAT_MIDDLE_CYCLES; j++) { // 写满10M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
161
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
162 163 164 165 166
                }
                LosTaskDelay(5); // delay 5 s
                break;
            case 2: // the 2 nd case
                len = write(fd[index], writebuf, strlen(writebuf));
F
Far 已提交
167
                ICUNIT_GOTO_EQUAL(len, strlen(writebuf), len, EXIT1);
L
lnlan 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                LosTaskDelay(7); // delay 7 s
                break;
            default:
                break;
        }

        ret = close(fd[index]);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

        ret = stat(pathname, &statbuf);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
        FatStatPrintf(statbuf);

        index++;
    }

F
Far 已提交
184
    for (i = index - 1; i >= 0; i--) {
L
lnlan 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file2%d.txt", FAT_PATH_NAME,
            i);
        ret = unlink(pathname);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
    }

    g_testCount++;

    free(bufWrite);
    return NULL;
EXIT1:
    for (i = index; i >= 0; i--) {
        close(fd[i]);
    }
EXIT:
    for (i = index; i >= 0; i--) {
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file2%d.txt", FAT_PATH_NAME,
            i);
        unlink(pathname);
    }
    g_testCount = 0;
    free(bufWrite);
    return NULL;
}

static VOID *PthreadF03(void *arg)
{
F
Far 已提交
212
    INT32 i, j, ret, len, index;
L
lnlan 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
    INT32 fd[100] = {};
    CHAR pathname[FAT_STANDARD_NAME_LENGTH] = "";
    CHAR pathname1[FAT_STANDARD_NAME_LENGTH] = "";
    CHAR filebuf[260] = "01234567890123456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123"
        "456789abcedfghij9876543210abcdeabcde0123456789abcedfghij9876543210abcdeabcde0123456789abcedfgh"
        "ij9876543210abcdeabcde0123456789abcedfghij9876543210lalalalalalalala";
    CHAR writebuf[20] = "0123456789";
    CHAR *bufWrite = nullptr;

    bufWrite = (CHAR *)malloc(BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB
    ICUNIT_ASSERT_NOT_EQUAL_NULL(bufWrite, NULL, NULL);
    (void)memset_s(bufWrite, BYTES_PER_MBYTES + 1, 0, BYTES_PER_MBYTES + 1); // BYTES_PER_MBYTES = 1MB

    for (i = 0; i < BYTES_PER_KBYTES * 4; i++) { // loop in 4 Kb
F
Far 已提交
227
        (void)strcat_s(bufWrite, BYTES_PER_MBYTES + 1, filebuf);
L
lnlan 已提交
228 229 230 231 232 233 234 235
    }

    index = 0;
    for (i = 0; i < FAT_MAX_CYCLES; i++) { // 100个文件
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file3%d.txt", FAT_PATH_NAME,
            index);
        fd[index] = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);
        if (fd[index] == -1) {
F
Far 已提交
236 237
		    index--;
            ICUNIT_GOTO_NOT_EQUAL(fd[index], -1, fd[index], EXIT1);
L
lnlan 已提交
238 239 240 241 242
        }
        switch (index % 3) { // mod 3
            case 0:
                for (j = 0; j < FAT_MAX_CYCLES; j++) { // 写满100M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
243
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
244 245 246 247 248 249
                }
                LosTaskDelay(3); // delay 3 s
                break;
            case 1:
                for (j = 0; j < FAT_MIDDLE_CYCLES; j++) { // 写满10M
                    len = write(fd[index], bufWrite, strlen(bufWrite));
F
Far 已提交
250
                    ICUNIT_GOTO_EQUAL(len, strlen(bufWrite), len, EXIT1);
L
lnlan 已提交
251 252 253 254 255
                }
                LosTaskDelay(5); // delay 5 s
                break;
            case 2: // the 2 nd case
                len = write(fd[index], writebuf, strlen(writebuf));
F
Far 已提交
256
                ICUNIT_GOTO_EQUAL(len, strlen(writebuf), len, EXIT1);
L
lnlan 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                LosTaskDelay(7); // delay 7 s
                break;
            default:
                break;
        }
        ret = close(fd[index]);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

        (void)snprintf_s(pathname1, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file3_%d.txt",
            FAT_PATH_NAME, index);

        ret = rename(pathname, pathname1);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

        index++;
    }

F
Far 已提交
274
    for (i = index - 1; i >= 0; i--) {
L
lnlan 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 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 336
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file3_%d.txt", FAT_PATH_NAME,
            i);
        ret = unlink(pathname);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
    }

    g_testCount++;
    free(bufWrite);
    return NULL;
EXIT1:
    for (i = index; i >= 0; i--) {
        close(fd[i]);
    }
    unlink(pathname);
EXIT:
    for (i = index; i >= 0; i--) {
        (void)snprintf_s(pathname, FAT_STANDARD_NAME_LENGTH, FAT_STANDARD_NAME_LENGTH, "%s/file3_%d.txt", FAT_PATH_NAME,
            i);
        unlink(pathname);
    }
    g_testCount = 0;
    free(bufWrite);
    return NULL;
}
static UINT32 TestCase(VOID)
{
    INT32 ret, i;
    CHAR bufname[FAT_STANDARD_NAME_LENGTH] = FAT_PATH_NAME;
    pthread_attr_t attr[FAT_MAX_THREADS];
    pthread_t threadId[FAT_MAX_THREADS];

    g_testCount = 0;

    ret = mkdir(bufname, S_IRWXU | S_IRWXG | S_IRWXO);
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);
    ret = PosixPthreadInit(&attr[0], TASK_PRIO_TEST - 2); // 2 less than TASK_PRIO_TEST
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

    ret = pthread_create(&threadId[0], &attr[0], PthreadF01, (void *)0);
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

    ret = PosixPthreadInit(&attr[1], TASK_PRIO_TEST - 1); // level less 1
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

    ret = pthread_create(&threadId[1], &attr[1], PthreadF02, (void *)1);
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

    ret = PosixPthreadInit(&attr[2], TASK_PRIO_TEST - 3); // fd 2 level less for 3
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);

    ret = pthread_create(&threadId[2], &attr[2], PthreadF03, (void *)2); // the no 2 thread
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
    for (i = 0; i < FAT_MAX_THREADS; i++) {
        ret = pthread_join(threadId[i], NULL);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
    }

    for (i = 0; i < FAT_MAX_THREADS; i++) {
        ret = pthread_attr_destroy(&attr[i]);
        ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT1);
    }

F
Far 已提交
337
    ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // there 3 threads
L
lnlan 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

    ret = rmdir(bufname);
    ICUNIT_GOTO_EQUAL(ret, FAT_NO_ERROR, ret, EXIT);

    return FAT_NO_ERROR;

EXIT1:
    for (i = 0; i < FAT_MAX_THREADS; i++) {
        pthread_join(threadId[i], NULL);
    }
    for (i = 0; i < FAT_MAX_THREADS; i++) {
        pthread_attr_destroy(&attr[i]);
    }
EXIT:
    rmdir(bufname);

    return FAT_NO_ERROR;
}

/* *
* - @test IT_FS_FAT_MUTIPTHREAD_046
* - @tspec pressure test
* - @ttitle multithreaded operating the files
* - @tbrief
1. In the same directory
2. Three threads
3. different priority
       pthread_f01 middle
       pthread_f02 low
       pthread_f03 high
4. pthread_f01 open, write, read, offset, close files
5. pthread_f02 open, search, close files
6. pthread_f03 create, rename, remove files
7. 100 files per thread include 100M, 10M, 10b
* - @ tprior 4
* - @ tauto TRUE
* - @ tremark
*/
VOID ItFsFatMutipthread046(VOID)
{
    TEST_ADD_CASE("IT_FS_FAT_MUTIPTHREAD_046", TestCase, TEST_VFS, TEST_VFAT, TEST_LEVEL4, TEST_PRESSURE);
}