我可以限制Haskell中每个函数/ monad /线程的内存使用情况吗? [英] May I limit memory usage per function/monad/thread in Haskell?

查看:156
本文介绍了我可以限制Haskell中每个函数/ monad /线程的内存使用情况吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个旨在作为服务工作的研究编译器项目。其中一个要求是某些用户在处理其呼叫时可能有限的内存使用情况(例如,来自IP abcd 的呼叫可能使用堆内存最多30mb)。



我用C语言编写的原型实现只是直接使用 malloc 'ing的内存池indeade(这实际上很难正确由于有效的类型)。手动内存管理,但是。

有没有什么办法在Haskell中实现这一点,通过限制函数,monad或轻量级线程上的堆使用? (我接受其他功能语言的建议,这可能会允许我这样做。)

解决方案

在最新版本的GHC ,可以使用 setAllocationCounter enableAllocationLimit GHC.Conc 。当设置了限制并且计数器达到0时,该线程接收到一个异步异常。



计数器测量分配,而不是大小现场设置。例如,这段代码达到了极限,尽管它的实时设置永远不会变得很大:

  { - #LANGUAGE NumDecimals# - } 
模块Main其中

导入Data.Foldable(for_)
导入System.IO
导入GHC.Conc(setAllocationCounter,enableAllocationLimit)

main :: IO()
main =
do setAllocationCounter 2e9
enableAllocationLimit
let writeToHandle h =
for_([1 ..] :: [Integer])
(hPutStrLn h。show)
withFile/ dev / nullWriteMode writeToHandle
return()

分配有点粗糙,但它仍然可以用来检测一些失控计算。

< Simon Marlow撰写的这篇博文详细介绍了


I'm working on a research compiler project intended to work as a service. One of the requirements is that certain users might have a limited memory usage (e.g., "calls from IP a.b.c.d may use up to 30mb of heap memory") while handling its calls.

My prototype implementation, written in C, simply uses a memory pool indead of malloc'ing directly (which is actually pretty hard to get right due to effective types). Manual memory management, though.

Is there any way to achieve this in Haskell, by limiting heap usage on a function, monad, or lightweight thread? (I'd accept suggestions of other functional languages which might allow me to do this.)

解决方案

In the latest versions of GHC, it is possible to set per-thread allocation counters and limits, using setAllocationCounter and enableAllocationLimit from GHC.Conc. When a limit is set and the counter reaches 0, the thread receives an asynchronous exception.

The counters measure allocation, and not the size of the live set. For example, this code hits the limit, despite its live set never becoming very big:

{-# LANGUAGE NumDecimals #-}
module Main where

import Data.Foldable (for_)
import System.IO
import GHC.Conc (setAllocationCounter,enableAllocationLimit)

main :: IO ()
main = 
  do setAllocationCounter 2e9
     enableAllocationLimit
     let writeToHandle h =
            for_ ([1..]::[Integer])
                 (hPutStrLn h . show)
     withFile "/dev/null" WriteMode writeToHandle
     return ()

Allocation is a bit crude as a measure, but it can still be useful to detect some "out of control" computations.

This blog post by Simon Marlow goes into more detail.

这篇关于我可以限制Haskell中每个函数/ monad /线程的内存使用情况吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