我正在尝试重现此处显示的结果 What Every programmer should know about memory ,具体结果如下图所示(论文中p20-21)
这基本上是不同工作大小的每个元素的周期图,图表中的突然上升是在工作集大小超过缓存大小的点。
为了完成这个我写了这个 code here .我看到所有数据都从内存中获取(通过每次使用 clflush 刷新缓存),性能是 对于所有数据大小都是一样的(正如预期的那样),但是随着缓存的运行,我看到了一个完全 相反趋势
Working Set: 16 Kb took 72.62 ticks per access
Working Set: 32 Kb took 46.31 ticks per access
Working Set: 64 Kb took 28.19 ticks per access
Working Set: 128 Kb took 23.70 ticks per access
Working Set: 256 Kb took 20.92 ticks per access
Working Set: 512 Kb took 35.07 ticks per access
Working Set: 1024 Kb took 24.59 ticks per access
Working Set: 2048 Kb took 24.44 ticks per access
Working Set: 3072 Kb took 24.70 ticks per access
Working Set: 4096 Kb took 22.17 ticks per access
Working Set: 5120 Kb took 21.90 ticks per access
Working Set: 6144 Kb took 23.29 ticks per access
有人可以解释这种行为吗?我相信“预取”在这里做得很好,在管道开始时将数据带到缓存中。
如果是这样,我如何重现图中显示的观察结果? 我的缓存大小是 L1 (32 Kb)、L2 (256 Kb) 和 L3 (3072 Kb)。
谢谢
最佳答案
这是我修改后的代码。它每次按 STEP 字节步进,更新内存。我选择 STEP 来匹配处理器的缓存行大小(64 字节)。它重复填充循环 REPEAT 次。它向每个缓存行写入一个字节。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define ARRAYSIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#define STEP (64)
#define REPEAT (1000)
inline void
clflush(volatile void *p)
{
asm volatile ("clflush (%0)" :: "r"(p));
}
inline uint64_t
rdtsc()
{
unsigned long a, d;
asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");
return a | ((uint64_t)d << 32);
}
//volatile int i;
volatile unsigned char data[1 << 26]; // 64MB
void sequentialAccess(int bytes)
{
for (int j = 0; j < REPEAT; j++)
for (int i = 0; i < bytes; i += STEP)
data[i] = i;
}
int rangeArr[] = {16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 10240, 12*1024, 14*1024, 16*1024};
inline void test()
{
for (int i = 0; i < ARRAYSIZE(rangeArr); i++)
{
uint64_t start, end;
int kilobytes = rangeArr[i];
start = rdtsc();
sequentialAccess(kilobytes * 1024);
end = rdtsc();
double ticksPerAccess = 1.0 * (end - start) / (kilobytes * 1024 / STEP) / REPEAT;
printf("%d kB took %lf ticks per access\n", kilobytes, ticksPerAccess);
}
}
int
main(int ac, char **av)
{
test();
return 0;
}
在我的“AMD Phenom(tm) II X4 965 处理器”(来自 /proc/cpuinfo 的字符串)上,我得到了以下结果:
16 kB took 9.148758 ticks per access
32 kB took 8.855980 ticks per access
64 kB took 9.008148 ticks per access
128 kB took 17.197035 ticks per access
256 kB took 14.416313 ticks per access
512 kB took 15.845552 ticks per access
1024 kB took 21.394375 ticks per access
2048 kB took 21.379112 ticks per access
3072 kB took 21.399206 ticks per access
4096 kB took 21.630234 ticks per access
6144 kB took 23.907972 ticks per access
8192 kB took 46.306525 ticks per access
10240 kB took 49.292271 ticks per access
12288 kB took 49.575894 ticks per access
14336 kB took 49.758874 ticks per access
16384 kB took 49.660779 ticks per access
这看起来有点像乌尔里希曲线。
编辑:在仔细检查 Ulrich Drepper 的原始基准后,我意识到他在测量区域之外构建一个链表,然后测量追踪该链表的成本。它测量一个称为“加载使用延迟”的参数,这是从内存系统中提取的一个非常有用的参数。
我相信以下代码更接近最初的理想。它还显着增加了迭代次数,以确保我的处理器上的节能功能不会启动。
在下面的代码中,您可以调整 NPAD 以匹配处理器的缓存行大小。您可以调整 ACCESSES 以控制基准循环迭代的次数。总迭代次数完全独立于数据集大小。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define NPAD (64 - sizeof(void *))
#define ACCESSES (1 << 28)
inline void
clflush(volatile void *p)
{
asm volatile ("clflush (%0)" :: "r"(p));
}
inline uint64_t
rdtsc()
{
unsigned long a, d;
asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");
return a | ((uint64_t)d << 32);
}
struct l
{
l *next;
char pad[NPAD];
};
l array[ (1 << 26) / sizeof(l) ];
void init_sequential(int bytes)
{
int elems = bytes / sizeof(l);
for (int i = 1; i < elems; i++)
{
array[i - 1].next = &array[i];
}
array[elems - 1].next = &array[0];
}
void measure_baseline( int accesses )
{
volatile l *ptr = &array[0];
while (accesses-- > 0)
ptr = ptr->next;
}
int rangeArr[] = {16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 10240, 12*1024, 14*1024, 16*1024};
inline void test()
{
for (int i = 0; i < sizeof(rangeArr) / sizeof(rangeArr[0]); i++)
{
uint64_t start, end;
int kilobytes = rangeArr[i];
init_sequential( kilobytes * 1024 );
start = rdtsc();
measure_baseline( ACCESSES );
end = rdtsc();
double ticksPerAccess = 1.0 * (end - start) / ACCESSES;
printf("%d kB took %lf ticks per access\n", kilobytes, ticksPerAccess);
}
}
int
main(int ac, char **av)
{
test();
return 0;
}
这是从我的处理器收集的数据:
16 kB took 3.062668 ticks per access
32 kB took 3.002012 ticks per access
64 kB took 3.001376 ticks per access
128 kB took 9.204764 ticks per access
256 kB took 9.284414 ticks per access
512 kB took 15.848642 ticks per access
1024 kB took 22.645605 ticks per access
2048 kB took 22.698062 ticks per access
3072 kB took 23.039498 ticks per access
4096 kB took 23.481494 ticks per access
6144 kB took 37.720315 ticks per access
8192 kB took 55.197783 ticks per access
10240 kB took 55.886692 ticks per access
12288 kB took 56.262199 ticks per access
14336 kB took 56.153559 ticks per access
16384 kB took 55.879395 ticks per access
这显示了在 L1D 中使用数据延迟的 3 个周期负载,这是我对该处理器(以及大多数主流高性能 PC 专用处理器)的预期。
关于c++ - CPU 缓存的这种性能行为的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592813/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我试过重新启动apache,缓存的页面仍然出现,所以一定有一个文件夹在某个地方。我没有“公共(public)/缓存”,那么我还应该查看哪些其他地方?是否有一个URL标志也可以触发此效果? 最佳答案 您需要触摸一个文件才能清除phusion,例如:touch/webapps/mycook/tmp/restart.txt参见docs 关于ruby-如何在Ubuntu中清除RubyPhusionPassenger的缓存?,我们在StackOverflow上找到一个类似的问题:
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
两个gsub产生不同的结果。谁能解释一下为什么?代码也可在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得.ver=9999str="\tCFBundleDevelopmentRegion\n\ten\n\tCFBundleVersion\n\t0.1.190\n\tAppID\n\t000000000000000"putsstr.gsub/(CFBundleVersion\n\t.*\.).*()/,"#{$1}#{ver}#{$2}"puts'--------'putsstr.gsub/(CFBundleVersio
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我在一段非常简单的代码(如我所想)中得到了一个错误的值:org=4caseorgwhenorg=4val='H'endputsval=>nil请不要生气,我希望我错过了一些非常明显的东西,但我真的想不通。谢谢。 最佳答案 这是典型的Ruby错误。case有两种被调用的方法,一种是你传递一个东西作为分支的基础,另一种是你不传递的东西。如果您确实在case中指定了一个表达式语句然后评估所有其他条件并与===进行比较.在这种情况下org评估为false和org===false显然不是真的。所有其他情况也是如此,它们要么是真的,要么是假的。
假设您在Ruby中执行此操作:ar=[1,2]x,y=ar然后,x==1和y==2。是否有一种方法可以在我自己的类中定义,从而产生相同的效果?例如rb=AllYourCode.newx,y=rb到目前为止,对于这样的赋值,我所能做的就是使x==rb和y=nil。Python有这样一个特性:>>>classFoo:...def__iter__(self):...returniter([1,2])...>>>x,y=Foo()>>>x1>>>y2 最佳答案 是的。定义#to_ary。这将使您的对象被视为要分配的数组。irb>o=Obje