Changeset 260318 in webkit


Ignore:
Timestamp:
Apr 18, 2020 10:17:12 AM (4 years ago)
Author:
Pablo Saavedra
Message:

[GTK][WPE] Replace evil strtok() calls with fscanf() in MemoryPressureMonitor.cpp
https://bugs.webkit.org/show_bug.cgi?id=210346

Reviewed by Adrian Perez de Castro.

  • UIProcess/linux/MemoryPressureMonitor.cpp:

(WebKit::lowWatermarkPages):
(WebKit::getCgroupControllerPath):
(WebKit::systemMemoryUsedAsPercentage):
(WebKit::CGroupMemoryController::getCgroupFileValue):

Location:
trunk/Source/WebKit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r260317 r260318  
     12020-04-18  Pablo Saavedra  <psaavedra@igalia.com>
     2
     3        [GTK][WPE] Replace evil strtok() calls with fscanf() in MemoryPressureMonitor.cpp
     4        https://bugs.webkit.org/show_bug.cgi?id=210346
     5
     6        Reviewed by Adrian Perez de Castro.
     7
     8        * UIProcess/linux/MemoryPressureMonitor.cpp:
     9        (WebKit::lowWatermarkPages):
     10        (WebKit::getCgroupControllerPath):
     11        (WebKit::systemMemoryUsedAsPercentage):
     12        (WebKit::CGroupMemoryController::getCgroupFileValue):
     13
    1142020-04-18  Rob Buis  <rbuis@igalia.com>
    215
  • trunk/Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp

    r259923 r260318  
    6161static const unsigned maxCgroupPath = 4096; // PATH_MAX = 4096 from (Linux) include/uapi/linux/limits.h
    6262
     63#define CGROUP_NAME_BUFFER_SIZE 40
     64#define MEMINFO_TOKEN_BUFFER_SIZE 50
     65#define STRINGIFY_EXPANDED(val) #val
     66#define STRINGIFY(val) STRINGIFY_EXPANDED(val)
     67#define ZONEINFO_TOKEN_BUFFER_SIZE 128
     68
    6369static size_t lowWatermarkPages(FILE* zoneInfoFile)
    6470{
    65     if (!zoneInfoFile)
     71    size_t low = 0;
     72    char buffer[ZONEINFO_TOKEN_BUFFER_SIZE + 1];
     73    bool inNormalZone = false;
     74
     75    if (!zoneInfoFile || fseek(zoneInfoFile, 0, SEEK_SET))
    6676        return notSet;
    6777
    68     if (fseek(zoneInfoFile, 0, SEEK_SET))
    69         return notSet;
    70 
    71     size_t low = 0;
    72     bool inZone = false;
    73     bool foundLow = false;
    74     char buffer[128];
    75     while (char* line = fgets(buffer, 128, zoneInfoFile)) {
    76         if (!strncmp(line, "Node", 4)) {
    77             inZone = true;
    78             foundLow = false;
    79             continue;
    80         }
    81 
    82         char* token = strtok(line, " ");
    83         if (!token)
    84             continue;
    85 
    86         if (!strcmp(token, "low")) {
    87             if (!inZone || foundLow) {
    88                 low = notSet;
     78    while (!feof(zoneInfoFile)) {
     79        int r;
     80        r = fscanf(zoneInfoFile, " Node %*u, zone %" STRINGIFY(ZONEINFO_TOKEN_BUFFER_SIZE) "[^\n]\n", buffer);
     81        if (r == 2 && !strcmp(buffer, "Normal"))
     82            inNormalZone = true;
     83        r = fscanf(zoneInfoFile, "%" STRINGIFY(ZONEINFO_TOKEN_BUFFER_SIZE) "s", buffer);
     84        if (r == 1 && inNormalZone && !strcmp(buffer, "low")) {
     85            r = fscanf(zoneInfoFile, "%zu", &low);
     86            if (r == 1)
    8987                break;
    90             }
    91             token = strtok(nullptr, " ");
    92             if (!token) {
    93                 low = notSet;
    94                 break;
    95             }
    96             bool ok = true;
    97             auto value = toIntegralType<size_t>(token, strlen(token), &ok);
    98             if (ok)
    99                 low += value;
    100             foundLow = true;
    10188        }
    10289    }
     
    176163{
    177164    CString cgroupMemoryControllerPath;
    178     if (!cgroupControllerFile)
     165    if (!cgroupControllerFile || fseek(cgroupControllerFile, 0, SEEK_SET))
    179166        return CString();
    180167
    181     if (fseek(cgroupControllerFile, 0, SEEK_SET))
    182         return CString();
    183 
    184     char buffer[maxCgroupPath];
    185     while (char* line = fgets(buffer, maxCgroupPath, cgroupControllerFile)) {
    186         char* token = strtok(line, "\n");
    187         if (!token)
    188             break;
    189 
    190         token = strtok(token, ":");
    191         token = strtok(nullptr, ":");
    192         if (!strcmp(token, controllerName)) {
    193             token = strtok(nullptr, ":");
    194             cgroupMemoryControllerPath = CString(token);
    195             return cgroupMemoryControllerPath;
     168    while (!feof(cgroupControllerFile)) {
     169        char name[CGROUP_NAME_BUFFER_SIZE + 1];
     170        char path[maxCgroupPath + 1];
     171        int scanResult = fscanf(cgroupControllerFile, "%*u:%" STRINGIFY(CGROUP_NAME_BUFFER_SIZE) "[^:]:%" STRINGIFY(PATH_MAX) "[^\n]", name, path);
     172        if (scanResult != 2)
     173            return CString();
     174        if (!strcmp(name, controllerName)) {
     175            return CString(path);
    196176        }
    197         if (!strncmp(token, "name=systemd", 12)) {
    198             token = strtok(nullptr, ":");
    199             cgroupMemoryControllerPath = CString(token);
    200         }
     177        if (!strcmp(name, "name=systemd"))
     178            cgroupMemoryControllerPath = CString(path);
    201179    }
    202180    return cgroupMemoryControllerPath;
     
    206184static int systemMemoryUsedAsPercentage(FILE* memInfoFile, FILE* zoneInfoFile, CGroupMemoryController* memoryController)
    207185{
    208     if (!memInfoFile)
    209         return -1;
    210 
    211     if (fseek(memInfoFile, 0, SEEK_SET))
     186    if (!memInfoFile || fseek(memInfoFile, 0, SEEK_SET))
    212187        return -1;
    213188
    214189    size_t memoryAvailable, memoryTotal, memoryFree, activeFile, inactiveFile, slabReclaimable;
    215190    memoryAvailable = memoryTotal = memoryFree = activeFile = inactiveFile = slabReclaimable = notSet;
    216     char buffer[128];
    217     while (char* line = fgets(buffer, 128, memInfoFile)) {
    218         char* token = strtok(line, " ");
    219         bool ok = true;
    220         if (!token)
    221             break;
    222 
    223         if (!strcmp(token, "MemAvailable:")) {
    224             if ((token = strtok(nullptr, " "))) {
    225                 memoryAvailable = toIntegralType<size_t>(token, strlen(token), &ok);
    226                 if (!ok)
    227                     memoryAvailable = notSet;
    228                 if (memoryTotal != notSet)
    229                     break;
    230             }
    231         } else if (!strcmp(token, "MemTotal:")) {
    232             if ((token = strtok(nullptr, " "))) {
    233                 memoryTotal = toIntegralType<size_t>(token, strlen(token), &ok);
    234                 if (!ok)
    235                     memoryTotal = notSet;
    236             } else
    237                 break;
    238         } else if (!strcmp(token, "MemFree:")) {
    239             if ((token = strtok(nullptr, " "))) {
    240                 memoryFree = toIntegralType<size_t>(token, strlen(token), &ok);
    241                 if (!ok)
    242                     memoryFree = notSet;
    243             } else
    244                 break;
    245         } else if (!strcmp(token, "Active(file):")) {
    246             if ((token = strtok(nullptr, " "))) {
    247                 activeFile = toIntegralType<size_t>(token, strlen(token), &ok);
    248                 if (!ok)
    249                     activeFile = notSet;
    250             } else
    251                 break;
    252         } else if (!strcmp(token, "Inactive(file):")) {
    253             if ((token = strtok(nullptr, " "))) {
    254                 inactiveFile = toIntegralType<size_t>(token, strlen(token), &ok);
    255                 if (!ok)
    256                     inactiveFile = notSet;
    257             } else
    258                 break;
    259         } else if (!strcmp(token, "SReclaimable:")) {
    260             if ((token = strtok(nullptr, " "))) {
    261                 slabReclaimable = toIntegralType<size_t>(token, strlen(token), &ok);
    262                 if (!ok)
    263                     slabReclaimable = notSet;
    264             } else
    265                 break;
    266         }
     191
     192    while (!feof(memInfoFile)) {
     193        char token[MEMINFO_TOKEN_BUFFER_SIZE + 1];
     194        size_t amount;
     195        if (fscanf(memInfoFile, "%" STRINGIFY(MEMINFO_TOKEN_BUFFER_SIZE) "s%zukB", token, &amount) != 2)
     196            continue;
     197        if (!strcmp(token, "MemTotal:"))
     198            memoryTotal = amount;
     199        else if (!strcmp(token, "MemFree:"))
     200            memoryFree = amount;
     201        else if (!strcmp(token, "MemAvailable:"))
     202            memoryAvailable = amount;
     203        else if (!strcmp(token, "Active(file):"))
     204            activeFile = amount;
     205        else if (!strcmp(token, "Inactive(file):"))
     206            inactiveFile = amount;
     207        else if (!strcmp(token, "SReclaimable:"))
     208            slabReclaimable = amount;
    267209
    268210        if (memoryTotal != notSet && memoryFree != notSet && activeFile != notSet && inactiveFile != notSet && slabReclaimable != notSet)
     
    403345size_t CGroupMemoryController::getCgroupFileValue(FILE *file)
    404346{
    405     char buffer[128];
    406     char* token;
    407     bool ok = true;
    408 
    409     if (!file)
     347    if (!file || fseek(file, 0, SEEK_SET))
    410348        return notSet;
    411349
    412     if (fseek(file, 0, SEEK_SET))
    413         return notSet;
    414 
    415     token = fgets(buffer, 128, file);
    416 
    417     size_t value = toIntegralType<size_t>(token, strlen(token), &ok);
    418     if (!ok)
    419         value = notSet;
    420     return value;
     350    size_t value;
     351    return (fscanf(file, "%zu", &value) == 1) ? value : notSet;
    421352}
    422353
Note: See TracChangeset for help on using the changeset viewer.