您的位置:首页 > 编程语言 > Go语言

google v8引擎常见问题

2013-08-20 15:35 495 查看
最近在项目中使用v8来进行扩展,下面简单说一下使用v8过程中遇到的一些问题。

v8的多线程调用
最初调研v8的测试代码是单线程的,后来一个项目在多线程中使用,出现了一些问题,后来看到参考3中的才恍然大悟,又翻了v8.h中关于Isolate的注释。

/**

* Isolate represents an isolated instance of the V8 engine. V8

* isolates have completely separate states. Objects from one isolate

* must not be used in other isolates. When V8 is initialized a

* default isolate is implicitly created and entered. The embedder

* can create additional isolates and use them in parallel in multiple

* threads. An isolate can be entered by at most one thread at any

* given time. The Locker/Unlocker API can be used to synchronize.

*/

多线程调用的时候,如果需要每个线程运行一个单独的v8 vm的话,就需要在线程初始化的时候运行Isolate::New(),线程结束前调用Isolate::Dispose()。

v8如果不显示创建Isolate,会自动创建一个默认的Isolate。

关于Locker
因为每个v8 vm就会有自己的heap,如果每个线程都运行一个v8 vm的话,内存占用就很恐怖,可以多个线程使用同一个Isolate,多个线程在使用v8的时候就需要使用Locker进行同步。
Locker(Isolate *)在某个Isolate中进行加锁;默认Locker不加参数进行构造就会锁住默认的Isolate,实现全局的互斥。
Locker保证一个Isolate同时只能够被一个线程使用。具体可以参考test/cctest/test-locker.cc中的代码。

Locker的位置
Isolate的一些方法需要进行加锁:
void Enter();
void Exit();
void Dispose();
void SetData(void* data);
void* GetData();

尤其是与Enter()相关的位置Isolate::Scope()内会自行调用Isolate::Enter(),所以Isolate::Scope在和Locker一起使用的时候,要注意Locker要在Isolate::Scope前否则运行会出错。

v8的GC管理
v8的gc对用户是透明的,在其认为需要进行的时候才会进行gc,一般是其托管的内存满足一定阈值的时候才会触发gc。但有时候这些是不够的,例如一些嵌入到v8中的Object,本身需要很多的外部资源,这个时候v8并不知道这些外部资源的消耗,长久运行下去就会导致内存紧张,这个典型的例子就是网页中图片的处理和buffer对象的处理。因此需要暗示v8进行主动的GC【GC耗时可能会比较长,内存资源充裕情况下不建议频繁GC】。

1. 外部分配资源管理
v8会有一个整体内存占用的上限阈值,通过AdjustAmountOfExternalAllocatedMemory()调整注册的外部内存数量,反映当前引擎的内存分配情况,当达到阈值的时候,v8引擎会强制调用GC进行资源清理。【可以伪造一个较大的数值来迫使v8进行GC】

/**

* Adjusts the amount of registered external memory. Used to give

* V8 an indication of the amount of externally allocated memory

* that is kept alive by JavaScript objects. V8 uses this to decide

* when to perform global garbage collections. Registering

* externally allocated memory will trigger global garbage

* collections more often than otherwise in an attempt to garbage

* collect the JavaScript objects keeping the externally allocated

* memory alive.

*

* \param change_in_bytes the change in externally allocated memory

* that is kept alive by JavaScript objects.

* \returns the adjusted value.

*/

static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);

2. 暗示v8调用GC
IdleNotification()当嵌入器空闲的时候进行资源清理;LowMemoryNotification()当内存过低时进行资源清理。

/**

* Optional notification that the embedder is idle.

* V8 uses the notification to reduce memory footprint.

* This call can be used repeatedly if the embedder remains idle.

* Returns true if the embedder should stop calling IdleNotification

* until real work has been done. This indicates that V8 has done

* as much cleanup as it will be able to do.

*/

static bool IdleNotification();

/**

* Optional notification that the system is running low on memory.

* V8 uses these notifications to attempt to free memory.

*/

static void LowMemoryNotification();

3. 启动时设置内存相关参数
SetFlagsFromCommandLine()和SetFlagsFromString(),这两个函数设置v8的几个flag:
FLAG_max_new_space_size
FLAG_max_old_space_size
FLAG_max_executable_size
这几个FLAG影响v8的Heap::Setup。

SetFlagsFromCommandLine()/SetFlagsFromString()应该在任何v8的API调用前调用。

4. SetResourceConstraints设置参数
SetResourceContraints()可以在v8 vm初始化前调用设置Isolate相关Heap的大小,一旦vm初始化后,就无法再进行调整了。

{

ResourceConstraints rc;

rc.set_max_young_space_size(2048); //KB

rc.set_max_old_space_size(10); //MB

rc.set_max_executable_size(10); //MB

rc.set_stack_limit(reinterpret_cast<uint32_t*>((char*)&rc- 1024 * 400));

SetResourceConstraints(&rc);

}

具体Heap设置的这些参数如何影响Heap以及GC,暂时没有细细研究,后面有时间再分析。Heap初始化调用关系图:

Context::New

Bootstrapper::CreateEnvironment

Genesis::Genesis

V8::Initialize

V8::Initialize

V8::InitializeHelper

V8::Initialize(Deserializer *des)

Isolate::Init

Heap::Setup

Heap::ConfigureHeapDefault

Heap::ConfigureHeap

FLAG_max_new_space_size

FLAG_max_old_space_size

FLAG_max_executable_size

google v8的示例很少,可以不急于从网上找答案,先看一下v8.h以及test/cctest下面的代码,学习一下开发者是如何使用v8的,就可以解决大部分v8的使用问题。
常见的v8使用,可以直接参考v8cgi和nodejs,相比之下v8cgi的代码要更清晰些;-)

参考

1. http://www.codeproject.com/KB/library/Using_V8_Javascript_VM.aspx
2. http://www.cppblog.com/pansunyou/archive/2010/11/23/google_v8.html

3. http://lajabs.net/2011/08/12/v8%E5%BC%95%E6%93%8E%E4%B8%AD%E7%9A%84%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%BA%94%E7%94%A8/
4. http://www.jiangmiao.org/blog/2247.html

转自:http://blog.chinaunix.net/uid-20357359-id-2688658.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: