-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Deno.memoryInfo() #6417
Add Deno.memoryInfo() #6417
Conversation
cli/ops/os.rs
Outdated
state.check_unstable("Deno.freemem"); | ||
state.check_env()?; | ||
match sys_info::mem_info() { | ||
Ok(info) => Ok(JsonOp::Sync(json!(info.free))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this op should be called mem_info instead and return the whole sys_info::MemInfo
to JavaScript?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It make sense. I made it return only free memory because in issue https://github.com/denoland/deno/issues/3802 it is related to Node.js os.freemem()
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could be added later in std's node compat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, now that memoryInfo()
returns all memory info the freemen()
function can be something like this:
function freemen(): number {
return memoryInfo().free;
}
If you think it could be fine I can add it.
cli/tests/unit/os_test.ts
Outdated
assertNotEquals(Deno.freemem(), ""); | ||
}); | ||
|
||
unitTest({ perms: { env: false } }, function freememPerm(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the env permission...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mee to. I just opted for the same permission used by other os
functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job at implementing this - it looks correct. Thanks for the work!
I wonder if the allow-env permission is what we want here.
Also I'm curious about why you want this op? What's your use-case?
No use case 😅 but let me explain. I wrote a Prometheus client for Deno (ts-prometheus) but when I started to add default metrics (process stats) I got that they aren't accessible from Deno cli. But I got confused and started to implement a freemem function for OT: I'm going to work on #5732 but I don't think there is an easy way to do it in Rust for every platform. |
That would be a welcomed effort. I think https://docs.rs/sys-info/0.7.0/sys_info/struct.MemInfo.html has everything you need? You can just modify this PR to expose |
This comment has been minimized.
This comment has been minimized.
@caspervonb Number has 53 bits of integer precision - that's about 9000 terabytes. That should be enough, I think... |
Waiting a decision about permissions I modified |
Errr.. yeah.... it's a double, was thinking 32bit for some reason 🤔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM; some very minor nits on naming, sorry did not catch that earlier. We should at-least try to avoid snake_case in public APIs and the test case could be a little bit stronger.
cli/js/lib.deno.unstable.d.ts
Outdated
export interface MemoryInfo { | ||
total: number; | ||
free: number; | ||
avail: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
available: number;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ops, I forgot it 😊
cli/js/lib.deno.unstable.d.ts
Outdated
avail: number; | ||
buffers: number; | ||
cached: number; | ||
swap_total: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swapTotal : number;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I am going to change it. Probably it would be better if the linter could raise an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would if the test used it, but seems like we don't lint type member names 🤔
cli/js/lib.deno.unstable.d.ts
Outdated
buffers: number; | ||
cached: number; | ||
swap_total: number; | ||
swap_free: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swapFree: number;
unitTest({ perms: { env: true } }, function freemem(): void { | ||
assertNotEquals(Deno.freemem(), ""); | ||
unitTest({ perms: { env: true } }, function memoryInfo(): void { | ||
assertNotEquals(Deno.memoryInfo(), {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const info = Deno.memoryInfo();
assert(info.total);
assert(info.free);
assert(info.avail);
// ... and so on to ensure that the fields are being serialized ;
// I believe the linter will object to `swap_total` and `swap_free` having underscores.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I honestly can't figure out whether it is right check every field. Because memory_info
function return an empty object when an error occurs. Otherwise it return a MemoryInfo
object and I expect its fields are properly set. Or they can be undefined
? I expect Rust code doesn't set fields to undefined but I'm honestly not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asserting n >= 0
should be fine.
In the exceptional case that it returns an error in Rust; an exception should be thrown in the JavaScript runtime as-well (e.g return an op-error).
By trying to implement {
"os": {
...
},
"process": {
...
}
} What do you think? |
@marcopacini sorry for slow review. Your suggestion with |
Superseded by #7350 |
Add
Deno.freemem()
that returns the amount of free memory as a number. Implemented usingsys-info::mem_info
.Related to https://github.com/denoland/deno/issues/3802