canonical / lxd

Powerful system container and virtual machine manager
https://canonical.com/lxd
GNU Affero General Public License v3.0
4.38k stars 931 forks source link

Difference in output from top command and iterating through task list of css_set:- when i itearate through list of css set containining taks i get only two tasks which is bash and the process used for getting the task list, while top shows there are many tasks. #3187

Closed praf111 closed 7 years ago

praf111 commented 7 years ago

The template below is mostly useful for bug reports and support questions. Feel free to remove anything which doesn't apply to you and add more information where it makes sense.

Required information

Issue description

A brief description of what failed or what could be improved.

Steps to reproduce

  1. Step one
  2. Step two
  3. Step three

Information to attach

stgraber commented 7 years ago

That's because exec sessions aren't children of pid 1 in containers, they are their own pid tree.

So if your tool goes up the tree based on PPID (which your output suggests), it won't see the most of the processes.

praf111 commented 7 years ago

Ok thanks, then how can i get handle over all the processes inside container using kernel API?

stgraber commented 7 years ago

Just iterate through /proc

praf111 commented 7 years ago

Actually i am trying to write a system call explicitly for containers, so i need some mechanism to distinguish between tasks of different containers (and also recognize all the tasks of particular container ). so when i use the task list provided by css_set i am not getting all the processes that belong to particular container, so i would like to know any API(in kernel space) provided by kernel so that i can handle over all the processes of particular container. i think iterating over /proc is fine at user level.

stgraber commented 7 years ago

So first of all, note that there are absolutely no concept of containers in the Linux kernel, so if you intend to submit upstream code that's aware of "containers", expect to have to rethink your design.

Containers tasks are tied together by a PID namespace. So you'd need to grab hold of the PID namespace for PID 1 in the container and then look at all the other tasks inside that namespace. I'm not sure what the functions inside the kernel are for that though. I've only ever played with the functions that convert PIDs back and forth in there.

Also note that the PID namespace is hierarchical so two processes that are "inside" the container may in fact be in different PID namespaces with one being in a children namespace. That's what happens when you run nested containers or when various software use pid namespaces as a security measure (systemd, chrome, ...).

stgraber commented 7 years ago

Closing as this isn't a LXD issue (nor a container issue even) but a question about the PID namespace implementation in the Linux kernel which may be better answered on containers@lists.linuxfoundation.org.

praf111 commented 7 years ago

Ok, thank you very much.