Skip to content
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

Attempt to "Fix" OpenBSD Build #1277

Closed
wants to merge 1 commit into from

Conversation

time-killer-games
Copy link
Contributor

@time-killer-games time-killer-games commented Aug 8, 2023

Fixes #1276

I will need someone to do additional research and make sure this is correct as I'd like to not be responsible for further breakage to what is already borked.

Now unused/free RAM matches OpenBSD top(1) and the following two code examples:

 int mib[2];
 mib[0] = CTL_VM;
 mib[1] = VM_UVMEXP;
 struct uvmexp buf;
 std::size_t sz = sizeof(buf);
 if (!sysctl(mib, 2, &buf, &sz, nullptr, 0))
   freeram = buf.free * sysconf(_SC_PAGESIZE);
freeram = (sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE));

htop

On the left is my example program. On the right is htop from this pr. Note my example program rounds up to the nearest 0.01 and htop does not. Although it's probably wrong I removed the code logic for getting the cached memory completely.

openbsd/OpenBSDMachine.c Outdated Show resolved Hide resolved
@@ -92,11 +92,12 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
char errbuf[_POSIX2_LINE_MAX];

OpenBSDMachine* this = xCalloc(1, sizeof(OpenBSDMachine));
Machine* super = &this->super;
Machine* super = (Machine*)&this->super;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the error message for this? this->super should be of type Machine in openbsd/OpenBSDMachine.h.

openbsd/OpenBSDMachine.c Outdated Show resolved Hide resolved
@@ -129,7 +130,7 @@ void Machine_delete(Machine* super) {
}

static void OpenBSDMachine_scanMemoryInfo(OpenBSDMachine* this) {
Machine* host = &this->super;
Machine* super = (Machine*)&this->super;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no cast necessary. Please provide build message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc it was saying there was a cast needed, which is odd. I didn't think that was necessary in C.

I'll revert the casts and show you the errors I'm getting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, i see now; they were warnings, not errors. In that case I'll revert that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you post the warnings you are getting? Just to check if there's anything funny going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was asleep. Getting that information now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same warning that can be found in other sources I did not modify, such as this one:

OpenBSDProcessList.c:41:11: warning: incompatible pointer types returning 'OpenBSDProcessList *' (aka 'struct OpenBSDProcessList_ *') from a function with result type 'ProcessList *' (aka 'struct ProcessList_ *') [-Wincompatible-pointer-types]

There seems to be 3 other instances of this warning:

OpenBSDMachine.c:117:11: warning: incompatible pointer types returning 'OpenBSDMachine *' (aka 'struct OpenBSDMachine_ *') from a function with result type 'Machine *' (aka 'struct Machine_ *') [-Wincompatible-pointer-types]
OpenBSDMachine.c:121:20: warning: incompatible pointer types initializing 'OpenBSDMachine *' (aka 'struct OpenBSDMachine_ *') with an expression of type 'Machine *' (aka 'struct Machine_ *') [-Wincompatible-pointer-types]
OpenBSDMachine.c:265:20: warning: incompatible pointer types initializing 'OpenBSDMachine *' (aka 'struct OpenBSDMachine_ *') with an expression of type 'Machine *' (aka 'struct Machine_ *') [-Wincompatible-pointer-types]

Comment on lines 143 to 153

// Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9)
const int bcache_mib[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT };
struct bcachestats bcstats;
size_t size_bcstats = sizeof(bcstats);

if (sysctl(bcache_mib, 3, &bcstats, &size_bcstats, NULL, 0) < 0) {
CRT_fatalError("cannot get vfs.bcachestat");
}

super->cachedMem = bcstats.numbufpages * this->pageSizeKB;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this part of the code removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what purpose the cached memory served, as I wasn't seeing that printed to the terminal as it's own stat in specific, although it does effect the amount of used ram reported, which is part of what made it not match up with top(1).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There'S a later place in the code that calculates the used memory shown in the Memory meter. Also the cached memory (i.e. file system cache) is not directly printed there, but shown as blue (file buffers/shared memory) or brown (volatile file system cache).

htop tries to track some more resources separately, thus things might seem unintuitive at first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you consider the used/free memory not matching top(1) to be intentional? If not, we will need to fix that, otherwise, this code can be put back and not much else needs changing at this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intended display is used+compressed+buffers+shared for the used value in the memory bar; the cached value is ignored intentionally, as that's memory that can usually be reclaimed by the kernel if needed. And although cached resides in RAM, it's not usually considered as "used" for htop due to this volatile nature.

used: Memory used by programs, libraries, data
compressed: Memory compressed by te kernel to avoid swapping (e.g. zram)
buffers: File buffers & dirty pages for I/O
shared: Shared memory segments, memory used e.g. by tmpfs
cached: volatile file system cache (e.g. recently read data to optimize disk access)

Example from Linux:
image
Although there's practically no memory unused by Linux, the meter declares the more reasonable value of ~20GiB of memory that cannot easily be dropped, but would need to be placed somewhere else if some new program wanted to use too much memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do want to make this a different pull request so this doesn't get conflated, but I think it would be good if NetBSD used code which was more in line with what OpenBSD is doing with the sysctl(), VM_UVMEXP, and swapctl(). NetBSD could be using near identical code for these things to reduce needed maintenance, but instead it is using sysctl() and the VM_UVMEXP2 struct, which is like the VM_UVMEXP struct but also covers what OpenBSD is doing to get swap related info with swapctl(). We'll cross that bridge when we get there, perhaps I'll link to this comment from my new pull request once this pull request is done.

Copy link
Contributor Author

@time-killer-games time-killer-games Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenBE I almost forgot about this part; did you want me to add back the code logic I took out here? Again, doing this will make it not print the correct amount of ram and it will no longer align with what OpenBSD's stock top cli prints for ram.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this change drops initializing the super->cachedMem member of the Machine structure, I'm inclined to have this block of code stay or be updated and follow the semantics used within htop.

The memory metrics in htop mostly fall into 4 categories (simplified):

  1. used: RAM used by a program for working memory, code or other resources
  2. shared: RAM shared between processes, like shmem or mmap'd files
  3. buffers: RAM allocated for write-back buffers of the filesystem
  4. caches: RAM used as filesystem (read) cache that could be dropped at any moment

Of those categories 1 and 2 are subject to potential swapping, while 4 is (mostly) never swapped. Category 3 is basically what you'd first write to disk, if you were to need some more free RAM.

This is much simplified, and differs slightly from what top displays regarding the handling of shared memory to accommodate for things like tmpfs/ramfs on Linux.

AFAICS, the code that was removed should have worked in this regard and if there's a need for that code to change I'd recommend to split out that change as a separate PR to separate build fixes from behavioral changes.

@BenBE BenBE added bug 🐛 Something isn't working build system 🔧 Affects the build system rather then the user experience BSD 🐡 Issues related to *BSD labels Aug 8, 2023
@BenBE BenBE added this to the 3.3.0 milestone Aug 8, 2023
@BenBE
Copy link
Member

BenBE commented Aug 8, 2023

Please keep behavior changes separate from build fixes.

Can go in with one single PR, but should be distinct commits. If necessary you may also group the build fixes as necessary. TIA.

@time-killer-games
Copy link
Contributor Author

i clicked commit on that by mistake i meant to revert them all at once.

@@ -273,7 +262,7 @@ static void OpenBSDMachine_scanCPUTime(OpenBSDMachine* this) {
}

void Machine_scan(Machine* super) {
OpenBSDMachine* this = (OpenBSDMachine*) super;
OpenBSDMachine* this = super;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this cast was here initially. Let me know if you want me to add it back. Though removing it didn't mess up the build. I might redo this pr from scratch if i mess up too much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule usually is casting from generic to specific (platform) type; no casting from platform type to generic (use ->super instead).

Also: Feel free to rebase/squash the commits to your hearts content, until they look clean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I'm done making all my changes locally to how we want the final result to look, i will create a new pr, close this one, and break it up into a couple commits, ones that have code changes related to one another in how it is broken up, without the excess in extra commits. Is that ok?

In the meantime, feel free to let me know how you want the commits broken up here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please instead of opening a new PR, just split the changes into commits as appropriate and push them (--force-with-lease) on the current branch.

Regarding splitting the commits:

  • One commit (or more, if there are vastly distinct build issues) for compile issues
  • One commit fixing the memory detection (see my other comment regarding that)

Apart from that, providing descriptive commit messages and following the information outlined in the style guide is much appreciated.

@BenBE BenBE added the OpenBSD 🐡 OpenBSD related issues label Aug 8, 2023
natoscott added a commit that referenced this pull request Aug 30, 2023
@natoscott
Copy link
Member

@time-killer-games I've merged the build-fix part of this PR, however lots of other changes have also been made in the meantime. Could you see if we have a clean OpenBSD build now please? Any issues, please let me know - thanks!

@time-killer-games
Copy link
Contributor Author

time-killer-games commented Sep 20, 2023

@time-killer-games I've merged the build-fix part of this PR, however lots of other changes have also been made in the meantime. Could you see if we have a clean OpenBSD build now please? Any issues, please let me know - thanks!

Without going too far into details my probation officer has taken all my devices except my phone. I don't know when she will let me have my stuff again, but hopefully soon so i can do this for you. I apologize for the inconvenience.

@BenBE
Copy link
Member

BenBE commented Dec 31, 2023

What's the status of this?

@BenBE
Copy link
Member

BenBE commented Jan 2, 2024

@fraggerfox What about cross-checking this PR for #1360? They seem to address similar issues.

@time-killer-games
Copy link
Contributor Author

time-killer-games commented Jan 5, 2024

@time-killer-games I've merged the build-fix part of this PR, however lots of other changes have also been made in the meantime. Could you see if we have a clean OpenBSD build now please? Any issues, please let me know - thanks!

I am going to attempt to resolve conflicts and we will go from there.

Edit: There was only one small conflict, so it wasn't as bad as I thought.

That said, I am going to test this pr again just to be sure nothing broke.

@time-killer-games
Copy link
Contributor Author

time-killer-games commented Jan 5, 2024

Alright, tried building in the current state, and got these errors:

openbsd/OpenBSDProcessTable.c:48:55: error: use of undeclared identifier 'super'
OpenBSDProcessTable* this = (OpenBSDProcessTable*) super;
^
openbsd/OpenBSDProcessTable.c:47:34: warning: unused parameter 'cast' [-Wunused-parameter]
void ProcessTable_delete(Object* cast) {
^
openbsd/OpenBSDProcessTable.c:133:32: error: no member named 'host' in 'struct ProcessTable_'
Machine* host = this->super.host;
~~~~~~~~~~~ ^
2 warnings and 2 errors generated.
*** Error 1 in . (Makefile:1549 'openbsd/OpenBSDProcessTable.o')
*** Error 2 in /home/openbsd/htop (Makefile:1095 'all')

I have no clue what to do now.

@BenBE
Copy link
Member

BenBE commented Jan 5, 2024

The first error is with the cast needing cast instead of super. This is given that you get Object * (any parent class) instead of the direct parent (which would be named super).

This second one is inheritance-related as that field in question moved one more parent up in the hierarchy (this->super.super.host, unchecked). There will likely be a similar check in #1360. Also compare with other platforms like in e.g. linux/LinuxProcessTable.c and their handling of this kind of access.

@time-killer-games
Copy link
Contributor Author

lol the needing of cast instead of super was rather obvious. I think it didn't occur to me at first because I never bothered looking up where that error was located in the actual code.

Fixing these things now. Will report back if everything worked.

@BenBE
Copy link
Member

BenBE commented Jan 5, 2024

Please do not merge, but rather rebase your changes onto the latest commits from main.

@time-killer-games
Copy link
Contributor Author

I think, unless I'm sadly mistaken, we're all set! :)

Screenshot_2024-01-05_12-17-39

@time-killer-games
Copy link
Contributor Author

Please do not merge, but rather rebase your changes onto the latest commits from main.

Taking care of it now.

@time-killer-games
Copy link
Contributor Author

@BenBE is it ok if i open a new pull request and close this one, to reduce the number of commits down to one commit? I ask because I am having a bit of trouble rebasing, I'm not very good with git and I'm concerned I might mess this up.

If so, what should be the title and description of the commit and name of the branch?

@BenBE
Copy link
Member

BenBE commented Jan 5, 2024

Preferably things should be resolved in this PR as it minimizes clutter in the issue tracker. If you really need to open a new one you should mark the new one to superseed this one, cross-link both issues accordingly so people can follow up in the new one.

I did a rebase+squash of your changes (as present on this PR). Please recheck. HTH.

In case you want to get a bit more familiar with how git works, there's a nice game called Oh My Git that explains many of the basics and some advanced topics.

@time-killer-games
Copy link
Contributor Author

time-killer-games commented Jan 6, 2024

I think after you go over my two recent review comments with your thoughts, and we address those things, we should be all set. @BenBE I tested and verified it still works as intended imo.

Screenshot_2024-01-05_17-40-29

Comparing htop with the output from my own cli ween / sysinfo from:

https://github.com/time-killer-games/ween

Co-authored-by: Benny Baumann <[email protected]>
@time-killer-games
Copy link
Contributor Author

time-killer-games commented Jan 6, 2024

@BenBE I've rebased well over a dozen times and every time i do it, it ruins all the code in the pull request and edits the code drastically in ways I'm not telling it to. Either we do this pr from scratch, or i give up. I ruined my entire night trying to figure out git, and never had a more terrible experience.

@BenBE
Copy link
Member

BenBE commented Jan 6, 2024

Superseeded by #1361

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSD 🐡 Issues related to *BSD bug 🐛 Something isn't working build system 🔧 Affects the build system rather then the user experience OpenBSD 🐡 OpenBSD related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenBSD 7.3 htop showing wrong amount of RAM in use.
3 participants