oneway(異步)
一、TransactionTooLargeException
對於通過Intent跨進程傳遞數據大家都應該很清楚,但是Intent攜帶的數據大小限制是多少,這個可能大家都沒有思考了,那麼下面以一個實際的案例來說明,具體代碼見如下:
Intent intent = new Intent();
Bitmap mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap b1 = Bitmap.createScaledBitmap(mBmp, 1024, 1024, false);
intent.putExtra("byte data", b1);
sendBroadcast(intent);
執行如下代碼片段會出現如下的錯誤信息,FAILED BINDER TRANSACTION ,很明顯和Binder傳輸有關係,那麼下面來分析一下
10-18 16:04:39.113 3514 3514 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 4194612)
10-18 16:04:39.115 3514 3514 D AndroidRuntime: Shutting down VM
10-18 16:04:39.118 3514 3514 E AndroidRuntime: FATAL EXCEPTION: main
10-18 16:04:39.118 3514 3514 E AndroidRuntime: Process: com.pax.printtest, PID: 3514
10-18 16:04:39.118 3514 3514 E AndroidRuntime: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 4194612 bytes
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.app.ContextImpl.sendBroadcast(ContextImpl.java:961)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:428)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at com.pax.api.test.MyJobServiceActivity.FUN3(MyJobServiceActivity.java:71)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at com.pax.api.test.MyJobServiceActivity$1.onClick(MyJobServiceActivity.java:51)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.view.View.performClick(View.java:5637)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.view.View$PerformClick.run(View.java:22433)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:751)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6121)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: Caused by: android.os.TransactionTooLargeException: data parcel size 4194612 bytes
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.os.BinderProxy.transactNative(Native Method)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.os.BinderProxy.transact(Binder.java:623)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3536)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: at android.app.ContextImpl.sendBroadcast(ContextImpl.java:956)
10-18 16:04:39.118 3514 3514 E AndroidRuntime: ... 12 more
二、Intent攜帶信息的大小受Binder限制
普通的應用是由Zygote孵化而來的用戶進程,所映射的Binder內存大小是不到1M的,準確說是 110241024) - (4096 *2) :這個限制定義在frameworks/native/libs/binder/processState.cpp類中,如果傳輸說句超過這個大小,系統就會報錯,因爲Binder本身就是爲了進程間頻繁而靈活的通信所設計的,並不是爲了拷貝大數據而使用的,所以當傳遞大的數據時會出現上述的錯誤
#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
ProcessState::ProcessState()
: mDriverFD(open_driver())//打開Binder設備驅動
, mVMStart(MAP_FAILED)
, mManagesContexts(false)
, mBinderContextCheckFunc(NULL)
, mBinderContextUserData(NULL)
, mThreadPoolStarted(false)
, mThreadPoolSeq(1)
{
if (mDriverFD >= 0) {
// XXX Ideally, there should be a specific define for whether we
// have mmap (or whether we could possibly have the kernel module
// availabla).
#if !defined(HAVE_WIN32_IPC)
// mmap the binder, providing a chunk of virtual address space to receive transactions.
//採用內存映射函數mmap,給binder分配一塊虛擬地址空間,用來接收事務
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);//沒有足夠空間飛培給/dev/binder,則關閉驅動
mDriverFD = -1;
}
#else
mDriverFD = -1;
#endif
}
LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");
}
可以看到內存映射的時候已經限制了最大的數據,所以超過了內存映射的限制就會出現上述的錯誤。
三、在Binder驅動中mmap的具體實現中還有一個4M的限制
能否不用ProcessState來初始化Binder服務,來突破1M-8KB的限制?
答案是當然可以了,Binder服務的初始化有兩步,open打開Binder驅動,mmap在Binder驅動中申請內核空間內存,所以我們只要手寫open,mmap就可以輕鬆突破這個限制
但是,在Binder驅動中mmap的具體實現中還有一個4M的限制
static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
{
int ret;
struct vm_struct *area;
struct binder_proc *proc = filp->private_data;
const char *failure_string;
struct binder_buffer *buffer;
if (proc->tsk != current)
return -EINVAL;
if ((vma->vm_end - vma->vm_start) > SZ_4M)
vma->vm_end = vma->vm_start + SZ_4M;//如果申請的size大於4MB了,會在驅動中被修改成4MB
binder_debug(BINDER_DEBUG_OPEN_CLOSE,
"binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
proc->pid, vma->vm_start, vma->vm_end,
(vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
(unsigned long)pgprot_val(vma->vm_page_prot));