关于再造Android的安全系统的思考

April 28th, 2013 Comments off

ruby on rails有个显式的路由表(routes.rb),用来把http请求匹配到不同计算资源。Android Intent的解析中转中,能不能引入一下这个机制,除了应用厂家做的对permission的需求之外(Android是通过这个Permission确定当前的Intent是否合法的),它发出的请求要经过一个路由表过滤,这个路由表由系统软件厂家(如Google官方)确定,甚至这个表里有时间限定等因素(如某时间段时,某应用做某行为是合法的)。

面向消费者的设备,不能在UI上做得太复杂,那就让“官家”(当然包括消费者自己)可以监视这些行为,做到监控与使用者的分离。

趁现在Elastos重新设计Android的机会,把这些思想做进去。

我的这些话需要熟悉Android framework实现及有互联网Ruby编程经验的人才能理解。

Android的安全问题是一个应用,一旦闯过安装确认这一关(Android应用安装时,有权限要求,要求用户确认),就一切畅通无阻,这时系统软件厂家、用户自己对它干坏事都无能为力。如果我们能在应用使用它的资源(即通过Permission控制的资源)中间架起一个审计机构,大概能解决这个问题。这也符合三权分立的思想。

 | 61 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

我觉得wordpress的filter、action模型是比较好的基于元数据编程的事件驱动模型,不知道有没有基于java的实现。wordpress是基于PHP的。不过这个模型有点重。

April 18th, 2013 Comments off

我觉得wordpress的filter、action模型是比较好的基于元数据编程的事件驱动模型,不知道有没有基于java的实现。wordpress是基于PHP的。不过这个模型有点重。

 | 45 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

从http的header联想到异步访问的问题

April 14th, 2013 Comments off

前几天,Github将所有Pages从主域名,移到了github.io。原因是防止有人利用同域的cookie,劫持用户的session。这里有一篇他们工程师的文章,详细分析了这种攻击的原理和应对方法,读后非常长知识。Cookies 的跨域脚本攻击 – Github 迁移域名的安全详解

Android上Intent与RPC(CAR接口暴露直接访问,需要共同的class环境)的差异性的问题。异步访问,互相之间REST,一次一了,不保持连接,然后就需要http的Header里的这一大堆东西,不象保持联接的,咱俩连接上了,合体了,啥都是信任的。不保持连接,降低了效率表现在好多方面。

同步API直接调用,为什么不被OSGi、Android这样的框架采用,因为涉及到调用安全的问题,C访问S,可S的生命周期与C不同步,当S被终止了,C再访问那个地址,就出错了。应用程序之于OS,也是C与S的关系,为什么不担心S挂了?因为应用程序要无条件信息OS。在被重构了的Android里,这是一个新的设计点。

 | 29 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

dalvik直接支持RPC

April 8th, 2013 3 comments

Android每个应用进程都有一个dalvik,可以让这些dalvik直接协作起来,基于IBinder,虚拟机群集。

dalvik最清楚当前进程里有哪些class,哪些object。它把这些让你可见,你就可以直接调用了,不用走INTENT机制了。以对象机制看见当前系统内的服务,这是当前OS软件人的理想。通过Dalvik级的资源暴露,你的程序可以远程看见服务者的接口,如果处于一个共同的类空间(就是你的classpath中有那个要访问的类的源码,这样才有元数据)还可以直接调用,对象是find出来的,不是new出来的。Intent还是不够方便。

 | 44 views | 3 comments | 0 flags

Categories: Uncategorized Tags:

关于软件计算机的思考

April 4th, 2013 Comments off

现在的web计算是不是软件计算机?想来不是,一是客户端只由主要功能为人机交互的浏览器组成,不能让很多设备共同参与;二是计算时的协作性不够,服务器端太透明,你不能指挥服务器替你协同;三是计算间的交互没有,哥儿几个能不能协作性地访问服务器呢?现在的协作是人工参与的,不是计算基础设施。
从这个角度想事情,Elastos大有可为。它超越了浏览器,是程序浏览器。不面向几台机器在想事情,而是面向一群机器,境界高了一层。

 | 31 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

Anatomy of a Program in Memory

March 27th, 2013 Comments off

http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

Memory management is the heart of operating systems; it is crucial for both programming and system administration. In the next few posts I’ll cover memory with an eye towards practical aspects, but without shying away from internals. While the concepts are generic, examples are mostly from Linux and Windows on 32-bit x86. This first post describes how programs are laid out in memory.

Each process in a multi-tasking OS runs in its own memory sandbox. This sandbox is the virtual address space, which in 32-bit mode is always a 4GB block of memory addresses. These virtual addresses are mapped to physical memory by page tables, which are maintained by the operating system kernel and consulted by the processor. Each process has its own set of page tables, but there is a catch. Once virtual addresses are enabled, they apply to all software running in the machine, including the kernel itself. Thus a portion of the virtual address space must be reserved to the kernel:

Kernel/User Memory Split

This does not mean the kernel uses that much physical memory, only that it has that portion of address space available to map whatever physical memory it wishes. Kernel space is flagged in the page tables as exclusive to privileged code (ring 2 or lower), hence a page fault is triggered if user-mode programs try to touch it. In Linux, kernel space is constantly present and maps the same physical memory in all processes. Kernel code and data are always addressable, ready to handle interrupts or system calls at any time. By contrast, the mapping for the user-mode portion of the address space changes whenever a process switch happens:

Process Switch Effects on Virtual Memory

Blue regions represent virtual addresses that are mapped to physical memory, whereas white regions are unmapped. In the example above, Firefox has used far more of its virtual address space due to its legendary memory hunger. The distinct bands in the address space correspond to memory segments like the heap, stack, and so on. Keep in mind these segments are simply a range of memory addresses and have nothing to do with Intel-style segments. Anyway, here is the standard segment layout in a Linux process:

Flexible Process Address Space Layout In Linux

When computing was happy and safe and cuddly, the starting virtual addresses for the segments shown above were exactly the same for nearly every process in a machine. This made it easy to exploit security vulnerabilities remotely. An exploit often needs to reference absolute memory locations: an address on the stack, the address for a library function, etc. Remote attackers must choose this location blindly, counting on the fact that address spaces are all the same. When they are, people get pwned. Thus address space randomization has become popular. Linux randomizes thestackmemory mapping segment, and heap by adding offsets to their starting addresses. Unfortunately the 32-bit address space is pretty tight, leaving little room for randomization andhampering its effectiveness.

The topmost segment in the process address space is the stack, which stores local variables and function parameters in most programming languages. Calling a method or function pushes a newstack frame onto the stack. The stack frame is destroyed when the function returns. This simple design, possible because the data obeys strict LIFO order, means that no complex data structure is needed to track stack contents – a simple pointer to the top of the stack will do. Pushing and popping are thus very fast and deterministic. Also, the constant reuse of stack regions tends to keep active stack memory in the cpu caches, speeding up access. Each thread in a process gets its own stack.

It is possible to exhaust the area mapping the stack by pushing more data than it can fit. This triggers a page fault that is handled in Linux by expand_stack(), which in turn callsacct_stack_growth() to check whether it’s appropriate to grow the stack. If the stack size is belowRLIMIT_STACK (usually 8MB), then normally the stack grows and the program continues merrily, unaware of what just happened. This is the normal mechanism whereby stack size adjusts to demand. However, if the maximum stack size has been reached, we have a stack overflow and the program receives a Segmentation Fault. While the mapped stack area expands to meet demand, it does not shrink back when the stack gets smaller. Like the federal budget, it only expands.

Dynamic stack growth is the only situation in which access to an unmapped memory region, shown in white above, might be valid. Any other access to unmapped memory triggers a page fault that results in a Segmentation Fault. Some mapped areas are read-only, hence write attempts to these areas also lead to segfaults.

Below the stack, we have the memory mapping segment. Here the kernel maps contents of files directly to memory. Any application can ask for such a mapping via the Linux mmap() system call (implementation) or CreateFileMapping() / MapViewOfFile() in Windows. Memory mapping is a convenient and high-performance way to do file I/O, so it is used for loading dynamic libraries. It is also possible to create an anonymous memory mapping that does not correspond to any files, being used instead for program data. In Linux, if you request a large block of memory via malloc(), the C library will create such an anonymous mapping instead of using heap memory. ‘Large’ means larger than MMAP_THRESHOLD bytes, 128 kB by default and adjustable via mallopt().

Speaking of the heap, it comes next in our plunge into address space. The heap provides runtime memory allocation, like the stack, meant for data that must outlive the function doing the allocation, unlike the stack. Most languages provide heap management to programs. Satisfying memory requests is thus a joint affair between the language runtime and the kernel. In C, the interface to heap allocation is malloc() and friends, whereas in a garbage-collected language like C# the interface is thenew keyword.

If there is enough space in the heap to satisfy a memory request, it can be handled by the language runtime without kernel involvement. Otherwise the heap is enlarged via the brk() system call (implementation) to make room for the requested block. Heap management is complex, requiring sophisticated algorithms that strive for speed and efficient memory usage in the face of our programs’ chaotic allocation patterns. The time needed to service a heap request can vary substantially. Real-time systems have special-purpose allocators to deal with this problem. Heaps also becomefragmented, shown below:

Fragmented Heap

Finally, we get to the lowest segments of memory: BSS, data, and program text. Both BSS and data store contents for static (global) variables in C. The difference is that BSS stores the contents ofuninitialized static variables, whose values are not set by the programmer in source code. The BSS memory area is anonymous: it does not map any file. If you say static int cntActiveUsers, the contents of cntActiveUsers live in the BSS.

The data segment, on the other hand, holds the contents for static variables initialized in source code. This memory area is not anonymous. It maps the part of the program’s binary image that contains the initial static values given in source code. So if you say static int cntWorkerBees = 10, the contents of cntWorkerBees live in the data segment and start out as 10. Even though the data segment maps a file, it is a private memory mapping, which means that updates to memory are not reflected in the underlying file. This must be the case, otherwise assignments to global variables would change your on-disk binary image. Inconceivable!

The data example in the diagram is trickier because it uses a pointer. In that case, the contents of pointer gonzo – a 4-byte memory address – live in the data segment. The actual string it points to does not, however. The string lives in the text segment, which is read-only and stores all of your code in addition to tidbits like string literals. The text segment also maps your binary file in memory, but writes to this area earn your program a Segmentation Fault. This helps prevent pointer bugs, though not as effectively as avoiding C in the first place. Here’s a diagram showing these segments and our example variables:

ELF Binary Image Mapped Into Memory

You can examine the memory areas in a Linux process by reading the file /proc/pid_of_process/maps. Keep in mind that a segment may contain many areas. For example, each memory mapped file normally has its own area in the mmap segment, and dynamic libraries have extra areas similar to BSS and data. The next post will clarify what ‘area’ really means. Also, sometimes people say “data segment” meaning all of data + bss + heap.

You can examine binary images using the nm and objdump commands to display symbols, their addresses, segments, and so on. Finally, the virtual address layout described above is the “flexible” layout in Linux, which has been the default for a few years. It assumes that we have a value forRLIMIT_STACK. When that’s not the case, Linux reverts back to the “classic” layout shown below:

Classic Process Address Space Layout In Linux

That’s it for virtual address space layout. The next post discusses how the kernel keeps track of these memory areas. Coming up we’ll look at memory mapping, how file reading and writing ties into all this and what memory usage figures mean.

 | 80 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

理解 chroot

March 27th, 2013 Comments off

理解 chroot

通过编写 chroot 来认识 chroot 发挥的作用和它带来的好处

王 华东 (wstoneh@126.com), 自由职业者

 http://www.ibm.com/developerworks/cn/linux/l-cn-chroot/

 

简介: chroot 在 Linux 系统中发挥了根目录的切换工作,同时带来了系统的安全性等好处。本文通过编写 chroot 来理解 chroot 的作用和好处,这不仅有助于更好的使用 chroot,同时加深了对 Linix 系统初始 RAM 磁盘工作的认识。

什么是 chroot

 

 

chroot,即 change root directory (更改 root 目录)。在 linux 系统中,系统默认的目录结构都是以 `/`,即是以根 (root) 开始的。而在使用 chroot 之后,系统的目录结构将以指定的位置作为 `/` 位置。
图 1. Linux 系统的目录结构
Linux 系统的目录结构

回页首

为何使用 chroot

