18c2ecf20Sopenharmony_ciION BUFFER SHARING UTILITY
28c2ecf20Sopenharmony_ci==========================
38c2ecf20Sopenharmony_ciFile: ion_test.sh : Utility to test ION driver buffer sharing mechanism.
48c2ecf20Sopenharmony_ciAuthor: Pintu Kumar <pintu.ping@gmail.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciIntroduction:
78c2ecf20Sopenharmony_ci-------------
88c2ecf20Sopenharmony_ciThis is a test utility to verify ION buffer sharing in user space
98c2ecf20Sopenharmony_cibetween 2 independent processes.
108c2ecf20Sopenharmony_ciIt uses unix domain socket (with SCM_RIGHTS) as IPC to transfer an FD to
118c2ecf20Sopenharmony_cianother process to share the same buffer.
128c2ecf20Sopenharmony_ciThis utility demonstrates how ION buffer sharing can be implemented between
138c2ecf20Sopenharmony_citwo user space processes, using various heap types.
148c2ecf20Sopenharmony_ciThe following heap types are supported by ION driver.
158c2ecf20Sopenharmony_ciION_HEAP_TYPE_SYSTEM (0)
168c2ecf20Sopenharmony_ciION_HEAP_TYPE_SYSTEM_CONTIG (1)
178c2ecf20Sopenharmony_ciION_HEAP_TYPE_CARVEOUT (2)
188c2ecf20Sopenharmony_ciION_HEAP_TYPE_CHUNK (3)
198c2ecf20Sopenharmony_ciION_HEAP_TYPE_DMA (4)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciBy default only the SYSTEM and SYSTEM_CONTIG heaps are supported.
228c2ecf20Sopenharmony_ciEach heap is associated with the respective heap id.
238c2ecf20Sopenharmony_ciThis utility is designed in the form of client/server program.
248c2ecf20Sopenharmony_ciThe server part (ionapp_export) is the exporter of the buffer.
258c2ecf20Sopenharmony_ciIt is responsible for creating an ION client, allocating the buffer based on
268c2ecf20Sopenharmony_cithe heap id, writing some data to this buffer and then exporting the FD
278c2ecf20Sopenharmony_ci(associated with this buffer) to another process using socket IPC.
288c2ecf20Sopenharmony_ciThis FD is called as buffer FD (which is different than the ION client FD).
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciThe client part (ionapp_import) is the importer of the buffer.
318c2ecf20Sopenharmony_ciIt retrives the FD from the socket data and installs into its address space.
328c2ecf20Sopenharmony_ciThis new FD internally points to the same kernel buffer.
338c2ecf20Sopenharmony_ciSo first it reads the data that is stored in this buffer and prints it.
348c2ecf20Sopenharmony_ciThen it writes the different size of data (it could be different data) to the
358c2ecf20Sopenharmony_cisame buffer.
368c2ecf20Sopenharmony_ciFinally the buffer FD must be closed by both the exporter and importer.
378c2ecf20Sopenharmony_ciThus the same kernel buffer is shared among two user space processes using
388c2ecf20Sopenharmony_ciION driver and only one time allocation.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciPrerequisite:
418c2ecf20Sopenharmony_ci-------------
428c2ecf20Sopenharmony_ciThis utility works only if /dev/ion interface is present.
438c2ecf20Sopenharmony_ciThe following configs needs to be enabled in kernel to include ion driver.
448c2ecf20Sopenharmony_ciCONFIG_ANDROID=y
458c2ecf20Sopenharmony_ciCONFIG_STAGING=y
468c2ecf20Sopenharmony_ciCONFIG_ION=y
478c2ecf20Sopenharmony_ciCONFIG_ION_SYSTEM_HEAP=y
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciThis utility requires to be run as root user.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciCompile and test:
538c2ecf20Sopenharmony_ci-----------------
548c2ecf20Sopenharmony_ciThis utility is made to be run as part of kselftest framework in kernel.
558c2ecf20Sopenharmony_ciTo compile and run using kselftest you can simply do the following from the
568c2ecf20Sopenharmony_cikernel top directory.
578c2ecf20Sopenharmony_cilinux$ make TARGETS=android kselftest
588c2ecf20Sopenharmony_ciOr you can also use:
598c2ecf20Sopenharmony_cilinux$ make -C tools/testing/selftests TARGETS=android run_tests
608c2ecf20Sopenharmony_ciUsing the selftest it can directly execute the ion_test.sh script to test the
618c2ecf20Sopenharmony_cibuffer sharing using ion system heap.
628c2ecf20Sopenharmony_ciCurrently the heap size is hard coded as just 10 bytes inside this script.
638c2ecf20Sopenharmony_ciYou need to be a root user to run under selftest.
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciYou can also compile and test manually using the following steps:
668c2ecf20Sopenharmony_ciion$ make
678c2ecf20Sopenharmony_ciThese will generate 2 executable: ionapp_export, ionapp_import
688c2ecf20Sopenharmony_ciNow you can run the export and import manually by specifying the heap type
698c2ecf20Sopenharmony_ciand the heap size.
708c2ecf20Sopenharmony_ciYou can also directly execute the shell script to run the test automatically.
718c2ecf20Sopenharmony_ciSimply use the following command to run the test.
728c2ecf20Sopenharmony_ciion$ sudo ./ion_test.sh
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciTest Results:
758c2ecf20Sopenharmony_ci-------------
768c2ecf20Sopenharmony_ciThe utility is verified on Ubuntu-32 bit system with Linux Kernel 4.14.
778c2ecf20Sopenharmony_ciHere is the snapshot of the test result using kselftest.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cilinux# make TARGETS=android kselftest
808c2ecf20Sopenharmony_ciheap_type: 0, heap_size: 10
818c2ecf20Sopenharmony_ci--------------------------------------
828c2ecf20Sopenharmony_ciheap type: 0
838c2ecf20Sopenharmony_ci  heap id: 1
848c2ecf20Sopenharmony_ciheap name: ion_system_heap
858c2ecf20Sopenharmony_ci--------------------------------------
868c2ecf20Sopenharmony_ciFill buffer content:
878c2ecf20Sopenharmony_ci0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd
888c2ecf20Sopenharmony_ciSharing fd: 6, Client fd: 5
898c2ecf20Sopenharmony_ci<ion_close_buffer_fd>: buffer release successfully....
908c2ecf20Sopenharmony_ciReceived buffer fd: 4
918c2ecf20Sopenharmony_ciRead buffer content:
928c2ecf20Sopenharmony_ci0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0x0 0x0 0x0 0x0 0x0 0x0
938c2ecf20Sopenharmony_ci0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
948c2ecf20Sopenharmony_ciFill buffer content:
958c2ecf20Sopenharmony_ci0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd
968c2ecf20Sopenharmony_ci0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd 0xfd
978c2ecf20Sopenharmony_ci0xfd 0xfd
988c2ecf20Sopenharmony_ci<ion_close_buffer_fd>: buffer release successfully....
998c2ecf20Sopenharmony_ciion_test.sh: heap_type: 0 - [PASS]
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciion_test.sh: done
102