From 06a0d1a446d1ba066bb2b7644b678327af250c73 Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:04:15 +0000 Subject: [PATCH 1/6] Error exit if either memory size exceeds 100% --- platformio/builder/tools/pioupload.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/platformio/builder/tools/pioupload.py b/platformio/builder/tools/pioupload.py index 224d0b90f8..68b6a37bee 100644 --- a/platformio/builder/tools/pioupload.py +++ b/platformio/builder/tools/pioupload.py @@ -218,16 +218,20 @@ def _format_availale_bytes(value, total): if int(ARGUMENTS.get("PIOVERBOSE", 0)): print(output) + memory_exceeded = False if data_max_size and data_size > data_max_size: + memory_exceeded = True sys.stderr.write( "Warning! The data size (%d bytes) is greater " "than maximum allowed (%s bytes)\n" % (data_size, data_max_size) ) if program_size > program_max_size: + memory_exceeded = True sys.stderr.write( "Error: The program size (%d bytes) is greater " "than maximum allowed (%s bytes)\n" % (program_size, program_max_size) ) + if memory_exceeded: env.Exit(1) From 196b3f6dbe71d6b37d28e0aab0c43d03ec30e07c Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:09:03 +0000 Subject: [PATCH 2/6] Improve explanation text --- platformio/builder/tools/pioupload.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/platformio/builder/tools/pioupload.py b/platformio/builder/tools/pioupload.py index 68b6a37bee..3d2ad3ab8e 100644 --- a/platformio/builder/tools/pioupload.py +++ b/platformio/builder/tools/pioupload.py @@ -222,14 +222,14 @@ def _format_availale_bytes(value, total): if data_max_size and data_size > data_max_size: memory_exceeded = True sys.stderr.write( - "Warning! The data size (%d bytes) is greater " - "than maximum allowed (%s bytes)\n" % (data_size, data_max_size) + "Error: Pre-allocated RAM usage (%d bytes) is greater " + "than RAM available (%s bytes)\n" % (data_size, data_max_size) ) if program_size > program_max_size: memory_exceeded = True sys.stderr.write( - "Error: The program size (%d bytes) is greater " - "than maximum allowed (%s bytes)\n" % (program_size, program_max_size) + "Error: Flash usage (%d bytes) is greater " + "than Flash available (%s bytes)\n" % (program_size, program_max_size) ) if memory_exceeded: env.Exit(1) From 59258c82d044d81887d0650f1537749bb27a3de9 Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:57:26 +0000 Subject: [PATCH 3/6] Add (minimum) ramfree option - commit 1 of 3 --- platformio/run/cli.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/platformio/run/cli.py b/platformio/run/cli.py index 1ed84a48b0..457ab68992 100644 --- a/platformio/run/cli.py +++ b/platformio/run/cli.py @@ -75,6 +75,17 @@ @click.option("--list-targets", is_flag=True) @click.option("-s", "--silent", is_flag=True) @click.option("-v", "--verbose", is_flag=True) +@click.option( + "-r", + "--ramfree", + type=int, + default=0, + help=( + "Specify the minimum number of bytes of RAM that must remain after pre-allocation. " + "Insufficient free ram can lead to crashes, reboots etc. when memory " + "becomes exhausted during dynamic allocation or stack usage." + ), +) @click.pass_context def cli( ctx, @@ -90,6 +101,7 @@ def cli( list_targets, silent, verbose, + ramfree, ): app.set_session_var("custom_project_conf", project_conf) @@ -154,6 +166,7 @@ def cli( is_test_running, silent, verbose, + ramfree, ) ) command_failed = any(r.get("succeeded") is False for r in results) @@ -186,6 +199,7 @@ def process_env( is_test_running, silent, verbose, + ramfree, ): if not is_test_running and not silent: print_processing_header(name, config, verbose) @@ -205,6 +219,7 @@ def process_env( program_args, silent, verbose, + ramfree, ).process() if result["succeeded"] and "monitor" in targets and "nobuild" not in targets: From a3f76e7a62e634703e8baef9276dee4546ff9960 Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:57:49 +0000 Subject: [PATCH 4/6] Add (minimum) ramfree option - commit 2 of 3 --- platformio/platform/_run.py | 1 + 1 file changed, 1 insertion(+) diff --git a/platformio/platform/_run.py b/platformio/platform/_run.py index 2912371dc9..f4f5a6d92a 100644 --- a/platformio/platform/_run.py +++ b/platformio/platform/_run.py @@ -80,6 +80,7 @@ def _run_scons(self, variables, targets, jobs): os.path.join(fs.get_source_dir(), "builder", "main.py"), ] args.append("PIOVERBOSE=%d" % int(self.verbose)) + args.append("RAMFREE=%d" % int(self.ramfree)) # pylint: disable=protected-access args.append("ISATTY=%d" % int(click._compat.isatty(sys.stdout))) # encode and append variables From 59aa012a0e9a620183c4371f3f328a133b2f0b3f Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:58:02 +0000 Subject: [PATCH 5/6] Add (minimum) ramfree option - commit 3 of 3 --- platformio/builder/tools/pioupload.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/platformio/builder/tools/pioupload.py b/platformio/builder/tools/pioupload.py index 3d2ad3ab8e..0ffaf3c18c 100644 --- a/platformio/builder/tools/pioupload.py +++ b/platformio/builder/tools/pioupload.py @@ -219,17 +219,24 @@ def _format_availale_bytes(value, total): print(output) memory_exceeded = False + ram_free = ARGUMENTS.get("RAMFREE", 0) if data_max_size and data_size > data_max_size: memory_exceeded = True sys.stderr.write( "Error: Pre-allocated RAM usage (%d bytes) is greater " - "than RAM available (%s bytes)\n" % (data_size, data_max_size) + "than RAM available (%d bytes)\n" % (data_size, data_max_size) + ) + elif ramfree and data_max_size and data_size + ram_free > data_max_size: + memory_exceeded = True + sys.stderr.write( + "Error: Pre-allocated RAM usage (%d bytes of %d total bytes) results in " + "less RAM available than minimum required (%d bytes)\n" % (data_size, data_max_size, ram_free) ) if program_size > program_max_size: memory_exceeded = True sys.stderr.write( "Error: Flash usage (%d bytes) is greater " - "than Flash available (%s bytes)\n" % (program_size, program_max_size) + "than Flash available (%d bytes)\n" % (program_size, program_max_size) ) if memory_exceeded: env.Exit(1) From abbb6852ea7e37404a9631311a799be70a94b216 Mon Sep 17 00:00:00 2001 From: Sophist <3001893+Sophist-UK@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:13:36 +0000 Subject: [PATCH 6/6] Tweak text --- platformio/builder/tools/pioupload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio/builder/tools/pioupload.py b/platformio/builder/tools/pioupload.py index 0ffaf3c18c..b871cce212 100644 --- a/platformio/builder/tools/pioupload.py +++ b/platformio/builder/tools/pioupload.py @@ -230,7 +230,7 @@ def _format_availale_bytes(value, total): memory_exceeded = True sys.stderr.write( "Error: Pre-allocated RAM usage (%d bytes of %d total bytes) results in " - "less RAM available than minimum required (%d bytes)\n" % (data_size, data_max_size, ram_free) + "less RAM remaining than minimum required (%d bytes)\n" % (data_size, data_max_size, ram_free) ) if program_size > program_max_size: memory_exceeded = True