简体   繁体   English

在大型实时系统中使用Haskell:如何(如果?)?

[英]Using Haskell for sizable real-time systems: how (if?)?

I've been curious to understand if it is possible to apply the power of Haskell to embedded realtime world, and in googling have found the Atom package. 我一直想知道是否有可能将Haskell的功能应用于嵌入式实时世界,并且在谷歌搜索中找到了Atom软件包。 I'd assume that in the complex case the code might have all the classical C bugs - crashes, memory corruptions, etc, which would then need to be traced to the original Haskell code that caused them. 我假设在复杂的情况下,代码可能包含所有经典的C错误-崩溃,内存损坏等,然后需要追溯到导致它们的原始Haskell代码。 So, this is the first part of the question: "If you had the experience with Atom, how did you deal with the task of debugging the low-level bugs in compiled C code and fixing them in Haskell original code ?" 因此,这是问题的第一部分:“如果您具有Atom的经验,您如何处理调试已编译的C代码中的低级bug并将其修复为Haskell原始代码的任务?”

I searched for some more examples for Atom, this blog post mentions the resulting C code 22KLOC (and obviously no code:), the included example is a toy. 我在Atom上搜索了更多示例, 该博客文章提到了生成的C代码22KLOC(显然没有代码:),其中包括一个玩具。 This and this references have a bit more practical code, but this is where this ends. 这个这个引用有一些更实际的代码,但这就是到此为止。 And the reason I put "sizable" in the subject is, I'm most interested if you might share your experiences of working with the generated C code in the range of 300KLOC+. 我在主题中添加“相当大”的原因是,如果您可以分享使用300KLOC +范围内生成的C代码的经验,我最感兴趣。

As I am a Haskell newbie, obviously there may be other ways that I did not find due to my unknown unknowns, so any other pointers for self-education in this area would be greatly appreciated - and this is the second part of the question - "what would be some other practical methods (if) of doing real-time development in Haskell?". 由于我是Haskell的新手,很明显,由于未知的未知数,可能还有其他找不到的方法,因此,在此领域进行自我教育的其他任何指示都将不胜感激-这是问题的第二部分- “如果在Haskell中进行实时开发,还有其他一些实用的方法吗?”。 If the multicore is also in the picture, that's an extra plus :-) 如果多核也出现在图片中,那是一个额外的优势:-)

(About usage of Haskell itself for this purpose: from what I read in this blog post , the garbage collection and laziness in Haskell makes it rather nondeterministic scheduling-wise, but maybe in two years something has changed. Real world Haskell programming question on SO was the closest that I could find to this topic) (关于为此目的使用Haskell本身:根据我在此博客文章中所读到的内容 ,Haskell中的垃圾收集和惰性使它在确定性调度方面变得相当不确定,但是也许在两年后发生了一些变化。 真实世界中关于Haskell编程的问题是我可以找到的最接近该主题的位置)

Note: "real-time" above is would be closer to "hard realtime" - I'm curious if it is possible to ensure that the pause time when the main task is not executing is under 0.5ms. 注意:上面的“实时”将更接近“硬实时”-我很好奇是否可以确保不执行主要任务时的暂停时间在0.5毫秒以下。

At Galois we use Haskell for two things: 在Galois,我们使用Haskell做两件事:

  • Soft real time (OS device layers, networking), where 1-5 ms response times are plausible. 软实时(OS设备层,网络),其中1-5毫秒的响应时间是合理的。 GHC generates fast code, and has plenty of support for tuning the garbage collector and scheduler to get the right timings. GHC生成快速代码,并提供大量支持来调整垃圾收集器和调度程序以获取正确的时间。
  • for true real time systems EDSLs are used to generate code for other languages that provide stronger timing guarantees. 对于真正的实时系统,EDSL用于生成其他语言的代码,从而提供更强的时序保证。 Eg Cryptol, Atom and Copilot. 例如,密码子,原子和副驾驶员。

So be careful to distinguish the EDSL (Copilot or Atom) from the host language (Haskell). 因此,请小心区分EDSL(副驾驶或Atom)与主机语言(Haskell)。


Some examples of critical systems, and in some cases, real-time systems, either written or generated from Haskell, produced by Galois. Galois生产的一些关键系统示例,有时还包括由Haskell编写或生成的实时系统。

EDSLs EDSL

Systems 系统篇

  • HaLVM -- a lightweight microkernel for embedded and mobile applications HaLVM-适用于嵌入式和移动应用程序的轻量级微内核
  • TSE -- a cross-domain (security level) network appliance TSE-跨域(安全级别)网络设备

It will be a long time before there is a Haskell system that fits in small memory and can guarantee sub-millisecond pause times. Haskell系统需要很短的时间才能适应较小的内存,并且可以保证亚毫秒级的暂停时间。 The community of Haskell implementors just doesn't seem to be interested in this kind of target. Haskell实现者社区似乎对这种目标不感兴趣。

There is healthy interest in using Haskell or something Haskell-like to compile down to something very efficient; 使用Haskell或类似Haskell的东西将其编译为非常有效的东西是很有益的。 for example, Bluespec compiles to hardware. 例如, Bluespec编译为硬件。

I don't think it will meet your needs, but if you're interested in functional programming and embedded systems you should learn about Erlang . 我认为它不能满足您的需求,但是如果您对功能编程和嵌入式系统感兴趣,则应该了解Erlang

Andrew, 安德鲁,

Yes, it can be tricky to debug problems through the generated code back to the original source. 是的,通过生成的代码将问题调试回原始源可能会很棘手。 One thing Atom provides is a means to probe internal expressions, then leaves if up to the user how to handle these probes. Atom提供的一件事是探查内部表达式的一种方法,然后由用户决定如何处理这些探查。 For vehicle testing, we build a transmitter (in Atom) and stream the probes out over a CAN bus. 对于车辆测试,我们构建了一个发射器(在Atom中),并通过CAN总线将探针流式传输出去。 We can then capture this data, formated it, then view it with tools like GTKWave, either in post-processing or realtime. 然后,我们可以捕获此数据,对其进行格式化,然后使用GTKWave之类的工具进行后期处理或实时查看。 For software simulation, probes are handled differently. 对于软件仿真,探针的处理方式有所不同。 Instead of getting probe data from a CAN protocol, hooks are made to the C code to lift the probe values directly. 与其从CAN协议获取探测数据,不如对C代码进行钩子以直接提升探测值。 The probe values are then used in the unit testing framework (distributed with Atom) to determine if a test passes or fails and to calculate simulation coverage. 然后,将探针值用于单元测试框架(随Atom一起分发)中,以确定测试是通过还是失败,并计算模拟覆盖率。

I don't think Haskell, or other Garbage Collected languages are very well-suited to hard-realtime systems, as GC's tend to amortize their runtimes into short pauses. 我认为Haskell或其他垃圾收集语言不太适合硬实时系统,因为GC倾向于将其运行时间摊销到短暂的暂停中。

Writing in Atom is not exactly programming in Haskell, as Haskell here can be seen as purely a preprocessor for the actual program you are writing. 用Atom编写并不是完全用Haskell进行编程,因为在这里Haskell可以看作纯粹是您正在编写的实际程序的预处理器。

I think Haskell is an awesome preprocessor, and using DSEL's like Atom is probably a great way to create sizable hard-realtime systems, but I don't know if Atom fits the bill or not. 我认为Haskell是一个很棒的预处理器,使用像Atom这样的DSEL可能是创建相当大的硬实时系统的好方法,但是我不知道Atom是否符合要求。 If it doesn't, I'm pretty sure it is possible (and I encourage anyone who does!) to implement a DSEL that does. 如果没有,我很确定有可能(并且我鼓励这样做的人!)实现可以做到的DSEL。

Having a very strong pre-processor like Haskell for a low-level language opens up a huge window of opportunity to implement abstractions through code-generation that are much more clumsy when implemented as C code text generators. 拥有像Haskell这样的非常强大的预处理器来使用低级语言,为通过代码生成实现抽象提供了巨大的机会,而这些代码生成在实现为C代码文本生成器时显得更加笨拙。

I've been fooling around with Atom. 我一直在鬼混Atom。 It is pretty cool, but I think it is best for small systems. 这很酷,但是我认为这对于小型系统是最好的。 Yes it runs in trucks and buses and implements real-world, critical applications, but that doesn't mean those applications are necessarily large or complex. 是的,它可以在卡车和公共汽车上运行并实现实际的关键应用程序,但这并不意味着这些应用程序必然很大或很复杂。 It really is for hard-real-time apps and goes to great lengths to make every operation take the exact same amount of time. 它确实适用于硬实时应用程序,并竭尽全力使每个操作花费完全相同的时间。 For example, instead of an if/else statement that conditionally executes one of two code branches that might differ in running time, it has a "mux" statement that always executes both branches before conditionally selecting one of the two computed values (so the total execution time is the same whichever value is selected). 例如,它不是使用if / else语句有条件地执行运行时间可能不同的两个代码分支之一,而是提供了一个“ mux”语句,该语句始终在有条件地选择两个计算值之一之前执行两个分支(因此,总和执行时间是相同的,无论选择哪个值)。 It doesn't have any significant type system other than built-in types (comparable to C's) that are enforced through GADT values passed through the Atom monad. 除了通过Atom monad传递的GADT值强制执行的内置类型(与C相比)外,它没有任何重要的类型系统。 The author is working on a static verification tool that analyzes the output C code, which is pretty cool (it uses an SMT solver), but I think Atom would benefit from more source-level features and checks. 作者正在研究一种静态验证工具,该工具可以分析输出的C代码,这很酷(它使用SMT求解器),但是我认为Atom将从更多的源代码级功能和检查中受益。 Even in my toy-sized app (LED flashlight controller), I've made a number of newbie errors that someone more experienced with the package might avoid, but that resulted in buggy output code that I'd rather have been caught by the compiler instead of through testing. 即使在我的玩具大小的应用程序(LED手电筒控制器)中,我也犯了许多新手错误,有些对此软件包有经验的人可能会避免,但是这导致了错误的输出代码,我宁愿被编译器捕获而不是通过测试。 On the other hand, it's still at version 0.1.something so improvements are undoubtedly coming. 另一方面,它仍然是0.1版本,因此无疑会有改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM