【原創】xenomai中4種實時內存分配器介紹及測試對比
xenomai中4種實時內存分配器介紹及測試對比
實時動態內存分配器是實時系統中保障任務高效運行的核心組件之一。在實時應用場景中,內存分配的確定性、時效性與可靠性直接影響系統的實時性能 —— 尤其是在工業自動化、機器人控制、航空航天等對響應時間有嚴苛要求的領域,傳統通用內存分配器(如 glibc 的 malloc)因存在不可預測的延遲和碎片問題,往往難以滿足實時性需求。
Xenomai 作為一款成熟的實時操作系統框架,針對實時環境的特殊需求,提供了一系列經過工業級場景長期驗證的動態內存分配器。這些分配器在設計上以 “確定性延遲” 為核心目標,確保內存操作的響應時間可控,同時兼顧低碎片率和高吞吐量。
它們均經過工業自動化領域的嚴苛考驗,具備穩定、高效、可預測的特點。對于開發者而言,這些分配器不僅可供xenomai實時應用直接調用,也可移植直接集成到實時應用中以快速滿足需求。
本文先簡單介紹 Xenomai 中幾種實時動態內存分配器,隨后給出在不同條件下的他們的分配釋放時間、開銷及碎片化率。
一、xenomai用戶態、內核態內存分配器介紹
老生常談,引用之前文章的一段話:
通常,操作系統的內存管理,內存分配算法一定要快,否則會影響應用程序的運行效率,其次是內存利用率要高。
但對于硬實時操作系統,首先要保證實時性,即確定性,不同內存大小的分配釋放時間必須是時間確定的。
無論linux還是xenomai,內核在服務或管理應用程序過程中經常需要內存分配,通常linux內存的分配與釋放都是時間不確定的,例如,惰性分配導致的缺頁異常、頁面換出和OOM會導致大且不可預測的延遲,不適用于受嚴格時間限制的實時應用程序。
xenomai作為硬實時內核,不能使用linux glibc這樣的內存分配釋放接口,為此xenomai采取的措施是
-
在內核態,在xenomai內核初始化時,先調用
__vmalloc()從linux管理的ZONE_NORMAL中分配 一片內存,然后由xenomai自己來管理這片內存,且xenomai提供的內存分配釋放時間確定的,這樣就不會因為內存的分配釋放影響實時性(該內存管理代碼量非常少,有效代碼數3百行左右,實現精巧,值得研究利用)。本文中也將該內存分配器加入了對比,用coreheap表示。關于內核態xnheap分配器本博客有專門寫過文章解析,詳見http://www.rzrgm.cn/wsg1100/p/13258388.html
-
在用戶態,glibc的內存管理不具有時間確定性,RT應用只能在程序初始化時分配并訪問(避免運行中產生pagefault),運行中不能使用,否則會嚴重影響實時性。為此xenomai實時應用庫libcobalt為RT應用實現了多個時間確定的內存動態分配釋放heap,供實時任務運行中分配內存。它們分別是xnheap、tlsf、heapmem。
綜上,xenomai系統中用戶態及內核態共有4種實時動態內存分配器:
-
coreheap
xenomai內核實時動態內存分配器,為內核態實時任務管理RTDM等提供實時動態內存分配。

