Repository: Phala-Network/phala-blockchain (language: rust)

Issue:
pruntime: unexpected memory usage increase
As I mentioned in https://github.com/Phala-Network/phala-blockchain/pull/1171, we saw an unexpected memory usage increase during the pruntime v2 testing.
![image](https://user-images.githubusercontent.com/6442159/219989823-035ef2ff-e5e4-4d47-a594-f5fd6022e7ea.png)
As shown in the picture, the peak memory usage increased significantly at the height of approximately 3.3M. However, the rust peak did not show any fluctuation at that point.

After some further investigating, I found there should be a bug in gramine that misreported the total memory usage. An [issue](https://github.com/gramineproject/gramine/issues/1213) was submitted to the upstream. Let's wait for the upstream fix.



Discussion / hints:

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 5a5c24100f..3cddbcac35 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7524,6 +7524,7 @@ dependencies = [
  "im",
  "insta",
  "itertools",
+ "libc",
  "log",
  "num-bigint 0.4.3",
  "num-traits",
diff --git a/crates/phactory/Cargo.toml b/crates/phactory/Cargo.toml
index f8b14320b4..a8f4d5314e 100644
--- a/crates/phactory/Cargo.toml
+++ b/crates/phactory/Cargo.toml
@@ -82,6 +82,7 @@ phala-scheduler = { path = "../phala-scheduler" }
 sgx-api-lite = { path = "../sgx-api-lite" }
 reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "socks"] }
 reqwest-env-proxy = { path = "../reqwest-env-proxy" }
+libc = "0.2"
 
 environmental = "1"
 once_cell = "1"
diff --git a/crates/phactory/src/prpc_service.rs b/crates/phactory/src/prpc_service.rs
index 861699be91..a8222e3e2a 100644
--- a/crates/phactory/src/prpc_service.rs
+++ b/crates/phactory/src/prpc_service.rs
@@ -1275,7 +1275,9 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> PhactoryApi for Rpc
     /// Get basic information about Phactory state.
     async fn get_info(&mut self, _request: ()) -> RpcResult<pb::PhactoryInfo> {
         let info = self.lock_phactory(true, true)?.get_info();
-        info!("Got info: {:?}", info.debug_info());
+        info!("Got info: {:?} mallinfo: {:?}", info.debug_info(), unsafe {
+            libc::mallinfo()
+        });
         Ok(info)
     }
 
diff --git a/standalone/pruntime/Cargo.lock b/standalone/pruntime/Cargo.lock
index 76db3363eb..4690df849b 100644
--- a/standalone/pruntime/Cargo.lock
+++ b/standalone/pruntime/Cargo.lock
@@ -4284,6 +4284,7 @@ dependencies = [
  "hex_fmt",
  "im",
  "itertools",
+ "libc",
  "log",
  "num-bigint",
  "num-traits",
diff --git a/standalone/pruntime/gramine-build/pruntime.manifest.template b/standalone/pruntime/gramine-build/pruntime.manifest.template
index 228acd60ec..60ff93c87f 100644
--- a/standalone/pruntime/gramine-build/pruntime.manifest.template
+++ b/standalone/pruntime/gramine-build/pruntime.manifest.template
@@ -16,11 +16,14 @@ insecure__use_cmdline_argv = true
 #}
 insecure__allow_eventfd = true
 stack.size = "1M"
+brk.max_size = "256M"
 
 [loader.env]
 LD_LIBRARY_PATH = "/lib:/lib/x86_64-linux-gnu"
 {# Without this, Glibc would allocate a 64MB ARENA for each thread. #}
 MALLOC_ARENA_MAX = "1"
+{# Chunks over 256K would alloc with mmap. #}
+MALLOC_MMAP_THRESHOLD_ = "262144"
 {#
 The size of the thread pool for the tokio runtime created by rocket that
 serving the async tasks.


Test patch: