热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ErlangERTS的Trap机制的设计及其用途

出处:http:mryufeng.iteye.comblog334744erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到。它的实现需要erlvm的配

出处:http://mryufeng.iteye.com/blog/334744

erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到。它的实现需要erl vm的配合。它的作用基本上有3个: 

1. 把费时操作分阶段做。由于erlang是个软实时系统,一个进程或者bif不能无限制的占用cpu时间。所以erlang的每个进程执行的时候,最多只能执行一定数量的指令.这个是设计方面的目标。实现上也要配套。所以比如md5,list_member查找这种可能耗时的操作都是用trap机制来实现的,也就是说 当进程调度到的时候 执行一定数量的计算 然后把上下文trap起来 放弃执行 等待下一次的调度 来继续计算。 

2. 延迟执行,实现上层的决策。 明显的例子是 send操作。 send的时候 节点间可能未连接,所以这个send的操作不能继续,先trap, 然后在下一次的调度的时候 执行节点连接操作,一旦成功 send操作就继续往下执行。对客户来讲这个操作是透明的。他不知道你幕后的这些事情。 

3. 主动放弃CPU yield. 

erlang设计还是蛮细致的! 

PS:涉及到费时操作的BIF有: 
do_bif_utf8_to_list 
ets_delete_1 
spawn_3 
monitor_2 
spawn_link_3 
spawn_opt_1 
send_2 
crc32_1 
adler32_1 
md5_1 
send_3 
build_utf8_return 
build_list_return 
finalize_list_to_list 
do_bif_utf8_to_list 
ets_select_reverse 
ets_match_spec_run_r_3 
re_run_3 
re_exec_trap 
keyfind 
monitor_node_3. 

>Can someone tell me how Erlang processes are scheduled? I‘m doing
>some quick research into real-time languages, I know Erlang is only
>a `soft‘ real-time system, but just how soft is it? Does Erlang make
>any attempt at all to meet real-time deadlines other than making
>context switches very fast and efficient?

Erlang processes are currently scheduled on a reduction count basis.
One reduction is roughly equivalent to a function call.
A process is allowed to run until it pauses to wait for input (a
message from some other process) or until it has executed 1000
reductions.

There are functions to slightly optimize the scheduling of a process
(yield(), bump_reductions(N)), but they are only meant for very
restricted use, and may be removed if the scheduler changes.

A process waiting for a message will be re-scheduled as soon as there
is something new in the message queue, or as soon as the receive timer
(receive ... after Time -> ... end) expires. It will then be put last
in the appropriate queue.

Erlang has 4 scheduler queues (priorities):
‘max‘, ‘high‘, ‘normal‘, and ‘low‘.


‘max‘ and ‘high‘ are strict. This means that the scheduler will
first look at the ‘max‘ queue and run processes there until the
queue is empty; then it will do the same for the ‘high‘ queue.

‘normal‘ and ‘low‘ are fair priorities. Assuming no processes at
levels ‘max‘ and ‘high‘, the scheduler will run ‘normal‘ processes
until the queue is empty, or until it has executed a total of 8000
reductions (8*Max_Process_Reds); then it will execute one ‘low‘
priority process, if there is one ready to run.

The relationship between ‘normal‘ and ‘low‘ introduces a risk of
priority inversion. If you have hundreds of (active) ‘normal‘
processes, and only a few ‘low‘, the ‘low‘ processes will actually get
higher priority than the ‘normal‘ processes.

There was an experimental version of a multi-pro Erlang runtime
system. It supported Erlang processes as OS threads, and then used the
scheduling and prority levels of the underlying OS.


One important thing to note about the scheduling is that from the
programmer‘s perspective, the following three things are invariant:

- scheduling is preemptive(抢占的).


- a process is suspended if it enters a receive statement, and there
is no matching message in the message queue.


- if a receive timer expires, the process is rescheduled.

The Erlang Processor, for example, will most likely schedule based on
CPU cycles (essentially a time-based scheduler). It may also have
multiple thread pools, allowing it to context switch within one
clock cycle to another process if the active process has to e.g.
wait for memory.

In a runtime environment that supports it, it should be possible
to also have erlang processes at interrupt priority (meaning that
they will be allowed to run as soon as there is something for them
to do -- not having to wait until a ‘normal‘ priority process finishes
its timeslice.)

技术分享

 Erlang schedulers are based on reduction counting as a method for measuring execution time.

A reduction is roughly equivalent to a function call. Since each function call may take a different amount of time,

the actual periods are not the same between different reductions.

When a process is scheduled to run, it is assigned a number of reductions that it is allowed to execute

(by default 2000 reductions in R13B04).

The process can execute until it consumes all its reduction quantum or pauses to wait for a message.

A process waiting for a message is rescheduled when a new message comes or a timer expires.

Rescheduled or new processes are put to the end of corresponding run queues.

Suspended (blocked) processes are not stored in the run queues

公平调度实际上有3部分:

  1.  erlang函数调用,由于erlang的代码翻译成opcode,由虚拟机执行,所以一次完整的函数调用为一个reduction. 因为erlang的函数通常都是递归执行的,

所以函数体一般很小。

    2.   bif的trap机制。简单的说就是bif会执行到大概几个reduction这样的时间片后放弃执行,把当前的执行情况记录下来,然后退出。等下次调度再执行

的时候,会继续之前的位置往下。

    3.  IO的调度。 IO也是公平调度的,把IO的处理量换算成reduction,算在宿主进程的时间片里面。

Erlang ERTS的Trap机制的设计及其用途


推荐阅读
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
手机用户2502916423
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有