Safe

GC safe-point (or safepoint) and safe-region

Root references
An object is dead really means it is useless. Only the programmer knows if an object is useless or not. In order for the program to decide if an object is useless, we can use compiler analysis, reference counting, or reachability analysis.

Reachability analysis assumes an object is live as long as it is reachable by the mutator. If an object‘s reference is contained by a slot of the mutator‘s stack, it‘s directly reachable. Those objects reachable from reachable objects are also reachable. So the issue for reachability analysis is to find out the references that are directly reachable, which are root references. The set of root references is root set.

The mutator‘s context has the data that are directly reachable, so to get root set is to find object references in the context. The context of a mutator refers to its stack and its register file (and some other thread-specific data). Global data are also directly reachable.

Root set enumeration
Normally, if a GC uses reachability to determine an object‘s liveness, GC needs to get a consistent snapshot of the mutator‘s context, so as to enumerate the root references. This is true for both stop-the-world (STW) and concurrent GC (mostly). "Consistent" means the snapshot looks like taken at a single time point. A consistent snapshot of root references are necessary for correctness, otherwise some live objects might be lost. Then the question is how to get the consistent mutator‘s context snapshot.

To get the consistent snapshot, a simple way is that the mutator suspends its execution during the root references enumeration. The snapshot is also consistent if the root set does not change during the enumeration process.

When a mutator suspends its execution, it is not necessarily able to enumerate the root references in its context, unless it book-keeps the reference information in its context. That is, it should be able to tell which stack slots have references, and which registers hold references. If GC can accurately gets the information, it is called precise root set enumeration; or it‘s imprecise.

(For imprecise enumeration, GC has to use some heuristics to conservatively guess the references from the context. So the GC is called conservative GC. This essay only discusses precise enumeration.)

Harmony supports precise root set enumeration with GC safe-point and safe-region.

Safe-point (or safepoint)
In order to support precise enumeration, JIT compiler should do additional work, because only JIT knows exactly stack frame info and register contents. When JIT compiles a method, for every instruction, it can book-keep the root reference information in case the execution is suspended at that instruction.

But to remember the info for every instruction is too expensive. It requires substantial space to store the information. This is also unnecessary, because only a few instructions will have the chances to be the suspension points in real execution. JIT only needs to book-keep information for those instruction points -- they are called safe-points. Safe-point means it is a safe suspension point for root set enumeration.

Btw, the ability of a compiler to know exact stack slots‘ information is not universally available in all programming languages. Only safe languages have the ability. For example, C/C++ doesn‘t.

Mutator suspension
The question to safe-points is, how we can guarantee that the mutator is suspended at safe-point.

There are basically two kinds of approaches to suspend a mutator, preemptively or voluntarily. The preemptive approach is to suspend the mutator whenever GC needs to start a collection. If it finds the mutator is suspended at an unsafe point, it will resume the mutator, rolling it forward to a safe-point. This was implemented in ORP [1], the predecessor of Harmony. But currently almost no JVM takes this approach.

The approach used in Harmony is voluntary suspension. When GC wants to trigger a collection, it simply sets a flag; the mutators poll the flag periodically, and will suspend once they find the flag is set. Those polling points are safe-points. It‘s mostly JIT‘s responsibility to insert the pollings at proper positions. Sometimes VM also needs to have some polling points.

Polling point
So where are the right places for polling GC trigger event? As I discussed above, we do not want to have polling points for every instruction. For voluntary suspension, a more serious problem is the polling overhead. So the basic principles for polling point insertion are: Firstly, polling points should be frequent enough so that GC does not wait too long for a mutator to suspend, because other mutators might be waiting for GC to free the space in order to continue. Secondly, polling points should not be too frequent to introduce big runtime overhead.

The best result is to have only adequate polling points that are necessary and sufficient. 
1. The mandatory polling points are the allocation sites. Allocation can trigger collection, so allocation site has to be a safe point. 
2. Long-time execution are always associated with method call or loop. So call sites and loop back sites are also expected polling points.

Those are the sites for polling points in Harmony: allocation sites, call sites and loop back sites. Mostly the runtime overhead is smaller than 1%. Unfortunately we found safe-point alone is not sufficient.

Safe-region
Why can safe-point alone be not sufficient? The reason is we forgot one case of long time execution. We forgot it because it‘s actually not long time execution, but long time idle. There are situations when the application can not respond promptly to a GC trigger event, such as sleep, or being blocked in a system call. These operations are out of JVM‘s control. JVM can not respond to GC trigger event in that period. So we introduce safe-region to solve the problem.

Safe-region is the section of code that no references are mutated in it, then it is safe to enumerate roots at any points of that region. In other words, the safe region is a big extended safe-point.

In safe-point design, the mutator polling for GC event will respond if the event is triggered. It responds by setting a ready flag when it‘s sure to suspend. Then the GC can proceed with root set enumeration. This is a hand-shaking protocol.

Safe-region just follows this protocol. The mutator sets the ready flag when it enters a safe-region. Before it leaves the region, it checks if GC has finished its enumeration (or collection), and no longer needs the mutator under suspension state. If it‘s true, it goes ahead and leaves the region; otherwise, it suspends itself as in a safe-point.

In Harmony implementation, we insert suspend_enable and suspend_disable to delimit the scope of safe-region.

时间: 2024-10-01 13:54:14

Safe的相关文章

UVA - 825Walking on the Safe Side(dp)

题目: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵,起点是1 * 1,终点是n * m,这个矩阵上有些点是不可以经过的,要求从起点到终点距离最短,并且不能走那种不能走的点,一共有多少种方式. 解题思路:要求路径最短的话,每个点要不向右走,要不向下走.dp[i][j] = dp[i][j + 1] + dp[i + 1][j]:当这个点不能通过,dp[i][j] = 0:这个坑点在样例输入,不一定是规范的输入,可能两个数字之间很多

Warning: date(): It is not safe to rely on the system's timezone settings.

PHP调试的时候出现了警告: It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了3种方法来解决这个问题. 实际上,从PHP 5.1.0开始当对使用date()等函数时,如果timezone设置不正确,在每一次调用时间函数时,都会产生E_NOTICE 或者 E_WARNING 信息,而又在php中,date.timezone这个选项,默认情况下是关闭的,无论用什么php命令都是格林威治标准时间,但是PHP5.3中如果没有设置部

PHP Warning: Unknown: It is not safe to rely on the system's timezone settings

PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings 解决: 2.找到date.timezone,修改为 date.timezone = RPC,后保存. [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone =  修改为 [D

uva 825 Walking on the Safe Side

卡的一手好输入 输入处理懒得写了,直接复制了,23333 给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这个位置不可走, 问有多少路径可以从(1,1)到(n,m),只能向下或向右. #include <stdio.h> #include <string.h> const int N = 1005; int n, m, dp[N][N], fb[N][N]; int dfs(int x,int y) { if(dp

HDU-2527 Safe Or Unsafe

http://acm.hdu.edu.cn/showproblem.php?pid=2527 建哈夫曼树,哈夫曼编码,求wpl值. Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1511    Accepted Submission(s): 594 Problem Description Javac++

暴力枚举 + 24点 --- hnu : Cracking the Safe

Cracking the Safe Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 46, Accepted users: 12 Problem 12886 : No special judgement Problem description Secret agent Roger is trying to crack a safe containing evil Syr

HNU 12886 Cracking the Safe(暴力枚举)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数,要你判断用 + .- . * ./.四种运算能不能得到一个结果为24的式子,可以用括号. 解释一下测试的第四组样例:应该是6 / (1 - 3 / 4) 暴力枚举三种符号分别是什么,然后枚举这三种符号运算的顺序,然后枚举这四个数字的24种排列方式,时间是4^3 * 6 * 24 然后注意要用double型,

Visual Source Safe(VSS)和Team Foundation Server(TFS)比较区别

Visual Source Safe 是用于软件专业开发人员的源代码管理工具Team Foundation Server是为开发团队提供集成源代码管理.问题跟踪和进程管理的更改管理系统 相同点 1.同时开发产品的多个版本2.在不影响其他版本的情况下对产品的一个已发布版本进行更改3.快速检索一批相关文件,确定做出更改的用户和时间4.比较文件的两个版本以及将更改从一个版本移至其他版本5.都提供了命令行客户端和Visual Studio 2005 集成 不同点 1.结构区别VSSVSS 是仅包含客户端

Windows 7 fails to go into safe mode. Stuck at classpnp.sys

Thailand的同事笔记本进不了OS,画面上只有一个指针形鼠标,连safe mode也进不了,在加载了\windows\system32\drivers\classpnp.sys后停留在同样的画面. 首先是觉得系统文件有关,检查下硬盘是否不良,用HDtune查看: 有行警告. 然后在网上搜索了一圈,发现一个记录了同样问题的Blog文章: http://www.laifuying.com/archives/412 作者的结论也是硬盘问题,同样的,对于搜索到若干办法,对于我也是没有生效. 就是以为