-
xnheap
xnheap源自 Xenomai 原始的雙內核中的 xnheap 。它足夠簡單且高效,用于管理通過 tmpfs 文件支持的動態內存分配,并且可以在用戶空間的多個進程之間共享,實現源碼為heapobj-pshared.c,通過xenoomai庫編譯配置選項
--enable-pshared=enable啟用多個進程之間共享對象時使用。注:內核中的xnheap在xenomai 3.1版本有所優化,使用紅黑樹代替了鏈表,所以xenomai 3.1版本及以上的用戶態xnheap與內核態xnheap性能有所區別。
-
tlsf(Two-Level Segregate Fit)
TLSF 是一種通用動態內存分配器,專門設計用于滿足實時要求:
-
有限響應時間 。確定的內存分配和釋放的最壞情況執行時間 (WCET) 必須提前知道,并且與應用程序數據無關。TLSF 具有恒定成本 O(1)。
-
快。 除了有限成本之外,分配器還必須足夠高效和快速。
-
高效內存利用 .傳統上,實時系統運行時間較長,有些(嵌入式應用程序)對內存大小有很強的限制。碎片化會對此類系統產生重大影響。它會急劇增加,并降低系統性能。衡量此效率的一種方法是分配器產生的內存碎片。TLSF 已在數百種不同的負載(實時任務、通用應用程序等)中進行了測試,平均碎片率低于 15%。 測得的最大碎裂率低于 25%。
-
TLSF 已包含在多個 Linux 發行版和應用程序中。盡管 TLSF 在許多場景中運行良好,但它在硬/軟實時應用程序的應用程序中脫穎而出,由于數據大小的高度可變性或對新情況的適應性,該應用程序使用顯式內存分配,具有很高的靈活性要求。TLSF在以下領域表現突出:
- 硬/軟實時系統:需嚴格滿足時間約束的場景(如工業控制、自動駕駛)。
- 動態內存需求高的場景:適應數據規模多變或需動態調整內存的靈活架構(如多媒體處理、3D重建)。
- 資源受限環境:嵌入式設備、網絡設備(路由器/交換機)、游戲引擎等內存敏感場景。
- 高可靠性應用:對內存碎片敏感的長周期運行系統(如通信基站、醫療設備)。
-
源碼位于tlsf\tlsf.c,tlsf版本為2.4.6版本。

-
heapmem
heapmem為《Marshall K. McKusick與Michael J. Karels在"4.3BSD Unix內核通用內存分配器設計》(USENIX 1988會議)中描述的分配器變體實現,詳見http://docs.FreeBSD.org/44doc/papers/kernmalloc.pdf。其核心機制為:空閑頁面列表通過 AVL 樹進行維護,以實現對多頁面內存范圍的快速查找;而存儲分桶內存的頁面則使用快速分配位圖來管理其內部塊。
heapmem和tlsf在xenomai庫中用于alchemy、VxWorks、psos等skin接口中task、sem等對象管理的分配釋放,編譯xenomai庫時通過configure配置參數選擇--with-localmem=<heapmem | tlsf>,若沒有指定,默認使用xnheap。
二、測試流程
按裝xenomai后,運行如下命令即可得出結果:
./smokey --run=8-10,31 --verbose=2
- 初始化:設置線程為高優先級實時任務,避免調度干擾測試精度,以固定塊大小和隨機大小兩種方式進行下面2、3測試。
- 內存分配測試
- 核心邏輯:持續分配固定大小的內存塊,記錄每次分配的耗時,直到無法分配為止。
- 模式檢查(MEMCHECK_PATTERN):向內存塊寫入特定模式數據,用于后續驗證內存是否被意外修改
- 內存釋放測試
- 隨機釋放(MEMCHECK_SHUFFLE):若開啟隨機釋放(+shuffle),則打亂釋放順序,模擬真實場景中內存塊無序釋放的情況,更貼近實際使用,記錄每次釋放的耗時。
- 碎片率計算:當釋放一半內存時,記錄最大連續空閑塊大小,評估堆管理器的抗碎片能力。
- 熱路徑測試(MEMCHECK_HOT)
測試堆管理器在多次分配 - 釋放循環后的穩定性,驗證是否存在累計錯誤(如內存泄露、性能退化)。
- 統計分配釋放最大、平均耗時,分配開銷,碎片化等數據。
三、測試結果
以下數據基于rockchip rk3562, linux 5.10.109 + xenomai 3.2.4測試,僅供參考。
| Heap | Test Type | Performance Metrics | |||||||
|---|---|---|---|---|---|---|---|---|---|
| Worst Alloc (μs) | Worst Free (μs) | Avg Max Alloc (μs) | Avg Max Free (μs) | Avg Alloc (μs) | Avg Free (μs) | Avg Overhead (%) | Avg Fragmentation (%) | ||
| coreheap | Fixed Blocks | 3.2 | 2.6 | 1.0 | 1.1 | 0.6 | 0.6 | 0.0 | 36.1 |
| Notable Cases: Max alloc: 3.2μs (256k@64), Max free: 2.6μs (512k@16) | |||||||||
| heapmem | Fixed Blocks | 8.8 | 6.1 | 0.9 | 1.3 | 0.4 | 0.5 | 0.0 | 40.1 |
| Notable Cases: Max alloc/free: 8.8μs/6.1μs (8k@16) | |||||||||
| tlsf | Fixed Blocks | 19.8 | 4.1 | 1.0 | 0.8 | 0.4 | 0.4 | 14.9 | 38.5 |
| Notable Cases: Max alloc: 19.8μs (8k@16) | |||||||||
| pshared | Fixed Blocks | 3.5 | 38.5 | 1.0 | 1.8 | 0.4 | 0.6 | 0.0 | 39.4 |
| Notable Cases: Max free: 38.5μs (1024k@16) | |||||||||
| coreheap | Random Blocks | 2.6 | 2.6 | 0.7 | 0.9 | 0.8 | 0.7 | 13.8 | 11.1 |
| heapmem | Random Blocks | 3.8 | 3.2 | 0.7 | 1.1 | 0.6 | 0.8 | 13.9 | 11.9 |
| tlsf | Random Blocks | 2.6 | 2.6 | 0.8 | 0.6 | 0.7 | 0.5 | 12.7 | 13.7 |
| pshared | Random Blocks | 2.3 | 3.5 | 0.8 | 1.2 | 0.7 | 0.9 | 13.9 | 11.9 |
無論是固定塊大小分配還是隨機大小分配,這幾個實時內存分配器都很優秀。
四、詳細輸出數據
測試輸出說明:
-
HEAPSZ 測試堆大小
-
BLOCKSZ 測試塊大小
-
NRBLKS 堆中可分配的塊數量
-
AVG-A 分配塊的平均時間(微秒)
-
AVG-F 釋放塊的平均時間(微秒)
-
MAX-A 分配塊的最大時間(微秒)
-
MAX-F 釋放塊的最大時間(微秒)
-
OVRH% 開銷,堆實際占用大小(arena_size)與可用大小(heap_size)的差值,反映管理開銷。
-
FRAG% 外部碎片 ,(1.0 - largest_free / maximum_free) * 100.0
-
FLAGS
+shuffle: 隨機釋放+hot: 在初始分配/釋放后測量(熱堆)
coreheap(kernel)
=== running memory_heapmem ===
seq_heap_size=2048k
random_alloc_rounds=1024
pattern_heap_size=128k
pattern_check_rounds=128
[SEQUENTIAL ALLOC->FREE, ^2 BLOCK SIZES] ON 'coreheap'
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
256k 64 4096 0.5 0.5 3.2 2.0 0.0 99.2 +shuffle +hot
1024k 16 65536 0.5 0.5 2.3 2.0 0.0 100.0 +shuffle +hot
1024k 2k 512 0.6 1.1 2.3 1.8 0.0 94.1 +shuffle
1024k 256 4096 0.6 0.9 2.3 2.3 0.0 98.9 +shuffle
... (364 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
512k 16 32768 0.5 0.5 2.3 2.6 0.0 100.0 +shuffle
1024k 256 4096 0.6 0.9 2.3 2.3 0.0 98.9 +shuffle
1024k 32 32768 0.5 0.6 2.3 2.3 0.0 99.9 +shuffle
512k 16 32768 0.5 0.5 2.3 2.3 0.0 100.0 +shuffle +hot
... (364 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
512k 16 32768 0.5 0.5 2.3 2.6 0.0 100.0 +shuffle
1024k 256 4096 0.6 0.9 2.3 2.3 0.0 98.9 +shuffle
1024k 32 32768 0.5 0.6 2.3 2.3 0.0 99.9 +shuffle
512k 16 32768 0.5 0.5 2.3 2.3 0.0 100.0 +shuffle +hot
... (364 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.5 0.5 2.0 2.3 0.0 100.0 +shuffle
1024k 16 65536 0.5 0.5 2.3 2.0 0.0 100.0 +shuffle +hot
1024k 32 32768 0.5 0.6 2.3 2.3 0.0 99.9 +shuffle
1024k 32 32768 0.5 0.5 2.3 2.3 0.0 99.9 +shuffle +hot
... (364 results following) ...
overall:
worst alloc time: 3.2 (us)
worst free time: 2.6 (us)
average of max. alloc times: 1.0 (us)
average of max. free times: 1.1 (us)
average alloc time: 0.6 (us)
average free time: 0.6 (us)
average overhead: 0.0%
average fragmentation: 36.1%
[SEQUENTIAL ALLOC->FREE, RANDOM BLOCK SIZES] ON 'coreheap'
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
64k 113 512 0.5 0.6 2.6 1.8 11.7 96.5 +shuffle
32k 159 128 0.6 0.6 2.6 1.2 37.9 0.0
512k 127k 4 1.2 1.3 2.3 2.0 0.2 0.0 +shuffle
512k 228k 2 1.8 0.9 2.3 0.9 10.7 0.0 +shuffle
... (32764 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 7 65536 0.5 0.5 1.5 2.6 56.2 100.0 +shuffle
512k 82k 6 0.6 1.2 0.6 2.6 2.9 33.3 +shuffle
512k 45 8192 0.5 0.5 2.0 2.3 29.7 0.0
1024k 479k 2 1.0 2.2 0.9 2.3 6.4 0.0 +shuffle
... (32764 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 7 65536 0.5 0.5 1.5 2.6 56.2 100.0 +shuffle
1024k 268 2048 0.5 0.6 2.0 0.9 47.7 95.4 +shuffle +hot
1024k 280 2048 0.6 0.6 2.0 0.9 45.3 0.0
1024k 666 1024 0.6 1.1 1.2 1.8 35.0 93.8 +shuffle
... (32764 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 2k 409 0.5 0.9 1.2 1.5 0.5 96.1 +shuffle
1024k 2k 409 0.5 0.6 0.9 1.2 2.3 96.6 +shuffle +hot
1024k 5k 186 0.5 0.6 0.6 2.0 1.7 94.6 +shuffle +hot
1024k 4k 227 0.5 0.6 1.2 2.0 2.5 94.7 +shuffle +hot
... (32764 results following) ...
overall:
worst alloc time: 2.6 (us)
worst free time: 2.6 (us)
average of max. alloc times: 0.7 (us)
average of max. free times: 0.9 (us)
average alloc time: 0.8 (us)
average free time: 0.7 (us)
average overhead: 13.8%
average fragmentation: 11.1%
heapmem
=== running memory_heapmem ===
seq_heap_size=2048k
random_alloc_rounds=1024
pattern_heap_size=128k
pattern_check_rounds=128
[SEQUENTIAL ALLOC->FREE, ^2 BLOCK SIZES] ON 'heapmem'
HEAPSZ test heap size
BLOCKSZ tested block size
NRBLKS number of blocks allocatable in heap
AVG-A average time to allocate block (us)
AVG-F average time to free block (us)
MAX-A max time to allocate block (us)
MAX-F max time to free block (us)
OVRH% overhead
FRAG% external fragmentation
FLAGS +shuffle: randomized free
+hot: measure after initial alloc/free pass (hot heap)
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
8k 16 512 0.3 0.3 8.8 6.1 0.0 0.0
1024k 32 32768 0.3 0.3 2.9 2.0 0.0 100.0 +shuffle +hot
1024k 128 8192 0.4 0.4 2.3 1.2 0.0 0.0
512k 16 32768 0.3 0.3 2.3 1.5 0.0 0.0
... (364 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
8k 16 512 0.3 0.3 8.8 6.1 0.0 0.0
8k 16 512 0.3 0.3 0.9 5.8 0.0 99.6 +shuffle
1024k 16 65536 0.3 0.3 2.0 3.8 0.0 100.0 +shuffle
1024k 128 8192 0.4 0.6 2.0 2.9 0.0 99.8 +shuffle
... (364 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
8k 16 512 0.3 0.3 8.8 6.1 0.0 0.0
8k 16 512 0.3 0.3 0.9 5.8 0.0 99.6 +shuffle
1024k 16 65536 0.3 0.3 2.0 3.8 0.0 100.0 +shuffle
1024k 128 8192 0.4 0.6 2.0 2.9 0.0 99.8 +shuffle
... (364 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.3 0.3 2.0 3.8 0.0 100.0 +shuffle
1024k 16 65536 0.3 0.3 1.5 1.8 0.0 100.0 +shuffle +hot
1024k 32 32768 0.3 0.4 1.8 2.9 0.0 100.0 +shuffle
1024k 32 32768 0.3 0.3 2.9 2.0 0.0 100.0 +shuffle +hot
... (364 results following) ...
overall:
worst alloc time: 8.8 (us)
worst free time: 6.1 (us)
average of max. alloc times: 0.9 (us)
average of max. free times: 1.3 (us)
average alloc time: 0.4 (us)
average free time: 0.5 (us)
average overhead: 0.0%
average fragmentation: 40.1%
[SEQUENTIAL ALLOC->FREE, RANDOM BLOCK SIZES] ON 'heapmem'
HEAPSZ test heap size
BLOCKSZ tested block size
NRBLKS number of blocks allocatable in heap
AVG-A average time to allocate block (us)
AVG-F average time to free block (us)
MAX-A max time to allocate block (us)
MAX-F max time to free block (us)
OVRH% overhead
FRAG% external fragmentation
FLAGS +shuffle: randomized free
+hot: measure after initial alloc/free pass (hot heap)
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 290k 3 1.8 1.5 3.8 2.6 14.9 0.0 +shuffle
512k 107k 4 1.0 1.0 2.3 1.5 16.3 50.0 +shuffle
256k 25k 9 0.7 0.8 2.3 1.5 10.2 40.0 +shuffle
1024k 106k 9 0.7 1.1 2.3 2.3 6.7 40.0 +shuffle
... (32764 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
512k 122k 4 0.7 1.6 0.6 3.2 4.6 0.0
512k 4 32768 0.3 0.3 1.8 2.9 75.0 100.0 +shuffle +hot
512k 48k 10 0.5 1.1 0.9 2.9 5.1 40.0 +shuffle
512k 5k 85 0.4 1.1 0.9 2.9 4.4 88.4 +shuffle
... (32764 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 542 1024 0.4 0.6 2.0 0.9 47.1 97.5 +shuffle +hot
1024k 612 1024 0.4 0.6 1.2 1.5 40.2 0.0
512k 4 32768 0.3 0.3 1.8 2.9 75.0 100.0 +shuffle +hot
1024k 678 1024 0.4 0.6 2.0 1.2 33.8 97.1 +shuffle +hot
... (32764 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 1k 682 0.4 0.7 0.9 1.8 0.4 97.1 +shuffle +hot
1024k 3k 256 0.4 1.3 0.9 1.8 0.2 95.3 +shuffle
1024k 3k 292 0.4 1.1 0.6 1.8 2.5 95.2 +shuffle
1024k 4k 227 0.4 0.7 0.9 1.2 3.0 93.9 +shuffle +hot
... (32764 results following) ...
overall:
worst alloc time: 3.8 (us)
worst free time: 3.2 (us)
average of max. alloc times: 0.7 (us)
average of max. free times: 1.1 (us)
average alloc time: 0.6 (us)
average free time: 0.8 (us)
average overhead: 13.9%
average fragmentation: 11.9%
tlsf
=== running memory_tlsf ===
seq_heap_size=2048k
random_alloc_rounds=1024
pattern_heap_size=128k
pattern_check_rounds=128
[SEQUENTIAL ALLOC->FREE, ^2 BLOCK SIZES] ON 'tlsf'
HEAPSZ test heap size
BLOCKSZ tested block size
NRBLKS number of blocks allocatable in heap
AVG-A average time to allocate block (us)
AVG-F average time to free block (us)
MAX-A max time to allocate block (us)
MAX-F max time to free block (us)
OVRH% overhead
FRAG% external fragmentation
FLAGS +shuffle: randomized free
+hot: measure after initial alloc/free pass (hot heap)
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
8k 16 256 0.5 0.4 19.8 2.9 50.0 0.0
1024k 16 32768 0.4 0.4 4.1 2.9 50.0 0.0 +hot
1024k 32 21845 0.4 0.5 3.5 1.5 33.3 99.8 +shuffle
512k 64 6553 0.4 0.5 3.5 0.9 20.0 0.0
... (364 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 32768 0.4 0.4 1.2 4.1 50.0 0.0
8k 16 256 0.5 0.4 19.8 2.9 50.0 0.0
1024k 16 32768 0.4 0.4 4.1 2.9 50.0 0.0 +hot
1024k 16 32768 0.4 0.5 0.9 2.3 50.0 99.9 +shuffle
... (364 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 32768 0.4 0.4 1.2 4.1 50.0 0.0
1024k 16 32768 0.4 0.4 4.1 2.9 50.0 0.0 +hot
1024k 16 32768 0.4 0.5 0.9 2.3 50.0 99.9 +shuffle
1024k 16 32768 0.4 0.4 2.0 1.5 50.0 99.9 +shuffle +hot
... (364 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 1k 1008 0.4 0.4 0.9 0.6 1.6 98.0 +shuffle +hot
1024k 1k 1008 0.4 0.4 1.2 0.9 1.6 97.8 +shuffle
1024k 2k 508 0.4 0.4 0.9 0.9 0.8 96.9 +shuffle
1024k 512 1985 0.4 0.4 1.8 0.9 3.1 98.9 +shuffle
... (364 results following) ...
overall:
worst alloc time: 19.8 (us)
worst free time: 4.1 (us)
average of max. alloc times: 1.0 (us)
average of max. free times: 0.8 (us)
average alloc time: 0.4 (us)
average free time: 0.4 (us)
average overhead: 14.9%
average fragmentation: 38.5%
[SEQUENTIAL ALLOC->FREE, RANDOM BLOCK SIZES] ON 'tlsf'
HEAPSZ test heap size
BLOCKSZ tested block size
NRBLKS number of blocks allocatable in heap
AVG-A average time to allocate block (us)
AVG-F average time to free block (us)
MAX-A max time to allocate block (us)
MAX-F max time to free block (us)
OVRH% overhead
FRAG% external fragmentation
FLAGS +shuffle: randomized free
+hot: measure after initial alloc/free pass (hot heap)
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
16k 3k 4 1.2 0.7 2.6 1.2 9.5 0.0
1024k 444k 2 1.8 0.7 2.3 0.9 13.2 0.0 +shuffle
512k 1k 360 0.4 0.4 2.3 2.0 1.9 97.2 +shuffle
8k 1k 7 0.8 0.4 2.3 0.6 11.7 25.0 +shuffle
... (32764 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
256k 670 381 0.4 0.4 0.9 2.6 2.6 0.0
128k 16k 7 0.5 0.7 0.9 2.3 12.3 25.0 +shuffle +hot
512k 5k 90 0.4 0.4 1.2 2.0 3.2 0.0
256k 445 565 0.4 0.4 0.9 2.0 4.1 97.5 +shuffle
... (32764 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 504k 1 1.5 0.6 0.9 0.6 50.8 0.0
1024k 504k 1 1.8 0.6 0.9 0.6 50.8 0.0 +shuffle
1024k 504k 1 1.2 0.3 0.6 0.3 50.8 0.0 +shuffle +hot
1024k 504k 1 1.2 0.6 0.6 0.6 50.8 0.0
... (32764 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 1k 829 0.4 0.4 1.2 0.9 1.8 98.1 +shuffle
1024k 1k 809 0.4 0.4 0.9 2.0 2.1 97.8 +shuffle
1024k 1k 550 0.4 0.4 0.9 0.6 1.4 97.1 +shuffle +hot
1024k 1k 829 0.4 0.4 0.9 0.9 2.4 97.8 +shuffle
... (32764 results following) ...
overall:
worst alloc time: 2.6 (us)
worst free time: 2.6 (us)
average of max. alloc times: 0.8 (us)
average of max. free times: 0.6 (us)
average alloc time: 0.7 (us)
average free time: 0.5 (us)
average overhead: 12.7%
average fragmentation: 13.7%
pshared
pshared用于進程間共享對象內存分配,由配置項--enable-pshared配置,和--with-localmem決定:
-
如果配置了
--enable-pshared=enable,則使用xnheap(即pshared)。 -
如果配置了
--enable-pshared=disable,則根據--with-localmem的配置選擇:a. 如果
--with-localmem=tlsf,則使用tlsf。b. 如果
--with-localmem=heapmem,則使用heapmem。c. 如果未配置
--with-localmem,則使用glibc malloc/free接口。
這里測試環境--enable-pshared=enable,所以其實際測試的是xnheap
=== running memory_pshared ===
seq_heap_size=2048k
random_alloc_rounds=1024
pattern_heap_size=128k
pattern_check_rounds=128
[SEQUENTIAL ALLOC->FREE, ^2 BLOCK SIZES] ON 'pshared'
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.3 0.4 3.5 38.5 0.0 0.0
8k 16 512 0.3 0.4 3.2 19.0 0.0 0.0
1024k 2k 512 0.4 0.6 2.0 1.2 0.0 96.9 +shuffle +hot
1024k 1k 1024 0.4 0.6 2.0 1.5 0.0 98.2 +shuffle +hot
... (364 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.3 0.4 3.5 38.5 0.0 0.0
512k 16 32768 0.3 0.4 1.2 36.5 0.0 0.0
256k 16 16384 0.3 0.4 1.5 26.5 0.0 0.0
128k 16 8192 0.3 0.4 0.9 19.8 0.0 0.0
... (364 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.3 0.4 3.5 38.5 0.0 0.0
512k 16 32768 0.3 0.4 1.2 36.5 0.0 0.0
256k 16 16384 0.3 0.4 1.5 26.5 0.0 0.0
128k 16 8192 0.3 0.4 0.9 19.8 0.0 0.0
... (364 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 16 65536 0.3 0.4 2.0 4.1 0.0 100.0 +shuffle
1024k 16 65536 0.3 0.3 1.8 2.0 0.0 100.0 +shuffle +hot
1024k 32 32768 0.3 0.4 1.8 3.2 0.0 100.0 +shuffle
1024k 32 32768 0.3 0.3 2.0 1.8 0.0 100.0 +shuffle +hot
... (364 results following) ...
overall:
worst alloc time: 3.5 (us)
worst free time: 38.5 (us)
average of max. alloc times: 1.0 (us)
average of max. free times: 1.8 (us)
average alloc time: 0.4 (us)
average free time: 0.6 (us)
average overhead: 0.0%
average fragmentation: 39.4%
[SEQUENTIAL ALLOC->FREE, RANDOM BLOCK SIZES] ON 'pshared'
sorted by: max alloc time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 139k 7 1.0 1.1 2.3 1.5 4.7 50.0 +shuffle
1024k 13k 75 0.5 0.8 2.3 1.5 3.5 0.0
1024k 76k 13 0.6 0.8 2.3 1.5 2.5 0.0
1024k 427k 2 1.8 1.5 2.3 1.8 16.5 0.0 +shuffle
... (32764 results following) ...
sorted by: max free time
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
512k 15 32768 0.3 0.3 0.9 3.5 6.2 100.0 +shuffle
64k 21k 2 1.2 2.3 0.9 3.2 33.3 0.0
1024k 333k 3 1.1 1.8 1.5 3.2 2.3 0.0
32k 5k 5 0.8 1.6 1.2 3.2 15.4 0.0
... (32764 results following) ...
sorted by: max overhead
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 82 8192 0.4 0.4 2.0 1.2 35.9 0.0 +hot
1024k 338 2048 0.4 0.7 2.0 1.5 34.0 0.0
1024k 341k 2 0.9 0.9 0.9 1.2 33.4 0.0 +hot
1024k 341k 2 1.0 1.0 0.9 1.2 33.4 0.0 +shuffle +hot
... (32764 results following) ...
sorted by: max fragmentation
HEAPSZ BLOCKSZ NRBLKS AVG-A AVG-F MAX-A MAX-F OVRH% FRAG% FLAGS
1024k 1k 682 0.4 1.3 1.8 2.0 3.7 97.7 +shuffle
1024k 1k 682 0.4 0.7 0.9 1.5 4.5 97.7 +shuffle +hot
1024k 2k 409 0.4 0.7 0.9 1.5 3.5 96.1 +shuffle +hot
1024k 7k 128 0.4 0.7 1.5 1.2 0.4 92.2 +shuffle +hot
... (32764 results following) ...
overall:
worst alloc time: 2.3 (us)
worst free time: 3.5 (us)
average of max. alloc times: 0.8 (us)
average of max. free times: 1.2 (us)
average alloc time: 0.7 (us)
average free time: 0.9 (us)
average overhead: 13.9%
average fragmentation: 11.9%

在實時應用場景中,內存分配的確定性、時效性與可靠性直接影響系統的實時性能 —— 尤其是在工業自動化、機器人控制、航空航天等對響應時間有嚴苛要求的領域,傳統通用內存分配器(如 glibc 的 malloc)因存在不可預測的延遲和碎片問題,往往難以滿足實時性需求。Xenomai 作為一款成熟的實時操作系統框架,針對實時環境的特殊需求,提供了一系列經過工業級場景長期驗證的動態內存分配器,本文介紹它們。
浙公網安備 33010602011771號