Modify Trace File on ns-2

to reduce your trace file size or clear what you want to to see or analyse trace file easily first of all you should learn how to rerad trace file then you can modify your trace file as follows.

Modified folders: ns-2/trace/, cmu-trace.h

1.Add new format type in cmu-trace.h.
class CMUTrace : public Trace {

void format_x(Packet *p, int offset);


2. Defined new format type in

void CMUTrace::format_x(Packet *p, int offset)
struct hdr_ip *ih = HDR_IP(p);
hdr_iett *hdr = hdr_iett::access(p);
double x = 0.0, y = 0.0, z = 0.0;
char op = (char) type_;
node_->getLoc(&x, &y, &z);
FILE * pFile;
int src = Address::instance().get_nodeaddr(ih->saddr());
pFile = fopen (“”,”a”);//if you want to have net values you should save the current .tr and .nam //thats why you should geberate new file to save your trace file lines…

if (op == ‘r’ && src!=src_)
switch (hdr->x_pt)

case xxxxx:

fprintf (pFile, “%.9f %d %d %s\n”,Scheduler::instance().clock(), // packet reception time
src, // packet src
src_, // packet receiver
x_packet_type_names[hdr->x_pt] //packet type

default : {;}

fclose (pFile);

If you get an error “Segmentation fault” pls check your file read and write operation…

If you get “Floating point exception” pls check your numerical values. Maybe you missed somthing in your rpogram.
For example; may be you are trying to divide your number to the ZERO
may be you are trying to generate a random index from your array even it is not get any value in it.


53 thoughts on “Modify Trace File on ns-2

    1. hdr_iett is just a packet name… you can’t find because we generated it…It means you can generate new type of packet pr packet header..

  1. dear sir i try to using emulation facility in NS-2,
    but i got error “Floating point exception” .Based from your blog,
    Can you explain to me where i’ve to check numerical values, in my tcl simulation script or the others folder in ns2?
    i’m using ns2 version 2.33 and i try to implements greedy perimeter stateless routing (gpsr) .Thanks for your attention.

    1. Floating point exception means you are trying to devide something to zero… but it is not exact answer just such an example of getting that error message.
      In ns-2 there is no any (my observation) division problem just you chech your calculation for example maybe you are trying to find a number in your array even if your array is empty this is just an example i couldnt know what is the exact answer but YOU SHOULD CHECHK YOUR CALCULATION debug your code…

  2. Hello Sir,

    I don’t want the new format. I just want to do some changes in the current format. Thats it.
    So, how could it be done?

  3. dear sir, i have one entry in packet cmn header that is channelindex_, i want to display that value in trace file too. how can i display that?

      1. just you will print the value which you changed….if this value is used then it will be available to print also. just call that value where you want to use….

  4. Hi There,

    I have ns-2.29 installed on my pc i want to analyse energy parameters on the aodv and dsr routing protocols using awk scripts.
    My tcl file is giving me errors anytime i run them,have you pls got any tcl files that incorporates energy parameters and also how to go abt the analysis. do you think i need to upgrade the version of my ns-2 as i think most of my errors are compatability issues as im using vista.
    Thanks and ope to hear frm u soon.

  5. Sir,
    I am facing of problem of floating point exception in modified file.please let me know how to handle it.

    1. I also faced that problem… I found solution as checking my calculations and I realized that my array doesnt have any value and I my calculation try to use for division. thats why you should check your calculation. maybe you try to divide some number to zero or something like that.
      because ns-2 doesnt have any problem about calculation…
      (at least thats was my problem because this error mesaage like error no meaning )

  6. i am using ns 2.29
    and i want to know the trace file format and how to read it to extract information from it using awk
    s 0.505582352 _5_ RTR — 0 AODV 237 [0 ffffffff 2 800] ——- [5:255 -1:255 29 0] [0x2 2 1 [4 0] [2 4]] (REQUEST)
    s 0.506864067 _3_ RTR — 0 AODV 237 [0 ffffffff 2 800] ——- [3:255 -1:255 29 0] [0x2 2 1 [4 0] [2 4]] (REQUEST)
    r 0.508346725 _2_ RTR — 0 AODV 196 [13a 2 4 800] ——- [4:255 2:255 30 2] [0x4 1 [4 4] 10.000000] (REPLY)
    s 0.508346725 _2_ RTR — 0 cbr 520 [0 0 0 0] ——- [2:0 4:0 30 4] [0] 0 0
    s 0.510000000 _2_ AGT — 2 cbr 500 [0 0 0 0] ——- [2:0 4:0 32 0] [2] 0 0
    r 0.510000000 _2_ RTR — 2 cbr 500 [0 0 0 0] ——- [2:0 4:0 32 0] [2] 0 0
    s 0.510000000 _2_ RTR — 2 cbr 520 [0 0 0 0] ——- [2:0 4:0 30 4] [2] 0 0
    what each field means.
    please reply me as soon as possible.

      1. hello, i am new on ns2. can u tell me plz how to turn on energy trace in tcl file and how to open that energy trace file.

      2. Energy should be shown on the current trace file. U need to read ns2 manual … There a section there explain this…

  7. hello sir.
    i want to calculate energy consumed by the nodes……so i m in need of awk file for energy..pls do the needful……..

  8. Hi
    if i want to send my own traffic ,I mean NS2 read the content of a file and send them ,like a log if video ,Do you know what should I do ??

  9. Hi
    if i want to send my own traffic ,I mean NS2 read the content of a file and send them ,like a log if video ,Do you know what should I do ??

  10. hello sir……………….
    i am calculating throughput and delay for AODV tcl script……………but am unable to verify whether the generated output is correct or not……………….pls help me

  11. hi my dear friend
    i have many question in ns.

    1) how i can simulate a webserver (how code or command or class & objects?)

    2) how i can install gnuplot?
    – how i can show the output analyze file, in gnuplot? (how code or command?)

    3) if i use SCTP Protocol&Agent,how should i do? (how code or command?)

    very thanks…

  12. Hi i’m new user to ns2 and i want to piggyback or combine two packets at a time and then send those packets in the multicast networks. Is it possible by ns2?

  13. Dear Sir
    I wanted to know how can I run a ns2 project if I have common, mac, tcl, tcl and trace folders in a project with their respectictive codes in a certain project.
    Thank you.

  14. hi sir
    i too need awk file for energy consumption for AODV and me how to view enerrgy information in old trace format..

  15. sir i want to change my trace file in this format “s -t 1.000000000 -Hs 0 -Hd -2 -Ni 0 -Nx 252.64 -Ny 235.76 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw — -Ma 0 -Md 0 -Ms 0
    -Mt 0 -Is 0.0 -Id 1.0 -It cbr -Il 512 -If 0 -Ii 0 -Iv 32 -Pn cbr -Pi 0 -Pf 0 -Po 4” How could it possible where i need to change please explain me ……….

  16. Hi,
    i’ve simulated a flat wsn of 5 nodes with mobility ( mobility pattern is generated using bonnmotion tool) and sink is placed in the middle of the sensing field of 100x100m. the trace output shows that all the application level packets get dropped and 0 packets received at the sink. the reason of packet drop is either ARP or NRTE, trace output also shows the exact movement of sensor nodes as provided. please guide whats wrong with the simulation that sink is not receiving any data.

  17. Hi,
    I used NS2.34 in linux, I work with a ring topology traffic multicast. In the tcl code I used join/leave, in the trace file there are prune.
    The problem is that I can not remove the prune in the trace file. Please help me if possible to remove the prune.
    Thank you for your help

    1. In order to have custom trace file there are some setting change or commands. I dont remeber exactly whic one the solution but you can change the trrace file library and rebuild it again. Please read ns-2 document Tracing section

  18. Respected,
    I want to utilize energy trace from old trace format of sensor node, but unable to get it.
    So will you pls provide me some guidance regarding the same.

  19. hey hilal ,,hope u r fine :)..
    i tried to modify ns2 energy model , i added life time function, that calculate life time of specific node at specific time ..then i tried to print lifetime value in my trace file (in case of using energy model).. to get the following output in my trace file ..” [energy ei es et er lifetime]”
    1- in file (line 345) ,i changed sprintf command line to be as the following ”
    sprintf(pt_->buffer() + offset,
    “[energy %f ei %.3f es %.3f et %.3f er %.3f lifetime %f] “,
    2- i recompiled ns2 using make command and i did’t get any errors..

    MY PROBLEM IS : when i simulate a simple script using energy model ..lifetime doesn’t appear in my trace file ..& i don’t know the reason of this .. do i have to change something else for example? ..
    hope that u can help me in this situation..

    Dalia Elewely

    1. Hi, if u read ns2 pdf it should tell what u need to do, also u can read the post related with the trace file. I do not have comp currently for this reason i couldnt provide the elaborated answer 😦

  20. Hi mam,
    I’m working on aodv routing protocol. Initially, in network deployment phase every node should broadcast packets for a certain period of time. This should be done before aodv routing protocol starts. Mam how can i get all broadcasting details in trace file. Mam i need your help. Waiting for your replay.


    1. When u do tracing all the data are automaticlly stored in the trace file.,, of course after u run nam….
      As an advice please try extremely simple
      Examples before u dive into…. Enty of examples in ns2 example folder….

  21. hello hilalkaratoy,
    I need your help, I’m trying to evaluate energy for DSR,DSDV and AODV respectively. I’m having problem with DSDV, when I play in nam I can not the traffic flow between senders and destination therefore in the trace file there are alot of drop packets. do you have any idea what is wrong?
    FYI: using same scenario for others was fine


  22. I’m learning ns2 and I got the error “floating point exception” when I am trying to simulate
    ” simple-wireless.tcl” which is an example located into [ns2.34/tcl/ex folder] and dont change anything of that file.

    Would you plz help me, what type of issues may cause this problem.


    1. Do u provide any input? Else print the internal parameter and see which parameter causes this error, also u can pick up another example or simplfy the current one…
      I run the example tcl files they were fine…

  23. Hi, I m simulating leach protocol to find the movement of node’s if any using getvel() function. I m getting floating point exception error during tcl script execution. I’ve not used any function except the methods available on the web for finding node’s position, energy and velocity during simulation. Please guide how to resolve this issue?

  24. Hi
    I going to add 1 octet to my paquet (size=512) so means that in this ligne : $cbr set packetSize_ 512 : add 8 to 512 or what ?????

    1. There could be
      loop somewhere, or
      time you set could be more than you think, or
      there is another node communicating with it or
      you have mistaken to read the trace file

  25. Hello Madam, I am getting folowing error.

    trace/cmu-trace.o: In function `hdr_song::access(Packet const*)’:[hdr_song::access(Packet const*)]+0x7): undefined reference to `hdr_song::offset_’
    collect2: ld returned 1 exit status
    make: *** [ns] Error 1

    My protocol name is song.
    Pl help

  26. I’m doing ns-2 project on “Detection and localization of spoofing attackers in wireless network”.so for detection of attackers we assigned unique IP address to each node in tcl scrip using command, tb-set-ip $node1 but how i can display that id in .tr file in the field of source node and destination node…i’m waiting for ur reply..

  27. Please can any body help me regarding the computation of energy consumed incurred/caused by an execution of a particular function or algorithm in NS-2

  28. Hi freinds,
    Please help me, i search awk script for to calculate energy dissipation ,throughput, PDR and end to end delay from and is like :
    M 0.0 nn 101 x 100 y 100 rp leach
    M 0.0 sc mit/uAMPS/sims/nodescen.tcl cp none seed 0.0
    M 0.0 prop Propagation/TwoRayGround ant Antenna/OmniAntenna
    s 0.000051243 _79_ AGT — 0 rca 2 [0 4f000000 0 0] ——- [A 79 -1 -1]
    s 0.000071732 _69_ AGT — 1 rca 2 [0 45000000 0 0] ——- [A 69 -1 -1]
    s 0.000116488 _35_ AGT — 2 rca 2 [0 23000000 0 0] ——- [A 35 -1 -1]
    s 0.000147102 _49_ AGT — 3 rca 2 [0 31000000 0 0] ——- [A 49 -1 -1]
    s 0.000194488 _53_ AGT — 4 rca 2 [0 35000000 0 0] ——- [A 53 -1 -1]
    r 0.000277263 _82_ AGT — 0 rca 2 [0 4f000000 ffff0008 0] ——- [A 79 -1 -1]
    r 0.000277263 _90_ AGT — 0 rca 2 [0 4f000000 ffff0008 0] ——- [A 79 -1 -1]
    if you have please help me. Please send some sample codes to my email
    Thank a lot!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s