[英]How can we specify physical address for variable?
Any suggestions/discussions are welcome! 欢迎任何建议/讨论!
The question is actually brief as title, but I'll explain why I need physical address. 问题实际上是简短的标题,但我会解释为什么我需要实际地址。
Background : 背景 :
These days I'm fascinated by cache and multi-core architecture, and now I'm quite curious how cache influence our programs, under the parallel environment. 这些天我对缓存和多核架构着迷,现在我很好奇缓存如何影响我们的程序,在并行环境下。
In some CPU models (for example, my Intel Core Duo T5800), the L2 cache is shared among cores. 在某些CPU型号(例如,我的英特尔酷睿双核T5800)中,L2缓存在核心之间共享。 So, if program A is accessing memory at physical address like
因此,如果程序A正在访问物理地址等的内存
0x00000000, 0x20000000, 0x40000000...
and program B accessing data at 和程序B访问数据
0x10000000, 0x30000000, 0x50000000...
Since these addresses share the same suffix, the related set in L2 cache will be flushed frequently. 由于这些地址共享相同的后缀,因此L2缓存中的相关集将经常刷新。 And we're expected to see two programs fighting with each other, reading data slowly from memory instead of cache, although, they are separated in different cores.
我们期望看到两个程序相互争斗,从内存缓慢读取数据而不是缓存,尽管它们在不同的核心中分开。
Then I want to verify the result in practice. 然后我想在实践中验证结果。 In this experiment, I have to know the physical address instead of virtual address.
在这个实验中,我必须知道物理地址而不是虚拟地址。 But how can I cope with this?
但我怎么能应付这个呢?
The first attempt: 第一次尝试:
Eat a large space from heap, mask, and get the certain address. 从堆,面具吃一大块空间,并获得一定的地址。
My CPU has a L2 cache with size=2048KB and associativity=8, so physical addressess like 0x12340000, 0x12380000, 0x123c0000
will be related to the first set in L2 cache. 我的CPU有一个L2缓存,大小= 2048KB,关联性= 8,因此物理地址如
0x12340000, 0x12380000, 0x123c0000
将与L2缓存中的第一个设置相关。
int HEAP[200000000]={0};
int *v[2];
int main(int argc, char **argv) {
v[0] = (int*)(((unsigned)(HEAP)+0x3fffc) & 0xfffc0000);
v[1] = (int*) ((unsigned)(v[0]) + 0x40000);
// one program pollute v[0], another polluting v[1]
}
Sadly, with the "help" of virtual memory, variable HEAP
is not always continuous inside physical memory. 遗憾的是,在虚拟内存的“帮助”下,变量
HEAP
在物理内存中并不总是连续的。 v[0]
and v[1]
might be related to different cache sets. v[0]
和v[1]
可能与不同的缓存集有关。
The second attempt 第二次尝试
access /proc/self/mem
, and try to get memory information. 访问
/proc/self/mem
,并尝试获取内存信息。
Hmm... seems that the results are still about virtual memory. 嗯......似乎结果仍然是关于虚拟内存。
Your understanding of memory and these addresses is incomplete/incorrect. 您对内存和这些地址的理解不完整/不正确。 Essentially, what you're trying to test is futile.
从本质上讲,你试图测试的是徒劳的。
In the context of user-mode processes, pretty much every single address you see is a virtual address . 在用户模式进程的上下文中,您看到的几乎每个地址都是虚拟地址 。 That is, an address that makes sense only in the context of that process.
也就是说,只有在该过程的上下文中才有意义的地址。 The OS manages the mapping of where this virtual memory space (unique to a process) maps to memory pages.
操作系统管理此虚拟内存空间(进程唯一)映射到内存页面的映射。 These memory pages at any given time may map to pages that are paged-in (ie reside in physical RAM) - or they may be paged-out, and exist only in the swap file on disk.
任何给定时间的这些内存页可以映射到被分页的页面(即驻留在物理RAM中) - 或者它们可以被分页,并且仅存在于磁盘上的交换文件中。
So to address the Background example, those addresses are from two different processes - it means absolutely nothing to try and compare them. 因此,为了解决背景示例,这些地址来自两个不同的过程 - 它绝对没有什么可以尝试和比较它们。 Whether or not their code is present in any of the caches depends on a number of things, including the cache-replacement strategy of the processor, the caching policies enabled by the OS, the number of other processes (including kernel-mode threads), etc.
它们的代码是否存在于任何缓存中取决于许多因素,包括处理器的缓存替换策略 ,OS启用的缓存策略,其他进程的数量(包括内核模式线程),等等
In your first attempt , again you aren't going to get anywhere as far as actually testing CPU cache directly. 在您的第一次尝试中 ,您再也无法直接实际测试CPU缓存。 First of all, your large buffer is not going to be on the heap.
首先,你的大缓冲区不会在堆上。 It is going to be part of the data section (specifically the .bss) of the executable.
它将成为可执行文件的数据部分(特别是.bss)的一部分。 The heap is used for the
malloc()
family of memory allocations. 该堆用于
malloc()
系列内存分配。 Secondly, it doesn't really matter if you allocate some huge 1GB region, because although it is contiguous in the virtual address space of your process, it is up to the OS to allocate pages of virtual memory wherever it deems fit - which may not actually be contiguous. 其次,如果你分配一些巨大的1GB区域并不重要,因为虽然它在你的进程的虚拟地址空间中是连续的,但是由操作系统来分配虚拟内存页面,它认为合适的地方 - 这可能不是实际上是连续的。 Again, you have pretty much no control over memory allocation from userspace.
同样,您几乎无法控制来自用户空间的内存分配。 " Is there a way to allocate contiguous physical memory from userspace in linux ?"
“ 有没有办法从linux中的用户空间分配连续的物理内存 ?” The short answer is No.
最简洁的答案是不。
/proc/$pid/maps
isn't going to get you anywhere either. /proc/$pid/maps
也不会让你到任何地方。 Yes there are plenty of addresses listed in there, but again, they are all in the virtual address space of process $pid
. 是的,那里列出了很多地址,但同样,它们都在进程
$pid
的虚拟地址空间中。 Some more information on these: How do I read from /proc/$pid/mem under Linux? 有关这些的更多信息: 如何从Linux下的/ proc / $ pid / mem读取?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.