当前位置:网站首页>Anonymous shared memory ashmem
Anonymous shared memory ashmem
2022-08-09 04:59:00 【ldxlz224】
概述
mmap 是 Linux The most familiar shared memory method in.通过打开同一个文件,并且使用 MAP_SHARED 标志来调用 mmap() 函数,Two processes can share a piece of memory space.但是这种方式存在一个问题,If part of the space of distribution don't need,These no longer used cannot be released individually“物理内存”,Why is it physical memory?,因为mmapAllocated address space,only when a process accesses a page,will allocate actual physical memory.These physical memory can only be munmap() one-time release.If a page of physical memory is no longer needed,want to release him alone,传统的 mmap impossible.所以就有了 ashmem
ashmem 的作用和用法
- 为了弥补 mmap 的不足,Android开发了 ashmem 匿名共享内存机制,This new mechanism is based on mmap 基础上,但是 ashmem 提供了 pin 和 unpin 两个 io 操作,Physical memory that can partially free up memory space.
- 使用 ashmem 需要打开 “dev/asheme”,And pass the descriptor to mmap() 作为参数.但是要注意的是,每次 open() Opening the device will result in a different file descriptor,Represents a different region of memory.So if you need multiple blocks of shared memory,Just turn on the device multiple times “dev/asheme” 就可以了.但是 ashmem Used for Shared memory is encountered a problem,Linux 实现共享内存是,Ask two processes to open a file at the same time,and pass the file descriptor to the kernel,因为文件在 Linux 中是唯一的,This way the system knows that two processes need to operate on the same piece of memory.但是 asheme 只能打开 “dev/asheme” 文件,So can't use a file object represents a piece of Shared memory,while the file descriptor is a number,Only valid in this process.Therefore, there is no way to pass the file descriptor to another process for use.
- Android 的 Binder Provides a means of passing file descriptors between two processes,这样 Binder 的 Service 和 client memory sharing between.因此 ashmem cannot be used for shared memory between any two processes,must be through Binder A connection is established between two processes.
Android 提供了一组使用 ashmem 的函数.头文件 ashmem.h 在/system/core/include/libcutil/下,实现代码 ashmem-dev.c 位于 /system/core/libcutil/ 中.使用 ashmem 步骤如下:
- First create a shared area,通过调用 int ashmem_create_region(const char *name, size_t size) 来完成
int ashmem_create_region(const char *name, size_t size)
{
int ret, save_errno;
// 打开设备文件 /dev/ashmem/ 返回一个文件描述符 id
int fd = __ashmem_open();
if (fd < 0) {
return fd;
}
// 判断 name 是否为 NULL 如果不为 NULL 通过 ASHMEM_NAME_LEN 来设置属性
if (name) {
char buf[ASHMEM_NAME_LEN] = {
0};
strlcpy(buf, name, sizeof(buf));
// 由 ioctl 操作 ASHMEM_SET_NAME 来设置名称
// ioctl(input/output control)是一个专用于设备输入输出操作的系统调用
ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
if (ret < 0) {
goto error;
}
}
// 通过 ioctl 设置内存大小
ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
if (ret < 0) {
goto error;
}
return fd;
error:
save_errno = errno;
close(fd);
errno = save_errno;
return ret;
}
(1)ashmem_create_region() 的主要工作是,打开设备文件 dev/ashmem 得到一个文件描述符 fd.
(2)如果 name 不为 Null 则通过 ioctl 操作 TEMP_FAILURE_RETRY 来设置名称.
(3)通过 ioctl 调用 TEMP_FAILURE_RETRY 设置内存大小.
- After get the file descriptor,通过 mmap 分配内存.
// addr:共享内存的地址,如果为NULL,Will be automatically assigned a block of memory
// length:共享内存的长度
// prot:some memory protectionflags(比如说:匿名,读,写权限等)
// flags:visible to other processes,whether updates will be delivered to the underlying file
// fd:文件描述符(used to initialize memory)
// offset:偏移量(用于初始化,offset从fdwhere to start reading,lengthCan represent read length
void* base = mmap(0,length,prot,flags,fd,offset)
- If you need to modify the memory properties through the following function,prot 参数和mmap的属性一致
int ashmem_set_prot_region(int fd, int prot)
{
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
}
- To unlock part of the memory call the following function
int ashmem_unpin_region(int fd, size_t offset, size_t len)
{
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = {
static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
}
解锁后,This part of the memory will be reclaimed when the memory is insufficient.
- If you need to unlock the memory locked again
int ashmem_pin_region(int fd, size_t offset, size_t len)
{
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = {
static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
}
- If you need to get the size of the memory block,then call the following function
int ashmem_get_size_region(int fd)
{
int ret = __ashmem_is_ashmem(fd, 1);
if (ret < 0) {
return ret;
}
return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
}
ashmem 实现原理
andorid 5.0 上
ashamed 是建立在 Linux Based on existing memory allocation and sharing,It's not complicated by itself.ashmem The driver's main job is to maintain a linked list,The role of this linked list is that the user IO 操作 unpin the next node,It stores the start and end addresses of the memory that needs to be unlocked..When the system memory is insufficient, part of the memory will be released through this linked list.The more complex work,as allocating address space,or through the kernel to complete,所以 ashmem The main job is to maintain unpinned 链表.
struct ashmem_area {
char name[ASHMEM_FULL_NAME_LEN]; /* 名字出现在 /proc/pid/maps */
struct list_head unpinned_list; /* 列表头 用户调用 pin 和 unpin 生成的*/
struct file *file; /* file used to allocate virtual space 这是通过 mmap Objects generated by allocating memory are not the same as files that open the device */
size_t size; /* 内存块的大小 */
unsigned long vm_start; /* 映射这个ashmem的vm_area的起始地址 */
unsigned long prot_mask; /* the size of the memory block */
};
当用户进程调用 open() When opening a device file,就会创建一个 ashmem_area 对象,保存一些 ashmem memory block information,这个 ashmem_area The pointer to the object is saved to the device file object file 的 private_data 字段中,When a user process uses a file descriptor to call IO 操作时,The driver can get this from the file object ashmem_area 对象了.
Another very important structural formula ashmem_range Records the basic information of the unlocked memory block.ashmem_range 就是unpinned 链表的节点,Records the basic information of the unlocked memory block.当用户进程执行unpin 操作时,The driver will generate a ashmem_range 的节点,This node will be on a global chain table LRU 中,当系统内存不足时,will call the driver registration ashmem_shrinker() 函数来释放内存,而 ashmem_shrinker() 函数将通过 LRU linked list to find unlocked memory and free modules,内存被释放后 ashmem_range 将会被移除,But it will remain in the process unpinned_list 中,At the same time its internal properties purged 会设置成 true Representative has been released,will not be released repeatedly..
边栏推荐
- leetcode:402. 移掉 K 位数字
- perl基础语法归纳
- 2022-08-08 mysql慢SQL-Q18-10GB数据量-mysql/innodb测试
- ABP 6.0.0-rc.1的新特性
- Example of 360 assessment feedback questions
- 如何选型APS系统,还需明确这七大关键因素
- could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
- 【Harmony OS】【ARK UI】ETS 上下文基本操作
- 【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
- LN论文、五种归一化原理和实现
猜你喜欢

GraalVM安装

浙江DAMA-CDGA/CDGP数据治理认证招生简章

2022年8月深圳产品经理认证招生简章(NPDP)

基于ABP和Magicodes实现Excel导出操作
![could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept](/img/00/328e4c296c00929140c9aff3bb896e.png)
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept

【Harmony OS】【FAQ】Hongmeng Questions Collection 1

Software testing method is introduced in detail

Nacos源码安装

杰理之一拖二 另一台手机超距 通话会无声【篇】

mysql内容不存在的报错
随机推荐
y91.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(二)
Parameters in dynamic libraries cannot be modified through macro definitions or global variables in header files
数据库设计---三范式和反范式设计
软件测试的发展趋势
力扣202-快乐数——哈希集合
Masked AutoEncoder论文及实现
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
说明高级语言、汇编语言、机器语言三者的区别,谈谈你对汇编语言的认识。
存储系统架构演变
mysql内容不存在的报错
matlab simulink 温度控制时延系统 模糊pid和smith控制
ABP中的数据过滤器
perl基础语法归纳
Quantitative Genetics Heritability Calculation 2: Half Siblings and Full Siblings
2022 High-altitude installation, maintenance, and demolition exam practice questions and mock exams
【Harmony OS】【ARK UI】自定义弹窗
【暑期每日一题】洛谷 P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
php使用phpoffice/phpspreadsheet导入导出excel表格
【Harmony OS】【ARK UI】Lightweight Data Storage