From 345b2ee8882cae518d9d37b96ccb980c0c5ab7c8 Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Tue, 17 Mar 2020 04:11:20 +0000 Subject: [PATCH] Bug 1619362 - Parse MOZ_BASE_PROFILER_STARTUP_DURATION and _INTERVAL. r=froydnj Differential Revision: https://phabricator.services.mozilla.com/D64988 --HG-- extra : moz-landing-system : lando --- mozglue/baseprofiler/core/platform.cpp | 53 ++++++++++--------- mozglue/misc/decimal/Decimal.cpp | 13 +++++ mozglue/misc/decimal/DoubleConversion.h | 27 ++++++++++ .../decimal/add-doubleconversion-impl.patch | 42 +++++++++++++++ mozglue/misc/decimal/moz-decimal-utils.h | 11 ++-- mozglue/misc/decimal/update.sh | 12 +++-- mozglue/misc/moz.build | 1 + 7 files changed, 126 insertions(+), 33 deletions(-) create mode 100644 mozglue/misc/decimal/DoubleConversion.h create mode 100644 mozglue/misc/decimal/add-doubleconversion-impl.patch diff --git a/mozglue/baseprofiler/core/platform.cpp b/mozglue/baseprofiler/core/platform.cpp index 28783ed284d6..562a6aff3490 100644 --- a/mozglue/baseprofiler/core/platform.cpp +++ b/mozglue/baseprofiler/core/platform.cpp @@ -47,8 +47,10 @@ # include "mozilla/Atomics.h" # include "mozilla/AutoProfilerLabel.h" # include "mozilla/BaseProfilerDetail.h" +# include "mozilla/DoubleConversion.h" # include "mozilla/Printf.h" # include "mozilla/Services.h" +# include "mozilla/Span.h" # include "mozilla/StackWalk.h" # include "mozilla/StaticPtr.h" # include "mozilla/ThreadLocal.h" @@ -2422,35 +2424,36 @@ void profiler_init(void* aStackTop) { const char* startupDuration = getenv("MOZ_BASE_PROFILER_STARTUP_DURATION"); if (startupDuration && startupDuration[0] != '\0') { - // TODO implement if needed - MOZ_CRASH("MOZ_BASE_PROFILER_STARTUP_DURATION unsupported"); - // errno = 0; - // double durationVal = PR_strtod(startupDuration, nullptr); - // if (errno == 0 && durationVal >= 0.0) { - // if (durationVal > 0.0) { - // duration = Some(durationVal); - // } - // LOG("- MOZ_BASE_PROFILER_STARTUP_DURATION = %f", durationVal); - // } else { - // LOG("- MOZ_BASE_PROFILER_STARTUP_DURATION not a valid float: %s", - // startupDuration); - // PrintUsageThenExit(1); - // } + // The duration is a floating point number. Use StringToDouble rather than + // strtod, so that "." is used as the decimal separator regardless of OS + // locale. + auto durationVal = StringToDouble(std::string(startupDuration)); + if (durationVal && *durationVal >= 0.0) { + if (*durationVal > 0.0) { + duration = Some(*durationVal); + } + LOG("- MOZ_BASE_PROFILER_STARTUP_DURATION = %f", *durationVal); + } else { + LOG("- MOZ_BASE_PROFILER_STARTUP_DURATION not a valid float: %s", + startupDuration); + PrintUsageThenExit(1); + } } const char* startupInterval = getenv("MOZ_BASE_PROFILER_STARTUP_INTERVAL"); if (startupInterval && startupInterval[0] != '\0') { - // TODO implement if needed - MOZ_CRASH("MOZ_BASE_PROFILER_STARTUP_INTERVAL unsupported"); - // errno = 0; - // interval = PR_strtod(startupInterval, nullptr); - // if (errno == 0 && interval > 0.0 && interval <= 1000.0) { - // LOG("- MOZ_BASE_PROFILER_STARTUP_INTERVAL = %f", interval); - // } else { - // LOG("- MOZ_BASE_PROFILER_STARTUP_INTERVAL not a valid float: %s", - // startupInterval); - // PrintUsageThenExit(1); - // } + // The interval is a floating point number. Use StringToDouble rather than + // strtod, so that "." is used as the decimal separator regardless of OS + // locale. + auto intervalValue = StringToDouble(MakeStringSpan(startupInterval)); + if (intervalValue && *intervalValue > 0.0 && *intervalValue <= 1000.0) { + interval = *intervalValue; + LOG("- MOZ_BASE_PROFILER_STARTUP_INTERVAL = %f", interval); + } else { + LOG("- MOZ_BASE_PROFILER_STARTUP_INTERVAL not a valid float: %s", + startupInterval); + PrintUsageThenExit(1); + } } features |= StartupExtraDefaultFeatures() & AvailableFeatures(); diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp index b08b4160f275..cc828e28439f 100644 --- a/mozglue/misc/decimal/Decimal.cpp +++ b/mozglue/misc/decimal/Decimal.cpp @@ -30,6 +30,7 @@ #include "Decimal.h" #include "moz-decimal-utils.h" +#include "DoubleConversion.h" using namespace moz_decimal_utils; @@ -1048,3 +1049,15 @@ Decimal Decimal::zero(Sign sign) } } // namespace blink + +// Implementation of DoubleConversion.h: + +namespace mozilla { + +Maybe StringToDouble(Span aStringSpan) { + bool valid = false; + double result = mozToDouble(aStringSpan, &valid); + return valid ? Some(result) : Nothing(); +} + +} diff --git a/mozglue/misc/decimal/DoubleConversion.h b/mozglue/misc/decimal/DoubleConversion.h new file mode 100644 index 000000000000..14c19e25400a --- /dev/null +++ b/mozglue/misc/decimal/DoubleConversion.h @@ -0,0 +1,27 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* A utility function that converts a string to a double independent of OS locale. */ + +#ifndef MOZILLA_DOUBLECONVERSION_H +#define MOZILLA_DOUBLECONVERSION_H + +#include "mozilla/Maybe.h" +#include "mozilla/Span.h" + +#include + +namespace mozilla { + +// Parses aStringSpan into a double floating point value. Always treats . as the +// decimal separator, regardless of OS locale. Consumes the entire string; +// trailing garbage is invalid. Returns Nothing() for invalid input. +// The implementation uses double_conversion::StringToDoubleConverter with +// NO_FLAGS, see double-conversion/string-to-double.h for more documentation. +Maybe StringToDouble(Span aStringSpan); + +} + +#endif // MOZILLA_DOUBLECONVERSION_H diff --git a/mozglue/misc/decimal/add-doubleconversion-impl.patch b/mozglue/misc/decimal/add-doubleconversion-impl.patch new file mode 100644 index 000000000000..1cf0fb6ff199 --- /dev/null +++ b/mozglue/misc/decimal/add-doubleconversion-impl.patch @@ -0,0 +1,42 @@ +diff --git a/mozglue/misc/decimal/Decimal.cpp b/mozglue/misc/decimal/Decimal.cpp +--- a/mozglue/misc/decimal/Decimal.cpp ++++ b/mozglue/misc/decimal/Decimal.cpp +@@ -25,16 +25,17 @@ + * 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 "Decimal.h" + #include "moz-decimal-utils.h" ++#include "DoubleConversion.h" + + using namespace moz_decimal_utils; + + #include + #include + + namespace blink { + +@@ -1043,8 +1044,20 @@ bool Decimal::toString(char* strBuf, siz + } + + Decimal Decimal::zero(Sign sign) + { + return Decimal(EncodedData(sign, EncodedData::ClassZero)); + } + + } // namespace blink ++ ++// Implementation of DoubleConversion.h: ++ ++namespace mozilla { ++ ++Maybe StringToDouble(Span aStringSpan) { ++ bool valid = false; ++ double result = mozToDouble(aStringSpan, &valid); ++ return valid ? Some(result) : Nothing(); ++} ++ ++} diff --git a/mozglue/misc/decimal/moz-decimal-utils.h b/mozglue/misc/decimal/moz-decimal-utils.h index a9caed0143b5..390bdaf02de0 100644 --- a/mozglue/misc/decimal/moz-decimal-utils.h +++ b/mozglue/misc/decimal/moz-decimal-utils.h @@ -14,6 +14,7 @@ #include "mozilla/ArrayUtils.h" #include "mozilla/Casting.h" #include "mozilla/FloatingPoint.h" +#include "mozilla/Span.h" #include #include @@ -42,18 +43,22 @@ typedef std::string String; -double mozToDouble(const String &aStr, bool *valid) { +double mozToDouble(mozilla::Span aStr, bool *valid) { double_conversion::StringToDoubleConverter converter( double_conversion::StringToDoubleConverter::NO_FLAGS, mozilla::UnspecifiedNaN(), mozilla::UnspecifiedNaN(), nullptr, nullptr); - const char* str = aStr.c_str(); - int length = mozilla::AssertedCast(strlen(str)); + const char* str = aStr.Elements(); + int length = mozilla::AssertedCast(aStr.Length()); int processed_char_count; // unused - NO_FLAGS requires the whole string to parse double result = converter.StringToDouble(str, length, &processed_char_count); *valid = mozilla::IsFinite(result); return result; } +double mozToDouble(const String &aStr, bool *valid) { + return mozToDouble(mozilla::MakeStringSpan(aStr.c_str()), valid); +} + String mozToString(double aNum) { char buffer[64]; int buffer_length = mozilla::ArrayLength(buffer); diff --git a/mozglue/misc/decimal/update.sh b/mozglue/misc/decimal/update.sh index 59e626f872b4..23748ebe2c25 100755 --- a/mozglue/misc/decimal/update.sh +++ b/mozglue/misc/decimal/update.sh @@ -35,6 +35,7 @@ if [ $# -eq 1 ]; then cp "$P" . done else + #LATEST_SHA=$(cat UPSTREAM-GIT-SHA) LATEST_SHA=$(git ls-remote https://chromium.googlesource.com/chromium/src.git/ | awk "/refs\/heads\/master/ {print \$1}") REPO_PATH="https://chromium.googlesource.com/chromium/src.git/+/$LATEST_SHA/third_party/WebKit/Source/platform" #REPO_PATH="https://github.com/WebKit/webkit/tree/master/Source/WebCore/platform" @@ -49,10 +50,11 @@ fi # Apply patches: -patch -p3 < zero-serialization.patch -patch -p3 < comparison-with-nan.patch -patch -p3 < mfbt-abi-markers.patch -patch -p3 < to-moz-dependencies.patch +patch -p4 < zero-serialization.patch +patch -p4 < comparison-with-nan.patch +patch -p4 < mfbt-abi-markers.patch +patch -p4 < to-moz-dependencies.patch +patch -p4 < add-doubleconversion-impl.patch # The following is disabled. See # https://bugzilla.mozilla.org/show_bug.cgi?id=1208357#c7 -#patch -p3 < fix-wshadow-warnings.patch +#patch -p4 < fix-wshadow-warnings.patch diff --git a/mozglue/misc/moz.build b/mozglue/misc/moz.build index 247682cc0384..e859205f8ca1 100644 --- a/mozglue/misc/moz.build +++ b/mozglue/misc/moz.build @@ -9,6 +9,7 @@ FINAL_LIBRARY = 'mozglue' EXPORTS.mozilla += [ 'AutoProfilerLabel.h', 'decimal/Decimal.h', + 'decimal/DoubleConversion.h', 'PlatformConditionVariable.h', 'PlatformMutex.h', 'Printf.h',