Q&A
1.Multiple call of same binder object from 15 threads,will the binder service handle request in 15 threads?
Answer: No.
Explain:The binder service may or may not handle in 15 threads.If threre's idle threads existed in thread pool , the binder service will re-use that thread to handle request.
In my test,only three threads created to handle the request when app query media DB from 15 threads
2.When app call binder service from one threads,does the binder service always use same thread to handle that?
Answer:No
Explain:If a thread in threadpool time out,it will exist automatically
3.Is there thread limitation in binder thread pool?
Answer:yes,the maximum thread nummber is set 15 in android implementation,see open_driver@ProcessState.cpp.
在代码中加入调试语句,测试CameraService调用次序
测试步骤:
1.adb shell ps | | find "/system/bin/mediaserver",得到mediaserver对应pid my-pid
2adb logcat -c
3..adb shell kill my-pid
4.adb logcat > out
5.launch Camera
采集adb logcat的输出文本
由下面Log可知,mediaserver进程号为1771,启动时创建的工作线程为1777,此线程1777和主线程1771构成初始线程池,且此两个线程不会超时退出线程循环.
08-12 08:45:53.226 1771 1771 D ProcessState: enter open_driver
08-12 08:45:53.710 1771 1771 D IPCThreadState: enter IPCThreadState::joinThreadPool 1
08-12 08:45:53.710 1771 1771 D IPCThreadState: begin call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ //block here
08-12 08:45:53.710 1771 1777 D IPCThreadState: enter IPCThreadState::joinThreadPool 1
08-12 08:45:53.710 1771 1777 D IPCThreadState: enter IPCThreadState::talkWithDriver 1
08-12 08:45:53.710 1771 1777 D IPCThreadState: bwr.write_size=4 bwr.read_size=256
08-12 08:45:53.710 1771 1777 D IPCThreadState: begin call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ
08-12 08:45:53.710 1771 1777 D IPCThreadState: end call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ
08-12 08:45:53.710 1771 1777 D IPCThreadState: leave IPCThreadState::talkWithDriver
08-12 08:45:53.710 1771 1777 D IPCThreadState: enter executeCommand cmd=29196
08-12 08:45:53.710 1771 1777 D IPCThreadState: leave executeCommands 29196
08-12 08:45:53.710 1771 1777 D IPCThreadState: enter IPCThreadState::talkWithDriver 1
08-12 08:45:53.710 1771 1777 D IPCThreadState: bwr.write_size=0 bwr.read_size=256
08-12 08:45:53.710 1771 1777 D IPCThreadState: begin call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ //block here
08-12 08:45:54.054 1771 1771 D IPCThreadState: end call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ //主线程被唤醒
08-12 08:45:54.054 1771 1771 D IPCThreadState: leave IPCThreadState::talkWithDriver
08-12 08:45:54.054 1771 1771 D IPCThreadState: enter executeCommand cmd=29196
08-12 08:45:54.054 1771 1771 D IPCThreadState: leave executeCommands 29196
08-12 08:45:54.054 1771 1771 D IPCThreadState: enter IPCThreadState::talkWithDriver 1
08-12 08:45:54.054 1771 1771 D IPCThreadState: bwr.write_size=0 bwr.read_size=0
08-12 08:45:54.054 1771 1771 D IPCThreadState: enter executeCommand cmd=-2144833022
08-12 08:45:54.054 1771 1771 D IPCThreadState: BR_TRANSACTION
08-12 08:45:54.054 1771 1771 D IPCThreadState: call transact1 here
08-12 08:45:57.960 1771 1771 D IPCThreadState: end call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ
08-12 08:45:57.960 1771 1771 D IPCThreadState: leave IPCThreadState::talkWithDriver
08-12 08:45:57.960 1771 1771 D IPCThreadState: enter executeCommand cmd=29196
08-12 08:45:57.960 1771 1771 D IPCThreadState: leave executeCommands 29196
08-12 08:45:57.960 1771 1771 D IPCThreadState: enter IPCThreadState::talkWithDriver 1
08-12 08:45:57.960 1771 1771 D IPCThreadState: bwr.write_size=0 bwr.read_size=0
08-12 08:45:57.960 1771 1771 D IPCThreadState: enter executeCommand cmd=-2144833022
08-12 08:45:57.960 1771 1771 D IPCThreadState: BR_TRANSACTION
08-12 08:45:57.960 1771 1771 D IPCThreadState: call transact1 here
08-12 08:45:57.960 1771 1771 D CameraService: enter CameraService::onTransact code=3 //code=BnCameraService::CONNECT,new CameraService::Client()
08-12 08:45:58.609 1771 1777 D IPCThreadState: end call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ
08-12 08:45:58.609 1771 1777 D IPCThreadState: BR_SPAWN_LOOPER: //这里新的Thread被binder要求创建出来,应该是由于CameraService需要创建新的binder对象 ICameraClient从而需要额外的线程来处理
08-12 08:45:58.609 1771 1777 D ProcessState: enter ProcessState::spawnPooledThread isMain=0
08-12 08:45:58.609 1771 1777 D ProcessState: create PoolThread 0xaa880
08-12 08:45:58.609 1771 1777 D ProcessState: leave ProcessState::spawnPooledThread isMain=0
08-12 08:45:58.609 1771 1814 D libutils.threads: enter Thread::_threadLoop //新线程创建成功
08-12 08:45:58.609 1771 1814 D IPCThreadState: enter IPCThreadState::joinThreadPool 0
08-12 08:45:58.609 1771 1777 D IPCThreadState: BR_TRANSACTION
08-12 08:45:58.609 1771 1777 D IPCThreadState: call transact1 here
08-12 08:45:58.609 1771 1777 D ICamera : enter BnCamera::onTransact code=2 //调用到了ICamera::onTransact,code=SET_PREVIEW_DISPLAY
08-12 08:45:58.609 1771 1813 V MediaPlayerService: player type = 4
08-12 08:45:58.609 1771 1771 D CameraService: enter CameraService::onTransact code=2 //code=GET_CAMERA_INFO
08-12 08:45:58.617 1771 1777 D IPCThreadState: BR_TRANSACTION
08-12 08:45:58.617 1771 1777 D ICamera : enter BnCamera::onTransact code=11 //code=SEND_COMMAND
08-12 08:45:58.617 1771 1777 D IPCThreadState: finish call transact
08-12 08:45:58.617 1792 1801 D IPCThreadState: begin call ioctl(mProcess->mDriverFD, BINDER_WRITE_READ
08-12 08:45:58.617 1771 1771 D ICamera : enter BnCamera::onTransact code=21 //code=GET_CUSTOM_PARAMETERS
08-12 08:45:58.625 1771 1771 D OMXCameraAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OMXCameraAdapter/OMXCameraAdapter.cpp:2824 getParameters - OMXCameraAdapter - getParametersOMX - exposure.nShutterSpeedMsec = 60
08-12 08:45:58.625 1771 1771 D OMXCameraAdapter: hardware/ti/omap4/omap3/camera-omap4/src/OMXCameraAdapter/OMXCameraAdapter.cpp:2825 getParameters - OMXCameraAdapter - getParametersOMX - exposure.nSensitivity = 100
08-12 08:45:58.656 1771 1814 D ICamera : enter BnCamera::onTransact code=20 //code=SET_CUSTOM_PARAMETERS
08-12 08:45:58.687 1771 1777 D ICamera : enter BnCamera::onTransact code=21
08-12 08:45:58.695 1771 1771 D CameraService: enter CameraService::onTransact code=1
08-12 08:45:58.710 1771 1814 D CameraService: enter CameraService::onTransact code=2
08-12 08:45:58.757 1771 1814 D ICamera : enter BnCamera::onTransact code=20
08-12 08:45:58.757 1771 1814 E CameraService: Invalid Burst Count setting burst count to 1
08-12 08:45:58.796 1771 1777 D ICamera : enter BnCamera::onTransact code=20
08-12 08:45:58.796 1771 1777 E CameraService: Invalid Burst Count setting burst count to 1
08-12 08:45:58.820 1771 1771 D ICamera : enter BnCamera::onTransact code=21
08-12 08:45:58.882 1771 1814 D ICamera : enter BnCamera::onTransact code=20
08-12 08:45:58.906 1771 1777 D ICamera : enter BnCamera::onTransact code=4
08-12 08:45:59.843 1771 1814 D ICamera : enter BnCamera::onTransact code=2
08-12 08:46:00.515 1771 1777 D ICamera : enter BnCamera::onTransact code=6
结论:mediaserver中,共3个线程被创建出来处理CameraService及CameraClient Binder的处理,tid=1771,1777,1814。