在经过 chroot 之后,系统读取到的目录和文件将不在是旧系统根下的而是新根下(即被指定的新的位置)的目录结构和文件,因此它带来的好处大致有以下3个:

  1. 增加了系统的安全性,限制了用户的权力;在经过 chroot 之后,在新根下将访问不到旧系统的根目录结构和文件,这样就增强了系统的安全性。这个一般是在登录 (login) 前使用 chroot,以此达到用户不能访问一些特定的文件。
  2. 建立一个与原系统隔离的系统目录结构,方便用户的开发;使用 chroot 后,系统读取的是新根下的目录和文件,这是一个与原系统根下文件不相关的目录结构。在这个新的环境中,可以用来测试软件的静态编译以及一些与系统不相关的独立开发。
  3. 切换系统的根目录位置,引导 Linux 系统启动以及急救系统等。chroot 的作用就是切换系统的根位置,而这个作用最为明显的是在系统初始引导磁盘的处理过程中使用,从初始 RAM 磁盘 (initrd) 切换系统的根位置并执行真正的 init。另外,当系统出现一些问题时,我们也可以使用 chroot 来切换到一个临时的系统。

回页首

chroot 的使用

为了更好的理解 chroot 发挥的作用,我们将尝试指定一个特定的位置进行根目录切换。但是由于在经过 chroot 之后,系统读取到的 bin/ 等与系统相关目录将不再是旧系统根目录下的,而是切换后新根下的目录结构和文件,因此我们有必要准备一些目录结构以及必要的文件。

清单 1. 准备切换的目录结构

busybox

Busybox 被称为是嵌入式 Linux 中的瑞士军刀。Busybox 包含了许多有用的命令,如 cat、find 等,但是它的体积却非常的小。

$ pwd
/home/wstone/Build/work
$ tree .
.
|– bin
| |– ash -> busybox
| |– bash
| `– busybox
|– etc
`– newhome

这里使用了静态编译后的 busybox 来提供必要的命令,使用静态编译仅是为了避免动态库文件的拷贝。当然我们也可以拷贝旧系统的下的命令到新的目录结构中使用,但是那些命令通常是动态编译的,这就意味着我们不得不拷贝相关的动态库文件到相应的目录结构中。同时这里的 bash 也非真正的 Bourne Again shell,而是一个执行 ash 的 shell 脚本。在清单 2中,展示了位于旧系统中的 chroot 命令的使用。需要注意的是在使用 chroot 时,要求拥有相关的操作权限。
清单 2. 位于系统中的 chroot 的使用

$ pwd
/home/wstone/Build/work

# chroot .
# pwd
/

# ls
ash: ls: not found

# busybox ls
bin      etc      newhome

3 directories, 3 files

 

我们可以看到当前路径(/home/wstone/Build/work/),在经过 chroot 后转变成了 `/` 目录,同时从新根下读取了与系统相关的目录结构。使用 ls 命令失败是由于我们创建的测试目录结构中并没有包含命令 ls,但是我们成功的使用了 busybox 中的 ls。以上看到的只是 chroot 的一种使用方式,其实标准的 chroot (Coreutils – GNU core utilities 提供的 chroot)使用方式有2种:
清单 3. 标准 chroot 的2种使用方式

[1] chroot NEWROOT [COMMAND...]
[2] chroot OPTION

 

刚才我们使用的是方式[2]。这将在没有给定环境时,默认执行 `/bin/sh`,但是当给定环境后,将运行 `${SHELL} –i`,即与环境相同的可交互的 shell。我们的目录结构中并没有包含sh,显然清单 2中的 chroot 运行了 `${SHELL} –i`。当然我们也可以在进行切换时指定需要的命令,即使用方式[1]。

清单 4. chroot 另一种方式的使用
# chroot . /bin/ash
#

清单 4 中,尝试了在经过 chroot 后,执行新目录结构下的 ash shell。不得不说的是,如果新根下的目录结构和文件准备的够充分,那么一个新的简单的 Linux 系统就可以使用了。其实更为常见的是在初始 RAM 磁盘 (initrd)中使用 chroot,以此来执行系统的init清单 5 中,展示的是在 Linux 2.4 内核 initrd 中使用 chroot。
清单 5. 在 Linux 2.4 内核 initrd 中使用 chroot 的示例

mount /dev/hda1 /new-root
cd /new-root
pivot_root . old-root
exec chroot . /sbin/init <dev/console >dev/console 2>&1
umount /old-root

 

由于 Linux 内核的升级,initrd 处理机制和格式发生了变化,在 Linux 2.6 内核 initrd 中不能再使用 pivot_root,因此一般也不再使用 chroot,而是选择使用 busybox 提供的 switch_root 或者 klibc 提供的 run-init 进行根目录的切换。(这并不是说不能在 Linux 2.6内核 initrd 中使用 chroot,选择 switch_root 或 run-init 仅是出于习惯和方便的考虑。)但是实质上,它们仅是将 chroot 的功能进行了封装,以此更加方便简单的切换根目录。
清单 6. 在 Linux 2.6 内核 initrd 中 chroot 的使用

[1] find -xdev / -exec rm '{}' ';
[2] cd /newmount; mount --move . /; chroot .

 

switch_root 和 run-init 完成了类似清单 6中的功能,删除 rootfs 的全部内容以释放空间,以及挂载新的根文件系统并进行切换。在 busybox 和 klibc中也有提供 chroot 命令,只是功能上与 Coreutils (GNU core utilities) 包含的 chroot 有稍许差异。

回页首

编写一个 chroot

上面介绍了 chroot 及其使用,但是编写一个简单的 chroot 并不复杂,下面我们就尝试编写chroot 以此来更好的认识 chroot 的处理过程,先编写一个粗略的 chroot 然后再完善它的功能。chroot 的编写涉及了2个函数,chroot() 以及 chdir(),它们都包含在 unistd.h 头文件中。
清单 7. 编写 chroot 涉及的2个函数

#include <unistd.h>
int chroot(const char *path);
int chdir(const char *path);

 

chroot() 将切换参数 path 所指位置为根目录 (/),chdir() 用来将当前的工作目录改变成以参数path 所指的目录。以此我们可以编写一个非常粗略的 `chroot`。
清单 8. 粗略的 `chroot`

#include <unistd.h>

int main(int argc, char *argv[])
{
    chroot(".");
    chdir("/");

    char *arrays[]={"ash",NULL};
    execvp("ash", arrays);

    return 0;
}

 

这个粗略的 `chroot` 仅能切换当前位置为根目录,同时默认执行 ash shell,不包含任何的错误处理及警告。编写并保存代码为test.c。在清单 9 中,展示了这个粗略 `chroot` 的使用情况,成功的进行了根目录的切换。
清单 9. 粗略 `chroot` 的使用

$ gcc -Wall test.c -o test

# ./test
# ls
ash: ls: not found

# busybox ls
bin      etc      newhome  test     test.c

 

下面给出功能将近完整的 chroot ,加上了一些错误处理并新增了可执行指定命令的功能。当在没有给出 chroot 切换后要执行的命令时,默认执行 `/bin/sh`,同时检测环境以确认使用何种 shell。
清单 10. 功能完整的 chroot

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if(argc<2){
        printf("Usage: chroot NEWROOT [COMMAND...] \n");
        return 1;
    }

    printf("newroot = %s\n", argv[1]);
    if(chroot(argv[1])) {
        perror("chroot");
        return 1;
    }

    if(chdir("/")) {
        perror("chdir");
        return 1;
    }

    if(argc == 2) {
        argv[0] = getenv("SHELL");
        if(!argv[0])
            argv[0] = (char *)"/bin/sh";

        argv[1] = (char *) "-i";
        argv[2] = NULL;
    } else {
        argv += 2;
    }

    execvp (argv[0], argv);
    printf("chroot: cannot run command `%s`\n", *argv);

    return 0;
}

 

保存以上代码为 newchroot.c 文件,编译后运行测试其功能。最后要指出的是,本文中的 `chroot` 并没有使用静态编译。如果有必要(如,在 initrd 中使用 chroot),chroot 应该使用静态编译,若是使用动态编译,那么要拷贝相关的动态库文件到相应目录结构中。
清单 11. `newchroot` 的测试

$ gcc -Wall newchroot.c -o newchroot

# ./newchroot . /bin/ash
newroot = .
#

 

回页首

结束语

在 Linux 系统初始引导的过程中,通常都有使用 chroot。但是 chroot 的好处不仅于此,它还增加了系统的安全性等。而通过本文后半部分对 chroot 的认识,我相信读者可以更好的发挥chroot 的作用。

 

回页首

下载

描述 名字 大小 下载方法
样例代码 work.tar.bz2 734KB HTTP

关于下载方法的信息

 

参考资料

关于作者

王华东,自由职业者,熟悉 Linux 系统管理,对 Linux 和 Open source 有浓厚的兴趣。通过 wstoneh@126.com 可以和他联系。

 | 51 views | 0 comments | 0 flags

Categories: Uncategorized Tags:

CPU缓存刷新的误解 http ifeve com cpu cache flushing fallacy…

March 26th, 2013 1 comment

CPU缓存刷新的误解

http://ifeve.com/cpu-cache-flushing-fallacy-cn/

原文地址  作者:Mechanical Sympathy  译者:潘曦  校对:Simon-SZ ,方腾飞

即使是资深的技术人员,我经常听到他们谈论某些操作是如何导致一个CPU缓存的刷新。看来这是关于CPU缓存如何工作和缓存子系统如何与执行核心交互的一个常见误区。本文将致力于解释CPU缓存的功能以及执行程序指令的CPU核心如何与缓存交互。我将以最新的Intel x86 CPU为例进行说明,其他CPU也使用相似技术以达到相同目的。

绝大部分常见的现代系统都被设计成在多处理器上共享内存。共享内存的系统都有一个单独的内存资源,它会被两个或者更多的独立CPU核心同时访问。核心到主存的延迟变化范围很大,大约在10-100纳秒。在100ns内,一个3GH的CPU可以处理多达1200条指令。每一个Sandy Bridge的CPU核心,在每个CPU时钟内可以并行处理4条指令。CPU使用缓存子系统避免了处理核心直接访问主存的延迟,这样能使CPU更高效的处理指令。一些缓存很小、非常快速并且集成在每个核心之内;而另一些则慢一些、更大、在各个核心间共享。这些缓存与寄存器和主内存一起构成了非持久性的内存体系。

当你在设计一个重要算法时要记住,缓存不命中所导致的延迟,可能会使你失去执行500条指令时间!这还仅是在单插槽(single-socket)系统上,如果是多插槽(multi-socket)系统,由于内存访问需要跨槽交互,可能会导致双倍的性能损失。

内存体系

图1.对于2012 Sandy Bridge核心来说,内存模型可以大致按照如下进行分解:

1.寄存器:在每个核心上,有160个用于整数和144个用于浮点的寄存器单元。访问这些寄存器只需要一个时钟周期,这构成了对执行核心来说最快的内存。编译器会将本地变量和函数参数分配到这些寄存器上。当使用超线程技术( hyperthreading )时,这些寄存器可以在超线程协同下共享。

2.内存排序缓冲(Memory Ordering Buffers (MOB) ):MOB由一个64长度的load缓冲和36长度的store缓冲组成。这些缓冲用于记录等待缓存子系统时正在执行的操作。store缓冲是一个完全的相关性队列,可以用于搜索已经存在store操作,这些store操作在等待L1缓存的时候被队列化。在数据与缓存子系统传输时, 缓冲可以让处理器异步运转。当处理器异步读或者异步写的时候,结果可以乱序返回。为了使之与已发布的内存模型( memory model )一致,MOB用于消除load和store的顺序。

3.Level 1 缓存:L1是一个本地核心内的缓存,被分成独立的32K数据缓存和32K指令缓存。访问需要3个时钟周期,并且当指令被核心流水化时, 如果数据已经在L1缓存中的话,访问时间可以忽略。

4.L2缓存:L2缓存是一个本地核心内的缓存,被设计为L1缓存与共享的L3缓存之间的缓冲。L2缓存大小为256K,主要作用是作为L1和L3之间的高效内存访问队列。L2缓存同时包含数据和指令。L2缓存的延迟为12个时钟周期。

5.L3缓存: 在同插槽的所有核心都共享L3缓存。L3缓存被分为数个2MB的段,每一个段都连接到槽上的环形网络。每一个核心也连接到这个环形网络上。地址通过hash的方式映射到段上以达到更大的吞吐量。根据缓存大小,延迟有可能高达38个时钟周期。在环上每增加一个节点将消耗一个额外的时钟周期。缓存大小根据段的数量最大可以达到20MB。L3缓存包括了在同一个槽上的所有L1和L2缓存中的数据。这种设计消耗了空间,但是使L3缓存可以拦截对L1和L2缓存的请求,减轻了各核心私有的L1和L2缓存的负担。

