2011年3月21日 星期一

ns節省記憶體的小技巧

.

在ns user manual 24.4.2中介紹了一些節省記憶體的小技巧:

  1. 避免trace-all:$ns trace-all $f會使得trace objects被塞到所有links,若你只想trace一個link,這個overhead是可以避免的,每個link約可節省14KB
  2. 在使用系列的var時,盡量用array:ns中的每個var都有overhead,如set n$i [$ns node]中的n$i(就像n1, n2, n3, ...),若需要系列的變數時,使用array取代,如n($si)(就像n(1), n(2), n(3), ...),那麼其實只有一個變數會被使用(即n),每個變數可以節省40+Btye
  3. 避免宣告不需要的變數:若一個物件在後面不會再用到,那麼避免為該物件取名,每個變數可省約80B,如下列會為CtrMcast的物件指定一個變數:cmcast(1),但這其實是不必要的set cmcast(1) [new CtrMcast $ns $n(1) $ctrmcastcomp [list 1 1]]
    可以代換成:
    new CtrMcast $ns $n(1) $ctrmcastcomp [list 1 1]
  4. 在FreeBSD上run:在FreeBSD上malloc的overhead比其他的OS小,ns2的作者們最後還是會將FreeBSD的allocator port到其他平台
  5. Dynamic binding: 在C++中使用bind()會使你創建的每個物件都消耗記憶體,若你創建許多相同的物件時,使用bind的代價太高。將bind()代換成delay_bind(),使得所需要的記憶體降到per-class level而非每個物件(我個人的解讀時,bind會使得所有物件都用到它,不管需不需要,就像global variable的宣告,而delay_bind則是當宣告的物件有用到它時,也就是宣告的物件為該class時,這時才真的宣告=>這時才秏記憶體),從ns/object.cc參考如何binding的範例
  6. disabling packet header:在大量packet的simulation中,將你simulation中不會用到的packet header取消將會大量的節省記憶體(ns default是將所有packet header打開
    可在宣告ns前(set ns [new Simulator])輸入:
    remove-all-packet-headers,將所有packet header關閉(在ns宣告前輸入才有用)
    再將需要的packet header加回來,如IP、TCP:
    add-packet-header IP TCP Flags

原文如下:

Some tips to saving memory (some of these use examples from the cmcast-100.tcl script): If you have many links or nodes:
  1. Avoid trace-all : $ns trace-all $f causes trace objects to be pushed on all links. If you only want to trace one link, there’s no need for this overhead. Saving is about 14 KB/link.
  2. Use arrays for sequences of variables : Each variable, say n$i in set n$i [$ns node], has a certain overhead. If a sequence of nodes are created as an array, i.e. n($i), then only one variable is created, consuming much less memory. Saving is about 40+ Byte/variable.
  3. Avoid unnecessary variables : If an object will not be referred to later on, avoid naming the object. E.g. set cmcast(1) [new CtrMcast $ns $n(1) $ctrmcastcomp [list 1 1]] would be better if replaced by new CtrMcast $ns $n(1) $ctrmcastcomp [list 1 1]. Saving is about 80 Byte/variable.
  4. Run on top of FreeBSD : malloc() overhead on FreeBSD is less than on some other systems. We will eventually port that allocator to other platofrms.
  5. Dynamic binding : Using bind() in C++ consumes memory for each object you create. This approach can be very expensive if you create many identical objects. Changing bind() to delay_bind() changes this memory requirement to per-class. See ns/object.cc for an example of how to do binding, either way.
  6. Disabling packet headers : For packet-intensive simulations, disabling all packet headers that you will not use in your simulation may significantly reduce memory usage. See Section 12.1 for detail.


.

沒有留言: