Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
fork
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3  Next  
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3203

PostPosted: Tue Sep 26, 2023 7:49 pm    Post subject: Reply with quote

He was referring to emerge hogging all resources during an update. AKA the most common cause of Gentoo machines getting hit by the killer.
Basically "make user's applications nice to the system instead of having system forcefully put them back in their place"
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9714
Location: almost Mile High in the USA

PostPosted: Tue Sep 26, 2023 10:57 pm    Post subject: Reply with quote

correct, this is a userspace problem at least for the most common reason for oom-killer for killing Gentoo users applications.
I suspect portage builds is the #1 consumer of memory and cpu cycles for an average Gentoo user. I don't think I have many other software that uses so many cpu cycles or memory.

And yes I was assuming people knew I meant make = ninja or waf of course :p No, really, it was generic to any build scheduling system. Most of them don't even care about system status and assume the OS will take care of it, while really the userland scheduler knows more about the nature of the jobs than the OS does. However the OS better return accurate status information to the build system to make decisions.
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Wed Sep 27, 2023 7:23 am    Post subject: Reply with quote

grknight wrote:



where does the kernel store the most accurate data on the amount of free RAM?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Wed Sep 27, 2023 10:48 am    Post subject: Reply with quote

eccerr0r wrote:


do you know?
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9714
Location: almost Mile High in the USA

PostPosted: Wed Sep 27, 2023 3:09 pm    Post subject: Reply with quote

Of course /proc/meminfo contains latest kernel memory information but you still need to consider how much cache/buffers are really needed to be kept.
But this is not the only part of the equation, knowing your own size/how much you expect to use, not just memory but also cpu and disk i/o are also important.
Kernel reporting of cpu utilization is tricky because if a process doesn't use the whole time slot and something else uses it, how should this be counted? What if the process creates a LWT, should it be counted as 1 or 2 processes? What if it's part of a cgroup, should that be counted?

Even a userland scheduler still has problems knowing exactly what each subprocess will do. Like if the build script requests to compile foo.c, how much memory will it take? It likely won't know either. Plus if it needs to run rust or something that's inherently multithreaded, it should NOT try to schedule more than 1 or 2 jobs at a time as it's unneeded except when dist-rust comes around...
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Sep 28, 2023 12:06 pm    Post subject: Reply with quote

eccerr0r wrote:



in the process of understanding the work of the kernel, I will try to figure it out. to begin with, tell me exactly from which file the system reads about the size of free memory? what exactly is the name of this variable in the kernel code? for example, I opened the source kernel, and I want to get the state of free memory during writing my code, from which variable should I read it?
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4439
Location: Bavaria

PostPosted: Thu Sep 28, 2023 12:56 pm    Post subject: Reply with quote

Gentoopc wrote:
[...] exactly from which file the system reads about the size of free memory? what exactly is the name of this variable in the kernel code? [...]

Maybe we dont have such a kernel expert who knows this deep internal kernel routines. If I would search for this, I would start with the Memory Management Subsystem (mm).

Interesting / Starting point could be:

/usr/src/linux/mm/memcontrol.c

because you have here:
Quote:
static const struct memory_stat memory_stats[] = {


together with

/usr/src/linux/include/linux/mm.h
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Thu Sep 28, 2023 2:39 pm    Post subject: Reply with quote

Gentoopc wrote:
tell me exactly from which file the system reads about the size of free memory?

"exactly", "mm/show_mm.c" is the source code that perform getting "MemAvailable".

However if you question "from which file the system reads about the size of free memory" mean how kernel know free memory size, then first kernel does not read from any "file" to get this information. it is runtime calculated and stored in a fixed location in kernel memory address space. The function that perform this calculation is "long si_mem_available(void)"

Gentoopc wrote:
what exactly is the name of this variable in the kernel code? for example, I opened the source kernel, and I want to get the state of free memory during writing my code, from which variable should I read it?


The "variable" name is "available" in fs/proc/meminfo.c in function "static int meminfo_proc_show(struct seq_file *m, void *v)"

Finally care to share where "my code" reside? A source code you will link into kernel? A source code you will link into a program that will "fork" by kernel?

Curious mind really want to understand the logic behind all these questions.
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3203

PostPosted: Thu Sep 28, 2023 3:07 pm    Post subject: Reply with quote

What are you trying to do by hacking kernel?
If you want to change behavior of some application based on free memory, why not just read it from /proc/meminfo instead of dealing with kernel's internal variables?
BTW, this is also how free gets this data before printing it in your terminal. You can find it running free under strace like this:
Code:
strace free |& grep open
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Fri Sep 29, 2023 12:31 pm    Post subject: Reply with quote

szatox wrote:
What are you trying to do by hacking kernel?


I'm trying to give the kernel at least some logic. this is a big word, but it's just an attempt by an ordinary user. I'm not an expert, I just do what I can for myself.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Fri Sep 29, 2023 12:38 pm    Post subject: Reply with quote

pingtoo wrote:


I need to get the size of the available RAM first, because fork() will depend on it. is it possible in the kernel to get this from grub ? for example. or from the bios. that is, I have to do it before the kernel starts.
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Fri Sep 29, 2023 12:51 pm    Post subject: Reply with quote

Gentoopc wrote:
pingtoo wrote:


I need to get the size of the available RAM first, because fork() will depend on it. is it possible in the kernel to get this from grub ? for example. or from the bios. that is, I have to do it before the kernel starts.


Usually boot loader (grub, as example) do pass this information to kernel. However once kernel started the boot loader no longer in memory, so it is not possible to get it from boot loader.

"I have to do it before the kernel starts", this sentence make me thinking you want to control available memory, Is my guessing correct?
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9714
Location: almost Mile High in the USA

PostPosted: Fri Sep 29, 2023 2:00 pm    Post subject: Reply with quote

Not sure why you want hardware memory, really all that matters is how much memory is available at that instant. If you have a firefox process hogging half your memory and your build is hogging the other half, is it a good idea to fork another process that might consume another GiB of RAM even if BIOS said you have 64GiB RAM?
Well in this case if you have swap available, yes it's fine but if not, no. But if you had 2GiB you may be in trouble from the get go.

All of this data is in /proc/meminfo anyway subtracting out kernel memory which you shouldn't bank on anyway.
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Sep 30, 2023 11:15 am    Post subject: Reply with quote

Gentoopc wrote:


"I have to do it before the kernel starts", this sentence make me thinking you want to control available memory, Is my guessing correct?



No, that's not true. I don't want to control RAM, I just need to monitor it. knowing the depth of the process tree and the size of the available RAM, it will be possible to abandon the tables in the kernel and killer() because it is simply no longer needed.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54453
Location: 56N 3W

PostPosted: Sat Sep 30, 2023 11:22 am    Post subject: Reply with quote

Gentoopc,

I don't quite follow the logic here.

You allow fork to run.
The new process dynamically allocates a chunk of RAM.
Now you no longer have enough RAM to keep the kernel alive.
The OOM cannot run so the kernel panics.

What did I miss?
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Sep 30, 2023 11:50 am    Post subject: Reply with quote

eccerr0r wrote:



a simple example. this is simplistic, but if you suddenly wanted to slow down the process creation functions by 89% when filling RAM. and you write the condition that

Code:

available_RAM

total_amount_of_RAM= (  (total_amount_of RAM/100)* 11 )


if(available_RAM<total_amount_of_RAM) {                   };



for example, in order for the body of this condition to work for the very first time, even at the start of the kernel of the very first in the queue, you need to know the RAM size. let's say this condition will be checked before the entire kernel is running. therefore, to calculate it, you need to get it from outside. well, this is an example.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54453
Location: 56N 3W

PostPosted: Sat Sep 30, 2023 12:00 pm    Post subject: Reply with quote

Gentoopc,

That's my point expressed in a different way.

How do you know, in advance of the fork, how much RAM the process needs to know if it should be allowed or not?

The OOM is a last ditch thing called by the kernel when it cannot get the RAM it needs to keep working.
The choice it faces, is to kill a user process or panic.

I can see this heading towards a very unkind description of early versions of Windows.
It only ever used half of RAM and kept the other half in case it was needed.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Sep 30, 2023 12:00 pm    Post subject: Reply with quote

NeddySeagoon wrote:
Gentoopc,

I don't quite follow the logic here.

You allow fork to run.
The new process dynamically allocates a chunk of RAM.
Now you no longer have enough RAM to keep the kernel alive.
The OOM cannot run so the kernel panics.

What did I miss?


you missed the fact that I will not kill the processes, they will be terminated, not killed, but completed by returning everything necessary to the processes that made them happy. it's like the work of a recursive function when it returns values. the size of RAM less than 11% will, for example, be a condition for how the recursion of the function begins to exit the recursion. well, this is so that you understand. you don't kill the recursive killer() function, it always works for you with the right approach and conditions. the same can be done with the work of fork(). this is just an example. As I said before.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Sep 30, 2023 12:06 pm    Post subject: Reply with quote

NeddySeagoon wrote:
Gentoopc,

That's my point expressed in a different way.

How do you know, in advance of the fork, how much RAM the process needs to know if it should be allowed or not?

The OOM is a last ditch thing called by the kernel when it cannot get the RAM it needs to keep working.
The choice it faces, is to kill a user process or panic.

I can see this heading towards a very unkind description of early versions of Windows.
It only ever used half of RAM and kept the other half in case it was needed.



fork() and other functions of the waiting processes will work without restriction until the moment when the RAM remains less than 11%. when this threshold is exceeded, the longest branch of the process tree will begin to roll back. the depth of the tree is not difficult to measure. when it roll back , it will return the necessary data in order to terminate other processes waiting for this data and free up RAM. the longest branch because it means there are already a lot of process waiting for it and cannot be completed.
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9714
Location: almost Mile High in the USA

PostPosted: Sat Sep 30, 2023 1:21 pm    Post subject: Reply with quote

also keep in mind unused ram is wasted ram.

fork() does not use much ram, it's what that program runs in that fork, usually that exec(). If it's just running a shell or something I'd prefer that it just runs that shell instead of what, hanging because there *might* not be enough ram because it might spawn a g++ on a 2GB process?
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1000
Location: Richmond Hill, Canada

PostPosted: Sat Sep 30, 2023 3:40 pm    Post subject: Reply with quote

So this is really a kernel process scheduling design exercise.

this is my understand of your thinking, A run request (fork) was submitted, but before the actual fork execute, you want to examine current available ram, and decide execution of the fork or postpone it (put in queue). is this is the idea?

First of all, in kernel there is no actual fork() function you can call, the fork(2) is a syscall that kernel expose to user space. The most close to the idea of fork() in kernel is kernel_clone()

You can use si_mem_available() to get available memory at the moment, do your calculation and decide if you let fork to execute. However since the call does not perform any kind of control, there could be race condition that after the call return the actual available is more or last than the return value

Gentoopc wrote:
when this threshold is exceeded, the longest branch of the process tree will begin to roll back.
How do you envision the rollback happen? Do you single the process(es)? or simply sit and wait until those process(es) complete?
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3203

PostPosted: Sat Sep 30, 2023 7:07 pm    Post subject: Reply with quote

Quote:
However since the call does not perform any kind of control, there could be race condition that after the call return the actual available is more or last than the return value
This unfortunately is a heuristic based on an inherently volatile status variable, so fortunately the race betweeen fork and malloc doesn't matter.

The real question is why not just enable swap and/or disable overcommitting memory.
I mean, I actually do know the answer to this question and I kinda sympathize, but at this point it really is a high time to say this topic is all about having fun with kernel hacking and not about solving any particular problem.
Anyway, let us know what you discover on your journey.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sun Oct 01, 2023 7:34 am    Post subject: Reply with quote

eccerr0r wrote:




what are you talking about? here there are processes, their kind of existence is a tree that constantly exists in dynamics. I propose to make the process tree see the RAM boundaries and grow accordingly. now you just have it growing stupidly until the RAM overflow happens and the killer() function starts killing the process at random, simply because it can't do anything. I suggest doing something thoughtful, because such an approach as it is now is not an approach worthy of software engineers, it is in the best case a campaign of some farmer.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sun Oct 01, 2023 7:47 am    Post subject: Reply with quote

pingtoo wrote:



English is not my native language. it's hard for me to explain. to put it quite simply, I understand that when fork() generates a process, it will be the root node, and from here the process tree will begin to grow. it's figurative. I understand that this is not static, it's all dynamic, the drawing of the process tree is constantly changing. I want to teach this process tree to grow so that killer() is not needed. this is an easy task, any junior will solve it. what's complicated about that? if there are no iron resources to create new processes, then why wait for them? to call killer()? this is nonsense,! if it is very difficult to free up resources to create new processes, then it is necessary to teach processes to be created correctly when there are resources for this, if there are none, it is necessary to teach them to wait until they are released. even this approach is better than just producing processes until the computer freezes. therefore, it does not matter which kellir() process is destroyed, the main thing is to destroy and think that this is the standard of the operating system and protect it in every way from attempts to change it.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sun Oct 01, 2023 8:04 am    Post subject: Reply with quote

pingtoo wrote:
Do you single the process(es)? or simply sit and wait until those process(es) complete?



Code:
int foo(int a){   if(a>0){  return(foo(a-1) ); };   return 0;   };




use the recursion principle in the right places, if the avalible_RAM is close to zero, then it's time tofolding in reverse order and return the results to the parent processes. well, it's just a thought.


Last edited by Gentoopc on Sun Oct 01, 2023 11:45 am; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum