Overview
Usually we dump not just a single process but a set of them, where every process may be sharing some resources with other processes. Thus we need somehow to distinguish which resources are shared and which are not.
For this sake kcmp system call has been introduced to Linux kernel. It takes two processes and compare a resource asked, returning result similar to well known strcmp call. This allows CRIU to track resources with a sorting algorithm.
Kcmp-trees
CRIU gather files, filesystems, vitrual memory descriptors, signal handlers and file descriptors associated with a process
each into Kcmp-tree. Thus at moment we are carrying five Kcmp-trees. Each declared with DECLARE_KCMP_TREE
helper.
For example
DECLARE_KCMP_TREE(vm_tree, KCMP_VM);
Each tree internally represented as red-black tree.
Requesting new ID
When CRIU gathers process resources it check if a resource is already sitting inside of a tree calling
kid_generate_gen()
helper. If a resource is not in a tree - it pushed into a tree
and a caller obtains new abstract ID which may be used inside CRIU images, otherwise the helper
returns zero notifying that this kind of resource already known to CRIU and has been handled earlier.
This feature is quite important to eliminate duplication of entries inside CRIU dump images, because two processes might share a lot of resources and dumping them multiple times would cause very serious performance issue.