6.主内存:在缓存完全没命中的情况下,DRAM通道到每个槽的延迟平均为65ns。具体延迟多少取决于很多因素,比如,下一次对同一缓存 行中数据的访问将极大降低延迟,而当队列化效果和内存刷新周期冲突时将显著增加延迟。每个槽使用4个内存通道聚合起来增加吞吐量,并通过在独立内存通道上流水线化( pipelining )将隐藏这种延迟。

7. NUMA:在一个多插槽的服务器上,会使用非一致性内存访问( non-uniform memory access )。所谓的非一致性是指,需要访问的内存可能在另一个插槽上,并且通过 QPI 总线访问需要额外花费40ns。 Sandy Bridge对于以往的兼容系统来说,在2插槽系统上是一个巨大的进步。在 Sandy Bridge上,QPI总线的能力从6.4GT/s提升到8.0GT/s,并且可以使用两条线路,消除了以前系统的瓶颈。对于 Nehalem and Westmere 来说,QPI只能使用内存控制器为一个单独插槽分配的带宽中的40%,这使访问远程内存成为一个瓶颈。另外,现在QPI链接可以使用预读取请求,而前一代系统不行。

关联度(Associativity Levels)

缓存是一个依赖于hash表的高效硬件。使用hash函数常常只是将地址中低位bit 进行映射 ,以实现缓存索引。hash表需要有解决对于同一位置冲突的机制。 关联度就是hash表中槽(slot)的数量,也被称为组(ways)和集合(sets),可以用来存储一个内存地址的hash版本。关联度的多少需要在存储数据的容量,耗电量和查询时间之间寻找平衡。(校对注:关联度越高,槽的数量越多,hash冲突越小,查询速度越快)

对于Sandy Bridge,L1和L2是8路组相连 ,L3是12路组相连 。(For Sandy Bridge the L1D and L2 are 8-way associative, the L3 is 12-way associative.)

缓存一致性

由于一些缓存在内核本地,我们需要一些方法保证一致性,使所有核心的内存视图一致。对于主流系统来说,内存子系统需要考虑“真实的来源(source of truth)”。如果数据只从缓存中来,那么它永远不会过期;当数据同时在缓存和主内存中存在时,缓存中存的是主拷贝(master copy)。这种内存管理被称为写-回(write-back),在此方式下,当新的缓存行占用旧行,导致旧行被驱逐时,缓存数据只会被写回主内存中。x86架构的每个缓存块的大小为64 bytes,称为缓存行( cache-line)。其它种类的处理器的缓存行大小可能不同。更大的缓存行容量降低延迟,但是需要更大的带宽校对注:数据总线带宽)。

为了保证缓存的一致性,缓存控制器跟踪每一个缓存行的状态,这些状态的数量是有限的。Intel使用MESIF协议,AMD使用 MOESI。在MESIF协议下,缓存行处于以下5个状态中的1个。

被修改(Modified):表明缓存行已经过期,在接下来的场景中要写回主内存。当写回主内存后状态将转变为排它( Exclusive )。

独享(Exclusive)表明缓存行被当前核心单独持有,并且与主内存中一致。当被写入时,状态将转变为修改(Modified)。要进入这个状态,需要发送一个 Request-For-Ownership (RFO)消息,这包含一个读操作再加上广播通知其他拷贝失效。

共享(Shared):表明缓存行是一个与主内存一致的拷贝。

失效(Invalid):表明是一个无效的缓存行。

向前( Forward ):一个特殊的共享状态。用来表示在NUMA体系中响应其他缓存的特定缓存。

为了从一个状态转变为另一个状态,在缓存之间,需要发送一系列的消息使状态改变生效。对于上一代(或之前)的 Nehalem核心的Intel CPU和Opteron核心的AMD CPU,插槽之间确保缓存一致性的流量需要通过内存总线共享,这极大地限制了可扩展性。如今,内存控制器的流量使用一个单独的总线来传输。例如,Intel的QPI和AMD的HyperTransport就用于插槽间的缓存一致性通讯。

缓存控制器作为L3缓存段的一个模块连接到插槽上的环行总线网络。每一个核心,L3缓存段,QPI控制器,内存控制器和集成图形子系统都连接到这个环行总线上。环由四个独立的通道构成,用于:在每个时钟内完成请求、嗅探、确认和传输32-bytes的数据(The ring is made up of 4 independent lanes for: requestsnoopacknowledge, and 32-bytesdata per cycle)。L3缓存包含所有L1和L2缓存中的缓存行,这有助于帮助核心在嗅探变化时快速确认改变的行。用于L3缓存段的缓存控制器记录了哪个核心可能改变自己的缓存行。

如果一个核心想要读取一些数据,并且这些数据在缓存中并不处于共享、独占或者被修改状态;那么它就需要在环形总线上做一个读操作。它要么从主内存中读取(缓存没命中),要么从L3缓存读取(如果没过期或者被其他核心嗅探到改变)。在任何情况下,一致性协议都能保证,读操作永远不会从缓存子系统返回一份过期拷贝。

并发编程

如果我们的缓存总是保证一致性,那么为什么我们在写并发程序时要担心可见性?这是因为核心为了得到更好的性能,对于其它线程来说,可能会出现数据修改的乱序。这么做主要有两个理由。

首先,我们的编译器在生成程序代码时,为了性能,可能让变量在寄存器中存在很长的时间,例如,变量在一个循环中重复使用。如果我们需要这些变量在核心之间可见,那么变量就不能在寄存器分配。在C语言中,可以添加“volatile”关键字达到这个目标。要记住,c/c++中volatile并不能保证让编译器不重排我们的指令。因此,需要使用内存屏障。

排序的第二个主要问题是,一个线程写了一个变量,然后很快读取,有可能从读缓冲中获得比缓存子系统中最新值要旧的值。这对于遵循单写入者原则(Single Writer Principle)的程序来说没有任何问题,但是对于 Dekker 和Peterson锁算法就是个很大问题。为了克服这一点,并且确保最新值可见,线程不能从本地读缓冲中读取值。可以使用屏障指令,防止下一个读操作在另一线程的写操作之前发生。在Java中对一个volatile变量进行写操作,除了永远不会在寄存器中分配之外,还会伴随一个完全的屏障指令。在x86架构上,屏障指令在读缓冲排空之前,会显著影响放置屏障的线程的运行。在其它处理器上,屏障有更有效率的实现,例如 Azul Vega在读缓冲上放置一个标志用于边界搜索。

当遵循单写入者原则时,要确保Java线程之间的内存次序,避免store屏障,那么就使用j.u.c.Atomic(Int|Long|Reference).lazySet()方法,而非放置一个volatile变量。

误区

回到作为并发算法中的一部分的“刷新缓存”误区上,我想,可以说我们永远不会在用户空间的程序上“刷新”CPU缓存。我相信这个误区的来源是由于在某些并发算法需要刷新、标记或者清空store缓冲以使下一个读操作可以看到最新值。为了达到这点,我们需要内存屏障而非刷新缓存。

这个误解的另一个可能来源是,L1缓存,或者 TLB,在上下文切换的时候可能需要根据地址索引策略进行刷新。ARM,在ARMv6之前,没有在TLB条目上使用地址空间标签,因此在上下文切换的时候需要刷新整个L1缓存。许多处理器因为类似的理由需要L1指令缓存刷新,在许多场景下,仅仅是因为指令缓存没有必要保持一致。上下文切换消耗很大,除了污染L2缓存之外,上下文切换还会导致TLB和/或者L1缓存刷新。Intel x86处理器在上下文切换时仅仅需要TLB刷新。

(校对注:TLB是Translation lookaside buffer,即页表缓冲;里面存放的是一些页表文件,又称为快表技术,由于“页表”存储在主存储器中,查询页表所付出的代价很大,由此产生了TLB。)

(全文完)

 | 48 views | 1 comments | 0 flags

Categories: Uncategorized Tags:

I like Elastos

March 20th, 2013 Comments off

this post is categoried with ‘Elastos’

 | 50 views | 0 comments | 0 flags

Categories: Elastos Tags:

关于webOS中的domain计算的思考

March 19th, 2013 Comments off

随着网页的规模越来越大,基于一个domain的预取、预算变得有必要,就是把一个domain的资源预加载好,等加载这个domain里面的具体计算内容时,就不用再加载那些“基础”资源了。这是我当初做webOS时的一个主张。因为对一个domain的信任(可以通过数字签名之类的手段),这些附属在domain上的计算可以是用Java写的,甚至是c++写的。

 | 61 views | 0 comments | 1 flags

Categories: Uncategorized Tags: