简体   繁体   English

文件系统性能非常差

[英]Very poor file system performance

I have a function that basically does this: 我有一个基本上可以做到这一点的功能:

int mmkdir(const char *path, mode_t mode)
{
    struct stat st;
    if (stat(path, &st) < 0)
    {
        if (errno != ENOENT)
            return -1;

        if (mkdir(path, S_IWUSR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0 && errno != EEXIST)
            return -1;
    }
    else if (!S_ISDIR(st.st_mode))
    {
        errno = ENOTDIR;
        return -1;
    }
    return 0;
}

int fun(int id, int id2)
{
    time_t ts;
    struct tm timeinfo;
    char buff[1024], buff1[20], buff2[20], buff3[20];

    ts = time(NULL);
    localtime_r(&ts, &timeinfo);
    strftime(buff1, sizeof(buff1), "%Y%m%d", &timeinfo);
    strftime(buff2, sizeof(buff2), "%H", &timeinfo);

    snprintf(buff, sizeof(buff), "%s/%s", dir, buff1);
    if (mmkdir(buff, ORDER_FILE_DEFAULT_PERMS) < 0)
        return -1;

    len = strlen(buff);
    snprintf(buff + len, sizeof(buff) - len, "/%s", buff2);
    if (mmkdir(buff, ORDER_FILE_DEFAULT_PERMS) < 0)
        return -1;
    len += strlen(buff2) + 1;

    strftime(buff3, sizeof(buff2), "%M%S", &timeinfo);
    snprintf(buff + len, sizeof(buff) - len - 1, "/%010d-%04d-%s%s.%s",
         id, id2, buff2, buff3, ext);

    return open(buff, O_WRONLY | O_APPEND | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
}

Plese don't check for errors, it's a working code. 请不要检查错误,这是一个有效的代码。 It basically calls stat (2), mkdir (2) (sometimes) and open (2). 它基本上调用stat (2), mkdir (2)(有时)和open (2)。

The problem is that when to I/O load on the server is pretty high, this piece of code sometimes takes even 7s (!!) to complete. 问题在于,当服务器上的I / O负载很高时,这段代码有时甚至需要7s(!!)才能完成。

These files that this functions create are located in a folder in /, which is mounted: 此函数创建的这些文件位于/的文件夹中,该文件夹已安装:

/dev/md0 on / type ext4 (rw,errors=remount-ro)

and can be up to 1000 files in a single folder. 一个文件夹中最多可以包含1000个文件。

What may be the problem? 可能是什么问题? Why this may take so long? 为什么要花这么长时间? Is there any misconfiguration? 是否有任何配置错误?

I don't ask only for improvements in the code, but in the server configuration also, if possible. 如果可能的话,我不仅要求改进代码,还要求改进服务器配置。

As per request in the comments, the output of cat /proc/mdstat is: 根据注释中的请求, cat /proc/mdstat的输出为:

Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md3 : active raid1 sdd1[1] sdc1[0]
      488282000 blocks super 1.2 [2/2] [UU]

md1 : active raid1 sda2[0] sdb2[1]
      15624120 blocks super 1.2 [2/2] [UU]

md2 : active raid1 sda3[0] sdb3[1]
      1958900 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sda1[0] sdb1[1]
      470798200 blocks super 1.2 [2/2] [UU]

that means that the disks are ok 这意味着磁盘还可以

If this function is called substantially more than once per hour, and the folders are not deleted, then your mmkdir calls are redundant. 如果每小时至少一次调用此函数,并且不删除文件夹,则mmkdir调用是多余的。 I would implement some kind of caching scheme where you remember the last hour in which you created a folder, and skip creating a new folder if you already created it. 我将实施某种缓存方案,让您记住创建文件夹的最后一个小时,如果已经创建,则跳过创建新文件夹的操作。 This will remove two stat calls, which could make a big difference on an overloaded system. 这将删除两个stat调用,这可能对过载的系统产生很大的影响。

There's likely to be no further improvement you can make to the code alone, since the open call would be the only remaining system call, and it cannot be elided without changing the meaning of the function. 您可能无法单独对代码进行进一步的改进,因为open调用将是唯一剩下的系统调用,并且在不更改函数含义的情况下就无法消除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM