1*4882a593SmuzhiyunION BUFFER SHARING UTILITY 2*4882a593Smuzhiyun========================== 3*4882a593SmuzhiyunFile: ion_test.sh : Utility to test ION driver buffer sharing mechanism. 4*4882a593SmuzhiyunAuthor: Pintu Kumar <pintu.ping@gmail.com> 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunIntroduction: 7*4882a593Smuzhiyun------------- 8*4882a593SmuzhiyunThis is a test utility to verify ION buffer sharing in user space 9*4882a593Smuzhiyunbetween 2 independent processes. 10*4882a593SmuzhiyunIt uses unix domain socket (with SCM_RIGHTS) as IPC to transfer an FD to 11*4882a593Smuzhiyunanother process to share the same buffer. 12*4882a593SmuzhiyunThis utility demonstrates how ION buffer sharing can be implemented between 13*4882a593Smuzhiyuntwo user space processes, using various heap types. 14*4882a593SmuzhiyunThe following heap types are supported by ION driver. 15*4882a593SmuzhiyunION_HEAP_TYPE_SYSTEM (0) 16*4882a593SmuzhiyunION_HEAP_TYPE_SYSTEM_CONTIG (1) 17*4882a593SmuzhiyunION_HEAP_TYPE_CARVEOUT (2) 18*4882a593SmuzhiyunION_HEAP_TYPE_CHUNK (3) 19*4882a593SmuzhiyunION_HEAP_TYPE_DMA (4) 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunBy default only the SYSTEM and SYSTEM_CONTIG heaps are supported. 22*4882a593SmuzhiyunEach heap is associated with the respective heap id. 23*4882a593SmuzhiyunThis utility is designed in the form of client/server program. 24*4882a593SmuzhiyunThe server part (ionapp_export) is the exporter of the buffer. 25*4882a593SmuzhiyunIt is responsible for creating an ION client, allocating the buffer based on 26*4882a593Smuzhiyunthe heap id, writing some data to this buffer and then exporting the FD 27*4882a593Smuzhiyun(associated with this buffer) to another process using socket IPC. 28*4882a593SmuzhiyunThis FD is called as buffer FD (which is different than the ION client FD). 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunThe client part (ionapp_import) is the importer of the buffer. 31*4882a593SmuzhiyunIt retrives the FD from the socket data and installs into its address space. 32*4882a593SmuzhiyunThis new FD internally points to the same kernel buffer. 33*4882a593SmuzhiyunSo first it reads the data that is stored in this buffer and prints it. 34*4882a593SmuzhiyunThen it writes the different size of data (it could be different data) to the 35*4882a593Smuzhiyunsame buffer. 36*4882a593SmuzhiyunFinally the buffer FD must be closed by both the exporter and importer. 37*4882a593SmuzhiyunThus the same kernel buffer is shared among two user space processes using 38*4882a593SmuzhiyunION driver and only one time allocation. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunPrerequisite: 41*4882a593Smuzhiyun------------- 42*4882a593SmuzhiyunThis utility works only if /dev/ion interface is present. 43*4882a593SmuzhiyunThe following configs needs to be enabled in kernel to include ion driver. 44*4882a593SmuzhiyunCONFIG_ANDROID=y 45*4882a593SmuzhiyunCONFIG_STAGING=y 46*4882a593SmuzhiyunCONFIG_ION=y 47*4882a593SmuzhiyunCONFIG_ION_SYSTEM_HEAP=y 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunThis utility requires to be run as root user. 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunCompile and test: 53*4882a593Smuzhiyun----------------- 54*4882a593SmuzhiyunThis utility is made to be run as part of kselftest framework in kernel. 55*4882a593SmuzhiyunTo compile and run using kselftest you can simply do the following from the 56*4882a593Smuzhiyunkernel top directory. 57*4882a593Smuzhiyunlinux$ make TARGETS=android kselftest 58*4882a593SmuzhiyunOr you can also use: 59*4882a593Smuzhiyunlinux$ make -C tools/testing/selftests TARGETS=android run_tests 60*4882a593SmuzhiyunUsing the selftest it can directly execute the ion_test.sh script to test the 61*4882a593Smuzhiyunbuffer sharing using ion system heap. 62*4882a593SmuzhiyunCurrently the heap size is hard coded as just 10 bytes inside this script. 63*4882a593SmuzhiyunYou need to be a root user to run under selftest. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunYou can also compile and test manually using the following steps: 66*4882a593Smuzhiyunion$ make 67*4882a593SmuzhiyunThese will generate 2 executable: ionapp_export, ionapp_import 68*4882a593SmuzhiyunNow you can run the export and import manually by specifying the heap type 69*4882a593Smuzhiyunand the heap size. 70*4882a593SmuzhiyunYou can also directly execute the shell script to run the test automatically. 71*4882a593SmuzhiyunSimply use the following command to run the test. 72*4882a593Smuzhiyunion$ sudo ./ion_test.sh 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunTest Results: 75*4882a593Smuzhiyun------------- 76*4882a593SmuzhiyunThe utility is verified on Ubuntu-32 bit system with Linux Kernel 4.14. 77*4882a593SmuzhiyunHere is the snapshot of the test result using kselftest. 78*4882a593Smuzhiyun 79*4882a593Smuzhiyunlinux# make TARGETS=android kselftest 80*4882a593Smuzhiyunheap_type: 0, heap_size: 10 81*4882a593Smuzhiyun-------------------------------------- 82*4882a593Smuzhiyunheap type: 0 83*4882a593Smuzhiyun heap id: 1 84*4882a593Smuzhiyunheap name: ion_system_heap 85*4882a593Smuzhiyun-------------------------------------- 86*4882a593SmuzhiyunFill buffer content: 87*4882a593Smuzhiyun0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 88*4882a593SmuzhiyunSharing fd: 6, Client fd: 5 89*4882a593Smuzhiyun<ion_close_buffer_fd>: buffer release successfully.... 90*4882a593SmuzhiyunReceived buffer fd: 4 91*4882a593SmuzhiyunRead buffer content: 92*4882a593Smuzhiyun0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0x0 0x0 0x0 0x0 0x0 0x0 93*4882a593Smuzhiyun0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 94*4882a593SmuzhiyunFill buffer content: 95*4882a593Smuzhiyun0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 96*4882a593Smuzhiyun0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 97*4882a593Smuzhiyun0xfd 0xfd 98*4882a593Smuzhiyun<ion_close_buffer_fd>: buffer release successfully.... 99*4882a593Smuzhiyunion_test.sh: heap_type: 0 - [PASS] 100*4882a593Smuzhiyun 101*4882a593Smuzhiyunion_test.sh: done 102