如何在 Haskell 中优化软实时应用程序的垃圾回收?
How do I optimise garbage collection for a soft real-time application in Haskell?
我在 Haskell 中编写了一个软实时应用程序,它处理模拟物理、碰撞检测等所有好东西。在做所有这些时,我分配了大量内存,如果我愿意的话,我可能会优化我的内存使用,但是因为我很好地坐在 40% CPU 并且无论如何只使用了 1% RAM,所以那不是'似乎没有必要。不过,我看到的是,很多时候,当垃圾收集器启动时,帧会被跳过。我已经通过使用 threadscope
进行分析验证了这是问题的原因:在垃圾收集器执行其业务时,有时长达 0.05 秒的时间内没有发生有用的计算,导致多达 3 个跳过的帧,这非常引人注目且非常烦人。
现在,我尝试通过每帧手动调用 performMinorGC
来解决这个问题,这似乎缓解了这个问题,使它变得更加流畅,除了整体 CPU 使用率急剧上升这一事实到 70% 左右。显然我宁愿避免这种情况。
我尝试的另一件事是使用 -H64k 将 GC 的分配 space 从 512k 减少到 64k,我还尝试设置 -I0.03 以尝试让它更频繁地收集。这两个选项都改变了我在 threadscope
中看到的垃圾回收模式,但它们仍然导致跳帧。
任何有 GC 优化经验的人都可以帮助我吗?我注定要手动调用 performMinorGC
并忍受由此造成的巨大性能损失吗?
编辑
我在这些测试中尝试 运行 它的时间相似,但由于它是实时的,因此没有必要 'done'。
每 4 帧 performMinorGC
的运行时统计信息:
9,776,109,768 bytes allocated in the heap
349,349,800 bytes copied during GC
53,547,152 bytes maximum residency (14 sample(s))
12,123,104 bytes maximum slop
105 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 15536 colls, 15536 par 3.033s 0.997s 0.0001s 0.0192s
Gen 1 14 colls, 13 par 0.207s 0.128s 0.0092s 0.0305s
Parallel GC work balance: 6.15% (serial 0%, perfect 100%)
TASKS: 20 (2 bound, 13 peak workers (18 total), using -N4)
SPARKS: 74772 (20785 converted, 0 overflowed, 0 dud, 38422 GC'd, 15565 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 9.773s ( 7.368s elapsed)
GC time 3.240s ( 1.126s elapsed)
EXIT time 0.003s ( 0.004s elapsed)
Total time 13.040s ( 8.499s elapsed)
Alloc rate 1,000,283,400 bytes per MUT second
Productivity 75.2% of total user, 115.3% of total elapsed
gc_alloc_block_sync: 29843
whitehole_spin: 0
gen[0].sync: 11
gen[1].sync: 71
没有performMinorGC
12,316,488,144 bytes allocated in the heap
447,495,936 bytes copied during GC
63,556,272 bytes maximum residency (15 sample(s))
15,418,296 bytes maximum slop
146 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 19292 colls, 19292 par 2.613s 0.950s 0.0000s 0.0161s
Gen 1 15 colls, 14 par 0.237s 0.165s 0.0110s 0.0499s
Parallel GC work balance: 2.67% (serial 0%, perfect 100%)
TASKS: 17 (2 bound, 13 peak workers (15 total), using -N4)
SPARKS: 100714 (29688 converted, 0 overflowed, 0 dud, 47577 GC'd, 23449 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 13.377s ( 9.917s elapsed)
GC time 2.850s ( 1.115s elapsed)
EXIT time 0.000s ( 0.006s elapsed)
Total time 16.247s ( 11.039s elapsed)
Alloc rate 920,744,995 bytes per MUT second
Productivity 82.5% of total user, 121.4% of total elapsed
gc_alloc_block_sync: 68533
whitehole_spin: 0
gen[0].sync: 9
gen[1].sync: 147
由于某种原因,现在的整体生产率似乎没有 performMinorGC
比我昨天测试时低 -- 之前总是 >90%。
你有很大的老一代。好像有 100Mb 大。
默认情况下,GHC
在堆大小达到上次主要 GC 后其大小的 2 倍时执行主要 GC。这意味着在某些时候 GC 必须扫描并复制 50Mb 的数据。如果您的处理器有 10Gb 的内存吞吐量限制,那么加载和复制 50Mb 将至少需要 0.01 秒(与 gen1 平均和最大暂停相比。)
(我假设您检查了事件日志以确保主要 GC 在 0.05 秒暂停期间实际工作。因此当 GC 等待其他线程而不是进行实际工作时,这不是线程同步的问题。)
因此,为了最大限度地减少 GC 暂停,您应该确保年老代很小。如果这 50Mb 中的大部分是静态数据,从一开始就分配到最后(例如纹理或网格),那么你就是 stuck。我知道的唯一解决方法是将数据打包到例如可存储的矢量并在需要时再次解压它的一部分。
如果数据是在执行期间分配的并且存在有限的时间(但足以在几个主要世代中存活下来),那么请尝试重新考虑您的管道。通常没有数据应该在一帧中存活下来,所以你做错了什么。例如。您保留了不该保留的数据。
其他不好的迹象 -- gen0 最大暂停 0.02 秒。这很奇怪。默认情况下 gen0 分配区域是 0.5Mb,所以 gen0 GC 应该很快。可能你有大 remembered set。可能的原因:可变结构(IORef、可变 Vector 等)或大量惰性 thunk 更新。
还有一个小问题(可能不相关):看起来您正在使用隐式并行,但只有 1/3 的火花被转换。您分配的 spart 太多,其中 1/2 已被 GC。
我在 Haskell 中编写了一个软实时应用程序,它处理模拟物理、碰撞检测等所有好东西。在做所有这些时,我分配了大量内存,如果我愿意的话,我可能会优化我的内存使用,但是因为我很好地坐在 40% CPU 并且无论如何只使用了 1% RAM,所以那不是'似乎没有必要。不过,我看到的是,很多时候,当垃圾收集器启动时,帧会被跳过。我已经通过使用 threadscope
进行分析验证了这是问题的原因:在垃圾收集器执行其业务时,有时长达 0.05 秒的时间内没有发生有用的计算,导致多达 3 个跳过的帧,这非常引人注目且非常烦人。
现在,我尝试通过每帧手动调用 performMinorGC
来解决这个问题,这似乎缓解了这个问题,使它变得更加流畅,除了整体 CPU 使用率急剧上升这一事实到 70% 左右。显然我宁愿避免这种情况。
我尝试的另一件事是使用 -H64k 将 GC 的分配 space 从 512k 减少到 64k,我还尝试设置 -I0.03 以尝试让它更频繁地收集。这两个选项都改变了我在 threadscope
中看到的垃圾回收模式,但它们仍然导致跳帧。
任何有 GC 优化经验的人都可以帮助我吗?我注定要手动调用 performMinorGC
并忍受由此造成的巨大性能损失吗?
编辑
我在这些测试中尝试 运行 它的时间相似,但由于它是实时的,因此没有必要 'done'。
每 4 帧 performMinorGC
的运行时统计信息:
9,776,109,768 bytes allocated in the heap
349,349,800 bytes copied during GC
53,547,152 bytes maximum residency (14 sample(s))
12,123,104 bytes maximum slop
105 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 15536 colls, 15536 par 3.033s 0.997s 0.0001s 0.0192s
Gen 1 14 colls, 13 par 0.207s 0.128s 0.0092s 0.0305s
Parallel GC work balance: 6.15% (serial 0%, perfect 100%)
TASKS: 20 (2 bound, 13 peak workers (18 total), using -N4)
SPARKS: 74772 (20785 converted, 0 overflowed, 0 dud, 38422 GC'd, 15565 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 9.773s ( 7.368s elapsed)
GC time 3.240s ( 1.126s elapsed)
EXIT time 0.003s ( 0.004s elapsed)
Total time 13.040s ( 8.499s elapsed)
Alloc rate 1,000,283,400 bytes per MUT second
Productivity 75.2% of total user, 115.3% of total elapsed
gc_alloc_block_sync: 29843
whitehole_spin: 0
gen[0].sync: 11
gen[1].sync: 71
没有performMinorGC
12,316,488,144 bytes allocated in the heap
447,495,936 bytes copied during GC
63,556,272 bytes maximum residency (15 sample(s))
15,418,296 bytes maximum slop
146 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 19292 colls, 19292 par 2.613s 0.950s 0.0000s 0.0161s
Gen 1 15 colls, 14 par 0.237s 0.165s 0.0110s 0.0499s
Parallel GC work balance: 2.67% (serial 0%, perfect 100%)
TASKS: 17 (2 bound, 13 peak workers (15 total), using -N4)
SPARKS: 100714 (29688 converted, 0 overflowed, 0 dud, 47577 GC'd, 23449 fizzled)
INIT time 0.000s ( 0.001s elapsed)
MUT time 13.377s ( 9.917s elapsed)
GC time 2.850s ( 1.115s elapsed)
EXIT time 0.000s ( 0.006s elapsed)
Total time 16.247s ( 11.039s elapsed)
Alloc rate 920,744,995 bytes per MUT second
Productivity 82.5% of total user, 121.4% of total elapsed
gc_alloc_block_sync: 68533
whitehole_spin: 0
gen[0].sync: 9
gen[1].sync: 147
由于某种原因,现在的整体生产率似乎没有 performMinorGC
比我昨天测试时低 -- 之前总是 >90%。
你有很大的老一代。好像有 100Mb 大。
默认情况下,GHC
在堆大小达到上次主要 GC 后其大小的 2 倍时执行主要 GC。这意味着在某些时候 GC 必须扫描并复制 50Mb 的数据。如果您的处理器有 10Gb 的内存吞吐量限制,那么加载和复制 50Mb 将至少需要 0.01 秒(与 gen1 平均和最大暂停相比。)
(我假设您检查了事件日志以确保主要 GC 在 0.05 秒暂停期间实际工作。因此当 GC 等待其他线程而不是进行实际工作时,这不是线程同步的问题。)
因此,为了最大限度地减少 GC 暂停,您应该确保年老代很小。如果这 50Mb 中的大部分是静态数据,从一开始就分配到最后(例如纹理或网格),那么你就是 stuck。我知道的唯一解决方法是将数据打包到例如可存储的矢量并在需要时再次解压它的一部分。
如果数据是在执行期间分配的并且存在有限的时间(但足以在几个主要世代中存活下来),那么请尝试重新考虑您的管道。通常没有数据应该在一帧中存活下来,所以你做错了什么。例如。您保留了不该保留的数据。
其他不好的迹象 -- gen0 最大暂停 0.02 秒。这很奇怪。默认情况下 gen0 分配区域是 0.5Mb,所以 gen0 GC 应该很快。可能你有大 remembered set。可能的原因:可变结构(IORef、可变 Vector 等)或大量惰性 thunk 更新。
还有一个小问题(可能不相关):看起来您正在使用隐式并行,但只有 1/3 的火花被转换。您分配的 spart 太多,其中 1/2 已被 GC。