diff options
author | 2023-09-21 07:25:18 -0700 | |
---|---|---|
committer | 2023-09-21 07:25:18 -0700 | |
commit | d1e9b33cac65794632921045e7a851c58ef1863d (patch) | |
tree | efca05827034d73b0481f37f7209940e266b46e6 | |
parent | abfc10afeb73f9447e47929359d37f2b488c3c81 (diff) | |
download | bun-d1e9b33cac65794632921045e7a851c58ef1863d.tar.gz bun-d1e9b33cac65794632921045e7a851c58ef1863d.tar.zst bun-d1e9b33cac65794632921045e7a851c58ef1863d.zip |
On Linux, respect memory limit from cgroups (#5849)
* Implement `process.constrainedMemory()`
* Add a comment
* Handle max
* Missing header
* We can use WTF::ramSize now
* Update WebKit
* Update ZigGlobalObject.cpp
* WebKit
* :scissors:
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rwxr-xr-x | bun.lockb | bin | 74396 -> 74396 bytes | |||
-rw-r--r-- | docs/runtime/nodejs-apis.md | 2 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | packages/bun-types/globals.d.ts | 8 | ||||
m--------- | src/bun.js/WebKit | 0 | ||||
-rw-r--r-- | src/bun.js/bindings/Process.cpp | 11 | ||||
-rw-r--r-- | src/bun.js/bindings/Process.lut.h | 114 | ||||
-rw-r--r-- | test/js/node/process/process.test.js | 10 |
8 files changed, 89 insertions, 58 deletions
Binary files differ diff --git a/docs/runtime/nodejs-apis.md b/docs/runtime/nodejs-apis.md index 3897c8282..305bc0bdc 100644 --- a/docs/runtime/nodejs-apis.md +++ b/docs/runtime/nodejs-apis.md @@ -589,7 +589,7 @@ The table below lists all globals implemented by Node.js and Bun's current compa ### [`process`](https://nodejs.org/api/process.html) -🟡 Missing `process.allowedNodeEnvironmentFlags` `process.channel` `process.constrainedMemory()` `process.getActiveResourcesInfo/setActiveResourcesInfo()` `process.setuid/setgid/setegid/seteuid/setgroups()` `process.hasUncaughtExceptionCaptureCallback` `process.initGroups()` `process.report` `process.resourceUsage()`. `process.binding` is partially implemented. +🟡 Missing `process.allowedNodeEnvironmentFlags` `process.channel` `process.getActiveResourcesInfo/setActiveResourcesInfo()` `process.setuid/setgid/setegid/seteuid/setgroups()` `process.hasUncaughtExceptionCaptureCallback` `process.initGroups()` `process.report` `process.resourceUsage()`. `process.binding` is partially implemented. ### [`queueMicrotask()`](https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask) diff --git a/package.json b/package.json index 7cd3ad94e..5cc85f12a 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "@types/react": "^18.0.25", "@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/parser": "^5.31.0", - "bun-webkit": "0.0.1-4d995edbc44062b251be638818edcd88d7d14dd7" + "bun-webkit": "0.0.1-f8aa36d4737faee92bd2d1e337a2fd01a5437c61" }, "version": "0.0.0", "prettier": "./.prettierrc.cjs" diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 276b86303..a1f1bd325 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -725,6 +725,14 @@ interface Process { * @param listener The event handler function */ listenerCount(eventName: string | symbol, listener?: Function): number; + + /** + * Get the constrained memory size for the process. + * + * On Linux, this is the memory limit for the process, accounting for cgroups 1 and 2. + * On other operating systems, this returns `undefined`. + */ + constrainedMemory(): number | undefined; } interface MemoryUsageObject { diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit -Subproject 4d995edbc44062b251be638818edcd88d7d14dd +Subproject e1aa0a58e282b53fc20503d6e7ec93c621bc557 diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index e95268fb1..c952cbef2 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -1328,6 +1328,16 @@ static Process* getProcessObject(JSC::JSGlobalObject* lexicalGlobalObject, JSVal return process; } +JSC_DEFINE_HOST_FUNCTION(Process_functionConstrainedMemory, + (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ +#if OS(LINUX) || OS(FREEBSD) + return JSValue::encode(jsDoubleNumber(static_cast<double>(WTF::ramSize()))); +#else + return JSValue::encode(jsUndefined()); +#endif +} + JSC_DEFINE_HOST_FUNCTION(Process_functionCpuUsage, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { @@ -1864,6 +1874,7 @@ extern "C" void Process__emitDisconnectEvent(Zig::GlobalObject* global) chdir Process_functionChdir Function 1 config constructProcessConfigObject PropertyCallback connected processConnected CustomAccessor + constrainedMemory Process_functionConstrainedMemory Function 0 cpuUsage Process_functionCpuUsage Function 1 cwd Process_functionCwd Function 1 debugPort processDebugPort CustomAccessor diff --git a/src/bun.js/bindings/Process.lut.h b/src/bun.js/bindings/Process.lut.h index 620acdb64..6016466c2 100644 --- a/src/bun.js/bindings/Process.lut.h +++ b/src/bun.js/bindings/Process.lut.h @@ -1,6 +1,6 @@ // File generated via `make static-hash-table` / `make cpp` -static const struct CompactHashIndex processObjectTableIndex[267] = { - { 47, -1 }, +static const struct CompactHashIndex processObjectTableIndex[268] = { + { 48, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -14,9 +14,9 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 20, -1 }, + { 21, -1 }, { -1, -1 }, - { 49, -1 }, + { 50, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -41,14 +41,14 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 34, -1 }, + { 35, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 29, -1 }, - { 13, -1 }, + { 30, -1 }, + { 14, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -57,11 +57,11 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 59, -1 }, + { 60, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 55, -1 }, + { 56, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -75,36 +75,36 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 43, -1 }, + { 44, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 0, -1 }, - { 28, -1 }, - { 37, -1 }, - { 42, -1 }, + { 29, -1 }, + { 38, -1 }, + { 43, -1 }, { -1, -1 }, - { 25, -1 }, - { 12, -1 }, + { 26, -1 }, + { 13, -1 }, { -1, -1 }, { -1, -1 }, - { 62, -1 }, + { 63, -1 }, { -1, -1 }, { -1, -1 }, - { 33, -1 }, - { 44, -1 }, + { 34, -1 }, + { 45, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 26, -1 }, + { 27, -1 }, { -1, -1 }, { -1, -1 }, - { 22, -1 }, + { 23, -1 }, { -1, -1 }, { 5, -1 }, { -1, -1 }, - { 64, -1 }, + { 65, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -115,19 +115,19 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 27, 261 }, + { 28, 262 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 23, 262 }, + { 24, 263 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 50, 265 }, + { 51, 266 }, { -1, -1 }, - { 19, -1 }, + { 20, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -139,10 +139,10 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 17, 257 }, + { 18, 257 }, { -1, -1 }, - { 14, -1 }, - { 57, -1 }, + { 15, -1 }, + { 58, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -161,12 +161,12 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 3, 266 }, + { 3, 267 }, { 1, -1 }, { -1, -1 }, - { 63, -1 }, + { 64, -1 }, { -1, -1 }, - { 11, -1 }, + { 12, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -174,24 +174,24 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 56, -1 }, + { 57, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 10, 256 }, { -1, -1 }, - { 16, 263 }, + { 17, 264 }, { -1, -1 }, - { 39, -1 }, + { 40, -1 }, { -1, -1 }, - { 41, -1 }, + { 42, -1 }, { -1, -1 }, - { 38, -1 }, - { 6, 264 }, + { 11, 260 }, + { 6, 265 }, { -1, -1 }, { -1, -1 }, { 4, -1 }, - { 51, -1 }, + { 52, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -205,11 +205,11 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 31, 260 }, + { 32, 261 }, { -1, -1 }, { -1, -1 }, - { 48, -1 }, - { 18, 258 }, + { 49, -1 }, + { 19, 258 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -219,10 +219,10 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 53, -1 }, + { 54, -1 }, { -1, -1 }, - { 32, -1 }, - { 24, -1 }, + { 33, -1 }, + { 25, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -233,12 +233,12 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 52, -1 }, + { 53, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 15, 259 }, + { 16, 259 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, @@ -256,20 +256,21 @@ static const struct CompactHashIndex processObjectTableIndex[267] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, - { 21, -1 }, - { 30, -1 }, - { 35, -1 }, + { 22, -1 }, + { 31, -1 }, { 36, -1 }, - { 40, -1 }, - { 45, -1 }, + { 37, -1 }, + { 39, -1 }, + { 41, -1 }, { 46, -1 }, - { 54, -1 }, - { 58, -1 }, - { 60, -1 }, + { 47, -1 }, + { 55, -1 }, + { 59, -1 }, { 61, -1 }, + { 62, -1 }, }; -static const struct HashTableValue processObjectTableValues[65] = { +static const struct HashTableValue processObjectTableValues[66] = { { "abort"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionAbort, 1 } }, { "allowedNodeEnvironmentFlags"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, Process_stubEmptySet } }, { "arch"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructArch } }, @@ -281,6 +282,7 @@ static const struct HashTableValue processObjectTableValues[65] = { { "chdir"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionChdir, 1 } }, { "config"_s, static_cast<unsigned>(PropertyAttribute::PropertyCallback), NoIntrinsic, { HashTableValue::LazyPropertyType, constructProcessConfigObject } }, { "connected"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processConnected, setProcessConnected } }, + { "constrainedMemory"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionConstrainedMemory, 0 } }, { "cpuUsage"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionCpuUsage, 1 } }, { "cwd"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, Process_functionCwd, 1 } }, { "debugPort"_s, static_cast<unsigned>(PropertyAttribute::CustomAccessor), NoIntrinsic, { HashTableValue::GetterSetterType, processDebugPort, setProcessDebugPort } }, @@ -338,4 +340,4 @@ static const struct HashTableValue processObjectTableValues[65] = { }; static const struct HashTable processObjectTable = - { 65, 255, true, nullptr, processObjectTableValues, processObjectTableIndex }; + { 66, 255, true, nullptr, processObjectTableValues, processObjectTableIndex }; diff --git a/test/js/node/process/process.test.js b/test/js/node/process/process.test.js index dc1081087..a4a8862a2 100644 --- a/test/js/node/process/process.test.js +++ b/test/js/node/process/process.test.js @@ -487,3 +487,13 @@ it("dlopen args parsing", () => { expect(() => process.dlopen({ module: { exports: Symbol("123") } }, "/tmp/not-found.so")).toThrow(); expect(() => process.dlopen({ module: { exports: Symbol("123") } }, Symbol("badddd"))).toThrow(); }); + +it("process.constrainedMemory()", () => { + if (process.platform === "linux") { + // On Linux, it returns 0 if the kernel doesn't support it + expect(process.constrainedMemory() >= 0).toBe(true); + } else { + // On unsupported platforms, it returns undefined + expect(process.constrainedMemory()).toBeUndefined(); + } +}); |