欢迎任何建议/讨论!
问题实际上是简短的标题,但我会解释为什么我需要实际地址.
背景:
这些天我对缓存和多核架构着迷,现在我很好奇缓存如何影响我们的程序,在并行环境下.
在某些CPU型号(例如,我的英特尔酷睿双核T5800)中,L2缓存在核心之间共享.所以,如果程序A正在访问像物理地址那样的内存
0x00000000, 0x20000000, 0x40000000...
和程序B访问数据
0x10000000, 0x30000000, 0x50000000...
由于这些地址共享相同的后缀,因此L2缓存中的相关集将经常刷新.我们期望看到两个程序相互争斗,从内存缓慢读取数据而不是缓存,尽管它们在不同的核心中分开.
然后我想在实践中验证结果.在这个实验中,我必须知道物理地址而不是虚拟地址.但我怎么能应付这个呢?
第一次尝试:
从堆,面具吃一大块空间,并获得一定的地址.
我的CPU有一个L2缓存,大小= 2048KB,关联性= 8,因此物理地址类似于0x12340000, 0x12380000, 0x123c0000L2缓存中的第一个设置.
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]
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,在虚拟内存的"帮助"下,变量HEAP在物理内存中并不总是连续的.v[0]并且v[1]可能与不同的缓存集有关.
第二次尝试
访问/proc/self/mem,并尝试获取内存信息.
嗯......似乎结果仍然是关于虚拟内存.
我或python是否与以下代码混淆?我希望__le__被召唤a <= ab,而不是__ge__:
#!/usr/bin/env python2
class B(object):
def __ge__(self, other):
print("__ge__ unexpectedly called")
class A(object):
def __le__(self, other):
print("__le__ called")
class AB(A, B):
pass
a = A()
ab = AB()
a <= ab # --> __ge__ unexpectedly called
ab <= a # --> __le__ called
Run Code Online (Sandbox Code Playgroud)
我得到了与python 2.7,3.2和pypy 1.9相同的行为.
我该怎么办才能被__le__召唤而不是__ge__?
正如这里所说,该命令[ -d FILE ]用于检查文件是否是目录.
但是,当我们错过FILE表达式中的参数时,它仍然返回true.
为什么?shell如何解释这个表达式呢?
示例应该很简单:)
$ [ -d /tmp ]
$ echo $? # prints 0
$ [ -d ]
$ echo $? # why prints 0 as well?
Run Code Online (Sandbox Code Playgroud) 这是意外情况:在以下脚本中,SIGALRM没有alarm()在预期时间调用该函数。
#!/bin/sh -x
alarm() {
echo "alarmed!!!"
}
trap alarm 14
OUTER=$(exec sh -c 'echo $PPID')
#for arg in `ls $0`; do
ls $0 | while read arg; do
INNER=$(exec sh -c 'echo $PPID')
# child A, the timer
sleep 1 && kill -s 14 $$ &
# child B, some other scripts
sleep 60 &
wait $!
done
Run Code Online (Sandbox Code Playgroud)
预期:1 秒后,alarm()应调用该函数。
实际上:
alarm()一直被调用直到 60 秒,或者当我们点击Ctrl+时C。
我们知道在脚本中,$$ …