Finally, I have to write a paper about binder. From initial contact with Android to spending a lot of time researching Android source code, Binder has been a stumbling block in analyzing the road. Read a few of the most popular Android source code analysis books, each time basically can not be the binder related knowledge reading, read thoroughly. Fortunately, has not given up, the first time can not understand the jump, the next time to reread, each reading has a new harvest. Now it's time to sort it out.
What is the binder I understand? An IPC (cross-process communication) implementation method. Note "Cross-process", which indicates that the data flows from one process to another process. First of all, why is it so difficult to cross-process? Because the address space of an application is a virtual address, it is mapped to a physical memory space. If the application is operating directly in the physical memory space, the application is very convenient to cross the process, and it is good to pass the physical address directly. However, different applications use virtual memory space mapped by the memory manager, which is unknown as to which physical memory area to map to, making it less convenient to pass data across processes, but it is not an option. Typical pipelines, message queues, shared memory, and so on in Linux can be used for cross-process communication. Android uses the binder way, naturally has its advantages, so-called "flexible, convenient", this advantage is still slowly realized.
To understand the binder mechanism of Android in depth, there are a few things to understand:
- Inside the Android operating system, it can be regarded as a service delivery mechanism based on C/s architecture. A service request is made by the client, which executes the service and returns the result. For example, an app needs to use a camera to make a service request to Mediaservice, and the communication between the two processes is done by binder, and there is a lot of this C/s structure in Android.
- The difference between a process and a class: a process can provide multiple services (either through multithreading or logic), such as Mediaservice, a process in Android that provides Audioflinger services, Audiopolicyservice Service, Mediaplayerservice service and so on. In the process of analysis, always be aware of what code the current process executes.
- Communication and business to differentiate: Binder is ultimately a form of communication, then who uses it? That must be the upper-level code. This is said to be "upper-level code" because the hierarchy of the code framework is the focus of analysis. These upper-level code may be executed in different processes.
If you have seen service implementations, or inherited relationships of various classes, they have a certain level of relationship. Now the use of "from the bottom to the top" level of analysis, when looking at the source code, the individual found that "from the bottom up" can often get good results. The sixth chapter of "in-depth understanding of Android Vol. 1" is analyzed.
Equipment can not be separated from the device driver, Binder is actually a driver, the protocol is a pseudo-device, through the common operation of the device, such as Open, IOCTL, mmap, etc., you may operate the binder "equipment." The key point is: through Mmap can put a virtual address and physical address map, this time, a process and binder share the same block of physical memory, if another B process also shared this memory through binder, then a, b two processes shared the same piece of memory-- This is how the different applications share memory (via binder). There is a word that, if you are here, two processes can be completely communicated through this binder, of course, the implementation of the details must be cumbersome. So to do encapsulation!! According to the 3rd above, the details of the communication are still described here, and no business is involved. The binder driver itself is more complex and does not make analysis. It can be simplified: it can be used for memory sharing- open binder devices, execute mmap, send requests through binder drives, and get results.
- Processstate and Ipcthreadstate
Since the encapsulation is to be done, this layer should be the encapsulation of the operating details of the binder device, and the analysis code is indeed the case. From the abstract point of view, encapsulation is definitely for easier operation, if the binder open, execute mmap and so on the results of encapsulation, the upper layer only need to invoke the corresponding interface, to achieve the purpose of encapsulation.
The first thing to understand is that processstate and Ipcthreadstate are two classes, as if it were nonsense, but the names of both of them are "imaginative" and always let me think of them as processes and threads. We know that an application corresponds to a Linux process, OK, knowing this is good, more accessible: http://blog.csdn.net/mirkerson/article/details/38128637. Processstate and Ipcthreadstate are packages that are more convenient for process/thread execution binders.
Processstate::P rocessstate (): Mdriverfd (Open_driver ())//open binder and related mapping operations to binder, do not look closely, Mvmstart (map_failed), MM Anagescontexts (False), mbindercontextcheckfunc (null), Mbindercontextuserdata (null), mthreadpoolstarted (false) , Mthreadpoolseq (1) {if (mdriverfd >= 0) {//XXX Ideally, there should is a specific define for whether W E//Mmap (or whether we could possibly have the kernel module//availabla). #if!defined (have_win32_ip C)//Mmap the binder, providing a chunk of virtual address space to receive transactions. Mvmstart = mmap (0, Binder_vm_size, Prot_read, Map_private | Map_noreserve, MDRIVERFD, 0); if (Mvmstart = = map_failed) {//*sigh* Aloge ("Using/dev/binder failed:unable to Mmap transaction Memory.\n "); Close (MDRIVERFD); MDRIVERFD =-1; } #else mdriverfd = 1; #endif} log_always_fatal_if (Mdriverfd < 0, Binder driver could not be opEned. terminating. ");
The above analysis shows that the encapsulation of the binder is done in processstate, which is actually done using ipcthreadstate-the result is given directly: each process has a processstate instance, Each thread has a ipcthreadstate instance, and the specific communication is done by each thread itself with binder (since a singleton mode is used).
- IBinder, Bpbinder and Bbinder
I just saw Processstate's package for binder, but this package is not enough. Because Processstate is only the binder open, mmap and so on encapsulation, the specific data should be how to pass binder, what kind of data to be passed and so on need finer granularity encapsulation, this is ibinder. IBinder provides a generic part of subclasses Bpbinder and Bbinder, and Bpbinder and bbinder use this generic part to communicate. So far, there has been an analysis of communication behavior and no business involved.
View Bpbinder's Transact implementations:
status_t bpbinder::transact ( uint32_t Code, const parcel& data, parcel* reply, uint32_t flags) { //Once a bind Er has died, it'll never come back to life. if (malive) { status_t status = Ipcthreadstate::self ()->transact (//Indeed it is called ipcthreadstate to communicate with binder Mhandle, code, data, reply, flags); if (status = = dead_object) malive = 0; return status; } return dead_object;}
In order not to complicate, to here only need to know IBinder as the base class, "developed" the client and server-side communication interface, specifically by Bpbinder and bbinder to achieve.
An important inheritance in object-oriented is to make the common part a single class (abstract class, interface), and then inherit, so that the class has a variable or method of the base class. The business layer is also this logic, the client and server at both ends of the implementation of the common part (if it is an interface), and then according to the method of the interface to implement their own logic, so much more concise. Take Mediaservice as an example, its general part is the iservicemanager,client end is the bpservicemanager,server end for the Bnservicemanager. Always do not understand, Bpservicemanager and bnservicemanager in different processes to execute, why must be designed to have a iservicemanager such a base class? In fact, if there is no base class, communication is completely possible, just need more and more complex data to ensure that the other side can understand what I want to execute the action.
Come here, the communication layer, the business layer has been explained. Summary:
- Classes with "Binder" in the class name are implemented for communication purposes
- The business layer uses the communication layer to implement communication
- Bonding between the communication layer and the business layer
As you can see, the actual class inheritance relationship is slightly more complex than the one discussed above. Here I have two key points to glue the different layers together:
- Bnservicemanager directly inherits the Bbinder, indicating that Bnservicemanager can participate directly in binder communication.
- The Bprefbase inherited by Bpservicemanager has a mremote ibinder type variable, which in the actual code points to Bpbinder, So Bpservicemanager can communicate with Bbinder through Bpbinder.
Here, the framework of the binder communication is finished.
In summary: Separating communication from the business layer, focusing on implementing the respective logic, and properly gluing together, becomes a complete binder-based cross-process communication.
The Binder for Android Analytics 01