FAT fsを使用してSDにファイルを書き込むと、メモリリークが発生することがあります。

FAT fsを使用してSDにファイルを書き込むと、メモリリークが発生することがあります。

SDカードスロット付きのHI3516A SoCベースのIPカメラがあります。カメラのオペレーティングシステムはカスタム3.4.45 Linuxカーネルです。すべてがうまく機能しますが、Azureのリモートストレージにあるカメラのビデオコピーが必要なので、内部RTPストリームをキャプチャしてセグメントに分割し、セグメントをSDカードに書き込むプログラムをCで書いています。私のAzureストレージに送信されます。

64GBのSDカードをカメラにマウントし、FAT fsを作成した後、/mnt/sd_cardにマウントしました。私のプログラムは期待どおりに動作しましたが、正常に動作してから1〜3日後にカメラのRAMがすべてどこかに消えたことがわかりました。

まず、プログラムにメモリリークがあることを確認し、次のコマンドでプログラムを終了しました。

kill -S SIGKILL 1223

ただし、メモリはシステムに返されません。私の考えには奇妙な行動です。数日間バグを修正した後、突然SDカードをアンマウントし、すべてのメモリがシステムに返されたことを発見しました。今、私のプログラムのファイル生成部分に問題があると判断しました。この状況をテストするために、次のプログラムを作成しました。

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <inttypes.h>
#include <time.h>

#define KB 1024
#define MB KB * 1024
#define GB MB * 1024


static void print_msg(const char *fmt, ...) {
    va_list args;
    time_t timer = time(NULL);

    printf("%s\t", ctime(&timer));

    va_start(args, fmt);
    vfprintf(stdout, fmt, args);
    va_end(args);
}

static int generate_file(const char *dest_fname, const size_t file_size) {
    char buffer[BUFSIZ];
    const char *source_fname = "/dev/urandom";
    size_t bytes_to_operate, avaliable_bytes = file_size;
    int bytes_readed, bytes_writed, source_fd = -1, dest_fd = -1, rc = EXIT_SUCCESS;


    source_fd = open(source_fname, O_RDONLY);
    if (source_fd < 0) {
        printf("Can't open source file '%s': %s.\n", source_fname, strerror(errno));
        rc = EXIT_FAILURE;
        goto end;
    }

    dest_fd = open(dest_fname, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    if (dest_fd < 0) {
        printf("Can't open destination file '%s': %s.\n", dest_fname, strerror(errno));
        rc = EXIT_FAILURE;
        goto end;
    }

    bytes_to_operate = avaliable_bytes - BUFSIZ;

    print_msg("Write random data to file\n");
    while(avaliable_bytes > 0) {
        bytes_to_operate = (avaliable_bytes > BUFSIZ) 
                ? BUFSIZ
                : avaliable_bytes;

        bytes_readed = read(source_fd, buffer, bytes_to_operate);
        if (bytes_readed < 0) {
            printf("Error while reading data from '%s': %s", source_fname,
                    strerror(errno));
            rc = EXIT_FAILURE;
            goto end;
        }

        if (bytes_readed != bytes_to_operate) {
            // don't read write more than read
            bytes_to_operate = bytes_readed;
        }

        bytes_writed = write(dest_fd, buffer, bytes_to_operate);

        if (bytes_writed < 0) {
            printf("Error while writing data to '%s': %s", dest_fname,
                    strerror(errno));
            rc = EXIT_FAILURE;
            goto end;
        }

        avaliable_bytes -= bytes_writed;
    }

end:
    print_msg("Close file\n");                
    close(source_fd);
    close(dest_fd);

    return rc;
}

static void generate_fpath(char *out, const char *dest_path, uint16_t index) {
    snprintf(out, PATH_MAX, "%s/file_%"SCNu16, dest_path, index);
}

int main(int argc, char** argv) {

    size_t file_size;
    const char *dest_path = argv[3];
    char fpath[PATH_MAX];
    uint16_t del_index = 0, cur_index = 0;

    switch(argv[2][0]) {
        case 'K':
            file_size = atoi(argv[1]) * KB;
            break;
        case 'M':
            file_size = atoi(argv[1]) * MB;
            break;
        default:
            file_size = atoi(argv[1]);
            break;
    } 

    print_msg("Generate files of size %s%s to %s\n", argv[1], argv[2], argv[3]);

    while (cur_index < UINT16_MAX) {
        generate_fpath(fpath, dest_path, cur_index);
        generate_file(fpath, file_size);
        printf("Created file %s\n", fpath);
        cur_index++;
        del_index++;
        sleep(1);
    }

    return EXIT_SUCCESS;
}

テストを始める前に私が持っているもの:

/mnt # free
             total         used         free       shared      buffers
Mem:        125044        58540        66504            0          480
-/+ buffers:              58060        66984
Swap:            0            0            0

これでテストコードを実行し、SDカードに10 MBのファイルを生成するように依頼します。

/mnt # mount /dev/sd_card2 /mnt/sd_card2
/mnt # ./random_size_file_generator 10 M /mnt/sd_card2

別のウィンドウで私は私の記憶を監視し始めました

~ # free
             total         used         free       shared      buffers
Mem:        125044        60988        64056            0         2180
-/+ buffers:              58808        66236
Swap:            0            0            0
~ # free
             total         used         free       shared      buffers
Mem:        125044        65464        59580            0         2204
-/+ buffers:              63260        61784
Swap:            0            0            0

...

~ # free
             total         used         free       shared      buffers
Mem:        125044       123004         2040            0         1104
-/+ buffers:             121900         3144
Swap:            0            0            0

RAMは2MBしか使用できません。コードを停止し、SDカードをマウント解除し、メモリの状態を見つけます。

/mnt # ./random_size_file_generator 10 /mnt/sd_card2
Created file /mnt/sd_card2/file_0
Created file /mnt/sd_card2/file_1
Created file /mnt/sd_card2/file_2
Created file /mnt/sd_card2/file_3
Created file /mnt/sd_card2/file_4
Created file /mnt/sd_card2/file_5
Created file /mnt/sd_card2/file_6
Created file /mnt/sd_card2/file_7
Created file /mnt/sd_card2/file_8
Created file /mnt/sd_card2/file_9
^C
/mnt # umount /mnt/sd_card2/
/mnt # free
             total         used         free       shared      buffers
Mem:        125044        58500        66544            0          472
-/+ buffers:              58028        67016
Swap:            0            0            0

それで、誰かが私に何が起こっているのかを説明できますか?私の考えでは、LinuxはすべてのデータがSDカードに書き込まれるようにバッファのメモリの一部を予約していましたが、プログラムが停止するとSDカードがマウント解除されるまでメモリは解放されません。

よろしくお願いします。

ベストアンサー1

おすすめ記事