Bug 209464 - Several refactorings done in the MemoryPressureMonitor.cpp
Summary: Several refactorings done in the MemoryPressureMonitor.cpp
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit2 (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Pablo Saavedra
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-03-23 23:39 PDT by Pablo Saavedra
Modified: 2020-03-31 07:21 PDT (History)
4 users (show)

See Also:


Attachments
patch (3.27 KB, patch)
2020-03-23 23:41 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.17 KB, patch)
2020-03-26 05:53 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.15 KB, patch)
2020-03-26 06:13 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.15 KB, patch)
2020-03-26 06:15 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.15 KB, patch)
2020-03-26 06:17 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.14 KB, patch)
2020-03-31 06:33 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff
patch (13.15 KB, patch)
2020-03-31 06:41 PDT, Pablo Saavedra
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Pablo Saavedra 2020-03-23 23:39:13 PDT
strtoull is a feature added in C++ since C11. This parses the C-string str interpreting its content as an `unsigned long long int` which is more appropriate  for the size_t (unsigned integer type) variables used by the `MemoryPressureMonitor.cpp` functions in counterpoint of atoll() what returns a `long long int`.
Comment 1 Pablo Saavedra 2020-03-23 23:41:41 PDT
Created attachment 394354 [details]
patch
Comment 2 Adrian Perez 2020-03-24 01:33:34 PDT
Comment on attachment 394354 [details]
patch

View in context: https://bugs.webkit.org/attachment.cgi?id=394354&action=review

> Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:87
> +            low += strtoull(token, nullptr, 10);

This is not doing any error checking to ensure that the parsing was
successful. There are also a generic string-to-integer conversion
function in WTF which you could use here:

  bool ok = true;
  low += WTF::toIntegralType<size_t, UChar>(token, strlen(token), &ok, 10);
  if (!ok) return 0;  // Return early on error.

…and so on for the rest of the conversions.
Comment 3 Pablo Saavedra 2020-03-26 05:53:48 PDT
Created attachment 394598 [details]
patch
Comment 4 Pablo Saavedra 2020-03-26 06:13:09 PDT
Created attachment 394599 [details]
patch
Comment 5 Pablo Saavedra 2020-03-26 06:15:01 PDT
Created attachment 394600 [details]
patch
Comment 6 Pablo Saavedra 2020-03-26 06:17:31 PDT
Created attachment 394601 [details]
patch
Comment 7 Pablo Saavedra 2020-03-26 06:25:58 PDT
(In reply to Adrian Perez from comment #2)
> Comment on attachment 394354 [details]
> patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394354&action=review
> 
> > Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:87
> > +            low += strtoull(token, nullptr, 10);
> 
> This is not doing any error checking to ensure that the parsing was
> successful. There are also a generic string-to-integer conversion
> function in WTF which you could use here:
> 
>   bool ok = true;
>   low += WTF::toIntegralType<size_t, UChar>(token, strlen(token), &ok, 10);
>   if (!ok) return 0;  // Return early on error.
> 
> …and so on for the rest of the conversions.

Thanks for your hints. I've used those in my new patch. In addition, I decided to clean up some repetitive code and fix some issues that I found running WPE in a cgroup-v2 with unified hierarchy.
Comment 8 Adrian Perez 2020-03-31 06:21:40 PDT
Comment on attachment 394601 [details]
patch

Thanks for the patch, having the checks for parsing numeric values in
places and some of the duplicated code factored out makes me more
confident in this being robust 👍️. Please check the comments before
landing.

It would be still nice to get rid of “strtok()” because often it is a
source of footguns; but I do not have any good suggestion that would
avoid writing a more formal parser… so let's just leave things as-is.

View in context: https://bugs.webkit.org/attachment.cgi?id=394601&action=review

> Source/WebKit/ChangeLog:34
> +        getMemoryUsageWithCgroup() sighly. The name of the controller

Typo: sighly → slightly

> Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp:232
> +    CString memoryControllerPath = CString();

You can remove the “= CString()” assignment. The default constructor
will initialize the value to an empty string anyway.
Comment 9 Pablo Saavedra 2020-03-31 06:33:48 PDT
Created attachment 395030 [details]
patch
Comment 10 Pablo Saavedra 2020-03-31 06:41:52 PDT
Created attachment 395031 [details]
patch
Comment 11 EWS 2020-03-31 07:20:46 PDT
Committed r259287: <https://trac.webkit.org/changeset/259287>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 395031 [details].
Comment 12 Radar WebKit Bug Importer 2020-03-31 07:21:14 PDT
<rdar://problem/61105994>