Add memory_estimation function for estimating shm usage
This makes it so that we can actually test that all of our shared memory
channels can fit in the shared memory actually available on a machine.
Change-Id: I348c02bafb6de94413da94c2f591019db4aee463
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/ipc_lib/BUILD b/aos/ipc_lib/BUILD
index 75bbd4d..bae23b8 100644
--- a/aos/ipc_lib/BUILD
+++ b/aos/ipc_lib/BUILD
@@ -445,3 +445,14 @@
"@com_github_gflags_gflags//:gflags",
],
)
+
+cc_library(
+ name = "memory_estimation",
+ srcs = ["memory_estimation.cc"],
+ hdrs = ["memory_estimation.h"],
+ visibility = ["//visibility:public"],
+ deps = [
+ ":lockless_queue",
+ "//aos:configuration",
+ ],
+)
diff --git a/aos/ipc_lib/memory_estimation.cc b/aos/ipc_lib/memory_estimation.cc
new file mode 100644
index 0000000..d193c75
--- /dev/null
+++ b/aos/ipc_lib/memory_estimation.cc
@@ -0,0 +1,16 @@
+#include "aos/ipc_lib/memory_estimation.h"
+
+#include "aos/ipc_lib/memory_mapped_queue.h"
+namespace aos::ipc_lib {
+size_t TotalSharedMemoryUsage(const aos::Configuration *config,
+ const aos::Node *node) {
+ size_t total_size = 0;
+ for (const aos::Channel *channel : *CHECK_NOTNULL(config->channels())) {
+ if (aos::configuration::ChannelIsReadableOnNode(channel, node)) {
+ total_size +=
+ LocklessQueueMemorySize(MakeQueueConfiguration(config, channel));
+ }
+ }
+ return total_size;
+}
+} // namespace aos::ipc_lib
diff --git a/aos/ipc_lib/memory_estimation.h b/aos/ipc_lib/memory_estimation.h
new file mode 100644
index 0000000..31eafc1
--- /dev/null
+++ b/aos/ipc_lib/memory_estimation.h
@@ -0,0 +1,13 @@
+#ifndef AOS_IPC_LIB_MEMORY_ESTIMATION_H_
+#define AOS_IPC_LIB_MEMORY_ESTIMATION_H_
+
+#include "aos/configuration.h"
+
+namespace aos::ipc_lib {
+// Returns the total shared memory that will be used by the specified config on
+// the specified node, in bytes.
+size_t TotalSharedMemoryUsage(const aos::Configuration *config,
+ const aos::Node *node);
+} // namespace aos::ipc_lib
+
+#endif // AOS_IPC_LIB_MEMORY_ESTIMATION_H_