Skip to content

Commit

Permalink
Bug 1643637: Make MOZ_PROFILER_STARTUP_ENTRIES understand sizes with …
Browse files Browse the repository at this point in the history
…units r=gerald

Differential Revision: https://phabricator.services.mozilla.com/D131022
  • Loading branch information
neelchauhan committed Nov 12, 2021
1 parent 6c9126e commit f11cb7b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions mozglue/baseprofiler/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <ostream>
#include <set>
#include <sstream>
#include <string_view>

// #include "memory_hooks.h"
#include "mozilla/ArrayUtils.h"
Expand Down Expand Up @@ -2023,6 +2024,7 @@ static void PrintUsageThenExit(int aExitCode) {
" If unset, the platform default is used:\n"
" %u entries per process, or %u when MOZ_PROFILER_STARTUP is set.\n"
" (%u bytes per entry -> %u or %u total bytes per process)\n"
" Optional units in bytes: KB, KiB, MB, MiB, GB, GiB\n"
"\n"
" MOZ_PROFILER_STARTUP_DURATION=<1..>\n"
" If MOZ_PROFILER_STARTUP is set, specifies the maximum life time\n"
Expand Down Expand Up @@ -2565,6 +2567,16 @@ static Vector<const char*> SplitAtCommas(const char* aString,
return array;
}

static const char* get_size_suffix(const char* str) {
const char* ptr = str;

while (isdigit(*ptr)) {
ptr++;
}

return ptr;
}

void profiler_init(void* aStackTop) {
LOG("profiler_init");

Expand Down Expand Up @@ -2633,6 +2645,27 @@ void profiler_init(void* aStackTop) {
if (startupCapacity && startupCapacity[0] != '\0') {
errno = 0;
long capacityLong = strtol(startupCapacity, nullptr, 10);
std::string_view sizeSuffix = get_size_suffix(startupCapacity);

if (sizeSuffix == "KB") {
capacityLong *= 1000 / scBytesPerEntry;
} else if (sizeSuffix == "KiB") {
capacityLong *= 1024 / scBytesPerEntry;
} else if (sizeSuffix == "MB") {
capacityLong *= (1000 * 1000) / scBytesPerEntry;
} else if (sizeSuffix == "MiB") {
capacityLong *= (1024 * 1024) / scBytesPerEntry;
} else if (sizeSuffix == "GB") {
capacityLong *= (1000 * 1000 * 1000) / scBytesPerEntry;
} else if (sizeSuffix == "GiB") {
capacityLong *= (1024 * 1024 * 1024) / scBytesPerEntry;
} else if (!sizeSuffix.empty()) {
PrintToConsole(
"- MOZ_PROFILER_STARTUP_ENTRIES unit must be one of the "
"following: KB, KiB, MB, MiB, GB, GiB");
PrintUsageThenExit(1);
}

// `long` could be 32 or 64 bits, so we force a 64-bit comparison with
// the maximum 32-bit signed number (as more than that is clamped down to
// 2^31 anyway).
Expand Down
32 changes: 32 additions & 0 deletions tools/profiler/core/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
#include <ostream>
#include <set>
#include <sstream>
#include <string_view>
#include <type_traits>

#if defined(GP_OS_android)
Expand Down Expand Up @@ -3162,6 +3163,7 @@ static void PrintUsageThenExit(int aExitCode) {
" If unset, the platform default is used:\n"
" %u entries per process, or %u when MOZ_PROFILER_STARTUP is set.\n"
" (%u bytes per entry -> %u or %u total bytes per process)\n"
" Optional units in bytes: KB, KiB, MB, MiB, GB, GiB\n"
"\n"
" MOZ_PROFILER_STARTUP_DURATION=<1..>\n"
" If MOZ_PROFILER_STARTUP is set, specifies the maximum life time of\n"
Expand Down Expand Up @@ -4310,6 +4312,16 @@ void profiler_init_threadmanager() {
});
}

static const char* get_size_suffix(const char* str) {
const char* ptr = str;

while (isdigit(*ptr)) {
ptr++;
}

return ptr;
}

void profiler_init(void* aStackTop) {
LOG("profiler_init");

Expand Down Expand Up @@ -4390,6 +4402,26 @@ void profiler_init(void* aStackTop) {
if (startupCapacity && startupCapacity[0] != '\0') {
errno = 0;
long capacityLong = strtol(startupCapacity, nullptr, 10);
std::string_view sizeSuffix = get_size_suffix(startupCapacity);

if (sizeSuffix == "KB") {
capacityLong *= 1000 / scBytesPerEntry;
} else if (sizeSuffix == "KiB") {
capacityLong *= 1024 / scBytesPerEntry;
} else if (sizeSuffix == "MB") {
capacityLong *= (1000 * 1000) / scBytesPerEntry;
} else if (sizeSuffix == "MiB") {
capacityLong *= (1024 * 1024) / scBytesPerEntry;
} else if (sizeSuffix == "GB") {
capacityLong *= (1000 * 1000 * 1000) / scBytesPerEntry;
} else if (sizeSuffix == "GiB") {
capacityLong *= (1024 * 1024 * 1024) / scBytesPerEntry;
} else if (!sizeSuffix.empty()) {
LOG("- MOZ_PROFILER_STARTUP_ENTRIES unit must be one of the "
"following: KB, KiB, MB, MiB, GB, GiB");
PrintUsageThenExit(1);
}

// `long` could be 32 or 64 bits, so we force a 64-bit comparison with
// the maximum 32-bit signed number (as more than that is clamped down to
// 2^31 anyway).
Expand Down

0 comments on commit f11cb7b

Please sign in to comment.